C language content

Debugging tools like gdb (GNU Debugger) are essential for debugging C programs. gdb allows you to examine what is happening inside a program while it runs or what it was doing at the moment it crashed. Here is a detailed guide on how to use gdb for debugging C programs:

Setting Up

1. Compile with Debugging Information:

To use gdb, you need to compile your C program with the -g flag which includes debugging information in the executable.

				
					gcc -g -o myprogram myprogram.c

				
			

Starting gdb

2. Starting gdb:

Run ‘gdb‘ with your executable:

				
					gdb myprogram

				
			

Basic Commands

3. Basic gdb Commands:

  • run (r): Starts your program.
  • break (b): Sets a breakpoint at a function or line number.
  • next (n): Executes the next line of code, stepping over function calls.
  • step (s): Executes the next line of code, stepping into function calls.
  • continue (c): Continues running your program until the next breakpoint.
  • print (p): Prints the value of a variable.
  • backtrace (bt): Displays the call stack.

Example Debugging Session

Let's walk through an example debugging session.

Code Example
				
					#include <stdio.h>

void function1(int);
void function2(int);

int main() {
    int x = 5;
    function1(x);
    return 0;
}

void function1(int a) {
    printf("Function1: %d\n", a);
    function2(a + 1);
}

void function2(int b) {
    printf("Function2: %d\n", b);
}

				
			

Debugging Steps

1. Compile the Code with Debugging Information:

				
					gcc -g -o myprogram myprogram.c

				
			

2. Start gdb:

				
					gdb myprogram

				
			

3. Set a Breakpoint at main:

				
					(gdb) break main
Breakpoint 1 at 0x40052a: file myprogram.c, line 6.

				
			

4. Run the Program:

				
					(gdb) run
Starting program: /path/to/myprogram
Breakpoint 1, main () at myprogram.c:6
6        int x = 5;

				
			

5. Step Through the Code:

				
					(gdb) next
7        function1(x);
(gdb) next
Function1: 5
8        return 0;

				
			

6. Set a Breakpoint in function2:

				
					(gdb) break function2
Breakpoint 2 at 0x400557: file myprogram.c, line 15.

				
			

7. Continue to the Next Breakpoint:

				
					(gdb) continue
Continuing.
Breakpoint 2, function2 (b=6) at myprogram.c:15
15        printf("Function2: %d\n", b);

				
			

8. Print Variable b:

				
					(gdb) print b
$1 = 6

				
			

9. Continue Running:

				
					(gdb) continue
Continuing.
Function2: 6
[Inferior 1 (process 10345) exited normally]

				
			

Analyzing a Crash

When a program crashes, gdb can help determine what caused the crash.

1. Run the Program:

				
					(gdb) run

				
			

2. If the Program Crashes:

				
					Program received signal SIGSEGV, Segmentation fault.
0x00000000004004b6 in function2 (b=6) at myprogram.c:15
15        printf("Function2: %d\n", b);

				
			

3. Inspect the Call Stack:

				
					(gdb) backtrace
#0  0x00000000004004b6 in function2 (b=6) at myprogram.c:15
#1  0x000000000040049a in function1 (a=5) at myprogram.c:10
#2  0x0000000000400478 in main () at myprogram.c:7

				
			

4. Inspect Variables:

				
					(gdb) print b
$1 = 6

				
			

Useful gdb Tips

List Source Code:

				
					(gdb) list

				
			

Inspect Memory:

				
					(gdb) x/10x &variable

				
			

Conditional Breakpoints:

				
					(gdb) break function2 if b > 5

				
			

Watchpoints: (stop when a variable changes)

				
					(gdb) watch variable

				
			

Quit gdb:

				
					(gdb) quit

				
			

Using these commands and techniques, you can effectively debug your C programs using gdb.

Scroll to Top