You are on page 1of 12

Operating System Lab (Week 2)

GNU Debugger

Brijendra Pratap Singh

Department of Computer Science & Engineering


Motilal Nahru National Institute of Technology Allahabad
Prayagraj - 211004, India

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

Those programs might be executing on the same machine as GDB


(native), on another machine (remote), or on a simulator.

Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 3 / 12
GDB supports the following languages:

Ada, Assembly, C, C++, D, Fortran, Go, Objective-C, OpenCL,


Modula-2, Pascal, Rust

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

Some most frequently used gdb command


b - Puts a breakpoint at the current line
b N - Puts a breakpoint at line N
b +N - Puts a breakpoint N lines down from the current line
b fn - Puts a breakpoint at the beginning of function ”fn”
d N - Deletes breakpoint number N
info break - list breakpoints
r - Runs the program until a breakpoint or error
c - Continues running the program until the next breakpoint or error
q - Quits gdb

Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 8 / 12
Example

let’s assume we have the following source file: main.cpp

#include <iostream>
using namespace std;

int divint(int, int);


int main()
{
int x = 5, y = 2;
cout << divint(x, y);

x =3; y = 0;
cout << divint(x, y);

return 0;
}

int divint(int a, int b)


{
return a / b;
}

Compile this program as: g++ -g main.cpp -o divide


$ ./divide
Floating point exception (core dumped)

Brijendra Pratap Singh (MNNIT Allahabad) Operating System Lab (Week 2) August 2020 9 / 12
GDB Example

You will find a core file in your current directory.


if it is not their. Type the following
$ ulimit -c unlimited
$ ./divide

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

$ gdb divide core


(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 12 / 12

You might also like