Debug Go program with GDB under Mac

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Installing GDB

MAC OS x installs Xcode with a 6.x version of GDB that only supports DWARF2. The go compiler file contains DWARFv3 debug information by default, so you need to upgrade the GDB version to 7.1 or more. Installation steps:

1 2 3 4 5 6
$ curl -O http://ftp.gnu.org/gnu/gdb/gdb-7.3.1.tar.gz$ tar -xzf gdb-3.7.1.tar.gzma$ cd gdb-7.3.1$ ./configure$ make$ [sudo] make install 

After installation through the above steps, the gdb7.3 will be installed to the /usr/local/bin/gdb original GDB 6.x /usr/bin/gdb . This time directly executes GDB, the default is to use GDB 6.x (depending on your $path settings). If you want to display a call to GDB 7.3, you have to use an absolute path /usr/local/bin/gdb . Because I need to use Xcode to debug the iOS program, so make sure GdB 6.x is still available, otherwise you can use /usr/local/bin/ gdb to replace the /usr/bin following, so that each call does not need to use the absolute path.
The rest is irrelevant to the operating system.

Use steps

1. Compile

1
$ go build -gcflags "-N -l" test.go 关闭内联优化,便于输出调试信息

2. Entering the GDB environment

1
$ sudo /usr/local/bin/gdb test   //默认的gdb是6.X版本,因此要指定路径

3. Load Golang run-time support

1
(gdb) source /usr/local/go/src/pkg/runtime/runtime-gdb.py

4. GDB under some debug commands

123456789Ten One A - - the - - - + - + A at - - - - - in - to + - the * $Panax Notoginseng - the + A the + - $ $ - -
(GDB) L Main.main #查看源代码代码(gdb) b #设置断点(GDB) Info b #查看断点信息Num Type Disp Enb Address What1 Breakpoint Keep y 0x0000000000002239 in Main.main at/users/kenshin/workspace/gogo/test.go:50(GDB) run #运行 until the breakpoint is triggered(GDB) n #继续执行下一行(GDB) s #进入函数/Method(GDB) info locals #查看局部变量maerr = {tab = 0x0, data = 0xc2000bdfc0}(GDB) info args #查看参数s = 0xc2000b6f00addr = "0.0.0.0:8000"(gdb) Help info(GDB) p addr #打印变量$6 = "0.0.0.0:8000"(GDB) c #继续执行 until the next breakpoint(GDB) Whatis s #查看变量类型type = struct Github.com/hoisie/web.server *(GDB) where/bt #查看堆栈信息, judging function call relationships#0 net.resolveinternetaddr (net= "UDP4") at/usr/local/go/src/pkg/net/ipsock.go:164#1 in net. Resolveudpaddr (net= "UDP4") at/usr/local/go/src/pkg/net/udpsock.go:45#2 in Main. Socketlisten (ip= "127.0.0.1") at/users/kenshin/workspace/gor/test.go:13(gdb) up #回到上一层调用函数#0 in net. Resolveudpaddr (net= "UDP4") at/usr/local/go/src/pkg/net/udpsock.go:45#1 in Main. Socketlisten (ip= "127.ma0.0.1") at/users/kenshin/workspace/gor/test.go:13(gdb) down #再进入下一层函数#0 net.resolveinternetaddr (net= "UDP4") at/usr/local/go/src/pkg/net/ipsock.go:164#1 in net. Resolveudpaddr (net= "UDP4") at/usr/local/go/src/pkg/net/udpsock.go:45#2 in Main. Socketlisten (ip= "127.0.0.1") at/users/kenshin/workspace/gor/test.go:13(GDB) Shell #输入shell, can enter into the shell, enter exit exit shell back to the GDB environmentbash-3.2# Exit

It is important to note that GDB support for slices, arrays, and strings data types for these golang is not very good, for example, p STR shows a map type of data

1 2 3 4 5 6
(gdb) p r   # r string := "process"$16 = {str = 0x2f0170 "process", len = 7}  (gdb) p r->  #可以通过`TAB`获得一些提示len  str(gdb) p r->str   $17 = (uint8 *) 0x2f0170 "process"

Now directly through the GDB debugging Golang is not very easy to use, output debugging information reading difficulties, goroutine inside the stack information is unable to track.

Now that there are so many open-source projects around go, hopefully soon there will be something like ipdb.

More GDB Commands Reference: HTTP://GOLANG.ORG/DOC/GDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.