Georgy Ivanov
Georgy Ivanov

Reputation: 91

gdb: search function by name to set a breakpoint

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:

  1. How to set a breakpoint on operator << (..., cosnt std::string&) ?
  2. I want to set a breakpoint, but i don't know the exact name of the function. How to search for a function using regexp or part of its name ?

Upvotes: 8

Views: 15237

Answers (1)

scottt
scottt

Reputation: 7228

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++.

Upvotes: 16

Related Questions