Teach `debugger tracing stop` to pick a file name automatically.

This will make the tracing write its result to the first available
filename of [mojo_trace_000.json, mojo_trace_001.json, ...].

(we always had a 'default' set on the |file_name| parameter, but it
didn't actually work as the parameter was positional and hence required
by default)

Fixes domokit/devtools#13.

R=tonyg@chromium.org

Review URL: https://codereview.chromium.org/1184453002.
diff --git a/README.md b/README.md
index 7c42b85..d6a52d3 100644
--- a/README.md
+++ b/README.md
@@ -233,8 +233,8 @@
 and retrieve the result:
 
 ```
-/devtools/common/debugger tracing start
-/devtools/common/debugger tracing stop result.json
+devtools/common/debugger tracing start
+devtools/common/debugger tracing stop [result.json]
 ```
 
 The trace file can be then loaded using the trace viewer in Chrome available at
diff --git a/mojo/devtools/common/debugger b/mojo/devtools/common/debugger
index 4ff747f..6bca9fb 100755
--- a/mojo/devtools/common/debugger
+++ b/mojo/devtools/common/debugger
@@ -4,6 +4,7 @@
 # found in the LICENSE file.
 
 import argparse
+import os.path
 import requests
 import sys
 
@@ -24,7 +25,18 @@
 
 def _tracing_stop(args):
   """Stops tracing and writes trace to file."""
-  file_name = args.file_name
+  if args.file_name:
+    file_name = args.file_name
+  else:
+    for i in xrange(1000):
+      candidate_file_name = 'mojo_trace_%03d.json' % i
+      if not os.path.exists(candidate_file_name):
+        file_name = candidate_file_name
+        break
+    else:
+      print 'Failed to pick a name for the trace output file.'
+      return 1
+
   trace = _send_request('stop_tracing').content
   with open(file_name, "wb") as trace_file:
     trace_file.write('{"traceEvents":[')
@@ -46,8 +58,8 @@
 
   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.add_argument('file_name', type=str, nargs='?',
+      help='name of the output file (optional)')
   stop_tracing_parser.set_defaults(func=_tracing_stop)