-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
debug_events.py
293 lines (213 loc) · 11.5 KB
/
debug_events.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
# -*- 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 traceback
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 GDBFrontendDebugEventsHandler:
cont_time = False
def __init__(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("GDBFrontendDebugEventsHandler", "gdb_on_new_objfile()")
api.globalvars.inferior_run_times[gdb.selected_inferior().num] = int(time.time())
def _mt():
self.gdb_on_new_objfile__mT(event)
gdb.post_event(_mt)
def gdb_on_new_objfile__mT(self, event):
pass
def gdb_on_clear_objfiles(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "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):
pass
def gdb_on_breakpoint_created(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "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)
def gdb_on_breakpoint_modified(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "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)
def gdb_on_breakpoint_deleted(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "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)
def gdb_on_stop(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "gdb_on_stop()")
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.IS_RUNNING, False)
api.globalvars.dont_emit_until_stop_or_exit = False
api.globalvars.step_time = time.time() * 1000 - self.cont_time
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:
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_THREAD_SAFETY, False)
util.verbose("GDBFrontendDebugEventsHandler", "Continuing for interrupt: THREAD_SAFETY.")
try:
gdb.execute("c")
except Exception as e:
print("[Error] (GDBFrontendSocket.gdb_on_stop__mT)", e)
elif interrupted_for_terminate:
util.verbose("GDBFrontendDebugEventsHandler", "Terminating for interrupt: TERMINATE.")
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_TERMINATE, False)
try:
gdb.execute("kill")
except Exception as e:
print("[Error] (GDBFrontendSocket.gdb_on_stop__mT)", e)
elif interrupted_for_signal:
try:
util.verbose("GDBFrontendDebugEventsHandler", "Continuing for interrupt: SIGNAL.")
gdb.execute("c")
except Exception as e:
print("[Error] (GDBFrontendSocket().gdb_on_stop__mT)", e)
elif interrupted_for_breakpoint_add:
util.verbose("GDBFrontendDebugEventsHandler", "Continuing for interrupt: BREAKPOINT_ADD.")
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_ADD, False)
if "address" in interrupted_for_breakpoint_add:
bp = api.debug.Breakpoint(
address = interrupted_for_breakpoint_add["address"]
)
else:
bp = api.debug.Breakpoint(
source = interrupted_for_breakpoint_add["file"],
line = interrupted_for_breakpoint_add["line"]
)
try:
gdb.execute("c")
except Exception as e:
print("[Error] (GDBFrontendSocket().gdb_on_stop__mT)", e)
elif interrupted_for_breakpoint_del:
util.verbose("GDBFrontendDebugEventsHandler", "Continuing for interrupt: BREAKPOINT_DEL.")
interrupted_for_breakpoint_del.delete()
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_DEL, False)
try:
gdb.execute("c")
except Exception as e:
print("[Error] (GDBFrontendSocket().gdb_on_stop__mT)", e)
elif interrupted_for_breakpoint_mod:
util.verbose("GDBFrontendDebugEventsHandler", "Continuing for interrupt: BREAKPOINT_MOD.")
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_MOD, False)
interrupted_for_breakpoint_mod["breakpoint"].condition = interrupted_for_breakpoint_mod["condition"]
try:
gdb.execute("c")
except Exception as e:
print("[Error] (GDBFrontendSocket().gdb_on_stop__mT)", e)
elif interrupted_for_breakpoint_set:
util.verbose("GDBFrontendDebugEventsHandler", "Continuing for interrupt: BREAKPOINT_SET.")
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.IS_INTERRUPTED_FOR_BREAKPOINT_SET, False)
interrupted_for_breakpoint_set["breakpoint"].enabled = interrupted_for_breakpoint_set["is_enabled"]
try:
gdb.execute("c")
except Exception as e:
print("[Error] (GDBFrontendSocket().gdb_on_stop__mT)", e)
def gdb_on_new_thread(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "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):
inferior_thread = event.inferior_thread
if isinstance(inferior_thread, gdb.InferiorThread):
api.globalvars.inferior_run_times[event.inferior_thread.inferior.num] = int(time.time())
elif isinstance(inferior_thread, gdb.Inferior):
api.globalvars.inferior_run_times[event.inferior_thread.num] = int(time.time())
def gdb_on_cont(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "gdb_on_cont()")
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.IS_RUNNING, True)
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):
pass
def gdb_on_exited(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "gdb_on_exited()")
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.IS_RUNNING, False)
api.globalvars.dont_emit_until_stop_or_exit = False
api.globalvars.step_time = False
def _mt():
self.gdb_on_exited__mT(event)
gdb.post_event(_mt)
def gdb_on_exited__mT(self, event):
response = {}
try:
del api.globalvars.inferior_run_times[event.inferior.num]
except Exception as e:
util.verbose("GDBFrontendDebugEventsHandler", e, traceback.format_exc())
api.globalvars.debugFlags.set(api.flags.AtomicDebugFlags.SELECTED_FRAMES, {})
def gdb_on_new_inferior(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "gdb_on_new_inferior()")
api.globalvars.inferior_run_times[event.inferior.num] = int(time.time())
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):
pass
def gdb_on_inferior_deleted(self, event):
util.verbose("GDBFrontendDebugEventsHandler", "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 = {}
del api.globalvars.inferior_run_times[event.inferior.num]
if api.globalvars.dont_emit_until_stop_or_exit: return