2
votes

GetThreadContext is a Windows API.

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

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

Like this:

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

1 Answers

2
votes

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

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.

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;

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