For this reason,
debuggers like gdb are invaluable to programmers and hackers alike.
gdb Basics
Commonly used commands in gdb are shown in Table 6-6.
To debug our example program, we issue the following commands. The first will
recompile with debugging options:
$gcc ??“ggdb ??“mpreferred-stack-boundary=2 ??“o meet meet.c
$gdb ??“q meet
(gdb) run Mr Haxor
Starting program: /home/aaharper/book/meet Mr Haxor
Hello Mr Haxor
Bye Mr Haxor
Program exited with code 015.
(gdb) b main
Breakpoint 1 at 0x8048393: file meet.c, line 9.
(gdb) run Mr Haxor
Starting program: /home/aaharper/book/meet Mr Haxor
Chapter 6: Programming Survival Skills
137
PART III
Breakpoint 1, main (argc=3, argv=0xbffffbe4) at meet.c:9
9 greeting(argv[1],argv[2]);
(gdb) n
Hello Mr Haxor
10 printf("Bye %s %s\n", argv[1], argv[2]);
(gdb) n
Bye Mr Haxor
11 }
(gdb) p argv[1]
$1 = 0xbffffd06 "Mr"
(gdb) p argv[2]
$2 = 0xbffffd09 "Haxor"
(gdb) p argc
$3 = 3
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x08048393 in main at meet.c:9
breakpoint already hit 1 time
(gdb) info reg
eax 0xd 13
ecx 0x0 0
edx 0xd 13
??¦truncated for brevity??¦
(gdb) quit
A debugging session is active.
Do you still want to close the debugger?(y or n) y
$
Gray Hat Hacking: The Ethical Hacker??™s Handbook
138
Command Description
b function Sets a breakpoint at function
b *mem Sets a breakpoint at absolute memory location
info b Displays information about breakpoints
delete b Removes a breakpoint
umrun
Starts debugging program from within gdb with given arguments
info reg Displays information about the current register state
stepi or si Executes one machine instruction
next or n Executes one function
bt Backtrace command that shows the names of stack frames
up/down Moves up and down the stack frames
print var
print /x $
Prints the value of the variable;
Prints the value of a register
x /NT A Examines memory where N=number of units to display; T=type of data to
display (x:hex, d:dec, c:char, s:string, i:instruction); A=absolute address or
symbolic name such as ???main???
quit Exit gdb
Table 6-6 Common gdb Commands
Disassembly with gdb
To conduct disassembly with gdb, you need the two following commands:
set disassembly-flavor
disassemble
The first command toggles back and forth between Intel (NASM) and AT&T format.
Pages:
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299