Catching SIGSEGV - handelling segmentation faults
I have been messing around with signals lately and i was wondering if there is a nice simple way to pass arguments into a signal handler.
This is so it can tell you information about how the program died - almost throwing and catching exceptions in java. However, I can't find how the signal handeller will be able to take arguments from the location where the program crashed. Code:
#include<signal.h> Does anyone know a method of doing this? Does one exist? If so, it could be very helpful for many... |
That's what a core file and debugger are for. You usually have to unwind the stack a bit (if the stack wasn't damaged) before you find the line in your program which was the initial cause of the problem (usually you pass a bad pointer to a library function).
What you want to do can be accomplished by changing a global variable, but it is cumbersome, ugly, and not very useful. Learning to use a debugger is far more useful and doesn't make your beautiful code look like Jabba the Hutt's mother. |
i see.
so the signal handler can only take in parameters of int and void but nothing else. hmm, i guess that answers no to any simple mechanism. |
Quote:
If you use the newer signal functions (e.g, sigaction() rather than plain-old signal()), however, you can get a little more information passed to your handler besides the signal number itself. In particular, if you set the flag SA_SIGINFO, your signal catching function will need to have signature: Code:
void func(int signo, siginfo_t *info, ucontext_t *context); |
ok,
i gave this a try like this Code:
#include<signal.h> Code:
7 Is this the correct usage of the handler and the related structures? |
As I said before, the simplest way to get the line number is to have extensive assignments to a global variable, so you can forget about which of your lines resulted in the fault. Plus, (as I've also said), the fault is very likely to be generated from within a call to a library, in which case you have no line number to refer to (unless you compiled yourself or used a version with the debug info not stripped + you have the source on hand).
Although sigaction does give you a lot more info, you have to build in some debugger features simply to display the info you want. The big question is: WHY? There is absolutely no advantage over a core file + debugger, or a debugger attached to a process. People have spent many years building features into debuggers, so use them. The only advantage I can think of is to give a partial stack trace which developers might hope to recover some useful information from, such as the case with Microsoft. But if you do that, be prepared to be inundated with useless bug reports. |
Quote:
The statement: Code:
printf("%d\n",__LINE__); When you use gdb (and compile with the greatest number of debugging symbols available), not only will it give you a stack trace, it will show the exact line number (and moreover, the exact assembly instruction) where the problem occurs. |
Btw, just for fun, here is a program which outputs the maximum possible amount of information which may be gleaned through siginfo (with the exception of the largely irrelevant floating-point state). It is targeted toward the i386 arch.
Code:
#include <signal.h> |
All times are GMT -5. The time now is 10:00 PM. |