Delve: Retrieve go module information

Created on 21 Aug 2020  Â·  6Comments  Â·  Source: go-delve/delve

  1. What version of Delve are you using (dlv version)?
    1.4.0
  2. What version of Go are you using? (go version)?
    1.14
  3. What operating system and processor architecture are you using?
    linux/amd64

Is there a way to retrieve the name of the go module of the debug program launched through dlv?

Most helpful comment

If you have access to the source code the go list command will provide the information about the module in "Module" field.

$ go list -m -f '{{.Dir}} {{.Path}}'
/Users/hakim/projects/cloud-code-samples/golang/go-guestbook/src/frontend frontend

Or even you can just parse the go.mod file :-)

If you don't have access to the source code

  • if you can access the binary, go version -m <binary> is convenient.
  $ go version -m frontend
frontend: go1.14.5
    path    frontend
    mod frontend    (devel) 
  • if the binary is not locally accessible but you have to work through delve running remotely, retrieving the variable info runtime.modinfo[16:] is one option. Plain text, new line separated.
(dlv) p runtime.modinfo[16:]
"path\tfrontend\nmod\tfrontend\t(devel)\t\n�2C1�\x18 r\x00�B\x10A\x16��...+-10 more"

All 6 comments

The infomation of the go module would not be generated into debug section.
ps: We can fetch all file name of source code if exectuate with optimizations and inlining disabled.

I think you can do it by printing the information stored for the runtime, see how it works here https://golang.org/cl/144220

I guess you can invoke the same ReadBuildInfo function?

Disclaimer, I haven't tried this yet.

The name of the module is always main. You can find out its compile-time directory path by using objdump and looking for main's compile unit. The import path can be extracted by parsing p runtime.modinfo[16:].

@aarzilli For example, in this Go folder, the go.mod has module frontend. From a breakpoint set in the main.go file, how do I get this frontend name?

If you have access to the source code the go list command will provide the information about the module in "Module" field.

$ go list -m -f '{{.Dir}} {{.Path}}'
/Users/hakim/projects/cloud-code-samples/golang/go-guestbook/src/frontend frontend

Or even you can just parse the go.mod file :-)

If you don't have access to the source code

  • if you can access the binary, go version -m <binary> is convenient.
  $ go version -m frontend
frontend: go1.14.5
    path    frontend
    mod frontend    (devel) 
  • if the binary is not locally accessible but you have to work through delve running remotely, retrieving the variable info runtime.modinfo[16:] is one option. Plain text, new line separated.
(dlv) p runtime.modinfo[16:]
"path\tfrontend\nmod\tfrontend\t(devel)\t\n�2C1�\x18 r\x00�B\x10A\x16��...+-10 more"

Thanks @hyangah! I'll use p runtime.modinfo[16:].

Was this page helpful?
0 / 5 - 0 ratings

Related issues

christianwoehrle picture christianwoehrle  Â·  5Comments

NanXiao picture NanXiao  Â·  3Comments

wusendong picture wusendong  Â·  5Comments

runningbar picture runningbar  Â·  4Comments

wxqzhy picture wxqzhy  Â·  4Comments