0

given a C++ program with:

int a;

I'd like to obtain the symbol name ("a") of address &a at runtime. I can do it via objdump but can I also do it at runtime? If there exists a way, how do I demangle its name?

Context: I need to profile an application (nope perf does not work in my case) so I am wrapping function calls, e.g. pthread_mutex_lock() using global defines and then track the address of the mutex variable and the RIP of the pthread_mutex_lock() call. I can do the postprocessing by calling into objdump (address to mangled_name mapping) I just thought there exists an API to do it at runtime.

hlitz
  • 635
  • 6
  • 24
  • http://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application – lcs Oct 21 '14 at 19:13
  • The link did not really help. I am not interested in the type of a (which would be int) but in its name. It should be a simple symbol table lookup, no reflection needed. – hlitz Oct 21 '14 at 19:49
  • `int a;` is insufficient specification. Are you actually talking about an exported global variable? Or could it be a function local? – nobody Oct 21 '14 at 20:04
  • 2
    The duplicate doesn't address OP's question . OP is asking for a mapping from identifiers to variables at runtime. That may loosely be called "reflection" but neither the question nor any of the answers on the "duplicate" go into that. – M.M Oct 21 '14 at 20:43
  • @hlitz do `std::map vars; ` and then use `vars["a"]` instead of `a` – M.M Oct 21 '14 at 20:44
  • @AndrewMedico In my particular case its a global but it would be good if it works for locals as well – hlitz Oct 21 '14 at 23:21
  • @MattMcNabb Won't work so well in my case (see edit above) – hlitz Oct 21 '14 at 23:29
  • Perhaps dlsym would work? I'd post an answer, but I can't given that this is still (incorrectly?) marked as duplicate. You might consider editing your question to state how it's different from the question it was marked as a duplicate of, and then hit "reopen" to try to get it unmarked. – Cel Skeggs Jun 12 '15 at 06:18

0 Answers0