19
19
----------------------------------------------------------------------------
20
20
module Main (main ) where
21
21
----------------------------------------------------------------------------
22
- import Text.PrettyPrint.ANSI.Leijen (pretty , plain )
23
22
import Codec.Archive.Zip (withArchive , unEntrySelector , getEntries )
24
23
import qualified Data.Set as Set
25
24
import Control.Arrow
@@ -36,6 +35,7 @@ import Data.Text ( Text )
36
35
import qualified Data.Text as T
37
36
import Data.Typeable ( typeOf )
38
37
import Data.Maybe ( fromMaybe )
38
+ import Data.List ( sortOn )
39
39
import GHC.Generics ( Generic )
40
40
import System.Environment ( lookupEnv )
41
41
import System.FilePath ((</>) , takeDirectory , takeExtension )
@@ -44,7 +44,8 @@ import qualified Data.ByteString.Lazy.Char8 as BL8 ( pack, unpack, fr
44
44
import qualified Control.Concurrent.Chan.Unagi.Bounded as Unagi
45
45
----------------------------------------------------------------------------
46
46
import Stg.Syntax hiding (sourceName , Scope )
47
- import Stg.Pretty ()
47
+ import Stg.IRLocation
48
+ import Stg.Pretty
48
49
import Stg.Interpreter
49
50
import Stg.Interpreter.Debug
50
51
import Stg.Interpreter.Base hiding (lookupEnv , getCurrentThreadState , getCurrentThreadState )
@@ -114,7 +115,7 @@ initESTG AttachArgs {..} = do
114
115
frameRef <- liftIO (newIORef scopes')
115
116
registerNewDebugSession __sessionId (ESTG dbgCmdI dbgOutO program)
116
117
$ flip catch handleDebuggerExceptions
117
- $ do liftIO $ loadAndRunProgram True True program [] dbgChan DbgStepByStep False
118
+ $ do liftIO $ loadAndRunProgram True True program [] dbgChan DbgStepByStep False defaultDebugSettings
118
119
-- ^ doesn't seem to return here
119
120
sendTerminatedEvent (TerminatedEvent False )
120
121
sendExitedEvent (ExitedEvent 0 )
@@ -160,6 +161,26 @@ talk CommandContinue = do
160
161
ESTG {.. } <- getDebugSession
161
162
send CmdContinue
162
163
sendContinueResponse (ContinueResponse True )
164
+
165
+ ESTG {.. } <- getDebugSession
166
+ _ <- liftIO $ Unagi. readChan outChan
167
+ resetObjectLifetimes
168
+ sendStoppedEvent defaultStoppedEvent
169
+ { stoppedEventReason = StoppedEventReasonBreakpoint
170
+ , stoppedEventThreadId = Just 0
171
+ }
172
+ {-
173
+ data StoppedEvent
174
+ = StoppedEvent
175
+ { stoppedEventReason :: StoppedEventReason
176
+ , stoppedEventDescription :: Maybe Text
177
+ , stoppedEventThreadId :: Maybe Int
178
+ , stoppedEventPreserveFocusHint :: Bool
179
+ , stoppedEventText :: Maybe Text
180
+ , stoppedEventAllThreadsStopped :: Bool
181
+ , stoppedEventHitBreakpointIds :: [Int]
182
+ -}
183
+
163
184
----------------------------------------------------------------------------
164
185
talk CommandConfigurationDone = do
165
186
sendConfigurationDoneResponse
@@ -301,16 +322,54 @@ talk CommandPause = sendPauseResponse
301
322
-- }
302
323
talk CommandSetBreakpoints = do
303
324
SetBreakpointsArguments {.. } <- getArguments
304
- let maybeName = sourceName setBreakpointsArgumentsSource
305
- case (setBreakpointsArgumentsBreakpoints, maybeName) of
306
- (Just [ SourceBreakpoint {.. } ], Just name) -> do
307
- send (CmdAddBreakpoint (T. encodeUtf8 name) sourceBreakpointLine)
308
- sendSetBreakpointsResponse
309
- [ defaultBreakpoint { breakpointId = Just sourceBreakpointLine
310
- , breakpointSource = Just setBreakpointsArgumentsSource
311
- , breakpointVerified = True
312
- }
313
- ]
325
+ let maybeSourceRef = sourceSourceReference setBreakpointsArgumentsSource
326
+ case (setBreakpointsArgumentsBreakpoints, maybeSourceRef) of
327
+ (Just sourceBreakpoints, Just sourceRef) -> do
328
+ (_sourceCodeText, locations) <- getSourceFromFullPak sourceRef
329
+ breakpoints <- forM sourceBreakpoints $ \ SourceBreakpoint {.. } -> do
330
+ -- filter all relevant ranges
331
+ {-
332
+ SP_RhsClosureExpr
333
+ -}
334
+ let onlySupported = \ case
335
+ SP_RhsClosureExpr {} -> True
336
+ _ -> False
337
+ let relevantLocations = filter (onlySupported . fst ) $ case sourceBreakpointColumn of
338
+ Nothing ->
339
+ [ p
340
+ | p@ (_,((startRow, startCol), (endRow, endCol))) <- locations
341
+ , startRow <= sourceBreakpointLine
342
+ , endRow >= sourceBreakpointLine
343
+ ]
344
+ Just col ->
345
+ [ p
346
+ | p@ (_,((startRow, startCol), (endRow, endCol))) <- locations
347
+ , startRow <= sourceBreakpointLine
348
+ , endRow >= sourceBreakpointLine
349
+ , startCol <= col
350
+ , endCol >= col
351
+ ]
352
+ liftIO $ putStrLn $ " relevantLocations: " ++ show relevantLocations
353
+ -- use the first location found
354
+ case sortOn snd relevantLocations of
355
+ (stgPoint@ (SP_RhsClosureExpr closureName), ((startRow, startCol), (endRow, endCol))) : _ -> do
356
+ let hitCount = fromMaybe 0 (sourceBreakpointHitCondition >>= readMaybe . T. unpack) :: Int
357
+ send (CmdAddBreakpoint closureName hitCount)
358
+ pure $ defaultBreakpoint
359
+ { breakpointVerified = True
360
+ , breakpointSource = Just setBreakpointsArgumentsSource
361
+ , breakpointLine = Just startRow
362
+ , breakpointColumn = Just startCol
363
+ , breakpointEndLine = Just endRow
364
+ , breakpointEndColumn = Just endCol
365
+ }
366
+ _ ->
367
+ pure $ defaultBreakpoint
368
+ { breakpointVerified = False
369
+ , breakpointSource = Just setBreakpointsArgumentsSource
370
+ , breakpointMessage = Just " no code found"
371
+ }
372
+ sendSetBreakpointsResponse breakpoints
314
373
_ ->
315
374
sendSetBreakpointsResponse []
316
375
----------------------------------------------------------------------------
@@ -338,7 +397,7 @@ talk CommandStackTrace = do
338
397
----------------------------------------------------------------------------
339
398
talk CommandSource = do
340
399
SourceArguments {.. } <- getArguments -- save path of fullpak in state
341
- source <- getSourceFromFullPak sourceArgumentsSourceReference
400
+ ( source, _locations) <- getSourceFromFullPak sourceArgumentsSourceReference
342
401
sendSourceResponse (SourceResponse source Nothing )
343
402
----------------------------------------------------------------------------
344
403
talk CommandThreads = do
@@ -421,17 +480,18 @@ getModuleListFromFullPak = do
421
480
]
422
481
----------------------------------------------------------------------------
423
482
-- | Retrieves list of modules from .fullpak file
424
- getSourceFromFullPak :: SourceId -> Adaptor ESTG Text
483
+ getSourceFromFullPak :: SourceId -> Adaptor ESTG ( Text , [( StgPoint , SrcRange )])
425
484
getSourceFromFullPak sourceId = do
426
485
sourcePath <- T. unpack <$> getSourcePathBySourceReferenceId sourceId
427
486
ESTG {.. } <- getDebugSession
428
487
liftIO $
429
488
if takeExtension sourcePath == " .stgbin"
430
489
then do
431
490
m <- readModpakL fullPakPath sourcePath decodeStgbin
432
- pure $ T. pack $ show $ plain (pretty m)
433
- else
434
- readModpakS fullPakPath sourcePath T. decodeUtf8
491
+ pure . pShow $ pprModule m
492
+ else do
493
+ ir <- readModpakS fullPakPath sourcePath T. decodeUtf8
494
+ pure (ir, [] )
435
495
----------------------------------------------------------------------------
436
496
-- | Asynchronous call to Debugger, sends message, does not wait for response
437
497
send
0 commit comments