title: CommandFailedError when running systrace on Android 6.0 device
body: <p>I want to capture a system trace, using the systrace tool.</p>
  <p>I have this device (not-rooted) :
  Pixi 4.5 Alcatel
  Android 6.0 (API 23)</p>
  <p>And I use Android Studio 4.2.2 and I have installed Python 2.7.18, as required (the version 3+ does not match).</p>
  <p>Here is the command line to view the list of available categories on my device :</p>
  <p><code>C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace&gt;C:\Users\myUser\AppData\Local\Programs\Python27\python systrace.py -l</code></p>
  <p>I chose this command with the -l option for my here question because it results (see below) in the expected list plus an extra set of lines which suggests that the error <strong>CommandFailedError</strong> is related to an exception on <strong>HasRoot</strong> :</p>
  <pre><code>
         gfx - Graphics
  
         input - Input
  
          view - View System
  
       webview - WebView
  
            wm - Window Manager
  
            am - Activity Manager
  
            sm - Sync Manager
  
         audio - Audio
  
         video - Video
  
        camera - Camera
  
           hal - Hardware Modules
  
           app - Application
  
           res - Resource Loading
  
        dalvik - Dalvik VM
  
            rs - RenderScript
  
          hwui - HWUI
  
          perf - Performance
  
        bionic - Bionic C Library
  
         power - Power Management
  
         sched - CPU Scheduling
  
          freq - CPU Frequency
  
          idle - CPU Idle
  
          load - CPU Load
  
  CRITICAL:root:(TimeoutThread-1-for-MainThread) Exception on HasRoot(myDeviceId, retries=3, timeout=30), attempt 1 of 4: CommandFailedError(&quot;(device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcar
  d_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']&quot;,)
  CRITICAL:root:(TimeoutThread-2-for-MainThread) Exception on HasRoot(myDeviceId, retries=3, timeout=30), attempt 2 of 4: CommandFailedError(&quot;(device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcar
  d_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']&quot;,)
  CRITICAL:root:(TimeoutThread-3-for-MainThread) Exception on HasRoot(myDeviceId, retries=3, timeout=30), attempt 3 of 4: CommandFailedError(&quot;(device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcar
  d_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']&quot;,)
  Traceback (most recent call last):
    File &quot;systrace.py&quot;, line 49, in &lt;module&gt;
      sys.exit(run_systrace.main())
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py&quot;, line 205, in main
      main_impl(sys.argv)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py&quot;, line 177, in main_impl
      atrace_agent.list_categories(options)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\tracing_agents\atrace_agent.py&quot;, line 65, in list_categories
      if not devutils.HasRoot():
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py&quot;, line 63, in timeout_r
  etry_wrapper
      impl, timeout, retries, desc=desc, retry_if_func=retry_if_func)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py&quot;, line 164, in Run
      error_log_func=error_log_func)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py&quot;, line 199, in JoinA
  ll
      self._JoinAll(watcher, timeout)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py&quot;, line 171, in _Join
  All
      thread.ReraiseIfException()
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py&quot;, line 94, in run
      self._ret = self._func(*self._args, **self._kwargs)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py&quot;, line 156, in &lt;lambda
  &gt;
      lambda: func(*args, **kwargs), name=thread_name)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py&quot;, line 51, in impl
      return f(*args, **kwargs)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py&quot;, line 562, in HasRoo
  t
      output = self.RunShellCommand(['id'], single_line=True)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py&quot;, line 56, in timeout_r
  etry_wrapper
      return impl()
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py&quot;, line 51, in impl
      return f(*args, **kwargs)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py&quot;, line 1556, in RunSh
  ellCommand
      raise device_errors.CommandFailedError(msg % output, str(self))
  devil.android.device_errors.CommandFailedError: (device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(
  shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']
  </code></pre>
  <p>By comparison, here is the error <strong>CommandFailedError</strong> obtained with a command which should generate an html report :</p>
  <pre><code>C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace&gt;C:\Users\myUser\AppData\Local\Programs\Python27\python systrace.py
  
  Traceback (most recent call last):
    File &quot;systrace.py&quot;, line 49, in &lt;module&gt;
      sys.exit(run_systrace.main())
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py&quot;, line 205, in main
      main_impl(sys.argv)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py&quot;, line 185, in main_impl
      controller.StartTracing()
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\systrace_runner.py&quot;, line 46, in StartTracing
      self._tracing_controller.StartTracing()
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\tracing_controller.py&quot;, line 156, in StartTracing
      timeout=self._controller_config.timeout):
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\common\py_utils\py_utils\__init__.py&quot;, line 103, in RunWithTimeout
      return timeout_retry.Run(func, timeout, 0, args=args)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py&quot;, line 164, in Run
      error_log_func=error_log_func)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py&quot;, line 199, in JoinA
  ll
      self._JoinAll(watcher, timeout)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py&quot;, line 171, in _Join
  All
      thread.ReraiseIfException()
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py&quot;, line 94, in run
      self._ret = self._func(*self._args, **self._kwargs)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py&quot;, line 156, in &lt;lambda
  &gt;
      lambda: func(*args, **kwargs), name=thread_name)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\tracing_agents\android_cgroup_agent.py&quot;, line 59, in StartAgent
  Tracing
      if not self._device_utils.HasRoot():
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py&quot;, line 56, in timeout_r
  etry_wrapper
      return impl()
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py&quot;, line 51, in impl
      return f(*args, **kwargs)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py&quot;, line 562, in HasRoo
  t
      output = self.RunShellCommand(['id'], single_line=True)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py&quot;, line 56, in timeout_r
  etry_wrapper
      return impl()
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py&quot;, line 51, in impl
      return f(*args, **kwargs)
    File &quot;C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py&quot;, line 1556, in RunSh
  ellCommand
      raise device_errors.CommandFailedError(msg % output, str(self))
  devil.android.device_errors.CommandFailedError: (device: deviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(
  shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']
  </code></pre>
  <p>I am sure that there is a proper way to run systrace on an old unrooted device but I just didn't find it.</p>
  <p>Does anyone have a clue or a solution? Thanks.</p>
  <p>EDIT :</p>
  <p>Thanks a lot for your analysis and your answer @yi-yang.</p>
  <p>As you recommended, I set single_line to False in the RunShellCommand call. Furthermore, I also modified the return line, which gives the final code in the device_utils.py file, lines 562-563 :</p>
  <pre><code>output = self.RunShellCommand(['id'], single_line=False)
  return output[0].startswith('uid=0(root)')
  </code></pre>
  <p>Note that I select the first line because there are still two lines, which means that some optional newline at the end needs to be stripped somehow...</p>
  <p>Now it works but only for a few categories, as in the following example:</p>
  <pre><code>systrace.py -o mynewtrace.html perf idle load
  </code></pre>
  <p>However, for any atrace data, including the very helpful &quot;sched&quot; (see atrace_agent.py), the cpu profiling not possible, as shown by the following messages:</p>
  <pre><code>Warning: Only 2 of 3 tracing agents started.
  IOError('Unable to get atrace data. Did you forget adb root?')
  </code></pre>
  <p>So the root privileges do matter, despite what the file comments (l. 556-558) claim :-(.
  I guess <strong>rooting</strong> is a must for the full experience!</p>