gdb调试应用笔记

发布时间 2023-09-25 09:28:09作者: njit-sam

GDB介绍(来自man手册):

The purpose of a debugger such as GDB is to allow you to see what is going
on "inside" another program while it executes -- or what another program
was doing at the moment it crashed.

翻译就是:调试器(如GBD)的目标工作就是让你看看程序执行时在内部是怎么样运行的或者在它挂掉的时候是怎么运行的。

GDB can do four main kinds of things (plus other things in support of
these) to help you catch bugs in the act:

  • Start your program, specifying anything that might affect its behavior.
  • Make your program stop on specified conditions.
  • Examine what has happened, when your program has stopped.
  • Change things in your program, so you can experiment with correcting
    the effects of one bug and go on to learn about another.

翻译就是:GBD主要可以做四件事帮你去抓BUG.

  • 开始你的程序,指定一些可能影响程序运行的事情
  • 让你的程序停止在特定的条件下
  • 当你的程序停止运行时,检查发生了什么。
  • 调整你的程序,这样你就可以尝试修正一个bug的影响并继续学习另一个问题

要使用GDB调试程序,生成程序的时候,需要带上-g选项,如gcc -g test.c -o test,执行的时候,使用gdb启动,gdb test

GDB可以有三种调试方式

  • 直接启动调试程序gdb a.out
  • 调试挂掉的程序,这里必须加上core文件dgb q.out core
  • 调试正在运行的程序,这里必须知道进程号gdb -p 1234

GDB命令不多,大概也就10来个

  • break
  • run
  • bt
  • print
  • next
  • edit
  • list
  • step
  • help
  • quit