I had a question about some results I am getting using libunwind. I have attempted this on both x86_64 and on normal x86 and in both cases I get weird results. What I have been trying to do is to use libunwind to get a stack trace of a certain thread of a process. So say we have process 2120, which has two threads, the original 2120 and TID 2121, what I want is a stack trace of 2121. Here is a code snippet of what I have done, this isn't production code by any means, just an example to do some testing, I am using the unwind-ptrace accessors that are bundled with libunwind.

int error = ptrace(PTRACE_ATTACH, realPID, (void *) 0, (void *) 0);
 if(error != 0)
     bail("Ptrace error 1", error);

 waitpid(realPID, NULL, __WALL);
error = ptrace(PTRACE_ATTACH, (realPID+1), (void *) 0, (void *) 0);
 if(error != 0)
     bail("Ptrace error 1+", error);
waitpid(realPID+1, NULL, __WALL);

 unw_addr_space_t uwas = unw_create_addr_space (&_UPT_accessors, 0);
 if (uwas == NULL)
   bail ("uwas == NULL", 0);

 unw_set_caching_policy(uwas, UNW_CACHE_NONE);

 void *arg3 = _UPT_create(realPID+1);
 if(arg3 == NULL)
   bail ("arg3 == NULL", 0);

 unw_cursor_t cur;

 error = unw_init_remote(&cur, uwas, arg3);
 if(error != 0)
   bail ("Init Error", error);

 //count the stack frames
 unw_proc_info_t info;
 unw_word_t blah = 0;
 char buff[300];
 size_t len = 300;
 printf("Stacktrace:\n\n");
 do{
     unw_get_proc_info(&cur, &info);
     printf("%d: %X\n", count, info.start_ip);
     count++;
     error = unw_step (&cur);
     if (error < 0)
       bail("Step Error", error);
 }while (error > 0);

 printf("\nStack Depth = %d\n", count);

 _UPT_destroy(arg3);

 unw_destroy_addr_space(uwas);

 error = ptrace(PTRACE_DETACH, (realPID+1), (void *) 0, (void *) 0);
 if (error != 0)
   bail ("Ptrace error 2+", error);
error = ptrace(PTRACE_DETACH, (realPID), (void *) 0, (void *) 0);
 if (error != 0)
   bail ("Ptrace error 2", error);

So this should give me a trace of the process with PID 1 greater than the one entered (which is usually the second task of a process, and in this case I am assuming this is always the case). I have used this code successfully to get a proper trace of the main thread, but when I try to get a trace of the second thread this is what I get under x86_64.

PID?: 25002
Tracing TID 25003
Stacktrace:

0: A9E09766

Stack Depth = 1

and under x86

PID?: 19114
Tracing TID 19115
Stacktrace:

0: B69402
1: B7DC4BA0
Bailing!
Error Message: Step Error
Error: -8

Stack Depth = 2

The error message in there is mine, but unw_step returns -8 after the second frame, which I think is UNW_EINVAL. As can be seen, these results don't make much sense. There should definitely be more than one frame, and I'm not getting step errors when tracing the main thread. This code works fine if I try to trace the main thread on both architectures with or without the second ptrace call for the second thread.

Any ideas?

Thanks,

Kyle







_______________________________________________
Libunwind-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/libunwind-devel

Reply via email to