shared object file path that is executed by the cu

2019-08-29 01:34发布

问题:

Is there a way to get the file path/file name of the .so that is currently under execution by a thread? The program is written in c++ and run on a 64-bit Linux 3.0 machine.

回答1:

You can sequentially read (from inside your process) the file /proc/self/maps to get the memory mapping (including those for shared objects) of the current process.

Then you could get your program counter (or those of the caller) and find in which segment it is. Perhaps backtrace or GCC builtin_return_address is relevant.

You might also use the dladdr function.

See proc(5), backtrace(3), dladdr(3) man pages, and also this answer.

addenda

From a signal handler you could get the program counter when the signal was sent by using sigaction(2) with SA_SIGINFO. The sa_sigaction function pointers gets a ucontext_t from which you could get the program counter register (using machine dependent C code). Then you could handle it.

I suggest looking into detail into what GCC is doing



回答2:

I think the closes thing is to get a list of all shared libraries loaded by your process. You can do that either by using pmap or lsof.