How can I get the suspended thread context?

Hi,

I want to suspend a thread and then get the conext of the thread after it is suspended.
I add a signal handler. But the handler does not run in the thread context.

How can I do it? Thanks.
hackbuteerAsked:
Who is Participating?
 
Computer101Connect With a Mentor Commented:
PAQed, with points refunded (250)

Computer101
E-E Admin
0
 
ahoffmannCommented:
please post code
0
 
hackbuteerAuthor Commented:
This is my test code.
Now I am sure I can get the stack top of the thread in the signal handler. But I want to get the more precise information about the thread context, like ESP, EIP, EBP of the last frame of the thread.

How can I do it?

Thank you very much.

==============================

pthread_attr_t pthread_attr;

void (*original_handler)(int) = NULL;
int mm = 0;

void signal_handler(int sig)
{
   int y;
   printf("y address is %#x\n", (int)&y);
}
 
void * thread_run(void* param )
{
    int x;
    printf("x address is %#x\n", (int)&x);
    original_handler = signal( SIGUSR1, signal_handler);
        if( original_handler == SIG_ERR )
            printf("signal() failed\n");
    mm = 1;
     
    while( mm != 2 )
        sleep(1);

    return 0;
}

int attach_system_thread(void)
{
  int stat;
  pthread_t tid = 0;
  void *(*sa)(void *) = ( void *(*)(void *) )thread_run;

  stat = pthread_create(&tid, 0, sa, 0);
  printf("tid=%#x\n", tid);
  assert(stat == 0);
   
  return (int)tid;

}

int main(void)
{
    int tid = attach_system_thread();
    while( mm != 1 )
        sleep(10);

    if( pthread_kill(tid, SIGUSR1) == -1 )
    {
        printf(" send signal failed %#x( EINVAL=%#x, ESRCH=%#x, EPERM=%#x )\n", errno, EINVAL, ESRCH, EPERM);
    }else{
        int i;
        for( i=0; i<4; i++ )
            sleep(1);
    }

    mm = 2;
     
    return 0;
}
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
slironmCommented:
Why not do the opposite - first catch the signal, see the state, and only then suspend the thread?

Liron.
0
 
hackbuteerAuthor Commented:
Yes, I want to do that.
I suspend the thread in the signal handler, I can get the return address. But where can I get the stored EIP?
0
 
jmcgOwnerCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
All Courses

From novice to tech pro — start learning today.