GDB for beginners.
1. Introduction.
2. What is GDB?
3. First steps.
4. Final words.
5. Quick reference.
6. Links.
Introduction.
What is GDB?
First steps.
Lets start with the simple Hello World example:
Breakpoints
Lets see it in action:
Step by step
Here is a more in-depth example:
and then again continue with stepi.
Disassembling
Get more info
Final words.
Quick reference
--basics---
--breakpoints--
--disassembling--
--stepping--
--registers--
--misc--
Links.
[1] Green Book - the best for ppc-assembly.
[2] Article on DevPit - GDB relates, and was originally written from a 32bit PowerPC architecture perspective and register information will vary across architectures.
[3] Original GDB docs
You also can get article in the .txt format here.
7/0.RAM Disk:> gdb -v GNU gdb 6.3 (AmigaOS build 20050719) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "ppc-amigaos". 7/0.RAM Disk:>
7/0.RAM Disk:> type hello.c
#include <stdio.h> main() { printf("just hello"); return 0; }
7/0.RAM Disk:> gcc -gstabs hello.c -o hello 7/0.RAM Disk:> gdb -q hello
(gdb) run Starting program: /RAM Disk/hello just hello Program terminated with signal SIGQUIT, Quit. The program no longer exists. (gdb) quit
7/0.RAM Disk:>
break _start : break at beginning of the _start() function break 10 : break at line 10 of the current file break *0xXXXXXXX : break at the address
7/0.RAM Disk:> gdb -q hello
(gdb) break main Breakpoint 1 at 0x7f974208: file hello.c, line 3. (gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x7f974208 in main at hello.c:3 (gdb) run Starting program: /RAM Disk/hello BS 653c1a68 Current action: 2 Breakpoint 1, main () at hello.c:3 3 { (gdb)
next (n) - Execute current statement and move to the next one in the function. step (s) - The same as next, but with difference that if you are at a function call and you hit next, then the function will execute and return. But if you hit step, then you will step into the first line of the called function. stepi (si) - Stepping a single assembly instruction at a time. This is only for experienced developers with knowledge of assembly language. continue (c) - If you are finished with manual stepping, then you can just type "c" and the program will continue execution until the next breakpoint is reached, or the program reaches the end.
7/0.RAM Disk:> gdb -q hello
(gdb) list 1 #include <stdio.h> 2 main() 3 { 4 printf("just hello"); 5 return 0; 6 } (gdb) break main Breakpoint 1 at 0x7f974208: file hello.c, line 3. (gdb) r Starting program: /RAM Disk/hello BS 63afafd8 Current action: 2 Breakpoint 1, main () at hello.c:3 3 { (gdb) step main () at hello.c:4 4 printf("just hello"); (gdb) step 0x7f97424c in printf () (gdb) step 0x7f974254 in __NewlibCall () (gdb) step just hello Program terminated with signal SIGQUIT, Quit. The program no longer exists. (gdb)
(gdb) break _start (gdb) r (gdb) disas ..... (gdb) break *0xaddress_after_bctrl (gdb) c
7/0.RAM Disk:> gdb -q hello
(gdb) break main Breakpoint 1 at 0x7f974208: file hello.c, line 3. (gdb) r Starting program: /RAM Disk/hello BS 6624f370 Current action: 2 Breakpoint 1, main () at hello.c:3 3 { (gdb) disas Dump of assembler code for function main: 0x7f974208 <main+0>: stwu r1,-16(r1) 0x7f97420c <main+4>: mflr r0 0x7f974210 <main+8>: stw r31,12(r1) 0x7f974214 <main+12>: stw r0,20(r1) 0x7f974218 <main+16>: mr r31,r1 0x7f97421c <main+20>: lis r9,25978 0x7f974220 <main+24>: addi r3,r9,-24520 0x7f974224 <main+28>: crclr 4*cr1+eq 0x7f974228 <main+32>: bl 0x7f97424c <printf> 0x7f97422c <main+36>: li r0,0 0x7f974230 <main+40>: mr r3,r0 0x7f974234 <main+44>: lwz r11,0(r1) 0x7f974238 <main+48>: lwz r0,4(r11) 0x7f97423c <main+52>: mtlr r0 0x7f974240 <main+56>: lwz r31,-4(r11) 0x7f974244 <main+60>: mr r1,r11 0x7f974248 <main+64>: blr End of assembler dump. (gdb)
-- info reg - show all the registers at current moment -- info reg X - show only register X
7/0.RAM Disk:> gdb -q hello
(gdb) break main Breakpoint 1 at 0x7f974208: file hello.c, line 3. (gdb) r Starting program: /RAM Disk/hello BS 657d7430 Current action: 2 Breakpoint 1, main () at hello.c:3 3 { (gdb) disas Dump of assembler code for function main: 0x7f974208 <main+0>: stwu r1,-16(r1) 0x7f97420c <main+4>: mflr r0 0x7f974210 <main+8>: stw r31,12(r1) 0x7f974214 <main+12>: stw r0,20(r1) 0x7f974218 <main+16>: mr r31,r1 0x7f97421c <main+20>: lis r9,25978 0x7f974220 <main+24>: addi r3,r9,-24520 0x7f974224 <main+28>: crclr 4*cr1+eq 0x7f974228 <main+32>: bl 0x7f97424c <printf> 0x7f97422c <main+36>: li r0,0 0x7f974230 <main+40>: mr r3,r0 0x7f974234 <main+44>: lwz r11,0(r1) 0x7f974238 <main+48>: lwz r0,4(r11) 0x7f97423c <main+52>: mtlr r0 0x7f974240 <main+56>: lwz r31,-4(r11) 0x7f974244 <main+60>: mr r1,r11 0x7f974248 <main+64>: blr End of assembler dump. (gdb) break *0x7f974224 Breakpoint 2 at 0x7f974224: file hello.c, line 4. (gdb) c Continuing. Current action: 0 BS 63b03568 Current action: 2 Breakpoint 2, 0x7f974224 in main () at hello.c:4 4 printf("just hello"); (gdb) info reg r3 r3 0x6579a038 1702469688 (gdb) x/1s 0x6579a038 0x6579a038 <_SDA_BASE_+28756968>: "just hello" (gdb)
amiga shell:> gdb filename - start gdb with loaded binary to it (gdb) file filename - load file when you are already in GDB (gdb) run - run :) (gdb) run arg1 arg2 etc - run program with command line args (gdb) quit - quit :) (gdb) help command - get help for a certain command
(gdb) break foo() - set break at function (gdb) break 5 - set break at line 5 of source code (gdb) break *0xXXXXXXXX - set break at address (gdb) info break - display breakpoints (gdb) delete X - delete breakpoint X (where X is a breakpoint number from "info break") (gdb) enable X - enable breakpoint X (gdb) disable X - disable breakpoint X
(gdb) disas - disassemble current function (gdb) disas 0xXXXXXXX - disassemble by address (unlike setting a breakpoint by address, you don't need a '*') (gdb) x/[num]i 0xXXXXXXX - disassemble by "examine" any amount of instructions ([num]) at any address in memory
(gdb) step - step into the function on this line (if possible) (gdb) stepi - step by assembly instructions (gdb) next - run to the next line of this function (gdb) continue - continue to next breakpoint (or till end) for all the stepping functions you can specify how many steps to do, e.g. stepi 100, or next 20.
(gdb) info registers - show integer registers only (the most usable) (gdb) info all-registers - show all registers (including floating point ones, special ones etc, not used so often) (gdb) info reg X - show only register X
(gdb) list - examine the source of the program (gdb) bt - alias of "backtrace": show the current stack (gdb) RETURN - if you hit "enter" while you in gdb, it will repeat the last command
Comments
Submitted by walkero on
Submitted by jaokim on
Submitted by YesCop on