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我已經更新了答案。 –