I'm trying to find out what part of a program prints to stdout.
I can set a breakpoint using command like:
b std::ostream::operator<<(int)
but when i type: b std::operator<<(std::ostream&, const std::string&)
no breakpoint is created.
So there are two questions:
operator << (..., cosnt std::string&)
?Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.
The rbreak command can be used to set breakpoints in all the functions in a program, like this: (gdb) rbreak . Print a table of all breakpoints, watchpoints, and catchpoints set and not deleted, with the following columns for each breakpoint: Breakpoint Numbers.
Just press c. It will continue execution until the next breakpoint.
To call a function in the program, GDB has to temporarily modify the state of the inferior. This has potentially undesired side effects. Also, having GDB call nested functions is likely to be erroneous and may even crash the program being debugged.
Use "info functions <<.*string" to search for functions with << and string in their names. info functions takes a regular expression as argument.
Then select from the listed functions the one you want. Remove the ; at the end of the declaration if any and paste the declaration as an argument to the break command:
$ gdb -q ./ostream-operator-breakpoint
<...>
(gdb) start
Temporary breakpoint 1 at 0x4006b0: file ostream-operator-breakpoint.cc, line 4.
Starting program: /home/scottt/Dropbox/stackoverflow/ostream-operator-breakpoint
<...>
(gdb) info functions <<.*string
All functions matching regular expression "<<.*string":
File /usr/src/debug/gcc-4.7.2-20121109/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.h:
std::basic_ostream<char, std::char_traits<char> > &std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&);
std::basic_ostream<wchar_t, std::char_traits<wchar_t> > &std::operator<< <wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >&, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&);
(gdb) break std::basic_ostream<char, std::char_traits<char> > &std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
Breakpoint 2 at 0x3cbfa94640: file /usr/src/debug/gcc-4.7.2-20121109/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.h, line 2750.
The start (or run) command is required for dynamically linked programs. Unless you first start the inferior process, info functions wouldn't list functions from shared libraries such as libstdc++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With