linux - 如何在Linux/Unix中实现GetThreadContext?

标签 linux windows cpu-registers

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 of u_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 by u_info.

但是,如果你想读取寄存器值,你需要借助内联汇编。例如,要检索 esp 的值,您可以使用以下内联汇编:

unsigned sp;
__asm __volatile("movl %%esp, %0" : "=r" (sp));
return sp;

这样就可以提取ebpeip等,希望对你有帮助!

关于linux - 如何在Linux/Unix中实现GetThreadContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17480853/

相关文章:

c++ - 如何检索 COM 类的接口(interface) ID 以便将其传递给 CoCreateInstance?

c - 返回类型是否节省空间或时间

emulation - 6502 仿真中 SBC 溢出后溢出标志被清除吗?

assembly - 保留堆栈中的字节 : x86 Assembly (64 bit)

c - Linux C 如何为 http 响应 block 动态分配内存?

linux - 如何在 "sed"命令中输入数学表达式?

C# 和 Mysql 不工作

windows - RegOpenKeyEx/RegGetValue 在存在的键上返回 ERROR_FILE_NOT_FOUND

linux - linux脚本的source和./execution的区别

python - 使用 python 创建 bash 文件