-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.py
More file actions
448 lines (382 loc) · 16.1 KB
/
debug.py
File metadata and controls
448 lines (382 loc) · 16.1 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import sublime_plugin
import sublime
import json
import random
import xmlrpc.client
from http.client import CannotSendRequest, ResponseNotReady
import threading
from contextlib import contextmanager
import os
import xmlrpc.client
from time import sleep
from subprocess import Popen
from datetime import datetime
debuggers = {} # key = window.id, value lldb proxy
output_callbacks = {} # key = window.id, value set of callback funcs
status_callbacks = {} # key = window.id, value set of callback funcs
debug_status = {}
def plugin_loaded():
global settings
settings = sublime.load_settings('SublimeAnarchyDebug.sublime-settings')
def plugin_unloaded():
for key, debugger in debuggers.items():
debugger.shutdown_server()
@contextmanager
def retry():
while True:
try:
yield
except CannotSendRequest:
sleep(0.2)
continue
except ResponseNotReady:
sleep(0.2)
continue
break
# lldb query functions
def lldb_update_status(window):
lldb = debuggers[window.id()]
with retry():
try:
status = lldb.get_status()
except xmlrpc.client.Fault:
status = None
except ConnectionRefusedError:
status = "LLDB exited"
if window.id() not in debug_status:
debug_status[window.id()] = "unknown"
if status != debug_status[window.id()]:
print("state change", debug_status[window.id()], '->', status)
debug_status[window.id()] = status
for callback in status_callbacks[window.id()]:
try:
callback(window, status)
except Exception as e:
print('Exception', e)
def lldb_update_console(window):
lldb = debuggers[window.id()]
with retry():
stdout_buffer = lldb.get_stdout()
if stdout_buffer is not None and len(stdout_buffer) > 0:
for callback in output_callbacks[window.id()]:
try:
callback(window, stdout_buffer)
except Exception:
pass
# default callbacks for query functions
def main_output_callback(window, output_buffer):
pass
def main_status_callback(window, status):
if not status:
for view in window.views():
view.erase_status('lldb')
return
lldb = debuggers[window.id()]
for view in window.views():
view.set_status('lldb', 'LLDB: ' + status)
if status.startswith('stopped') or status.startswith('crashed') or status.startswith('plan_complete'):
update_run_marker(window, lldb=lldb)
if status.startswith('exited'):
lldb.shutdown_server()
def debugger_thread(p, port, window):
global settings
sleep(0.5)
project_settings = window.project_data().get('settings', {}).get('SublimeAnarchyDebug', {}).get('debug', {})
lldb = xmlrpc.client.ServerProxy('http://localhost:' + str(port), allow_none=True)
project_path = os.path.dirname(window.project_file_name())
with retry():
lldb.prepare(
project_settings.get('executable').replace('${project_path}', project_path),
project_settings.get('params', []),
project_settings.get('environment', None),
project_settings.get('path', None),
project_settings.get('working_dir', project_path).replace('${project_path}', project_path)
)
debuggers[window.id()] = lldb
status_callbacks[window.id()] = set()
status_callbacks[window.id()].add(main_status_callback)
output_callbacks[window.id()] = set()
output_callbacks[window.id()].add(main_output_callback)
# start the app
status = "unknown"
while status not in ["stopped,signal", "stopped,breakpoint"]:
with retry():
status = lldb.get_status()
sleep(0.5)
if settings.get('auto_show_lldb_console', True):
window.run_command('atdebug_console', { "show": True })
# load saved breakpoints
atlldb.load_breakpoints(window, lldb)
with retry():
lldb.start()
# polling loop
debug_status[window.id()] = "stopped,signal"
try:
while True:
sleep(1)
lldb_update_status(window)
lldb_update_console(window)
except Exception as e:
print("exception", e)
except ConnectionRefusedError:
print("LLDB Debug server down")
_kill_lldb(window)
if p:
p.wait()
def _kill_lldb(window):
# so the debug server exited or crashed
if window.id() in debuggers:
del debuggers[window.id()]
if window.id() in debug_status:
del debug_status[window.id()]
if window.id() in status_callbacks:
del status_callbacks[window.id()]
if window.id() in output_callbacks:
del output_callbacks[window.id()]
for view in window.views():
view.erase_status('lldb')
update_run_marker(view.window())
window.run_command('atdebug_console', { "show": False })
class atdebug(sublime_plugin.WindowCommand):
def _start_debugger(self):
self._stop_debugger()
path = os.path.dirname(self.window.project_file_name())
port = random.randint(12000,13000)
#port = 12345
lldb_server_executable = os.path.join(sublime.packages_path(), "SublimeAnarchyDebug", "lldb_bridge", "lldb_server.py")
args = ['/usr/bin/python', lldb_server_executable, settings.get('lldb_python_path'), str(port)]
p = Popen(args, cwd=path)
#p = None
threading.Thread(target=debugger_thread, name='debugger_thread', args=(p, port, self.window)).start()
def _stop_debugger(self):
lldb = debuggers.get(self.window.id(), None)
if lldb:
with retry():
try:
lldb.shutdown_server()
except ConnectionRefusedError:
_kill_lldb(self.window)
def run(self, *args, **kwargs):
if kwargs.get('start', False):
self._start_debugger()
if kwargs.get('stop', False):
self._stop_debugger()
action = kwargs.get('action', 'nop')
with retry():
lldb = debuggers.get(self.window.id(), None)
if not lldb:
return
if action == 'nop':
return
elif action == 'continue':
debug_status[self.window.id()] = "running"
lldb.start()
elif action == 'pause':
lldb.pause()
elif action == 'step_into':
debug_status[self.window.id()] = "stepping"
lldb.step_into()
elif action == 'step_over':
debug_status[self.window.id()] = "stepping"
lldb.step_over()
elif action == 'step_out':
debug_status[self.window.id()] = "stepping"
lldb.step_out()
elif action == 'stop':
lldb.stop()
update_run_marker(self.window, lldb=lldb)
def is_enabled(self, *args, **kwargs):
if not self.window.project_file_name():
return False
if kwargs.get('start', False) and debuggers.get(self.window.id(), None) == None:
return True
if kwargs.get('stop', False) and debuggers.get(self.window.id(), None) != None:
return True
if kwargs.get('action', None) and debuggers.get(self.window.id(), None) != None:
return True
return False
class atlldb(sublime_plugin.TextCommand):
@staticmethod
def save_breakpoints(window, lldb=None, breakpoints=None):
project_data = window.project_data()
if lldb:
with retry():
breakpoints = lldb.get_breakpoints()
for bp in breakpoints:
del bp['id']
if 'settings' not in project_data:
project_data['settings'] = {}
if 'SublimeAnarchyDebug' not in project_data['settings']:
project_data['settings']['SublimeAnarchyDebug'] = {}
project_data['settings']['SublimeAnarchyDebug']['breakpoints'] = breakpoints
window.set_project_data(project_data)
@staticmethod
def load_breakpoints(window, lldb):
breakpoints = window.project_data().get('settings', {}).get('SublimeAnarchyDebug', {}).get('breakpoints', [])
with retry():
lldb.delete_all_breakpoints()
for bp in breakpoints:
with retry():
bp_id = lldb.set_breakpoint(bp['file'], bp['line'], bp['condition'], bp['ignore_count'])
if not bp['enabled']:
with retry():
lldb.disable_breakpoint(bp_id)
def _disable_breakpoint(self, lldb, bp):
with retry():
breakpoints = lldb.get_breakpoints()
for lldb_bp in breakpoints:
if lldb_bp['file'] == bp['file'] and lldb_bp['line'] == bp['line']:
with retry():
lldb.disable_breakpoint(lldb_bp['id'])
self.save_breakpoints(self.view.window(), lldb=lldb)
def _enable_breakpoint(self, lldb, bp):
with retry():
breakpoints = lldb.get_breakpoints()
for lldb_bp in breakpoints:
if lldb_bp['file'] == bp['file'] and lldb_bp['line'] == bp['line']:
with retry():
lldb.enable_breakpoint(lldb_bp['id'])
self.save_breakpoints(self.view.window(), lldb=lldb)
def _create_breakpoint(self, lldb, file, line):
with retry():
lldb.set_breakpoint(file, line, None, 0)
self.save_breakpoints(self.view.window(), lldb=lldb)
def _remove_breakpoint(self, lldb, bp):
with retry():
breakpoints = lldb.get_breakpoints()
for lldb_bp in breakpoints:
if lldb_bp['file'] == bp['file'] and lldb_bp['line'] == bp['line']:
with retry():
lldb.delete_breakpoint(lldb_bp['id'])
self.save_breakpoints(self.view.window(), lldb=lldb)
def toggle_breakpoint(self, lldb):
breakpoints = self.view.window().project_data().get('settings', {}).get('SublimeAnarchyDebug', {}).get('breakpoints', [])
cursor = self.view.sel()[0].begin()
row, col = self.view.rowcol(cursor)
found = []
new_bps = []
for bp in breakpoints:
if bp['file'] == self.view.file_name() and bp['line'] == row:
if lldb:
self._remove_breakpoint(lldb, bp)
found.append(bp)
if len(found) == 0:
breakpoints.append({
"file": self.view.file_name(),
"line": row,
"enabled": True,
"condition": None,
"ignore_count": 0
})
if lldb:
self._create_breakpoint(lldb, self.view.file_name(), row)
else:
for bp in found:
breakpoints.remove(bp)
if not lldb:
self.save_breakpoints(self.view.window(), breakpoints=breakpoints)
update_markers(self.view)
def enable_disable_breakpoint(self, lldb):
breakpoints = self.view.window().project_data().get('settings', {}).get('SublimeAnarchyDebug', {}).get('breakpoints', [])
cursor = self.view.sel()[0].begin()
row, col = self.view.rowcol(cursor)
found = False
for bp in breakpoints:
if bp['file'] == self.view.file_name() and bp['line'] == row and bp['enabled'] == True:
bp['enabled'] = False
if lldb:
self._disable_breakpoint(lldb, bp)
found = True
elif bp['file'] == self.view.file_name() and bp['line'] == row and bp['enabled'] == False:
bp['enabled'] = True
if lldb:
self._enable_breakpoint(lldb, bp)
found = True
if not lldb:
self.save_breakpoints(self.view.window(), breakpoints=breakpoints)
update_markers(self.view)
def run(self, *args, **kwargs):
lldb = debuggers.get(self.view.window().id(), None)
if kwargs.get('toggle_breakpoint', False):
self.toggle_breakpoint(lldb)
if kwargs.get('enable_disable_breakpoint', False):
self.enable_disable_breakpoint(lldb)
def is_enabled(self, *args, **kwargs):
if "source.swift" in self.view.scope_name(0) and self.view.window().project_file_name():
return False
# only show enable/disable when there is a breakpoint
if kwargs.get('enable_disable_breakpoint', False):
breakpoints = self.view.window().project_data().get('settings', {}).get('SublimeAnarchyDebug', {}).get('breakpoints', [])
cursor = self.view.sel()[0].begin()
row, col = self.view.rowcol(cursor)
new_bps = []
for bp in breakpoints:
if bp['file'] == self.view.file_name() and bp['line'] == row:
return True
return False
return True
class LLDBBreakPointHighlighter(sublime_plugin.EventListener):
def enable(self, view):
if not view: return False
if "source.swift" not in view.scope_name(0): return False
return True
def on_activated(self, view):
if not self.enable(view):
return
update_markers(view)
def update_breakpoint_marker(view):
breakpoints = view.window().project_data().get('settings', {}).get('SublimeAnarchyDebug', {}).get('breakpoints', [])
enabled_markers = []
disabled_markers = []
for bp in breakpoints:
if bp['file'] == view.file_name():
location = view.line(view.text_point(bp['line'], 0))
if bp['enabled']:
enabled_markers.append(location)
else:
disabled_markers.append(location)
view.add_regions("breakpoint_enabled", enabled_markers, "breakpoint_enabled", "Packages/SublimeAnarchyDebug/images/breakpoint_enabled.png", sublime.HIDDEN)
view.add_regions("breakpoint_disabled", disabled_markers, "breakpoint_disabled", "Packages/SublimeAnarchyDebug/images/breakpoint_disabled.png", sublime.HIDDEN)
def update_run_marker(window, lldb=None):
if not lldb:
for view in window.views():
view.erase_regions("run_pointer")
return
with retry():
try:
bt = lldb.get_backtrace_for_selected_thread()
if 'bt' not in bt:
for view in window.views():
view.erase_regions("run_pointer")
return
for frame in bt['bt']:
if 'file' in frame and frame['line'] != 0:
found = False
for view in window.views():
if view.file_name() == frame['file']:
location = view.line(view.text_point(frame['line'] - 1, 0))
view.add_regions("run_pointer", [location], "entity.name.class", "Packages/SublimeAnarchyDebug/images/stop_point.png", sublime.DRAW_NO_FILL)
if not view.visible_region().contains(location):
view.show_at_center(location)
if window.active_group() == 0:
window.focus_view(view)
found = True
if not found:
grp = window.active_group()
window.focus_group(0)
view = window.open_file(frame['file'] + ":" + str(frame['line']), sublime.ENCODED_POSITION)
window.focus_group(grp)
location = view.line(view.text_point(frame['line'] - 1, 0))
view.add_regions("run_pointer", [location], "entity.name.class", "Packages/SublimeAnarchyDebug/images/stop_point.png", sublime.DRAW_NO_FILL)
if not view.visible_region().contains(location):
view.show_at_center(location)
break
except xmlrpc.client.Fault:
for view in window.views():
view.erase_regions("run_pointer")
def update_markers(view):
update_breakpoint_marker(view)
lldb = debuggers.get(view.window().id(), None)
update_run_marker(view.window(), lldb=lldb)
if lldb:
lldb_update_status(view.window())