gdb 调试 golang

准备

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

import (
	"fmt"
)

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) 7.12.0.20161007-git
Copyright (C) 2016 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 go...done.
warning: File "/usr/local/go/src/runtime/runtime-gdb.py" 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/runtime-gdb.py
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"

针对gdb运行起来出现在前面的警告,我们需要阅读和理解,并进行相应的操作。

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

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 退出调试
q

发布了10 篇原创文章 · 获赞 1 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

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

分享到微信朋友圈

×

扫一扫,手机浏览