在某个字符串被写入std:cerr之前gdb break [英] gdb break before a certain string is written to std:cerr

查看:142
本文介绍了在某个字符串被写入std:cerr之前gdb break的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试一个大的应用程序,并且我想在某个字符串写入 std :: cerr 之前中断。我有办法做到这一点?



代码中有许多地方写这个字符串。
字符串并不总是这样的形式

pre $ stc :: cerr<<< 一些错误代码;

它可能是其他形式,如

  std :: cerr<< write_error_code(); 

另请注意,有很多事情正在写入 std :: cerr ,所以我不能在 std :: cerr

上打破。

我也有同样的问题,但对于 std :: cout 但我会发布另一个问题,如果答案不适用于此。

解决方案

这是我在Linux上做的一个С++示例。这个例子是用gcc 4.3编译的。

  #include< iostream> 
#include< stdio.h>
#include< setjmp.h>

void write_error_code()
{
std :: cerr<< 一些错误代码;
}

int main()
{
std :: cout<< Start,main()\\\
;
std :: cerr<< 一些错误代码<<的std :: ENDL;
std :: cerr<< 一些错误代码? <<的std :: ENDL;
std :: cerr<< 一些错误代码? <<的std :: ENDL;
write_error_code();
std :: cout<< End,main()\\\
;

返回0;
}

我看了一下反汇编,看到 std :: cerr<< 是一个符号名称 _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc 的函数。

该函数有2个参数: __ out __ s

所以我创建了一个 .gdbinit 文件:

  host:srv2-x64rh5-02,操作系统:Linux 2.6.18-238.el5>更多.gdbinit 
文件a.out
b _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
命令
silent
if strcmp(__ s,some error code)== 0
print某些错误代码已经作为arg给出了
bt
c
else
c
end
end
r
quit

在这里我测试我的程序:

  host:srv2-x64rh5-02,操作系统:Linux 2.6.18-238.el5> gdb  - q ./a.out 
从/import/home/sergey.kurenkov/src/linux.x64.5.0/tests/test.br_on_std_cerr/a.out...done中读取符号。
断点1在0x400710
警告:在添加的符号文件系统提供的DSO中找不到可加载部分,位于0x2aaaaaaab000
开始,main()
$ 1 =某些错误代码已被作为参数给出
#0 std :: operator<< <的std :: char_traits<炭> > (__out = ...,__s = 0x400a4csome error code)$ home / zalex / test / gcc_build / x86_64-unknown-linux-gnu / libstdc ++ - v3 / include / ostream:$ 50 $ b $在main.cpp的main()中,b#1 0x0000000000400873:13
一些错误代码
一些错误代码?
一些错误代码?
$ 2 =一些错误代码已经作为参数给出
#0 std :: operator<< <的std :: char_traits<炭> > (__out = ...,__s = 0x400a4csome error code)$ home / zalex / test / gcc_build / x86_64-unknown-linux-gnu / libstdc ++ - v3 / include / ostream:$ 50 $ b $在main.cpp的write_error_code()中,b#1 0x000000000040084f:7
#2 0x00000000004008f5 main.cpp:18
一些错误codeEnd,main()

程序正常退出。


I am debugging a large application and I would like to break before a certain string is written to std::cerr. I there a way to do this?

There are many places in the code where this string is written. The string is not always of the form

std::cerr << "some error code";

It might be of other forms such as

std::cerr << write_error_code();

Also note that there are many things being written to std::cerr so I can't just break on std::cerr.

I also have the same question but for std::cout but I will post another question if the answer does not apply to that as well.

解决方案

This is a С++ example I have made on Linux. The example has been built with 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;
}

I took a look at disassemble and saw that std::cerr << "" is a function with the symbol name _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc.
The function has 2 args: __out and __s.
So I created a .gdbinit file:

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

And here I test my program:

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.

这篇关于在某个字符串被写入std:cerr之前gdb break的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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