Last active
September 18, 2018 06:50
-
-
Save joyhuang9473/11fdd4d23666b567d0c4653f0c3c39d0 to your computer and use it in GitHub Desktop.
Owen Hsu: Introduction to gdb https://www.slideshare.net/owenhsu/introduction-to-gdb-3790833
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
void bar(int *val) { | |
*val = 11; | |
val = NULL; | |
*val = 17; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void bar(int*); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "bar.h" | |
int foo = 3; | |
int main() | |
{ | |
foo = 8; | |
bar(&foo); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user@pc:~$ gcc -Wall -g -fPIC -shared bar.c -o libbar.so | |
user@pc:~$ gcc -Wall -g foo.c ./libbar.so -o foobar | |
user@pc:~$ gdb foobar | |
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1 | |
Copyright (C) 2016 Free Software Foundation, Inc. | |
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> | |
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-linux-gnu". | |
Type "show configuration" for configuration details. | |
For bug reporting instructions, please see: | |
<http://www.gnu.org/software/gdb/bugs/>. | |
Find the GDB manual and other documentation resources online at: | |
<http://www.gnu.org/software/gdb/documentation/>. | |
For help, type "help". | |
Type "apropos word" to search for commands related to "word"... | |
Reading symbols from foobar...done. | |
(gdb) break main | |
Breakpoint 1 at 0x40068a: file foo.c, line 5. | |
(gdb) display foo | |
1: foo = 3 | |
(gdb) run | |
Starting program: /home/user/foobar | |
Breakpoint 1, main () at foo.c:5 | |
5 foo = 8; | |
1: foo = 3 | |
(gdb) info shared | |
From To Syms Read Shared Object Library | |
0x00007ffff7dd7ac0 0x00007ffff7df5850 Yes /lib64/ld-linux-x86-64.so.2 | |
0x00007ffff7bd5550 0x00007ffff7bd5677 Yes ./libbar.so | |
0x00007ffff782a8b0 0x00007ffff797db04 Yes /lib/x86_64-linux-gnu/libc.so.6 | |
(gdb) next | |
6 bar(&foo); | |
1: foo = 8 | |
(gdb) step | |
bar (val=0x601038 <foo>) at bar.c:3 | |
3 *val = 11; | |
1: foo = 8 | |
(gdb) info stack | |
#0 bar (val=0x601038 <foo>) at bar.c:3 | |
#1 0x000000000040069e in main () at foo.c:6 | |
(gdb) list | |
1 #include <stdlib.h> | |
2 void bar(int *val) { | |
3 *val = 11; | |
4 val = NULL; | |
5 *val = 17; | |
6 } | |
(gdb) display val | |
2: val = (int *) 0x601038 <foo> | |
(gdb) print val | |
$1 = (int *) 0x601038 <foo> | |
(gdb) step | |
4 val = NULL; | |
1: foo = 11 | |
2: val = (int *) 0x601038 <foo> | |
(gdb) step | |
5 *val = 17; | |
1: foo = 11 | |
2: val = (int *) 0x0 | |
(gdb) step | |
Program received signal SIGSEGV, Segmentation fault. | |
0x00007ffff7bd566e in bar (val=0x0) at bar.c:5 | |
5 *val = 17; | |
1: foo = 11 | |
2: val = (int *) 0x0 | |
(gdb) break bar | |
Breakpoint 2 at 0x7ffff7bd5658: file bar.c, line 3. | |
(gdb) info breakpoints | |
Num Type Disp Enb Address What | |
1 breakpoint keep y 0x000000000040068a in main at foo.c:5 | |
breakpoint already hit 1 time | |
2 breakpoint keep y 0x00007ffff7bd5658 in bar at bar.c:3 | |
(gdb) delete 1 | |
(gdb) run | |
The program being debugged has been started already. | |
Start it from the beginning? (y or n) y | |
Starting program: /home/user/foobar | |
Breakpoint 2, bar (val=0x601038 <foo>) at bar.c:3 | |
3 *val = 11; | |
1: foo = 8 | |
2: val = (int *) 0x601038 <foo> | |
(gdb) step | |
4 val = NULL; | |
1: foo = 11 | |
2: val = (int *) 0x601038 <foo> | |
(gdb) shell vim bar.c | |
(gdb) shell gcc -Wall -g -fPIC -shared bar.c -o libbar.so | |
(gdb) run | |
The program being debugged has been started already. | |
Start it from the beginning? (y or n) y | |
Starting program: /home/user/foobar | |
Breakpoint 2, bar (val=0x601038 <foo>) at bar.c:3 | |
3 *val = 11; | |
1: foo = 8 | |
2: val = (int *) 0x601038 <foo> | |
(gdb) c | |
Continuing. | |
[Inferior 1 (process 15540) exited normally] | |
(gdb) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user@pc:~$ gcc -Wall -fPIC -shared bar.c -o libbar.so | |
user@pc:~$ gcc -Wall -g foo.c ./libbar.so -o foobar | |
user@pc:~$ gdb foobar | |
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1 | |
Copyright (C) 2016 Free Software Foundation, Inc. | |
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> | |
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-linux-gnu". | |
Type "show configuration" for configuration details. | |
For bug reporting instructions, please see: | |
<http://www.gnu.org/software/gdb/bugs/>. | |
Find the GDB manual and other documentation resources online at: | |
<http://www.gnu.org/software/gdb/documentation/>. | |
For help, type "help". | |
Type "apropos word" to search for commands related to "word"... | |
Reading symbols from foobar...done. | |
(gdb) watch foo | |
Hardware watchpoint 1: foo | |
(gdb) run | |
Starting program: /home/user/foobar | |
Hardware watchpoint 1: foo | |
Old value = 3 | |
New value = 8 | |
main () at foo.c:6 | |
6 bar(&foo); | |
(gdb) stepi | |
0x0000000000400699 6 bar(&foo); | |
(gdb) stepi | |
0x0000000000400560 in bar@plt () | |
(gdb) info stack | |
#0 0x0000000000400560 in bar@plt () | |
#1 0x000000000040069e in main () at foo.c:6 | |
(gdb) info frame | |
Stack level 0, frame at 0x7fffffffe480: | |
rip = 0x400560 in bar@plt; saved rip = 0x40069e | |
called by frame at 0x7fffffffe490 | |
Arglist at 0x7fffffffe470, args: | |
Locals at 0x7fffffffe470, Previous frame's sp is 0x7fffffffe480 | |
Saved registers: | |
rip at 0x7fffffffe478 | |
(gdb) step | |
Single stepping until exit from function bar@plt, | |
which has no line number information. | |
0x00007ffff7bd5650 in bar () from ./libbar.so | |
(gdb) info stack | |
#0 0x00007ffff7bd5650 in bar () from ./libbar.so | |
#1 0x000000000040069e in main () at foo.c:6 | |
(gdb) infor frame | |
Undefined command: "infor". Try "help". | |
(gdb) info frame | |
Stack level 0, frame at 0x7fffffffe480: | |
rip = 0x7ffff7bd5650 in bar; saved rip = 0x40069e | |
called by frame at 0x7fffffffe490 | |
Arglist at 0x7fffffffe470, args: | |
Locals at 0x7fffffffe470, Previous frame's sp is 0x7fffffffe480 | |
Saved registers: | |
rip at 0x7fffffffe478 | |
(gdb) stepi | |
0x00007ffff7bd5651 in bar () from ./libbar.so | |
(gdb) stepi | |
0x00007ffff7bd5654 in bar () from ./libbar.so | |
(gdb) stepi | |
0x00007ffff7bd5658 in bar () from ./libbar.so | |
(gdb) stepi | |
0x00007ffff7bd565c in bar () from ./libbar.so | |
(gdb) stepi | |
Hardware watchpoint 1: foo | |
Old value = 8 | |
New value = 11 | |
0x00007ffff7bd5662 in bar () from ./libbar.so | |
(gdb) stepi | |
0x00007ffff7bd566a in bar () from ./libbar.so | |
(gdb) stepi | |
0x00007ffff7bd566e in bar () from ./libbar.so | |
(gdb) stepi | |
Program received signal SIGSEGV, Segmentation fault. | |
0x00007ffff7bd566e in bar () from ./libbar.so | |
(gdb) disassemble 0x00007ffff7bd566e | |
Dump of assembler code for function bar: | |
0x00007ffff7bd5650 <+0>: push %rbp | |
0x00007ffff7bd5651 <+1>: mov %rsp,%rbp | |
0x00007ffff7bd5654 <+4>: mov %rdi,-0x8(%rbp) | |
0x00007ffff7bd5658 <+8>: mov -0x8(%rbp),%rax | |
0x00007ffff7bd565c <+12>: movl $0xb,(%rax) | |
0x00007ffff7bd5662 <+18>: movq $0x0,-0x8(%rbp) | |
0x00007ffff7bd566a <+26>: mov -0x8(%rbp),%rax | |
=> 0x00007ffff7bd566e <+30>: movl $0x11,(%rax) | |
0x00007ffff7bd5674 <+36>: nop | |
0x00007ffff7bd5675 <+37>: pop %rbp | |
0x00007ffff7bd5676 <+38>: retq | |
End of assembler dump. | |
(gdb) run | |
The program being debugged has been started already. | |
Start it from the beginning? (y or n) y | |
Starting program: /home/user/foobar | |
Hardware watchpoint 1: foo | |
Old value = 3 | |
New value = 8 | |
main () at foo.c:6 | |
6 bar(&foo); | |
(gdb) step | |
Hardware watchpoint 1: foo | |
Old value = 8 | |
New value = 11 | |
0x00007ffff7bd5662 in bar () from ./libbar.so | |
(gdb) stepi | |
0x00007ffff7bd566a in bar () from ./libbar.so | |
(gdb) stepi | |
0x00007ffff7bd566e in bar () from ./libbar.so | |
(gdb) stepi | |
Program received signal SIGSEGV, Segmentation fault. | |
0x00007ffff7bd566e in bar () from ./libbar.so | |
(gdb) disassemble 0x00007ffff7bd566e | |
Dump of assembler code for function bar: | |
0x00007ffff7bd5650 <+0>: push %rbp | |
0x00007ffff7bd5651 <+1>: mov %rsp,%rbp | |
0x00007ffff7bd5654 <+4>: mov %rdi,-0x8(%rbp) | |
0x00007ffff7bd5658 <+8>: mov -0x8(%rbp),%rax | |
0x00007ffff7bd565c <+12>: movl $0xb,(%rax) | |
0x00007ffff7bd5662 <+18>: movq $0x0,-0x8(%rbp) | |
0x00007ffff7bd566a <+26>: mov -0x8(%rbp),%rax | |
=> 0x00007ffff7bd566e <+30>: movl $0x11,(%rax) | |
0x00007ffff7bd5674 <+36>: nop | |
0x00007ffff7bd5675 <+37>: pop %rbp | |
0x00007ffff7bd5676 <+38>: retq | |
End of assembler dump. | |
(gdb) shell objdump -d libbar.so | less | |
(gdb) shell vim libbar.so | |
(gdb) run | |
The program being debugged has been started already. | |
Start it from the beginning? (y or n) y | |
Starting program: /home/user/foobar | |
Hardware watchpoint 1: foo | |
Old value = 3 | |
New value = 8 | |
main () at foo.c:6 | |
6 bar(&foo); | |
(gdb) step | |
Hardware watchpoint 1: foo | |
Old value = 8 | |
New value = 11 | |
0x00007ffff7bd5662 in bar () from ./libbar.so | |
(gdb) step | |
Single stepping until exit from function bar, | |
which has no line number information. | |
main () at foo.c:8 | |
8 return 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment