CS 491/591 Introduction to Computer Security - Fall 2014

TA Contact

David Pouliot
Email: david at salempfg.com
Office Hours: Monday 12:00 - 1:30 pm, Wednesday 10:30 - 12:00
Class page: Intro to Computer Security

Most of the homework assignments require the use of GDB.    I put together a list of some of the GDB commands you might find useful if you don't have any experience with GDB. 

If possible, compile your programs with the -g flag.   For example:
gcc homework2.c -g

I like to start gdb with the -tui flag as well, this gives a nice split screen.  
gdb -tui ./a.out

You can then split the layout again with:

(gdb) layout split

This will allow 3 split layout screens, one for GDB commands, and 2 others that can be used to watch the source code, the assembly code or the registers, whatever you prefer.     If you use the layout split command, you may also need to use the focus command:
(gdb) focus cmd
Focus set to CMD window.   
One last thing about layout, when using the split layouts, often the screen will get messed up.    The refresh command will fix this. 

The rest of the layout commands:

layout src Standard layout�source on top, command window on the bottom
layout asm Just like the "src" layout, except it's an assembly window on top
layout split Three windows: source on top, assembly in the middle, and command at the bottom
layout reg Opens the register window on top of either source or assembly, whichever was opened last
tui reg general Show the general registers
tui reg float Show the floating point registers
tui reg system Show the "system" registers
tui reg next Show the next page of registers�this is important because there might be pages of registers that aren't in the "general", "float", or "system" sets

Like most debuggers, you can set breakpoints.    You can do this with the break command, or just the shortcut b:
(gdb) b main
Breakpoint 1 at 0x8048395: file hello.c, line 5.

To start the debugger, simply use the run command (r for short).

(gdb) r 
Starting program: /u/dpouliot/a.out

To step through the program, use the next (n) and step (s) commands.   Next steps over function calls, step steps into function calls.    Another useful step instruction is stepi which is for step instruction.     This can be useful for stepping through injectoed code where the normal step commands don't work like they should.

The disp command will put the value of a variable to the screen:
(gdb) disp i
1: i = 10
You can also use print to do this:
(gdb) print i
$1 = 10
Another useful gdb command is the x command.   It is used to display memory contents. 
(gdb)x/16 0x4006c0
0x4006c0 <main+48>:     0x4800400b      0xf4e8c789      0x48000001      0xbef0458b
0x4006d0 <main+64>:     0x00400b7f      0xe8c78948      0x000001e3      0xf0458b48
0x4006e0 <main+80>:     0x400b85be      0xc7894800      0x0001d2e8      0x458b4800
0x4006f0 <main+96>:     0x0b8cbef8      0x89480040      0x01c1e8c7      0x8b480000
The info command (i) is also useful.    If you didn't have the registers in one of the gdb frames from using the layout commands, you could get the values in the registers by using this:
(gdb) i r
rax            0x400690 4195984
rbx            0x0      0
rcx            0x0      0
rdx            0x7fffffffeb88   140737488350088
rsi            0x7fffffffeb78   140737488350072
rdi            0x1      1
rbp            0x7fffffffea90   0x7fffffffea90
rsp            0x7fffffffea70   0x7fffffffea70
r8             0x7ffff7dd4e80   140737351863936
r9             0x7ffff7dea560   140737351951712
r10            0x7fffffffe920   140737488349472
r11            0x7ffff7a35dd0   140737348066768
r12            0x4004d0 4195536
r13            0x7fffffffeb70   140737488350064
r14            0x0      0
r15            0x0      0
rip            0x4006b0 0x4006b0 <main+32>
Here is a useful link for gdb - debugging with pipe

There is more that you can do with GDB, but these commands should give you enough tools to complete the homework assignments.