Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support debug for gop project #18

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/go-delve/gore v0.11.6
github.com/go-delve/liner v1.2.3-0.20231231155935-4726ab1d7f62
github.com/google/go-dap v0.11.0
github.com/goplus/mod v0.12.2
github.com/goplus/mod v0.12.3
github.com/hashicorp/golang-lru v1.0.2
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-isatty v0.0.20
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-dap v0.11.0 h1:SpAZJL41rOOvd85PuLCCLE1dteTQOyKNnn0H3DBHywo=
github.com/google/go-dap v0.11.0/go.mod h1:HAeyoSd2WIfTfg+0GRXcFrb+RnojAtGNh+k+XTIxJDE=
github.com/goplus/mod v0.12.2 h1:5RHUdmXT5jlzOFUAfrx/5uSk4RTphFPFcCbRUt/AIBo=
github.com/goplus/mod v0.12.2/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
github.com/goplus/mod v0.12.3 h1:qLU5/F27CzUTQhCQRN1WruiCepUn5GdLKQTB41OsYfk=
github.com/goplus/mod v0.12.3/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
github.com/hashicorp/golang-lru v1.0.2 h1:dV3g9Z/unq5DpblPpw+Oqcv4dU/1omnb4Ok8iPY6p1c=
github.com/hashicorp/golang-lru v1.0.2/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
57 changes: 39 additions & 18 deletions pkg/proc/bininfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2182,6 +2182,34 @@ func (bi *BinaryInfo) registerTypeToPackageMap(entry *dwarf.Entry) {
bi.PackageMap[name] = []string{path}
}

// check file is gop or gop classfile
func isGopFile(file string) bool {
fileExt := filepath.Ext(file)
return fileExt != "" && fileExt != ".go" && fileExt != ".s"
}

// gop file relative path to absolute path
func relPathToAbsPathByPackage(imageMod *gopmod.Module, filePakage string, relPath string) string {
absPath := filePakage
if imageMod == nil {
panic("imageMod is nil")
}
if filePakage == "main" {
filePakage = imageMod.Path()
}
if imageMod.PkgType(filePakage) != -1 {
fileMod, err := imageMod.Lookup(filePakage)
if err == nil {
absPath = fileMod.ModDir
}
}
absPath = filepath.Join(absPath, relPath)
if runtime.GOOS == "windows" {
absPath = filepath.ToSlash(absPath)
}
return absPath
}

func (bi *BinaryInfo) loadDebugInfoMaps(image *Image, debugInfoBytes, debugLineBytes []byte, wg *sync.WaitGroup, cont func()) {
if wg != nil {
defer wg.Done()
Expand Down Expand Up @@ -2209,6 +2237,8 @@ func (bi *BinaryInfo) loadDebugInfoMaps(image *Image, debugInfoBytes, debugLineB

reader := image.DwarfReader()

imageMod := gopmod.Default

for {
entry, err := reader.Next()
if err != nil {
Expand Down Expand Up @@ -2266,10 +2296,13 @@ func (bi *BinaryInfo) loadDebugInfoMaps(image *Image, debugInfoBytes, debugLineB
cu.producer = cu.producer[:semicolon]
}
}

imageMod, err := gopmod.Load(image.Path)
if err != nil {
imageMod = gopmod.Default
// load currentImageDir modInfo
imageDir := filepath.Dir(image.Path)
if imageMod.Path() != imageDir {
imageMod, err = gopmod.Load(imageDir)
LiusCraft marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
imageMod = gopmod.Default
}
}
if cu.lineInfo != nil {
cuName := cu.name
Expand All @@ -2279,21 +2312,9 @@ func (bi *BinaryInfo) loadDebugInfoMaps(image *Image, debugInfoBytes, debugLineB
// filter test file suffix: support test debug
cuName = strings.TrimSuffix(cuName, "_test")
for _, fileEntry := range cu.lineInfo.FileNames {
fileExt := filepath.Ext(fileEntry.Path)
if fileExt != "" && fileExt != ".go" && fileExt != ".s" && !filepath.IsAbs(fileEntry.Path) {
LiusCraft marked this conversation as resolved.
Show resolved Hide resolved
if isGopFile(fileEntry.Path) {
filePakage := strings.TrimSuffix(cuName, cu.lineInfo.IncludeDirs[fileEntry.DirIdx])
absPath := filePakage
if filePakage == "main" {
filePakage = imageMod.Path()
}
fileMod, err := imageMod.Lookup(filePakage)
if err == nil {
absPath = fileMod.ModDir
}
absPath = filepath.Join(absPath, fileEntry.Path)
if runtime.GOOS == "windows" {
absPath = filepath.ToSlash(absPath)
}
absPath := relPathToAbsPathByPackage(imageMod, filePakage, fileEntry.Path)
cu.lineInfo.Lookup[absPath] = fileEntry
delete(cu.lineInfo.Lookup, fileEntry.Path)
fileEntry.Path = absPath
Expand Down
3 changes: 2 additions & 1 deletion vendor/github.com/goplus/mod/modload/module.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ github.com/go-delve/liner
# github.com/google/go-dap v0.11.0
## explicit; go 1.13
github.com/google/go-dap
# github.com/goplus/mod v0.12.2
# github.com/goplus/mod v0.12.3
## explicit; go 1.16
github.com/goplus/mod
github.com/goplus/mod/gopmod
Expand Down