GDB is open source GNU debugger which is used to debug and examine faulty codes. GDB has broader application. I will try to pull most of it from beginner to some extent of advance level.
GDB has bunch of commands and their arguments. Lets starts with some useful commands
sample.c
1 #include <stdio.h>
2 int main(int argc, char *argv[])
3 {
4 int i = 0;
5 for( i = 0;i< 2;i++)
6 {
7 printf("Hello World\n");
8 }
9 }
10
This is sample code taken for reference.
compilation of code
$gcc -g -o run sample.c
option -g is used to add all symbols.
Running Code in GDB
$gdb <.exe>
This command is used to start an executable file
madcoder@linux:~/Desktop/Laboratory/gdb$ gdb run
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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 run...done.
(gdb) <this is the area where most of the gdb commands get executed>
Once this appears then its time to execute gdb commands.
1. run command
(gdb) run or r
This command is used to run code or program. Once this command is invoked code will run till output unless any checkpoints inserted in between.
ex:
(gdb) r
Starting program: /home/madcoder/Desktop/Laboratory/gdb/run
Hello World
Hello World
[Inferior 1 (process 3750) exited with code 014]
(gdb)
This is the expected output of above program.
2. Start command
(gdb) start
This command starts executing the code and pauses at beginning of main() function. Start command creates a temporally break point at the starting of the main function.
ex:
(gdb) start
Temporary breakpoint 1 at 0x40053c: file sample.c, line 4.
Starting program: /home/madcoder/Desktop/Laboratory/gdb/run
Temporary breakpoint 1, main (argc=1, argv=0x7fffffffe008) at sample.c:4
4 int i = 0;
(gdb)
pause at line number 4. starting of program, hereafter other gdb commands can be used to get into code for debugging prospective.
3. List command
This command
(gdb) list
ex:
(gdb) list
1 #include <stdio.h>
2 int main(int argc, char *argv[])
3 {
4 int i = 0;
5 for( i = 0;i<2;i++)
6 {
7 printf("Hello World\n");
8 }
9 }
10
(gdb)
# Printing values in GDB
(gdb) print or p
Using this command one can print value of a variable or value of an address at any break point or watch point or aborted point.
(gdb) n
20 var1 = my_fun(i, var1);
(gdb)
18 for(i = 0; i <10; i++)
(gdb)
20 var1 = my_fun(i, var1);
(gdb)
18 for(i = 0; i <10; i++)
(gdb)
20 var1 = my_fun(i, var1);
(gdb)
18 for(i = 0; i <10; i++)
(gdb)
20 var1 = my_fun(i, var1);
(gdb) print i
$2 = 5
After stepping into 5 time into loop the incremented value of variable i can be obtained. Stepping into code using GDB, will see in later command practice.
Printing values using print command is very useful when it comes to print a linked list of a structure. More practice with print command will help more in debugging specially in case of logical errors.
#Setting Break point and Watch point
(gdb) break or b <line number or filename.c:line number>
Breakpoint setting will give more exposer to bug fixing. here the point where all interviewers grill the candidates. But here i will discuss few points where programmers should not use breakpoints. Breakpoint is not applicable for library functions(Static and Dynamic). If some code is running on device or real time hardware and the there is no possibility of altering the sequence of running, there using breaking points may lead to major harm. In such cases programmer can create another instance of the terminal and there they can attach that process using GDB. Ex.
Terminal -1 Code is running unstoppable, open a new terminal and run gdb alon and try to attach current running process with PID.
(gdb) attach <pid>
and here you can check for the parameter and examine the behaviour of code.
# Passing command line arguments in GDB
M-1
$gdb --args arg1 arg2 ... ./a.out
M-2
$gdb ./a.out
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
.........
.........
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) run arg1 arg2 arg3
GDB has bunch of commands and their arguments. Lets starts with some useful commands
sample.c
1 #include <stdio.h>
2 int main(int argc, char *argv[])
3 {
4 int i = 0;
5 for( i = 0;i< 2;i++)
6 {
7 printf("Hello World\n");
8 }
9 }
10
This is sample code taken for reference.
compilation of code
$gcc -g -o run sample.c
option -g is used to add all symbols.
Running Code in GDB
$gdb <.exe>
This command is used to start an executable file
madcoder@linux:~/Desktop/Laboratory/gdb$ gdb run
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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 run...done.
(gdb) <this is the area where most of the gdb commands get executed>
Once this appears then its time to execute gdb commands.
1. run command
(gdb) run or r
This command is used to run code or program. Once this command is invoked code will run till output unless any checkpoints inserted in between.
ex:
(gdb) r
Starting program: /home/madcoder/Desktop/Laboratory/gdb/run
Hello World
Hello World
[Inferior 1 (process 3750) exited with code 014]
(gdb)
This is the expected output of above program.
2. Start command
(gdb) start
This command starts executing the code and pauses at beginning of main() function. Start command creates a temporally break point at the starting of the main function.
ex:
(gdb) start
Temporary breakpoint 1 at 0x40053c: file sample.c, line 4.
Starting program: /home/madcoder/Desktop/Laboratory/gdb/run
Temporary breakpoint 1, main (argc=1, argv=0x7fffffffe008) at sample.c:4
4 int i = 0;
(gdb)
pause at line number 4. starting of program, hereafter other gdb commands can be used to get into code for debugging prospective.
3. List command
This command
(gdb) list
ex:
(gdb) list
1 #include <stdio.h>
2 int main(int argc, char *argv[])
3 {
4 int i = 0;
5 for( i = 0;i<2;i++)
6 {
7 printf("Hello World\n");
8 }
9 }
10
(gdb)
# Printing values in GDB
(gdb) print or p
Using this command one can print value of a variable or value of an address at any break point or watch point or aborted point.
(gdb) n
20 var1 = my_fun(i, var1);
(gdb)
18 for(i = 0; i <10; i++)
(gdb)
20 var1 = my_fun(i, var1);
(gdb)
18 for(i = 0; i <10; i++)
(gdb)
20 var1 = my_fun(i, var1);
(gdb)
18 for(i = 0; i <10; i++)
(gdb)
20 var1 = my_fun(i, var1);
(gdb) print i
$2 = 5
After stepping into 5 time into loop the incremented value of variable i can be obtained. Stepping into code using GDB, will see in later command practice.
Printing values using print command is very useful when it comes to print a linked list of a structure. More practice with print command will help more in debugging specially in case of logical errors.
#Setting Break point and Watch point
(gdb) break or b <line number or filename.c:line number>
Breakpoint setting will give more exposer to bug fixing. here the point where all interviewers grill the candidates. But here i will discuss few points where programmers should not use breakpoints. Breakpoint is not applicable for library functions(Static and Dynamic). If some code is running on device or real time hardware and the there is no possibility of altering the sequence of running, there using breaking points may lead to major harm. In such cases programmer can create another instance of the terminal and there they can attach that process using GDB. Ex.
Terminal -1 Code is running unstoppable, open a new terminal and run gdb alon and try to attach current running process with PID.
(gdb) attach <pid>
and here you can check for the parameter and examine the behaviour of code.
# Passing command line arguments in GDB
M-1
$gdb --args arg1 arg2 ... ./a.out
M-2
$gdb ./a.out
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
.........
.........
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) run arg1 arg2 arg3