1 / 23

GDB 调试技术

GDB 调试技术. GDB功能. 设置断点 监视程序变量的值 程序的单步执行 查看 / 修改变量的值 查看寄存器的堆栈情况 远程调试 调试线程. 远程调试. 思想:在 GDB 术语中,有一小段驻留在目标机上的代码被称为“调试桩” (debugging stub) ,也称为“调试代理” (debugging agent) ,其责任就是在目标机上实现由主机上的调试器发送过来的调试命令,此外还有向主机调试器报告目标机上发生的异常事件,调试代理与 GDB 主机之间的通信遵循“ GDB 远程串行协议 (Remote Serial Protocol) ”。. 远程调试.

Download Presentation

GDB 调试技术

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GDB调试技术

  2. GDB功能 设置断点 监视程序变量的值 程序的单步执行 查看/修改变量的值 查看寄存器的堆栈情况 远程调试 调试线程

  3. 远程调试 思想:在GDB术语中,有一小段驻留在目标机上的代码被称为“调试桩”(debugging stub),也称为“调试代理”(debugging agent),其责任就是在目标机上实现由主机上的调试器发送过来的调试命令,此外还有向主机调试器报告目标机上发生的异常事件,调试代理与GDB主机之间的通信遵循“GDB远程串行协议(Remote Serial Protocol)”。

  4. 远程调试 main() { int i; long result = 0; for(i=1; i<=100; i++) { result += i; } printf("result[1-100] = %d \n", result ); printf("result[1-250] = %d \n", func(250) ); } #include <stdio.h> int func(int n) { int sum=0,i; for(i=0; i<n; i++) { sum+=i; } return sum; }

  5. gdb常用命令 list:列表显示源代码。 run:执行当前被调试的程序 kill:终止正在调试的程序。 quit:终止gdb file:装入想要调试的可执行文件。 break:在代码里设置断点,程序执行到这里时挂起    next:执行一行源代码但不进入函数内部。 step:执行一行源代码而且进入函数内部。 watch:监视一个变量的值

  6. 启动gdb 编译调试程序 $gcc –g test.c –o test

  7. 启动gdb 编译调试程序 $gcc –g test.c –o test

  8. 列出源代码 List命令 (gdb) list line1,line2

  9. 列出源代码 list 1 list 17,22 list func

  10. 列出源代码

  11. 断点管理 1、设置断点

  12. 断点管理 2、查看断点

  13. 断点管理 3、管理断点

  14. 断点管理

  15. 执行程序 run : continue: 共同点:遇到用户设置的断点后会停下来。 不同点:run 从程序开始执行。 continue只能在程序运行后执行,用在程序被断点停住以后,通过continue命令继续执行。

  16. 执行程序 run : continue: 共同点:遇到用户设置的断点后会停下来。 不同点:run 从程序开始执行。 continue只能在程序运行后执行,用在程序被断点停住以后,通过continue命令继续执行。

  17. 执行程序 单步执行: next:执行一行源代码但不进入函数内部。 step:执行一行源代码而且进入函数内部。

  18. 显示程序变量 gdb提供print和display两条显示命令,这两条命令基本功能相同,区别在于display可以锁定显示的变量或者寄存器,当执行程序时,每执行一次都会显示被锁定的变量。print命令只能在调用的时候显示指定的变量或者寄存器值。

  19. 显示程序变量

  20. 显示程序变量

  21. 显示程序变量 watch <expr>为表达式(变量)expr设置一个观察点,当表达式值有变化时,程序会停止执行。

  22. 用GDB调试有问题的程序 通过调试一个有问题的程序,进一步熟练使用VI操作,而且熟练掌握GCC编译命令及Gdb的调试命令,通过对有问题程序的跟踪调试,进一步提高发现问题和解决问题的能力。 代码如下:

  23. 用GDB调试有问题的程序 #include <stdio.h> int display1(char *string); int display2(char *string); int main () { char string[] = "Embedded Linux"; display1 (string); display2 (string); } int display1 (char *string) { printf ("The original string is %s \n", string); } int display2 (char *string1) { char *string2; int size,i; size = strlen (string1); string2 = (char *) malloc (size + 1); for (i = 0; i < size; i++) string2[size - i] = string1[i]; string2[size+1] = ' '; printf("The string afterward is %s\n",string2); }

More Related