2 {-# OPTIONS_HADDOCK hide #-}
3 -----------------------------------------------------------------------------
5 -- Module : GHC.TopHandler
6 -- Copyright : (c) The University of Glasgow, 2001-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
13 -- Support for catching exceptions raised during top-level computations
14 -- (e.g. @Main.main@, 'Control.Concurrent.forkIO', and foreign exports)
16 -----------------------------------------------------------------------------
19 module GHC.TopHandler (
20 runMainIO, runIO, runIOFastExit, runNonIO,
21 topHandler, topHandlerFastExit,
22 reportStackOverflow, reportError,
28 import Control.Exception
30 import Foreign.C ( CInt )
33 import GHC.Prim (unsafeCoerce#)
35 -- | 'runMainIO' is wrapped around 'Main.main' (or whatever main is
36 -- called in the program). It catches otherwise uncaught exceptions,
37 -- and also flushes stdout\/stderr before exiting.
38 runMainIO :: IO a -> IO a
39 runMainIO main = (do a <- main; cleanUp; return a) `catchException` topHandler
41 -- | 'runIO' is wrapped around every @foreign export@ and @foreign
42 -- import \"wrapper\"@ to mop up any uncaught exceptions. Thus, the
43 -- result of running 'System.Exit.exitWith' in a foreign-exported
44 -- function is the same as in the main thread: it terminates the
48 runIO main = catchException main topHandler
50 -- | Like 'runIO', but in the event of an exception that causes an exit,
51 -- we don't shut down the system cleanly, we just exit. This is
52 -- useful in some cases, because the safe exit version will give other
53 -- threads a chance to clean up first, which might shut down the
54 -- system in a different way. For example, try
56 -- main = forkIO (runIO (exitWith (ExitFailure 1))) >> threadDelay 10000
58 -- This will sometimes exit with "interrupted" and code 0, because the
59 -- main thread is given a chance to shut down when the child thread calls
60 -- safeExit. There is a race to shut down between the main and child threads.
62 runIOFastExit :: IO a -> IO a
63 runIOFastExit main = catchException main topHandlerFastExit
64 -- NB. this is used by the testsuite driver
66 -- | The same as 'runIO', but for non-IO computations. Used for
67 -- wrapping @foreign export@ and @foreign import \"wrapper\"@ when these
68 -- are used to export Haskell functions with non-IO types.
71 runNonIO a = catchException (a `seq` return a) topHandler
73 topHandler :: Exception -> IO a
74 topHandler err = catchException (real_handler safeExit err) topHandler
76 topHandlerFastExit :: Exception -> IO a
77 topHandlerFastExit err =
78 catchException (real_handler fastExit err) topHandlerFastExit
80 -- Make sure we handle errors while reporting the error!
81 -- (e.g. evaluating the string passed to 'error' might generate
82 -- another error, etc.)
84 real_handler :: (Int -> IO a) -> Exception -> IO a
85 real_handler exit exn =
88 AsyncException StackOverflow -> do
92 -- only the main thread gets ExitException exceptions
93 ExitException ExitSuccess -> exit 0
94 ExitException (ExitFailure n) -> exit n
101 reportStackOverflow :: IO a
102 reportStackOverflow = do callStackOverflowHook; return undefined
104 reportError :: Exception -> IO a
106 handler <- getUncaughtExceptionHandler
110 -- SUP: Are the hooks allowed to re-enter Haskell land? If so, remove
112 foreign import ccall unsafe "stackOverflow"
113 callStackOverflowHook :: IO ()
115 -- try to flush stdout/stderr, but don't worry if we fail
116 -- (these handles might have errors, and we don't want to go into
117 -- an infinite loop).
120 hFlush stdout `catchException` \_ -> return ()
121 hFlush stderr `catchException` \_ -> return ()
123 cleanUpAndExit :: Int -> IO a
124 cleanUpAndExit r = do cleanUp; safeExit r
126 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
127 -- compiler doesn't let us declare that as the result type of a foreign export.
128 safeExit :: Int -> IO a
129 safeExit r = unsafeCoerce# (shutdownHaskellAndExit $ fromIntegral r)
131 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
132 -- re-enter Haskell land through finalizers.
133 foreign import ccall "Rts.h shutdownHaskellAndExit"
134 shutdownHaskellAndExit :: CInt -> IO ()
136 fastExit :: Int -> IO a
137 fastExit r = unsafeCoerce# (stg_exit (fromIntegral r))
139 foreign import ccall "Rts.h stg_exit"
140 stg_exit :: CInt -> IO ()