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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FreeRTOS是一个开源的实时操作系统,它提供了一套用嵌入式系统的任务调度和管理机制。GDB(GNU Debugger)是一个功能强大的调试工具,可以用于调试C/C++程序。在使用FreeRTOS进行开发时,可以结合GDB进行调试。 要在FreeRTOS中使用GDB进行调试,需要进行以下几个步骤: 1. 配置编译器:首先,需要确保你的编译器支持GDB调试功能。常用的编译器如GCC和Keil都支持GDB调试。 2. 编译选项:在编译FreeRTOS应用程序时,需要添加一些编译选项以支持GDB调试。例如,在GCC中,可以使用"-g"选项来生成调试信息。 3. 连接器脚本:在链接应用程序时,需要使用连接器脚本来指定调试信息的位置。连接器脚本可以告诉GDB在哪里找到符号表和调试信息。 4. 启动GDB调试:在编译和链接完成后,可以使用GDB启动调试会话。可以通过命令行输入"gdb"命令来启动GDB,并使用"target remote"命令连接到目标设备。 5. 设置断点:在GDB中,可以使用"break"命令设置断点。可以设置函数断点、行号断点或地址断点等。 6. 执行调试:一旦设置好断点,可以使用GDB调试命令来执行程序。可以使用"run"命令来运行程序,使用"step"命令逐行执行,使用"next"命令执行下一行,使用"continue"命令继续执行等。 7. 查看变量:在调试过程中,可以使用GDB的"print"命令来查看变量的值。可以使用"info locals"命令查看局部变量,使用"info global"命令查看全局变量等。 8. 结束调试:当调试完成后,可以使用GDB的"quit"命令退出调试会话。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值