[C++学习]gdb调试

1 调试普通文件

记得加入-g参数 否则进不去调试模型
gdb 目标程序

g++ -o main  main.cpp  -g
gdb main

此时进入了gdb调试模式下
set args 设置程序运行的参数u
b 20 表示在第20行设置断点,可以设置多个断点
r 表示开始运行程序,程序运行到断点的位置会停下来。如果没有遇到断定会一直运行下去
n next, 执行当前语句,若是函数调用,则不会进入函数内部
s step 执行当前语句,如果为函数调用,则会进入函数内部(注意了,如果函数是库函数或第三方提供的函数,用s也是进不去的,因为没有源代码,如果是自定义的函数,只要有源码就可以进去。)
p print 显示变量或表达式的值,如果p后面是表达式,会执行这个表达式。[重要]
continue c 继续运行程序,遇到下一个断点停止,如果没有遇到断点,程序将一直运行。
VS的F5

set var

设置变量的值。
假设程序中定义了两个变量:
int ii;
char name[21];
set var ii=10 把ii的值设置为10;
set var name=“西施”。

quit q 退出 gdb

exmple

(base) ubuntu@ubuntu:~/muke01/01gdb$ g++ -o main  main.cpp  -g
(base) ubuntu@ubuntu:~/muke01/01gdb$ gdb main
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 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/>.

--Type <RET> for more, q to quit, c to continue without paging--
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from main...
(gdb) set args 张三 西施 我是一只傻傻鸟
(gdb) b 20
Breakpoint 1 at 0x258e: file main.cpp, line 20.
(gdb) b 25
Breakpoint 2 at 0x260f: file main.cpp, line 25.
(gdb) run
Starting program: /home/ubuntu/muke01/01gdb/main 张三 西施 我是一只傻傻鸟
表白前的准备工作一。
表白前的准备工作二。

Breakpoint 1, main (argc=4, argv=0x7fffffffdaf8, envp=0x7fffffffdb20) at main.cpp:20
20          cout << "表白前的准备工作三。\n";
(gdb) n
表白前的准备工作三。
21          cout << "表白前的准备工作四。\n";
(gdb) n
表白前的准备工作四。
22          cout << "表白前的准备工作五。\n";
(gdb) c
Continuing.
表白前的准备工作五。
张三开始表白。
西施:我是一只傻傻鸟
表白完成。

