設備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

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