How to properly debug a binary generated by `go test -c` using GDB?

huangapple go评论70阅读模式
英文:

How to properly debug a binary generated by `go test -c` using GDB?

问题

go test命令支持-c标志,其描述如下:

-c  将测试二进制文件编译为pkg.test,但不运行它。
    (其中pkg是包导入路径的最后一个元素。)

据我所了解,通过这种方式生成二进制文件是使用GDB进行交互式运行的方法。然而,由于测试二进制文件是通过将源文件和测试文件临时组合在某个/tmp/目录中创建的,所以当我在gdb中运行list命令时会出现以下情况:

加载Go运行时支持。
(gdb) list
42      github.com/<username>/<project>/_test/_testmain.go: 没有那个文件或目录。

这意味着我无法像以前那样在GDB中愉快地检查Go源代码。我知道可以通过向go test命令传递-work标志来强制临时目录保留,但这仍然很麻烦,因为二进制文件不是在该目录中创建的等等。我想知道是否有人找到了解决这个问题的简洁方法。

英文:

The go test command has support for the -c flag, described as follows:

-c  Compile the test binary to pkg.test but do not run it.
    (Where pkg is the last element of the package&#39;s import path.)

As far as I understand, generating a binary like this is the way to run it interactively using GDB. However, since the test binary is created by combining the source and test files temporarily in some /tmp/ directory, this is what happens when I run list in gdb:

Loading Go Runtime support.
(gdb) list
42      github.com/&lt;username&gt;/&lt;project&gt;/_test/_testmain.go: No such file or directory.

This means I cannot happily inspect the Go source code in GDB like I'm used to. I know it is possible to force the temporary directory to stay by passing the -work flag to the go test command, but then it is still a huge hassle since the binary is not created in that directory and such. I was wondering if anyone found a clean solution to this problem.

答案1

得分: 4

Go 1.5已经发布,但仍然没有官方认可的Go调试器。我在使用GDB来有效地调试Go程序或测试二进制文件方面并没有太大的成功。然而,我在使用Delve时取得了一些成功,Delve是一个非官方的调试器,仍在开发中:https://github.com/derekparker/delve

要在调试器中运行你的测试代码,只需安装Delve:

go get -u github.com/derekparker/delve/cmd/dlv

...然后在你的工作区内从调试器中启动测试:

dlv test

在调试器提示符下,你可以单步执行、设置断点等。

试试看吧!

英文:

Go 1.5 has been released, and there is still no officially sanctioned Go debugger. I haven't had much success using GDB for effectively debugging Go programs or test binaries. However, I have had success using Delve, a non-official debugger that is still undergoing development: https://github.com/derekparker/delve

To run your test code in the debugger, simply install delve:

go get -u github.com/derekparker/delve/cmd/dlv

... and then start the tests in the debugger from within your workspace:

dlv test

From the debugger prompt, you can single-step, set breakpoints, etc.

Give it a whirl!

答案2

得分: 3

很遗憾,这似乎是一个已知的问题,不会修复。请参考以下讨论:

https://groups.google.com/forum/#!topic/golang-nuts/nIA09gp3eNU

我看到了两种解决这个问题的方法。

1)创建一个.gdbinit文件,并使用set substitute-path命令将gdb重定向到源代码的实际位置。这个文件可以由go工具生成,但可能会覆盖某人的自定义.gdbinit文件,并且会将go工具与gdb绑定在一起,这似乎不是一个好主意。

2)用实际磁盘上的位置替换可执行文件中的源文件路径(这些路径指向/tmp/...)。如果真实路径比/tmp/...路径短,这是很直接的。这可能需要编译器/链接器的额外支持,以使这个解决方案更通用。

这个问题在Go Google Code问题跟踪器上引发了讨论,最终决定如下:

https://code.google.com/p/go/issues/detail?id=2881

