LinuxDebugger-gdb

[Linux] Debugger-gdb

  • Default mode for compiling code under Linux
  • Use of gdb
    • list option, referred to as l
    • run option, referred to as r
    • break point
    • View breakpoints
    • Delete breakpoint
    • Enabling and disabling breakpoints
    • Run step by step
    • Run statement by statement
    • View variables
    • Jump
    • finish
    • continue
    • call stack
    • Modify the value of a variable
    • info locals

Default mode for compiling code under Linux

Under Linux, we use gcc/g++ to compile code. The default is release mode. Release mode cannot be debugged.
If you want your code to be released in debug form, you need to add the -g option after gcc/g++.
So if you do not add the -g option after gcc/g++ and use gdb to debug the program, the following situation will occur


It will tell you that the program has no debugging information and you have no way to debug it.
The executable program we compiled and generated by adding the -g option can be debugged using gdb.

In addition to this method that can prove that gcc/g++ compiled code under Linux, the default is release mode, I can also look at the size of the executable program compiled with the -g option and the executable program compiled without the -g option. Releasing code in debug form is more expensive than releasing code in release form, because the executable program released in debug form needs to add some debugging information.

The executable program format formed in Linux is the elf format. So we still use the readelf command to obtain the binary format of the executable program, so we can check whether the executable program has debug information.

So if you want your program to be debugged, you must add the -g option to gcc/g++ and release it in debug form.

Usage of gdb

If you want to use the gdb debugger, just add the name of the executable program you want to debug after gdb. The premise is that the executable program you want to debug is in the current directory.

gdb executable program name


If you want to exit gdb, enter quit to exit.

list option, referred to as l

This option allows me to see the code we are debugging and display the corresponding line number.
But it doesn’t display it for us from the beginning, so we can add a number after lsit or l to print from the line number we specify.


Gdb will remember the last option we entered, so we only need to press Enter to view the following code.

run option, referred to as r

This option is to run the program to start debugging and stop when it encounters a breakpoint.

Breakpoint

If we want to break the point, we can use the break option, referred to as b
We can complete it by adding the number of lines we want to break after break or b. For example, if we want to break the line 18, we can directly break/b 18.

There is another problem here, which is how to break the point if debugging multiple source files. It is very simple, just break/b + source file name + : + line number.

If you want to break a certain function, break/b + function name

If you want to break the function across files break/b + source file name +: + function name

View breakpoints

If we want to see what breakpoints there are, enter the info break/b option to view it.

Delete breakpoint

delete/d + the serial number of the breakpoint. The serial number of the breakpoint is the Num on the left of info b.
For example, if we want to delete breakpoint No. 1, just delete/d 1 will delete the breakpoint.

It should be noted here that the serial number of the breakpoint is increasing. For example, if we delete breakpoint No. 4 and add it back, breakpoint No. 4 will not be regenerated but breakpoint No. 5 will be generated.

If we use quit to exit gdb and then enter gdb, all the breakpoints we hit before will be cleared.

Enabling and disabling breakpoints

disable: disable breakpoints
enable: Enable breakpoints


For example, let’s put a breakpoint on line 18 of this code.


When we check the breakpoint, the Enb list indicates whether the breakpoint is enabled (y means enabled and n means disabled).
We can see that breakpoint No. 1 is enabled when we run the following, and the program will stop on line 18.

No matter how many times we use the run option it always stops at line 18.

This is to use disable to disable the breakpoint. The program will not stop at line 18. When using info b to view the breakpoint, the Enb list of breakpoint No. 1 will change from y to n.

This breakpoint has not been deleted, it has just been disabled. If you use enable to enable the breakpoint, it will stay on line 18 when you use run again.

Run process by process

next/n

It can only be used after run is executed, so we need a breakpoint. For example, if we set a breakpoint on line 17 and then run it, then we can use the next/n command.

This instruction will not enter the called function, but will skip the function directly.

Run statement by statement

step/s
The difference between it and next is that it will enter the called function.

View variables

p
p + variable name, you can view the value of the variable

p + & variable name, you can view the address of the variable

But when we are debugging, we will find that the value of the variable cannot be displayed frequently. If you want the variable to be displayed frequently during debugging, use the display command.
display + variable name

If you do not want to display undisplay + serial number

Jump

until + specifies the number of lines, will jump to the specified line

finish

If you enter a function and want to stop it after executing the function, you can use the finish instruction.

continue

abbreviationc
If you want to run directly from one breakpoint to the next breakpoint, you can use the continue command.

Call stack

bt, you can view the call stack and function calling status

Modify the value of a variable

set var variable name = modified value

info locals

Display the value of the local variable of the current function