C-gdb-step-by-step-runtime-debug
Step 1 : Create a file test.c with programming mistakes |
|
---|---|
#include <stdio.h>
#include <stdlib.h>
void fun_2()
{
int a[5] = {1,2,3,4,5};
int *p = a;
a[5] = 400;
a[6] = 400;
*p = 100;
}
void fun_1()
{
int a[5] = {1,2,3,4,5};
int *p;
p += 3;
*p = 100;
p += 3;
*p = 200;
}
int main()
{
char *p;
// Allocation #1 of 30 bytes
p = (char *) malloc(30);
free(p);
// Allocation #2 of 12 bytes
p = (char *) malloc(8);
free(p);
// Allocation #3 of 16 bytes
p = (char *) malloc(25);
free(p);
fun_1();
fun_2();
return 0;
}
|
Step 2 : Compile program and create executable |
|
---|---|
$ gcc -g test.c -o test
$ ./test
Segmentation fault (core dumped)
|
Step 3 : Segmentation fault is seen in above test. Let us run gdb |
|
---|---|
$ gdb test
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
(gdb) r
Starting program: /home/test/Desktop/training/valgrind/test
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555245 in fun_1 () at test.c:21
21 *p = 100;
(gdb) q
A debugging session is active.
Inferior 1 [process 112408] will be killed.
Quit anyway? (y or n) y
|
Step 4 : SIGSEGV is seen at 0x0000555555555245 in fun_1( ) at test.c Line number 21 |
|
---|---|
Step 5 : Add break points to do step by step debugging using gdb |
|
---|---|
$ gdb test
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...
(gdb) b main
Breakpoint 1 at 0x1271: file test.c, line 29.
(gdb) b fun_1
Breakpoint 2 at 0x11fe: file test.c, line 15.
(gdb) list
15 {
16 int a[5] = {1,2,3,4,5};
17 int *p;
18
19 p += 3;
20
21 *p = 100;
22
23 p += 3;
24
(gdb) list main
24
25 *p = 200;
26 }
27
28 int main()
29 {
30 char *p;
31
32 // Allocation #1 of 30 bytes
33 p = (char *) malloc(30);
(gdb) b 30
Breakpoint 3 at 0x127d: file test.c, line 33.
(gdb)
Note: breakpoint 3 also set at pc 0x127d.
Breakpoint 4 at 0x127d: file test.c, line 33.
(gdb) r
Starting program: /home/test/Desktop/training/valgrind/test
Breakpoint 1, main () at test.c:29
29 {
(gdb) s
Breakpoint 3, main () at test.c:33
33 p = (char *) malloc(30);
(gdb) s
Breakpoint 2, fun_1 () at test.c:15
15 {
(gdb) s
16 int a[5] = {1,2,3,4,5};
(gdb) s
19 p += 3;
(gdb) s
21 *p = 100;
(gdb) s
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555245 in fun_1 () at test.c:21
21 *p = 100;
(gdb) s
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb) q
|
GDB Cheat sheet |
|||
---|---|---|---|
Sl.No |
Command |
Description |
Example |
1 |
|
|
(gdb) r
Starting program: /home/test/gdb
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555245 in fun_1 () at prg1.c:21
21 *p = 100;
(gdb)
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/test/gdb
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555245 in fun_1 () at prg1.c:21
21 *p = 100;
(gdb)
|
2 |
|
|
(gdb) help
List of classes of commands:
aliases -- Aliases of other commands.
breakpoints -- Making program stop at certain points.
data -- Examining data.
files -- Specifying and examining files.
internals -- Maintenance commands.
obscure -- Obscure features.
running -- Running the program.
stack -- Examining the stack.
status -- Status inquiries.
support -- Support facilities.
tracepoints -- Tracing of program execution without stopping the program.
user-defined -- User-defined commands.
Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
|
3 |
|
|
Reading symbols from test...
(gdb)
(gdb) q
test@test:~/gdb$
|
4 |
|
|
(gdb) list
15 {
16 int a[5] = {1,2,3,4,5};
17 int *p;
18
19 p += 3;
20
21 *p = 100;
22
23 p += 3;
24
(gdb)
|
5 |
|
|
(gdb) set listsize unlimited
(gdb) list
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void fun_2()
5 {
6 int a[5] = {1,2,3,4,5};
7 int *p = a;
8
9 a[5] = 400;
10 a[6] = 400;
11 *p = 100;
12 }
13
14 void fun_1()
15 {
16 int a[5] = {1,2,3,4,5};
17 int *p;
18
19 p += 3;
20
21 *p = 100;
22
23 p += 3;
24
25 *p = 200;
26 }
27
28 int main()
29 {
30 char *p;
31
32 // Allocation #1 of 30 bytes
33 p = (char *) malloc(30);
34 free(p);
|
6 |
|
|
(gdb) b main
Note: breakpoint 5 also set at pc 0x555555555271.
Breakpoint 6 at 0x555555555271: file prg1.c, line 29.
(gdb) list
24
25 *p = 200;
26 }
27
28 int main()
29 {
30 char *p;
31
32 // Allocation #1 of 30 bytes
33 p = (char *) malloc(30);
(gdb) b 29
Note: breakpoints 5 and 6 also set at pc 0x555555555271.
Breakpoint 7 at 0x555555555271: file prg1.c, line 29.
(gdb) b 31
Breakpoint 8 at 0x55555555527d: file prg1.c, line 33.
(gdb) b 32
Note: breakpoint 8 also set at pc 0x55555555527d.
Breakpoint 9 at 0x55555555527d: file prg1.c, line 33.
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/test/gdb
Breakpoint 5, main () at prg1.c:29
29 {
(gdb) c
Continuing.
Breakpoint 8, main () at prg1.c:33
33 p = (char *) malloc(30);
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555245 in fun_1 () at prg1.c:21
21 *p = 100;
(gdb)
Breakpoint 7 at 0x555555555271: file prg1.c, line 29.
(gdb) b 31
Breakpoint 8 at 0x55555555527d: file prg1.c, line 33.
(gdb) b 32
Note: breakpoint 8 also set at pc 0x55555555527d.
Breakpoint 9 at 0x55555555527d: file prg1.c, line 33.
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/test/gdb
Breakpoint 5, main () at prg1.c:29
29 {
(gdb) c
Continuing.
Breakpoint 8, main () at prg1.c:33
33 p = (char *) malloc(30);
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555245 in fun_1 () at prg1.c:21
21 *p = 100;
(gdb)
|
7 |
|
|
(gdb) n
33 p = (char *) malloc(30);
(gdb) n
34 free(p);
(gdb) n
37 p = (char *) malloc(8);
(gdb) n 3
42 free(p);
(gdb)
|
8 |
|
|
(gdb) b 43
Breakpoint 1 at 0x12cb: file prg1.c, line 44.
(gdb) r
Starting program: /home/test/gdb
Breakpoint 1, main () at prg1.c:44
44 fun_1();
(gdb) s
fun_1 () at prg1.c:15
15 {
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/test/gdb
Breakpoint 1, main () at prg1.c:44
44 fun_1();
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555245 in fun_1 () at prg1.c:21
21 *p = 100;
(gdb)
|
9 |
|
|
(gdb) b main
Breakpoint 1 at 0x1271: file prg1.c, line 29.
(gdb) until 43
The program is not being run.
(gdb) r
Starting program: /home/test/gdb
Breakpoint 1, main () at prg1.c:29
29 {
(gdb) until 43
main () at prg1.c:44
44 fun_1();
(gdb)
|
10 |
|
|
(gdb) print p
$3 = (int *) 0x5555555552fc <__libc_csu_init+12>
(gdb) print &p
$4 = (int **) 0x7fffffffddf8
(gdb) print *p
$5 = 1230389504
(gdb) p/d p
$6 = 93824992236284
(gdb) p/u p
$7 = 93824992236284
(gdb) p/t p
$8 = 10101010101010101010101010101010101001011111100
(gdb) p/c p
$9 = 252 '\374'
(gdb)
|
11 |
|
|
(gdb) info args
No arguments.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000555555555271 in main
at prg1.c:29
breakpoint already hit 1 time
(gdb) w p
Cannot use this setting with the "with" command
(gdb) watch p
Hardware watchpoint 2: p
(gdb) info w
Ambiguous info command "w": warranty, watchpoints, win.
(gdb) info watch
Num Type Disp Enb Address What
2 hw watchpoint keep y p
(gdb) info frame
Stack level 0, frame at 0x7fffffffde50:
rip = 0x5555555552cb in main (prg1.c:44); saved rip = 0x7ffff7de50b3
source language c.
Arglist at 0x7fffffffde28, args:
Locals at 0x7fffffffde28, Previous frame's sp is 0x7fffffffde50
Saved registers:
rbp at 0x7fffffffde40, rip at 0x7fffffffde48
(gdb) info local
p = 0x5555555592a0 ""
(gdb)
|
12 |
|
|
(gdb) b main
Breakpoint 1 at 0x1271: file prg1.c, line 29.
(gdb) b 10
Breakpoint 2 at 0x11d6: file prg1.c, line 10.
(gdb)
|
13 |
|
|
(gdb) watch p
Hardware watchpoint 2: p
(gdb)
|
14 |
|
|
(gdb) clear
Deleted breakpoints 1 3 No breakpoint at this line.
(gdb)
|
15 |
|
|
(gdb) delete
Delete all breakpoints? (y or n) y
(gdb)
|
16 |
|
|
(gdb) info b
No breakpoints or watchpoints.
(gdb) b main
Breakpoint 5 at 0x555555555271: file prg1.c, line 29.
(gdb) disable 5
(gdb) info b
Num Type Disp Enb Address What
5 breakpoint keep n 0x0000555555555271 in main at prg1.c:29
(gdb) enable 5
(gdb) info b
Num Type Disp Enb Address What
5 breakpoint keep y 0x0000555555555271 in main at prg1.c:29
(gdb)
|
17 |
|
|
(gdb) b main
Breakpoint 1 at 0x1271: file prg1.c, line 29.
(gdb) r
Starting program: /home/test/gdb
Breakpoint 1, main () at prg1.c:29
(gdb) kill
Kill the program being debugged? (y or n) y
[Inferior 1 (process 59946) killed]
(gdb)
|
18 |
|
|
|
19 |
|
|
|
20 |
|
|
(gdb) bt
#0 fun_1 () at prg1.c:15
#1 0x00005555555552d5 in main () at prg1.c:44
(gdb)
|
21 |
|
|
(gdb) up
#1 0x00005555555552d5 in main () at prg1.c:44
44 fun_1();
(gdb) down
#0 fun_1 () at prg1.c:16
16 int a[5] = {1,2,3,4,5};
(gdb) up
#1 0x00005555555552d5 in main () at prg1.c:44
44 fun_1();
(gdb) down
#0 fun_1 () at prg1.c:16
16 int a[5] = {1,2,3,4,5};
(gdb)
|
22 |
|
|
(gdb) finish
Run till exit from #0 fun_1 () at prg1.c:15
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555245 in fun_1 () at prg1.c:21
21 *p = 100;
(gdb)
|
23 |
|
|
(gdb) s
fun_1 () at prg1.c:15
15 {
(gdb) return
Make fun_1 return now? (y or n) y
#0 main () at prg1.c:45
45 fun_2();
(gdb)
|