Articles Catalogue
Preface
For children's shoes who want to learn how to debug gdb, searching online is a lot of information, too much information to know how to screen. Of course, the most effective way is to read the GDB manual. But for me who want to get started quickly, I need to master some of the most commonly used instructions and skills. During this period, I found a very good one. Powerful textbooks, feel like gdb bible—— 100-gdb-tips,100-gdb-tips-gitbook It is strongly recommended to refer to this document. Documents have basically covered the various operations and techniques used by gdb, but they are relatively independent and decentralized, there is no complete use process, I will summarize the overall use of a process.
Using gdb
Usually we write a program, the specific program is as follows; and compiled into an executable file with debugging information, and then loaded and debugged with gdb;
/* * demo learning gdb * gdb-sample.c */ #include <stdio.h> void func_a(int *p){ printf("%s:p is %d | valuse is %d \n",__func__,p,*p); } void func_b(int *p){ *p = 12345; func_a(p); } void func_c(int *p){ p = NULL; func_b(p); } int main(void) { int i = 0,j=0; int *p = &j; for(; i<6; i++){ if(i<2){ func_a(p); }else if(i<4){ func_b(p); }else{ func_c(p); } } return 0; }
gcc -g gdb-sample.c -o gdb-sample
Remember to take the - g option and compile it successfully and generate the executable gdb-sample.
General steps required for a complete process
1 Loader
gdb -q gdb-sample
2 View
2.1 View Function
The function interface of the current executable file can be viewed by using i functions or info functions.
(gdb) i functions All defined functions: File gdb-sample.c: void func_a(int *); void func_b(int *); void func_c(int *); int main(void); Non-debugging symbols: 0x0000000000400460 _init 0x0000000000400490 puts@plt 0x00000000004004a0 __stack_chk_fail@plt 0x00000000004004b0 printf@plt 0x00000000004004c0 __libc_start_main@plt 0x00000000004004e0 _start 0x0000000000400510 deregister_tm_clones 0x0000000000400550 register_tm_clones 0x0000000000400590 __do_global_dtors_aux 0x00000000004005b0 frame_dummy 0x00000000004006f0 __libc_csu_init 0x0000000000400760 __libc_csu_fini 0x0000000000400764 _fini
3 Setting breakpoints
3.1 Set breakpoints according to function names
(gdb) b main Breakpoint 1 at 0x400658: file gdb-sample.c, line 19.
3.2 Depending on the location of the program (lines)
(gdb) l 1 #include <stdio.h> 2 3 void func_a(int *p){ 4 printf("%s:p is %d | valuse is %d \n",__func__,p,*p); 5 } 6 7 void func_b(int *p){ 8 *p = 12345; 9 func_a(p); 10 } (gdb) 11 12 void func_c(int *p){ 13 p = NULL; 14 func_b(p); 15 } 16 17 18 19 int main(void) { 20 int i = 0,j=0; (gdb) 21 int *p = &j; 22 for(; i<6; i++){ 23 if(i<2){ 24 func_a(p); 25 }else if(i<4){ 26 func_b(p); 27 }else{ 28 func_c(p); 29 } 30 } (gdb) b 19 Breakpoint 3 at 0x400618: file gdb-sample.c, line 19.
Remove Breakpoint
(gdb) d Delete all breakpoints? (y or n) y
4 Running Program
Run or r is used to run the program. If it is interrupted by breakpoint, c can be used to continue running the program.
(gdb) r Starting program: /home/thinkpad/code/gdb-tips/core_dump/gdb-sample Breakpoint 1, main () at gdb-sample.c:19 19 int main(void) {
next or n can be used for single-step debugging; it will not enter the sub-function;
Step or s can be used for single-step debugging; it will enter the sub-function;
5 View Variables
Viewing variables can use print and p
(gdb) b main Breakpoint 6 at 0x400618: file gdb-sample.c, line 19. (gdb) r The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /home/zhaojh/code/gdb-tips/core_dump/gdb-sample Breakpoint 6, main () at gdb-sample.c:19 19 int main(void) { (gdb) n 20 int i = 0,j=0; (gdb) p i $1 = 0 (gdb) n 21 int *p = &j; (gdb) n 22 for(; i<6; i++){ (gdb) p p $2 = (int *) 0x7fffffffe2f8 (gdb) p *p $3 = 0 (gdb)
6 View Register
Registers can be viewed by using i r, info register, i register and info r.
(gdb) i r rax 0x7fffffffe2f8 140737488347896 rbx 0x0 0 rcx 0x0 0 rdx 0x7fffffffe408 140737488348168 rsi 0x7fffffffe3f8 140737488348152 rdi 0x1 1 rbp 0x7fffffffe310 0x7fffffffe310 rsp 0x7fffffffe2f0 0x7fffffffe2f0 r8 0x400710 4196112 r9 0x7ffff7de7ac0 140737351940800 r10 0x846 2118 r11 0x7ffff7a2d740 140737348032320 r12 0x4004a0 4195488 r13 0x7fffffffe3f0 140737488348144 r14 0x0 0 r15 0x0 0 rip 0x40063f 0x40063f <main+47> eflags 0x293 [ CF AF SF IF ] cs 0x33 51 ss 0x2b 43 ds 0x0 0 es 0x0 0 fs 0x0 0 gs 0x0 0
summary
Recorded some relatively simple and commonly used gdb instructions, as a starting point is better, more advanced debugging reference gdb bible—— 100-gdb-tips,100-gdb-tips-gitbook.
Reference resources
https://github.com/hellogcc/100-gdb-tips
https://wizardforcel.gitbooks.io/100-gdb-tips/content/