Skip to content

Commit 6f5bfcf

Browse files
committed
all debug command return result
1 parent 46ac63f commit 6f5bfcf

File tree

4 files changed

+64
-41
lines changed

4 files changed

+64
-41
lines changed

external-stg-interpreter/lib/Stg/Interpreter/Base.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ instance Show (PrintableMVar a) where
223223

224224
data DebuggerChan
225225
= DebuggerChan
226-
{ dbgSyncRequest :: MVar DebugRequest
227-
, dbgSyncResponse :: MVar DebugResponse
226+
{ dbgSyncRequest :: MVar DebugCommand
227+
, dbgSyncResponse :: MVar DebugOutput
228228
, dbgAsyncEventIn :: InChan DebugEvent
229229
, dbgAsyncEventOut :: OutChan DebugEvent
230230
}
@@ -233,9 +233,10 @@ data DebuggerChan
233233
instance Show DebuggerChan where
234234
show _ = "DebuggerChan"
235235

236-
type DebugRequest = DebugCommand
237-
type DebugResponse = DebugOutput
238-
type DebugEvent = DebugOutput
236+
data DebugEvent
237+
= DbgEventHitBreakpoint !Name
238+
| DbgEventStopped
239+
deriving (Show)
239240

240241
data DebugCommand
241242
= CmdListClosures
@@ -256,6 +257,8 @@ data DebugOutput
256257
| DbgOutThreadReport !Int !ThreadState !Name !Addr String
257258
| DbgOutHeapObject !Addr !HeapObject
258259
| DbgOutResult ![Atom]
260+
| DbgOutString !String
261+
| DbgOutByteString !ByteString
259262
| DbgOut
260263
deriving (Show)
261264

external-stg-interpreter/lib/Stg/Interpreter/Debugger.hs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import Stg.Syntax
1414

1515
import Stg.Interpreter.Debugger.Internal
1616

17+
sendDebugEvent :: DebugEvent -> M ()
18+
sendDebugEvent dbgEvent = do
19+
DebuggerChan{..} <- gets ssDebuggerChan
20+
liftIO $ Unagi.writeChan dbgAsyncEventIn dbgEvent
21+
1722
getNextDebugCommand :: M DebugCommand
1823
getNextDebugCommand = do
1924
DebuggerChan{..} <- gets ssDebuggerChan
@@ -37,30 +42,38 @@ runDebugCommand cmd = do
3742

3843
CmdClearClosureList -> do
3944
modify' $ \s@StgState{..} -> s {ssEvaluatedClosures = Set.empty}
45+
liftIO $ putMVar dbgSyncResponse DbgOut
4046

4147
CmdListClosures -> do
4248
closures <- gets ssEvaluatedClosures
4349
liftIO $ putMVar dbgSyncResponse $ DbgOutClosureList $ Set.toList closures
4450

4551
CmdAddBreakpoint n i -> do
4652
modify' $ \s@StgState{..} -> s {ssBreakpoints = Map.insert n i ssBreakpoints}
53+
liftIO $ putMVar dbgSyncResponse DbgOut
4754

4855
CmdRemoveBreakpoint n -> do
4956
modify' $ \s@StgState{..} -> s {ssBreakpoints = Map.delete n ssBreakpoints}
57+
liftIO $ putMVar dbgSyncResponse DbgOut
5058

51-
CmdStep -> pure ()
59+
CmdStep -> liftIO $ putMVar dbgSyncResponse DbgOut
5260

5361
CmdContinue -> do
5462
modify' $ \s@StgState{..} -> s {ssDebugState = DbgRunProgram}
63+
liftIO $ putMVar dbgSyncResponse DbgOut
5564

5665
CmdPeekHeap addr -> do
5766
heap <- gets ssHeap
58-
when (IntMap.member addr heap) $ do
59-
ho <- readHeap $ HeapPtr addr
60-
liftIO $ putMVar dbgSyncResponse $ DbgOutHeapObject addr ho
67+
case IntMap.member addr heap of
68+
True -> do
69+
ho <- readHeap $ HeapPtr addr
70+
liftIO $ putMVar dbgSyncResponse $ DbgOutHeapObject addr ho
71+
False -> do
72+
liftIO $ putMVar dbgSyncResponse DbgOut
6173

6274
CmdStop -> do
6375
modify' $ \s@StgState{..} -> s {ssDebugState = DbgStepByStep}
76+
liftIO $ putMVar dbgSyncResponse DbgOut
6477

6578
CmdInternal cmd -> do
6679
runInternalCommand cmd
@@ -102,7 +115,7 @@ checkBreakpoint breakpointName = do
102115
shouldStep <- hasFuel
103116
case dbgState of
104117
DbgStepByStep -> do
105-
reportStateAsync
118+
sendDebugEvent DbgEventStopped
106119
unless exit processCommandsUntilExit
107120
DbgRunProgram -> do
108121
unless shouldStep $ modify' $ \s@StgState{..} -> s {ssDebugState = DbgStepByStep}
@@ -117,6 +130,6 @@ checkBreakpoint breakpointName = do
117130
| otherwise -> do
118131
-- HINT: trigger breakpoint
119132
liftIO $ putStrLn $ "hit breakpoint: " ++ show breakpointName
120-
reportStateAsync
133+
sendDebugEvent $ DbgEventHitBreakpoint breakpointName
121134
modify' $ \s@StgState{..} -> s {ssDebugState = DbgStepByStep}
122135
unless exit processCommandsUntilExit

external-stg-interpreter/lib/Stg/Interpreter/Debugger/Internal.hs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ reportStateSync = do
4545
msg <- getThreadReport
4646
liftIO $ putMVar dbgSyncResponse msg
4747

48-
reportStateAsync :: M ()
49-
reportStateAsync = do
50-
DebuggerChan{..} <- gets ssDebuggerChan
51-
msg <- getThreadReport
52-
liftIO $ Unagi.writeChan dbgAsyncEventIn msg
53-
5448
getThreadReport :: M DebugOutput
5549
getThreadReport = do
5650
tid <- gets ssCurrentThreadId
@@ -120,40 +114,46 @@ showRetainerTree i = do
120114
tree <- getRetainerTree i
121115
liftIO $ putStrLn $ drawTree tree
122116

117+
wrapWithDbgOut :: ([String] -> M ()) -> [String] -> M ()
118+
wrapWithDbgOut cmdM args = do
119+
cmdM args
120+
DebuggerChan{..} <- gets ssDebuggerChan
121+
liftIO $ putMVar dbgSyncResponse DbgOut
122+
123123
dbgCommands :: [([String], String, [String] -> M ())]
124124
dbgCommands =
125125
[ ( ["gc"]
126126
, "run sync. garbage collector"
127-
, \_ -> do
127+
, wrapWithDbgOut $ \_ -> do
128128
curClosureAddr <- gets ssCurrentClosureAddr
129129
GC.runGCSync [HeapPtr curClosureAddr]
130130
)
131131
, ( ["cleardb"]
132132
, "clear retainer db"
133-
, \_ -> clearRetanerDb
133+
, wrapWithDbgOut $ \_ -> clearRetanerDb
134134
)
135135

136136
, ( ["loaddb"]
137137
, "load retainer db"
138-
, \_ -> loadRetainerDb2
138+
, wrapWithDbgOut $ \_ -> loadRetainerDb2
139139
)
140140

141141
, ( ["?"]
142142
, "show debuggers' all internal commands"
143-
, \_ -> printHelp
143+
, wrapWithDbgOut $ \_ -> printHelp
144144
)
145145

146146
, ( ["report"]
147147
, "report some internal data"
148-
, \_ -> do
148+
, wrapWithDbgOut $ \_ -> do
149149
heapStart <- gets ssHeapStartAddress
150150
liftIO $ do
151151
putStrLn $ "heap start address: " ++ show heapStart
152152
)
153153

154154
, ( ["query", "??"]
155155
, "queries a given list of NAME_PATTERNs in static global env as substring"
156-
, \patterns -> do
156+
, wrapWithDbgOut $ \patterns -> do
157157
env <- gets ssStaticGlobalEnv
158158
let filterPattern pat resultList = [n | n <- resultList, List.isInfixOf pat n]
159159
matches = foldr filterPattern (map show $ Map.keys env) patterns
@@ -162,14 +162,14 @@ dbgCommands =
162162

163163
, ( ["?b"]
164164
, "list breakpoints"
165-
, \_ -> do
165+
, wrapWithDbgOut $ \_ -> do
166166
bks <- Map.toList <$> gets ssBreakpoints
167167
liftIO $ putStrLn $ unlines [printf "%-40s %d [fuel]" (BS8.unpack name) fuel | (name, fuel) <- bks]
168168
)
169169

170170
, ( ["?r"]
171171
, "[START] [END] list a given region or all regions if the arguments are omitted"
172-
, \case
172+
, wrapWithDbgOut $ \case
173173
[] -> do
174174
regions <- Map.keys <$> gets ssRegions
175175
liftIO $ putStrLn $ unlines $ map show regions
@@ -180,31 +180,31 @@ dbgCommands =
180180

181181
, ( ["?r-dump"]
182182
, "[START] [END] dump all heap object from the given region"
183-
, \case
183+
, wrapWithDbgOut $ \case
184184
[start] -> showRegion True start start
185185
[start, end] -> showRegion True start end
186186
_ -> pure ()
187187
)
188188

189189
, ( ["+r"]
190190
, "add region: +r START_CLOSURE_NAME [END_CLOSURE_NAME] ; if only the start is provided then it will be the end marker also"
191-
, \case
191+
, wrapWithDbgOut $ \case
192192
[start] -> addRegion start start
193193
[start, end] -> addRegion start end
194194
_ -> pure ()
195195
)
196196

197197
, ( ["-r"]
198198
, "del region: -r START_CLOSURE_NAME [END_CLOSURE_NAME] ; if only the start is provided then it will be the end marker also"
199-
, \case
199+
, wrapWithDbgOut $ \case
200200
[start] -> delRegion start start
201201
[start, end] -> delRegion start end
202202
_ -> pure ()
203203
)
204204

205205
, ( ["peek-range", "pr"]
206206
, "ADDR_START ADDR_END [COUNT] - list all heap objects in the given heap address region, optionally show only the first (COUNT) elements"
207-
, \case
207+
, wrapWithDbgOut $ \case
208208
[start, end]
209209
| Just s <- Text.readMaybe start
210210
, Just e <- Text.readMaybe end
@@ -223,7 +223,7 @@ dbgCommands =
223223

224224
, ( ["count-range", "cr"]
225225
, "ADDR_START ADDR_END - count heap objects in the given heap address region"
226-
, \case
226+
, wrapWithDbgOut $ \case
227227
[start, end]
228228
| Just s <- Text.readMaybe start
229229
, Just e <- Text.readMaybe end
@@ -235,7 +235,7 @@ dbgCommands =
235235

236236
, ( ["retainer", "ret"]
237237
, "ADDR - show the retainer objects (heap objects that refer to the queried object"
238-
, \case
238+
, wrapWithDbgOut $ \case
239239
[addrS]
240240
| Just addr <- Text.readMaybe addrS
241241
-> showRetainer addr
@@ -244,7 +244,7 @@ dbgCommands =
244244

245245
, ( ["ret-tree", "rt"]
246246
, "ADDR - show the retainer tree of an object"
247-
, \case
247+
, wrapWithDbgOut $ \case
248248
[addrS]
249249
| Just addr <- Text.readMaybe addrS
250250
-> showRetainerTree addr
@@ -253,7 +253,7 @@ dbgCommands =
253253

254254
, ( ["trace-origin", "to"]
255255
, "ADDR - traces back heap object origin until the first dead object"
256-
, \case
256+
, wrapWithDbgOut $ \case
257257
[addrS]
258258
| Just addr <- Text.readMaybe addrS
259259
-> showOriginTrace addr
@@ -262,14 +262,14 @@ dbgCommands =
262262

263263
, ( ["?e"]
264264
, "list all trace events and heap address state"
265-
, \_-> do
265+
, wrapWithDbgOut $ \_-> do
266266
events <- gets ssTraceEvents
267267
forM_ (reverse events) $ \(msg, AddressState{..}) -> liftIO $ printf "%-10d %s\n" asNextHeapAddr (show msg)
268268
)
269269

270270
, ( ["?e-dump"]
271271
, "list all trace events and the whole address state"
272-
, \_-> do
272+
, wrapWithDbgOut $ \_-> do
273273
events <- gets ssTraceEvents
274274
forM_ (reverse events) $ \(msg, a) -> liftIO $ do
275275
print msg
@@ -278,14 +278,14 @@ dbgCommands =
278278

279279
, ( ["?m"]
280280
, "list all trace markers and heap address state"
281-
, \_-> do
281+
, wrapWithDbgOut $ \_-> do
282282
markers <- gets ssTraceMarkers
283283
forM_ (reverse markers) $ \(msg, AddressState{..}) -> liftIO $ printf "%-10d %s\n" asNextHeapAddr (show msg)
284284
)
285285

286286
, ( ["?m-dump"]
287287
, "list all trace markers and the whole address state"
288-
, \_-> do
288+
, wrapWithDbgOut $ \_-> do
289289
markers <- gets ssTraceMarkers
290290
forM_ (reverse markers) $ \(msg, a) -> liftIO $ do
291291
print msg
@@ -294,7 +294,7 @@ dbgCommands =
294294

295295
, ( ["save-state"]
296296
, "DIR_NAME - save stg state as datalog facts to the given directory"
297-
, \case
297+
, wrapWithDbgOut $ \case
298298
[dirName] -> do
299299
s <- get
300300
liftIO $ do
@@ -305,7 +305,7 @@ dbgCommands =
305305

306306
, ( ["fuel"]
307307
, "STEP-COUNT - make multiple steps ; 'fuel -' - turn off step count check"
308-
, \case
308+
, wrapWithDbgOut $ \case
309309
["-"]
310310
-> modify' $ \s@StgState{..} -> s {ssDebugFuel = Nothing}
311311
[countS]

external-stg-interpreter/lib/Stg/Interpreter/Debugger/UI.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ printDebugOutput = \case
104104
putStrLn $ "addr: " ++ show addr
105105
printHeapObject heapObj
106106

107+
DbgOutString msg -> putStrLn msg
108+
109+
DbgOutByteString msg -> BS8.putStrLn msg
110+
111+
DbgOut -> pure ()
112+
107113
printHeapObject :: HeapObject -> IO ()
108114
printHeapObject = \case
109115
Con{..} -> do
@@ -182,9 +188,10 @@ runDebugScript :: DebuggerChan -> [String] -> IO ()
182188
runDebugScript dbgChan@DebuggerChan{..} lines = do
183189
let waitBreakpoint = do
184190
msg <- Unagi.readChan dbgAsyncEventOut
191+
print msg
185192
case msg of
186-
DbgOutThreadReport{} -> printDebugOutput msg
187-
_ -> printDebugOutput msg >> waitBreakpoint
193+
DbgEventHitBreakpoint{} -> putMVar dbgSyncRequest $ CmdInternal "get-current-thread-state"
194+
_ -> waitBreakpoint
188195

189196
forM_ lines $ \cmd -> do
190197
putStrLn cmd

0 commit comments

Comments
 (0)