-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
debug_server.py
353 lines (260 loc) · 12.2 KB
/
debug_server.py
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# -*- coding: utf-8 -*-
#
# gdb-frontend is a easy, flexible and extensionable gui debugger
#
# https://github.com/rohanrhu/gdb-frontend
# https://oguzhaneroglu.com/projects/gdb-frontend/
#
# Licensed under GNU/GPLv3
# Copyright (C) 2019, Oğuzhan Eroğlu (https://oguzhaneroglu.com/) <[email protected]>
import importlib
import json
import time
import config
config.init()
import plugin
import util
import api.debug
import api.collabration
import api.flags
import api.globalvars
import api.collabration
import websocket
import terminal_daemon
gdb = importlib.import_module("gdb")
api.globalvars.init()
plugin.init()
api.collabration.init()
class GDBFrontendSocket(websocket.WebSocketHandler):
cont_time = False
screen_resolution = [0, 0]
terminalDaemon = False
def __init__(self, request, client_address, server):
websocket.WebSocketHandler.__init__(self, request, client_address, server)
def handleConnection(self):
util.verbose(self.client_address[0], "is connected.")
self.server.ws_clients.append(self)
self.connectGDBEvents()
def connectGDBEvents(self):
gdb.events.new_objfile.connect(self.gdb_on_new_objfile)
gdb.events.clear_objfiles.connect(self.gdb_on_clear_objfiles)
gdb.events.breakpoint_created.connect(self.gdb_on_breakpoint_created)
gdb.events.breakpoint_modified.connect(self.gdb_on_breakpoint_modified)
gdb.events.breakpoint_deleted.connect(self.gdb_on_breakpoint_deleted)
gdb.events.stop.connect(self.gdb_on_stop)
gdb.events.new_thread.connect(self.gdb_on_new_thread)
gdb.events.cont.connect(self.gdb_on_cont)
gdb.events.exited.connect(self.gdb_on_exited)
gdb.events.inferior_deleted.connect(self.gdb_on_inferior_deleted)
gdb.events.new_inferior.connect(self.gdb_on_new_inferior)
def disconnectGDBEvents(self):
gdb.events.new_objfile.disconnect(self.gdb_on_new_objfile)
gdb.events.clear_objfiles.disconnect(self.gdb_on_clear_objfiles)
gdb.events.breakpoint_created.disconnect(self.gdb_on_breakpoint_created)
gdb.events.breakpoint_modified.disconnect(self.gdb_on_breakpoint_modified)
gdb.events.breakpoint_deleted.disconnect(self.gdb_on_breakpoint_deleted)
gdb.events.stop.disconnect(self.gdb_on_stop)
gdb.events.new_thread.disconnect(self.gdb_on_new_thread)
gdb.events.cont.disconnect(self.gdb_on_cont)
gdb.events.exited.disconnect(self.gdb_on_exited)
gdb.events.inferior_deleted.disconnect(self.gdb_on_inferior_deleted)
gdb.events.new_inferior.disconnect(self.gdb_on_new_inferior)
def gdb_on_new_objfile(self, event):
util.verbose("gdb_on_new_objfile()")
def _mt():
self.gdb_on_new_objfile__mT(event)
gdb.post_event(_mt)
def gdb_on_new_objfile__mT(self, event):
response = {}
response["event"] = "new_objfile"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_clear_objfiles(self, event):
util.verbose("gdb_on_clear_objfiles()")
if api.globalvars.dont_emit_until_stop_or_exit: return
gdb.post_event(self.gdb_on_clear_objfiles__mT)
def gdb_on_clear_objfiles__mT(self):
response = {}
response["event"] = "clear_objfiles"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_breakpoint_created(self, event):
util.verbose("gdb_on_breakpoint_created()")
gdb.post_event(self.gdb_on_breakpoint_created__mT)
def gdb_on_breakpoint_created__mT(self):
interrupted = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_ADD)
if interrupted:
pass
else:
response = {}
response["event"] = "breakpoint_created"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_breakpoint_modified(self, event):
util.verbose("gdb_on_breakpoint_modified()")
gdb.post_event(self.gdb_on_breakpoint_modified__mT)
def gdb_on_breakpoint_modified__mT(self):
interrupted = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_MOD)
interrupted = interrupted or api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_SET)
if interrupted:
pass
else:
response = {}
response["event"] = "breakpoint_modified"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_breakpoint_deleted(self, event):
util.verbose("gdb_on_breakpoint_deleted()")
if api.globalvars.dont_emit_until_stop_or_exit: return
gdb.post_event(self.gdb_on_breakpoint_deleted__mT)
def gdb_on_breakpoint_deleted__mT(self):
interrupted = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_DEL)
if interrupted:
pass
else:
response = {}
response["event"] = "breakpoint_deleted"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_stop(self, event):
util.verbose("gdb_on_stop()")
gdb.post_event(self.gdb_on_stop__mT)
def gdb_on_stop__mT(self):
interrupted_for_thread_safety = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_THREAD_SAFETY)
interrupted_for_terminate = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_TERMINATE)
interrupted_for_signal = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_SIGNAL)
interrupted_for_breakpoint_add = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_ADD)
interrupted_for_breakpoint_del = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_DEL)
interrupted_for_breakpoint_mod = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_MOD)
interrupted_for_breakpoint_set = api.globalvars.debugFlags.get(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_SET)
if interrupted_for_thread_safety:
pass
elif interrupted_for_terminate:
pass
elif interrupted_for_signal:
pass
elif interrupted_for_breakpoint_add:
pass
elif interrupted_for_breakpoint_del:
pass
elif interrupted_for_breakpoint_mod:
pass
elif interrupted_for_breakpoint_set:
pass
else:
response = {}
response["event"] = "stop"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_new_thread(self, event):
util.verbose("gdb_on_new_thread()")
def _mt():
self.gdb_on_new_thread__mT(event)
gdb.post_event(_mt)
def gdb_on_new_thread__mT(self, event):
response = {}
response["event"] = "new_thread"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_cont(self, event):
util.verbose("gdb_on_cont()")
self.cont_time = time.time() * 1000
if api.globalvars.dont_emit_until_stop_or_exit: return
gdb.post_event(self.gdb_on_cont__mT)
def gdb_on_cont__mT(self):
util.verbose("gdb_on_cont__mT()")
response = {}
response["event"] = "cont"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_exited(self, event):
util.verbose("gdb_on_exited()")
def _mt():
self.gdb_on_exited__mT(event)
gdb.post_event(_mt)
def gdb_on_exited__mT(self, event):
response = {}
response["event"] = "exited"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_new_inferior(self, event):
util.verbose("gdb_on_new_inferior()")
if api.globalvars.dont_emit_until_stop_or_exit: return
def _mt():
self.gdb_on_new_inferior__mT(event)
gdb.post_event(_mt)
def gdb_on_new_inferior__mT(self, event):
response = {}
response["event"] = "new_inferior"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def gdb_on_inferior_deleted(self, event):
util.verbose("gdb_on_inferior_deleted()")
def _mt():
self.gdb_on_inferior_deleted__mT(event)
gdb.post_event(_mt)
def gdb_on_inferior_deleted__mT(self, event):
response = {}
if api.globalvars.dont_emit_until_stop_or_exit: return
response["event"] = "inferior_deleted"
response["state"] = api.debug.getState()
self.wsSend(json.dumps(response))
def handleClose(self):
util.verbose(self.client_address[0], "is disconnected.")
if self.terminalDaemon:
self.terminalDaemon.stop()
if self in self.server.ws_clients:
self.server.ws_clients.remove(self)
self.disconnectGDBEvents()
def emit(self, event, message={}):
message["event"] = event
self.wsSend(json.dumps(message))
def handleMessage(self):
if self.terminalDaemon and self.terminalDaemon.handleMessage():
return
message = json.loads(self.message)
event = message["event"]
if event == "start_terminal":
util.verbose("Starting terminal daemon for client#%d" % self.client_id)
tmux_cmd = ["tmux", "a", "-t", config.TERMINAL_ID]
if config.WORKDIR:
tmux_cmd.append("-c")
tmux_cmd.append(config.WORKDIR)
self.terminalDaemon = terminal_daemon.TerminalDaemon(ws=self, terminal_command=tmux_cmd)
self.terminalDaemon.start()
elif event == "get_state":
self.emit(message["return_event"], {
"state": api.debug.getState()
})
elif event == "get_sources":
self.emit(message["return_event"], {
"state": {
"sources": api.debug.getSources()
}
})
elif event == "get_registers":
self.emit(message["return_event"], {
"registers": api.debug.getRegisters()
})
elif event == "signal":
api.debug.signal(message["signal"])
self.emit(message["return_event"], {})
elif event == "enhanced_collabration_enable":
api.collabration.enableEnhancedCollabration()
elif event == "enhanced_collabration_disable":
api.collabration.disableEnhancedCollabration()
elif event == "collabration_state":
api.collabration.setState(message["state"], client_id=self.client_id)
elif event == "collabration_state__scroll":
api.collabration.setState__scroll(message["scroll_position"], client_id=self.client_id)
elif event == "collabration_state__cursor":
api.collabration.setState__cursor(message["cursor_position"], client_id=self.client_id)
elif event == "collabration_state__watches":
api.collabration.setState__watches(message["watches"], client_id=self.client_id)
elif event == "collabration_state__draw_path":
api.collabration.setState__draw_path(message["path"], client_id=self.client_id)
elif event == "collabration_state__draw_clear":
api.collabration.setState__draw_clear(client_id=self.client_id)
for _plugin_name, _plugin in plugin.plugins.items():
if hasattr(_plugin, "event"):
_plugin.event(self, event, message)