On x86 (either 64-bit or 32-bit) Linux -- for example:
void signal_handler(int) {
// want to know where the program is interrupted ...
}
int main() {
...
signal(SIGALRM, signal_handler);
alarm(5);
...
printf(...); <------- at this point, we trigger signal_handler
...
}
In signal_handler, how can we know we are interrupted at printf in main()?
Workaround idea: if there are only a small number of places which can trigger a signal handler or you are only interested in which bigger block it happened you could maintain that in a variable.
Use sigaction with SA_SIGINFO set in sa_flags.
Prototype code:
Run it and hit Ctrl-C. (Use Ctrl-\ to terminate...)
This is for x86_64. For 32-bit x86, use
REG_EIP
instead ofREG_RIP
.[edit]
Of course, if you are actually in a library function (like
printf
) or a system call (likewrite
), the RIP/EIP register might point somewhere funny...You might want to use libunwind to crawl the stack.
Depending on your OS / Platform, it could be in a variety of areas:
Without having additional info, I don't think we can track this much further. Adding a C/C++ tag to your might generate more responses and views.
With timer-based signals, which instruction triggered the signal is a toss of the coin.