refactor: move loadModuleData from runtimeTypeToDIE and expose the apis (#3741)

This commit is contained in:
Jayant 2024-06-25 23:03:28 +08:00 committed by GitHub
parent a8293a36f5
commit a4196f35a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 18 deletions

@ -1028,7 +1028,7 @@ func (bi *BinaryInfo) AddImage(path string, addr uint64) error {
} }
// moduleDataToImage finds the image corresponding to the given module data object. // 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) fn := bi.PCToFunc(md.text)
if fn != nil { if fn != nil {
return bi.funcToImage(fn) return bi.funcToImage(fn)
@ -1047,7 +1047,7 @@ func (bi *BinaryInfo) moduleDataToImage(md *moduleData) *Image {
} }
// imageToModuleData finds the module data in mds corresponding to the given 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 { for _, md := range mds {
im2 := bi.moduleDataToImage(&md) im2 := bi.moduleDataToImage(&md)
if im2 != nil && im2.index == image.index { if im2 != nil && im2.index == image.index {

@ -1,13 +1,13 @@
package proc package proc
// delve counterpart to runtime.moduledata // ModuleData counterpart to runtime.moduleData
type moduleData struct { type ModuleData struct {
text, etext uint64 text, etext uint64
types, etypes uint64 types, etypes uint64
typemapVar *Variable typemapVar *Variable
} }
func loadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]moduleData, error) { func LoadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]ModuleData, error) {
// +rtype -var firstmoduledata moduledata // +rtype -var firstmoduledata moduledata
// +rtype -field moduledata.text uintptr // +rtype -field moduledata.text uintptr
// +rtype -field moduledata.types uintptr // +rtype -field moduledata.types uintptr
@ -19,7 +19,7 @@ func loadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]moduleData, error)
return nil, err return nil, err
} }
r := []moduleData{} r := []ModuleData{}
for md.Addr != 0 { for md.Addr != 0 {
const ( const (
@ -51,7 +51,7 @@ func loadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]moduleData, error)
return ret return ret
} }
r = append(r, moduleData{ r = append(r, ModuleData{
types: touint(typesField), etypes: touint(etypesField), types: touint(typesField), etypes: touint(etypesField),
text: touint(textField), etext: touint(etextField), text: touint(textField), etext: touint(etextField),
typemapVar: vars[typemapField], typemapVar: vars[typemapField],
@ -69,7 +69,7 @@ func loadModuleData(bi *BinaryInfo, mem MemoryReadWriter) ([]moduleData, error)
return r, nil 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 { for i := range mds {
if typeAddr >= mds[i].types && typeAddr < mds[i].etypes { if typeAddr >= mds[i].types && typeAddr < mds[i].etypes {
return &mds[i] return &mds[i]

@ -89,7 +89,7 @@ func (ctxt *loadDebugInfoMapsContext) lookupAbstractOrigin(bi *BinaryInfo, off d
return r 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. // 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 // - Before go1.7 the type name is retrieved directly from the runtime._type
// and looked up in debug_info // and looked up in debug_info
@ -98,18 +98,13 @@ func (ctxt *loadDebugInfoMapsContext) lookupAbstractOrigin(bi *BinaryInfo, off d
// debug_info // debug_info
// - After go1.11 the runtimeTypeToDIE map is used to look up the address of // - After go1.11 the runtimeTypeToDIE map is used to look up the address of
// the type and map it directly to a DIE. // 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 bi := _type.bi
_type = _type.maybeDereference() _type = _type.maybeDereference()
// go 1.11 implementation: use extended attribute in debug_info // 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) md := findModuleDataForType(bi, mds, _type.Addr, _type.mem)
if md != nil { if md != nil {
so := bi.moduleDataToImage(md) so := bi.moduleDataToImage(md)
@ -154,7 +149,12 @@ func resolveParametricType(bi *BinaryInfo, mem MemoryReadWriter, t godwarf.Type,
} }
_type := newVariable("", rtypeAddr, runtimeType, bi, mem) _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 { if err != nil {
return ptyp.TypedefType.Type, err return ptyp.TypedefType.Type, err
} }
@ -175,7 +175,7 @@ func dwarfToRuntimeType(bi *BinaryInfo, mem MemoryReadWriter, typ godwarf.Type)
return 0, 0, false, nil return 0, 0, false, nil
} }
mds, err := loadModuleData(bi, mem) mds, err := LoadModuleData(bi, mem)
if err != nil { if err != nil {
return 0, 0, false, err return 0, 0, false, err
} }

@ -2337,7 +2337,13 @@ func (v *Variable) loadInterface(recurseLevel int, loadData bool, cfg LoadConfig
return 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 { if err != nil {
v.Unreadable = err v.Unreadable = err
return return