gdb调试技巧

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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值