pkg/proc,service/debugger: fix debuginfod-find source (#3762)

Fixes bug where the incorrect Build ID could be used to try and download the source for a binary. This is because the Build ID was stored on the BinInfo object for a target and not the image itself.
This commit is contained in:
Derek Parker 2024-06-28 00:15:37 -07:00 committed by GitHub
parent a4196f35a9
commit b9f50fe9b0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 10 deletions

@ -53,9 +53,6 @@ type BinaryInfo struct {
DebugInfoDirectories []string DebugInfoDirectories []string
// BuildID of this binary.
BuildID string
// Functions is a list of all DW_TAG_subprogram entries in debug_info, sorted by entry point // Functions is a list of all DW_TAG_subprogram entries in debug_info, sorted by entry point
Functions []Function Functions []Function
// Sources is a list of all source files found in debug_line. // Sources is a list of all source files found in debug_line.
@ -952,6 +949,7 @@ func (bi *BinaryInfo) PCToImage(pc uint64) *Image {
type Image struct { type Image struct {
Path string Path string
StaticBase uint64 StaticBase uint64
BuildID string
addr uint64 addr uint64
index int // index of this object in BinaryInfo.SharedObjects index int // index of this object in BinaryInfo.SharedObjects
@ -1437,10 +1435,10 @@ func (bi *BinaryInfo) openSeparateDebugInfo(image *Image, exe *elf.File, debugIn
} }
} }
if debugFilePath == "" && len(bi.BuildID) > 2 { if debugFilePath == "" && len(image.BuildID) > 2 {
// Build ID method: look for a file named .build-id/nn/nnnnnnnn.debug in // Build ID method: look for a file named .build-id/nn/nnnnnnnn.debug in
// every debug info directory. // every debug info directory.
find(nil, fmt.Sprintf(".build-id/%s/%s.debug", bi.BuildID[:2], bi.BuildID[2:])) find(nil, fmt.Sprintf(".build-id/%s/%s.debug", image.BuildID[:2], image.BuildID[2:]))
} }
if debugFilePath == "" { if debugFilePath == "" {
@ -1478,11 +1476,11 @@ func (bi *BinaryInfo) openSeparateDebugInfo(image *Image, exe *elf.File, debugIn
} }
} }
if debugFilePath == "" && len(bi.BuildID) > 2 { if debugFilePath == "" && len(image.BuildID) > 2 {
// Previous versions of delve looked for the build id in every debug info // Previous versions of delve looked for the build id in every debug info
// directory that contained the build-id substring. This behavior deviates // directory that contained the build-id substring. This behavior deviates
// from the ones specified by GDB but we keep it for backwards compatibility. // from the ones specified by GDB but we keep it for backwards compatibility.
find(func(dir string) bool { return strings.Contains(dir, "build-id") }, fmt.Sprintf("%s/%s.debug", bi.BuildID[:2], bi.BuildID[2:])) find(func(dir string) bool { return strings.Contains(dir, "build-id") }, fmt.Sprintf("%s/%s.debug", image.BuildID[:2], image.BuildID[2:]))
} }
if debugFilePath == "" { if debugFilePath == "" {
@ -1497,7 +1495,7 @@ func (bi *BinaryInfo) openSeparateDebugInfo(image *Image, exe *elf.File, debugIn
// has debuginfod so that we can use that in order to find any relevant debug information. // has debuginfod so that we can use that in order to find any relevant debug information.
if debugFilePath == "" { if debugFilePath == "" {
var err error var err error
debugFilePath, err = debuginfod.GetDebuginfo(bi.BuildID) debugFilePath, err = debuginfod.GetDebuginfo(image.BuildID)
if err != nil { if err != nil {
return nil, nil, ErrNoDebugInfoFound return nil, nil, ErrNoDebugInfoFound
} }
@ -1708,7 +1706,7 @@ func (bi *BinaryInfo) loadBuildID(image *Image, file *elf.File) {
bi.logger.Warnf("can't read build-id desc: %v", err) bi.logger.Warnf("can't read build-id desc: %v", err)
return return
} }
bi.BuildID = hex.EncodeToString(descBinary) image.BuildID = hex.EncodeToString(descBinary)
} }
func (bi *BinaryInfo) getDebugLink(exe *elf.File) (debugLink string, crc uint32) { func (bi *BinaryInfo) getDebugLink(exe *elf.File) (debugLink string, crc uint32) {

@ -2346,7 +2346,12 @@ func (d *Debugger) TargetGroup() *proc.TargetGroup {
} }
func (d *Debugger) BuildID() string { func (d *Debugger) BuildID() string {
return d.target.Selected.BinInfo().BuildID loc, err := d.target.Selected.CurrentThread().Location()
if err != nil {
return ""
}
img := d.target.Selected.BinInfo().PCToImage(loc.PC)
return img.BuildID
} }
func (d *Debugger) AttachPid() int { func (d *Debugger) AttachPid() int {