Debugging C Programs in Unix with GDB: A Beginner’s Guide

A programmer debugging C code in the GDB terminal on their computer, with the screen showing debugging commands and a focused work environment.

The skill of debugging is a very important skill that a programmer must develop. Regardless of whether you are working alone on a project, or you are part of a team, you need to be able to find and resolve any problem in your code quickly, and this is success.

This article shall present GNU Debugger (GDB) which is an ardent tool used in C programming to enable a programmer to step-through his/her program, examine the variables, set breakpoints and to debug logic errors. In case you are working on a Unix-based system, understanding GDB will go a long way in making you efficient and understanding system in your C programs.

What is GDB?

GNU Debugger (GDB) is an application that enables developers to debug programs that are written in the C (and other such languages as C++, Fortran, and Assembly) language. The concept of ensuring that the mistakes or bugs in your code are detected and fixed to ensure that your program works is referred to as debugging. GDB will have a range of commands that enable the programmer to stop the execution of the program, examine the values of the variables and step through the code line by line. This is what makes it a precious tool to any person writing C programs on a Unix environment.

First of all, it is necessary to have GDB installed on your system. Majority of Unix-based systems have GDB installed. Otherwise, it is easy to install it via your package manager.

Installing GDB: An Easy Case

Let’s first go through the setup of GDB and execution of a simple program before getting into the debugging methods.

Write a Simple C Program:

We are going to write a simple C program with a mistake in it. The sample of a program to add two numbers is this:

#include <stdio.h>

int main() {

    int a = 5;

    int b = 10;

    int sum = a + b;

    printf(“a + b = a + %d = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a + b = a

    return 0;

}

Assemble the Program with Debugging Script:

In order to use GDB you must compile your C program with debug information. This may be accomplished by passing the -g flag to GCC:

gcc -g program.c -o program

The -g flag has the advantage that GDB is able to debug the program source code.

Start GDB:

Screenshot of the GDB terminal showing a C program execution with commands like 'run', 'next', and 'print' used for debugging.

GDB can now be started with the debugging information built into the program, by typing:

gdb ./program

Running the Program in GDB:

When you are in the GDB prompt you can type run to start the program.

(gdb) run

Basic GDB Commands

Implementing Your Program, Step-by-Step

After GDB is executing your program, you can use a number of commands to control the execution of your program and aid yourself in debugging it more easily.

  • run: Start your program.
  • next: Move to the following line of code.
  • step: Step in a function to check its behavior.
  • continue: Resume the program to the next breakpoint or termination point.
  • quit: Exit GDB.

For example, to go through the program line-by-line, you can use the next or step command. This will allow you to see the way the variables vary and identify the problems in time.

Inspecting Variables

The ability of GDB to examine variables at any stage of program execution is one of the most powerful capabilities of GDB.

print: This command is used to print the value of a given variable.

(gdb) print sum

  •  This will be a representation of the value of the sum variable at the present state of the program.

display: This is like the print command; however, it uses the default setting of printing the value of the variable after each step.

(gdb) display sum

Setting Breakpoints

GDB terminal showing a breakpoint set at the start of the main function using the 'break main' command.

Breakpoints

Breakpoints are points that you can insert in your code to tell GDB to pause when the program reaches the point. This gives you the ability to investigate the state of the program at certain stages and can thus assist you in determining where the issue is.

break: Place a breakpoint on a given line number or on a given function.

(gdb) break main

This will introduce a break point at the beginning of the main body.
delete: Remove a breakpoint.

(gdb) delete

info breakpoints: List breakpoints.

(gdb) info breakpoints

After you have a breakpoint, you can use the run command to start the program. It will stop at the breakpoint and will enable you to examine the status of the program.

Tracing Logic Errors

GDB terminal showing backtrace output after a segmentation fault, displaying the call stack and error location.

Logic errors may be the cause when your program is not performing as you hoped. GDB enables you to follow the execution of your program and it therefore becomes easier to identify the points of failure.

backtrace: This command will help you to see the call chronology of the functions and go back to the point the program is presently in.

(gdb) backtrace

info locals: Display local variables in this function.

(gdb) info locals

These are commands that enable you to recover the sequence of execution of the program and determine the logical errors that may not be easily discerned.

The Debugging of Advanced C Programs

In C programs that are more complicated, debugging may be more difficult. The following are further debugging hints for larger programs:

With Multiple Files: Use GDB

In case your program is distributed in several files, GDB can still debug the complete program, provided that these files are compiled with the -g flag.

Segmentation Faults:

Segmentation faults (segfaults) are not unusual in C programs, and particularly occur with pointers. In case GDB hits a segfault, it will simply halt the program. backtrace can then be used in following the fault.

(gdb) backtrace

Watchpoints:

To terminate the program when the value of a variable has changed, you can set a watchpoint:

(gdb) watch variable_name

Conclusion

Knowing how to debug programs is one of the best skills that any programmer should have and GDB is a great tool that will enable you to acquire this skill. Having this guide in consideration, you should have no trouble breaking even in debugging your C programs in Unix systems. Be it a simple problem you need to solve, or a complex bug, the step-by-step execution, breakpoints, and inspection of variables will enable you to track and debug issues with accuracy with the help of GDB.

It will take time and practice until you can identify and fix bugs more effectively, until you are able to write a C code that is cleaner and more efficient.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x