设备GDB调试

1. 概述

GDB调试的三种方式:

  1. 目标板直接使用GDB进行调试。

  2. 目标板使用gdbserver,主机使用xxx-linux-gdb作为客户端。

  3. 目标板使用ulimit -c unlimited,生成core文件;然后主机使用xxx-linux-gdb ./test ./core。

Brendan Gregg关于gdb介绍《gdb Debugging Full Example (Tutorial): ncurses》。

2. 目标板直接使用GDB进行调试

原文:https://www.cnblogs.com/arnoldlu/p/9633254.html

交叉编译gdb和gdbserver:https://blog.csdn.net/mayue_web/article/details/103970045

3. gdb+gdbserver远程调试

原文:https://www.cnblogs.com/arnoldlu/p/9633254.html

交叉编译gdbserver:https://blog.csdn.net/mayue_web/article/details/103970045

4. 通过core+gdb离线分析

该部分转载自:
Linux下gdb调试生成core文件并调试core文件
https://blog.csdn.net/wkd_007/article/details/79757289

4.1 什么是core文件?

有问题的程序运行后,产生“段错误 (核心已转储)”时生成的具有堆栈信息和调试信息的文件。

编译时需要加 -g 选项使程序生成调试信息: gcc -g core_test.c -o core_test

4.2 怎样配置生成 core 文件

(1) core文件开关
①使用 ulimit -c 查看core开关,如果为0表示关闭,不会生成core文件;
②使用 ulimit -c [filesize] 设置core文件大小,当最小设置为4之后才会生成core文件;
③使用 ulimit -c unlimited 设置core文件大小为不限制,这是常用的做法;
④如果需要开机就执行,则需要将这句命令写到 /etc/profile 等文件。
在这里插入图片描述
(2) core文件命名和保存路径
①core文件有默认的名称和路径,但为了方便,我们通常会自己命名和指定保存路径;
②可以通过 /proc/sys/kernel/core_pattern 设置 core 文件名和保存路径,方法如下:

echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
命名的参数列表: 
%p - insert pid into filename 添加pid 
%u - insert current uid into filename 添加当前uid 
%g - insert current gid into filename 添加当前gid 
%s - insert signal that caused the coredump into the filename 添加导致产生core的信号 
%t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间 
%h - insert hostname where the coredump happened into filename 添加主机名 
%e - insert coredumping executable name into filename 添加命令名。

4.3 调试core文件

(1)方法1: gdb [exec file] [core file] 然后执行bt看堆栈信息:
在这里插入图片描述
(2)方法②: gdb -c [core file],然后 file [exec file],最后再使用 bt 查看错误位置:
在这里插入图片描述

5. 参考资料

https://www.cnblogs.com/arnoldlu/p/9633254.html
https://blog.csdn.net/wkd_007/article/details/79757289
http://blog.chinaunix.net/uid-25311424-id-4439482.html
http://www.360doc.com/content/16/0315/14/14679766_542391141.shtml

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章