2013-01-13 88 views

回答

2

如何設置在性病斷點:: 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] 
+0

您是對的。我想在某些情況下在cerr變量上調用<<運算符時停止。 我正在使用Linux。並且捕獲系統調用寫入不是我相信的選項,因爲我經常寫入文件。 – Dejwi

+0

@Dejwi我已經更新了答案。 –