OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding=utf-8 | 2 # coding=utf-8 |
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 """Traces an executable and its child processes and extract the files accessed | 7 """Traces an executable and its child processes and extract the files accessed |
8 by them. | 8 by them. |
9 | 9 |
10 The implementation uses OS-specific API. The native Kernel logger and the ETL | 10 The implementation uses OS-specific API. The native Kernel logger and the ETL |
(...skipping 1484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1495 try: | 1495 try: |
1496 context = cls.Context(blacklist, item['cwd']) | 1496 context = cls.Context(blacklist, item['cwd']) |
1497 for pidfile in glob.iglob('%s.%s.*' % (logname, item['trace'])): | 1497 for pidfile in glob.iglob('%s.%s.*' % (logname, item['trace'])): |
1498 pid = pidfile.rsplit('.', 1)[1] | 1498 pid = pidfile.rsplit('.', 1)[1] |
1499 if pid.isdigit(): | 1499 if pid.isdigit(): |
1500 pid = int(pid) | 1500 pid = int(pid) |
1501 # TODO(maruel): Load as utf-8 | 1501 # TODO(maruel): Load as utf-8 |
1502 for line in open(pidfile, 'rb'): | 1502 for line in open(pidfile, 'rb'): |
1503 context.on_line(pid, line) | 1503 context.on_line(pid, line) |
1504 result['results'] = context.to_results() | 1504 result['results'] = context.to_results() |
1505 except TracingFailure, e: | 1505 except TracingFailure: |
1506 result['exception'] = e | 1506 result['exception'] = sys.exc_info() |
1507 out.append(result) | 1507 out.append(result) |
1508 return out | 1508 return out |
1509 | 1509 |
1510 | 1510 |
1511 class Dtrace(ApiBase): | 1511 class Dtrace(ApiBase): |
1512 """Uses DTrace framework through dtrace. Requires root access. | 1512 """Uses DTrace framework through dtrace. Requires root access. |
1513 | 1513 |
1514 Implies Mac OSX. | 1514 Implies Mac OSX. |
1515 | 1515 |
1516 dtruss can't be used because it has compatibility issues with python. | 1516 dtruss can't be used because it has compatibility issues with python. |
(...skipping 1518 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3035 - logfile: File to load. | 3035 - logfile: File to load. |
3036 - root_dir: Root directory to use to determine if a file is relevant to the | 3036 - root_dir: Root directory to use to determine if a file is relevant to the |
3037 trace or not. | 3037 trace or not. |
3038 - api: A tracing api instance. | 3038 - api: A tracing api instance. |
3039 - blacklist: Optional blacklist function to filter out unimportant files. | 3039 - blacklist: Optional blacklist function to filter out unimportant files. |
3040 """ | 3040 """ |
3041 data = api.parse_log(logfile, (blacklist or (lambda _: False))) | 3041 data = api.parse_log(logfile, (blacklist or (lambda _: False))) |
3042 assert len(data) == 1, 'More than one trace was detected!' | 3042 assert len(data) == 1, 'More than one trace was detected!' |
3043 if 'exception' in data[0]: | 3043 if 'exception' in data[0]: |
3044 # It got an exception, raise it. | 3044 # It got an exception, raise it. |
3045 raise data[0]['exception'] | 3045 raise \ |
csharp
2012/10/15 15:38:28
Would it be better to use brackets, or does raise
M-A Ruel
2012/10/15 15:40:25
It didn't work when I used (). :/
It's really an a
| |
3046 data[0]['exception'][0], \ | |
3047 data[0]['exception'][1], \ | |
3048 data[0]['exception'][2] | |
3046 results = data[0]['results'] | 3049 results = data[0]['results'] |
3047 if root_dir: | 3050 if root_dir: |
3048 results = results.strip_root(root_dir) | 3051 results = results.strip_root(root_dir) |
3049 return results | 3052 return results |
3050 | 3053 |
3051 | 3054 |
3052 def CMDclean(args): | 3055 def CMDclean(args): |
3053 """Cleans up traces.""" | 3056 """Cleans up traces.""" |
3054 parser = OptionParserTraceInputs(command='clean') | 3057 parser = OptionParserTraceInputs(command='clean') |
3055 options, args = parser.parse_args(args) | 3058 options, args = parser.parse_args(args) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3107 | 3110 |
3108 variables = dict(options.variables) | 3111 variables = dict(options.variables) |
3109 api = get_api() | 3112 api = get_api() |
3110 def blacklist(f): | 3113 def blacklist(f): |
3111 return any(re.match(b, f) for b in options.blacklist) | 3114 return any(re.match(b, f) for b in options.blacklist) |
3112 data = api.parse_log(options.log, blacklist) | 3115 data = api.parse_log(options.log, blacklist) |
3113 # Process each trace. | 3116 # Process each trace. |
3114 output_as_json = [] | 3117 output_as_json = [] |
3115 for item in data: | 3118 for item in data: |
3116 if 'exception' in item: | 3119 if 'exception' in item: |
3120 # Do not abort the other traces. | |
3117 print >> sys.stderr, ( | 3121 print >> sys.stderr, ( |
3118 'Trace %s: Got an exception: %s' % (item['trace'], item['exception'])) | 3122 'Trace %s: Got an exception: %s' % ( |
3123 item['trace'], item['exception'][1])) | |
3119 continue | 3124 continue |
3120 results = item['results'] | 3125 results = item['results'] |
3121 if options.root_dir: | 3126 if options.root_dir: |
3122 results = results.strip_root(options.root_dir) | 3127 results = results.strip_root(options.root_dir) |
3123 | 3128 |
3124 if options.json: | 3129 if options.json: |
3125 output_as_json.append(results.flatten()) | 3130 output_as_json.append(results.flatten()) |
3126 else: | 3131 else: |
3127 simplified = extract_directories( | 3132 simplified = extract_directories( |
3128 options.root_dir, results.files, blacklist) | 3133 options.root_dir, results.files, blacklist) |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3249 main_impl(argv) | 3254 main_impl(argv) |
3250 except TracingFailure, e: | 3255 except TracingFailure, e: |
3251 sys.stderr.write('\nError: ') | 3256 sys.stderr.write('\nError: ') |
3252 sys.stderr.write(str(e)) | 3257 sys.stderr.write(str(e)) |
3253 sys.stderr.write('\n') | 3258 sys.stderr.write('\n') |
3254 return 1 | 3259 return 1 |
3255 | 3260 |
3256 | 3261 |
3257 if __name__ == '__main__': | 3262 if __name__ == '__main__': |
3258 sys.exit(main(sys.argv[1:])) | 3263 sys.exit(main(sys.argv[1:])) |
OLD | NEW |