systrace: handle user builds more gracefully

This change adds an indication of which command line args require root to the
--help output.  It also defers opening the output file until after the trace
has actual started being dumped so that if atrace exits with an error the
output file is not truncated.

Bug: 6513400
Change-Id: Ifa0ed1d3d5eef891fee7d9109c2bb988b03c2e4d
diff --git a/systrace.py b/systrace.py
index 8586caf..ea3e76e 100755
--- a/systrace.py
+++ b/systrace.py
@@ -34,7 +34,7 @@
   parser.add_option('-b', '--buf-size', dest='trace_buf_size', type='int',
                     help='use a trace buffer size of N KB', metavar='N')
   parser.add_option('-d', '--disk', dest='trace_disk', default=False,
-                    action='store_true', help='trace disk I/O')
+                    action='store_true', help='trace disk I/O (requires root)')
   parser.add_option('-f', '--cpu-freq', dest='trace_cpu_freq', default=False,
                     action='store_true', help='trace CPU frequency changes')
   parser.add_option('-i', '--cpu-idle', dest='trace_cpu_idle', default=False,
@@ -46,7 +46,8 @@
                     'scheduler (allows longer trace times by reducing data ' +
                     'rate into buffer)')
   parser.add_option('-w', '--workqueue', dest='trace_workqueue', default=False,
-                    action='store_true', help='trace the kernel workqueues')
+                    action='store_true', help='trace the kernel workqueues ' +
+                    '(requires root)')
   parser.add_option('--set-tags', dest='set_tags', action='store',
                     help='set the enabled trace tags and exit; set to a ' +
                     'comma separated list of: ' +
@@ -69,7 +70,7 @@
     try:
       subprocess.check_call(atrace_args)
     except subprocess.CalledProcessError, e:
-      print sys.stderr, 'unable to set tags: %s' % e
+      print >> sys.stderr, 'unable to set tags: %s' % e
     print '\nSet enabled tags to: %s\n' % ', '.join(tags)
     print ('You will likely need to restart the Android framework for this to ' +
           'take effect:\n\n    adb shell stop\n    adb shell ' +
@@ -112,8 +113,6 @@
     js = compiled_js_tag % (open(js_filename).read())
 
   html_filename = options.output_file
-  html_file = open(html_filename, 'w')
-  html_file.write(html_prefix % (css, js))
 
   trace_started = False
   leftovers = ''
@@ -142,6 +141,8 @@
             sys.stdout.write("downloading trace...")
             sys.stdout.flush()
             out = ''.join(lines[i+1:])
+            html_file = open(html_filename, 'w')
+            html_file.write(html_prefix % (css, js))
             trace_started = True
             break
           elif 'TRACE:'.startswith(line) and i == len(lines) - 1:
@@ -158,14 +159,17 @@
     if result is not None:
       break
   if result != 0:
-    print sys.stderr, 'adb returned error code %d' % result
-  else:
+    print >> sys.stderr, 'adb returned error code %d' % result
+  elif trace_started:
     html_out = dec.flush().replace('\n', '\\n\\\n').replace('\r', '')
     if len(html_out) > 0:
       html_file.write(html_out)
     html_file.write(html_suffix)
     html_file.close()
     print " done\n\n    wrote file://%s/%s\n" % (os.getcwd(), options.output_file)
+  else:
+    print >> sys.stderr, ('An error occured while capturing the trace.  Output ' +
+      'file was not written.')
 
 html_prefix = """<!DOCTYPE HTML>
 <html>