Go: question: how to put breakpoint to a Go method with gdb or delve

Created on 23 Mar 2019  路  12Comments  路  Source: golang/go

What version of Go are you using (go version)?

$ go version
go version go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

Didnt check as this is a query related to debuggers with go rather than go itself

What operating system and processor architecture are you using (go env)?

go env Output

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/vignesh/go-book/"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build931353307=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

I want to debug a go code. I want to put breakpoint on a method (belonging to a named type) in a imported package. I went through a lot of online materials where they only put breakpoints on a line number in file (eg:breakpoint a.go:15).

I have done a lot of debugging in 'C' where I put breakpoints on functions. Is this kind of debugging possible in go??

I have the following code in my main package.

clientContext := sdk.Context(fabsdk.WithUser("Admin"), fabsdk.WithOrg("ordererorg")).

I want to put breakpoints to

1) function WithUser() which belongs to package fabsdk in file github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/context.go.
2) method Context() belonging to type FabricSDK in file github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/fabsdk.go in package fabsdk.

Is it possible to put breakpoints to method WithUser() and context() or Is breakpoints only allowed at a line number in a file?? I could use either gdb or delve or any other debugger as well.

What did you expect to see?

need to put breakpoints on a method or function

What did you see instead?

(gdb) br sdk.Context
Function "sdk.Context" not defined.
Make breakpoint pending on future shared library load? (y or [n]) n

(gdb) br github.com/hyperledger/fabric-sdk-go/pkg/fabsdk.Context
Function "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk.Context" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 2 (github.com/hyperledger/fabric-sdk-go/pkg/fabsdk.Context) pending.

FrozenDueToAge WaitingForInfo

Most helpful comment

You can also place a breakpoint using a line number, like "br fabsdk/context.go:NNN". I believe in gdb it will do some amount of disambiguation of the file name, so you don't have to type the full path to use this syntax.

All 12 comments

(for gdb)

function WithUser() which belongs to package fabsdk in file github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/context.go.

Try b github.com/hyperledger/fabric-sdk-go/pkg/fabsdk.WithUser?

method Context() belonging to type FabricSDK in file github.com/hyperledger/fabric-sdk-go/pkg/fabsdk/fabsdk.go in package fabsdk.

Try b 'github.com/hyperledger/fabric-sdk-go/pkg/fabsdk.(*FabricSDK).Context'?

In general, the symbol name of a method is pkg_path.T.M for value method or pkg_path.(*T).M for pointer method.

@cherrymui , I tried your solution with delve and it works. Thank you very much for your response and the explanation of path of a symbol :)

I'd actually like to re-open this as a feature request. Writing out the full symbol name is onerous. I often resort to running nm/objdump, grepping for the function name I want, and then using the address instead.

It'd be nice if delve did some/all of these things:

  • Given b WithUser and only a single function containing the substring WithUser, set a breakpoint on it and print what it resolved to.
  • Given b WithUser, print a list of all symbols containing the substring WithUser, so that the user can easily copy/paste one.
  • Add a command like resolve WithUser that prints a list of all symbols with the substring WithUser.

cc @aarzilli @heschik @thanm

Bonus: support the same features in gdb/lldb!

Also cc @dr2chase @aclements (I'm not sure exactly who the debugger team is nowadays)

Just b Context is probably ambiguous but delve should be able to resolve something like b FabricSDK.Context or b fabsdk.Context correctly. Supporting something like this in lldb would require writing a language layer for it (which existed for a while until it was removed because nobody maintained it).

Even if b Context is ambiguous, delve could print all the possible disambiguations (up to some limit), no? That鈥檇 be more useful than simply rejecting it.

I didn鈥檛 realize the lldb script had been removed. Bummer.

Even if b Context is ambiguous, delve could print all the possible disambiguations (up to some limit), no?

yes, and it does.

You can also place a breakpoint using a line number, like "br fabsdk/context.go:NNN". I believe in gdb it will do some amount of disambiguation of the file name, so you don't have to type the full path to use this syntax.

And just for the record, I don't think there's anything in the compiler/linker we can do to affect any of this. Maybe we could do something in runtime-gdb.py? But to me this seems like a Delve feature request, if anything.

I agree with @heschik that this is more on the debugger side than the compiler side.

Ideally maybe gdb/lldb should probably just hint symbols with partial matches. C++ function names are also quite long, in many cases.

@josharian would you mind updating the title of this issue with what you'd like to accomplish? Should your feature requests be separate issues perhaps?

Re-reading this thread, I think I'll just close it.

Was this page helpful?
0 / 5 - 0 ratings