NOTE
1.2 GCC
Install GCC on Windows/MSYS2 and record common GDB debugging commands.
This is a historical learning note and may contain outdated or incomplete understanding.
1. Installation
1.1. Install MSYS2
1.2. Install GCC
In the current MSYS2 setup, if you use the UCRT64 environment, you can install the corresponding toolchain:
pacman -Syu
pacman -S --needed mingw-w64-ucrt-x86_64-toolchain mingw-w64-ucrt-x86_64-gdb
The pacman -S base-devel gcc gdb command in the original note is more oriented toward the MSYS environment itself. When compiling native Windows programs, use the toolchain packages corresponding to the selected environment.
2. GDB
2.1. Running
run/r: run the program and stop when a breakpoint is reached.continue/c: continue execution until the next breakpoint or until the program exits.next/n: execute one step; when a function call is encountered, normally do not enter the function body.step/s: execute one step; when a debuggable function call is encountered, enter the function body.until: continue running to a specified source-code position after the current position; commonly used to quickly get out of a loop. Exact behavior depends on the current position and arguments.until LINE: run to the specified source-code position.finish: run until the current function returns.call function(args): call a visible function in the debugging context, for examplecall gdb_test(55).quit/q: exit GDB.
2.2. Set Breakpoints
break n/b n: set a breakpoint at line n.break file.cpp:578: set a breakpoint at the specified file and line.break fn1 if a > b: set a conditional breakpoint.break func: set a breakpoint at the function entry.delete n: delete breakpoint number n.disable n: disable breakpoint number n.enable n: enable breakpoint number n.clear n: clear the breakpoint at the specified source-code line.info breakpoints/info b: display breakpoints.delete breakpoints: delete all breakpoints.
2.3. View Source Code
list/l: list source code.list LINE: show source code around the specified line.list FUNCTION: show source code around the specified function.- Run
listagain without arguments: continue displaying subsequent source code.
2.4. Print Expressions
print EXPR/p EXPR: evaluate and print an expression.print a: print the variablea.print ++a: execute the expression and print the result; note that this modifies the state of the program being debugged.print name: print the variable.print gdb_test(22): call the function and print the result.display EXPR: automatically display the expression each time execution pauses.watch EXPR: set a watchpoint; pause when the value of the expression changes. Whether hardware watchpoints are available depends on the platform and resources.whatis: query the type of a variable or function.info functions: view functions.info locals: display local variables in the current stack frame.
2.5. Query Runtime Information
where/bt/backtrace: display the current call stack.up/down: switch the current stack frame.set args ARGS: set program runtime arguments.show args: view runtime arguments.info program: view program execution state, process information, and the reason it stopped.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub