示例代码

 1 #include <iostream>
2 using namespace std;
3
4 void Print()
5 {
6 cout<<"helloworld"<<endl;
7 }
8
9 class TestDebug
10 {
11 public:
12 TestDebug()
13 {
14 cout<<"TestDebug"<<endl;
15 int temp=0;
16 ti = 0;
17 while(1)//调试自动变量
18 {
19 if(++temp==10)
20 break;
21 }
22 ti = temp;
23 }
24
25 void disPlayTi()
26 {
27 cout<<"ti:"<<ti<<endl;
28 }
29
30 private:
31 int ti;
32 };
33
34
35 int main() {
36
37 Print();
38 TestDebug tg;
39 tg.disPlayTi();
40 return 0;
41 }

使用gdb调试程序的前提是在编译程序时使用gcc -g的选项,这样才能在程序中生成调试相关的信息。下面我们就使用上面的示例代码来调试程序。

生成可调试程序

keqin@ubuntu:~/Test$ gcc -g hello.cpp -o hello -lstdc++

启动调试——gdb + 程序名

keqin@ubuntu:~/Test$ gdb hello 

#有时程序运行需要外设参数也就是main(int argc,char **argv)中的参数,可以使用 gdb --args +程序名 argv1 argv2 

启动成功后,对应如下输出,"(gdb)"开头标志着进入了可调式模式。

GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from hello...done.
(gdb)

帮助:

输入help可以获得所有命令的帮助,

(gdb) help  #获取所有可用帮助指令
List of classes of commands: aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands Type "help" followed by a class name for a list of commands in that class.
Type "help all" for the list of all commands.
Type "help" followed by command name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.

输入help + command可以获取到某个命令的详细帮助信息,如输入help break来查看如何添加断点。其输出类似于Linux的man帮助

