In this article, we will discuss how to use the gdb debugger to debug C Programs in six simple steps.
Compile a C program example with errors for debugging purposes.
To learn how to debug C Programming, let's create the following C program, which calculates and prints a factorial of numbers. However, the C program contains some errors for our debugging purpose.
$ Vim factorial. c
# Include <stdio. h>
Int main ()
{
Inti, num, j;
Printf ("Enter the number :");
Scanf ("% d", & num );
For (I = 1; I <num; I ++)
J = j * I;
Printf ("Thefactorial of % d is % d \ n", num, j );
}
$ Cc factorial. c
$./A. out
Enter the number: 3
The factorial of 3 is 12548672
Let's start debugging it and learn many of the most useful commands in gdb.
Step 1: Use the compile option-g to compile the C program
Use the-g option to compile your C program, which allows the compiler to collect debugging information.
$ Cc-g factorial. c
Note: The above command creates a. out file for debugging shown below.
Step 2: Run gdb
Run the C debugger (gdb) as follows:
$ Gdb a. out
Step 3: set breakpoints in the C program
Syntax: break line_number
Other formats:
Break [file_name]: line_number
Break [file_name]: func_name
In the C program, place the breakpoint where you assume the error is located. When you execute a program, the debugger stops at the breakpoint and gives you a prompt for debugging.
Therefore, before running the program, let's set the following breakpoint in the program.
(Gdb) break 10
Breakpoint 1 at 0x40050b: file factorial. c, line 10.
Step 4: Execute the C program in the gdb debugger
Run [args]
You can also use the run command in the dbg debugger to start the program. You can also use run args to give the command line parameters to the program. The example program we use here does not require any command line parameters. Let's enter run to run the program.
(Gdb) run
Starting program:/root/valgrind/a. out
When you execute the C program, it will execute to the first breakpoint and give you a prompt for debugging.
Breakpoint 1, main () at factorial. c: 10
10 j = j * I;
You can also use the gdb commands described in the following sections to debug the C program.
Step 5: print the variable values in the gdb debugger
Syntax: print {variable}
Examples:
Print I
Print j
Print num
(Gdb) p I
$1 = 1
(Gdb) p j
$2 = 3042592
(Gdb) p num
$3 = 3
(Gdb)
As shown above, in factorial. c, we didn't initialize the variable j, so it gave a junk value as a factorial value.
Solve this problem by initializing j to 1, compile the C program and execute it again.
Even after the fix, it seems that there are some problems in the factorial. c program, and it still gives the wrong factorial value.
Therefore, place a breakpoint on line 1 and continue with the description in the next section.
Step 6: Continue. Execute the-gdb command in one step.
When the program stops at the breakpoint, there are three gdb operations you can choose from. They continue to be executed to the next breakpoint, step by step to enter the function, or step by step to the next program line.
# C or continue: the debugger continues until the next breakpoint.
# N or next: the debugger executes the next line as a single command;
# S or step: similar to next, but does not use a function as a single instruction, instead of running a row in the function.
By continuing or performing one step, you can find the problem because we didn't use the <= condition check in the for loop. Therefore, changing <to <= will solve this problem.
Gdb command shortcut
Use the following most common gdb shortcut:
# L-list
# P-print
# C-continue
# S-step
# N-next
# ENTER: ENTER the ENTER key to run the previous command again.
Omnipotent Gb commands
# L command: Use the gdb command l or list to print the source code in debug mode, and use <l line number> to view the specified line number or <l function> to view the specified function.
# Bt: backtrack-prints information of all stack frames, or up to COUNT frames internally.
# Help-view the help of a special gdb topic-help topic life
# Quit-exit the gdb debugger. Www.2cto.com