Debugging is a fundamental skill that any self-identified programmer should have. It allows us to view, review, and fix errors in our code. One powerful debugging tool is the GNU Debugger, GDB for short.

This guide will look at working with GDB to step into or over a function in our code.

Basic GDB Usage

To illustrate this, you can use your code or use the sample provided below. In the example below, the loop me function contains a loop that we will examine with GDB.

#include

void loopMe()

{


    for(int i=0;i<5;i ){


        printf(“i is %dn, i);


    }

}

int main() {


    int a = 10;


    a = 10;


    printf(“Value of a is %d”, a);


    loopMe();


    return 0;

}

Compile the code with -g as:

Next, launch the program with GDB as:

Once in GDB, we can run the program by using the run or r command. You can stop the program while it’s running by using the CTRL C key.

Let us set a breakpoint at the main function to halt the execution at that point. In the example above, we halt at line 10.

Breakpoint 1 at 0x555555555171: file loop.c, line 10.

To step through your program line by line, you can use the next or n command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/step-into-over-function-gdb-01.png" data-lazy- height="320" src="data:image/svg xml,” width=”974″>

Once you get to the function you want to work on, in the example above, the loopMe() function, you can step over it using the next command.

This will skip function and go directly return 0 as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/step-into-over-function-gdb-02.png" data-lazy- height="249" src="data:image/svg xml,” width=”656″>

You can also step into the function and work on it using the step or s command. For example, to enter the loopMe() function, we can do:

The command will step into the function as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/step-into-over-function-gdb-03.png" data-lazy- height="124" src="data:image/svg xml,” width=”918″>

Now that we are inside the loopMe() function, we can go through it line by line using the next command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/step-into-over-function-gdb-04.png" data-lazy- height="318" src="data:image/svg xml,” width=”974″>

As you can see, we run through the loop and see how the loop executes.

Conclusion

In this quick tutorial, we discussed the process of using GDB to step over or into a function when debugging.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/john-150×150.png60f2fc897f5ad.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list