c++ - 我使用 gdb 跟踪 'catch' 和 'syscall' 失败,它报告找不到 'brk'?

标签 c++ linux gdb try-catch system-calls

我在ubuntu16.04下做了一个gdb的catch选项的小实验,如下:

$ cat e.cpp
#include<stdlib.h> 
#include<exception> 
int main() 
{ 
  try{ 
    throw 1; 
  }catch(std::exception e) 
  { 
  } 
  exit(0); 
} 

$ gcc e.cpp -g -lstdc++ $ gdb a.out

... 
Reading symbols from a.out...done. 
(gdb) catch throw 
Catchpoint 1 (throw) 
(gdb) catch syscall 
Catchpoint 2 (any syscall) 
(gdb) r 
Starting program: /home/a/cpp/a.out  

Catchpoint 2 (call to syscall brk), 0x00007ffff7df12e9 in __brk (addr=addr@entry=0x0) at ../sysdeps/unix/sysv/linux/x86_64/brk.c:31 
31    ../sysdeps/unix/sysv/linux/x86_64/brk.c: No such file or directory.

这个错误说明了什么?如果我再次使用 'r' 命令,程序将终止:

(gdb) r 
The program being debugged has been started already. 
Start it from the beginning? (y or n)  

这太奇怪了。我如何以及为什么会收到此 brk 错误?

最佳答案

当您在 gdb 中设置系统调用捕获点而不带任何参数时,您会在所有系统调用上设置它,请参阅 Setting Catchpoints :

If no argument is specified, calls to and returns from all system calls will be caught.

这是 gdb 在你完成后告诉你的:

(gdb) catch syscall 
Catchpoint 2 (any syscall) 

然后您运行该程序并立即捕获 brk 系统调用。这并不奇怪,因为 brk 系统调用用于动态内存分配实现(实际上是用于 malloc 实现)并且您的程序当然会进行一些内存分配。

当系统调用被捕获时,gdb 试图为您打印 brk.c 中的第 31 行,但失败了,因为您的操作系统中很可能没有安装 libc 源。您应该忽略此错误,因为您不是在调试 libc 代码,而只是在 try catch 系统调用。

您可能想要的是查看 bt 以了解您在代码中的何处分配内存。

关于c++ - 我使用 gdb 跟踪 'catch' 和 'syscall' 失败,它报告找不到 'brk'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39788258/

相关文章:

c++ - 从函数返回的对象的延长生命周期

c++ - 为什么我不能在基类型标准 :vector? 中存储派生类

c++ - 在 C++ 中的函数定义中混合指针和引用

python - 无法在python生成的ubuntu中找到输出文件

java - Linux环境下Mule应用与mysql数据库的数据库连接问题

c - 为什么在 GCC 编译运行正常时 GDB 显示段错误

c++ - LLDB 中 GDB 的 “define” 是什么?

c - GDB 无法读取文件

c++ - 获取函数原型(prototype)的简单方法?

linux - 制作-j 8 g++ : internal compiler error: Killed (program cc1plus)