Operating System Lab (Week 2) : GNU Debugger
Operating System Lab (Week 2) : GNU Debugger
GNU Debugger
August 2020
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 1 / 12
GNU Debugger
GNU Debugger
allows you to see what is going on ‘inside’ another program while it
executes
allows you to see what another program was doing at the moment it
crashed
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 2 / 12
GNU Debugger
GDB can do four main kinds of things (plus other things in support of
these) to help you catch bugs in the act:
Start your program, specifying anything that might affect its behavior
Make your program stop on specified conditions
Examine what has happened, when your program has stopped
Change things in your program, so you can experiment with
correcting the effects of one bug and go on to learn about another
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 3 / 12
GDB supports the following languages:
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 4 / 12
How GDB Debugs?
GDB allows you to run the program up to a certain point, then stop
and print out the values of certain variables at that point
or step through the program one line at a time and print out the
values of each variable after executing each line
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 5 / 12
GDB
GDB
GDB cannot be used for programs that compile with errors and it
does not help in fixing those errors
Check whether gdb is installed in your machine or not ($gdb -help)
if it is not installed, install it ($ sudo apt-get install libc6-dbg gdb
valgrind)
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 6 / 12
GDB
To let GDB be able to read all that information line by line from the
symbol table, we need to compile it a bit differently
Normally we compile our programs as: gcc hello.cc -o hello
Instead of doing this, we need to compile with the -g flag as: gcc -g
hello.cc -o hello
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 7 / 12
GDB
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 8 / 12
Example
#include <iostream>
using namespace std;
x =3; y = 0;
cout << divint(x, y);
return 0;
}
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 9 / 12
GDB Example
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 10 / 12
GDB Example
$ gdb divide
(gdb) r
(gdb) l
(gdb) where
(gdb) up
(gdb) list
(gdb) p x
(gdb) p y
(gdb) q
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 11 / 12
GDB Example
Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 12 / 12