How to implement GetThreadContext on Linux / Unix?

GetThreadContext is a Windows API.

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

I wonder how to implement it on Linux. How to get register information for a specified stream in Linux?

Like this:

 pthread_create(thread_id, ...); ... func(thread_id, reg_info) { //get the reg_info by thread_id. ?? } 
+2
source share
1 answer

A personal way to get thread information is to use get_thread_area() . On the get_thread_area() man page:

get_thread_area() returns a record in the current thread of the Thread Local Storage (TLS) array. The record index corresponds to the value u_info->entry_number passed by the user. If the value is within the bounds, get_thread_area() copies the corresponding TLS record to the area pointed to by u_info .

But, if you want to read the value of the register, you need to use the built-in assembly. For example, to get the esp value, you can use the following built-in assembly:

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

This way you can extract ebp , eip , etc. Hope this helps!

+3
source

All Articles