C指针原理(30)-C语言-LINUX/UNIX环境下调试
file 文件名
在gdb中載入某可執行文件
break
設置斷點
支持如下形式:
break 行號;
break 函數名稱;
break 行號/函數名稱 if 條件
info
查看和可執行程序相關的
各種信息
kill
終止正在調試的程序
顯示變量或表達式的值
set args
設置調試程序的運行參數
“set args 參數列表”
delete
刪除設置的某個斷點
或觀測點
delete后可以使用breakpoints、
checkpoint、display、mem和
tracepoints。可使用
help delete查看
clear
刪除設置在指定行號
或函數上的斷點
continue
從斷點處繼續執行程序
list
列出gdb中可加載的
程序代碼
不帶任何參數使用list命令時,
會從開始位置列出所有代碼,
同時list還支持列出指定行號
之間的代碼
watch
在程序中設置觀測點
如果數據改變,將給出
變化前后的情況
run
運行在gdb中可加載的程序
next
單步執行程序
step
進入所調用的函數內部,
查看執行情況
退出調用函數,回到調用處
使用finish命令
whatis
查看變量或函數類型
調用格式為“whatis 變量名/
函數名
ptype
顯示數據結構定義情況
與whatis不同的是,
ptype可以顯示類或
數據結構的定義情況
make
編譯程序
quit
退出
一、gdb,在shell窗口調試
main.c內容:
main.c
#include <stdio.h>
int main()
{
int y=0;
for (int i=0;i<10;i++){
y+=i;
}
return 0;
}
編譯:
xxxxxxx@xxxxxxx-desktop:~/test$ gcc -ggdb -std=c99 -o main main.c
啟動gdb
1、gdb 執行文件名
xxxxxxx@xxxxxxx-desktop:~/test$ gdb main
GNU gdb (GDB) 7.1-ubuntu
Copyright ? 2010 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 “i486-linux-gnu”.
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/;…
Reading symbols from /home/xxxxxxx/test/main…done.
2、list為列出源代碼
(gdb) list
1
2 #include <stdio.h>
3 int main()
4 {
5 int y=0;
6 for (int i=0;i<10;i++){
7 y+=i;
8 }
9 return 0;
10 }
3、運行
(gdb) run
Starting program: /home/xxxxxxx/test/main
Program exited normally.
4、退出gdb
(gdb) quit
5、啟動gdb后,再設置要加載的文件
(gdb) file main
Reading symbols from /home/xxxxxxx/test/main…done.
6、設置斷點,,使用break行號
(gdb) list
warning: Source file is more recent than executable.
1
2 #include <stdio.h>
3 int main()
4 {
5 int y=0;
6 for (int i=0;i<10;i++){
7 y+=i;
8 }
9 return 0;
10 }
(gdb) break 7
Breakpoint 1 at 0x80483ca: file main.c, line 7.
7、運行,run
(gdb) run
Starting program: /home/xxxxxxx/test/main
Breakpoint 1, main () at main.c:7
7 y+=i;
8、離開這個斷點,繼續運行
(gdb) c
Continuing.
9、監視變量
(gdb) watch y
Hardware watchpoint 2: y
10、檢查變量變化
(gdb) c
Continuing.
Hardware watchpoint 2: y
Old value = 1
New value = 3
main () at main.c:6
6 for (int i=0;i<10;i++){
(gdb) c
Continuing.
Breakpoint 1, main () at main.c:7
7 y+=i;
(gdb) c
Continuing.
Hardware watchpoint 2: y
Old value = 3
New value = 6
main () at main.c:6
6 for (int i=0;i<10;i++){
二、ddd,使用
可以在選擇行或某變量后,watch,break等按鈕,很方便,圖形方式調試
右中部的面板是一些流程調試
splint是一個動態檢查C語言程序安全弱點和編寫錯誤的程序.splint會進行
多種常規檢查,包括未使用的變量,類型不一致,使用未定義變量,無法執行的
代碼,忽略返回值,執行路徑未返回,無限循環等錯誤.
安裝:
root@dp:/home/dp/cursestest # cd /usr/ports/devel/splint
root@dp:/usr/ports/devel/splint # make install clean
總結
以上是生活随笔為你收集整理的C指针原理(30)-C语言-LINUX/UNIX环境下调试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C指针原理(29)-Ncurses-文本
- 下一篇: centos的glibc升级方案 'GL