llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.92k stars 11.52k forks source link

Can't easily call a C++ function in a binary without debug information, even if I can break on it #22585

Open 320883c9-c86b-4d56-9205-f787dd194847 opened 9 years ago

320883c9-c86b-4d56-9205-f787dd194847 commented 9 years ago
Bugzilla Link 22211
Version unspecified
OS All
CC @jrmuizel,@jdm,@jwatt,@jasonmolenda

Extended Description

I'd like to easily be able to attach lldb to a Firefox Nightly build and call a function called mozilla_sampler_save_profile_to_file, like this:

(lldb) print (void)mozilla_sampler_save_profile_to_file("/Users/mstange/Desktop/profile.txt")

But since this binary doesn't come with debugging information, lldb gives me an error:

error: use of undeclared identifier 'mozilla_sampler_save_profile_to_file'

It can, however, break on the function:

(lldb) b mozilla_sampler_save_profile_to_file Breakpoint 1: where = XUL`mozilla_sampler_save_profile_to_file(char const*), address = 0x000000010253d2a0

As a user of lldb, I can now take that address, cast it to a function pointer, and call the function through that pointer:

(lldb) p ((void()(const char))0x000000010253d2a0)("/Users/mstange/Desktop/profile.txt")

Or, instead of copy-pasting the address, I can do a little scripting:

(lldb) script Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.

lldb.debugger.HandleCommand('print ((void()(const char))0x%0x)("/Users/mstange/Desktop/profile.txt")' % lldb.target.FindFunctions("mozilla_sampler_save_profile_to_file").symbols[0].addr.load_addr)

It would be nice if lldb could simplify this process for me. It knows the address of the function already, and it could theoretically derive the argument types from the demangled function name.

In https://bugzilla.mozilla.org/show_bug.cgi?id=1118228 I'm adding a user defined lldb function called 'callfunc' to the lldb helpers that are included in the Firefox source, which makes this a little easier.

jasonmolenda commented 9 years ago

NB the originator knew this but it's worth calling out explicitly - this is a problem with functions that have a mangled name.