Debugging C Program using 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.
Eg:
$cc -g test.c //test.c is the sample program to debug
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
Syntax:
break [file_name]:line_number
Eg:
break test.c:3 // this will set a break point at line number3
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.
Step 4. Execute the C program in gdb debugger
You can start running the program using the run command in the gdb debugger.
Eg: run [args] // args are optional
Step 5. Printing the variable values inside gdb debugger
Syntax: print {variable}
Example:
print i
Step 6. Continue, stepping over and in – gdb commands
There are three kind of gdb operations you can choose when the program stops at a break point. They are continuing until the next break point, stepping in, or stepping over the next program lines.
c or continue: Debugger will continue executing until the next break point.
n or next: Debugger will execute the next line as single instruction.
s or step: Same as next, but does not treats function as a single
instruction, instead goes into the function and executes it line by line.
gdb command shortcuts
Use following shortcuts for most of the frequent gdb operations.
l – list
p – print
c – continue
s – step
ENTER: pressing enter key would execute the previously executed command again.
Miscellaneous gdb commands
l command: Use gdb command l or list to print the source code in the debug mode. Use l line-number to view a specific line number (or) l function to view a specific function.
bt: backtrack – Print backtrace of all stack frames, or innermost COUNT frames.
help – View help for a particular gdb topic — help TOPICNAME.
quit – Exit from the gdb debugger.
Compile your C program with -g option. This allows the compiler to collect the debugging information.
Eg:
$cc -g test.c //test.c is the sample program to debug
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
Syntax:
break [file_name]:line_number
Eg:
break test.c:3 // this will set a break point at line number3
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.
Step 4. Execute the C program in gdb debugger
You can start running the program using the run command in the gdb debugger.
Eg: run [args] // args are optional
Step 5. Printing the variable values inside gdb debugger
Syntax: print {variable}
Example:
print i
Step 6. Continue, stepping over and in – gdb commands
There are three kind of gdb operations you can choose when the program stops at a break point. They are continuing until the next break point, stepping in, or stepping over the next program lines.
c or continue: Debugger will continue executing until the next break point.
n or next: Debugger will execute the next line as single instruction.
s or step: Same as next, but does not treats function as a single
instruction, instead goes into the function and executes it line by line.
gdb command shortcuts
Use following shortcuts for most of the frequent gdb operations.
l – list
p – print
c – continue
s – step
ENTER: pressing enter key would execute the previously executed command again.
Miscellaneous gdb commands
l command: Use gdb command l or list to print the source code in the debug mode. Use l line-number to view a specific line number (or) l function to view a specific function.
bt: backtrack – Print backtrace of all stack frames, or innermost COUNT frames.
help – View help for a particular gdb topic — help TOPICNAME.
quit – Exit from the gdb debugger.
Watch the video for demo https://www.youtube.com/watch?v=sCtY--xRUyI
Comments
Post a Comment