| #!/usr/bin/env python |
| # Copyright 2014 The Chromium Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import argparse |
| import requests |
| import sys |
| |
| _MOJO_DEBUGGER_PORT = 7777 |
| |
| |
| def _send_request(request): |
| """Sends a request to mojo:debugger.""" |
| url = 'http://localhost:%s/%s' % (_MOJO_DEBUGGER_PORT, request) |
| return requests.get(url) |
| |
| |
| def _tracing_start(_): |
| """Starts tracing.""" |
| _send_request('start_tracing') |
| print "Started tracing." |
| |
| |
| def _tracing_stop(args): |
| """Stops tracing and writes trace to file.""" |
| file_name = args.file_name |
| trace = _send_request('stop_tracing').content |
| with open(file_name, "wb") as trace_file: |
| trace_file.write('{"traceEvents":[') |
| trace_file.write(trace) |
| trace_file.write(']}') |
| print "Trace saved in %s" % file_name |
| |
| |
| def _add_tracing_command(subparsers): |
| """Sets up the command line parser to manage tracing.""" |
| tracing_parser = subparsers.add_parser('tracing', |
| help='trace event profiler') |
| tracing_subparser = tracing_parser.add_subparsers( |
| help='the command to run') |
| |
| start_tracing_parser = tracing_subparser.add_parser('start', |
| help='start tracing') |
| start_tracing_parser.set_defaults(func=_tracing_start) |
| |
| stop_tracing_parser = tracing_subparser.add_parser('stop', |
| help='stop tracing and retrieve the result') |
| stop_tracing_parser.add_argument('file_name', type=str, |
| default='mojo.trace') |
| stop_tracing_parser.set_defaults(func=_tracing_stop) |
| |
| |
| def main(): |
| parser = argparse.ArgumentParser(description='Command-line interface for ' |
| 'mojo:debugger') |
| subparsers = parser.add_subparsers(help='the tool to run') |
| _add_tracing_command(subparsers) |
| |
| args = parser.parse_args() |
| args.func(args) |
| return 0 |
| |
| if __name__ == '__main__': |
| sys.exit(main()) |