gdb 调试 golang


  1. deepin操作系统
  2. apt-get install gdb
  3. 还缺个小demo
package main

import (

func test(s string, i int) string {
	return fmt.Sprintf("s: %s, i: %d", s, i)

func main() {
	s := "hello word!"
	i := 123
	fmt.Println(test(s, i))


  1. go build -gcflags “-N -l” 关闭优化
  2. gdb go
root@nihao-PC:/coding/go# gdb go
GNU gdb (Debian 7.12-6+b2)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>
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:
Find the GDB manual and other documentation resources online at:
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from go...done.
warning: File "/usr/local/go/src/runtime/" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
	add-auto-load-safe-path /usr/local/go/src/runtime/
line to your configuration file "/root/.gdbinit".
To completely disable this security protection add
	set auto-load safe-path /
line to your configuration file "/root/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
	info "(gdb)Auto-loading safe path"


  1. vim /root/.gdbinit
    根据警告提示,新建.gdbinit 文件写入以下内容,此操作是为了解决gdb 不识别goroutine 相关的命令参数
	set auto-load safe-path /
	add-auto-load-safe-path /usr/local/go/src/runtime/

gdb 调试 golang 部分命令

# l 打印main方法,一般打印支持前后10行
l main.main 

# l main.go:linenumber 指定文件和行号打印前后10行的代码
l main.go:12

# b main.main 指定函数设置断点
b main.main

# b main.go 12 指定文件行设置断点
b main.go 12

# info [args] 查询[参数]相关的信息
info breakpoints # 查看断点信息
info args 		 # 查看参数信息
info goroutines  # 查看协程信息
info frame       # 查看栈帧信息
info locals      # 查看局部变量

# bt 查看调用栈信息
bt               # 查看当前调用栈信息
goroutine 1 bt   # 查看协程1的调用栈信息
goroutine 32 bt  # 查看协程32的调用栈信息

# p [arg] 查看[变量]信息
p s              # 查看s的值
p i              # 查看i的值
p $len(s)        # 查看s变量的长度

# whatis 用于查看对象的类型
whatis s
whatis i

# n next 跳转到下一个断点
n 				 # 跳转到下一个断点

# c continue 继续执行
c                # 继续下一步

# x/3xw &[变量] // 查看 [变量] 内存数据
x/3xw &s 		 # 查看 s 内存数据

# q quit 退出调试

发布了10 篇原创文章 · 获赞 1 · 访问量 1万+


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客