Panic.lhs 5.45 KB
Newer Older
1
%
2
% (c) The GRASP Project, Glasgow University, 1992-2000
3
4
5
6
7
8
9
10
11
%
\section{Panic error messages}

Defines basic funtions for printing error messages.

It's hard to put these functions anywhere else without causing
some unnecessary loops in the module dependency graph.

\begin{code}
12
13
14
module Panic  
   ( 
     GhcException(..), ghcError, progName, 
sof's avatar
sof committed
15
     panic, panic#, assertPanic, trace,
16
     showException, showGhcException, tryMost,
17
     installSignalHandlers,
18

19
     catchJust, tryJust, ioErrors, throwTo,
20
   ) where
21

22
#include "HsVersions.h"
sof's avatar
sof committed
23
#include "config.h"
24

25
import Config
26
import FastTypes
27

28
#ifndef mingw32_HOST_OS
sof's avatar
sof committed
29
# if __GLASGOW_HASKELL__ > 504
30
import System.Posix.Signals
sof's avatar
sof committed
31
# else
32
import Posix		( Handler(Catch), installHandler, sigINT, sigQUIT )
33
# endif /* GHC > 504 */
34
35
36
37
38
39
40

import CONCURRENT	( myThreadId )

# if __GLASGOW_HASKELL__ < 500
import EXCEPTION        ( raiseInThread )
# else
import EXCEPTION	( throwTo )
41
# endif /* GHC < 500 */
42
#endif /* mingw32_HOST_OS */
43

44
45
46
47
#if __GLASGOW_HASKELL__ > 408
import EXCEPTION	( catchJust, tryJust, ioErrors )
#endif

48
import DYNAMIC
49
import qualified EXCEPTION as Exception
50
51
import TRACE		( trace )
import UNSAFE_IO	( unsafePerformIO )
52
53

import System
54
55
\end{code}

56
57
58
59
GHC's own exception type.

\begin{code}
ghcError :: GhcException -> a
60
ghcError e = Exception.throwDyn e
61

62
63
64
65
66
67
68
69
70
-- error messages all take the form
--
--	<location>: <error>
--
-- If the location is on the command line, or in GHC itself, then 
-- <location>="ghc".  All of the error types below correspond to 
-- a <location> of "ghc", except for ProgramError (where the string is
-- assumed to contain a location already, so we don't print one).

71
data GhcException
sof's avatar
sof committed
72
73
  = PhaseFailed String		-- name of phase 
  		ExitCode	-- an external phase (eg. cpp) failed
74
  | Interrupted			-- someone pressed ^C
75
  | UsageError String		-- prints the short usage msg after the error
76
  | CmdLineError String		-- cmdline prob, but doesn't print usage
77
  | Panic String		-- the `impossible' happened
78
79
  | InstallationError String	-- an installation problem
  | ProgramError String		-- error in the user's code, probably
80
81
82
83
84
85
86
  deriving Eq

progName = unsafePerformIO (getProgName)
{-# NOINLINE progName #-}

short_usage = "Usage: For basic information, try the `--help' option."
   
87
showException :: Exception.Exception -> String
88
-- Show expected dynamic exceptions specially
89
90
91
showException (Exception.DynException d) | Just e <- fromDynamic d 
					 = show (e::GhcException)
showException other_exn	       	 	 = show other_exn
92

93
instance Show GhcException where
94
95
  showsPrec _ e@(ProgramError _) = showGhcException e
  showsPrec _ e = showString progName . showString ": " . showGhcException e
96

97
showGhcException (UsageError str)
98
   = showString str . showChar '\n' . showString short_usage
99
showGhcException (PhaseFailed phase code)
sof's avatar
sof committed
100
101
102
103
104
105
106
107
   = showString "phase `" . showString phase . 
     showString "' failed (exitcode = " . shows int_code . 
     showString ")"
  where
    int_code = 
      case code of
        ExitSuccess   -> (0::Int)
	ExitFailure x -> x
108
109
110
111
112
113
114
showGhcException (CmdLineError str)
   = showString str
showGhcException (ProgramError str)
   = showString str
showGhcException (InstallationError str)
   = showString str
showGhcException (Interrupted)
115
   = showString "interrupted"
116
showGhcException (Panic s)
117
118
   = showString ("panic! (the `impossible' happened, GHC version "
		 ++ cProjectVersion ++ "):\n\t"
119
120
	         ++ s ++ "\n\n"
	         ++ "Please report it as a compiler bug "
121
122
	         ++ "to glasgow-haskell-bugs@haskell.org,\n"
		 ++ "or http://sourceforge.net/projects/ghc/.\n\n")
123
124
125
126
127
128
129
130
131

ghcExceptionTc = mkTyCon "GhcException"
{-# NOINLINE ghcExceptionTc #-}
instance Typeable GhcException where
  typeOf _ = mkAppTy ghcExceptionTc []
\end{code}

Panics and asserts.

132
133
\begin{code}
panic :: String -> a
134
panic x = Exception.throwDyn (Panic x)
135
136
137
138
139

-- #-versions because panic can't return an unboxed int, and that's
-- what TAG_ is with GHC at the moment.  Ugh. (Simon)
-- No, man -- Too Beautiful! (Will)

140
141
panic# :: String -> FastInt
panic# s = case (panic s) of () -> _ILIT 0
142
143

assertPanic :: String -> Int -> a
144
assertPanic file line = 
145
  Exception.throw (Exception.AssertionFailed 
146
           ("ASSERT failed! file " ++ file ++ ", line " ++ show line))
147
\end{code}
148
149
150
151
152
153

\begin{code}
-- | tryMost is like try, but passes through Interrupted and Panic
-- exceptions.  Used when we want soft failures when reading interface
-- files, for example.

154
tryMost :: IO a -> IO (Either Exception.Exception a)
155
tryMost action = do r <- myTry action; filter r
156
  where
157
   filter (Left e@(Exception.DynException d))
158
159
	    | Just ghc_ex <- fromDynamic d
		= case ghc_ex of
160
161
		    Interrupted -> Exception.throw e
		    Panic _     -> Exception.throw e
162
163
164
165
166
		    _other      -> return (Left e)
   filter other 
     = return other

#if __GLASGOW_HASKELL__ <= 408
167
myTry = Exception.tryAllIO
168
#else
169
myTry = Exception.try
170
171
172
173
174
175
176
#endif
\end{code}	

Compatibility stuff:

\begin{code}
#if __GLASGOW_HASKELL__ <= 408
sof's avatar
sof committed
177
catchJust = Exception.catchIO
178
tryJust   = Exception.tryIO
sof's avatar
sof committed
179
180
ioErrors  = Exception.justIoErrors
throwTo   = Exception.raiseInThread
181
182
#endif
\end{code}
183
184
185
186
187
188
189
190

Standard signal handlers for catching ^C, which just throw an
exception in the main thread.  NOTE: must be called from the main
thread.

\begin{code}
installSignalHandlers :: IO ()
installSignalHandlers = do
191
#ifndef mingw32_HOST_OS
192
193
194
195
196
197
198
199
  main_thread <- myThreadId
  let sig_handler = Catch (throwTo main_thread 
				(Exception.DynException (toDyn Interrupted)))
  installHandler sigQUIT sig_handler Nothing 
  installHandler sigINT  sig_handler Nothing
#endif
  return ()
\end{code}