go程序调试总结

Comments(201)


Posted on 2014-10-04 03:51:00 golang


首先需要注意的是:golang1.3之后的版本,对于支持gdb调试存在很大的问题。产生这个问题的原因是,golang的runtime没有完整的被gdb支持。

最新比较完整支持gdb调试的版本是golang 1.2.2,但是也有个别问题存在。

为什么会出现以上种种问题,golang官网给出的解释是:

GDB does not understand Go programs well. The stack management, threading, and runtime contain aspects that differ enough from the execution model GDB expects that they can confuse the debugger, even when the program is compiled with gccgo. As a consequence, although GDB can be useful in some situations, it is not a reliable debugger for Go programs, particularly heavily concurrent ones. Moreover, it is not a priority for the Go project to address these issues, which are difficult. In short, the instructions below should be taken only as a guide to how to use GDB when it works, not as a guarantee of success.

翻译一下:

GDB不能很好的理解GO程序。堆栈管理、线程和runtime包含了非常不一样的执行模式,这不是GDB期望的,他们会扰乱调试器,即使go程序是使用gccgo编译的。结果就是,虽然GDB在某些场合下是有用的,但是对go程序来说并不是一个可靠的调试器。尤其是在大量并发的时候。而且,这不是Golang项目优先考虑的事情,这很困难。总而言之,下面的操作手册,只是当GDB正常工作的时候,引导你如何使用GDB,不能保证总是成功。

并且从google group讨论组和stackoverflow中,可以看到golang的多个版本对于GDB的支持都有这样那样的问题。 不过既然官方的手册都这么说了,我们也只有在合适的场合使用GDB吧。

默认情况下,编译过的二进制文件已经包含了 DWARFv3 调试信息,只要 GDB7.1 以上版本都可以进行调试。 在OSX下,如无法执行调试指令,可尝试用sudo方式执行gdb。

在编译go程序的时候,需要关闭内联优化:** -gcflags “-N -l”**。可以在go get/build/test的时候指定这个参数。

有两种方式可以下断点:

  • gdb命令运行之后,使用break file:lineno
  • 使用runtime.BreakPoint()

演示环境:Ubuntu 14.04 LTS amd64 / Golang 1.2.2 / GDB 7.7

package main

import (
    "fmt"
    "runtime"
)

func test(s string, x int) (r string) {
    r = fmt.Sprintf("test: %s %d", s, x)
    runtime.Breakpoint()
    return r
}
func main() {
    s := "haha"
    i := 1234
    println(test(s, i))
}

启动GDB:

➜  ~  gdb ./test
GNU gdb (Ubuntu 7.7-0ubuntu3) 7.7
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...done.
**Loading Go Runtime support.**
(gdb)

以模块名的方式查看源码:

(gdb) l main.main
9       r = fmt.Sprintf("test: %s %d", s, x)
10      runtime.Breakpoint()
11      return r
12  }
13
14  func main() {
15      s := "haha"
16      i := 1234
17      println(test(s, i))

以文件名加行号的方式查看源码:

(gdb) l /home/roy/test.go:1
1   package main
2
3   import (
4       "fmt"
5       "runtime"
6   )
7
8   func test(s string, x int) (r string) {
9       r = fmt.Sprintf("test: %s %d", s, x)
10      runtime.Breakpoint()

设置断点:

(gdb) b main.main
Breakpoint 2 at 0x400d80: file /home/roy/test.go, line 14.
(gdb) b test.go:18
Breakpoint 3 at 0x400e0e: file /home/roy/test.go, line 18.

启动进程,触发断点: (n 单步执行,c执行到下一个断点)

(gdb) run
Starting program: /home/roy/test

Breakpoint 2, main.main () at /home/roy/test.go:14
14  func main() {

查看goroutines:

(gdb) info goroutines
* 1  running runtime.gosched
* 2  syscall runtime.notetsleepg

查看指定goroutine的调用堆栈:

(gdb) goroutine 1 bt
Python Exception <class 'gdb.MemoryError'> Cannot access memory at address 0x8:
Error occurred in Python command: Cannot access memory at address 0x8

当我们想查看goroutine调用堆栈的时候,gdb报告无法访问内存。 Cannot access memory at address 0x8,这句是说无法访问0x8位置的内存,所以接下来,gdb收到了一个SIGSEGV的信号,说明触发了段错误:

(gdb) n

Program received signal SIGSEGV, Segmentation fault.
0x000000000041529b in runtime.gosched () at /home/roy/go/src/pkg/runtime/proc.c:1368
1368        runtime·mcall(runtime·gosched0);

此时,我们需要重启gdb的调试,否则执行任何命令都会报告段错误。

打印栈帧信息:

(gdb) info frame
Stack level 0, frame at 0x7ffff7e2ef48:
 rip = 0x400d80 in main.main (/home/roy/test.go:14); saved rip = 0x412e1f
 source language go.
 Arglist at 0x7ffff7e2ef38, args:
 Locals at 0x7ffff7e2ef38, Previous frame's sp is 0x7ffff7e2ef48
 Saved registers:
 rip at 0x7ffff7e2ef40

查看局部变量:

(gdb) info locals
i = 1234
s = "haha"

以Pretty-Print的方式打印变量:

(gdb) p s
$1 = "haha"

获取对象的长度,即cap:

(gdb) p $len(s)
$3 = 4

查看对象类型:

(gdb) whatis i
type = int
(gdb) whatis s
type = struct string

输入n单步执行到进入test函数,查看传递给函数的参数:

(gdb) n
main.test (s="haha", x=1234, r="test: haha 1234") at /home/roy/test.go:11
11      return r
(gdb) info args
s = "haha"
x = 1234
r = "test: haha 1234"

输入q,或者Ctrl-D退出:

(gdb) q
A debugging session is active.

    Inferior 1 [process 6079] will be killed.

Quit anyway? (y or n) y
前一篇: 两张图看懂GDT、GDTR、LDT、LDTR的关系 后一篇: Golang中Timer的陷阱

Captcha:
验证码

Email:

Content: (Support Markdown Syntax)


HeneganY  2021-07-19 01:18:40 From 127.0.0.1

viagra without a doctor prescription


Reislent  2021-08-14 00:47:12 From 127.0.0.1

program assistance ventolin inhaler coupons inhaler brands levalbuterol inhaler


taitare  2021-09-09 03:53:42 From 127.0.0.1

https://buystromectolon.com/ - ivermectin for dogs


appearedo  2021-10-11 02:28:10 From 127.0.0.1

Prednisone


prednisone dosage   2021-11-17 04:31:59 From 127.0.0.1

dapoxetine 30mg price priligy cheapest uk viagra with dapoxetine first orgams how long does it take for dapoxetine to work


Alfredrip  2021-12-08 01:28:14 From 127.0.0.1

adult virtual sex games sex games for android free download sex games to play with girlfriend


viagra professional   2021-12-08 02:54:39 From 127.0.0.1

ivermectin-pyrantel ivermectin stromectol how long does ivermectin keep working when to start puppy on ivermectin 6 months


sweelay  2021-12-08 21:48:20 From 127.0.0.1

cheap cialis online canadian pharmacy


cialis shelf life   2021-12-21 16:34:33 From 127.0.0.1

viagra generika prezzi del viagra viagra per durare di piГ№ dove acquistare viagra online sicuro


viagra pfizer   2021-12-29 19:52:45 From 127.0.0.1

viagra nebenwirkungen acquistare viagra prezzo del viagra in farmacia pomata come viagra