General debugging of gdb under Linux


Linux system: ubuntu-20.04

Introduction to gdb

The full name of gdb is "GNU symbolic debugger". It is easy to see from the name that it was born in the GNU project (also born at the same time as GCC, Emacs, etc.) and is a commonly used program debugger under Linux. Since its development, GDB has iterated many versions. The current GDB supports debugging programs written in multiple programming languages, including C, C++, Go, Objective-C, OpenCL, Ada, etc. In actual scenarios, GDB is more commonly used to debug C and C++ programs. Generally speaking, gdb mainly helps us complete the following four functions:

  • Start your program and run it as you like according to your customized requirements.
  • Pause a program at a specified place or condition.
  • When the program is stopped, you can examine what is happening in your program at that time.
  • Modify variables or conditions in the program during program execution to correct the impact of one bug to test other bugs.

  • 1. gdb installation
sudo apt-get install gdb

Insert image description here

  • 2. Write routines
#include"stdio.h"

void func()
{
    
    
	int i=0;
	for(i=0;i<5;i++)
	{
    
    
		printf("%d\n",i);
	}
}

int main()
{
    
    
	func();
	return 0;
}

Insert image description here
Compilation runs normally.

  • 3. Perform gdb debugging
    . Next, debug. You must gcc test1.cadd -g after compilation to indicate that you need to perform gdb debugging on this file, and then gdb a.outenter the gdb debugging mode, as follows:

①Enter " r" to indicate run to run the program
Insert image description here

②Enter " l" to indicate list to view the source program.
Due to the character limit, if you can't see it completely at one time, please try it several times .
Insert image description here

③ Enter " b 14" to indicate the break point on line 14
④ Enter " s" to indicate step to enter the breakpoint
⑤ Enter " n" to indicate next to enter the next statement
⑥ Enter " p i" to indicate printf to print the value of variable i at this time
Insert image description here

⑥You can use shell to call terminal commands in gdb, for example, enter " shell cat test1.c" to view the file content
Insert image description here

⑦Enter quitmeans exiting gdb debugging mode
Insert image description here

⑧Input set logging onmeans turning on the log file function, and a gdb.txt file will be generated
⑨Input watch imeans observation point, observe the i variable
⑩Input info watchpointsmeans wipe the observation point situation
Insert image description here

Insert image description here

  • Debugging the core file
    If the core file is not generated, you need to check your ulimit limit, use " ulimit -a", and use " " to remove the limit ulimit -c unlimited.
    Insert image description here
    After lifting the restriction, compile and generate the core file and check for segfault:gdb 二进制文件 core文件

  • Debugging an executing file (such as an infinite loop)
    ./a.out & means running it in the background. After execution, a process number
    gdb -p pid will be returned.
    Insert image description here

Summary of commonly used gdb commands

Insert image description here

Guess you like

Origin blog.csdn.net/Dustinthewine/article/details/133186425