- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathsymbolicate-linux-fatal
executable file
·290 lines (240 loc) · 9.76 KB
/
symbolicate-linux-fatal
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
#!/usr/bin/env python3
# symbolicate-linux-fatal - Symbolicate Linux stack traces -*- python -*-
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
# ----------------------------------------------------------------------------
#
# Symbolicates fatalError stack traces on Linux. Takes the main binary
# and a log file containing a stack trace. Non-stacktrace lines are output
# unmodified. Stack trace elements are analyzed using reconstructed debug
# target matching the original process in where shared libs where mapped.
#
# TODOs:
# * verbose output
# * search symbols by name for the not <unavailable> ones
#
# ----------------------------------------------------------------------------
importargparse
importdatetime
importos
importsubprocess
importsys
try:
importlldb
exceptImportError:
fromdistutilsimportspawn
swift_exec=spawn.find_executable('swift')
ifswift_execisnotNone:
site_packages=os.path.join(os.path.dirname(swift_exec),
'../lib/python2.7/site-packages/')
sys.path.append(site_packages)
importlldb
lldb_target=None
known_memmap= {}
defprint_with_flush(buff):
print(buff)
sys.stdout.flush()
defprocess_ldd(lddoutput):
dyn_libs= {}
forlineinlddoutput.splitlines():
ldd_tokens=line.split()
iflen(ldd_tokens) >=2:
lib=ldd_tokens[-2]
dyn_libs[ldd_tokens[0]] =lib
real_name=os.path.basename(os.path.realpath(lib))
dyn_libs[real_name] =lib
returndyn_libs
defsetup_lldb_target(binary, memmap):
globallldb_target
ifnotlldb_target:
lldb_debugger=lldb.SBDebugger.Create()
lldb_target=lldb_debugger.CreateTarget(binary)
module=lldb_target.GetModuleAtIndex(0)
fordynlib_pathinmemmap:
module=lldb_target.AddModule(
dynlib_path, lldb.LLDB_ARCH_DEFAULT, None, None)
text_section=module.FindSection(".text")
slide=text_section.GetFileAddress() -text_section.GetFileOffset()
lldb_target.SetModuleLoadAddress(module, memmap[dynlib_path] -slide)
defcheck_base_address(dynlib_path, dynlib_baseaddr, memmap):
globalknown_memmap
ifdynlib_pathinmemmapordynlib_pathinknown_memmap:
ifdynlib_pathinmemmap:
existing_baseaddr=memmap[dynlib_path]
else:
existing_baseaddr=known_memmap[dynlib_path]
ifexisting_baseaddr!=dynlib_baseaddr:
error_msg="Mismatched base address for: {0:s}, " \
"had: {1:x}, now got {2:x}"
error_msg=error_msg.format(
dynlib_path, existing_baseaddr, dynlib_baseaddr)
raiseException(error_msg)
defsymbolicate_one(frame_addr, frame_idx, dynlib_fname):
globallldb_target
so_addr=lldb_target.ResolveLoadAddress(frame_addr-1)
sym_ctx=so_addr.GetSymbolContext(lldb.eSymbolContextEverything)
frame_fragment="{0: <4d} {1:20s} 0x{2:016x}".format(
frame_idx, dynlib_fname, frame_addr)
symbol=sym_ctx.GetSymbol()
ifnotsymbol.IsValid():
raiseException("symbol isn't valid")
symbol_base=symbol.GetStartAddress().GetLoadAddress(lldb_target)
symbol_fragment="{0:s} + {1:d}".format(
symbol.GetName(), frame_addr-symbol_base)
line_entry=sym_ctx.GetLineEntry()
ifline_entry.IsValid():
line_fragment="at {0:s}:{1:d}".format(
line_entry.GetFileSpec().GetFilename(), line_entry.GetLine())
else:
line_fragment=""
return"{0:s} {1:s} {2:s}".format(
frame_fragment, symbol_fragment, line_fragment)
defget_processed_stack(binary, dyn_libs, stack):
globallldb_target
globalknown_memmap
processed_stack= []
iflen(stack) ==0:
returnprocessed_stack
memmap= {}
full_stack= []
forlineinstack:
stack_tokens=line.split()
dynlib_fname=stack_tokens[1]
ifdynlib_fnameindyn_libs:
dynlib_path=dyn_libs[dynlib_fname]
elifdynlib_fnameinbinary:
dynlib_path=binary
else:
dynlib_path=None
try:
framePC=int(stack_tokens[2], 16)
symbol_offset=int(stack_tokens[-1], 10)
exceptException:
full_stack.append({"line": line, "framePC": 0, "dynlib_fname": ""})
continue
if"<unavailable>"instack_tokens[3]:
dynlib_baseaddr=framePC-symbol_offset
check_base_address(dynlib_path, dynlib_baseaddr, memmap)
known_memmap[dynlib_path] =dynlib_baseaddr
memmap[dynlib_path] =dynlib_baseaddr
else:
framePC=framePC+symbol_offset
full_stack.append(
{"line": line, "framePC": framePC, "dynlib_fname": dynlib_fname})
setup_lldb_target(binary, memmap)
forframe_idx, frameinenumerate(full_stack):
frame_addr=frame["framePC"]
dynlib_fname=frame["dynlib_fname"]
try:
sym_line=symbolicate_one(frame_addr, frame_idx, dynlib_fname)
processed_stack.append(sym_line)
exceptException:
processed_stack.append(frame["line"].rstrip())
returnprocessed_stack
defis_fatal_error(line):
returnline.startswith("Fatal error:")
defis_stack_trace_header(line):
returnline.startswith("Current stack trace:")
defshould_print_previous_line(current_line, previous_line):
returnis_fatal_error(previous_line) and \
notis_stack_trace_header(current_line)
defshould_print_current_line(current_line, previous_line):
return (notis_fatal_error(current_line) and
notis_stack_trace_header(current_line)) or \
(is_stack_trace_header(current_line) and
notis_fatal_error(previous_line))
deffatal_error_with_stack_trace_found(current_line, previous_line):
returnis_stack_trace_header(current_line) and \
is_fatal_error(previous_line)
defprint_stack(fatal_error_header,
fatal_error_stack_trace_header,
fatal_log_format,
processed_stack):
ifnotfatal_error_header:
forlineinprocessed_stack:
print_with_flush(line)
else:
# fatal error with a stack trace
stack_str=fatal_error_header+fatal_error_stack_trace_header+ \
'\n'.join(processed_stack)
formatted_output=fatal_log_format
if"%t"informatted_output:
current_time=datetime.datetime.now()
time_in_iso_format= \
current_time.strftime('%Y-%m-%dT%H:%M:%S,%f%z')
formatted_output= \
formatted_output.replace("%t", time_in_iso_format)
if"%m"informatted_output:
formatted_output=formatted_output.replace("%m", stack_str)
print_with_flush(formatted_output)
defmain():
parser=argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""Symbolicates stack traces in Linux log files.""")
parser.add_argument(
"binary", help="Executable which produced the log file")
parser.add_argument(
"log", nargs='?', type=argparse.FileType("rU"), default="-",
help="Log file for symbolication. Defaults to stdin.")
parser.add_argument(
"--fatal-log-format", default="%m",
help="Format for logging fatal errors. Variable %%t will be "
"replaced with current time in ISO 8601 format, variable "
"%%m will be replaced with the error message with a full "
"stack trace.")
args=parser.parse_args()
binary=args.binary
fatal_log_format=args.fatal_log_format
lddoutput=subprocess.check_output(
['ldd', binary], stderr=subprocess.STDOUT)
dyn_libs=process_ldd(lddoutput)
instack=False
previous_line=""
stackidx=0
stack= []
fatal_error_header=""
fatal_error_stack_trace_header=""
whileTrue:
current_line=args.log.readline()
ifnotcurrent_line:
break
ifinstackandcurrent_line.startswith(str(stackidx)):
stack.append(current_line)
stackidx=stackidx+1
else:
processed_stack=get_processed_stack(binary, dyn_libs, stack)
print_stack(fatal_error_header,
fatal_error_stack_trace_header,
fatal_log_format,
processed_stack)
instack=False
stackidx=0
stack= []
fatal_error_header=""
fatal_error_stack_trace_header=""
ifis_stack_trace_header(current_line):
instack=True
ifshould_print_previous_line(current_line, previous_line):
print_with_flush(previous_line.rstrip())
ifshould_print_current_line(current_line, previous_line):
print_with_flush(current_line.rstrip())
iffatal_error_with_stack_trace_found(current_line, previous_line):
fatal_error_header=previous_line
fatal_error_stack_trace_header=current_line
previous_line=current_line
ifis_fatal_error(previous_line):
print_with_flush(previous_line.rstrip())
processed_stack=get_processed_stack(binary, dyn_libs, stack)
print_stack(fatal_error_header,
fatal_error_stack_trace_header,
fatal_log_format,
processed_stack)
if__name__=='__main__':
main()