{-# LANGUAGE MagicHash #-}

-- | A module providing various remote external variables which are available
-- by default in any session.
--
-- A good way to check whether some function or variable can be added here is
-- if you can successfully ask for the type of its fully qualified name on @ghci@
-- (e.g. @:t Data.List.singleton@)
--
-- Meant to be imported qualified @as Remote@:
-- @
-- import GHC.Debugger.Runtime.Eval.RemoteExpr (RemoteExpr)
-- import qualified GHC.Debugger.Runtime.Eval.RemoteExpr as Remote
-- import qualified GHC.Debugger.Runtime.Eval.RemoteExpr.Builtin as Remote
-- @
module GHC.Debugger.Runtime.Eval.RemoteExpr.Builtin where

import GHC.Exts
import Data.Word
import Foreign.C.String
import GHC.Conc.Sync
import GHC.InfoProv
import GHC.Unit.Module
import GHC.Stack.CloneStack
import GHC.Exts.Heap
import GHC.Exts.Heap.Closures
import GHC.Debugger.Runtime.Eval.RemoteExpr (RemoteExpr)
import qualified GHC.Debugger.Runtime.Eval.RemoteExpr as Remote
import GHC.Stack.Annotation.Experimental
import GHC.Types.SrcLoc
import GHC.Debugger.Session.Builtin (debuggerRuntimeInternalModName)
import qualified GHC.Exts.Heap.Closures as GHC
import GHCi.RemoteTypes (HValue)

pair :: Remote.RemoteExpr (a -> b -> (a,b))
pair :: forall a b. RemoteExpr (a -> b -> (a, b))
pair = String -> RemoteExpr (a -> b -> (a, b))
forall a. String -> RemoteExpr a
Remote.raw String
"(,)"

fst :: Remote.RemoteExpr ((a,b) -> a)
fst :: forall a b. RemoteExpr ((a, b) -> a)
fst = String -> RemoteExpr ((a, b) -> a)
forall a. String -> RemoteExpr a
Remote.raw String
"Prelude.fst"

snd :: Remote.RemoteExpr ((a,b) -> b)
snd :: forall a b. RemoteExpr ((a, b) -> b)
snd = String -> RemoteExpr ((a, b) -> b)
forall a. String -> RemoteExpr a
Remote.raw String
"Prelude.snd"

-- | Remote 'GHC.Stack.CloneStack.cloneThreadStack'
cloneThreadStack :: RemoteExpr ThreadId -> RemoteExpr (IO StackSnapshot)
cloneThreadStack :: RemoteExpr ThreadId -> RemoteExpr (IO StackSnapshot)
cloneThreadStack = RemoteExpr (ThreadId -> IO StackSnapshot)
-> RemoteExpr ThreadId -> RemoteExpr (IO StackSnapshot)
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (ThreadId -> IO StackSnapshot)
 -> RemoteExpr ThreadId -> RemoteExpr (IO StackSnapshot))
-> RemoteExpr (ThreadId -> IO StackSnapshot)
-> RemoteExpr ThreadId
-> RemoteExpr (IO StackSnapshot)
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String -> [String] -> RemoteExpr (ThreadId -> IO StackSnapshot)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Stack.CloneStack") String
"cloneThreadStack" []

-- | Remote 'GHC.Stack.CloneStack.decode'
decode :: RemoteExpr StackSnapshot -> RemoteExpr (IO [StackEntry])
decode :: RemoteExpr StackSnapshot -> RemoteExpr (IO [StackEntry])
decode = RemoteExpr (StackSnapshot -> IO [StackEntry])
-> RemoteExpr StackSnapshot -> RemoteExpr (IO [StackEntry])
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (StackSnapshot -> IO [StackEntry])
 -> RemoteExpr StackSnapshot -> RemoteExpr (IO [StackEntry]))
-> RemoteExpr (StackSnapshot -> IO [StackEntry])
-> RemoteExpr StackSnapshot
-> RemoteExpr (IO [StackEntry])
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String
-> [String]
-> RemoteExpr (StackSnapshot -> IO [StackEntry])
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Stack.CloneStack") String
"decode" []

-- | Remote 'GHC.Exts.Stack.decodeStack'
decodeStack :: RemoteExpr StackSnapshot -> RemoteExpr (IO StgStackClosure)
decodeStack :: RemoteExpr StackSnapshot -> RemoteExpr (IO StgStackClosure)
decodeStack = RemoteExpr (StackSnapshot -> IO StgStackClosure)
-> RemoteExpr StackSnapshot -> RemoteExpr (IO StgStackClosure)
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (StackSnapshot -> IO StgStackClosure)
 -> RemoteExpr StackSnapshot -> RemoteExpr (IO StgStackClosure))
