Skip to content

Commit

Permalink
feat: Change gop file path from relative path under the package to <p…
Browse files Browse the repository at this point in the history
…ackage>/<gopfile path>
  • Loading branch information
LiusCraft committed Jan 19, 2024
1 parent 6ee5a8d commit 970b888
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
28 changes: 26 additions & 2 deletions pkg/proc/bininfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2288,10 +2288,34 @@ func (bi *BinaryInfo) loadDebugInfoMaps(image *Image, debugInfoBytes, debugLineB

bi.lookupFunc = nil
bi.lookupGenericFunc = nil

// TODO: support find module path
// e: github.com/liuscraft/testgomod/<gopfile path> to github.com/liuscraft/testgomod/<version>/<gopfile path>
// feat: support debug vscode setBreakpoints, but not support "next" command
tempGopSourceInfo := make(map[string]map[string]string)
for _, pbi := range bi.ListPackagesBuildInfo(true) {
if pbi.DirectoryPath == "." {
continue
}
tempGopSourceInfo[pbi.ImportPath] = make(map[string]string)
for fileSource, _ := range pbi.Files {
ext := filepath.Ext(fileSource)
if ext == ".go" || ext == "" {
continue
}
tempGopSourceInfo[pbi.ImportPath][fileSource] = pbi.ImportPath + strings.TrimPrefix(fileSource, pbi.DirectoryPath)
bi.Sources = append(bi.Sources, tempGopSourceInfo[pbi.ImportPath][fileSource])
}
}
for _, cu := range image.compileUnits {
if cu.lineInfo != nil {
for _, fileEntry := range cu.lineInfo.FileNames {
gopPath := tempGopSourceInfo[cu.name][fileEntry.Path]
if gopPath != "" {
delete(cu.lineInfo.Lookup, fileEntry.Path)
fileEntry.Path = gopPath
cu.lineInfo.Lookup[gopPath] = fileEntry
continue
}
bi.Sources = append(bi.Sources, fileEntry.Path)
}
}
Expand Down Expand Up @@ -2739,7 +2763,7 @@ func (bi *BinaryInfo) ListPackagesBuildInfo(includeFiles bool) []*PackageBuildIn
ip := strings.ReplaceAll(cu.name, "\\", "/")
if _, ok := m[ip]; !ok {
path := cu.lineInfo.FirstFile()
if ext := filepath.Ext(path); ext != ".go" && ext != ".s" {
if ext := filepath.Ext(path); ext != ".go" && ext != ".s" && ext != ".gop" {
continue
}
dp := filepath.Dir(path)
Expand Down
18 changes: 18 additions & 0 deletions pkg/proc/proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,24 @@ func TestStep(t *testing.T) {
}
})
}
func getPackageDir(pkg string) string {
cmd := exec.Command("go", "list", "-f", "{{.Dir}}", pkg)
out, err := cmd.Output()
if err != nil {
return "."
}
return string(bytes.TrimSpace(out))
}
func TestProcSource(t *testing.T) {
protest.AllowRecording(t)
withTestProcess("/home/liuscraft/codings/go/gop-delve/_fixtures/goptest/", t, func(p *proc.Target, grp *proc.TargetGroup, fixture protest.Fixture) {
for _, v := range p.BinInfo().Sources {
if strings.Contains(v, ".gop") {
t.Log(v)
}
}
})
}

func TestBreakpoint(t *testing.T) {
protest.AllowRecording(t)
Expand Down
8 changes: 5 additions & 3 deletions pkg/proc/test/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ func BuildFixture(name string, flags BuildFlags) Fixture {
}

fixturesDir := FindFixturesDir()

if name[0] == '/' {
fixturesDir = ""
}
dir := fixturesDir
path := filepath.Join(fixturesDir, name+".go")
path := filepath.Join(fixturesDir, name)
if name[len(name)-1] == '/' {
dir = filepath.Join(dir, name)
path = ""
Expand Down Expand Up @@ -164,7 +166,7 @@ func BuildFixture(name string, flags BuildFlags) Fixture {
}
}
if path != "" {
buildFlags = append(buildFlags, name+".go")
buildFlags = append(buildFlags, name)
}

cmd := exec.Command("go", buildFlags...)
Expand Down
17 changes: 16 additions & 1 deletion service/dap/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,16 @@ func gopFileNameFilter(file string) bool {
}
return false
}
func handlerPathAtFilter(filePath string) string {
atIndex := strings.Index(filePath, "@")
if atIndex > 0 {
versionEndIndex := strings.Index(filePath[atIndex:], "/")
if versionEndIndex > 0 {
filePath = filePath[:atIndex] + filePath[atIndex+versionEndIndex:]
}
}
return filePath
}
func (s *Session) handlerGopFilePath(filePath string, sources []string) string {
if !gopFileNameFilter(filePath) {
return filePath
Expand Down Expand Up @@ -1422,9 +1432,11 @@ func (s *Session) handlerGopFilePath(filePath string, sources []string) string {
if s.sourcesChece == nil {
s.sourcesChece = make(map[string]string)
}
sourceFilePath := filePath
filePath = handlerPathAtFilter(filePath)
for _, v := range isGopSources {
if strings.Contains(filePath, v) {
s.sourcesChece[v] = filePath
s.sourcesChece[v] = sourceFilePath
return v
}
}
Expand Down Expand Up @@ -1461,6 +1473,9 @@ func (s *Session) onSetBreakpointsRequest(request *dap.SetBreakpointsRequest) {

response := &dap.SetBreakpointsResponse{Response: *newResponse(request.Request)}
for _, b := range breakpoints {
if b.Source == nil {
continue
}
b.Source.Path = request.Arguments.Source.Path
}
response.Body.Breakpoints = breakpoints
Expand Down

0 comments on commit 970b888

Please sign in to comment.