1 -- -----------------------------------------------------------------------------
2 -- $Id: TopHandler.lhs,v 1.4 2002/02/05 17:32:27 simonmar Exp $
4 -- (c) The University of Glasgow, 2001
8 -- 'Top-level' IO actions want to catch exceptions (e.g., forkIO and
9 -- GHC.Main.mainIO) and report them - topHandler is the exception
10 -- handler they should use for this:
12 -- make sure we handle errors while reporting the error!
13 -- (e.g. evaluating the string passed to 'error' might generate
14 -- another error, etc.)
16 -- These functions can't go in GHC.Main, because GHC.Main isn't
17 -- included in HSstd.o (because GHC.Main depends on Main, which
18 -- doesn't exist yet...).
21 module GHC.TopHandler (
22 topHandler, reportStackOverflow, reportError
29 import Foreign.C.String
34 topHandler :: Exception -> IO ()
35 topHandler err = catchException (real_handler err) topHandler
37 real_handler :: Exception -> IO ()
40 AsyncException StackOverflow -> reportStackOverflow True
42 -- only the main thread gets ExitException exceptions
43 ExitException ExitSuccess -> shutdownHaskellAndExit 0
44 ExitException (ExitFailure n) -> shutdownHaskellAndExit n
46 Deadlock -> reportError True
47 "no threads to run: infinite loop or deadlock?"
49 ErrorCall s -> reportError True s
50 other -> reportError True (showsPrec 0 other "\n")
52 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
53 -- re-enter Haskell land through finalizers.
54 foreign import ccall "shutdownHaskellAndExit"
55 shutdownHaskellAndExit :: Int -> IO ()
57 reportStackOverflow :: Bool -> IO ()
58 reportStackOverflow bombOut = do
59 (hFlush stdout) `catchException` (\ _ -> return ())
66 reportError :: Bool -> String -> IO ()
67 reportError bombOut str = do
68 (hFlush stdout) `catchException` (\ _ -> return ())
69 withCStringLen str $ \(cstr,len) -> do
70 writeErrString errorHdrHook cstr len
76 foreign label "ErrorHdrHook" errorHdrHook :: Ptr ()
78 foreign import "ErrorHdrHook" errorHdrHook :: Ptr ()
81 foreign import ccall "writeErrString__" unsafe
82 writeErrString :: Ptr () -> CString -> Int -> IO ()
84 -- SUP: Are the hooks allowed to re-enter Haskell land? If so, remove
86 foreign import ccall "stackOverflow" unsafe
87 callStackOverflowHook :: IO ()
89 foreign import ccall "stg_exit" unsafe
90 stg_exit :: Int -> IO ()