博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
gdb调试技巧
阅读量:5283 次
发布时间:2019-06-14

本文共 3324 字,大约阅读时间需要 11 分钟。

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

1       #include 
/* include the information about the standard library */23 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) list1       #include 
23 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 mainBreakpoint 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:55 int n=0;# We type next to move to the next command of the program.(gdb) next6 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 accessProgram 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) quitThe program is running. Exit anyway? (y or n) y

 

转载于:https://www.cnblogs.com/hu983/p/5394447.html

你可能感兴趣的文章
kill新号专题
查看>>
MVC学习系列——Model验证扩展
查看>>
自定义返回模型
查看>>
HDU 4122
查看>>
Suite3.4.7和Keil u3自带fx2.h、fx2regs.h文件的异同
查看>>
打飞机游戏【来源于Crossin的编程教室 http://chuansong.me/account/crossincode 】
查看>>
[LeetCode] Merge Intervals
查看>>
【翻译自mos文章】当点击完 finishbutton后,dbca 或者dbua hang住
查看>>
Linux编程简介——gcc
查看>>
2019年春季学期第四周作业
查看>>
MVC4.0 利用IActionFilter实现简单的后台操作日志功能
查看>>
rotate the clock
查看>>
bugku 变量
查看>>
数据库01 /Mysql初识以及基本命令操作
查看>>
数据库02 /MySQL基础数据类型以及多表之间建立联系
查看>>
Python并发编程04/多线程
查看>>
CF461B Appleman and Tree
查看>>
CF219D Choosing Capital for Treeland
查看>>
杂七杂八的小笔记本
查看>>
51Nod1353 树
查看>>