2

gdb の IDE およびフロントエンドとして qt-creator を使用しています。std::cerr 変数で operator<< が呼び出されたときにブレークポイントを設定するにはどうすればよいですか? qt-creator から可能ですか、それとも gdb をスタンドアロンで実行する必要がありますか?

4

1 に答える 1

2

std::cerr にブレークポイントを設定するにはどうすればよいですか

あなたの質問は意味がありません: std::cerris a global variable . 関数にのみブレークポイントを設定できます。変数にウォッチポイントを設定することもできるので、変数が変更されると GDB が停止しますが、それもおそらくあなたが望むものではありません。

おそらくあなたが尋ねているのは、「STDERR_FILENOファイル記述子に何かが書き込まれているときにどうすれば停止できますか?」ということです。

その場合はcatch syscall write、答えになる可能性があります (ただし、実際の答えは、明らかにしなかった OS によって異なります)。

アップデート:

私は頻繁にファイルに書き込んでいるので、システムコールの書き込みをキャッチすることは私が信じているオプションではありません

syscall キャッチポイントを書き込み時に条件付きにすることができます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]
于 2013-01-13T16:49:22.740 に答える