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 sessionC source code test_scanf.c1 #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 #include23 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