1

I have a shared library with 2 methods (among others of course) that i want to port to linux:

GetProcessName() - Should get the executable name or path (either would do)
GetModuleName() - Should return the name of the module that's running the code (either dll or exe).

In a windows environment I use GetModuleFileName with a different variation for both methods.

I've seen plenty of solutions for getting the process name, but most of them seem kinda Hacky.
And I haven't found a solution for getting the module name.

Is there a similar function in linux ?
How can I implement them ?

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • For the process there is [this](http://stackoverflow.com/a/1528493/63743). I don't think there is an equivalent of the running libs or at least one that would not be seriously "hacky". – Duck May 14 '14 at 13:40
  • I guess I can make do with a "hacky" solution for the running module/library if it's the only way, but i haven't found any solution for that yet. – Yochai Timmer May 14 '14 at 13:52
  • @MSalters: Isn't the duplicate less specific than this question? While the answer to that question (how to get path to an executable/library file) is no doubt part of solution to this question, the other question and it's answer do not address the question of how to get the path to the library that is **currently executing**. Would an answer to that question be relevant to the duplicate question? – eerorika May 14 '14 at 14:10
  • @user2079303: It's indeed less specific: it can get any library, not just the one currently executing. You just need an address, which is the current IP. – MSalters May 14 '14 at 14:19
  • @MSalters: And what's the simplest, non compiler/arch specific way (as long as it works on linux) to get the current IP? It doesn't seem trivial to me and I can't find such question using the search feature. Should I create a new question for that? – eerorika May 14 '14 at 14:33
  • @user2079303: Ehm, the current Instruction Pointer is a register on all Linux architectures I'm aware of. I didn't mean IP as in 127.0.0.1. – MSalters May 14 '14 at 14:40
  • @MSalters: I understood what you meant. My question is, how to read the register in c++ i.e. how to get the current value of the instruction pointer. Preferably in a way that doesn't depend on compiler or cpu architechture. – eerorika May 14 '14 at 14:47
  • No compiler-independent way (ISO C++ does not assume an IP register exists), GCC has `__builtin_return_address(0)` (put this in a non-inlined helper function) – MSalters May 14 '14 at 14:51
  • @MSalters __builtin_return_address(0) still wont get me the module name. – Yochai Timmer May 14 '14 at 14:55
  • @MSalters This long debate just shows it's not answered anywhere. should be reopened so someone can answer it for the rest of the world to see. – Yochai Timmer May 14 '14 at 14:57

1 Answers1

2

Since there's some confusion, here are the two steps to get the "module" data for the current function.

  1. Get the current function, either by the IP register or by calling a small function that just returns __builtin_return_address(0). Yet another method is to just take &foo inside foo().
  2. Call dladdr, passing the address of the current function.
Evg
  • 25,259
  • 5
  • 41
  • 83
MSalters
  • 173,980
  • 10
  • 155
  • 350