blog & exceptions

Well, this is another way to which i have attempted an half-assed attempt at trying to populate my site with something useful. Useful being the key word. I actually am writing something, unlike my mediawiki attempt, and cool, this saves drafts…good for when the power goes out, like it was doing this morning…nothing like the sounds of power transformers down the street blowing up to motivate me to save my work, but considering im on my laptop it doesn’t matter the slightest to me.

Now for the useful.

I’ve been attempting to find a suitable replacement for what windows calls structured exception handling (SEH), in linux.
SEH works similar to regular exceptions, but it can handle all non standard exceptions like segmentation faults and invalid instructions and the like, and allows the programmer to handle these in a (hopefully) non-crash way and gracefully let the user know or do something else.
In linux, this doesn’t exist as availably as it does in windows, because visual c has __try and __except blocks that can be made easily. The internals of this work by all windows programs have a pointer to an exception handler structure at the start of memory pointer by the fs register (fs:[0]) and this is called when something goes wrong. Linux however, has no such thing.

Linux does have signals, and advanced ways to handle them. A signal in Linux, is an exception in Windows.

Linux’s way of handling these is via a function called sigaction, sure there is also signal function, but that in this instance is useless.
sigaction allows the programmer to select exactly what signals need to be handled, and allows a function handler to be specified for them. Now the useful part here is not only does it catch the signals, but then allow the program state to be changed before returning to the exact same spot where the signal occurred…like in windows.

For example, lets say a segmentation fault occurs, because i wrote to an invalid memory pointer, i would then be able to catch this signal (SIGSEGV), know exactly where im trying to write to, and exactly where in my program it is doing so from, but i can simply alter the memory location by overwriting the faulting instruction or skipping over it entirely.

Now this is extremely useful because some programs require this effect, for example in Project64. For Project64 which deals with this behavior quite a bit to be ported to linux, or and part of its CPU, this code would need to be modified to allow the exceptions to be caught this way.
Well, its what ive been trying to figure out lately anyway.

Example Code

Leave a Reply

You must be logged in to post a comment.