這是我在Linux上做的一個++例子。這個例子已經用gcc 4.3編譯。
#include <iostream>
#include <stdio.h>
#include <setjmp.h>
void write_error_code()
{
std::cerr << "some error code";
}
int main()
{
std::cout << "Start, main()\n";
std::cerr << "some error code" << std::endl;
std::cerr << "some error code?" << std::endl;
std::cerr << "some error code?" << std::endl;
write_error_code();
std::cout << "End, main()\n";
return 0;
}
我看了看拆機一看,發現std::cerr << ""
與符號名稱_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
的功能。
該函數有2個參數:__out
和__s
。
所以我創建了一個.gdbinit
文件:
host: srv2-x64rh5-02, OS: Linux 2.6.18-238.el5>more .gdbinit
file a.out
b _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
commands
silent
if strcmp(__s, "some error code") == 0
print "Some error code has been given as an arg"
bt
c
else
c
end
end
r
quit
在這裏測試我的程序:
host: srv2-x64rh5-02, OS: Linux 2.6.18-238.el5>gdb -q ./a.out
Reading symbols from /import/home/sergey.kurenkov/src/linux.x64.5.0/tests/test.br_on_std_cerr/a.out...done.
Breakpoint 1 at 0x400710
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000
Start, main()
$1 = "Some error code has been given as an arg"
#0 std::operator<< <std::char_traits<char> > (__out=..., __s=0x400a4c "some error code")
at /home/zalex/test/gcc_build/x86_64-unknown-linux-gnu/libstdc++-v3/include/ostream:509
#1 0x0000000000400873 in main() at main.cpp:13
some error code
some error code?
some error code?
$2 = "Some error code has been given as an arg"
#0 std::operator<< <std::char_traits<char> > (__out=..., __s=0x400a4c "some error code")
at /home/zalex/test/gcc_build/x86_64-unknown-linux-gnu/libstdc++-v3/include/ostream:509
#1 0x000000000040084f in write_error_code() at main.cpp:7
#2 0x00000000004008f5 in main() at main.cpp:18
some error codeEnd, main()
Program exited normally.
爽,謝謝!這需要我花一些時間來消化,所以我稍後可能會爲你提供一些問題。我非常感謝你投入這一切! – 2012-08-16 20:22:12