-
Notifications
You must be signed in to change notification settings - Fork 1
/
daemon_logs.py
274 lines (220 loc) · 8.34 KB
/
daemon_logs.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
"""
Latch on to speechsynthesisd and stream its logs.
"""
import fcntl
from functools import partial
import os
from random import random
import sys
from threading import Event, Thread
import lldb # pylint: disable=E0401
TEMP_DIR = '(char *)[NSTemporaryDirectory() UTF8String]'
STDERR_SYMBOL = '__stderrp'
MACINTALK_MODULE = 'MacinTalk'
MEOW_DEBUG_HOOK = 'MTBEPhraseProcessor::GenerateSamples'
WORKER_DEBUG_HOOK = 'MTBEWorker::DebugLog'
MEOW_DEBUG_FLAG = 'MTBEDebugFlags::sMEOWDebug'
WORKER_DEBUG_FLAG = 'MTBEDebugFlags::sMTXDebug'
DONE_AUDIO_HOOK = 'MTBEAudioFileSoundOutput::Disengage'
def main(pid):
"""
Setup log dumping and forward it to stdout.
"""
log('Hooking up to daemon...')
with LogHook(pid) as logs:
log('Reading logs...')
while True:
print(logs.readline().strip())
class LogHook:
"""
A context manager that yields a file handle of log
messages from speechsynthesisd.
"""
def __init__(self,
pid,
log_meow=True,
log_worker=False,
log_done_audio=False):
"""
Configure a hook.
Args:
pid: the process ID of speechsynthesisd.
log_meow: whether or not to capture MEOW logs.
log_worker: whether or not to capture worker logs.
log_done_audio: enable a special debug log that
prints "*** AUDIO CLOSED **" when an output audio
file is closed.
"""
self.pid = pid
self.log_meow = log_meow
self.log_worker = log_worker
self.log_done_audio = log_done_audio
self._attached = None
def __enter__(self):
assert self._attached is None
self._attached = _AttachedHook(self.pid, self.log_meow, self.log_worker,
self.log_done_audio)
return self._attached.our_fifo
def __exit__(self, *args):
self._attached.stop()
self._attached = None
class _AttachedHook:
"""
An attached debugging session.
"""
def __init__(self, pid, log_meow, log_worker, log_done_audio):
self.done_event = Event()
self.debugger = lldb.SBDebugger.Create()
self.listener = self.debugger.GetListener()
self.target = self.debugger.CreateTarget('')
error = lldb.SBError()
self.process = self.target.AttachToProcessWithID(self.listener, pid, error)
try_sb_error(error)
self.our_fifo, self.their_fifo = setup_log_fifo(stopped_thread(self.process))
if log_meow:
try_breakpoint(self.target.BreakpointCreateByName(MEOW_DEBUG_HOOK))
if log_worker:
try_breakpoint(self.target.BreakpointCreateByName(WORKER_DEBUG_HOOK))
if log_done_audio:
try_breakpoint(self.target.BreakpointCreateByName(DONE_AUDIO_HOOK))
self.meow_flag = find_symbol(self.target, MEOW_DEBUG_FLAG)
self.worker_flag = find_symbol(self.target, WORKER_DEBUG_FLAG)
self.restore_stderr = replace_stderr(self.target, self.process, self.their_fifo)
try_sb_error(self.process.Continue())
assert not self.done_event.is_set()
self.bg_thread = Thread(target=self.poll_thread)
self.bg_thread.start()
def stop(self):
self.done_event.set()
self.bg_thread.join()
self.process.Detach()
def poll_thread(self):
while not self.done_event.is_set():
# https://github.com/llvm-mirror/lldb/blob/master/examples/python/process_events.py
event = lldb.SBEvent()
if not self.listener.WaitForEvent(1, event):
self.restore_stderr()
continue
thread = stopped_thread(self.process)
if not thread:
continue
try:
sym_name = thread.GetFrameAtIndex(0).GetSymbol().GetName()
if MEOW_DEBUG_HOOK in sym_name:
set_debug_flag(self.target, self.process, self.meow_flag)
elif WORKER_DEBUG_HOOK in sym_name:
set_debug_flag(self.target, self.process, self.worker_flag)
elif DONE_AUDIO_HOOK in sym_name:
write_log(thread, self.their_fifo, '*** AUDIO CLOSED ***')
finally:
try_sb_error(self.process.Continue())
def stopped_thread(process):
"""
Get the first stopped thread.
"""
for thread in process:
if thread.GetStopReason() not in [0, lldb.eStopReasonNone]:
return thread
return None
def setup_log_fifo(thread):
"""
Create a FIFO from the attached process to this one.
Args:
thread: an SBThread in the process.
Returns:
A tuple (our_fifo, their_fifo), where our_fifo is a
file handle and their_fifo is an SBValue for the
FILE* to the FIFO.
"""
frame = thread.GetFrameAtIndex(0)
temp_dir = frame.EvaluateExpression(TEMP_DIR).GetSummary()[1:-1]
log_out_path = os.path.join(temp_dir, 'socket' + str(random()))
os.mkfifo(log_out_path)
our_fifo = os.open(log_out_path, os.O_RDONLY | os.O_NONBLOCK)
flags = fcntl.fcntl(our_fifo, fcntl.F_GETFL)
fcntl.fcntl(our_fifo, fcntl.F_SETFL, flags ^ os.O_NONBLOCK)
our_fifo = os.fdopen(our_fifo, 'r')
their_fifo = frame.EvaluateExpression('(void *)fopen("' + log_out_path + '", "w")')
enable_line_buffering(frame, their_fifo)
return our_fifo, their_fifo
def enable_line_buffering(frame, fifo_value):
"""
Enable line buffering for a FILE* SBValue.
"""
error = lldb.SBError()
address = fifo_value.GetData().GetAddress(error, 0)
try_sb_error(error)
frame.EvaluateExpression('(int)setvbuf(' + str(address) + ', 0, 1, 512)')
def write_log(thread, fifo_value, message):
"""
Write a log message on behalf of the process.
Args:
thread: a stopped SBThread.
fifo_value: the SBValue of the FILE*.
message: the message to log. Should not contain
backslashes or quotation marks.
"""
assert '\\' not in message and '"' not in message, 'log messages are not escaped'
frame = thread.GetFrameAtIndex(0)
error = lldb.SBError()
address = fifo_value.GetData().GetAddress(error, 0)
try_sb_error(error)
code = '(int)fprintf((void *)' + str(address) + ', (char *)"' + message + '\\n")'
frame.EvaluateExpression(code)
def find_symbol(target, name, module=MACINTALK_MODULE):
"""
Find an SBSymbol by the given name.
"""
for mod in target.module_iter():
if module and module != mod.GetFileSpec().GetFilename():
continue
for sym in mod:
if sym.GetName() == name:
return sym
raise RuntimeError('symbol not found: ' + name)
def set_debug_flag(target, process, flag_sym):
"""
Enable a debug flag.
"""
error = lldb.SBError()
addr = flag_sym.GetStartAddress()
process.WriteMemory(addr.GetLoadAddress(target), '\x01', error)
try_sb_error(error)
def replace_stderr(target, process, fifo_value):
"""
Replace ___stderrp with a FILE* SBValue.
Args:
target: an SBTarget to change.
fifo_value: an SBValue representing the result of an
fopen() call.
Returns:
A function to restore the old values.
"""
restore_fns = []
for mod in target.module_iter():
for sym in mod:
if sym.GetName() == STDERR_SYMBOL:
if sym.GetStartAddress().GetOffset() != 0:
addr = sym.GetStartAddress().GetLoadAddress(target)
error = lldb.SBError()
str_val = fifo_value.GetData().GetString(error, 0)
try_sb_error(error)
old_value = process.ReadMemory(addr, len(str_val), error)
try_sb_error(error)
process.WriteMemory(addr, str_val, error)
try_sb_error(error)
restore_fns.append(partial(process.WriteMemory, addr, old_value, error))
return lambda: [f() for f in restore_fns]
def log(msg):
sys.stderr.write(msg + '\n')
def try_sb_error(err):
if err.fail:
raise RuntimeError(err.GetCString())
def try_breakpoint(bp):
if bp.GetNumLocations() != 1:
raise RuntimeError('could not set breakpoint')
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.stderr.write('Usage: dump_meow_logs.py <pid>\n')
sys.exit(1)
main(int(sys.argv[1]))