GDB (Step by Step Introduction)

About GDB

GDB represents GNU Project Debugger and is a strong investigating device for C(along with different dialects like C++).It assists you with looking around inside your C projects while they are executing and furthermore permits you to see what precisely happens when your program crashes. GDB works on executable records which are parallel documents created by assemblage process.

The primary example program has some sensible blunders. The program should yield the summation of (X^0)/0! + (X^1)/1! + (X^2)/2! + (X^3)/3! + (X^4)/4! + … + (X^n)/n!, given x and n as information sources. Anyway the program yields a worth of limitlessness, paying little heed to the data sources. We will make you stride by venture through the investigating system and follow the mistakes:
GDB (GNU debugger) can stop the execution of a running project and let you look at/assume responsibility for its memory. Likely quite possibly the most useful asset can assist you with making certain about the specific reason for an issue with your program. For more often than not it works like having printfs in your code however it’s considerably more extensible and productive. In the models beneath, a “$” brief means an order is composed to a working framework shell, and a “#” brief means an order is composed to the GDB order brief.

Prior to running gdb, kindly be certain that the program is arranged with the – g compiler banner. This banner advises the compiler to embed “investigate images” into the executable so you can allude to the factors by similar names as you proclaimed them in C.

Dispatch a Program in GDB

To investigate a program, we should dispatch it with GDB.

$ gdb ./program

Assuming the program requires order line contentions, kindly investigate the Programs with Command Line Arguments segment (in total agreement). Later GDB effectively stacks the program we will be taken to the GDB order brief. We can collaborate with GDB through a bunch of GDB orders. Type an order and press return to execute the order.

Valuable GDB Commands

break: Insert a breakpoint. A breakpoint is an exceptional area in the program, and GDB will stop the execution of the program once this area is reached. A breakpoint is recognized by a document name and a line number (like hello_world.c:10, which means the tenth line in record hello_world.c). Model:

run: This order advises the GDB to begin executing the program from the earliest starting point. The program will continue to execute until it hits a “breakpoint” set with the break order above.

next: Only works when the program is “stopped” as a result of a breakpoint. It executes the following assertion in the program and respite the program once more. This conduct is otherwise called “venture over”, which implies if the following assertion to execute is a capacity call, it will “venture over” the capacity call and will just respite the program again later the capacity call returns.

step: Similar to “next”, however it works in a “progression into” design. On the off chance that the following assertion to execute is a capacity call, step will stop the program at the principal executable explanation INSIDE that capacity.

finish: This order proceeds the presently executing capacity, and interruption the program again just later the capacity returns. It’s especially valuable when you inadvertly ventured into a capacity that you don’t really think often about (like printf).

proceed: Continue executing a stopped program. It works in basically the same manner to run as in the program will just stop once another breakpoint is hit, yet it doesn’t restart the program.

backtrace: When a program is stopped or encounted a blunder, this order can be utilized to assess the latest call stack. It will give you data regarding which code called a specific capacity – it’s especially helpful when you have issues like a division shortcoming.

print: This is a definitive order! Essentially all the other things we do in GDB is to set things up so we can utilize print to print out a variable worth. The print order acknowledges variable names (dependent upon scopes), or even plain memory areas. It can likewise be utilized to execute a capacity and print out the bring esteem back. Model:

// int count = 0;
# print count

This pair of orders assist you with exploring between various capacity outlines in the call stack. For instance, you need to get to a neighborhood variable in the guest’s edge of the current executing capacity, which is out-of-scope, you can’t immediate access it through print by alluding to it utilizing the variable name. What you can do rather is to go up one level in the call stack spending and afterward print the variable. It’s not unexpected utilized related to the backtrace order so you realize what work you are taking a gander at/going to.

data breakpoints: This order provides you with a rundown of breakpoints that are at present set in the program. (There are numerous other helpful information orders; see notes)

cripple/empower: This pair of order is utilized to oversee breakpoints. You can have a great deal of breakpoints in a program and that is fine, simply debilitate the ones you don’t need, and once again empower them when you really want them once more! You ought to allude to breakpoints utilizing the breakpoint number returned by the “data breakpoints” order referenced previously. Model:

Utilizing the TUI

GDB gives a helpful element called the TUI (terminal UI) that permits you to see code as well as gathering close by the troubleshooting console. This keeps you from posting every now and again to observe where you are. To enact the TUI, press either Ctrl + X 2 (control + X, trailed by 2) to see both source and gathering, or Ctrl + X 1 to see simply source. To switch between sees, use Ctrl + X O.

Programs with Command Line Arguments
There are two methods for dispatching a program with order line contentions.

To utilize the – – args contention when dispatching GDB in shell:

$ gdb – – args arg1 arg2 ./program

To append contentions to the “run” order in GDB order brief:

# run arg1 arg2

A considerable lot of these orders have short-hand portrayals (by and large it’s simply the principal letter of the full order, similar to b for break, and so forth)
When you feel OK with GDB, it’s great to know some more “progressed” orders that might assist you with investigating your code later in this course. A large number of those orders work at a lower level, connecting with machine guidelines and plain memory areas. This rundown contains many such orders.

To learn C program debugging, let us create the following C program that calculates and prints the factorial of a number. However this C program contains some errors in it for our debugging purpose.

$ vim factorial.c
# include <stdio.h>

int main()
	int i, num, j;
	printf ("Enter the number: ");
	scanf ("%d", &num );

	for (i=1; i<num; i++)

	printf("The factorial of %d is %d\n",num,j);
$ cc factorial.c

$ ./a.out
Enter the number: 3
The factorial of 3 is 12548672

Let us debug it while reviewing the most useful commands in gdb.

Step 1. Compile the C program with debugging option -g

Compile your C program with -g option. This allows the compiler to collect the debugging information.

$ cc -g factorial.c

Note: The above command creates a.out file which will be used for debugging as shown below.

Step 2. Launch gdb

Launch the C debugger (gdb) as shown below.

$ gdb a.out

Step 3. Set up a break point inside C program


break line_number

Other formats:

  • break [file_name]:line_number
  • break [file_name]:func_name

Places break point in the C program, where you suspect errors. While executing the program, the debugger will stop at the break point, and gives you the prompt to debug.

So before starting up the program, let us place the following break point in our program.

break 10
Breakpoint 1 at 0x804846f: file factorial.c, line 10.

Step 4. Execute the C program in gdb debugger

run [args]

You can start running the program using the run command in the gdb debugger. You can also give command line arguments to the program via run args. The example program we used here does not requires any command line arguments so let us give run, and start the program execution.

Starting program: /home/sathiyamoorthy/Debugging/c/a.out

Once you executed the C program, it would execute until the first break point, and give you the prompt for debugging.

Breakpoint 1, main () at factorial.c:10
10			j=j*i;

You can use various gdb commands to debug the C program as explained in the sections below.

Step 5. Printing the variable values inside gdb debugger

Syntax: print {variable}

print i
print j
print num
(gdb) p i
$1 = 1
(gdb) p j
$2 = 3042592
(gdb) p num
$3 = 3

Also ReadHow to set the SVG background color?

Leave a Reply

Your email address will not be published. Required fields are marked *