Tutorial 4 (Oct 19): gdb examine + review
GDB commands
A useful reference for commands: http://csapp.cs.cmu.edu/2e/docs/gdbnotes-x86-64.pdf
Examining memory
You already know from A1 and A2 that display/i $pc shows the next instruction to be executed, everytime you hit a breakpoint or you step through the instructions using ni or si. This is actually one case of examining memory. Using display, we actually perform an x (examine) command every time GDB stops. In this case, we are examining the next instruction (display/i). The $pc part is the address from which to start examining.
Besides examining instructions, we can also examine arbitrary memory locations, or look for stack variables. To do this, we combine the x (examine) command with a format and an address. (For more details about the format for examine, look at the reference mentioned above.
x/d $fp+16 | Examine the (4-byte) word at the address which is 16 bytes from the frame pointer. | 32-bit integers can be stored in 4 bytes, as each byte is 8 bits. |
x/g $fp+16 | Examine the (8-byte) giant word at the address which is 16 bytes from the frame pointer. | 64-bit integers can be stored in 8 bytes, as each byte is 8 bits. |
x/s 0xbfff08ff | Examine the string at address 0xbfff08ff | The s format means string. You can also use formats you previously used with the p (print) command, such as /t, /d, /x |
x/20i main | Examine 20 instructions of the main() function | You can use labels as pointers to specific address locations. |
x/xg $fp+16 |
Examine the (8-byte) giant word at the address which is 16 bytes from the frame pointer, |
Display
You might also find display/register useful, if you find yourself printing the same register many times while debugging. While display/i $pc will print the instruction everytime GDB stops, display/$register (eg. display/$x19) will print the register everytime GDB stops. To make it stop displaying, look for the number of the display and do undisplay #. For example, if you have five displays setup and you want to delete the 2nd one, just type undisplay 2.
Breakpoint at a line
Someone asked in class about setting a breakpoint on a particular line number of the code. From what I could find, this feature depends on the version of GDB you are using. In versions that support it, you could simply do b linenumber. However, it seems this is not supported on the school's ARM server.
In-Class Exercises
We will be using this file [gdbx.s](1882 bytes) to demonstrate gdb during class.
To download from terminal, run this command:wget "www.edwinckc.com/uploads/355/gdbx.s"