-
Notifications
You must be signed in to change notification settings - Fork 0
/
lui.py
executable file
·182 lines (143 loc) · 4.87 KB
/
lui.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
#!/usr/bin/env python
##===-- lui.py -----------------------------------------------*- Python -*-===##
##
## The LLVM Compiler Infrastructure
##
## This file is distributed under the University of Illinois Open Source
## License. See LICENSE.TXT for details.
##
##===----------------------------------------------------------------------===##
import lldb
import lldbutil
import argparse
import os
import signal
import sys
import Queue
import debuggerdriver
import statuswin
import layout
import urwid
event_queue = None
debug = False
def test_a_out(driver):
driver.handleCommand('target create a.out')
driver.handleCommand('b main')
driver.handleCommand('run')
def handle_args(driver, argv):
parser = argparse.ArgumentParser(description='LLDB Terminal User Interface')
parser.add_argument("-p", "--attach", dest="pid", type=int,
help="Attach to specified Process ID")
parser.add_argument("-c", "--core",
help="Load specified core file")
parser.add_argument("-d", "--debug", action='store_true',
help="Enable lui debugging")
parser.add_argument("--layout",
help="Load layout from file")
parser.add_argument("--default_layout", action='store_true',
help="Dump default layout")
parser.add_argument('target', nargs='*',
help="debug target")
args = parser.parse_args()
if args.default_layout:
print layout.default_layout
sys.exit(0)
if args.layout:
with open(args.layout) as f:
json = f.read()
layout.load_layout(json)
else:
layout.load_layout()
global debug
debug = args.debug
if args.pid is not None:
driver.attachProcess(args.pid)
elif args.core is not None:
if not os.path.exists(args.core):
raise Exception("Specified core file '%s' does not exist." % args.core)
driver.loadCore(options.core)
elif len(args.target) > 0:
target = args.target[0]
target_args = args.target[1:]
if not os.path.isfile(target):
raise Exception("Specified target '%s' does not exist" % target)
driver.createTarget(target, target_args)
def sigint_handler(signal, frame):
global debugger
debugger.terminate()
class LLDBView(urwid.WidgetWrap):
palette = [('body', 'light gray', 'black', 'standout'),
('error', 'dark red', 'black', 'standout'),
('header', 'black', 'light gray', 'bold'),
('key', 'light cyan', 'black', 'underline'),
('title', 'white', 'black', 'bold'),
('running','light green','black', 'blink'),
('stopped','yellow', 'black', 'bold'),
('exited', 'dark red', 'black', 'bold'),
]
def __init__(self, event_queue, driver):
self.event_queue = event_queue
self.driver = driver
urwid.WidgetWrap.__init__(self, self.main_window())
def main_window(self):
self.status_win = statuswin.StatusWin(self.event_queue)
builder = layout.LayoutBuilder(self.event_queue, self.driver)
w = builder.build(layout.loaded_layout['default'])
self.frame = urwid.Frame(body = w, footer = urwid.AttrWrap(self.status_win, 'footer'))
return self.frame
class LLDBUI:
def __init__(self, event_queue, driver):
self.event_queue = event_queue
self.driver = driver
self.view = LLDBView(self.event_queue, self.driver)
def unhandled_input(self, k):
if k == 'f5':
self.driver.handleCommand('run')
if k == 'shift f5':
self.driver.handleCommand('continue')
if k == 'f10':
self.driver.terminate()
raise urwid.ExitMainLoop()
if k == 'f1':
test_a_out(self.driver)
def main(self):
loop = urwid.MainLoop(self.view, self.view.palette,
unhandled_input = self.unhandled_input)
fd = loop.watch_pipe(self.event_queue)
self.event_queue.set_pipe(fd)
loop.run()
class LLDBEventQueue:
def __init__(self):
self.queue = Queue.Queue()
self.listeners = []
self.fd = -1
def set_pipe(self, fd):
self.fd = fd
def add_listener(self, listener):
self.listeners.append(listener)
def put(self, event):
self.queue.put(event)
if self.fd != -1:
os.write(self.fd, '1')
def __call__(self, data):
while not self.queue.empty():
event = self.queue.get()
for listener in self.listeners:
listener.handle_lldb_event(event)
def main():
signal.signal(signal.SIGINT, sigint_handler)
global event_queue
event_queue = LLDBEventQueue()
global debugger
debugger = lldb.SBDebugger.Create()
driver = debuggerdriver.createDriver(debugger, event_queue)
handle_args(driver, sys.argv)
view = LLDBUI(event_queue, driver)
# start the driver thread
driver.start()
# hack to avoid hanging waiting for prompts!
driver.handleCommand("settings set auto-confirm true")
view.main()
if __name__ == "__main__":
print lldb
main()