Debugger under Linux - gdb usage guide

Preface;
In VSthe environment, we can not only write code, compile, and run executable programs, but also debug the generated executable programs. In this chapter, we will learn how to Linuxdebug in the environment.

1. Preface

To debug, you must first generate an executable program, so first prepare a simple C program:

touch Test.c
vim Test.c
//C代码:
 #include<stdio.h>

int accumulate(int n)
{
    
    
	printf("Start\n");
	int sum = 0;
	for (int i = 0; i <= n; i++)
	{
    
    
		sum += i;
	}
	printf("End\n");
	return sum;
}

 int main()
 {
    
    
	 int n = 100;
	 int ret = accumulate(n);
	 printf("%d\n", ret);
	 return 0;
 }

To review make/Makefilethe use of:

touch Makefile
vim Makefile
//Makefile的内容
Test:Test.c
	gcc Test.c -o Test -std=c99
.PHONE:clean
clean:
	rm -f Test
  • The meaning of the option here -std=c99is: forthe operation of defining the variable i in the loop is c99only supported, so it needs to be declared at compile time. Otherwise, the following error will appear:

insert image description here

2. Install the gdb debugger

In Linux, we use gdbto debug; switch root identity or use sudothe command to install:

yum install -y gdb

3. Enter debugging

After the executable file ( Test) is generated, use the command gdb[file name] to enter debugging.

gdb Test
  • If you directly Testdebug the file, there will be an error report without debugging information, as shown in the figure below:

insert image description here

  • The reason is that there are two ways to publish a program: releaseand debug. gcc/g++The executable program generated by default is releaseversion. And we all know that debugging is aimed at debugthe version of the executable program, because only debugthe version contains debugging information.
  • When gcc/g++compiling, -gyou can select the generated debugversion Testfile by adding options (in order to distinguish, you can debugadd a suffix to the version file -debug, and releaseadd a suffix to the version -release).

Amended Makefilefile:

Test-debug:Test.c
	gcc Test.c -o Test-debug -g -std=c99

.PHONE:clean
clean:
	rm -f Test 

Regenerate the file Test-debugand enter debug:

make
gdb Test-debug

4. Debug related instructions

  • gdb[文件名]: enter debugging
  • Ctrl + dor quit: exit debug
  • ist+ [行号]or l + [行号](short form): Displays the source code of an executable program. Start displaying from the line number, and display 10 lines at a time

(After using this command for the first time, you can directly press Enter to continue executing the previous command for convenience. Other commands have the same effect)

insert image description here

  • list + [函数名]or l + [函数名]: display the source code of a function

insert image description here

  • runOr r : run the program (run until the end of the program if no breakpoint is set)

insert image description here

  • break + [行号]Or b + [行号]: set a breakpoint on a certain line
  • info + bView breakpoint information

insert image description here

  • break + [函数名]: Set a breakpoint at the beginning of a function
  • delete/d breakpoint n: delete the breakpoint nnumbered

insert image description here

  • delete/d breakpoint: remove all breakpoints
  • disable breakpoint: disable breakpoint

insert image description here

  • enable breakpoint: enable breakpoint

insert image description here

  • nextor n: process by process ( vsin F10)
  • stepor s: statement by statement ( vsin F11)

insert image description here

  • finish: Execute until the current function returns, then stop and wait for the command
  • printOr p: print the value of the expression, the value of the variable can be modified or the function can be called through the expression
  • p + 变量: print variable value
  • set + 变量: modify the value of the variable
  • continueor c: run from the current position until the next breakpoint
  • display + 变量名: Trace to view a variable, displaying its value each time you stop
  • undisplay: untrack those variables that were previously set
  • until + 行号: jump to a line to execute
  • breaktraceOr bt: view function calls and parameters at all levels
  • info + 变量 + locals: View the value of the local variable of the current stack frame;

gdbCommonly used commands are summarized here.

When learning gdb, we should VScompare it with debugging in the environment to understand better, for example:

  1. requivalent vstoF5
  2. bEquivalent vsto setting a breakpoint in
  3. nequivalent vstoF10
  4. sF11Equivalent to (at the function) in vs
  5. p/displayEquivalent to vssurveillance in

gdbThe instructions in are generally abbreviated, for example:

  1. list——l
  2. run——r
  3. break——b
  4. delete——d
  5. breakpoint——b
  6. next——n
  7. step——s
  8. print——p
  9. continue——c
  10. breaktrace——bt

This is the end of this article, the code text is not easy, please support me a lot! !

Guess you like

Origin blog.csdn.net/weixin_67401157/article/details/131732917
Recommended