1. Introduction.
2. What is GDB?
3. First steps.
4. Final words.
5. Quick reference.
6. Links.
Introduction.
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:>
(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:>
Breakpoints
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
Lets see it in action:
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:33{(gdb)
Step by step
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.
Here is a more in-depth example:
7/0.RAM Disk:> gdb -q hello
(gdb) list
1#include <stdio.h>2 main()3{4printf("just hello");5return0;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:33{(gdb) step
main () at hello.c:44printf("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
and then again continue with stepi.
Disassembling
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:33{(gdb) disas
Dump of assembler code forfunction 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,259780x7f974220<main+24>: addi r3,r9,-245200x7f974224<main+28>: crclr 4*cr1+eq
0x7f974228<main+32>: bl 0x7f97424c<printf>0x7f97422c<main+36>: li r0,00x7f974230<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)
Get more info
-- 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:33{(gdb) disas
Dump of assembler code forfunction 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,259780x7f974220<main+24>: addi r3,r9,-245200x7f974224<main+28>: crclr 4*cr1+eq
0x7f974228<main+32>: bl 0x7f97424c<printf>0x7f97422c<main+36>: li r0,00x7f974230<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:44printf("just hello");(gdb) info reg r3
r3 0x6579a0381702469688(gdb) x/1s 0x6579a0380x6579a038<_SDA_BASE_+28756968>:"just hello"(gdb)
Final words.
Quick reference
--basics---
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
--breakpoints--
(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
--disassembling--
(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
--stepping--
(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.
--registers--
(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
--misc--
(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
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
Great article. I was looking for something like that. The debugging on programming is the most useful thing to know. Thanks again for that blog post.
Submitted by jaokim on
Great intiative and great article!
Thank you!
Submitted by YesCop on
I agree with my "colleagues". Great initiative and article.
For me nothing is really new but I will appreciate some more focus on how to read not memories but C++ variables like arrays or struct.
Of course if this is possible in GDB.
Comments
Submitted by walkero on
Great article. I was looking for something like that. The debugging on programming is the most useful thing to know. Thanks again for that blog post.
Submitted by jaokim on
Great intiative and great article!
Thank you!
Submitted by YesCop on
I agree with my "colleagues". Great initiative and article.
For me nothing is really new but I will appreciate some more focus on how to read not memories but C++ variables like arrays or struct.
Of course if this is possible in GDB.