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