/
Responses.hs
195 lines (167 loc) · 5.36 KB
/
Responses.hs
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
module Ruab.Backend.GDB.Responses where
-- import {{{1
import Control.Applicative ((<$>), (<*>))
import Control.Monad (guard, msum, (<=<))
import Data.List (find)
import Ruab.Backend.GDB.Representation
-- types {{{1
type BkptNumber = Int
data Breakpoint = Breakpoint { -- {{{2
bkptNumber :: BkptNumber
, bkptType :: BreakpointType
, bkptDisp :: BreakpointDisp
, bkptEnabled :: Bool
, bkptAddress :: String
, bkptFunc :: String
, bkptFile :: String
, bkptFullname :: String
, bkptLine :: Int
, bkptTimes :: Int
, bkptOriginalLocation :: String
}
deriving Show
type BreakpointType = String -- {{{2
data BreakpointDisp -- {{{2
= BreakpointKeep
| BreakpointDel
deriving Show
instance Read BreakpointDisp where
readsPrec _ "del" = [(BreakpointDel, "")]
readsPrec _ "keep" = [(BreakpointKeep, "")]
readsPrec _ _ = []
newtype Stack -- {{{2
= Stack {stackFrames :: [Frame] }
deriving Show
data Frame = Frame { -- {{{2
frameLevel :: Maybe Int
, frameAddr :: String
, frameFunc :: String
, frameArgs :: Maybe [Arg]
, frameFile :: String
, frameFullname :: Maybe String
, frameLine :: Int
} deriving Show
data Stopped = Stopped { -- {{{2
stoppedReason :: StopReason
, stoppedFrame :: Frame
, stoppedThreadId :: Int
, stoppedThreads :: String
, stoppedCore :: Int
}
deriving Show
data StopReason -- {{{2
= BreakpointHit {
bkptHitDisp :: BreakpointDisp
, bkptHitNumber :: BkptNumber
}
| EndSteppingRange
| FunctionFinished
deriving Show
data Arg = Arg { -- {{{2
argName :: String
, argValue :: String
} deriving Show
-- composition {{{1
responseBreakpoint :: Result -> Maybe Breakpoint -- {{{2
responseBreakpoint (Result variable value) = do
guard (variable == "bkpt")
(Tuple rs) <- asTuple value
Breakpoint
<$> get rs tryRead "number"
<*> get rs Just "type"
<*> get rs tryRead "disp"
<*> get rs gdbBool "enabled"
<*> get rs Just "addr"
<*> get rs Just "func"
<*> get rs Just "file"
<*> get rs Just "fullname"
<*> get rs tryRead "line"
<*> get rs tryRead "times"
<*> get rs Just "original-location"
responseStack :: Result -> Maybe Stack -- {{{2
responseStack (Result variable value) = do
guard (variable == "stack")
list <- asList value
case list of
EmptyList -> Just $ Stack []
ResultList is ->
Stack <$> mapM responseFrame is
_ -> Nothing
responseFrame :: Result -> Maybe Frame -- {{{2
responseFrame (Result variable value) = do
guard (variable == "frame")
(Tuple rs) <- asTuple value
Frame
<$> Just (get rs tryRead "level")
<*> get rs Just "addr"
<*> get rs Just "func"
<*> Just (msum (map responseArgs rs))
<*> get rs Just "file"
<*> Just (get rs Just "fullname")
<*> get rs tryRead "line"
responseStopped :: [Result] -> Maybe Stopped -- {{{2
responseStopped rs = do
Stopped
<$> responseStopReason rs
<*> msum (map responseFrame rs)
<*> get rs tryRead "thread-id"
<*> get rs Just "stopped-threads"
<*> get rs tryRead "core"
responseStopReason :: [Result] -> Maybe StopReason -- {{{2
responseStopReason rs = do
reason <- find (("reason"==) . resVariable) rs >>= asConst . resValue
case reason of
"breakpoint-hit" ->
BreakpointHit
<$> get rs tryRead "disp"
<*> get rs tryRead "bkptno"
"end-stepping-range" -> Just EndSteppingRange
"function-finished" -> Just FunctionFinished
_ -> Nothing
responseArgs :: Result -> Maybe [Arg] -- {{{2
responseArgs (Result variable value) = do
guard (variable == "args")
list <- asList value
case list of
EmptyList -> Just []
ValueList is -> do
mapM ((responseArg . tupleResults) <=< asTuple) is
_ -> Nothing
responseArg :: [Result] -> Maybe Arg -- {{{2
responseArg rs = do
Arg
<$> get rs Just "name"
<*> get rs Just "value"
-- responses {{{1
response_stack_list_frames :: [Result] -> Maybe Stack -- {{{2
response_stack_list_frames [item] = responseStack item
response_stack_list_frames _ = Nothing
response_break_insert :: [Result] -> Maybe Breakpoint -- {{{2
response_break_insert [item] = responseBreakpoint item
response_break_insert _ = Nothing
response_data_evaluate_expression :: [Result] -> Maybe String -- {{{2
response_data_evaluate_expression [(Result variable value)] = do
guard (variable == "value")
asConst value
response_data_evaluate_expression _ = Nothing -- {{{2
response_error :: [Result] -> Maybe String -- {{{2
response_error [(Result variable value)] = do
guard (variable == "msg")
asConst value
response_error _ = Nothing
response_stopped :: [Result] -> Maybe Stopped -- {{{2
response_stopped items = responseStopped items
response_exec_return :: [Result] -> Maybe Frame -- {{{2
response_exec_return [item] = responseFrame item
response_exec_return _ = Nothing
-- utils {{{1
get :: [Result] -> (String -> Maybe a) -> (String -> Maybe a) -- {{{2
get rs parse key = find ((key==) . resVariable) rs >>= asConst . resValue >>= parse
tryRead :: Read a => String -> Maybe a -- {{{2
tryRead str = case readsPrec 0 str of
[(x, "")] -> Just x
_ -> Nothing
gdbBool :: String -> Maybe Bool -- {{{2
gdbBool "y" = Just True
gdbBool "n" = Just False
gdbBool _ = Nothing