I'm trying to collect the stacktrace of the target thread. For this purpose, I start a new thread and signal to the target thread in JNI:
//this is in a new thread
while (true) {
usleep(100000);
syscall(__NR_tgkill, pid, targetThread, SIGPROF);
}
And then, every time the signal comes, this function is called:
void signalHandler(int signal, siginfo *info, void *context_) {
ucontext_t *context = reinterpret_cast<ucontext_t *>(context_);
mcontext_t mcontext = context->uc_mcontext;
//get the stacktrace from mcontext_t
};
I can get the mcontext_t of the target thread, and I see the struct is:
struct sigcontext {
__u64 fault_address;
__u64 regs[31];
__u64 sp;
__u64 pc;
__u64 pstate;
__u8 __reserved[4096] __attribute__((__aligned__(16)));
};
However, I don't know how to get the stacktrace from the sigcontext and even what means of these fields. I searched for lots of blogs, they always use fp and pc or backtrace(), but I can't find it.