Thread: Signal and exception handling

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    This probably isn't possible, or at least not easy, but is there a way to redefine abort() to call your own abort_exception() function?

    I've seen some C code that does something like this:
    Code:
    #define malloc  myMalloc
    I'm guessing you'd have to do that in all the files that call abort() though... So I guess I'm wondering if there's a way to globally re-link all the object files to call your abort() instead of the regular C abort()?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    cpjust makes a good point: just replace "abort()" with your own function - and you don't really need a different name, as long as you link that object file before the standard C library, it should take your abort instead of the other one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    cpjust makes a good point: just replace "abort()" with your own function - and you don't really need a different name, as long as you link that object file before the standard C library, it should take your abort instead of the other one.

    --
    Mats
    This sounds like an interesting experiment, so I'll probably try it out when I get some time.
    Do you know if that would still work if the code that's calling abort() was in a separate .lib file that's being linked or would the .lib file need to be recompiled first?

    Also, wouldn't you get Linker errors about abort() already defined in file blah...? I've always found those kinds of errors a pain in the ass to get rid of.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    10

    yes, maybe...

    Quote Originally Posted by cpjust View Post
    This probably isn't possible, or at least not easy, but is there a way to redefine abort() to call your own abort_exception() function?

    I've seen some C code that does something like this:
    Code:
    #define malloc  myMalloc
    I'm guessing you'd have to do that in all the files that call abort() though... So I guess I'm wondering if there's a way to globally re-link all the object files to call your abort() instead of the regular C abort()?
    Dear cpjust,
    Dear Mats,

    Thanks for your input. Maybe it is possible to do something along those lines. I am unsure whether it is possible to shadow abort since I am not calling it directly. Also, sometimes the code gets SEGV. However, I will try to work in that direction and post the outcome. I was also thinking I could set some flag instead of throwing an exception (if exception handling needs a valid stack to backtrace and the signal handler caller corrupts it), then hope that the code somehow reaches the place where my catch is, and query the flag there instead of catch()ing.

    Thanks again for you help, everyone who has replied!

    Kind regards,

    Nils.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nts View Post
    Thanks for your input. Maybe it is possible to do something along those lines. I am unsure whether it is possible to shadow abort since I am not calling it directly.
    Nothing should be calling it. I don't know of any C library routines which call abort(). If you're not calling it, nobody is calling it.

    Also, sometimes the code gets SEGV.
    Under UNIX, the result of ignoring a SIGSEGV signal which was not generated with raise() or kill() is undefined. It will probably lead to an infinite loop. Here's what happens:

    1. Code attempts to access bogus memory location
    2. Processor signal interrupt, calls SIGSEGV handler
    3. Handler returns, CPU goes to the same instruction again
    4. Go to step 1

    I was also thinking I could set some flag instead of throwing an exception
    yes, Yes, YES.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    10
    Quote Originally Posted by brewbuck View Post
    Nothing should be calling it. I don't know of any C library routines which call abort(). If you're not calling it, nobody is calling it.
    assert() does. But then assert could also be easily replaced my myassert() which in my code would unfortunately only solve a small part of the problem.

    Quote Originally Posted by brewbuck View Post
    Under UNIX, the result of ignoring a SIGSEGV signal which was not generated with raise() or kill() is undefined. It will probably lead to an infinite loop. Here's what happens:

    1. Code attempts to access bogus memory location
    2. Processor signal interrupt, calls SIGSEGV handler
    3. Handler returns, CPU goes to the same instruction again
    4. Go to step 1
    Oh, it goes to the same instruction? That may make sense from the point of view of the OS but here it creates trouble. So, in that case any signal handler that sets a flag should not return, or should it? But if it waits and a different thread throws an exception, our thread will not go to the catch either, or will it?!

    Nils.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nts View Post
    Oh, it goes to the same instruction? That may make sense from the point of view of the OS but here it creates trouble.
    What other behavior would make any sense? The idea is to trap a bad memory access, do something that hopefully fixes the situation, then try again. If it just moved on to the next instruction, then that memory access acts like it never existed in the first place, which means your program has just become a big mess of "undefined."

    So, in that case any signal handler that sets a flag should not return, or should it? But if it waits and a different thread throws an exception, our thread will not go to the catch either, or will it?!
    Exceptions only propagate within a single thread. So the exception has to be thrown by the same thread which will ultimately handle it. As for whether the signal handler should return... It must always return. How could it not?

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    10
    Quote Originally Posted by brewbuck View Post
    What other behavior would make any sense? The idea is to trap a bad memory access, do something that hopefully fixes the situation, then try again. If it just moved on to the next instruction, then that memory access acts like it never existed in the first place, which means your program has just become a big mess of "undefined."
    Yes, that's what I meant, from an OS point of view it does make sense. From a programmer's point of view retrying that instruction would mean that the source of the problem must be fixed first. Since we are not dealing with a page fault but with something unknown in an unknown (to the signal handler) context it would be better for us to return elsewhere... But that would require contextual information and cannot be guessed by the compiler, I do realise that.

    Quote Originally Posted by brewbuck View Post
    Exceptions only propagate within a single thread. So the exception has to be thrown by the same thread which will ultimately handle it. As for whether the signal handler should return... It must always return. How could it not?
    Well, since the data is apparently faulty we really do not want it to return to the method where the signal occurred in order to avoid an infinite signal/catcher loop. Instead we really want the IP to point to the catch() clause or some other defined position. Maybe with a long jump we could do that, but in each stack state (i.e. method, possibly in each for loop etc?) we would then need to register the address to jump to so that the signal handler would only jump within the "same stack state" location. Sounds like our own try-catch construct, too bad the real one does not do it :-(

    OK, I will try something but tomorrow -- right now my head is spinning...

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nts View Post
    OK, I will try something but tomorrow -- right now my head is spinning...
    You say that the model here is: Test piece of data. If fault caught, discard data and try something else. Why not test each piece of data in its own thread. If the thread crashes, who cares? You don't have to create a new thread for every single piece of data you test. Just make one, go into your loop, and run until you crash, logging your progress all the while. When the crash inevitably occurs, spawn a new thread and start your loop again where you left off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Signal handler function - pointer to this gets lost
    By maus in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2009, 09:10 AM
  2. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  3. Atomic Operations
    By Elysia in forum Windows Programming
    Replies: 27
    Last Post: 03-27-2008, 02:38 AM
  4. POSIX Signal Handling
    By nine-hundred in forum C Programming
    Replies: 8
    Last Post: 04-13-2007, 02:08 PM