This shows you the differences between two versions of the page.
isc:labs:05:extra:gdb-tutorial [2024/11/03 11:54] florin.stancu |
isc:labs:05:extra:gdb-tutorial [2024/11/03 11:58] (current) florin.stancu |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== GDB tutorial ===== | + | ====== GDB tutorial ====== |
- | ==== Loading a program ==== | + | ===== Loading a program ===== |
In order to start debugging using GDB, you need to specify the program to be inspected. There are two options for doing this: | In order to start debugging using GDB, you need to specify the program to be inspected. There are two options for doing this: | ||
Line 65: | Line 65: | ||
</note> | </note> | ||
- | ==== Breakpoints ==== | + | ===== Breakpoints ===== |
Breakpoints represent places in your program where the execution should be stopped. | Breakpoints represent places in your program where the execution should be stopped. | ||
Line 125: | Line 125: | ||
The ''start'' command is very similar to ''run'', but instead of running the program until it ends (or until it crashes), it sets a breakpoint at the beginning of the main function. | The ''start'' command is very similar to ''run'', but instead of running the program until it ends (or until it crashes), it sets a breakpoint at the beginning of the main function. | ||
- | ==== Step ==== | + | ===== Stepping ===== |
There might be situations when you only want to execute one line of source code, or one machine instruction from your program. | There might be situations when you only want to execute one line of source code, or one machine instruction from your program. | ||
Line 134: | Line 134: | ||
* ''next'' or ''n'' (step over) - Continue to the next source line in the current stack frame. This is similar to step, but function calls that appear within the line of code are executed without stopping. | * ''next'' or ''n'' (step over) - Continue to the next source line in the current stack frame. This is similar to step, but function calls that appear within the line of code are executed without stopping. | ||
- | There are also equivalent functions for the machine instructions: ''stepi'' and ''nexti''. | + | There are also equivalent functions for the machine instructions: ''stepi'' and ''nexti''. Those are useful when the binary uses various compiler optimizations and the source lines debug metadata are unreliable (i.e., they skip too much). |
If you stepped into a function and you want to continue the execution until the function returns, you can use the ''finish'' or ''f'' (step out) command. | If you stepped into a function and you want to continue the execution until the function returns, you can use the ''finish'' or ''f'' (step out) command. | ||
- | ==== Printing variables and memory ==== | + | ===== Printing variables and memory ===== |
No need to manually print registers anymore, but you still might need to print the content of a variable: | No need to manually print registers anymore, but you still might need to print the content of a variable: | ||
Line 208: | Line 208: | ||
</code> | </code> | ||
- | ==== Stack info ==== | + | ===== Stack info ===== |
A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack. | A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack. |