module GHC.Runtime.Interpreter
( module GHC.Runtime.Interpreter.Types
, evalStmt, EvalStatus_(..), EvalStatus, EvalResult(..), EvalExpr(..)
, resumeStmt
, abandonStmt
, evalIO
, evalString
, evalStringToIOString
, mallocData
, createBCOs
, addSptEntry
, mkCostCentres
, costCentreStackInfo
, newBreakArray
, storeBreakpoint
, breakpointStatus
, getBreakpointVar
, getClosure
, getModBreaks
, seqHValue
, interpreterDynamic
, interpreterProfiled
, initObjLinker
, lookupSymbol
, lookupClosure
, loadDLL
, loadArchive
, loadObj
, unloadObj
, addLibrarySearchPath
, removeLibrarySearchPath
, resolveObjs
, findSystemLibrary
, interpCmd, Message(..), withIServ, withIServ_
, hscInterp, stopInterp
, iservCall, readIServ, writeIServ
, purgeLookupSymbolCache
, freeHValueRefs
, mkFinalizedHValue
, wormhole, wormholeRef
, mkEvalOpts
, fromEvalResult
) where
import GHC.Prelude
import GHC.Driver.Ppr (showSDoc)
import GHC.Driver.Env
import GHC.Driver.Session
import GHC.Runtime.Interpreter.Types
import GHCi.Message
import GHCi.RemoteTypes
import GHCi.ResolvedBCO
import GHCi.BreakArray (BreakArray)
import GHC.Runtime.Eval.Types(BreakInfo(..))
import GHC.ByteCode.Types
import GHC.Linker.Types
import GHC.Data.Maybe
import GHC.Data.FastString
import GHC.Types.Unique
import GHC.Types.SrcLoc
import GHC.Types.Unique.FM
import GHC.Types.Basic
import GHC.Utils.Panic
import GHC.Utils.Exception as Ex
import GHC.Utils.Outputable(brackets, ppr)
import GHC.Utils.Fingerprint
import GHC.Utils.Misc
import GHC.Unit.Module
import GHC.Unit.Module.ModIface
import GHC.Unit.Home.ModInfo
#if defined(HAVE_INTERNAL_INTERPRETER)
import GHCi.Run
import GHC.Platform.Ways
#endif
import Control.Concurrent
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Catch as MC (mask, onException)
import Data.Binary
import Data.Binary.Put
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LB
import Data.Array ((!))
import Data.IORef
import Foreign hiding (void)
import qualified GHC.Exts.Heap as Heap
import GHC.Stack.CCS (CostCentre,CostCentreStack)
import System.Exit
import GHC.IO.Handle.Types (Handle)
#if defined(mingw32_HOST_OS)
import Foreign.C
import GHC.IO.Handle.FD (fdToHandle)
#else
import System.Posix as Posix
#endif
import System.Directory
import System.Process
import GHC.Conc (getNumProcessors, pseq, par)
interpCmd :: Binary a => Interp -> Message a -> IO a
interpCmd interp msg = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> run msg
#endif
ExternalInterp c i -> withIServ_ c i $ \iserv ->
uninterruptibleMask_ $
iservCall iserv msg
hscInterp :: HscEnv -> Interp
hscInterp hsc_env = case hsc_interp hsc_env of
Nothing -> throw (InstallationError "Couldn't find a target code interpreter. Try with -fexternal-interpreter")
Just i -> i
withIServ
:: (ExceptionMonad m)
=> IServConfig -> IServ -> (IServInstance -> m (IServInstance, a)) -> m a
withIServ conf (IServ mIServState) action =
MC.mask $ \restore -> do
state <- liftIO $ takeMVar mIServState
iserv <- case state of
IServPending ->
liftIO (spawnIServ conf)
`MC.onException` (liftIO $ putMVar mIServState state)
IServRunning inst -> return inst
let iserv' = iserv{ iservPendingFrees = [] }
(iserv'',a) <- (do
liftIO $ when (not (null (iservPendingFrees iserv))) $
iservCall iserv (FreeHValueRefs (iservPendingFrees iserv))
restore $ action iserv')
`MC.onException` (liftIO $ putMVar mIServState (IServRunning iserv'))
liftIO $ putMVar mIServState (IServRunning iserv'')
return a
withIServ_
:: (MonadIO m, ExceptionMonad m)
=> IServConfig -> IServ -> (IServInstance -> m a) -> m a
withIServ_ conf iserv action = withIServ conf iserv $ \inst ->
(inst,) <$> action inst
evalStmt
:: Interp
-> DynFlags
-> Bool
-> EvalExpr ForeignHValue
-> IO (EvalStatus_ [ForeignHValue] [HValueRef])
evalStmt interp dflags step foreign_expr = do
status <- withExpr foreign_expr $ \expr ->
interpCmd interp (EvalStmt (mkEvalOpts dflags step) expr)
handleEvalStatus interp status
where
withExpr :: EvalExpr ForeignHValue -> (EvalExpr HValueRef -> IO a) -> IO a
withExpr (EvalThis fhv) cont =
withForeignRef fhv $ \hvref -> cont (EvalThis hvref)
withExpr (EvalApp fl fr) cont =
withExpr fl $ \fl' ->
withExpr fr $ \fr' ->
cont (EvalApp fl' fr')
resumeStmt
:: Interp
-> DynFlags
-> Bool
-> ForeignRef (ResumeContext [HValueRef])
-> IO (EvalStatus_ [ForeignHValue] [HValueRef])
resumeStmt interp dflags step resume_ctxt = do
status <- withForeignRef resume_ctxt $ \rhv ->
interpCmd interp (ResumeStmt (mkEvalOpts dflags step) rhv)
handleEvalStatus interp status
abandonStmt :: Interp -> ForeignRef (ResumeContext [HValueRef]) -> IO ()
abandonStmt interp resume_ctxt =
withForeignRef resume_ctxt $ \rhv ->
interpCmd interp (AbandonStmt rhv)
handleEvalStatus
:: Interp
-> EvalStatus [HValueRef]
-> IO (EvalStatus_ [ForeignHValue] [HValueRef])
handleEvalStatus interp status =
case status of
EvalBreak a b c d e f -> return (EvalBreak a b c d e f)
EvalComplete alloc res ->
EvalComplete alloc <$> addFinalizer res
where
addFinalizer (EvalException e) = return (EvalException e)
addFinalizer (EvalSuccess rs) =
EvalSuccess <$> mapM (mkFinalizedHValue interp) rs
evalIO :: Interp -> ForeignHValue -> IO ()
evalIO interp fhv =
liftIO $ withForeignRef fhv $ \fhv ->
interpCmd interp (EvalIO fhv) >>= fromEvalResult
evalString :: Interp -> ForeignHValue -> IO String
evalString interp fhv =
liftIO $ withForeignRef fhv $ \fhv ->
interpCmd interp (EvalString fhv) >>= fromEvalResult
evalStringToIOString :: Interp -> ForeignHValue -> String -> IO String
evalStringToIOString interp fhv str =
liftIO $ withForeignRef fhv $ \fhv ->
interpCmd interp (EvalStringToString fhv str) >>= fromEvalResult
mallocData :: Interp -> ByteString -> IO (RemotePtr ())
mallocData interp bs = interpCmd interp (MallocData bs)
mkCostCentres :: Interp -> String -> [(String,String)] -> IO [RemotePtr CostCentre]
mkCostCentres interp mod ccs =
interpCmd interp (MkCostCentres mod ccs)
createBCOs :: Interp -> DynFlags -> [ResolvedBCO] -> IO [HValueRef]
createBCOs interp dflags rbcos = do
n_jobs <- case parMakeCount dflags of
Nothing -> liftIO getNumProcessors
Just n -> return n
if (n_jobs == 1)
then
interpCmd interp (CreateBCOs [runPut (put rbcos)])
else do
old_caps <- getNumCapabilities
if old_caps == n_jobs
then void $ evaluate puts
else bracket_ (setNumCapabilities n_jobs)
(setNumCapabilities old_caps)
(void $ evaluate puts)
interpCmd interp (CreateBCOs puts)
where
puts = parMap doChunk (chunkList 100 rbcos)
doChunk c = pseq (LB.length bs) bs
where bs = runPut (put c)
parMap _ [] = []
parMap f (x:xs) = fx `par` (fxs `pseq` (fx : fxs))
where fx = f x; fxs = parMap f xs
addSptEntry :: Interp -> Fingerprint -> ForeignHValue -> IO ()
addSptEntry interp fpr ref =
withForeignRef ref $ \val ->
interpCmd interp (AddSptEntry fpr val)
costCentreStackInfo :: Interp -> RemotePtr CostCentreStack -> IO [String]
costCentreStackInfo interp ccs =
interpCmd interp (CostCentreStackInfo ccs)
newBreakArray :: Interp -> Int -> IO (ForeignRef BreakArray)
newBreakArray interp size = do
breakArray <- interpCmd interp (NewBreakArray size)
mkFinalizedHValue interp breakArray
storeBreakpoint :: Interp -> ForeignRef BreakArray -> Int -> Int -> IO ()
storeBreakpoint interp ref ix cnt = do
withForeignRef ref $ \breakarray ->
interpCmd interp (SetupBreakpoint breakarray ix cnt)
breakpointStatus :: Interp -> ForeignRef BreakArray -> Int -> IO Bool
breakpointStatus interp ref ix =
withForeignRef ref $ \breakarray ->
interpCmd interp (BreakpointStatus breakarray ix)
getBreakpointVar :: Interp -> ForeignHValue -> Int -> IO (Maybe ForeignHValue)
getBreakpointVar interp ref ix =
withForeignRef ref $ \apStack -> do
mb <- interpCmd interp (GetBreakpointVar apStack ix)
mapM (mkFinalizedHValue interp) mb
getClosure :: Interp -> ForeignHValue -> IO (Heap.GenClosure ForeignHValue)
getClosure interp ref =
withForeignRef ref $ \hval -> do
mb <- interpCmd interp (GetClosure hval)
mapM (mkFinalizedHValue interp) mb
seqHValue :: Interp -> HscEnv -> ForeignHValue -> IO (EvalResult ())
seqHValue interp hsc_env ref =
withForeignRef ref $ \hval -> do
status <- interpCmd interp (Seq hval)
handleSeqHValueStatus interp hsc_env status
handleSeqHValueStatus :: Interp -> HscEnv -> EvalStatus () -> IO (EvalResult ())
handleSeqHValueStatus interp hsc_env eval_status =
case eval_status of
(EvalBreak is_exception _ ix mod_uniq resume_ctxt _) -> do
resume_ctxt_fhv <- liftIO $ mkFinalizedHValue interp resume_ctxt
let hmi = expectJust "handleRunStatus" $
lookupHptDirectly (hsc_HPT hsc_env)
(mkUniqueGrimily mod_uniq)
modl = mi_module (hm_iface hmi)
bp | is_exception = Nothing
| otherwise = Just (BreakInfo modl ix)
sdocBpLoc = brackets . ppr . getSeqBpSpan
putStrLn ("*** Ignoring breakpoint " ++
(showSDoc (hsc_dflags hsc_env) $ sdocBpLoc bp))
withForeignRef resume_ctxt_fhv $ \hval -> do
status <- interpCmd interp (ResumeSeq hval)
handleSeqHValueStatus interp hsc_env status
(EvalComplete _ r) -> return r
where
getSeqBpSpan :: Maybe BreakInfo -> SrcSpan
getSeqBpSpan (Just BreakInfo{..}) =
(modBreaks_locs (breaks breakInfo_module)) ! breakInfo_number
getSeqBpSpan Nothing = mkGeneralSrcSpan (fsLit "<unknown>")
breaks mod = getModBreaks $ expectJust "getSeqBpSpan" $
lookupHpt (hsc_HPT hsc_env) (moduleName mod)
initObjLinker :: Interp -> IO ()
initObjLinker interp = interpCmd interp InitLinker
lookupSymbol :: Interp -> FastString -> IO (Maybe (Ptr ()))
lookupSymbol interp str = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> fmap fromRemotePtr <$> run (LookupSymbol (unpackFS str))
#endif
ExternalInterp c i -> withIServ c i $ \iserv -> do
let cache = iservLookupSymbolCache iserv
case lookupUFM cache str of
Just p -> return (iserv, Just p)
Nothing -> do
m <- uninterruptibleMask_ $
iservCall iserv (LookupSymbol (unpackFS str))
case m of
Nothing -> return (iserv, Nothing)
Just r -> do
let p = fromRemotePtr r
cache' = addToUFM cache str p
iserv' = iserv {iservLookupSymbolCache = cache'}
return (iserv', Just p)
lookupClosure :: Interp -> String -> IO (Maybe HValueRef)
lookupClosure interp str =
interpCmd interp (LookupClosure str)
purgeLookupSymbolCache :: Interp -> IO ()
purgeLookupSymbolCache interp = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> pure ()
#endif
ExternalInterp _ (IServ mstate) ->
modifyMVar_ mstate $ \state -> pure $ case state of
IServPending -> state
IServRunning iserv -> IServRunning
(iserv { iservLookupSymbolCache = emptyUFM })
loadDLL :: Interp -> String -> IO (Maybe String)
loadDLL interp str = interpCmd interp (LoadDLL str)
loadArchive :: Interp -> String -> IO ()
loadArchive interp path = do
path' <- canonicalizePath path
interpCmd interp (LoadArchive path')
loadObj :: Interp -> String -> IO ()
loadObj interp path = do
path' <- canonicalizePath path
interpCmd interp (LoadObj path')
unloadObj :: Interp -> String -> IO ()
unloadObj interp path = do
path' <- canonicalizePath path
interpCmd interp (UnloadObj path')
addLibrarySearchPath :: Interp -> String -> IO (Ptr ())
addLibrarySearchPath interp str =
fromRemotePtr <$> interpCmd interp (AddLibrarySearchPath str)
removeLibrarySearchPath :: Interp -> Ptr () -> IO Bool
removeLibrarySearchPath interp p =
interpCmd interp (RemoveLibrarySearchPath (toRemotePtr p))
resolveObjs :: Interp -> IO SuccessFlag
resolveObjs interp = successIf <$> interpCmd interp ResolveObjs
findSystemLibrary :: Interp -> String -> IO (Maybe String)
findSystemLibrary interp str = interpCmd interp (FindSystemLibrary str)
iservCall :: Binary a => IServInstance -> Message a -> IO a
iservCall iserv msg =
remoteCall (iservPipe iserv) msg
`catch` \(e :: SomeException) -> handleIServFailure iserv e
readIServ :: IServInstance -> Get a -> IO a
readIServ iserv get =
readPipe (iservPipe iserv) get
`catch` \(e :: SomeException) -> handleIServFailure iserv e
writeIServ :: IServInstance -> Put -> IO ()
writeIServ iserv put =
writePipe (iservPipe iserv) put
`catch` \(e :: SomeException) -> handleIServFailure iserv e
handleIServFailure :: IServInstance -> SomeException -> IO a
handleIServFailure iserv e = do
let proc = iservProcess iserv
ex <- getProcessExitCode proc
case ex of
Just (ExitFailure n) ->
throwIO (InstallationError ("ghc-iserv terminated (" ++ show n ++ ")"))
_ -> do
terminateProcess proc
_ <- waitForProcess proc
throw e
spawnIServ :: IServConfig -> IO IServInstance
spawnIServ conf = do
iservConfTrace conf
let createProc = fromMaybe (\cp -> do { (_,_,_,ph) <- createProcess cp
; return ph })
(iservConfHook conf)
(ph, rh, wh) <- runWithPipes createProc (iservConfProgram conf)
(iservConfOpts conf)
lo_ref <- newIORef Nothing
return $ IServInstance
{ iservPipe = Pipe { pipeRead = rh, pipeWrite = wh, pipeLeftovers = lo_ref }
, iservProcess = ph
, iservLookupSymbolCache = emptyUFM
, iservPendingFrees = []
}
stopInterp :: Interp -> IO ()
stopInterp interp = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> pure ()
#endif
ExternalInterp _ (IServ mstate) ->
MC.mask $ \_restore -> modifyMVar_ mstate $ \state -> do
case state of
IServPending -> pure state
IServRunning i -> do
ex <- getProcessExitCode (iservProcess i)
if isJust ex
then pure ()
else iservCall i Shutdown
pure IServPending
runWithPipes :: (CreateProcess -> IO ProcessHandle)
-> FilePath -> [String] -> IO (ProcessHandle, Handle, Handle)
#if defined(mingw32_HOST_OS)
foreign import ccall "io.h _close"
c__close :: CInt -> IO CInt
foreign import ccall unsafe "io.h _get_osfhandle"
_get_osfhandle :: CInt -> IO CInt
runWithPipes createProc prog opts = do
(rfd1, wfd1) <- createPipeFd
(rfd2, wfd2) <- createPipeFd
wh_client <- _get_osfhandle wfd1
rh_client <- _get_osfhandle rfd2
let args = show wh_client : show rh_client : opts
ph <- createProc (proc prog args)
rh <- mkHandle rfd1
wh <- mkHandle wfd2
return (ph, rh, wh)
where mkHandle :: CInt -> IO Handle
mkHandle fd = (fdToHandle fd) `Ex.onException` (c__close fd)
#else
runWithPipes createProc prog opts = do
(rfd1, wfd1) <- Posix.createPipe
(rfd2, wfd2) <- Posix.createPipe
setFdOption rfd1 CloseOnExec True
setFdOption wfd2 CloseOnExec True
let args = show wfd1 : show rfd2 : opts
ph <- createProc (proc prog args)
closeFd wfd1
closeFd rfd2
rh <- fdToHandle rfd1
wh <- fdToHandle wfd2
return (ph, rh, wh)
#endif
mkFinalizedHValue :: Interp -> RemoteRef a -> IO (ForeignRef a)
mkFinalizedHValue interp rref = do
let hvref = toHValueRef rref
free <- case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> return (freeRemoteRef hvref)
#endif
ExternalInterp _ (IServ i) -> return $ modifyMVar_ i $ \state ->
case state of
IServPending {} -> pure state
IServRunning inst -> do
let !inst' = inst {iservPendingFrees = hvref:iservPendingFrees inst}
pure (IServRunning inst')
mkForeignRef rref free
freeHValueRefs :: Interp -> [HValueRef] -> IO ()
freeHValueRefs _ [] = return ()
freeHValueRefs interp refs = interpCmd interp (FreeHValueRefs refs)
wormhole :: Interp -> ForeignRef a -> IO a
wormhole interp r = wormholeRef interp (unsafeForeignRefToRemoteRef r)
wormholeRef :: Interp -> RemoteRef a -> IO a
wormholeRef interp _r = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> localRef _r
#endif
ExternalInterp {}
-> throwIO (InstallationError "this operation requires -fno-external-interpreter")
mkEvalOpts :: DynFlags -> Bool -> EvalOpts
mkEvalOpts dflags step =
EvalOpts
{ useSandboxThread = gopt Opt_GhciSandbox dflags
, singleStep = step
, breakOnException = gopt Opt_BreakOnException dflags
, breakOnError = gopt Opt_BreakOnError dflags }
fromEvalResult :: EvalResult a -> IO a
fromEvalResult (EvalException e) = throwIO (fromSerializableException e)
fromEvalResult (EvalSuccess a) = return a
getModBreaks :: HomeModInfo -> ModBreaks
getModBreaks hmi
| Just linkable <- hm_linkable hmi,
[BCOs cbc _] <- linkableUnlinked linkable
= fromMaybe emptyModBreaks (bc_breaks cbc)
| otherwise
= emptyModBreaks
interpreterProfiled :: Interp -> Bool
interpreterProfiled interp = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> hostIsProfiled
#endif
ExternalInterp c _ -> iservConfProfiled c
interpreterDynamic :: Interp -> Bool
interpreterDynamic interp = case interpInstance interp of
#if defined(HAVE_INTERNAL_INTERPRETER)
InternalInterp -> hostIsDynamic
#endif
ExternalInterp c _ -> iservConfDynamic c