-> RemoteExpr (StackSnapshot -> IO StgStackClosure)
-> RemoteExpr StackSnapshot
-> RemoteExpr (IO StgStackClosure)
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String
-> [String]
-> RemoteExpr (StackSnapshot -> IO StgStackClosure)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Exts.Stack") String
"decodeStack" []

-- | Remote 'GHC.Exts.Heap.Closures.ssc_stack'
ssc_stack :: RemoteExpr StgStackClosure -> RemoteExpr [StackFrame]
ssc_stack :: RemoteExpr StgStackClosure -> RemoteExpr [StackFrame]
ssc_stack = RemoteExpr (StgStackClosure -> [StackFrame])
-> RemoteExpr StgStackClosure -> RemoteExpr [StackFrame]
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (StgStackClosure -> [StackFrame])
 -> RemoteExpr StgStackClosure -> RemoteExpr [StackFrame])
-> RemoteExpr (StgStackClosure -> [StackFrame])
-> RemoteExpr StgStackClosure
-> RemoteExpr [StackFrame]
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String
-> [String]
-> RemoteExpr (StgStackClosure -> [StackFrame])
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Exts.Heap.Closures") String
"ssc_stack" []

-- | Remote 'GHC.Internal.Stack.Decode.decodeStackWithIpe"
decodeStackWithIpe :: RemoteExpr StackSnapshot -> RemoteExpr (IO [(StackFrame, Maybe InfoProv)])
decodeStackWithIpe :: RemoteExpr StackSnapshot
-> RemoteExpr (IO [(StackFrame, Maybe InfoProv)])
decodeStackWithIpe = RemoteExpr (StackSnapshot -> IO [(StackFrame, Maybe InfoProv)])
-> RemoteExpr StackSnapshot
-> RemoteExpr (IO [(StackFrame, Maybe InfoProv)])
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (StackSnapshot -> IO [(StackFrame, Maybe InfoProv)])
 -> RemoteExpr StackSnapshot
 -> RemoteExpr (IO [(StackFrame, Maybe InfoProv)]))
-> RemoteExpr (StackSnapshot -> IO [(StackFrame, Maybe InfoProv)])
-> RemoteExpr StackSnapshot
-> RemoteExpr (IO [(StackFrame, Maybe InfoProv)])
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String
-> [String]
-> RemoteExpr (StackSnapshot -> IO [(StackFrame, Maybe InfoProv)])
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Internal.Stack.Decode") String
"decodeStackWithIpe" []

-- | Remote 'GHC.Exts.Heap.getClosureData'
getClosureData :: RemoteExpr StgStackClosure -> RemoteExpr (IO Closure)
getClosureData :: RemoteExpr StgStackClosure -> RemoteExpr (IO Closure)
getClosureData = RemoteExpr (StgStackClosure -> IO Closure)
-> RemoteExpr StgStackClosure -> RemoteExpr (IO Closure)
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (StgStackClosure -> IO Closure)
 -> RemoteExpr StgStackClosure -> RemoteExpr (IO Closure))
-> RemoteExpr (StgStackClosure -> IO Closure)
-> RemoteExpr StgStackClosure
-> RemoteExpr (IO Closure)
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String -> [String] -> RemoteExpr (StgStackClosure -> IO Closure)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Exts.Heap") String
"getClosureData" [String
"GHC.Exts.LiftedRep", String
"_"]

-- | Remote 'GHC.Conc.Sync.fromThreadId'
fromThreadId :: RemoteExpr ThreadId -> RemoteExpr Word64
fromThreadId :: RemoteExpr ThreadId -> RemoteExpr Word64
fromThreadId = RemoteExpr (ThreadId -> Word64)
-> RemoteExpr ThreadId -> RemoteExpr Word64
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (ThreadId -> Word64)
 -> RemoteExpr ThreadId -> RemoteExpr Word64)
-> RemoteExpr (ThreadId -> Word64)
-> RemoteExpr ThreadId
-> RemoteExpr Word64
forall a b. (a -> b) -> a -> b
$ ModuleName -> String -> [String] -> RemoteExpr (ThreadId -> Word64)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Conc.Sync") String
"fromThreadId" []

-- | Remote 'GHC.Conc.Sync.threadStatus'
threadStatus :: RemoteExpr ThreadId -> RemoteExpr (IO ThreadStatus)
threadStatus :: RemoteExpr ThreadId -> RemoteExpr (IO ThreadStatus)
threadStatus = RemoteExpr (ThreadId -> IO ThreadStatus)
-> RemoteExpr ThreadId -> RemoteExpr (IO ThreadStatus)
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (ThreadId -> IO ThreadStatus)
 -> RemoteExpr ThreadId -> RemoteExpr (IO ThreadStatus))
