{-# LANGUAGE CPP, NamedFieldPuns, TupleSections, LambdaCase,
   DuplicateRecordFields, RecordWildCards, TupleSections, ViewPatterns,
   TypeApplications, ScopedTypeVariables, BangPatterns #-}
module GHC.Debugger where

import GHC.Debugger.Breakpoint
import GHC.Debugger.Run
import GHC.Debugger.Stopped
import GHC.Debugger.Stopped.Exception (getExceptionInfo)
import GHC.Debugger.Monad
import GHC.Debugger.Interface.Messages

--------------------------------------------------------------------------------
-- * Executing commands
--------------------------------------------------------------------------------

-- | Execute the given debugger command in the current 'Debugger' session
execute :: Command -> Debugger Response
execute :: Command -> Debugger Response
execute = \case
  Command
ClearFunctionBreakpoints -> Response
DidClearBreakpoints Response -> Debugger () -> Debugger Response
forall a b. a -> Debugger b -> Debugger a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Maybe AbsFilePath -> Debugger ()
clearBreakpoints Maybe AbsFilePath
forall a. Maybe a
Nothing
  ClearModBreakpoints AbsFilePath
fp -> Response
DidClearBreakpoints Response -> Debugger () -> Debugger Response
forall a b. a -> Debugger b -> Debugger a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Maybe AbsFilePath -> Debugger ()
clearBreakpoints (AbsFilePath -> Maybe AbsFilePath
forall a. a -> Maybe a
Just AbsFilePath
fp)
  SetBreakpoint{Breakpoint
brk :: Breakpoint
brk :: Command -> Breakpoint
brk, Maybe Int
hitCount :: Maybe Int
hitCount :: Command -> Maybe Int
hitCount, Maybe String
condition :: Maybe String
condition :: Command -> Maybe String
condition, Maybe String
logMessage :: Maybe String
logMessage :: Command -> Maybe String
logMessage} ->
    BreakFound -> Response
DidSetBreakpoint (BreakFound -> Response)
-> Debugger BreakFound -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Breakpoint
-> BreakpointStatus -> BreakpointAction -> Debugger BreakFound
setBreakpoint Breakpoint
brk (Maybe Int -> Maybe String -> BreakpointStatus
condBreakEnableStatus Maybe Int
hitCount Maybe String
condition) (BreakpointAction
-> (String -> BreakpointAction) -> Maybe String -> BreakpointAction
forall b a. b -> (a -> b) -> Maybe a -> b
maybe BreakpointAction
BreakpointStop (String -> BreakpointAction
BreakpointLogAndResume (String -> BreakpointAction)
-> (String -> String) -> String -> BreakpointAction
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
logMessageExpression) Maybe String
logMessage)
  DelBreakpoint Breakpoint
bp -> BreakFound -> Response
DidRemoveBreakpoint (BreakFound -> Response)
-> Debugger BreakFound -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Breakpoint
-> BreakpointStatus -> BreakpointAction -> Debugger BreakFound
setBreakpoint Breakpoint
bp BreakpointStatus
BreakpointDisabled BreakpointAction
BreakpointStop
  GetBreakpointsAt Breakpoint
bp -> Maybe SourceSpan -> Response
DidGetBreakpoints (Maybe SourceSpan -> Response)
-> Debugger (Maybe SourceSpan) -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Breakpoint -> Debugger (Maybe SourceSpan)
getBreakpointsAt Breakpoint
bp
  Command
GetThreads -> [DebuggeeThread] -> Response
GotThreads ([DebuggeeThread] -> Response)
-> Debugger [DebuggeeThread] -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Debugger [DebuggeeThread]
getThreads
  GetStacktrace RemoteThreadId
i -> [DbgStackFrame] -> Response
GotStacktrace ([DbgStackFrame] -> Response)
-> Debugger [DbgStackFrame] -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RemoteThreadId -> Debugger [DbgStackFrame]
getStacktrace RemoteThreadId
i
  GetScopes RemoteThreadId
threadId Int
frameIx -> [ScopeInfo] -> Response
GotScopes ([ScopeInfo] -> Response)
-> Debugger [ScopeInfo] -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RemoteThreadId -> Int -> Debugger [ScopeInfo]
getScopes RemoteThreadId
threadId Int
frameIx
  GetVariables RemoteThreadId
threadId Int
frameIx VariableReference
varRef -> VariableResult -> Response
GotVariables (VariableResult -> Response)
-> Debugger VariableResult -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RemoteThreadId
-> Int -> VariableReference -> Debugger VariableResult
getVariables RemoteThreadId
threadId Int
frameIx VariableReference
varRef
  GetExceptionInfo RemoteThreadId
threadId -> ExceptionInfo -> Response
GotExceptionInfo (ExceptionInfo -> Response)
-> Debugger ExceptionInfo -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RemoteThreadId -> Debugger ExceptionInfo
getExceptionInfo RemoteThreadId
threadId
  DoEval String
exp_s -> EvalResult -> Response
DidEval (EvalResult -> Response)
-> Debugger EvalResult -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Debugger EvalResult
doEvalCommand String
exp_s
  Command
DoContinue -> EvalResult -> Response
DidContinue (EvalResult -> Response)
-> Debugger EvalResult -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Debugger EvalResult
doContinue
  Command
DoSingleStep -> EvalResult -> Response
DidStep (EvalResult -> Response)
-> Debugger EvalResult -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Debugger EvalResult
doSingleStep
  Command
DoStepOut -> EvalResult -> Response
DidStep (EvalResult -> Response)
-> Debugger EvalResult -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Debugger EvalResult
doStepOut
  Command
DoStepLocal -> EvalResult -> Response
DidStep (EvalResult -> Response)
-> Debugger EvalResult -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Debugger EvalResult
doLocalStep
  DebugExecution { EntryPoint
entryPoint :: EntryPoint
entryPoint :: Command -> EntryPoint
entryPoint, AbsFilePath
entryFile :: AbsFilePath
entryFile :: Command -> AbsFilePath
entryFile, [String]
runArgs :: [String]
runArgs :: Command -> [String]
runArgs } -> EvalResult -> Response
DidExec (EvalResult -> Response)
-> Debugger EvalResult -> Debugger Response
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AbsFilePath -> EntryPoint -> [String] -> Debugger EvalResult
debugExecution AbsFilePath
entryFile EntryPoint
entryPoint [String]
runArgs