summaryrefslogtreecommitdiff
path: root/src/GDB.hs
blob: 3c60457b64a3063d799d2d6e6b4ee579b4404b58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
{-# LANGUAGE OverloadedStrings #-}

module GDB (
    GDB, gdbProcess,
    gdbStart,
    addInferior,
    gdbSession,
) where

import Control.Concurrent
import Control.Concurrent.STM
import Control.Monad.IO.Class
import Control.Monad.Identity
import Control.Monad.Reader

import Data.Char
import Data.List
import Data.Text (Text)
import Data.Text qualified as T
import Data.Void

import Text.Megaparsec hiding (State)
import Text.Megaparsec.Char

import System.Process

import Output
import Process

data GDB = GDB
    { gdbProcess_ :: Process
    , gdbResult :: MVar (ResultClass, [(Text, MiValue)])
    , gdbInferiors :: MVar [Inferior]
    , gdbThreadGroups :: TChan Text
    , gdbOnCrash :: Process -> IO ()
    }

gdbProcess :: GDB -> Process
gdbProcess = gdbProcess_

data Inferior = Inferior
    { infProcess :: Process
    , infPid :: Pid
    , infThreadGroup :: Text
    , infThreads :: [Text]
    }

data MiRecord = ResultRecord ResultClass [(Text, MiValue)]
              | ExecAsyncOutput Text [(Text, MiValue)]
              | StatusAsyncOutput Text [(Text, MiValue)]
              | NotifyAsyncOutput Text [(Text, MiValue)]
              | ConsoleStreamOutput Text
              | TargetStreamOutput Text
              | LogStreamOutput Text
    deriving (Show)

data ResultClass = Done | Connected | Error | Exit
    deriving (Show)

data MiValue = MiString Text
             | MiTuple [(Text, MiValue)]
             | MiList [MiValue]
    deriving (Show)


gdbCmd :: String
gdbCmd = "gdb --quiet --interpreter=mi3"

gdbStart :: (MonadOutput m, MonadFail m) => (Process -> IO ()) -> m GDB
gdbStart onCrash = do
    (Just hin, Just hout, Just herr, handle) <- liftIO $ createProcess (shell gdbCmd)
        { std_in = CreatePipe, std_out = CreatePipe, std_err = CreatePipe
        }
    pout <- liftIO $ newTVarIO []

    let process = Process
            { procName = ProcNameGDB
            , procHandle = handle
            , procStdin = hin
            , procOutput = pout
            , procKillWith = Nothing
            , procNode = undefined
            }
    gdb <- GDB
        <$> pure process
        <*> liftIO newEmptyMVar
        <*> liftIO (newMVar [])
        <*> liftIO newTChanIO
        <*> pure onCrash

    out <- getOutput
    liftIO $ void $ forkIO $ flip runReaderT out $
        lineReadingLoop process hout $ gdbLine gdb
    liftIO $ void $ forkIO $ flip runReaderT out $
        lineReadingLoop process herr $ outProc OutputChildStderr process

    gdbCommand gdb "-gdb-set schedule-multiple on"
    gdbCommand gdb "-gdb-set mi-async on"
    gdbCommand gdb "-gdb-set non-stop on"
    gdbCommand gdb "-gdb-set print symbol-loading off"

    return gdb

gdbLine :: GDB -> Text -> ReaderT Output IO ()
gdbLine _ "(gdb)" = return ()
gdbLine _ "(gdb) " = return ()
gdbLine gdb rline = either (outProc OutputError (gdbProcess gdb) . T.pack . errorBundlePretty) go $
    runParser miOutputRecord "" rline
  where
    go = \case
        ResultRecord cls params -> liftIO $ putMVar (gdbResult gdb) (cls, params)
        ExecAsyncOutput cls params -> (cls,) <$> liftIO (readMVar (gdbInferiors gdb)) >>= \case
            ("stopped", infs)
                | Just (MiString "signal-received") <- lookup "reason" params
                , Just (MiString tid) <- lookup "thread-id" params
                , Just inf <- find (elem tid . infThreads) infs
                -> do
                    -- It is needed to switch thread manually in non-stop mode,
                    -- fork to avoid blocking further input and reply processing.
                    out <- getOutput
                    void $ liftIO $ forkIO $ do
                        flip runReaderT out $ do
                            gdbCommand gdb $ "-thread-select " <> tid
                        gdbOnCrash gdb $ infProcess inf
            _ -> return ()
        StatusAsyncOutput cls params -> outProc OutputChildInfo (gdbProcess gdb) $ "status: " <> cls <> " " <> T.pack (show params)
        NotifyAsyncOutput cls params -> case cls of
            "thread-group-added" | Just (MiString tgid) <- lookup "id" params -> do
                liftIO $ atomically $ writeTChan (gdbThreadGroups gdb) tgid
            "thread-group-exited" | Just (MiString tgid) <- lookup "id" params -> do
                liftIO $ modifyMVar_ (gdbInferiors gdb) $ return . filter ((/=tgid) . infThreadGroup)
            "thread-created"
                | Just (MiString tid) <- lookup "id" params
                , Just (MiString tgid) <- lookup "group-id" params
                -> liftIO $ modifyMVar_ (gdbInferiors gdb) $ return . map (\inf -> if infThreadGroup inf == tgid then inf { infThreads = tid : infThreads inf } else inf)
            "thread-exited"
                | Just (MiString tid) <- lookup "id" params
                , Just (MiString tgid) <- lookup "group-id" params
                -> liftIO $ modifyMVar_ (gdbInferiors gdb) $ return . map (\inf -> if infThreadGroup inf == tgid then inf { infThreads = filter (/=tid) $ infThreads inf } else inf)
            _ -> return ()
        ConsoleStreamOutput line -> mapM_ (outLine OutputAlways Nothing) (T.lines line)
        TargetStreamOutput line -> mapM_ (outProc OutputChildStderr (gdbProcess gdb) . ("target-stream: " <>)) (T.lines line)
        LogStreamOutput line -> mapM_ (outProc OutputChildInfo (gdbProcess gdb) . ("log: " <>)) (T.lines line)

addInferior :: MonadOutput m => GDB -> Process -> m ()
addInferior gdb process = do
    liftIO (getPid $ procHandle process) >>= \case
        Nothing -> outProc OutputError process $ "failed to get PID"
        Just pid -> do
            tgid <- liftIO (atomically $ tryReadTChan $ gdbThreadGroups gdb) >>= \case
                Just tgid -> return tgid
                Nothing -> do
                    gdbCommand gdb $ "-add-inferior"
                    liftIO $ atomically $ readTChan $ gdbThreadGroups gdb

            liftIO $ modifyMVar_ (gdbInferiors gdb) $ return . (:) Inferior
                { infProcess = process
                , infPid = pid
                , infThreadGroup = tgid
                , infThreads = []
                }

            gdbCommand gdb $ "-target-attach --thread-group " <> tgid <> " " <> T.pack (show pid)
            gdbCommand gdb $ "-exec-continue --thread-group " <> tgid

gdbCommand :: MonadOutput m => GDB -> Text -> m ()
gdbCommand gdb cmd = do
    send (gdbProcess gdb) cmd
    liftIO (takeMVar (gdbResult gdb)) >>= \case
        (Done, _) -> return ()
        (Connected, _) -> outProc OutputChildInfo (gdbProcess gdb) "result connected"
        (Error, _) -> outProc OutputError (gdbProcess gdb) $ "command error: " <> cmd
        (Exit, _) -> outProc OutputError (gdbProcess gdb) "result exit"

gdbSession :: MonadOutput m => GDB -> m ()
gdbSession gdb = loop ""
  where
    loop prev = outPromptGetLine "gdb> " >>= \case
        Just line -> do
            let cmd = if T.null line then prev else line
            gdbCommand gdb ("-interpreter-exec console \"" <> cmd <> "\"")
            loop cmd
        Nothing -> return ()


type MiParser = ParsecT Void MiStream Identity

type MiStream = Text

miOutputRecord :: MiParser MiRecord
miOutputRecord = choice
    [ miResultRecord
    , miExecAsync
    , miStatusAsync
    , miNotifyAsync
    , miConsoleStream
    , miTargetStream
    , miLogStream
    ]

miResultRecord :: MiParser MiRecord
miResultRecord = char '^' >> ResultRecord <$> resultClass <*> many result

miExecAsync, miStatusAsync, miNotifyAsync :: MiParser MiRecord
miExecAsync = char '*' >> ExecAsyncOutput <$> miString <*> many (char ',' *> result)
miStatusAsync = char '+' >> StatusAsyncOutput <$> miString <*> many (char ',' *> result)
miNotifyAsync = char '=' >> NotifyAsyncOutput <$> miString <*> many (char ',' *> result)

miConsoleStream, miTargetStream, miLogStream :: MiParser MiRecord
miConsoleStream = char '~' >> ConsoleStreamOutput <$> miCString
miTargetStream = char '@' >> TargetStreamOutput <$> miCString
miLogStream = char '&' >> LogStreamOutput <$> miCString

resultClass :: MiParser ResultClass
resultClass = label "result-class" $ choice
    [ return Done <* string "done"
    , return Done <* string "running" -- equivalent to "done" per documentation
    , return Connected <* string "connected"
    , return Error <* string "error"
    , return Exit <* string "exit"
    ]

result :: MiParser (Text, MiValue)
result = (,) <$> miString <* char '=' <*> miValue

miString :: MiParser Text
miString = label "string" $ takeWhile1P Nothing (\x -> isAlphaNum x || x == '_' || x == '-')

miCString :: MiParser Text
miCString = label "c-string" $ do
    void $ char '"'
    let go = choice
            [ char '"' >> return []
            , takeWhile1P Nothing (`notElem` ['\"', '\\']) >>= \s -> (s:) <$> go
            ,do void $ char '\\'
                c <- choice
                    [ char '\\' >> return '\\'
                    , char '"' >> return '"'
                    , char 'n' >> return '\n'
                    , char 'r' >> return '\r'
                    , char 't' >> return '\t'
                    ]
                ((T.singleton c) :) <$> go
            ]
    T.concat <$> go

listOf :: MiParser a -> MiParser [a]
listOf item = do
    x <- item
    (x:) <$> choice [ char ',' >> listOf item, return [] ]

miTuple :: MiParser [(Text, MiValue)]
miTuple = between (char '{') (char '}') $ listOf result <|> return []

miList :: MiParser [MiValue]
miList = between (char '[') (char ']') $ listOf miValue <|> return []

miValue :: MiParser MiValue
miValue = choice
    [ MiString <$> miCString
    , MiTuple <$> miTuple
    , MiList <$> miList
    ]