This article keep tracks of the most used commands of GDB
Write a classic C program with your favoriate editor and save it as main.c
.
1
2
3
4
5
| #include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello world\n");
}
|
Compile it with gcc
, which is a popular c/c++ compiler on linux.
Debug with gdb
1
| gdb main // gdb programname -p [pid]
|
1
2
3
4
| (gdb) b main // set breakpoint
(gdb) layout src // gui
(gdb) p // print p/x hex
(gdb) i b // list all breakpoints, you can list other info
|
1
2
3
4
5
6
7
8
9
10
11
12
13
| ┌─test.c───────────────────────────────────────────────────┐
│ 3 int main(int argc, char *argv[]) { │
│B+>4 printf("Hello world\n"); │
│ 5 } │
└──────────────────────────────────────────────────────────┘
process 31551 In: main L4 PC: 0x555555555144
(gdb) r
Starting program: /home/junxxx/Workspace/the_c_language/test
Breakpoint 1, main (argc=1, argv=0x7fffffffe338)
at test.c:4
(gdb)
|
Abbreviation | Command | Explanation |
---|
b | break | break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION] |
c | continue | conntinue [N] |
s | step | step [N] |
n | next | next [N] |
p | print | print [[OPTION]… –] [/FMT] [EXP] |
i | info, inf | use help i to check out info |
Reference
- Debugging with GDB