[Linux debugger-gdb use]

Table of contents

1. Background

2. use

 3 Command Summary


1. Background

Through the study of C language, we know that there are two ways to release the program, debug mode and release mode, and the debug mode is what we call the debug mode. We are already familiar with the debugging of the VS series under the Windows platform. Next, let's take a look at how to debug under Linux without a graphical interface!


2. use

First, let's create a c++ file and write code into it:

 #include<iostream>
  2 using namespace std;
  3 
  4 int AddSum(int& n)                                                                                                                      
  5 {
  6   int sum=0;
  7   for(int i=1;i<=n;i++)
  8   {
  9     sum+=i;
 10   }
 11   return sum;
 12 }
 13 
 14 int main()
 15 {
 16   int num=10;
 17   int sum=AddSum(num);
 18   cout<<sum<<endl;
 19   return 0;
 20 }

Run it:

[grm@VM-8-12-centos lesson3]$ g++ test.cpp
[grm@VM-8-12-centos lesson3]$ ll
total 16
-rwxrwxr-x 1 grm grm 8960 Jan  4 22:12 a.out
-rw-rw-r-- 1 grm grm  219 Jan  4 22:11 test.cpp
[grm@VM-8-12-centos lesson3]$ ./a.out
55

Then add debug information for it:

We can clearly observe that the size of the file has obviously changed after adding the debugging information:

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

Then start debugging: gdb+ binFile (executable file)

Exit: ctrl + d or quit
[grm@VM-8-12-centos lesson3]$ gdb a.out
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/grm/lesson3/a.out...done.
(gdb) l
7	  for(int i=1;i<=n;i++)
8	  {
9	    sum+=i;
10	  }
11	  return sum;
12	}
13	
14	int main()
15	{
16	  int num=10;
(gdb) r
Starting program: /home/grm/lesson3/a.out 
55
[Inferior 1 (process 11983) exited normally]
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64 libgcc-4.8.5-44.el7.x86_64

The l here means: list/l line number: display the source code of binFile, then go down to the last position, and list 10 lines each time.

r:  r or run: run the program (similar to f5 in VS)

Let’s introduce two more commonly used commands: n and s

n or next: single execution   (process by process, f10 under VS)
s or step: enter function call  (statement by statement, f11 under VS)

Before introducing these two commands, we have to introduce a breakpoint command, break(b) line number: set a breakpoint on a certain line

Of course, it is also possible to break a line or a function in the specified file: b file name: function name (line number)

 We might as well set a breakpoint on the ninth line, then run up to the ninth line, and read the code step by step:

 But we found that the value of the variable cannot be observed now, so what should we do?

We can use display variable name: track to view a variable and display its value every time we stop

(gdb) display sum
1: sum = 1
(gdb) display i
2: i = 1
(gdb) n

Breakpoint 1, AddSum (n=@0x7fffffffe448: 10) at test.cpp:9
9	    sum+=i;
2: i = 2
1: sum = 1
(gdb) n
7	  for(int i=1;i<=n;i++)
2: i = 2
1: sum = 3
(gdb) n

Breakpoint 1, AddSum (n=@0x7fffffffe448: 10) at test.cpp:9
9	    sum+=i;
2: i = 3
1: sum = 3

When we display the variable name through display, we can find that the display variable will be printed out every time the process is carried out. To cancel, use undisplay + variable name.

We can also use until to jump to the number of lines we want to jump to: until X line number: jump to X line

will run the program up to the number of lines we jumped to.

If we want to just stop after running through this function, we can use: finish

To run from one breakpoint to another, use: continue(c)


 3 Command Summary

In addition, there are some instructions that you can go down and study by yourself. I have listed a table here for your reference:

list / l line number
Display the binFile source code, and then go down to the last position, and list 10 lines each time.
list / l function name
List the source code of a function
r or run
run the program
n or next
Single execution (by process)
s or step
Step into a function call (statement-by-statement)
break(b)  line number
set a breakpoint on a line
break  function name
Set a breakpoint at the beginning of a function
info break
View breakpoint information
finish
Execute until the current function returns, then stand down and wait for the command
print(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 variable
print variable value
see where
Modify the value of a variable
continue ( or c)
Run from the current position to the next breakpoint instead of stepping through the program
delete breakpoints
remove all breakpoints
delete breakpoints n
: Delete the breakpoint with the serial number n (note that the serial number is not the number of lines)
disable breakpoints
disable breakpoint
enable breakpoints
enable breakpoint
info ( or i) breakpoints
See which breakpoints are currently set
display  variable name
Trace through a variable, displaying its value each time you stop
undisplay
Untrack those variables that were previously set
until X line number
until X line number
breaktrace ( or bt)
View function calls and parameters at all levels
info i) locals
View the value of the local variable of the current stack frame
quit/Ctrl+d
exit gdb

Guess you like

Origin blog.csdn.net/m0_68872612/article/details/128555063