Отладка в GDB

download Отладка в GDB

If you can't read please download the document

Transcript of Отладка в GDB

  • 1. GDB

2. Arguments & environment Stack unused memory Heap Uninitialized data Initialized data Text 3. int main(int argc, char *argv[]) { int number; int *pointer; number = atoi(argv[1]); pointer = number; print(number); return 0; } void print(int *x) { printf("The number supplied is %dn", *x); } 4. $ gcc -o test test.c test.c: In function main: test.c:7:13: warning: assignment makes pointer from integer without a cast test.c: At top level: test.c:8:5: note: previous implicit declaration of print was here test.c: In function print: test.c:15:5: warning: incompatible implicit declaration of built-in function printf $ ./testSegmentation fault 5. $ gdb test Reading symbols from /home/user/test...(no debugging symbols found)...done. (gdb) run Starting program: /home/user/testProgram received signal SIGSEGV, Segmentation fault. 0x00007ffff7a82b35 in ?? () from /lib/x86_64-linux-gnu/libc.so.6 6. (gdb) backtrace #00x00007ffff7a82b35 in ?? () from /lib/x86_64-linux-gnu/libc.so.6 #10x00007ffff7a7f900 in atoi () from /lib/x86_64-linux-gnu/libc.so.6 #20x000000000040056b in main () 7. (gdb) x/10i $rip => 0x7ffff7a82b35:movzbl (%rbx),%eax 0x7ffff7a82b38:mov0x68(%r8),%r9 0x7ffff7a82b3c:mov%rbx,%r13 0x7ffff7a82b3f:movsbq %al,%rcx 0x7ffff7a82b43:testb$0x20,0x1(%r9,%rcx,2) 0x7ffff7a82b49:je0x7ffff7a82b65 0x7ffff7a82b4b:nopl0x0(%rax,%rax,1) 0x7ffff7a82b50:add$0x1,%r13 0x7ffff7a82b54:movzbl 0x0(%r13),%eax 0x7ffff7a82b59:movsbq %al,%rcx 8. 9. (gdb) info registers rax0x00 rbx0x00 rcx0x00 rdx0xa10 rsi0x00 rdi0x00 rbp0x7fffffffe1600x7fffffffe160 rsp0x7fffffffe0c00x7fffffffe0c0 rip0x7ffff7a82b350x7ffff7a82b35 eflags0x10283[ CF SF IF RF ] cs0x3351 ss0x2b43 ... 10. (gdb) info locals No symbol table info available. (gdb) info args No symbol table info available. (gdb) quit A debugging session is active. Inferior 1 [process 29043] will be killed. Quit anyway? (y or n) y 11. $ gcc-g-o test test.c $ gdb test Reading symbols from /home/ium/test...done. (gdb) list 1int main(int argc, char *argv[]) 2{ 3int number; 4int *pointer; 5 6number = atoi(argv[1]); 7pointer = number; 8print(number); 9 10return 0; 12. (gdb) break 6 Breakpoint 1 at 0x400553: file test.c, line 6. (gdb) run Starting program: /home/ium/testBreakpoint 1, main (argc=1, argv=0x7fffffffe248) at test.c:6 6number = atoi(argv[1]); (gdb) print argv[1] $1 = 0x0 13. (gdb) continue Continuing. Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7a82b35 in ?? () from /lib/x86_64-linux-gnu/libc.so.6 14. (gdb) delete Delete all breakpoints? (y or n) y (gdb) run 255 The program being debugged has been started already. Start it from the beginning? (y or n) y Starting program: /home/user/test 255 Program received signal SIGSEGV, Segmentation fault. 0x000000000040059f in print (x=0xff) at test.c:15 15printf("The number supplied is %dn", *x); 15. (gdb) backtrace #00x000000000040059f in print (x=0xff) at test.c:15 #10x0000000000400588 in main (argc=2, argv=0x7fffffffe228) at test.c:8 (gdb) info args x = 0xff (gdb) frame 1 #10x0000000000400588 in main (argc=2, argv=0x7fffffffe228) at test.c:8 8print(number); 16. (gdb) info locals number = 255 pointer = 0xff (gdb) frame 0 (gdb) x /5i $rip => 0x40059f : mov(%rax),%eax 0x4005a1 : mov%eax,%esi 0x4005a3 : mov$0x4006ac,%edi 0x4005a8 : mov$0x0,%eax 0x4005ad : callq0x400428 (gdb) x /s 0x4006ac 0x4006ac:"The number supplied is %dn" 17. (gdb) info registers rax0xff255 rbx0x00 rcx0x55 rdx0x40058f 4195727 rsi0x00 rdi0xff255 rbp0x7fffffffe1100x7fffffffe110 rsp0x7fffffffe1000x7fffffffe100 rip0x40059f 0x40059f eflags0x10206[ PF IF RF ] cs0x3351 ss0x2b43 18. 19. (gdb) disassemble print Dump of assembler code for function print: 0x000000000040058f :push%rbp 0x0000000000400590 :mov%rsp,%rbp 0x0000000000400593 :sub$0x10,%rsp 0x0000000000400597 :mov%rdi,-0x8(%rbp) 0x000000000040059b :mov-0x8(%rbp),%rax => 0x000000000040059f :mov(%rax),%eax 0x00000000004005a1 :mov%eax,%esi 0x00000000004005a3 :mov$0x4006ac,%edi 0x00000000004005a8 :mov$0x0,%eax 0x00000000004005ad :callq0x400428 0x00000000004005b2 :leaveq0x00000000004005b3 :retq End of assembler dump. 20. 21. (gdb) x /4xg $rsp 0x7fffffffe170: 0x00000000000000000x00000000000000ff 0x7fffffffe180: 0x00007fffffffe1b00x0000000000400588 (gdb) print $rbp $1 = (void *) 0x7fffffffe180 22. 23.