在std :: cerr上打印某些内容时设置gdb断点 [英] Set gdb breakpoint when something is printed on std::cerr

查看:84
本文介绍了在std :: cerr上打印某些内容时设置gdb断点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将qt-creator用作IDE和gdb的前端.当operator<<时,如何设置断点?在std :: cerr变量上被调用?是可以从qt-creator还是我必须独立运行gdb?

I'm using qt-creator as an IDE and frontend for gdb. How can I set a breakpoint when operator<< is called on std::cerr variable? Is it possible from qt-creator or I will have to run gdb standalone?

推荐答案

如何在std :: cerr上设置断点

How can I set a breakpoint at std::cerr

您的问题没有道理: std :: cerr 是全局变量.您只能在函数上设置断点.您还可以在变量上设置观察点,以便在修改变量时GDB停止,但也可能不是您想要的.

Your question doesn't make sense: std::cerr is a global variable. You can set breakpoints only on functions. You can also set watchpoints on variables, so GDB stops when the variable is modified, but it's likely not what you want either.

您可能正在问 是:当将某些内容写入 STDERR_FILENO 文件描述符时,如何停止?"

What you are probably asking is: "how can I stop when something is written to STDERR_FILENO file descriptor?".

如果是这种情况, catch syscall write 可能是答案(但是实际答案取决于您的操作系统,而您并未透露).

If that's the case, catch syscall write might be the answer (but the real answer depends on your OS, which you didn't reveal).

更新:

我相信

捕获syscall写入不是一种选择,因为我经常写入文件

catching syscall write is not an option I believe, because I'm frequently writing to files

您可以在写入 STDERR_FILENO (在Linux中为2)的条件下使syscall捕获点为条件.示例(这将在Linux/x86_64上运行,但需要针对Linux/ix86进行调整):

You can make the syscall catchpoint conditional on writing to STDERR_FILENO (which is 2 on Linux). Example (this will work on Linux/x86_64, but would need to be adjusted for 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]

这篇关于在std :: cerr上打印某些内容时设置gdb断点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