这很烦人,但这只是许多烦人可能性中的最小烦人之一。作为一个规则,go工具不应该在源代码目录中乱写,因为这些目录甚至可能没有写权限,而且它在退出后也不应该在其他地方留下文件。_testmain.go中几乎没有什么有趣的东西。使用gdb进行测试的人可以在testing.Main上设置断点。

Russ
状态:不幸的

简而言之,这很糟糕,虽然你可以通过一些方法解决并使用GDB调试可执行文件,但开发团队不太可能让这变得更容易。

英文:

Unfortunately, this appears to be a known issue that's not going to be fixed. See this discussion:

https://groups.google.com/forum/#!topic/golang-nuts/nIA09gp3eNU

> I've seen two solutions to this problem.
>
> 1) create a .gdbinit file with a set substitute-path command to
> redirect gdb to the actual location of the source. This file could be
> generated by the go tool but you'd risk overwriting someone's custom
> .gdbinit file and would tie the go tool to gdb which seems like a bad
> idea.
>
> 2) Replace the source file paths in the executable (which are pointing
> to /tmp/...) with the location they reside on disk. This is
> straightforward if the real path is shorter then the /tmp/... path.
> This would likely require additional support from the compiler /
> linker to make this solution more generic.

It spawned this issue on the Go Google Code issue tracker, to which the decision ended up being:

https://code.google.com/p/go/issues/detail?id=2881

> This is annoying, but it is the least of many annoying possibilities.
> As a rule, the go tool should not be scribbling in the source
> directories, which might not even be writable, and it shouldn't be
> leaving files elsewhere after it exits. There is next to nothing
> interesting in _testmain.go. People testing with gdb can break on
> testing.Main instead.
>
> Russ
> Status: Unfortunate

So, in short, it sucks, and while you can work around it and GDB a test executable, the development team is unlikely to make it as easy as it could be for you.

答案3

得分: 0

我对golang还不太熟悉,但基本的调试似乎是有效的。

你正在尝试使用的list命令只能在代码中的某个断点处使用。例如:

(gdb) b aws.go:54
Breakpoint 1 at 0x61841: file /Users/mat/gocode/src/github.com/stellar/deliverator/aws/aws.go, line 54.
(gdb) r
Starting program: /Users/mat/gocode/src/github.com/stellar/deliverator/aws/aws.test
[省略:关于BinaryCache的一些警告]

Breakpoint 1, github.com/stellar/deliverator/aws.imageIsNewer (latest=0xc2081fe2d0, ami=0xc2081fe3c0, ~r2=false)
    at /Users/mat/gocode/src/github.com/stellar/deliverator/aws/aws.go:54
