OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 from third_party import asan_symbolize | 7 from third_party import asan_symbolize |
8 | 8 |
| 9 import argparse |
| 10 import base64 |
| 11 import json |
9 import os | 12 import os |
10 import sys | 13 import sys |
11 | 14 |
12 class LineBuffered(object): | 15 class LineBuffered(object): |
13 """Disable buffering on a file object.""" | 16 """Disable buffering on a file object.""" |
14 def __init__(self, stream): | 17 def __init__(self, stream): |
15 self.stream = stream | 18 self.stream = stream |
16 | 19 |
17 def write(self, data): | 20 def write(self, data): |
18 self.stream.write(data) | 21 self.stream.write(data) |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 # is located in the product dir. | 86 # is located in the product dir. |
84 outermost_bundle = bundle_positions[0] | 87 outermost_bundle = bundle_positions[0] |
85 product_dir = path_parts[:outermost_bundle] | 88 product_dir = path_parts[:outermost_bundle] |
86 # In case 2 this is the same as |outermost_bundle|. | 89 # In case 2 this is the same as |outermost_bundle|. |
87 innermost_bundle = bundle_positions[-1] | 90 innermost_bundle = bundle_positions[-1] |
88 dsym_path = product_dir + [path_parts[innermost_bundle]] | 91 dsym_path = product_dir + [path_parts[innermost_bundle]] |
89 result = '%s.dSYM' % os.path.sep.join(dsym_path) | 92 result = '%s.dSYM' % os.path.sep.join(dsym_path) |
90 return [result] | 93 return [result] |
91 | 94 |
92 | 95 |
| 96 # We want our output to match base::EscapeJSONString(), which produces |
| 97 # doubly-escaped strings. The first escaping pass is handled by this class. The |
| 98 # second pass happens when JSON data is dumped to file. |
| 99 class StringEncoder(json.JSONEncoder): |
| 100 def __init__(self): |
| 101 json.JSONEncoder.__init__(self) |
| 102 |
| 103 def encode(self, s): |
| 104 assert(isinstance(s, basestring)) |
| 105 encoded = json.JSONEncoder.encode(self, s) |
| 106 assert(len(encoded) >= 2) |
| 107 assert(encoded[0] == '"') |
| 108 assert(encoded[-1] == '"') |
| 109 encoded = encoded[1:-1] |
| 110 # Special case from base::EscapeJSONString(). |
| 111 encoded = encoded.replace('<', '\u003C') |
| 112 return encoded |
| 113 |
| 114 |
| 115 class JSONTestRunSymbolizer(object): |
| 116 def __init__(self, symbolization_loop): |
| 117 self.string_encoder = StringEncoder() |
| 118 self.symbolization_loop = symbolization_loop |
| 119 |
| 120 def symbolize_snippet(self, snippet): |
| 121 symbolized_lines = [] |
| 122 for line in snippet.split('\n'): |
| 123 symbolized_lines += self.symbolization_loop.process_line(line) |
| 124 return '\n'.join(symbolized_lines) |
| 125 |
| 126 def symbolize(self, test_run): |
| 127 original_snippet = base64.b64decode(test_run['output_snippet_base64']) |
| 128 symbolized_snippet = self.symbolize_snippet(original_snippet) |
| 129 if symbolized_snippet == original_snippet: |
| 130 # No sanitizer reports in snippet. |
| 131 return |
| 132 |
| 133 test_run['original_output_snippet'] = test_run['output_snippet'] |
| 134 test_run['original_output_snippet_base64'] = \ |
| 135 test_run['output_snippet_base64'] |
| 136 |
| 137 escaped_snippet = StringEncoder().encode(symbolized_snippet) |
| 138 test_run['output_snippet'] = escaped_snippet |
| 139 test_run['output_snippet_base64'] = \ |
| 140 base64.b64encode(symbolized_snippet) |
| 141 test_run['snippet_processed_by'] = 'asan_symbolize.py' |
| 142 # Originally, "lossless" refers to "no Unicode data lost while encoding the |
| 143 # string". However, since we're applying another kind of transformation |
| 144 # (symbolization), it doesn't seem right to consider the snippet lossless. |
| 145 test_run['losless_snippet'] = False |
| 146 |
| 147 |
| 148 def symbolize_snippets_in_json(filename, symbolization_loop): |
| 149 with open(filename, 'r') as f: |
| 150 json_data = json.load(f) |
| 151 |
| 152 test_run_symbolizer = JSONTestRunSymbolizer(symbolization_loop) |
| 153 for iteration_data in json_data['per_iteration_data']: |
| 154 for test_name, test_runs in iteration_data.iteritems(): |
| 155 for test_run in test_runs: |
| 156 test_run_symbolizer.symbolize(test_run) |
| 157 |
| 158 with open(filename, 'w') as f: |
| 159 json.dump(json_data, f, indent=3, sort_keys=True) |
| 160 |
| 161 |
93 def main(): | 162 def main(): |
| 163 parser = argparse.ArgumentParser(description='Symbolize sanitizer reports.') |
| 164 parser.add_argument('--test-summary-json-file', |
| 165 help='Path to a JSON file produced by the test launcher. The script will ' |
| 166 'ignore stdandard input and instead symbolize the output stnippets ' |
| 167 'inside the JSON file. The result will be written back to the JSON ' |
| 168 'file.') |
| 169 parser.add_argument('strip_path_prefix', nargs='*', |
| 170 help='When printing source file names, the longest prefix ending in one ' |
| 171 'of these substrings will be stripped. E.g.: "Release/../../".') |
| 172 args = parser.parse_args() |
| 173 |
94 disable_buffering() | 174 disable_buffering() |
95 set_symbolizer_path() | 175 set_symbolizer_path() |
96 asan_symbolize.demangle = True | 176 asan_symbolize.demangle = True |
97 asan_symbolize.fix_filename_patterns = sys.argv[1:] | 177 asan_symbolize.fix_filename_patterns = args.strip_path_prefix |
98 asan_symbolize.logfile = sys.stdin | |
99 loop = asan_symbolize.SymbolizationLoop(dsym_hint_producer=chrome_dsym_hints) | 178 loop = asan_symbolize.SymbolizationLoop(dsym_hint_producer=chrome_dsym_hints) |
100 loop.process_logfile() | 179 |
| 180 if args.test_summary_json_file: |
| 181 symbolize_snippets_in_json(args.test_summary_json_file, loop) |
| 182 else: |
| 183 # Process stdin. |
| 184 asan_symbolize.logfile = sys.stdin |
| 185 loop.process_logfile() |
101 | 186 |
102 if __name__ == '__main__': | 187 if __name__ == '__main__': |
103 main() | 188 main() |
OLD | NEW |