gdb调试技巧
2016-04-15 10:43
363 查看
Basic GDB commands:
run – Start execution of a program.
break line-number – Insert a breakpoint at the given line number. When a running program reaches a breakpoint, execution stops and control returns to the debugger.
break function-name – Insert a breakpoint at the first line of the named function. Commonly break main is used to stop execution at the beginning of the program.
cont – Continue execution after a breakpoint.
print expression – Display the value of an expression.
next – Execute a single line on the program, but treat function calls as a single line. This command is used to skip over function calls.
list – List the source program.
where – Print the list of currently active functions.
info breakpoints – Print a list of breakpoints.
delete – delete a breakpoint.
quit – Stop the debugger.
An example GDB session
C source code test_scanf.c
Debugger Session
run – Start execution of a program.
break line-number – Insert a breakpoint at the given line number. When a running program reaches a breakpoint, execution stops and control returns to the debugger.
break function-name – Insert a breakpoint at the first line of the named function. Commonly break main is used to stop execution at the beginning of the program.
cont – Continue execution after a breakpoint.
print expression – Display the value of an expression.
next – Execute a single line on the program, but treat function calls as a single line. This command is used to skip over function calls.
list – List the source program.
where – Print the list of currently active functions.
info breakpoints – Print a list of breakpoints.
delete – delete a breakpoint.
quit – Stop the debugger.
An example GDB session
C source code test_scanf.c
1 #include <stdio.h> /* include the information about the standard library */ 2 3 main() 4 { 5 int n=0; 6 scanf("%d",n); /* read a decimal integer from the standard input */ 7 printf("%d",n); /* print a decimal integer on the standard output */ 8 return 0; 9 }
Debugger Session
# The source code test_scanf.c is compiled with the debugging option –g. # The executable code test_scanf is produced. queen(1)% g++ -g -otest_scanf test_scanf.c #GDB debugger is started. It will trace the execution of the program test_scanf. queen(2)% gdb test_scanf # We list the source code to be examined. (gdb) list 1 #include <stdio.h> 2 3 main() 4 { 5 int n=0; 6 scanf("%d",n); 7 printf("%d",n); 8 return 0; 9 } # We set up a break-point. (gdb) break main Breakpoint 1 at 0x1062c: file test_scanf.c, line 5. # We start the execution of the program (gdb) run # We are informed that the break-point is set at line 5. This is the first line of # function main. Breakpoint 1, main () at test_scanf.c:5 5 int n=0; # We type next to move to the next command of the program. (gdb) next 6 scanf("%d",n); # We are informed that reading will be performed next. We type next again # to execute it. (gdb) next # When prompted for an input we type 5. 5 # We receive the information about the failure of the execution. # The interrupt signal is SIGSEGV, e.g. illegal storage access Program received signal SIGSEGV, Segmentation fault. 0xff30f640 in number () from /usr/lib/libc.so.1 # We print out the stack trace, i.e. the sequence of functions, which were # active when the program died. (gdb) where #0 0xff30f640 in number () from /usr/lib/libc.so.1 #1 0xff30ec8c in __doscan_u () from /usr/lib/libc.so.1 #2 0xff30e368 in _doscan () from /usr/lib/libc.so.1 #3 0xff3145a0 in vscanf () from /usr/lib/libc.so.1 #4 0xff313398 in scanf () from /usr/lib/libc.so.1 #5 0x10644 in main () at test_scanf.c:6 # We examine the stack trace. We see a list of library functions called # by scanf. The conclusion is: This was reading that failed. We check # the documentation of scanf. The argument of scanf has to be a pointer. # We need to substitute variable name n by its address &n. Next we quit # the debugger to make the correction. (gdb) quit The program is running. Exit anyway? (y or n) y
相关文章推荐
- 软件工程---相互观看与评价
- 【软件工程】复利计算器--结对编程3.0评论博客
- AE开发能否实现TOC Control里添加多个Data Frame
- 表单事件,onblur,onfocus,焦点
- 链表
- Centos 6.5 安装oracle
- SpatialHadoop:有效的分析你的空间数据
- java中的23种设计模式
- 2016.04.15
- TP下的session.memcache
- Hash概率问题
- Java技术晋升之路
- jQuery冒泡事件阻止
- 复利计算器评论
- Linux使用笔记
- 二叉树层次遍历
- C#+ AE 注意问题汇总(不断更新)
- MFC线程编程
- 插入排序
- 解决org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'localSessi