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 -- Top-level IO actions want to catch exceptions (e.g., 'forkIO' and
13 -- 'GHC.Main.mainIO') and report them - 'topHandler' is the exception
14 -- handler they should use for this.
16 -----------------------------------------------------------------------------
18 module GHC.TopHandler (
19 runIO, runNonIO, reportStackOverflow, reportError
26 import Foreign.C.String
30 import GHC.Prim (unsafeCoerce#)
32 -- | 'runIO' is wrapped around 'Main.main' by @TcModule@. It is also wrapped
33 -- around every @foreign export@ and @foreign import \"wrapper\"@ to mop up
34 -- any uncaught exceptions. Thus, the result of running
35 -- 'System.Exit.exitWith' in a foreign-exported function is the same as
36 -- in the main thread: it terminates the program.
39 runIO main = catchException main topHandler
41 -- | The same as 'runIO', but for non-IO computations. Used for
42 -- wrapping @foreign export@ and @foreign import \"wrapper\"@ when these
43 -- are used to export Haskell functions with non-IO types.
46 runNonIO a = catchException (a `seq` return a) topHandler
48 topHandler :: Exception -> IO a
49 topHandler err = catchException (real_handler err) topHandler
51 -- Make sure we handle errors while reporting the error!
52 -- (e.g. evaluating the string passed to 'error' might generate
53 -- another error, etc.)
55 real_handler :: Exception -> IO a
58 AsyncException StackOverflow -> reportStackOverflow True
60 -- only the main thread gets ExitException exceptions
61 ExitException ExitSuccess -> safe_exit 0
62 ExitException (ExitFailure n) -> safe_exit n
64 Deadlock -> reportError True
65 "no threads to run: infinite loop or deadlock?"
67 ErrorCall s -> reportError True s
68 other -> reportError True (showsPrec 0 other "\n")
70 reportStackOverflow :: Bool -> IO a
71 reportStackOverflow bombOut = do
72 (hFlush stdout) `catchException` (\ _ -> return ())
78 reportError :: Bool -> String -> IO a
79 reportError bombOut str = do
80 (hFlush stdout) `catchException` (\ _ -> return ())
81 withCStringLen str $ \(cstr,len) -> do
82 writeErrString errorHdrHook cstr len
88 foreign import ccall "&ErrorHdrHook" errorHdrHook :: Ptr ()
90 foreign import ccall "ErrorHdrHook" errorHdrHook :: Ptr ()
93 foreign import ccall unsafe "writeErrString__"
94 writeErrString :: Ptr () -> CString -> Int -> IO ()
96 -- SUP: Are the hooks allowed to re-enter Haskell land? If so, remove
98 foreign import ccall unsafe "stackOverflow"
99 callStackOverflowHook :: IO ()
101 foreign import ccall unsafe "stg_exit"
102 stg_exit :: Int -> IO ()
105 exit r = unsafeCoerce# (stg_exit r)
107 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
108 -- re-enter Haskell land through finalizers.
109 foreign import ccall "shutdownHaskellAndExit"
110 shutdownHaskellAndExit :: Int -> IO ()
112 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
113 -- compiler doesn't let us declare that as the result type of a foreign export.
114 safe_exit :: Int -> IO a
115 safe_exit r = unsafeCoerce# (shutdownHaskellAndExit r)