在std::cerr上打印内容时设置gdb断点

在std::cerr上打印内容时设置gdb断点,gdb,qt-creator,stderr,Gdb,Qt Creator,Stderr,我使用QtCreator作为gdb的IDE和前端。当运算符运行时,如何设置断点 如何在std::cerr上设置断点 你的问题毫无意义:std::cerr是一个全局变量。只能在函数上设置断点。您还可以在变量上设置观察点,这样当修改变量时GDB就会停止,但这也可能不是您想要的 您可能会问:“当某些内容写入STDERR\u FILENO文件描述符时,如何停止?” 如果是这样的话,catch syscall write可能就是答案(但真正的答案取决于您的操作系统,您没有透露) 更新: 我认为捕获系统调用

我使用QtCreator作为gdb的IDE和前端。当运算符运行时,如何设置断点 如何在std::cerr上设置断点

你的问题毫无意义:
std::cerr
是一个全局变量。只能在函数上设置断点。您还可以在变量上设置观察点,这样当修改变量时GDB就会停止,但这也可能不是您想要的

您可能会问:“当某些内容写入
STDERR\u FILENO
文件描述符时,如何停止?”

如果是这样的话,
catch syscall write
可能就是答案(但真正的答案取决于您的操作系统,您没有透露)

更新:

我认为捕获系统调用写入不是一个选项,因为我经常向文件写入

您可以将syscall捕获点设置为有条件写入
STDERR\u FILENO
(Linux上为2)。示例(这将在Linux/x86_64上运行,但需要针对Linux/ix86进行调整):

cat t.cc
#包括
使用名称空间std;
int main()
{

你说得对吗?有什么事我想停下来
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]