当前线程执行的共享对象文件路径 [英] shared object file path that is executed by the current thread

查看:62
本文介绍了当前线程执行的共享对象文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取当前正在由线程执行的 .so 的文件路径/文件名?该程序是用 C++ 编写的,并在 64 位 Linux 3.0 机器上运行.

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.

推荐答案

您可以顺序读取(从您的进程内部)文件 /proc/self/maps 以获取内存映射(包括那些对于共享对象)当前进程.

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.

然后你可以得到你的程序计数器(或调用者的计数器)并找到它在哪个段中.也许 backtraceGCC builtin_return_address 是相关的.

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.

您也可以使用 dladdr 函数.

You might also use the dladdr function.

参见 proc(5), backtrace(3), dladdr(3)手册页,以及这个答案.

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

从信号处理程序中,您可以使用 sigaction(2)SA_SIGINFO.sa_sigaction 函数指针获取一个 ucontext_t,您可以从中获取程序计数器寄存器(使用机器相关的 C 代码).然后你就可以处理了.

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.

我建议详细了解 GCC 正在做什么

这篇关于当前线程执行的共享对象文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