gdb) help break
Set breakpoint at specified location.
break [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]
PROBE_MODIFIER shall be present if the command is to be placed in a
probe point. Accepted values are `-probe' (for a generic, automatically
guessed probe type), `-probe-stap' (for a SystemTap probe) or
`-probe-dtrace' (for a DTrace probe).
LOCATION may be a linespec, address, or explicit location as described
below. With no LOCATION, uses current execution address of the selected
stack frame. This is useful for breaking on return to a stack frame. THREADNUM is the number from "info threads".
CONDITION is a boolean expression.

Linespecs are colon-separated lists of location parameters, such as
source filename, function name, label name, and line number.
Example: To specify the start of a label named "the_top" in the
function "fact" in the file "factorial.c", use
"factorial.c:fact:the_top". Address locations begin with "*" and specify an exact address in the
program. Example: To specify the fourth byte past the start function
"main", use "*main + 4". ---Type <return> to continue, or q <return> to quit---

例如:

添加断点:

下面列举一些打断点的方式

break + 函数名

(gdb) break Print()
Breakpoint 1 at 0x40090a: file hello.cpp, line 6.

break +文件名:函数名

(gdb) break hello.cpp:Print()
 Note: breakpoint 1 also set at pc 0x40090a.
 Breakpoint 2 at 0x40090a: file hello.cpp, line 6.

 

break 文件名:行号

(gdb) break hello.cpp:39
Breakpoint 6 at 0x400951: file hello.cpp, line 39.

调试类中的成员函数

(gdb) break TestDebug #按Tab键补全
TestDebug TestDebug::TestDebug() TestDebug::disPlayTi()
(gdb) break TestDebug::TestDebug()
Breakpoint 3 at 0x4009d8: file hello.cpp, line 14.

执行调试(run)

(gdb) run
Starting program: /home/guilin/Test/hello Breakpoint 1, Print () at hello.cpp:6
6 cout<<"helloworld"<<endl;

继续运行到下个断点(continue)

(gdb) continue
Continuing.
helloworld Breakpoint 3, TestDebug::TestDebug (this=0x7fffffffe1d0) at hello.cpp:14
14 cout<<"TestDebug"<<endl;

//如果感觉断点不够,调试过程可以继续添加加断点。

打印变量值(print)

出于调试需要,有时候我们需要查看某个变量的值。

(gdb) print temp
$1 = 0

监视某个变量(watch)——调试过程中,变量数据发生变化会自动停下来。

(gdb) break 14      #先打断点
Breakpoint 20 at 0x4009d8: file hello.cpp, line 14.
(gdb) run       #运行程序
Starting program: /home/guilin/Test/hello
helloworld Breakpoint 20, TestDebug::TestDebug (this=0x7fffffffe1a0) at hello.cpp:14
14 cout<<"TestDebug"<<endl;
(gdb) watch temp   #调试成中添加监控变量
Hardware watchpoint 21: temp
(gdb) continue    #继续执行
Continuing.
TestDebug Hardware watchpoint 21: temp Old value = 0     #变量旧值
New value = 1     #变量新值 
0x0000000000400a09 in TestDebug::TestDebug (this=0x7fffffffe1a0) at hello.cpp:19
19 if(++temp==10)

添加条件断点:

我们有时需程序自动运行到某个临界条件而在此条件满足之前不去调试它,这时可以使用条件断点。

gdb)break hello.cpp:39 if temp>8 #当TestDebug::TestDebug 中temp>8时才中止运行 
Note: breakpoint 3 also set at pc 0x4009d8. Breakpoint 32 at 0x4009d8: file hello.cpp, line 19.

单步调试(next)——一般对应IDE中的F10

(gdb) next  
helloworld
38 TestDebug tg;
(gdb) next
TestDebug
39 tg.disPlayTi();

单步进入(step)——一般对应IDE中的F11

Breakpoint 9, main () at hello.cpp:37
37 Print();
(gdb) step  #调试时会进入到函数内部
Print () at hello.cpp:6
6 cout<<"helloworld"<<endl;
(gdb)

 

执行到下个断点(continue)——一般对应IDE中F5

(gdb) continue
Continuing.
TestDebug Breakpoint 31, TestDebug::TestDebug (this=0x7fffffffe1d0) at hello.cpp:16
16 ti = 0;
(gdb) print temp
$2 = 0

修改变量值(set var 变量名=value)

有时候有些有异常处理的分支无法进入,或者我们想提前到达某个临界条件。可以去直接修改变量的值以达到目的。

(gdb) run
Starting program: /home/guilin/Test/hello
helloworld Breakpoint 20, TestDebug::TestDebug (this=0x7fffffffe1a0) at hello.cpp:14
14 cout<<"TestDebug"<<endl;
(gdb) watch temp
Hardware watchpoint 22: temp
(gdb) c
Continuing.
TestDebug Hardware watchpoint 22: temp Old value = 0
New value = 1
0x0000000000400a09 in TestDebug::TestDebug (this=0x7fffffffe1a0) at hello.cpp:19
19 if(++temp==10)
(gdb) set var temp=9
(gdb) c
Continuing. Hardware watchpoint 22: temp Old value = 9
New value = 10
0x0000000000400a09 in TestDebug::TestDebug (this=0x7fffffffe1a0) at hello.cpp:19
19 if(++temp==10)

查看堆栈调用(backtrace)

(gdb) backtrace  #简写为bt
#0 main () at hello.cpp:39

清除断点:

clear

用法与break类似,如何创建,如何删除。

delete

delete(删除所有断点)

(gdb) delete
Delete all breakpoints? (y or n)

gdb调试用命令与一般调试方法的更多相关文章

  1. 调试多线程 & 查死锁的bug & gcore命令 & gdb对多线程的调试 & gcore & pstack & 调试常用命令

    gdb thread apply all bt 如果你发现有那么几个栈停在 pthread_wait 或者类似调用上,大致就可以得出结论:就是它们几个儿女情长,耽误了整个进程. 注意gdb的版本要高于 ...

  2. gdb调试常用命令

    gdb 调试常用命令 gcc -g mian.c -o main.out -o (定制生成的可执行文件的名称,缺省时为a.out) -g 使gdb可调试,在编译的时候,产生调试信息 gdb main. ...

  3. 使用 GDB 调试需要命令行参数的程序

    使用 gdb 命令提供的 --args 选项可以调试需要命令行参数的程序,如下: gdb --args a.out arg1 arg2 arg3

  4. gdb的调试常用命令

    一.gdb常用的命令 list                       l    常看源代码 break                  b    设置断点     b  10(行号)    b ...

  5. Linux Bash命令关于程序调试详解

    转载:http://os.51cto.com/art/201006/207230.htm 参考:<Linux shell 脚本攻略>Page22-23 Linux bash程序在程序员的使 ...

  6. 调试JMETER脚本的5种方法

    如果你曾经设计过JMeter脚本,我敢打赌你至少有一次弄清楚Json Extractor无法正常工作的原因.你猜怎么着?我去过那儿! 你知道为什么最好的JMeter Performance Engin ...

  7. GDB(十)--调试正在运行的进程

    我编写了一个循环: long i;    for (i = 0; i < 999999; i++) {        mt.a += 1;        sleep(1);    }把它编译成a ...

  8. 调试python 程序的几种方法总结

    程序能一次写完并正常运行的概率很小,基本不超过1%.总会有各种各样的bug需要修正.有的bug很简单,看看错误信息就知道,有的bug很复杂,我们需要知道出错时,哪些变量的值是正确的,哪些变量的值是错误 ...

  9. [vscode] os.getcwd(),调试和命令行运行的结果不一致

    问题描述: 调试和命令行运行的时候工作目录不一致 这会导致一个问题,我想从上级目录导入模块的话,F5调试就会找不到模块,而命令行则没问题 那么我该如何调试呢? 目录结构: top  └ folder_ ...

随机推荐

  1. c语言:输出汉字编码

    #include<stdio.h> main() { //char a[5]; //strcpy(a,"啊"); char a[5]="职"; pr ...

  2. DEV -C++源码中的中文复制粘贴乱码解决方案

    右击源代码用记事本打开,再复制,再粘贴,就没有乱码了

  3. 微信小程序云开发-添加数据

    一.数据的添加 使用add方法添加数据 添加完成后,在数据库中查询,可以看到数据库中添加了1条数据,此时添加的数据系统自动添加了_openid 将[添加]功能写到对应的方法中 wxml页面中,点击[添 ...

  4. 使用ThinkPHP5.0.12连接Mongo数据库的经验

    本地开发环境xamppv3.2.2,ThinkPHP5.0.12版本. 由于之前开发项目时使用的是TP3.2.3+mongo数据库,也是在本地进行的,所以也进行过mongo数据库驱动的配置.详细方法可 ...

  5. 每天五分钟Go - 常量

    常量的声明 常量使用const进行对不会被改变值的修饰符 const 常量名 [常量类型] = 常量值,和变量的声名一样可以省略类型 const a=1 const b="a" c ...

  6. 第五篇--Chorme浏览器主页被篡改

    解决方法:关闭谷歌浏览器,右击桌面快捷方式,查看属性,然后将target后面的网址删掉.并且任务栏的google打开方式,最好也把流氓网址删掉.之后就正常了.

  7. Java 异步编程的几种方式

    前言 异步编程是让程序并发运行的一种手段.它允许多个事情同时发生,当程序调用需要长时间运行的方法时,它不会阻塞当前的执行流程,程序可以继续运行,当方法执行完成时通知给主线程根据需要获取其执行结果或者失 ...

  8. ;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk

    ;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk ;~ 此脚本用于测试执行一行或多行AHK脚本源代码的效果;~ 此脚本最后修改于2019年9月22日20时03分;~ 把此 ...

  9. 使用策略者模式减少switch case 语句

    策略者模式 很简单的一个定义:抽象策略(Strategy)类:定义了一个公共接口,各种不同的算法以不同的方式实现这个接口,环境角色使用这个接口调用不同的算法,一般使用接口或抽象类实现. 场景 在这之前 ...

  10. kubernetes/k8s CRI分析-kubelet创建pod分析

    先来简单回顾上一篇博客<kubernetes/k8s CRI 分析-容器运行时接口分析>的内容. 上篇博文先对 CRI 做了介绍,然后对 kubelet CRI 相关源码包括 kubele ...