1
我使用qt-creator作为gdb的IDE和前端。如何在std :: cerr变量上调用运算符< <时如何设置断点?是否有可能从qt-creator或我将不得不运行gdb独立?在std :: cerr上打印某些内容时设置gdb断点
我使用qt-creator作为gdb的IDE和前端。如何在std :: cerr变量上调用运算符< <时如何设置断点?是否有可能从qt-creator或我将不得不运行gdb独立?在std :: cerr上打印某些内容时设置gdb断点
如何设置在性病断点:: CERR
你的问题没有任何意义:std::cerr
是一个全局变量。您只能在函数上设置断点。你也可以在变量上设置观察点,所以当变量被修改时GDB停止,但它可能不是你想要的。
你是什么可能是问的是:“如果有什么东西写到STDERR_FILENO
文件描述符?”,我该怎么办?
如果是这样,catch syscall write
可能是答案(但真正的答案取决于你的操作系统,你没有透露)。
更新:
捕捉系统调用写是不是一种选择,我相信,因为我经常写入文件
可以使系统调用,捕获点条件上写STDERR_FILENO
(在Linux上是2)。示例(这将在Linux/x86_64上工作,但需要针对Linux/ix86进行调整):
cat t.cc
#include <iostream>
using namespace std;
int main()
{
cout << "Hello,";
cerr << "error 1" << endl;
cout << " World!" << endl;
cerr << "error 2" << endl;
}
gcc -g t.cc
gdb -q ./a.out
Reading symbols from /tmp/a.out...done.
(gdb) catch syscall write
Catchpoint 2 (syscall 'write' [1])
# Make the catchpoint conditional on writing to stderr:
(gdb) cond 2 $rdi == 2
# By default, "catch syscall" will stop both before and after the actual syscall
# Ignoring the catchpoint once will skip past the "after" stop.
(gdb) command 2
Type commands for breakpoint(s) 2, one per line.
End with a line saying just "end".
>ignore 2 1
>end
(gdb) r
Starting program: /tmp/a.out
Hello,
Catchpoint 2 (call to syscall write), 0x00007ffff7383ff0 in __write_nocancel() at ../sysdeps/unix/syscall-template.S:82
82 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) x/s $rsi
0x400a83: "error 1" # Good: we caught our first write to std::cerr
(gdb) c
Continuing.
error 1
Catchpoint 2 (call to syscall write), 0x00007ffff7383ff0 in __write_nocancel() at ../sysdeps/unix/syscall-template.S:82
82 in ../sysdeps/unix/syscall-template.S
(gdb) x/s $rsi
0x7ffff76298e3 <_IO_2_1_stderr_+131>: "\n" # I didn't know endl gets a separate write syscall.
(gdb) c
Continuing.
World!
Catchpoint 2 (call to syscall write), 0x00007ffff7383ff0 in __write_nocancel() at ../sysdeps/unix/syscall-template.S:82
82 in ../sysdeps/unix/syscall-template.S
(gdb) x/s $rsi
0x400a93: "error 2"
(gdb) c
Continuing.
error 2
Catchpoint 2 (call to syscall write), 0x00007ffff7383ff0 in __write_nocancel() at ../sysdeps/unix/syscall-template.S:82
82 in ../sysdeps/unix/syscall-template.S
(gdb) x/s $rsi
0x7ffff76298e3 <_IO_2_1_stderr_+131>: "\n"
(gdb) c
Continuing.
[Inferior 1 (process 17291) exited normally]
您是对的。我想在某些情况下在cerr变量上调用<<运算符时停止。 我正在使用Linux。并且捕获系统调用写入不是我相信的选项,因为我经常写入文件。 – Dejwi
@Dejwi我已经更新了答案。 –