[Linux] The use of gdb

Table of contents

1. Pre-work using gdb

2. How to debug with gdb

1. How to see my code

2. How to break the point 

3. How to run the program

4. How to debug step by step

5. How to debug statement by statement

6. How to monitor variable values 

7. How to jump to the specified position

8. Finish running a function

9. How to jump to the next breakpoint

10. How to disable/enable breakpoints

3. Summary

Write at the end:


1. Pre-work using gdb

Let's write some simple programs first:

is an ordinary printing program, and then, 

Use Makefile to automatically build our project:

After compiling with make directly like this:

We can debug by typing gdb test:

But here he reminds us that this executable program has no debugging information,

Enter quit to exit gdb debugging. Why is there no debug information?

Because the release version is released by default in the Linux environment.

We need to add the -g option when compiling to add debugging information to the executable program:

Now that's done, let's go into debugging again with gdb test:

Found it, we succeeded, and now we can start debugging. 

2. How to debug with gdb

Let's write a simple code first, which is used for testing and debugging, just write according to your preferences:

After we add debugging information to run, use gdb.

1. How to see my code

Enter l + number to start from the first few lines, but generally not all will be displayed:

(l + source file name can specify the file)

As long as we press Enter again, he will continue to output to us:

2. How to break the point 

We can use b + line number for breakpoints:

(You can also use b + function name, b + mycode.c:10 to specify the file line number)

So how do we check whether the breakpoint is actually hit?

You can enter info b to view:

As you can see, Num represents the number of the breakpoint.

So how to delete the breakpoint?

Enter d + breakpoint number to delete this breakpoint:

3. How to run the program

When you set a breakpoint, you have to run the program to debug, so how to run it?

Enter r to:

Now to our breakpoint set at line 15,

4. How to debug step by step

Enter n for step-by-step debugging (without entering the function)

It should be noted here that if there is a breakpoint in the function, even if you use statement by statement, gdb will stop there.

So here first delete the breakpoint in the function, and then continue n:

We can see that when a function is encountered, it is run directly as a statement. 

5. How to debug statement by statement

Enter s to debug the comment statement:

We can see that he entered the add function.

6. How to monitor variable values 

Just use p + variable name:

But in this way, you can only take a look at it when you enter it. Can it be displayed all the time? Can,

Enter display + variable name:

In this way, he can appear next to us when we continue to debug,

Then use undisplay to cancel the observation.

7. How to jump to the specified position

At this time we are caught in this cycle, how can we break out?

Enter until + line number to jump to the specified line:

You can see that we broke out of the loop and came to line 9. 

8. Finish running a function

When we no longer want to be in this function,

Enter finish to run the current function directly:

He also told us the return value very intimately. 

9. How to jump to the next breakpoint

Enter c to:

We have successfully reached our breakpoint on line 21 from line 15 

10. How to disable/enable breakpoints

Use disable + breakpoint number to disable the breakpoint:

Use enable + breakpoint number to open the breakpoint

3. Summary

Here I have summed up what I think is commonly used, which can be used for a quick review:

l (list code)

r (run code)

c (continue to run)

finish (end the current function)

p + variable (print the value of the variable)

b + line number (break point)

info (view breakpoint)

d + line number (delete breakpoint)

disable/enable (disable breakpoint/enable breakpoint)

display/undisplay (monitor the value of a variable/undisplay)

until + line number (go to the line)

n (executed process by process)

s (execute statement by statement) (will enter the function)

bt (view call stack)

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131789741