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.

1
gcc -o main -g main.c 

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)
AbbreviationCommandExplanation
bbreakbreak [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]
ccontinueconntinue [N]
sstepstep [N]
nnextnext [N]
pprintprint [[OPTION]… –] [/FMT] [EXP]
iinfo, infuse help i to check out info

Reference

  1. Debugging with GDB