如何在Linux/Unix中实现GetThreadContext? [英] How to implement GetThreadContext in Linux/Unix?

查看:120
本文介绍了如何在Linux/Unix中实现GetThreadContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GetThreadContext是Windows API.

GetThreadContext is a Windows API.

BOOL WINAPI GetThreadContext(
_In_     HANDLE hThread,
_Inout_  LPCONTEXT lpContext
);

我想知道如何在Linux中实现它. 如何在Linux中检索指定线程的注册信息?

I wonder that how to implement it in linux. How to retrieves the register information of the specified thread in Linux?

赞:

pthread_create(thread_id, ...);
...
func(thread_id, reg_info)
{
    //get the reg_info by thread_id.
    ??
}

推荐答案

获取线程信息的特定于Linux的方法是使用get_thread_area().在get_thread_area()手册页中:

A Linux-specific way of getting thread info is to use get_thread_area(). From the get_thread_area() man page:

get_thread_area()返回当前线程的线程本地存储(TLS)数组中的一项.条目的索引对应于用户传递的u_info->entry_number值.如果该值是有界的,则get_thread_area()将相应的TLS条目复制到u_info指向的区域中.

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.

但是,如果要读取寄存器值,则需要帮助进行内联汇编. Fox示例,要检索esp的值,可以使用以下内联汇编:

But, if you want to read the register value you need to take help of inline assembly. Fox example, to retrieve the value of esp you can use the following inline assembly:

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

通过这种方式,您可以提取ebpeip等.希望对您有所帮助!

In this way, you can extract ebp, eip etc. Hope this will help!

这篇关于如何在Linux/Unix中实现GetThreadContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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