Skip to content

Commit

Permalink
refactor: move loadModuleData from runtimeTypeToDIE and expose the ap…
Browse files Browse the repository at this point in the history
…is (#3741)
  • Loading branch information
jayantxie committed Jun 25, 2024
1 parent a8293a3 commit a4196f3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pkg/proc/bininfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ func (bi *BinaryInfo) AddImage(path string, addr uint64) error {
}

// moduleDataToImage finds the image corresponding to the given module data object.
func (bi *BinaryInfo) moduleDataToImage(md *moduleData) *Image {
func (bi *BinaryInfo) moduleDataToImage(md *ModuleData) *Image {
fn := bi.PCToFunc(md.text)
if fn != nil {
return bi.funcToImage(fn)
Expand All @@ -1047,7 +1047,7 @@ func (bi *BinaryInfo) moduleDataToImage(md *moduleData) *Image {
}

// imageToModuleData finds the module data in mds corresponding to the given image.
func (bi *BinaryInfo) imageToModuleData(image *Image, mds []moduleData) *moduleData {
func (bi *BinaryInfo) imageToModuleData(image *Image, mds []ModuleData) *ModuleData {
for _, md := range mds {
im2 := bi.moduleDataToImage(&md)
if im2 != nil && im2.index == image.index {
Expand Down
12 changes: 6 additions & 6 deletions pkg/proc/moduledata.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package proc

// delve counterpart to runtime.moduledata
type moduleData struct {
// ModuleData counterpart to runtime.moduleData
type ModuleData struct {
text, etext uint64
types, etypes uint64
typemapVar *Variable
}

func loadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]moduleData, error) {
func LoadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]ModuleData, error) {
// +rtype -var firstmoduledata moduledata
// +rtype -field moduledata.text uintptr
// +rtype -field moduledata.types uintptr
Expand All @@ -19,7 +19,7 @@ func loadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]moduleData, error)
return nil, err
}

r := []moduleData{}
r := []ModuleData{}

for md.Addr != 0 {
const (
Expand Down Expand Up @@ -51,7 +51,7 @@ func loadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]moduleData, error)
return ret
}

r = append(r, moduleData{
r = append(r, ModuleData{
types: touint(typesField), etypes: touint(etypesField),
text: touint(textField), etext: touint(etextField),
typemapVar: vars[typemapField],
Expand All @@ -69,7 +69,7 @@ func loadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]moduleData, error)
return r, nil
}

func findModuleDataForType(bi *BinaryInfo, mds []moduleData, typeAddr uint64, mem MemoryReadWriter) *moduleData {
func findModuleDataForType(bi *BinaryInfo, mds []ModuleData, typeAddr uint64, mem MemoryReadWriter) *ModuleData {
for i := range mds {
if typeAddr >= mds[i].types && typeAddr < mds[i].etypes {
return &mds[i]
Expand Down
18 changes: 9 additions & 9 deletions pkg/proc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (ctxt *loadDebugInfoMapsContext) lookupAbstractOrigin(bi *BinaryInfo, off d
return r
}

// runtimeTypeToDIE returns the DIE corresponding to the runtime._type.
// RuntimeTypeToDIE returns the DIE corresponding to the runtime._type.
// This is done in three different ways depending on the version of go.
// - Before go1.7 the type name is retrieved directly from the runtime._type
// and looked up in debug_info
Expand All @@ -98,18 +98,13 @@ func (ctxt *loadDebugInfoMapsContext) lookupAbstractOrigin(bi *BinaryInfo, off d
// debug_info
// - After go1.11 the runtimeTypeToDIE map is used to look up the address of
// the type and map it directly to a DIE.
func runtimeTypeToDIE(_type *Variable, dataAddr uint64) (typ godwarf.Type, kind int64, err error) {
func RuntimeTypeToDIE(_type *Variable, dataAddr uint64, mds []ModuleData) (typ godwarf.Type, kind int64, err error) {
bi := _type.bi

_type = _type.maybeDereference()

// go 1.11 implementation: use extended attribute in debug_info

mds, err := loadModuleData(bi, _type.mem)
if err != nil {
return nil, 0, fmt.Errorf("error loading module data: %v", err)
}

md := findModuleDataForType(bi, mds, _type.Addr, _type.mem)
if md != nil {
so := bi.moduleDataToImage(md)
Expand Down Expand Up @@ -154,7 +149,12 @@ func resolveParametricType(bi *BinaryInfo, mem MemoryReadWriter, t godwarf.Type,
}
_type := newVariable("", rtypeAddr, runtimeType, bi, mem)

typ, _, err := runtimeTypeToDIE(_type, 0)
mds, err := LoadModuleData(bi, _type.mem)
if err != nil {
return ptyp.TypedefType.Type, fmt.Errorf("error loading module data: %v", err)
}

typ, _, err := RuntimeTypeToDIE(_type, 0, mds)
if err != nil {
return ptyp.TypedefType.Type, err
}
Expand All @@ -175,7 +175,7 @@ func dwarfToRuntimeType(bi *BinaryInfo, mem MemoryReadWriter, typ godwarf.Type)
return 0, 0, false, nil
}

mds, err := loadModuleData(bi, mem)
mds, err := LoadModuleData(bi, mem)
if err != nil {
return 0, 0, false, err
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/proc/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -2337,7 +2337,13 @@ func (v *Variable) loadInterface(recurseLevel int, loadData bool, cfg LoadConfig
return
}

typ, kind, err := runtimeTypeToDIE(_type, data.Addr)
mds, err := LoadModuleData(_type.bi, _type.mem)
if err != nil {
v.Unreadable = fmt.Errorf("error loading module data: %v", err)
return
}

typ, kind, err := RuntimeTypeToDIE(_type, data.Addr, mds)
if err != nil {
v.Unreadable = err
return
Expand Down

0 comments on commit a4196f3

Please sign in to comment.