[Linux] How to debug c++ in linux? Introduction and use of gdb.

  1. background

1.1. Prerequisite knowledge

There are two ways to release the program, debug mode and release mode
Binary programs from Linux gcc/g++, the default is release mode
To use gdb debugging, you must add the -g option when generating the binary program from the source code

Is there a difference in the debugging method on windows?

1. The idea of ​​debugging is the same
2. The operation mode of debugging must be different (command line debugging)

  1. start using

2.1. Install gdb

sudo yum install -y gdb

2.2. Use

gdb binFile exit: ctrl + d or quit /q

2.2.1. Display binFile source code (list/l)

Generally, list/l will remember the number of rows displayed last time, and then go down from the last position, and list 10 rows each time.
list/l [Line number]: From the line number to the following 10 lines.
list/l [function name]: List the source code of a function. Column 10 rows.

2.2.2. Add/delete breakpoint (break/b)

break/b line number: set a breakpoint on a certain line.
delete breakpoints / d: delete all breakpoints delete breakpoints / d [n]: delete the breakpoint with the serial number n (n is not the line number)
info b: view the breakpoint
info (or i) breakpoints: See which breakpoints are currently set
disable breakpoints: disable breakpoints (it will not stop when encountering a breakpoint)
(enable breakpoints) / enable: enable breakpoints
disable breakpoints [Number]: disable a certain breakpoint.
(enable breakpoints) / enable [serial number]: enable a certain breakpoint

2.2.3. View (info) (p) (display)

info b/break : View all current breakpoints.
info/i locals: View the value of local variables in the current stack frame.
p 【变量名】打印变量
display 变量名:跟踪查看一个变量,每次停下来都显示它的值 。(也可以查看地址)
undisplay:全部取消对先前设置的那些变量的跟踪。
undisplay 【序号】:取消对这个变量的跟踪。

2.2.4.运行调试(r/run)(n/next)(s/stop)(c/continue)

run/r:从开始连续而非单步执行程序(调试运行)
n/next:单条执行。 (逐过程)
s/step:进入函数调用(逐语句)
continue/c:从当前位置开始连续而非单步执行程序,也就是从当前位置开始到下一个断点或者结束。
finish:执行完当前函数,然后停下来等待命令 。可以帮我们快速的甄别那个函数出问题了。
until 【行号】:跳至X行。(可能不是预期行,去掉空格/无用的符号)

2.2.5.查看调用堆栈(bt)

bt :查看调用堆栈。

2.2.6.修改参数的值

set 【var】:修改变量的值,根据我们的需要修改临时变量的值
  1. 理解

3.1.f5 / ctrl+f5 / shift+f5

f5:调试运行,有断点直接停下来。 (r/run)
ctrl+f5:不调试执行,在断点不停。
shift+f5:取消调试。(q/quit)
f11:逐个语句(s/step)
f10:逐过程(n/next)

Guess you like

Origin blog.csdn.net/zxf123567/article/details/129595506