Skip to content

Commit f400d44

Browse files
authored
Adds DebugThreadState (#6)
- Adds a data type to hold the debubgger exeucting thread + output event sink
1 parent 3b97e22 commit f400d44

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

dap/exe/Main.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ initESTG AttachArgs {..} = do
119119
let dbgChan = DebuggerChan (dbgCmdO, dbgOutI)
120120
flip catch handleDebuggerExceptions
121121
$ registerNewDebugSession __sessionId (ESTG dbgCmdI dbgOutO program)
122-
$ liftIO $ loadAndRunProgram True True program [] dbgChan DbgStepByStep False defaultDebugSettings
122+
(liftIO $ loadAndRunProgram True True program [] dbgChan DbgStepByStep False defaultDebugSettings)
123+
(pure ())
124+
-- (forever $ do
125+
-- message <- liftIO (Unagi.readChan dbgOutO)
126+
-- -- logic goes here for conversion to 'OutputEvent'
127+
-- sendOutputEvent defaultOutputEvent)
123128
----------------------------------------------------------------------------
124129
-- | Exception Handler
125130
handleDebuggerExceptions :: SomeException -> Adaptor ESTG ()

dap/src/DAP/Adaptor.hs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,20 @@ registerNewDebugSession
177177
:: SessionId
178178
-> app
179179
-> Adaptor app ()
180+
-- ^ Action to run debugger (operates in a forked thread that gets killed when disconnect is set)
181+
-> Adaptor app ()
180182
-- ^ Long running operation, meant to be used as a sink for
181183
-- the debugger to emit events and for the adaptor to forward to the editor
182184
-- This function should be in a 'forever' loop waiting on the read end of
183185
-- a debugger channel.
184186
-> Adaptor app ()
185-
registerNewDebugSession k v action = do
187+
registerNewDebugSession k v debuggerExecution outputEventSink = do
186188
store <- gets appStore
187-
tid <- fork (resetAdaptorStatePayload >> action)
188-
liftIO . atomically $ modifyTVar' store (H.insert k (tid, v))
189+
debuggerThreadState <-
190+
DebuggerThreadState
191+
<$> fork (debuggerExecution)
192+
<*> fork (outputEventSink)
193+
liftIO . atomically $ modifyTVar' store (H.insert k (debuggerThreadState, v))
189194
setDebugSessionId k
190195
logInfo $ BL8.pack $ "Registered new debug session: " <> unpack k
191196
----------------------------------------------------------------------------
@@ -194,7 +199,7 @@ getDebugSession = do
194199
(_, _, app) <- getDebugSessionWithThreadIdAndSessionId
195200
pure app
196201
----------------------------------------------------------------------------
197-
getDebugSessionWithThreadIdAndSessionId :: Adaptor app (SessionId, ThreadId, app)
202+
getDebugSessionWithThreadIdAndSessionId :: Adaptor app (SessionId, DebuggerThreadState, app)
198203
getDebugSessionWithThreadIdAndSessionId = do
199204
sessionId <- getDebugSessionId
200205
appStore <- liftIO . readTVarIO =<< getAppStore
@@ -217,10 +222,11 @@ getDebugSessionWithThreadIdAndSessionId = do
217222
----------------------------------------------------------------------------
218223
destroyDebugSession :: Adaptor app ()
219224
destroyDebugSession = do
220-
(sessionId, tid, app) <- getDebugSessionWithThreadIdAndSessionId
225+
(sessionId, DebuggerThreadState {..}, app) <- getDebugSessionWithThreadIdAndSessionId
221226
store <- getAppStore
222227
liftIO $ do
223-
killThread tid
228+
killThread debuggerThread
229+
killThread debuggerOutputEventThread
224230
atomically $ modifyTVar' store (H.delete sessionId)
225231
logInfo $ BL8.pack $ "SessionId " <> unpack sessionId <> " ended"
226232
----------------------------------------------------------------------------

dap/src/DAP/Types.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ module DAP.Types
207207
-- * Loaded Sources Path
208208
, SourcePath
209209
, SourceId
210+
-- * Debug Thread state
211+
, DebuggerThreadState (..)
210212
) where
211213
----------------------------------------------------------------------------
212214
import Data.Word ( Word32 )
@@ -336,7 +338,18 @@ type SourceId = Int
336338
-- The 'ThreadId' is meant to be an asynchronous operation that
337339
-- allows initalized debuggers to emit custom events
338340
-- when they receive messages from the debugger
339-
type AppStore app = TVar (H.HashMap SessionId (ThreadId, app))
341+
type AppStore app = TVar (H.HashMap SessionId (DebuggerThreadState, app))
342+
343+
-- | 'DebuggerThreadState'
344+
-- State to hold both the thread that executes the debugger and the thread used
345+
-- to propagate output events from the debugger + debuggee to the editor (via the
346+
-- DAP server).
347+
data DebuggerThreadState
348+
= DebuggerThreadState
349+
{ debuggerThread :: ThreadId
350+
, debuggerOutputEventThread :: ThreadId
351+
}
352+
340353
----------------------------------------------------------------------------
341354
data ServerConfig
342355
= ServerConfig

0 commit comments

Comments
 (0)