how to get the pc register for any thread on Linux?
My debug thread will monitor the memory开发者_开发技巧 usage. When it finds the inconsistency situation it will generate a coredump.
As I found, the coredump is not very accurate (looks a bit later than the moment the problem happened, because I need to do some cleanup in the debug thread before abort()
).
I want to record the exact %pc
register of the thread val at that time, how can I do that?
One way to get a 'recent' %pc
--program counter-- (also know as %ip
instruction pointer) is to save the state of your process with makecontext
void makecontext(ucontext_t *ucp, void (*func)(), int argc, ...);
and read the right value in the buffer: REG_EIP
as defined in sys/ucontext.h
context.uc_mcontext.gregs[REG_EIP];
Please note that the data structure is obviously machine-dependent, and for instance, if you were using a SPARC, the %ip
would have been stored in REG_RIP
--
as far as I know, there is no way to read this value for all the threads, you'll have to read the %pc
on each of them, and gather manually your data.
精彩评论