21.6 C
Jaipur
Monday, October 18, 2021

How to Step Into or Over a Function in GDB – Linux Hint

Must read

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.

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:

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:

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

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.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article