Skip to content

Commit

Permalink
proc: correctly set G struct offset for 1.11
Browse files Browse the repository at this point in the history
The offset of G changed in go1.11 from 0x8a0 to 0x30. See:
golang/go#23617
  • Loading branch information
aarzilli committed May 24, 2018
1 parent 8952664 commit 3835257
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
17 changes: 16 additions & 1 deletion pkg/goversion/go_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,26 @@ func Installed() (GoVersion, bool) {
}

// RuntimeAfterOrEqual checks that the runtime version is major.minor or a
// later version or a development version.
// later version, or a development version.
func RuntimeAfterOrEqual(major, minor int) bool {
ver, _ := Parse(runtime.Version())
if ver.IsDevel() {
return true
}
return ver.AfterOrEqual(GoVersion{major, minor, -1, 0, 0, ""})
}

const producerVersionPrefix = "Go cmd/compile "

// ProducerAfterOrEqual checks that the DW_AT_producer version is
// major.minor or a later version, or a development version.
func ProducerAfterOrEqual(producer string, major, minor int) bool {
if strings.HasPrefix(producer, producerVersionPrefix) {
producer = producer[len(producerVersionPrefix):]
}
ver, _ := Parse(producer)
if ver.IsDevel() {
return true
}
return ver.AfterOrEqual(GoVersion{major, minor, -1, 0, 0, ""})
}
34 changes: 28 additions & 6 deletions pkg/proc/bininfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/derekparker/delve/pkg/dwarf/line"
"github.com/derekparker/delve/pkg/dwarf/op"
"github.com/derekparker/delve/pkg/dwarf/reader"
"github.com/derekparker/delve/pkg/goversion"
)

type BinaryInfo struct {
Expand Down Expand Up @@ -75,7 +76,8 @@ type compileUnit struct {
Name string // univocal name for non-go compile units
lineInfo *line.DebugLineInfo // debug_line segment associated with this compile unit
LowPC, HighPC uint64
optimized bool // this compile unit is optimized
optimized bool // this compile unit is optimized
producer string // producer attribute
}

// Function describes a function in the target program.
Expand Down Expand Up @@ -351,7 +353,7 @@ func (bi *BinaryInfo) LoadFromData(dwdata *dwarf.Data, debugFrameBytes, debugLin

bi.loclistInit(debugLocBytes)

bi.loadDebugInfoMaps(debugLineBytes, nil)
bi.loadDebugInfoMaps(debugLineBytes, nil, nil)
}

func (bi *BinaryInfo) loclistInit(data []byte) {
Expand Down Expand Up @@ -426,6 +428,15 @@ func (bi *BinaryInfo) findCompileUnit(pc uint64) *compileUnit {
return nil
}

func (bi *BinaryInfo) Producer() string {
for _, cu := range bi.compileUnits {
if cu.isgo && cu.producer != "" {
return cu.producer
}
}
return ""
}

// ELF ///////////////////////////////////////////////////////////////

func (bi *BinaryInfo) LoadBinaryInfoElf(path string, wg *sync.WaitGroup) error {
Expand Down Expand Up @@ -456,7 +467,7 @@ func (bi *BinaryInfo) LoadBinaryInfoElf(path string, wg *sync.WaitGroup) error {

wg.Add(3)
go bi.parseDebugFrameElf(elfFile, wg)
go bi.loadDebugInfoMaps(debugLineBytes, wg)
go bi.loadDebugInfoMaps(debugLineBytes, wg, nil)
go bi.setGStructOffsetElf(elfFile, wg)
return nil
}
Expand Down Expand Up @@ -568,7 +579,7 @@ func (bi *BinaryInfo) LoadBinaryInfoPE(path string, wg *sync.WaitGroup) error {

wg.Add(2)
go bi.parseDebugFramePE(peFile, wg)
go bi.loadDebugInfoMaps(debugLineBytes, wg)
go bi.loadDebugInfoMaps(debugLineBytes, wg, nil)

// Use ArbitraryUserPointer (0x28) as pointer to pointer
// to G struct per:
Expand Down Expand Up @@ -736,11 +747,22 @@ func (bi *BinaryInfo) LoadBinaryInfoMacho(path string, wg *sync.WaitGroup) error

wg.Add(2)
go bi.parseDebugFrameMacho(exe, wg)
go bi.loadDebugInfoMaps(debugLineBytes, wg)
bi.gStructOffset = 0x8a0
go bi.loadDebugInfoMaps(debugLineBytes, wg, bi.setGStructOffsetMacho)
return nil
}

func (bi *BinaryInfo) setGStructOffsetMacho() {
// In go1.11 it's 0x30, before 0x8a0, see:
// https://github.com/golang/go/issues/23617
// and go commit b3a854c733257c5249c3435ffcee194f8439676a
producer := bi.Producer()
if producer != "" && goversion.ProducerAfterOrEqual(producer, 1, 11) {
bi.gStructOffset = 0x30
return
}
bi.gStructOffset = 0x8a0
}

func (bi *BinaryInfo) parseDebugFrameMacho(exe *macho.File, wg *sync.WaitGroup) {
defer wg.Done()

Expand Down
18 changes: 14 additions & 4 deletions pkg/proc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (v packageVarsByAddr) Len() int { return len(v) }
func (v packageVarsByAddr) Less(i int, j int) bool { return v[i].addr < v[j].addr }
func (v packageVarsByAddr) Swap(i int, j int) { v[i], v[j] = v[j], v[i] }

func (bi *BinaryInfo) loadDebugInfoMaps(debugLineBytes []byte, wg *sync.WaitGroup) {
func (bi *BinaryInfo) loadDebugInfoMaps(debugLineBytes []byte, wg *sync.WaitGroup, cont func()) {
if wg != nil {
defer wg.Done()
}
Expand Down Expand Up @@ -212,9 +212,15 @@ func (bi *BinaryInfo) loadDebugInfoMaps(debugLineBytes []byte, wg *sync.WaitGrou
if lineInfoOffset >= 0 && lineInfoOffset < int64(len(debugLineBytes)) {
cu.lineInfo = line.Parse(compdir, bytes.NewBuffer(debugLineBytes[lineInfoOffset:]))
}
if producer, _ := entry.Val(dwarf.AttrProducer).(string); cu.isgo && producer != "" {
semicolon := strings.Index(producer, ";")
cu.optimized = semicolon < 0 || !strings.Contains(producer[semicolon:], "-N") || !strings.Contains(producer[semicolon:], "-l")
cu.producer, _ = entry.Val(dwarf.AttrProducer).(string)
if cu.isgo {
semicolon := strings.Index(cu.producer, ";")
if semicolon < 0 {
cu.optimized = true
} else {
cu.optimized = !strings.Contains(cu.producer[semicolon:], "-N") || !strings.Contains(cu.producer[semicolon:], "-l")
cu.producer = cu.producer[:semicolon]
}
}
bi.compileUnits = append(bi.compileUnits, cu)

Expand Down Expand Up @@ -302,6 +308,10 @@ func (bi *BinaryInfo) loadDebugInfoMaps(debugLineBytes []byte, wg *sync.WaitGrou
}
sort.Strings(bi.Sources)
bi.Sources = uniq(bi.Sources)

if cont != nil {
cont()
}
}

func uniq(s []string) []string {
Expand Down

0 comments on commit 3835257

Please sign in to comment.