Breakpoint 2, main (argc=4, argv=0x7fffffffdaf8, envp=0x7fffffffdb20) at main.cpp:25
25          for (int ii = 0; ii < 10; ii++) {
    
    
(gdb) b
Note: breakpoint 2 also set at pc 0x55555555660f.
Breakpoint 3 at 0x55555555660f: file main.cpp, line 25.
(gdb) p ii
$1 = 21845
(gdb) n
26              string str = "这是第" + to_string(ii) + "个超级女生。";
(gdb) p ii
$2 = 0
(gdb) set var ii=5
(gdb) n
27              cout << str << endl;
(gdb) n
这是第5个超级女生。
26              string str = "这是第" + to_string(ii) + "个超级女生。";
(gdb) n
25          for (int ii = 0; ii < 10; ii++) {
    
    
(gdb) n
26              string str = "这是第" + to_string(ii) + "个超级女生。";
(gdb) p ii
$3 = 6
(gdb) p ii =8
$4 = 8
(gdb) n
27              cout << str << endl;
(gdb) n
这是第8个超级女生。
26              string str = "这是第" + to_string(ii) + "个超级女生。";
(gdb) 

(base) ubuntu@ubuntu:~/muke01/01gdb$ g++ -o main  main.cpp  -g
(base) ubuntu@ubuntu:~/muke01/01gdb$ gdb main
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 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/>.

--Type <RET> for more, q to quit, c to continue without paging--
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from main...
(gdb) set args 张三 西施 我是一只傻傻鸟
(gdb) b 20
Breakpoint 1 at 0x258e: file main.cpp, line 20.
(gdb) b 25
Breakpoint 2 at 0x260f: file main.cpp, line 25.
(gdb) run
Starting program: /home/ubuntu/muke01/01gdb/main 张三 西施 我是一只傻傻鸟
表白前的准备工作一。
表白前的准备工作二。

Breakpoint 1, main (argc=4, argv=0x7fffffffdaf8, envp=0x7fffffffdb20) at main.cpp:20
20          cout << "表白前的准备工作三。\n";
(gdb) n
表白前的准备工作三。
21          cout << "表白前的准备工作四。\n";
(gdb) n
表白前的准备工作四。
22          cout << "表白前的准备工作五。\n";
(gdb) c
Continuing.
表白前的准备工作五。
张三开始表白。
西施:我是一只傻傻鸟
表白完成。

Breakpoint 2, main (argc=4, argv=0x7fffffffdaf8, envp=0x7fffffffdb20) at main.cpp:25
25          for (int ii = 0; ii < 10; ii++) {
(gdb) b
Note: breakpoint 2 also set at pc 0x55555555660f.
Breakpoint 3 at 0x55555555660f: file main.cpp, line 25.
(gdb) p ii
$1 = 21845
(gdb) n
26              string str = "这是第" + to_string(ii) + "个超级女生。";
(gdb) p ii
$2 = 0
(gdb) set var ii=5
(gdb) n
27              cout << str << endl;
(gdb) n
这是第5个超级女生。
26              string str = "这是第" + to_string(ii) + "个超级女生。";
(gdb) n
25          for (int ii = 0; ii < 10; ii++) {
(gdb) n
26              string str = "这是第" + to_string(ii) + "个超级女生。";
(gdb) p ii
$3 = 6
(gdb) p ii =8
$4 = 8
(gdb) n
27              cout << str << endl;
(gdb) n
这是第8个超级女生。
26              string str = "这是第" + to_string(ii) + "个超级女生。";
(gdb) 

2. 调试core文件[难]

如果程序在运行的过程中发生了内存泄漏,会被内核强行终止,提示“段错误(吐核)”,内存的状态将保存在core文件中,方便程序员进一步分析。
Linux缺省不会生成core文件,需要修改系统参数。
调试core文件的步骤如下:
1)用ulimit -a查看当前用户的资源限制参数;
2)用ulimit -c unlimited把core file size改为unlimited;
3)运行程序,产生core文件;
4)运行gdb 程序名 core文件名;
5)在gdb中,用bt查看函数调用栈。
示例:

#include <cstring>
#include <iostream>
using namespace std;

void bb(const int bh,const string xm)
{
  char *ptr=nullptr;
  *ptr=3;
  //strcpy(ptr,xm.c_str());
}

void aa(const int no,const string name)
{
  bb(3,"冰冰");
}

int main()
{
  aa(8,"西施");

  return 0;
}
(base) ubuntu@ubuntu:~/Desktop$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 126374
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 126374
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

(base) ubuntu@ubuntu:~/Desktop$ ulimit -c unlimited
(base) ubuntu@ubuntu:~/Desktop$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 126374
max locked memory       (kbytes, -l) 65536
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 126374
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

经过以上操作,会形成core文件。

(base) ubuntu@ubuntu:~/muke01/01gdb$ g++ -o main1  main1.cpp  -g
(base) ubuntu@ubuntu:~/muke01/01gdb$ main1
main1: command not found
(base) ubuntu@ubuntu:~/muke01/01gdb$ ./main1
Segmentation fault (core dumped)

3. gdb调试正在运行中的程序

gdb 程序名 -p 进程编号

#include <unistd.h>
#include <iostream>
using namespace std;

void bb(const int bh,const string xm)
{
  for (int ii=0;ii<1000000;ii++)
  {
    sleep(1);
    cout << "ii=" << ii << endl;
  }
}

void aa(const int no,const string name)
{
  bb(3,"冰冰");
}

int main()
{
  aa(8,"西施");

  return 0;
}

在ubuntu下我的程序没有出现 core.xxx xxx
应该是代表进程名

例程:

(base) ubuntu@ubuntu:~/muke01/01gdb$ gdb ./main2 -p 5991
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 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 ./main2...
Attaching to program: /home/ubuntu/muke01/01gdb/main2, process 5991
Could not attach to process.  If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user.  For more details, see /etc/sysctl.d/10-ptrace.conf
ptrace: Operation not permitted.
(gdb) b 21
Breakpoint 1 at 0x13f3: file main2.cpp, line 21.
(gdb) r
Starting program: /home/ubuntu/muke01/01gdb/main2 

Breakpoint 1, main () at main2.cpp:21
21	    aa(8, "西施");
(gdb) n
i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
i=11

猜你喜欢

转载自blog.csdn.net/weixin_40293999/article/details/132711568