gdb break before a certain string is written to std:cerr


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. <br /> The function has 2 args: __out and __s. <br /> 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.


  • setContentView shows number; how can I find layout after decompile code?
  • Thread can't count, giving wrong result
  • Porting an Android application to Xamarin?
  • How can I implement my own hook function with LSM?
  • How can I pass complex expression to parametrized active pattern?
  • Installing ocropus-0.4.4
  • fscanf not reading floats correctly
  • Call C++ class member function from C (Visual Studio)
  • Reaping zombie process - child
  • Iterating over a container bidirectionally
  • C++ String tokenisation from 3D .obj files
  • std::system Exception when instantiating a singleton object
  • OpenMP and File I/O
  • Detecting null parameter in preprocessor macro
  • Function calls are not supported. Consider replacing the function or lambda with a reference to an e
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • How to revert to previous XCode version?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • output of program is not same as passed argument
  • Akka Routing: Reply's send to router ends up as dead letters
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • Android Google Maps API OnLocationChanged only called once
  • How to get Windows thread pool to call class member function?
  • unknown Exception android
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?