|
| 1 | +-- | Lower-level interface to evaluating things in the (possibly remote) debuggee process |
| 2 | +module GHC.Debugger.Runtime.Eval where |
| 3 | + |
| 4 | +import GHC |
| 5 | +import GHC.Driver.Env |
| 6 | +import GHC.Runtime.Interpreter as Interp |
| 7 | +import Control.Monad.Reader |
| 8 | +import GHC.Driver.Config |
| 9 | +import GHCi.RemoteTypes |
| 10 | + |
| 11 | +import GHC.Debugger.Monad |
| 12 | + |
| 13 | +-------------------------------------------------------------------------------- |
| 14 | +-- * Evaluation on Foreign Heap Values |
| 15 | +-------------------------------------------------------------------------------- |
| 16 | + |
| 17 | +-- | Evaluate `f x` for any @f :: a -> b@ and any @x :: a@. |
| 18 | +-- The result is the foreign reference to a heap value of type @b@ |
| 19 | +evalApplication :: ForeignHValue -> ForeignHValue -> Debugger ForeignHValue |
| 20 | +evalApplication fref aref = do |
| 21 | + hsc_env <- getSession |
| 22 | + mk_list_fv <- compileExprRemote "(pure @IO . (:[])) :: a -> IO [a]" |
| 23 | + |
| 24 | + let eval_opts = initEvalOpts (hsc_dflags hsc_env) EvalStepNone |
| 25 | + interp = hscInterp hsc_env |
| 26 | + |
| 27 | + liftIO (evalStmt interp eval_opts $ (EvalThis mk_list_fv) `EvalApp` ((EvalThis fref) `EvalApp` (EvalThis aref))) |
| 28 | + >>= liftIO . handleSingStatus |
| 29 | + |
| 30 | +-- | Evaluate `f x` for any @f :: a -> IO b@ and any @x :: a@. |
| 31 | +-- The result is the foreign reference to a heap value of type @b@ (the IO action is executed) |
| 32 | +evalApplicationIO :: ForeignHValue -> ForeignHValue -> Debugger ForeignHValue |
| 33 | +evalApplicationIO fref aref = do |
| 34 | + hsc_env <- getSession |
| 35 | + fmap_list_fv <- compileExprRemote "(fmap (:[])) :: IO a -> IO [a]" |
| 36 | + |
| 37 | + let eval_opts = initEvalOpts (hsc_dflags hsc_env) EvalStepNone |
| 38 | + interp = hscInterp hsc_env |
| 39 | + |
| 40 | + liftIO (evalStmt interp eval_opts $ (EvalThis fmap_list_fv) `EvalApp` ((EvalThis fref) `EvalApp` (EvalThis aref))) |
| 41 | + >>= liftIO . handleSingStatus |
| 42 | + |
| 43 | +-- | Handle the 'EvalStatus_' of an evaluation using 'EvalStepNone' which returns a single value |
| 44 | +handleSingStatus :: MonadFail m => EvalStatus_ [ForeignHValue] [HValueRef] -> m ForeignHValue |
| 45 | +handleSingStatus status = |
| 46 | + case status of |
| 47 | + EvalComplete _ (EvalSuccess [res]) -> pure res |
| 48 | + EvalComplete _ (EvalSuccess []) -> |
| 49 | + fail "evaluation did not bind any values" |
| 50 | + EvalComplete _ (EvalSuccess (_:_:_)) -> |
| 51 | + fail "evaluation produced more than one value" |
| 52 | + EvalComplete _ (EvalException e) -> |
| 53 | + fail ("evaluation raised an exception: " ++ show e) |
| 54 | + EvalBreak {} -> |
| 55 | + --TODO: Could we accidentally hit this if we set a breakpoint regardless of whether EvalStep=None? perhaps. |
| 56 | + fail "evaluation unexpectedly hit a breakpoint" |
| 57 | + |
| 58 | +-- | Handle the 'EvalStatus_' of an evaluation using 'EvalStepNone' which returns a list of values |
| 59 | +handleMultiStatus :: MonadFail m => EvalStatus_ [ForeignHValue] [HValueRef] -> m [ForeignHValue] |
| 60 | +handleMultiStatus status = |
| 61 | + case status of |
| 62 | + EvalComplete _ (EvalSuccess res) -> pure res |
| 63 | + EvalComplete _ (EvalException e) -> |
| 64 | + fail ("evaluation raised an exception: " ++ show e) |
| 65 | + -- TODO?: throwIO (fromSerializableException e) |
| 66 | + EvalBreak {} -> |
| 67 | + fail "evaluation unexpectedly hit a breakpoint" |
0 commit comments