GDB debugging entry notes

Official Website Manual: User Manual

Motivation: Learning this is to find the location of the Segment Default error.
According to the version of linux or mac, some may not be gdb but lldb, but these two are similar and can be compared.

1. Basic operation

  1. When installing, enter in the terminal sudo apt-get install gdbIf you need a specific version, go to the official website to download the source code. Clion comes with gdb, which is separate from this additional installation.

  2. Before debugging with gdb: Use Debug mode in CMakeLists set(CMAKE_BUILD_TYPE Debug), or add -g to the compilation option, set(CMAKE_CXX_FLAGS "-g")and then compile to generate an executable file.

  3. Enter gdb mode for an executable file: · Load the file
    directly with · Enter the debug mode first, and then load the file with the commandgdb filename
    gdbfile filename

  4. To run gdb, enter run/r. Execute the current statement next/n, continue to run the program until the endpoint is encountered continue/c, and exit the usequit

  5. After entering the gdb mode, the commands previously used in the terminal will not work. For example, if you want to view the files in the current folder, you will report an lsundefined command if you want to use it. At this time, you can enter it shell ls. Add a shell before the usual terminal commands. alright.

  6. If you want to query related commands, you can search in the user manual, or enter to man gdbview the introduction, if you want to see how to use a specific command, you can enter help b, where b is a command name, and then the specific usage of break will be displayed.

2. File display

If you use ssh for remote use, you can’t use the IDE to directly view the code, you can use listthe function to display the code and the number of lines corresponding to the code; set listsize 20that is, you can display 20 lines of code at a time.

list命令可以指定行号,函数:
(gdb) l 24
(gdb) l main
还可以指定向前或向后打印:
(gdb) l -
(gdb) l +
还可以指定范围:
(gdb) l 1,10

insert image description hereIf you want to view the memory address corresponding to a certain line of code, you can use:
info line 19
insert image description here

3. Common commands

A. Parameter setting

After loading the binary file to be debugged , if you need to give some parameters to run the program, such as configuration files, dataset folders, etc., you need to use it in gdb:
set args config.yaml ~/dataset/dirName
If you want to view parameters:
show argsorprint args

B. Breakpoint setting

Breakpoints set in Clion can be seen in gdb, but not vice versa.
Commonly used breakpoints have three forms: line number, function name, and condition.
break/b lineNum
break/b functionNameSet a breakpoint at the function entry
break/b test.cpp:23 if b==0. Write the file name when multiple files are used. Note that the variable must be within the life cycle.
If you want to view the breakpoint information:
info breakpoint

If you need to enter and exit the function: step/s
Note that if the project contains the source code of the function, you can enter it, but if it only contains the library without the source code, you cannot enter.
Exit from inside the function:finish

C. View & set variables

Directly display the value of the variable: print 变量名
Setting the variable here shows some advantages. After modification in the IDE, it needs to be recompiled. Here, you can continue to debug after directly modifying it:set var 变量名=新值

4. Debugging multi-threaded programs

Since the multi-threaded function library #include <pthread> is used in the program, the corresponding instruction should be added when compiling the terminalgcc -g demo.cpp -o demo -lpthread

In the terminal:
view the command of the thread: ps -aL | grep demo
view the relationship between the main thread and the sub-thread (tree diagram):pstree -p 主线程id

In gdb:
view the current thread: info threads
enter a certain thread: thread 线程号
if only the current thread is run, other threads are suspended: set scheduler-locking on
the default is to run all threads, or you can manually set it: set scheduler-locking off
specify a thread to execute the command: thread apply 线程编号 gdb命令
let all threads execute same command:thread apply all gdb命令

Guess you like

Origin blog.csdn.net/catpico/article/details/121167158