记录一下常用的GDB命令,
首先要用GCC -g编译:
gcc -g [源文件1, 源文件2...] -o [编译后的可执行文件]
启动GDB:
gdb [编译后的可执行文件]
进入GDB后的信息:
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2) 7.4-2012.04 Copyright (C) 2012 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 "i686-linux-gnu". For bug reporting instructions, please see: <http://bugs.launchpad.net/gdb-linaro/>... Reading symbols from /home/peng1/Dropbox/CProgram/cpro1...done. (gdb)
好了,以后的命令都是在这个”(gdb)”的提示符下进行的
命令list [行号], 列出源代码,不加行号,默认从第一行开始,列出10行,再l则会列出下面的10行:
(gdb) l 1 #include 2 #define MAXLINE 1000 3 4 int mygetline(char line[], int max); 5 int strindex(char source[], char searchfor[]); 6 7 char pattern[]="ould"; 8 9 int main() 10 {
命令list [文件名]:[行号], 列出对应文件的源代码:
(gdb) l mygetlines.c:1 1 #include <stdio.h> 2 3 int mygetline(char s[], int lim) 4 { 5 int c, i; 6 i=0; 7 8 while(--lim>0 && (c=getchar())!=EOF && c!='\n') 9 { 10 s[i++]=c;
加断点:
break [行号] break [方法名] break [文件名]:[行号]
例如:
输入break main
(gdb) break main Breakpoint 1 at 0x8048470: file cpro1.c, line 10.
(gdb) break strindex.c:2 Breakpoint 3 at 0x80484f6: file strindex.c, line 2.
查看断点:
info break
例如:
(gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x08048470 in main at cpro1.c:10
好了,断点已经加好了,可以运行了:
run [命令行参数]
例如:
(gdb) run Starting program: /home/peng1/Dropbox/CProgram/cpro1 Breakpoint 1, main () at cpro1.c:10 10 {
单步执行:
step或s 单步执行,会跳入方法中 next或n 单步执行,不会跳入方法中
例如:
(gdb) s 12 int found=0; (gdb) s 14 while(mygetline(line, MAXLINE)>0) (gdb) s mygetline (s=0xbfffeec4 "\001", lim=1000) at cpro1.c:29 29 i=0; (gdb) s 31 while(--lim>0 && (c=getchar())!=EOF && c!='\n') (gdb) s
查看当前代码运行到的位置:
where
例如:
(gdb) where #0 mygetline (s=0xbfffeec4 "\n", lim=999) at cpro1.c:39 #1 0x080484cf in main () at cpro1.c:14
显示变量的值:
print [变量名] p [变量名] p [文件名]::[变量名]
例如:
(gdb) p c $1 = 10 (gdb) p s $2 = 0xbfffeec4 "\n"
继续执行到下一个断点:
continue c
退出GDB:
quit q