-> RemoteExpr (ThreadId -> IO ThreadStatus)
-> RemoteExpr ThreadId
-> RemoteExpr (IO ThreadStatus)
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String -> [String] -> RemoteExpr (ThreadId -> IO ThreadStatus)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Conc.Sync") String
"threadStatus" []

-- | Remote 'GHC.Conc.Sync.listThreads'
listThreads :: RemoteExpr (IO [ThreadId])
listThreads :: RemoteExpr (IO [ThreadId])
listThreads = ModuleName -> String -> [String] -> RemoteExpr (IO [ThreadId])
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Conc.Sync") String
"listThreads" []

-- | Remote 'GHC.Conc.Sync.threadLabel'
threadLabel :: RemoteExpr ThreadId -> RemoteExpr (IO (Maybe String))
threadLabel :: RemoteExpr ThreadId -> RemoteExpr (IO (Maybe String))
threadLabel = RemoteExpr (ThreadId -> IO (Maybe String))
-> RemoteExpr ThreadId -> RemoteExpr (IO (Maybe String))
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (ThreadId -> IO (Maybe String))
 -> RemoteExpr ThreadId -> RemoteExpr (IO (Maybe String)))
-> RemoteExpr (ThreadId -> IO (Maybe String))
-> RemoteExpr ThreadId
-> RemoteExpr (IO (Maybe String))
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String -> [String] -> RemoteExpr (ThreadId -> IO (Maybe String))
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Conc.Sync") String
"threadLabel" []

-- | Remote 'Foreign.C.String.peekCString'
peekCString :: RemoteExpr CString -> RemoteExpr (IO String)
peekCString :: RemoteExpr CString -> RemoteExpr (IO String)
peekCString = RemoteExpr (CString -> IO String)
-> RemoteExpr CString -> RemoteExpr (IO String)
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (CString -> IO String)
 -> RemoteExpr CString -> RemoteExpr (IO String))
-> RemoteExpr (CString -> IO String)
-> RemoteExpr CString
-> RemoteExpr (IO String)
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String -> [String] -> RemoteExpr (CString -> IO String)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"Foreign.C.String") String
"peekCString" []

