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
|
{-# 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.Text (Text)
import Data.Text qualified as T
import Data.Text.IO qualified as T
import Data.Void
import Text.Megaparsec hiding (State)
import Text.Megaparsec.Char
import System.IO.Error
import System.Process
import Output
import Process
data GDB = GDB
{ gdbProcess_ :: Process
, gdbResult :: MVar (ResultClass, [(Text, MiValue)])
}
gdbProcess :: GDB -> Process
gdbProcess = gdbProcess_
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) => m GDB
gdbStart = 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
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 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 -> outProc OutputChildInfo (gdbProcess gdb) $ "exec: " <> cls <> " " <> T.pack (show params)
StatusAsyncOutput cls params -> outProc OutputChildInfo (gdbProcess gdb) $ "status: " <> cls <> " " <> T.pack (show params)
NotifyAsyncOutput cls params -> case cls of
"library-loaded" -> return ()
"library-unloaded" -> return ()
"cmd-param-changed" -> return ()
"thread-group-added" -> return ()
"thread-group-started" -> return ()
"thread-created" -> return ()
"thread-selected" -> return ()
_ -> outProc OutputChildInfo (gdbProcess gdb) $ "notify: " <> cls <> " " <> T.pack (show params)
ConsoleStreamOutput line -> mapM_ (outProc OutputChildStdout (gdbProcess gdb)) (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 -> Int -> Pid -> m ()
addInferior gdb i pid = do
gdbCommand gdb $ "-add-inferior"
gdbCommand gdb $ "-target-attach --thread-group i" <> T.pack (show i) <> " " <> T.pack (show pid)
gdbCommand gdb $ "-exec-continue --thread-group i" <> T.pack (show i)
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 = do
outPrompt "gdb> "
loop
outClearPrompt
where
loop = liftIO (catchIOError (Just <$> T.getLine) (\e -> if isEOFError e then return Nothing else ioError e)) >>= \case
Just line -> do
gdbCommand gdb ("-interpreter-exec console \"" <> line <> "\"")
loop
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
]
|