我使用 qt-creator 作为 gdb 的 IDE 和前端。当对 std::cerr 变量调用operator
如何在 std::cerr 设置断点
你的问题没有意义:std::cerr
是一个全球性的variable。您只能在函数上设置断点。您还可以在变量上设置观察点,这样当变量被修改时 GDB 就会停止,但这也可能不是您想要的。
你是什么probably问题是:“当有东西被写入时我怎样才能停止STDERR_FILENO
文件描述符?”。
如果是这样的话,catch syscall write
可能是答案(但真正的答案取决于您的操作系统,您没有透露)。
Update:
我认为捕获系统调用写入不是一个选项,因为我经常写入文件
您可以使系统调用捕获点以写入为条件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]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)