-- | Remote 'GHC.Base.indexAddrArray#'
indexAddrArray :: RemoteExpr ByteArray# -> RemoteExpr (Int# -> Ptr a)
indexAddrArray :: forall a. RemoteExpr ByteArray# -> RemoteExpr (Int# -> Ptr a)
indexAddrArray = RemoteExpr (ByteArray# -> Int# -> Ptr a)
-> RemoteExpr ByteArray# -> RemoteExpr (Int# -> Ptr a)
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (ByteArray# -> Int# -> Ptr a)
 -> RemoteExpr ByteArray# -> RemoteExpr (Int# -> Ptr a))
-> RemoteExpr (ByteArray# -> Int# -> Ptr a)
-> RemoteExpr ByteArray#
-> RemoteExpr (Int# -> Ptr a)
forall a b. (a -> b) -> a -> b
$
  String -> RemoteExpr (ByteArray# -> Int# -> Ptr a)
forall a. String -> RemoteExpr a
Remote.raw String
"\\b i -> GHC.Ptr.Ptr (GHC.Base.indexAddrArray# b i)"

-- | Remote 'Data.Maybe.maybeToList'
maybeToList :: RemoteExpr (Maybe a) -> RemoteExpr [a]
maybeToList :: forall a. RemoteExpr (Maybe a) -> RemoteExpr [a]
maybeToList = RemoteExpr (Maybe a -> [a])
-> RemoteExpr (Maybe a) -> RemoteExpr [a]
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (Maybe a -> [a])
 -> RemoteExpr (Maybe a) -> RemoteExpr [a])
-> RemoteExpr (Maybe a -> [a])
-> RemoteExpr (Maybe a)
-> RemoteExpr [a]
forall a b. (a -> b) -> a -> b
$ ModuleName -> String -> [String] -> RemoteExpr (Maybe a -> [a])
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"Data.Maybe") String
"maybeToList" []

-- | Function composition on the remote process
compose :: RemoteExpr (b -> c) -> RemoteExpr (a -> b) -> RemoteExpr (a -> c)
RemoteExpr (b -> c)
f compose :: forall b c a.
RemoteExpr (b -> c) -> RemoteExpr (a -> b) -> RemoteExpr (a -> c)
`compose` RemoteExpr (a -> b)
g = RemoteExpr ((a -> b) -> a -> c)
-> RemoteExpr (a -> b) -> RemoteExpr (a -> c)
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr ((b -> c) -> (a -> b) -> a -> c)
-> RemoteExpr (b -> c) -> RemoteExpr ((a -> b) -> a -> c)
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app RemoteExpr ((b -> c) -> (a -> b) -> a -> c)
forall b c a. RemoteExpr ((b -> c) -> (a -> b) -> a -> c)
composeVar RemoteExpr (b -> c)
f) RemoteExpr (a -> b)
g where
  composeVar :: RemoteExpr ((b -> c) -> (a -> b) -> (a -> c))
  composeVar :: forall b c a. RemoteExpr ((b -> c) -> (a -> b) -> a -> c)
composeVar = ModuleName
-> String
-> [String]
-> RemoteExpr ((b -> c) -> (a -> b) -> a -> c)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Base") String
"." []

displayStackAnnotation :: RemoteExpr SomeStackAnnotation -> RemoteExpr String
displayStackAnnotation :: RemoteExpr SomeStackAnnotation -> RemoteExpr String
displayStackAnnotation = RemoteExpr (SomeStackAnnotation -> String)
-> RemoteExpr SomeStackAnnotation -> RemoteExpr String
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (SomeStackAnnotation -> String)
 -> RemoteExpr SomeStackAnnotation -> RemoteExpr String)
-> RemoteExpr (SomeStackAnnotation -> String)
-> RemoteExpr SomeStackAnnotation
-> RemoteExpr String
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String -> [String] -> RemoteExpr (SomeStackAnnotation -> String)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Internal.Stack.Annotation") String
"displayStackAnnotation" [String
"GHC.Stack.Annotation.Experimental.SomeStackAnnotation"]

displayStackAnnotationShort :: RemoteExpr SomeStackAnnotation -> RemoteExpr String
displayStackAnnotationShort :: RemoteExpr SomeStackAnnotation -> RemoteExpr String
displayStackAnnotationShort = RemoteExpr (SomeStackAnnotation -> String)
-> RemoteExpr SomeStackAnnotation -> RemoteExpr String
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (SomeStackAnnotation -> String)
 -> RemoteExpr SomeStackAnnotation -> RemoteExpr String)
-> RemoteExpr (SomeStackAnnotation -> String)
-> RemoteExpr SomeStackAnnotation
-> RemoteExpr String
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String -> [String] -> RemoteExpr (SomeStackAnnotation -> String)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Internal.Stack.Annotation") String
"displayStackAnnotationShort" [String
"GHC.Stack.Annotation.Experimental.SomeStackAnnotation"]

stackAnnotationSourceLocation :: RemoteExpr SomeStackAnnotation -> RemoteExpr (Maybe SrcLoc)
stackAnnotationSourceLocation :: RemoteExpr SomeStackAnnotation -> RemoteExpr (Maybe SrcLoc)
stackAnnotationSourceLocation = RemoteExpr (SomeStackAnnotation -> Maybe SrcLoc)
-> RemoteExpr SomeStackAnnotation -> RemoteExpr (Maybe SrcLoc)
forall a b. RemoteExpr (a -> b) -> RemoteExpr a -> RemoteExpr b
Remote.app (RemoteExpr (SomeStackAnnotation -> Maybe SrcLoc)
 -> RemoteExpr SomeStackAnnotation -> RemoteExpr (Maybe SrcLoc))
-> RemoteExpr (SomeStackAnnotation -> Maybe SrcLoc)
-> RemoteExpr SomeStackAnnotation
-> RemoteExpr (Maybe SrcLoc)
forall a b. (a -> b) -> a -> b
$ ModuleName
-> String
-> [String]
-> RemoteExpr (SomeStackAnnotation -> Maybe SrcLoc)
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var (String -> ModuleName
mkModuleName String
"GHC.Internal.Stack.Annotation") String
"stackAnnotationSourceLocation" [String
"SGHC.Stack.Annotation.Experimental.omeStackAnnotation"]

unpackStackFields :: Remote.RemoteExpr ([GHC.StackField] -> Maybe [Int] -> IO [HValue])
unpackStackFields :: RemoteExpr ([StackField] -> Maybe [Int] -> IO [HValue])
unpackStackFields = ModuleName
-> String
-> [String]
-> RemoteExpr ([StackField] -> Maybe [Int] -> IO [HValue])
forall a. ModuleName -> String -> [String] -> RemoteExpr a
Remote.var ModuleName
debuggerRuntimeInternalModName String
"unpackStackFields" []