forked from snare/voltron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbgentry.py
280 lines (229 loc) · 9.97 KB
/
dbgentry.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
try:
import logging
blessed = None
import blessed
import voltron
from voltron.core import Server
from voltron.plugin import PluginManager
try:
import lldb
in_lldb = True
except ImportError:
in_lldb = False
try:
import gdb
in_gdb = True
except ImportError:
in_gdb = False
if "vtrace" in locals():
in_vdb = True
import os
import sys
def parent_directory(the_path):
return os.path.abspath(os.path.join(the_path, os.pardir))
def add_vdb_to_path(vtrace):
sys.path.append(parent_directory(parent_directory(vtrace.__file__)))
# don't pass over this line!
# in order for the *VDB adaptor plugin* (not this file) to import
# vdb stuff, the import path must be updated.
# this is it.
#
# since atm vdb imports everything relatively
# (typically its not installed), then we use this
# hack to extract the package that's in use.
add_vdb_to_path(vtrace)
import vtrace
else:
in_vdb = False
voltron.setup_env()
log = voltron.setup_logging('debugger')
class VoltronCommand (object):
"""
Parent class for common methods across all debugger hosts.
"""
def handle_command(self, command):
global log
if "status" in command:
self.status()
elif 'debug' in command:
if 'enable' in command:
log.setLevel(logging.DEBUG)
print("Debug logging enabled")
elif 'disable' in command:
log.setLevel(logging.INFO)
print("Debug logging disabled")
else:
enabled = "enabled" if log.getEffectiveLevel() == logging.DEBUG else "disabled"
print("Debug logging is currently " + enabled)
elif 'init' in command:
self.register_hooks()
elif 'stopped' in command or 'update' in command:
self.adaptor.update_state()
else:
print("Usage: voltron <init|status|debug|update>")
def status(self):
if self.server != None:
summs = self.server.client_summary()
print("The following listeners are active:")
listen = voltron.config['server']['listen']
if listen['domain']:
print(" domain socket ({})".format(voltron.env['sock']))
if listen['tcp']:
print(" TCP socket ({})".format(listen['tcp']))
if listen['http']:
print(" web server ({})".format(listen['http']))
print("There are {} clients attached:".format(len(summs)))
for summary in summs:
print(" " + summary)
else:
print("Server is not running (no inferior)")
if in_lldb:
class VoltronLLDBCommand (VoltronCommand):
"""
Debugger command class for LLDB
"""
def __init__(self, debugger, dict):
super(VoltronCommand, self).__init__()
# grab the debugger and command interpreter
self.debugger = debugger
self.ci = self.debugger.GetCommandInterpreter()
# install the voltron command handler
self.debugger.HandleCommand('command script add -f dbgentry.lldb_invoke voltron')
# load plugins
self.pm = PluginManager()
# set up an lldb adaptor and set it as the package-wide adaptor
self.adaptor = self.pm.debugger_plugin_for_host('lldb').adaptor_class()
voltron.debugger = self.adaptor
# start the server
self.server = Server()
self.server.start()
self.hook_idx = None
def invoke(self, debugger, command, result, dict):
self.handle_command(command)
def register_hooks(self):
try:
output = self.adaptor.command("target stop-hook list")
if not 'voltron' in output:
output = self.adaptor.command('target stop-hook add -o \'voltron stopped\'')
try:
# hahaha this sucks
self.hook_idx = int(res.GetOutput().strip().split()[2][1:])
except:
pass
print("Registered stop-hook")
except:
print("No targets")
def unregister_hooks(self):
cmd = 'target stop-hook delete {}'.format(self.hook_idx if self.hook_idx else '')
self.debugger.HandleCommand(cmd)
def __lldb_init_module(debugger, env_dict):
"""
Called by LLDB when the module is loaded
"""
if not 'cmd' in env_dict:
log.debug("Initialising LLDB command")
env_dict['cmd'] = VoltronLLDBCommand(debugger, env_dict)
print(blessed.Terminal().bold_red("Voltron loaded."))
print("Run `voltron init` after you load a target.")
def lldb_invoke(debugger, command, result, env_dict):
"""
Called when the voltron command is invoked within LLDB
"""
env_dict['cmd'].invoke(debugger, command, result, env_dict)
if in_gdb:
class VoltronGDBCommand (VoltronCommand, gdb.Command):
"""
Debugger command class for GDB
"""
def __init__(self):
super(VoltronCommand, self).__init__("voltron", gdb.COMMAND_NONE, gdb.COMPLETE_NONE)
# load plugins
self.pm = PluginManager()
# set up a gdb adaptor and set it as the package-wide adaptor
self.adaptor = self.pm.debugger_plugin_for_host('gdb').adaptor_class()
voltron.debugger = self.adaptor
# server is started and stopped with the inferior to avoid GDB hanging on exit
self.server = None
def invoke(self, arg, from_tty):
self.handle_command(arg)
def register_hooks(self):
gdb.events.stop.connect(self.stop_handler)
gdb.events.exited.connect(self.exit_handler)
gdb.events.cont.connect(self.cont_handler)
def unregister_hooks(self):
gdb.events.stop.disconnect(self.stop_handler)
gdb.events.exited.disconnect(self.exit_handler)
gdb.events.cont.disconnect(self.cont_handler)
def stop_handler(self, event):
self.adaptor.update_state()
log.debug('Inferior stopped')
def exit_handler(self, event):
log.debug('Inferior exited')
self.server.stop()
def cont_handler(self, event):
log.debug('Inferior continued')
if self.server == None:
self.server = Server()
self.server.start()
if __name__ == "__main__":
log.debug('Initialising GDB command')
inst = VoltronGDBCommand()
print(blessed.Terminal().bold_red("Voltron loaded."))
if in_vdb:
class VoltronVDBCommand(VoltronCommand, vtrace.Notifier):
"""
Debugger command class for VDB
"""
def __init__(self, vdb, vtrace):
"""
vdb is the debugger instance
vtrace is the vtrace module?
"""
super(VoltronCommand, self).__init__()
self._vdb = vdb
self._vtrace = vtrace
self.pm = PluginManager()
self.adaptor = self.pm.debugger_plugin_for_host('vdb').adaptor_class(self._vdb, self._vtrace)
voltron.debugger = self.adaptor
self.server = Server()
self.server.start()
def invoke(self, arg, from_tty):
self.handle_command(arg)
def register_hooks(self):
self._vdb.registerNotifier(vtrace.NOTIFY_ALL, self)
def unregister_hooks(self):
self._vdb.deregisterNotifier(vtrace.NOTIFY_ALL, self)
def notify(self, event, trace):
if event == self._vtrace.NOTIFY_DETACH:
self.exit_handler(event)
elif event == self._vtrace.NOTIFY_EXIT:
self.exit_handler(event)
elif event == self._vtrace.NOTIFY_BREAK:
self.stop_handler(event)
elif event == self._vtrace.NOTIFY_STEP:
self.stop_handler(event)
elif event == self._vtrace.NOTIFY_CONTINUE:
self.cont_handler(event)
def stop_handler(self, event):
self.adaptor.update_state()
log.debug('Inferior stopped')
def exit_handler(self, event):
log.debug('Inferior exited')
self.server.stop()
# vdb doesn't signal STOP/BREAK on exit, so we
# clear an outstanding Wait requests
self.adaptor.update_state()
def cont_handler(self, event):
log.debug('Inferior continued')
# wb: i have no idea if this __name__ test is actually correct
# but __builtin__ is its value when run from vdbbin
if __name__ == "__builtin__":
log.debug('Initialising VDB command')
inst = VoltronVDBCommand(db, vtrace)
inst.register_hooks()
print(blessed.Terminal().bold_red("Voltron loaded."))
except Exception, e:
msg = "Exception {} raised while loading Voltron: {}".format(type(e), str(e))
if blessed:
msg = blessed.Terminal().bold_red(msg)
print(msg)