54	    layout := &quot;2006-01-02T15:04:05.000Z&quot;
(gdb) list
49	func imageIsNewer(latest *ec2.Image, ami *ec2.Image) bool {
50	    if latest == nil {
51	        return true
52	    }
53
54	    layout := &quot;2006-01-02T15:04:05.000Z&quot;
55
56	    amiCreationTime, amiErr := time.Parse(layout, *ami.CreationDate)
57	    if amiErr != nil {
58	        panic(amiErr)

这是在我的项目的aws子目录中运行以下命令后的情况:

go test -c
gdb aws.test

另外需要注意的是,断点似乎只能放在特定的位置。似乎必须是一个表达式,但这个结论只是通过实验得出的。

英文:

I'm still new to the golang game but for what it's worth basic debugging seems to work.

The list command you're trying to work can be used so long as you're already at a breakpoint somewhere in your code. For example:

(gdb) b aws.go:54
Breakpoint 1 at 0x61841: file /Users/mat/gocode/src/github.com/stellar/deliverator/aws/aws.go, line 54.
(gdb) r
Starting program: /Users/mat/gocode/src/github.com/stellar/deliverator/aws/aws.test
[snip: some arnings about BinaryCache]

Breakpoint 1, github.com/stellar/deliverator/aws.imageIsNewer (latest=0xc2081fe2d0, ami=0xc2081fe3c0, ~r2=false)
    at /Users/mat/gocode/src/github.com/stellar/deliverator/aws/aws.go:54
54	    layout := &quot;2006-01-02T15:04:05.000Z&quot;
(gdb) list
49	func imageIsNewer(latest *ec2.Image, ami *ec2.Image) bool {
50	    if latest == nil {
51	        return true
52	    }
53
54	    layout := &quot;2006-01-02T15:04:05.000Z&quot;
55
56	    amiCreationTime, amiErr := time.Parse(layout, *ami.CreationDate)
57	    if amiErr != nil {
58	        panic(amiErr)

This is just after running the following in the aws subdir of my project:

go test -c
gdb aws.test

As an additional caveat, it does seem very selective about where breakpoints can be placed. Seems like it has to be an expression but that conclusion is only via experimentation.

答案4

得分: 0

如果你愿意使用除了GDB之外的工具,可以尝试使用godebug。首先,使用以下命令安装它:

go get github.com/mailgun/godebug

接下来,在你的代码中插入一个断点,可以通过添加以下语句实现:

_ = "breakpoint"

然后,使用godebug test命令运行你的测试。

godebug test

它支持go test命令的许多参数。

  -test.bench string
        选择要运行的基准测试的每个路径组件的正则表达式
  -test.benchmem
        打印基准测试的内存分配
  -test.benchtime duration
        每个基准测试的大致运行时间(默认为1秒)
  -test.blockprofile string
        在执行后将goroutine阻塞分析写入指定的文件
  -test.blockprofilerate int
        如果>= 0,则调用runtime.SetBlockProfileRate()(默认为1)
  -test.count n
        运行测试和基准测试n次(默认为1)
  -test.coverprofile string
        在执行后将覆盖率分析写入指定的文件
  -test.cpu string
        逗号分隔的每个测试使用的CPU数量列表
  -test.cpuprofile string
        在执行期间将CPU分析写入指定的文件
  -test.memprofile string
        在执行后将内存分析写入指定的文件
  -test.memprofilerate int
        如果>=0,则设置runtime.MemProfileRate
  -test.outputdir string
        写入分析文件的目录
  -test.parallel int
        最大测试并行度(默认为4)
  -test.run string
        选择要运行的测试和示例的正则表达式
  -test.short
        运行较小的测试套件以节省时间
  -test.timeout duration
        如果为正数,则为所有测试设置总时间限制
  -test.trace string
        在执行后将执行跟踪写入指定的文件
  -test.v
        详细输出:打印额外的输出
英文:

If you're willing to use tools besides GDB, check out godebug. To use it, first install with:

go get github.com/mailgun/godebug

Next, insert a breakpoint somewhere by adding the following statement to your code:

_ = &quot;breakpoint&quot;

Now run your tests with the godebug test command.

godebug test

It supports many of the parameters from the go test command.

> -test.bench string
> regular expression per path component to select benchmarks to run
> -test.benchmem
> print memory allocations for benchmarks
> -test.benchtime duration
> approximate run time for each benchmark (default 1s)
> -test.blockprofile string
> write a goroutine blocking profile to the named file after execution
> -test.blockprofilerate int
> if >= 0, calls runtime.SetBlockProfileRate() (default 1)
> -test.count n
> run tests and benchmarks n times (default 1)
> -test.coverprofile string
> write a coverage profile to the named file after execution
> -test.cpu string
> comma-separated list of number of CPUs to use for each test
> -test.cpuprofile string
> write a cpu profile to the named file during execution
> -test.memprofile string
> write a memory profile to the named file after execution
> -test.memprofilerate int
> if >=0, sets runtime.MemProfileRate
> -test.outputdir string
> directory in which to write profiles
> -test.parallel int
> maximum test parallelism (default 4)
> -test.run string
> regular expression to select tests and examples to run
> -test.short
> run smaller test suite to save time
> -test.timeout duration
> if positive, sets an aggregate time limit for all tests
> -test.trace string
> write an execution trace to the named file after execution
> -test.v
> verbose: print additional output

huangapple
  • 本文由 发表于 2013年12月18日 09:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/20648192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定