Linux Debugger-Usage of gdb

Table of Contents

background

memories gcc/g++

use

Add -g option to gcc/g++

gdb+debug executable program

list/l line number: display the binFile source code, and then go down from the last position, 10 lines at a time

list/l function name: List the source code of a function.

r or run: run the program. (Equivalent to F5 for vs debugging)

break(b) line number: set a breakpoint on a certain line

info break(b): View breakpoint information.

d + breakpoint number: delete breakpoint

delete breakpoints breakpoint number:

disable breakpoint breakpoint number: disable breakpoint

enable breakpoint breakpoint number: enable breakpoint

n or next: single execution. (Equivalent to F10 step-by-step during vs debugging)

s or step: Enter function call (equivalent to F11 during vs debugging, statement by statement)

bt: View function call chain

p variable: print variable value.

display variable name: (similar to the monitoring window of vs debugging) track and view a variable, and display its value every time you stop, (long display, built-in type structure type and other custom types)

undisplay: Cancel tracking of previously set variables (cancel long display)

until X line number: jump to X line (generally used within functions)

finish: Execute until the current function returns, then wait for the command

continue (or c): Continuously execute the program from the current position instead of single-stepping, and adjust directly from the breakpoint to another breakpoint

set var: modify the value of a variable


Background

There are two ways to release a program, debug mode and release mode.

Binary programs produced by Linux gcc/g++ are in release mode by default.

To use gdb debugging, you must add the -g option when generating a binary program from source code.

Memory gcc/g + +

-E only activates preprocessing. This does not generate a file. You need to redirect it to an output file.
-S compiles to assembly language without assembly and linking
-c compile to object code
-o file output to file
-static This option applies static linking to the generated files
-g Generate debugging information. The GNU debugger can exploit this information.
-shared This option will try to use dynamic libraries, so the generated files are smaller, but the system requires dynamic libraries.
-O0
-O1
-O2
-O3 4 levels of compiler optimization options, -O0 means no optimization, -O1 is the default value, -O3 has the highest optimization level
-w does not generate any warning messages.
-Wall generates all warning messages

Use

Add the -g option to gcc/g++

gdb + debug executable program

list/l Line number: Display binFile source code, then go to the last position Below, 10 rows at a time
list/l function name: List the source code of a certain function.

r or run: run the program. (Equivalent to F5 in vs debugging)

break(b) line number: set a breakpoint on a certain line

info break(b): View breakpoint information.

D + breakpoint number: delete point
delete breakpoints breakpoint number:

disable breakpoint breakpoint number: disable breakpoint
enable breakpoint breakpoint number: enable breakpoint

Note: breakpionts is effective for all breakpoints. If you do not add s, you must write the breakpoint number.

n or next: Single execution. (Equivalent to F10 step-by-step during vs debugging)

s or step: Entering a function call (equivalent to F11 during vs debugging, statement by statement)

bt: View function call chain

p variable: print variable value.

Display variable name: (similar to the monitoring window of vs debugging) track and view a variable, and display its value every time you stop, (long display, built-in type structure type and other custom types)

undisplay: Cancel tracking of those variables previously set (cancel long display)

Note that this is not followed by the variable name

But with the number

until X line number: jump to line X (usually used within a function)

For example, I am running in a for loop above, and I want to run directly to the next place, such as this printf

Run directly to line 10 and stop here

finish: execute until the current function returns, then stand down and wait for the command

continue(or c): start continuously from the current position instead of Single-step the program and adjust directly from breakpoint to another breakpoint

set var: Modify the value of the variable

All instructions for viewing content do not affect debugging instructions (you can view the contents of the function during debugging)