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 -----------------------------------------------------------------------------
17 module GHC.TopHandler (
18 runIO, runNonIO, reportStackOverflow, reportError
25 import Foreign.C.String
29 import GHC.Prim (unsafeCoerce#)
31 -- | 'runIO' is wrapped around @Main.main@ by @TcModule@. It is also wrapped
32 -- around every @foreign export@ and @foreign import \"wrapper\"@ to mop up
33 -- any uncaught exceptions. Thus, the result of running
34 -- 'System.Exit.exitWith' in a foreign-exported function is the same as
35 -- in the main thread: it terminates the program.
38 runIO main = catchException main topHandler
40 -- | The same as 'runIO', but for non-IO computations. Used for
41 -- wrapping @foreign export@ and @foreign import \"wrapper\"@ when these
42 -- are used to export Haskell functions with non-IO types.
45 runNonIO a = catchException (a `seq` return a) topHandler
47 topHandler :: Exception -> IO a
48 topHandler err = catchException (real_handler err) topHandler
50 -- Make sure we handle errors while reporting the error!
51 -- (e.g. evaluating the string passed to 'error' might generate
52 -- another error, etc.)
54 real_handler :: Exception -> IO a
57 AsyncException StackOverflow -> reportStackOverflow True
59 -- only the main thread gets ExitException exceptions
60 ExitException ExitSuccess -> safe_exit 0
61 ExitException (ExitFailure n) -> safe_exit n
63 Deadlock -> reportError True
64 "no threads to run: infinite loop or deadlock?"
66 ErrorCall s -> reportError True s
67 other -> reportError True (showsPrec 0 other "\n")
69 reportStackOverflow :: Bool -> IO a
70 reportStackOverflow bombOut = do
71 (hFlush stdout) `catchException` (\ _ -> return ())
77 reportError :: Bool -> String -> IO a
78 reportError bombOut str = do
79 (hFlush stdout) `catchException` (\ _ -> return ())
80 withCStringLen str $ \(cstr,len) -> do
81 writeErrString errorHdrHook cstr len
87 foreign import ccall "&ErrorHdrHook" errorHdrHook :: Ptr ()
89 foreign import ccall "ErrorHdrHook" errorHdrHook :: Ptr ()
92 foreign import ccall unsafe "writeErrString__"
93 writeErrString :: Ptr () -> CString -> Int -> IO ()
95 -- SUP: Are the hooks allowed to re-enter Haskell land? If so, remove
97 foreign import ccall unsafe "stackOverflow"
98 callStackOverflowHook :: IO ()
100 foreign import ccall unsafe "stg_exit"
101 stg_exit :: Int -> IO ()
104 exit r = unsafeCoerce# (stg_exit r)
106 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
107 -- re-enter Haskell land through finalizers.
108 foreign import ccall "shutdownHaskellAndExit"
109 shutdownHaskellAndExit :: Int -> IO ()
111 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
112 -- compiler doesn't let us declare that as the result type of a foreign export.
113 safe_exit :: Int -> IO a
114 safe_exit r = unsafeCoerce# (shutdownHaskellAndExit r)