Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?
like image 374
Georgy Ivanov Avatar asked May 13 '13 12:05

Georgy Ivanov


People also ask

How do I set a breakpoint in a specific file in GDB?

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.

Which command in GDB is used to find all breakpoints?

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.

How do you get to the next breakpoint in GDB?

Just press c. It will continue execution until the next breakpoint.

Can you call functions in GDB?

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.


1 Answers

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

like image 181
scottt Avatar answered Sep 21 '22 14:09

scottt