GDB 操作手册

Getting Started
If you have never run GDB before, you can start it up with the following command:
\gdb < program >
If you have been compiling to a.out then you would start GDB up with \ gdb ./a.out
Make sure to add the -g flag when compiling your program! This would look something like \g++ -g my_program.cpp
And here’s some conventions:

  • [arg] - This is an optional argument
  • < arg > - This is a required argument
  • <foo | bar> - This is a required argument that can be either foo or bar

Some Tips
There’s a few tricks I have learned that make GDB easier to use. First of all, you don’t actually need to type out the full command name to run a command. You can type the first few characters of a command, or potentially even a single character. This means instead of typing layout you can type lay, and instead of typing break you can type b

Sometimes you want to run the same command a bunch of times in a row. Typing next a bunch of times is a pain. However, if you enter an empty command and just hit Enter then GDB will repeat the last command. Instead of typing next a bunch of times, you can type it a single time and then repeatedly hit the Enter key.

The Commands

  • run [args] - This command will start your program. You can optionally specify arguments, these will get passed as command-line arguments to your program.
  • layout src - This command displays the source code of your program above the main GDB command window.
  • backtrace - This command will show all function calls that were made to reach the current location. This is particularly useful if your code breaks inside of a library - you can see which of your functions called the library’s function.
  • frame < frame number > - Each function that backtrace shows you has its own stack frame, with a frame number shown in the leftmost column. You can enter one of these frames with the frame command, bringing GDB back to that function. This shows you the relevant code and lets you look at variables inside of that function.
  • print < variable > - This command prints out the contents of a variable. Any valid C/C++ wil work here, you can access arrays (a[10]), access fields of structs or classes (person.name), and even dereference pointers (*ptr).
  • display < variable > - This command works similarly to print. However, display will print out your variable whenever you move to a new line of code.
  • break <function | function:label | file:line | line> - This command sets a breakpoint at the desired location. GDB will stop execution once the breakpoint is hit, letting you inspect problematic areas of code. The breakpoint locatiion can be given in a couple of forms. You can use function names as breakpoint locations, or you can give a filename and line number in the form file.cpp:42. If you find yourself using file:line on the same location repeatedly, consider adding a label there, and using function:label to give it a stable location and convenient name.
  • continue - This will continue the execution of your program after a breakpoint is hit.
  • del [breakpoint number] - This will either delete all breakpoints if given no arguments, or delete a single breakpoint specified by the breakpoint number. When creating a new breakpoint GDB will show you its number
  • step [count] - This will make GDB move to the next line of code, moving into any functions encountered. A count can be specified to step multiple times.
  • next [count] - This command works almost the same as step. However, next will step over functions. This can be useful when you want to avoid stepping through library functions.
  • help [count] - If no command is given this will show you information about all GDB commands. Otherwise, if a command is given, help will show more information about that specific command.

Debugging Assembly

  • layout <reg | asm> - There are layouts designed specifically for debugging assembly code and for reverse engineering programs. Similar to src, asm will show the relevant assembly code in a pane above the main GDB window. reg shows both the assembly code and the values of all registers, each in their own pane.
  • x[/FMT] <memory location | variabel | $register> - x is used to examine the value of a variable or memory location. x, like print, is used to print out information about your program. However, x will work with arbitrary memory locations and allows you to examine the contents of registers. The FMT string lets you change how the value is displayed, you can display values as hex, decimal, and in many different sizes. help x has more information on FMT.
  • info registers - This command prints out the values of all registers.

猜你喜欢

转载自blog.csdn.net/BSCHN123/article/details/105214022
今日推荐