3

I'm trying to step through llvm's opt program (for an assignment) and the instructor suggested setting a breakpoint at runOnFunction. I see this in one of the files:

bool InstCombiner::runOnFunction(Function &F) { /* (Code removed for SO) */ }

but gdb does not seem to find the runOnFunction breakpoint. It occurred to me that the problem might be namespaces? I tried this but gdb never breaks, it just creates the fooOpt.s file:

(gdb) b runOnFunction
Function "runOnFunction" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (runOnFunction) pending.
(gdb) r -S -instcombine -debug -o ~/Desktop/fooOpt.s ~/Desktop/foo.s

I'm on a Mac so I don't have objdump but otool produces 5.6 million lines, wading through that for a starting point does not seem reasonable as runOnFunction appears more than once there.

asimes
  • 5,749
  • 5
  • 39
  • 76
  • `InstCombiner` was not namespace, it is class.. And it is class [`llvm::InstCombiner`](http://llvm.org/docs/doxygen/html/classllvm_1_1InstCombiner.html) from `llvm` namespace. So, right command may sound like `break 'llvm::InstCombiner::runOnFunction(llvm::Function &F)'` or even just `break sourceFile.cpp:line`, where line is first statement in runOnFunction – osgx Apr 03 '14 at 04:12

1 Answers1

7

Gdb has several builtin commands to find name of such functions. First is info functions, which can be used with optional regexp argument to grep all available functions, https://sourceware.org/gdb/current/onlinedocs/gdb/Symbols.html

info functions regexp

Print the names and data types of all defined functions whose names contain a match for regular expression regexp. Thus, ‘info fun step’ finds all functions whose names include step; ‘info fun ^step’ finds those whose names start with step. If a function name contains characters that conflict with the regular expression language (e.g. ‘operator*()’), they may be quoted with a backslash.

So, you can try info functions runOnFunction to get the name. Sometimes it can be useful to add quotes around name when doing break command.

The other way is to use rbreak command instead of break (b). rbreak will do regexp search in functions names and may define several breakpoints: https://sourceware.org/gdb/current/onlinedocs/gdb/Set-Breaks.html#Set-Breaks

rbreak regex

Set breakpoints on all functions matching the regular expression regex. This command sets an unconditional breakpoint on all matches, printing a list of all breakpoints it set. ...

The syntax of the regular expression is the standard one used with tools like grep. Note that this is different from the syntax used by shells, so for instance foo* matches all functions that include an fo followed by zero or more os. There is an implicit .* leading and trailing the regular expression you supply, so to match only functions that begin with foo, use ^foo.

(or even rbreak file:regex to limit search to single source file)

PS: if you want, you can turn on or off C++ function name demangling with set print demangle on or off (https://sourceware.org/gdb/current/onlinedocs/gdb/Debugging-C-Plus-Plus.html#Debugging-C-Plus-Plus). With demangling turned off it will be easier to copy function name to break command.

osgx
  • 90,338
  • 53
  • 357
  • 513
  • Thank you, I didn't realize I could use regex to find fuctions – asimes Apr 03 '14 at 03:56
  • Actually I did the same error when I patched the bash+gdb script `callgraph` from http://stackoverflow.com/a/1980793/196561 and http://stackoverflow.com/a/1980770/196561. I need get all function list from the executable, filter it with regex, and then define breakpoints on all filtered functions. I did this with `info functions` and external `grep`, and with `info functions regex` it will be much faster for big projects. Also, I should try to modify the `callgraph.tar.gz` to use `rbreak`, but I'm not sure if it possible to define `commands` for all breakpoints, generated by `rbreak`. – osgx Apr 03 '14 at 04:01
  • Yes, I did notice it was quite slow to do a regex search (the project is big) but `grep` works. Actually, since I know the file your suggestion of using that helps, thanks again – asimes Apr 03 '14 at 04:08
  • PS, setting breakpoints for C++ was always a hard part of gdb for me. There is manual with solution: http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_gdb.html - ... "breakpoints in C++ programs" "*you need to specify methods and data members using the "classname::" prefix. In addition, you often need to use a leading ' before a name for gdb to find the symbol*", `(gdb) break 'BufMgr::pinPage(int, Page *&, int)'` – osgx Apr 03 '14 at 04:11