shared object file path that is executed by the cu

2019-08-29 02:03发布

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.

2条回答
戒情不戒烟
2楼-- · 2019-08-29 02:21

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.

查看更多
时光不老,我们不散
3楼-- · 2019-08-29 02:24

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

查看更多
登录 后发表回答