I need to intercept and trace signals from any binaries, like strace does it under linux. I don't need a so verbose output like the real one strace. I just want to know how it works, how can I intercept signal and how can I trace them. Thanks in advance :)
相关问题
- Multiple sockets for clients to connect to
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- how to get running process information in java?
Intercepting signals to other processes is something you should not do for any reason other than debugging them. This is what strace is intended for. Processes should be capable of handling their own signals.
Needless to say, if you are writing a debugger, understand ptrace().
This is a simple implementation:
Put somewhere in your
int main()
several calls tosignal()
, one for each signal you want to catch. The first argument is the signal name; the second is the signal handler function (more on that below):Now, write a signal function. It must return void and accept an int:
void SignalHandler(int signal_number)
:That's it! You can also test it by sending a signal to yourself with the function
raise(SIGNAL_NAME)
; for example, tryraise(SIGTERM);
!strace uses the
ptrace()
system call for tracing, which also allows you to intercept (and possibly manipulate) signals sent to the process.Here's a tiny example: