Add a cli for a running mojo:debugger.

This currently allows to stop and start tracing.

R=qsr@chromium.org

Review URL: https://codereview.chromium.org/1156823004

Cr-Mirrored-From: https://github.com/domokit/mojo
Cr-Mirrored-Commit: 70ae7c0040a0733511f2ca674b91b39026f1f8c7
diff --git a/debugger b/debugger
new file mode 100755
index 0000000..b512b4b
--- /dev/null
+++ b/debugger
@@ -0,0 +1,52 @@
+#!/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):
+  url = 'http://localhost:%s/%s' % (_MOJO_DEBUGGER_PORT, request)
+  return requests.get(url)
+
+
+def start_tracing_command(args):
+    _send_request('start_tracing')
+    print "Started tracing."
+
+
+def stop_tracing_command(args):
+    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 main():
+  parser = argparse.ArgumentParser(description='Command-line interface for '
+                                                'mojo:debugger')
+  subparsers = parser.add_subparsers(help='sub-command help')
+
+  start_tracing_parser = subparsers.add_parser('start_tracing',
+                                               help='starts tracing')
+  start_tracing_parser.set_defaults(func=start_tracing_command)
+
+  stop_tracing_parser = subparsers.add_parser('stop_tracing',
+                                              help='stops tracing')
+  stop_tracing_parser.add_argument('file_name', type=str, default='mojo.trace')
+  stop_tracing_parser.set_defaults(func=stop_tracing_command)
+
+  args = parser.parse_args()
+  args.func(args)
+  return 0
+
+if __name__ == '__main__':
+  sys.exit(main())