Purpose | Example |
the program | $ cat inf.c |
| #include |
| |
| int main() { |
| int x; |
| |
| x = 1; |
| while ( x < 4 ) { |
| printf("The value of x = %d\n", x++); |
| } |
| |
| return 0; |
| } |
| |
compile with -g | $ cc -g inf.c -o inf |
execute with gdb | $ gdb inf |
| GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-32.el5_6.2) |
| Copyright (C) 2009 Free Software Foundation, Inc. |
| License GPLv3+: GNU GPL version 3 or later |
| 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 "x86_64-redhat-linux-gnu". |
| For bug reporting instructions, please see: |
| ... |
| Reading symbols from /home/sanjeev/_/inf...done. |
list (l) | (gdb) list |
| 1 #include |
| 2 |
| 3 int main() { |
| 4 int x; |
| 5 |
| 6 x = 1; |
| 7 while ( x < 4 ) { |
| 8 printf("The value of x = %d\n", x++); |
| 9 } |
| 10 |
break (b) at line 1 | (gdb) break 1 |
| Breakpoint 1 at 0x4004a0: file inf.c, line 1. |
run (r) up to 1 | (gdb) run |
| Starting program: /home/sanjeev/_/inf |
| warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000 |
| |
| Breakpoint 1, main () at inf.c:6 |
| 6 x = 1; |
print (p) the value of x | (gdb) print x |
| $1 = 0 |
go to next line | (gdb) next |
| 7 while ( x < 4 ) { |
statement to run | (gdb) p x |
value of x | $2 = 1 |
| (gdb) n |
| 8 printf("The value of x = %d\n", x++); |
| (gdb) p x |
| $3 = 1 |
| (gdb) n |
output of the program | The value of x = 1 |
| 7 while ( x < 4 ) { |
| (gdb) p x |
| $4 = 2 |
| (gdb) n |
| 8 printf("The value of x = %d\n", x++); |
| (gdb) p x |
| $5 = 2 |
print address of x | (gdb) p &x |
| $6 = (int *) 0x7fffffffe6cc |
| (gdb) n |
output of the program | The value of x = 2 |
| 7 while ( x < 4 ) { |
| (gdb) p x |
| $7 = 3 |
| (gdb) n |
| 8 printf("The value of x = %d\n", x++); |
| (gdb) p x |
| $8 = 3 |
| (gdb) n |
output of the program | The value of x = 3 |
| 7 while ( x < 4 ) { |
| (gdb) p &x |
| $9 = (int *) 0x7fffffffe6cc |
| (gdb) p x |
| $10 = 4 |
print 4 bytes at &x | (gdb) x/4b &x |
| 0x7fffffffe6cc: 00000100 00000000 00000000 00000000 |
print 1 word at &x | (gdb) x/1w &x |
| 0x7fffffffe6cc: 00000000000000000000000000000100 |
| (gdb) n |
| 11 return 0; |
| (gdb) n |
| 12 } |
| (gdb) n |
| 0x0000003590c1d994 in __libc_start_main () from /lib64/libc.so.6 |
quit gdb | (gdb) quit |
| A debugging session is active. |
| |
| Inferior 1 [process 21997] will be killed. |
| |
confirm exit | Quit anyway? (y or n) y |