1 -----------------------------------------------------------------------------
3 -- Monadery code used in InteractiveUI
5 -- (c) The GHC Team 2005-2006
7 -----------------------------------------------------------------------------
11 #include "HsVersions.h"
14 import Outputable hiding (printForUser, printForUserPartWay)
15 import qualified Outputable
16 import Panic hiding (showException)
28 import Control.Exception as Exception
31 import Data.Int ( Int64 )
36 import System.Directory
37 import System.Environment
39 import Control.Monad as Monad
42 -----------------------------------------------------------------------------
45 type Command = (String, String -> GHCi Bool, Maybe String, String -> IO [String])
47 data GHCiState = GHCiState
54 session :: GHC.Session,
55 options :: [GHCiOption],
56 prelude :: GHC.Module,
58 breaks :: ![(Int, BreakLocation)],
59 tickarrays :: ModuleEnv TickArray,
60 -- tickarrays caches the TickArray for loaded modules,
61 -- so that we don't rebuild it each time the user sets
63 -- ":" at the GHCi prompt repeats the last command, so we
65 last_command :: Maybe Command,
67 remembered_ctx :: [(CtxtCmd, [String], [String])],
68 -- we remember the :module commands between :loads, so that
69 -- on a :reload we can replay them. See bugs #2049,
70 -- #1873, #1360. Previously we tried to remember modules that
71 -- were supposed to be in the context but currently had errors,
72 -- but this was complicated. Just replaying the :module commands
73 -- seems to be the right thing.
74 virtual_path :: FilePath,
75 ghc_e :: Bool -- True if this is 'ghc -e' (or runghc)
83 type TickArray = Array Int [(BreakIndex,SrcSpan)]
86 = ShowTiming -- show time/allocs after evaluation
87 | ShowType -- show the type of expressions
88 | RevertCAFs -- revert CAFs after every evaluation
93 { breakModule :: !GHC.Module
94 , breakLoc :: !SrcSpan
95 , breakTick :: {-# UNPACK #-} !Int
96 , onBreakCmd :: String
99 instance Eq BreakLocation where
100 loc1 == loc2 = breakModule loc1 == breakModule loc2 &&
101 breakTick loc1 == breakTick loc2
103 prettyLocations :: [(Int, BreakLocation)] -> SDoc
104 prettyLocations [] = text "No active breakpoints."
105 prettyLocations locs = vcat $ map (\(i, loc) -> brackets (int i) <+> ppr loc) $ reverse $ locs
107 instance Outputable BreakLocation where
108 ppr loc = (ppr $ breakModule loc) <+> ppr (breakLoc loc) <+>
109 if null (onBreakCmd loc)
111 else doubleQuotes (text (onBreakCmd loc))
113 recordBreak :: BreakLocation -> GHCi (Bool{- was already present -}, Int)
114 recordBreak brkLoc = do
116 let oldActiveBreaks = breaks st
117 -- don't store the same break point twice
118 case [ nm | (nm, loc) <- oldActiveBreaks, loc == brkLoc ] of
119 (nm:_) -> return (True, nm)
121 let oldCounter = break_ctr st
122 newCounter = oldCounter + 1
123 setGHCiState $ st { break_ctr = newCounter,
124 breaks = (oldCounter, brkLoc) : oldActiveBreaks
126 return (False, oldCounter)
128 newtype GHCi a = GHCi { unGHCi :: IORef GHCiState -> IO a }
130 startGHCi :: GHCi a -> GHCiState -> IO a
131 startGHCi g state = do ref <- newIORef state; unGHCi g ref
133 instance Monad GHCi where
134 (GHCi m) >>= k = GHCi $ \s -> m s >>= \a -> unGHCi (k a) s
135 return a = GHCi $ \_ -> return a
137 instance Functor GHCi where
138 fmap f m = m >>= return . f
140 ghciHandleDyn :: Typeable t => (t -> GHCi a) -> GHCi a -> GHCi a
141 ghciHandleDyn h (GHCi m) = GHCi $ \s ->
142 Exception.catchDyn (m s) (\e -> unGHCi (h e) s)
144 getGHCiState :: GHCi GHCiState
145 getGHCiState = GHCi $ \r -> readIORef r
146 setGHCiState :: GHCiState -> GHCi ()
147 setGHCiState s = GHCi $ \r -> writeIORef r s
149 -- for convenience...
150 getSession :: GHCi Session
151 getSession = getGHCiState >>= return . session
152 getPrelude :: GHCi Module
153 getPrelude = getGHCiState >>= return . prelude
155 GLOBAL_VAR(saved_sess, no_saved_sess, GHC.Session)
157 no_saved_sess :: Session
158 no_saved_sess = error "no saved_ses"
160 saveSession :: GHCi ()
161 saveSession = getSession >>= io . writeIORef saved_sess
163 splatSavedSession :: GHCi ()
164 splatSavedSession = io (writeIORef saved_sess no_saved_sess)
166 restoreSession :: IO Session
167 restoreSession = readIORef saved_sess
169 getDynFlags :: GHCi DynFlags
172 io (GHC.getSessionDynFlags s)
173 setDynFlags :: DynFlags -> GHCi [PackageId]
174 setDynFlags dflags = do
176 io (GHC.setSessionDynFlags s dflags)
178 isOptionSet :: GHCiOption -> GHCi Bool
180 = do st <- getGHCiState
181 return (opt `elem` options st)
183 setOption :: GHCiOption -> GHCi ()
185 = do st <- getGHCiState
186 setGHCiState (st{ options = opt : filter (/= opt) (options st) })
188 unsetOption :: GHCiOption -> GHCi ()
190 = do st <- getGHCiState
191 setGHCiState (st{ options = filter (/= opt) (options st) })
194 io m = GHCi (\_ -> m)
196 printForUser :: SDoc -> GHCi ()
197 printForUser doc = do
198 session <- getSession
199 unqual <- io (GHC.getPrintUnqual session)
200 io $ Outputable.printForUser stdout unqual doc
202 printForUserPartWay :: SDoc -> GHCi ()
203 printForUserPartWay doc = do
204 session <- getSession
205 unqual <- io (GHC.getPrintUnqual session)
206 io $ Outputable.printForUserPartWay stdout opt_PprUserLength unqual doc
208 withVirtualPath :: GHCi a -> GHCi a
209 withVirtualPath m = do
210 ghci_wd <- io getCurrentDirectory -- Store the cwd of GHCi
212 io$ setCurrentDirectory (virtual_path st)
213 result <- m -- Evaluate in the virtual wd..
214 vwd <- io getCurrentDirectory
215 setGHCiState (st{ virtual_path = vwd}) -- Update the virtual path
216 io$ setCurrentDirectory ghci_wd -- ..and restore GHCi wd
219 runStmt :: String -> GHC.SingleStep -> GHCi GHC.RunResult
220 runStmt expr step = withVirtualPath$ do
221 session <- getSession
223 io$ withProgName (progname st) $ withArgs (args st) $
224 GHC.runStmt session expr step
226 resume :: GHC.SingleStep -> GHCi GHC.RunResult
227 resume step = withVirtualPath$ do
228 session <- getSession
229 io$ GHC.resume session step
232 -- --------------------------------------------------------------------------
233 -- timing & statistics
235 timeIt :: GHCi a -> GHCi a
237 = do b <- isOptionSet ShowTiming
240 else do allocs1 <- io $ getAllocations
241 time1 <- io $ getCPUTime
243 allocs2 <- io $ getAllocations
244 time2 <- io $ getCPUTime
245 io $ printTimes (fromIntegral (allocs2 - allocs1))
249 foreign import ccall unsafe "getAllocations" getAllocations :: IO Int64
250 -- defined in ghc/rts/Stats.c
252 printTimes :: Integer -> Integer -> IO ()
253 printTimes allocs psecs
254 = do let secs = (fromIntegral psecs / (10^12)) :: Float
255 secs_str = showFFloat (Just 2) secs
257 parens (text (secs_str "") <+> text "secs" <> comma <+>
258 text (show allocs) <+> text "bytes")))
260 -----------------------------------------------------------------------------
263 revertCAFs :: GHCi ()
267 when (not (ghc_e s)) $ io turnOffBuffering
268 -- Have to turn off buffering again, because we just
269 -- reverted stdout, stderr & stdin to their defaults.
271 foreign import ccall "revertCAFs" rts_revertCAFs :: IO ()
272 -- Make it "safe", just in case
274 -----------------------------------------------------------------------------
275 -- To flush buffers for the *interpreted* computation we need
276 -- to refer to *its* stdout/stderr handles
278 GLOBAL_VAR(stdin_ptr, error "no stdin_ptr", Ptr ())
279 GLOBAL_VAR(stdout_ptr, error "no stdout_ptr", Ptr ())
280 GLOBAL_VAR(stderr_ptr, error "no stderr_ptr", Ptr ())
282 -- After various attempts, I believe this is the least bad way to do
283 -- what we want. We know look up the address of the static stdin,
284 -- stdout, and stderr closures in the loaded base package, and each
285 -- time we need to refer to them we cast the pointer to a Handle.
286 -- This avoids any problems with the CAF having been reverted, because
287 -- we'll always get the current value.
289 -- The previous attempt that didn't work was to compile an expression
290 -- like "hSetBuffering stdout NoBuffering" into an expression of type
291 -- IO () and run this expression each time we needed it, but the
292 -- problem is that evaluating the expression might cache the contents
293 -- of the Handle rather than referring to it from its static address
294 -- each time. There's no safe workaround for this.
296 initInterpBuffering :: GHC.Session -> IO ()
297 initInterpBuffering session
298 = do -- make sure these are linked
299 dflags <- GHC.getSessionDynFlags session
302 -- ToDo: we should really look up these names properly, but
303 -- it's a fiddle and not all the bits are exposed via the GHC
305 mb_stdin_ptr <- ObjLink.lookupSymbol "base_GHCziHandle_stdin_closure"
306 mb_stdout_ptr <- ObjLink.lookupSymbol "base_GHCziHandle_stdout_closure"
307 mb_stderr_ptr <- ObjLink.lookupSymbol "base_GHCziHandle_stderr_closure"
309 let f ref (Just ptr) = writeIORef ref ptr
310 f _ Nothing = panic "interactiveUI:setBuffering2"
311 zipWithM f [stdin_ptr,stdout_ptr,stderr_ptr]
312 [mb_stdin_ptr,mb_stdout_ptr,mb_stderr_ptr]
315 flushInterpBuffers :: GHCi ()
317 = io $ do getHandle stdout_ptr >>= hFlush
318 getHandle stderr_ptr >>= hFlush
320 turnOffBuffering :: IO ()
322 = do hdls <- mapM getHandle [stdin_ptr,stdout_ptr,stderr_ptr]
323 mapM_ (\h -> hSetBuffering h NoBuffering) hdls
325 getHandle :: IORef (Ptr ()) -> IO Handle
327 (Ptr addr) <- readIORef ref
328 case addrToHValue# addr of (# hval #) -> return (unsafeCoerce# hval)