Reputation: 185
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.
??
}
Upvotes: 2
Views: 1061
Reputation: 142535
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 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
.
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!
Upvotes: 2