使用gdb调试:
源代码:
1 // gdb debug 2 // meet.c 3 # include<stdio.h> 4 # include<stdlib.h> 5 6 void greeting(char *temp1, char *temp2) 7 { 8 char name[400]; 9 strcpy(name, temp2); 10 printf("Hello %s %s \n", temp1, name); 11 } 12 13 int main(int argc, char *argv[] ) 14 { 15 greeting(argv[1], argv[2]); 16 printf("Bye %s %s \n", argv[1], argv[2]); 17 }
GDB常用命令:
b<function> |
在function处设置断点 |
b*mem |
在指定的绝对内存位置设置断点 |
infob |
显示相关断点信息 |
deleteb |
删除断点 |
inforeg |
显示相关当前寄存器状态信息 |
Infothread |
查看线程 |
stepior si |
执行一条指令 |
nextor n |
执行一个函数 |
c |
继续执行 |
finish |
执行到返回 |
bt |
回溯命令显示栈帧的名称 |
up/down |
向上或向下移动栈帧 |
printvar |
打印变量信息 |
X/ NT A |
检查内存,其中N表示要显示的单位数,T表示显示的数据类型(x:hex,d:dec,c:char,s:string,i:instruction),A表示绝对地址或像main这样的符号名称 |
quit |
退出gdb |
调试:
调试程序需执行一下指令,让程序重新编译,包含所需调试信息。
gcc -g -o meet meet.c zhon@zhon-Rev-1-0:~$ gdb meet GNU gdb (Ubuntu/Linaro 7.4-2012.02-0ubuntu2) 7.4-2012.02 Copyright (C) 2012 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". For bug reporting instructions, please see: <http://bugs.launchpad.net/gdb-linaro/>... Reading symbols from /home/zhon/meet...done. (gdb) run Mr Haxor Starting program: /home/zhon/meet Mr Haxor Hello Mr Haxor Bye Mr Haxor (gdb) b main Breakpoint 1 at 0x40064d: file meet.c, line 15. (gdb) print The history is empty. (gdb) list 6 void greeting(char *temp1, char *temp2) 7 { 8 char name[400]; 9 strcpy(name, temp2); 10 printf("Hello %s %s \n", temp1, name); 11 } 12 13 int main(int argc, char *argv[] ) 14 { 15 greeting(argv[1], argv[2]); (gdb) run Mr Haxor Starting program: /home/zhon/meet Mr Haxor Breakpoint 1, main (argc=3, argv=0x7fffffffe088) at meet.c:15 15 greeting(argv[1], argv[2]); (gdb) n Hello Mr Haxor 16 printf("Bye %s %s \n", argv[1], argv[2]); (gdb) n Bye Mr Haxor 17 } (gdb) p argv[1] $1 = 0x7fffffffe38e "Mr" (gdb) p argv[2] $2 = 0x7fffffffe391 "Haxor" (gdb) info b Num Type Disp Enb Address What 1 breakpoint keep y 0x000000000040064d in main at meet.c:15 breakpoint already hit 1 time (gdb) info reg rax 0xe 14 rbx 0x0 0 rcx 0xc 12 rdx 0x7ffff7dd59f0 140737351866864 rsi 0x7ffffff3 2147483635 rdi 0x0 0 rbp 0x7fffffffdfa0 0x7fffffffdfa0 rsp 0x7fffffffdf90 0x7fffffffdf90 r8 0x7ffff7dd2660 140737351853664 r9 0x7ffff7a5f1e4 140737348235748 r10 0x5 5 r11 0x246 582 r12 0x400500 4195584 r13 0x7fffffffe080 140737488347264 r14 0x0 0 r15 0x0 0 rip 0x400699 0x400699 <main+91> eflags 0x206 [ PF IF ] cs 0x33 51 ss 0x2b 43 ds 0x0 0 es 0x0 0 fs 0x0 0 ---Type <return> to continue, or q <return> to quit---q Quit (gdb) quit A debugging session is active. Inferior 1 [process 16297] will be killed. Quit anyway? (y or n) y 反汇编: (gdb) disassemble greeting Dump of assembler code for function greeting: 0x00000000004005c4 <+0>: push %rbp 0x00000000004005c5 <+1>: mov %rsp,%rbp 0x00000000004005c8 <+4>: sub $0x1b0,%rsp 0x00000000004005cf <+11>: mov %rdi,-0x1a8(%rbp) 0x00000000004005d6 <+18>: mov %rsi,-0x1b0(%rbp) 0x00000000004005dd <+25>: mov %fs:0x28,%rax 0x00000000004005e6 <+34>: mov %rax,-0x8(%rbp) 0x00000000004005ea <+38>: xor %eax,%eax 0x00000000004005ec <+40>: mov -0x1b0(%rbp),%rdx 0x00000000004005f3 <+47>: lea -0x1a0(%rbp),%rax 0x00000000004005fa <+54>: mov %rdx,%rsi 0x00000000004005fd <+57>: mov %rax,%rdi 0x0000000000400600 <+60>: callq 0x4004b0 <strcpy@plt> 0x0000000000400605 <+65>: mov $0x40078c,%eax 0x000000000040060a <+70>: lea -0x1a0(%rbp),%rdx 0x0000000000400611 <+77>: mov -0x1a8(%rbp),%rcx 0x0000000000400618 <+84>: mov %rcx,%rsi 0x000000000040061b <+87>: mov %rax,%rdi 0x000000000040061e <+90>: mov $0x0,%eax 0x0000000000400623 <+95>: callq 0x4004d0 <printf@plt> 0x0000000000400628 <+100>: mov -0x8(%rbp),%rax 0x000000000040062c <+104>: xor %fs:0x28,%rax 0x0000000000400635 <+113>: je 0x40063c <greeting+120> 0x0000000000400637 <+115>: callq 0x4004c0 <__stack_chk_fail@plt> 0x000000000040063c <+120>: leaveq 0x000000000040063d <+121>: retq End of assembler dump.