GetThreadContext 是一个 Windows API。
BOOL WINAPI GetThreadContext(
_In_ HANDLE hThread,
_Inout_ LPCONTEXT lpContext
);
我想知道如何在linux中实现它。 Linux中如何获取指定线程的寄存器信息?
像这样:
pthread_create(thread_id, ...);
...
func(thread_id, reg_info)
{
//get the reg_info by thread_id.
??
}
最佳答案
获取线程信息的 Linux 特定方法是使用 get_thread_area()
。从 get_thread_area()
手册页:
get_thread_area()
returns an entry in the current thread's Thread Local Storage (TLS) array. The index of the entry corresponds to the value ofu_info->entry_number
, passed in by the user. If the value is in bounds,get_thread_area()
copies the corresponding TLS entry into the area pointed to byu_info
.
但是,如果你想读取寄存器值,你需要借助内联汇编。例如,要检索 esp 的值,您可以使用以下内联汇编:
unsigned sp;
__asm __volatile("movl %%esp, %0" : "=r" (sp));
return sp;
这样就可以提取ebp
、eip
等,希望对你有帮助!
关于linux - 如何在Linux/Unix中实现GetThreadContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17480853/