Home | History | Annotate | Download | only in debug
      1 <html devsite>
      2   <head>
      3     <title>Using Strace</title>
      4     <meta name="project_path" value="/_project.yaml" />
      5     <meta name="book_path" value="/_book.yaml" />
      6   </head>
      7   <body>
      8   <!--
      9       Copyright 2017 The Android Open Source Project
     10 
     11       Licensed under the Apache License, Version 2.0 (the "License");
     12       you may not use this file except in compliance with the License.
     13       You may obtain a copy of the License at
     14 
     15           http://www.apache.org/licenses/LICENSE-2.0
     16 
     17       Unless required by applicable law or agreed to in writing, software
     18       distributed under the License is distributed on an "AS IS" BASIS,
     19       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     20       See the License for the specific language governing permissions and
     21       limitations under the License.
     22   -->
     23 
     24 <p><a href="https://strace.io">Strace</a> enables you to see the system calls a
     25 process makes and what those system calls return. A typical process makes a lot
     26 of system calls, so you'll want to review the
     27 <a href="http://man7.org/linux/man-pages/man1/strace.1.html">strace man page</a>
     28 to learn how to collect only data you're actually interested in.</p>
     29 
     30 <h2 id=build-strace>Building strace</h2>
     31 
     32 <p>To build strace, run the following:
     33 <pre class="devsite-terminal devsite-click-to-copy">
     34 mmma -j6 external/strace
     35 </pre>
     36 
     37 <h2 id=attach-strace>Attaching to a running process</h2>
     38 
     39 <p>The simplest and most common use case for strace is to attach it to a running
     40 process, which you can do with:</p>
     41 <pre class="devsite-terminal devsite-click-to-copy">
     42 adb shell strace -f -p PID
     43 </pre>
     44 <p>The <code>-f</code> flag tells strace to attach to all the threads in the
     45 process, plus any new threads spawned later.</p>
     46 
     47 <h2 id=app-strace>Using on an application</h2>
     48 <p>To use strace on an application:</p>
     49 
     50 <ol>
     51 <li>Set up a directory for strace logs:
     52 <pre class="devsite-click-to-copy">
     53 <code class="devsite-terminal">adb shell setenforce 0</code>
     54 <code class="devsite-terminal">adb shell mkdir /data/local/tmp/strace</code>
     55 <code class="devsite-terminal">adb shell chmod 777 /data/local/tmp/strace</code>
     56 </pre>
     57 </li>
     58 
     59 <li>Choose the process to trace before launching it:
     60 <pre class="devsite-terminal devsite-click-to-copy">
     61 adb shell setprop wrap.com.google.android.browser "logwrapper strace -f -o /data/local/tmp/strace/strace.com.google.android.browser.txt"
     62 </pre>
     63 </li>
     64 <li>Launch the process normally.</li>
     65 </ol>
     66 
     67 <h2 id=zygote-systrace>Using on the zygote</h2>
     68 <p>To use strace on the zygote, fix the relevant <code>init.rc</code> zygote
     69 line (requires <code>adb shell setenforce 0</code>):
     70 </p>
     71 
     72 <pre class="devsite-click-to-copy">
     73 <code class="devsite-terminal">cd system/core/</code>
     74 <code class="devsite-terminal">patch -p1 &lt;&lt;EOF
     75 --- a/rootdir/init.zygote32.rc
     76 +++ b/rootdir/init.zygote32.rc
     77 @@ -1,4 +1,4 @@
     78 -service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
     79 +service zygote /system/xbin/strace -o /data/local/tmp/zygote.strace /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
     80      class main
     81      socket zygote stream 660 root system
     82      onrestart write /sys/android_power/request_state wake
     83 EOF</code>
     84 </pre>
     85 
     86 <h2 id=get-logs-boot>Getting strace logs during Android boot</h2>
     87 
     88 <p>To get strace logs during Android boot, make the following changes:</p>
     89 
     90 <ul>
     91 <li>Since the process name changes from <code>zygote</code> to
     92 <code>strace</code>, the given service may fail to start due to the missing
     93 SELinux <code>file_context</code> for <code>strace</code>. The solution is to
     94 add a new line for strace in <code>system/sepolicy/private/file_contexts</code>
     95 and copy the original file context over. Example:
     96 <pre class="devsite-click-to-copy">
     97 /dev/socket/zygote      u:object_r:zygote_socket:s0
     98 + /system/xbin/strace u:object_r:zygote_socket:s0
     99 </pre>
    100 </li>
    101 
    102 <li>Add kernel command, then boot the device in SELinux permissive mode. You can
    103 do this by adding <code>androidboot.selinux=permissive</code>to
    104 <code>BOARD_KERNEL_CMDLINE</code>. (This variable becomes read-only in
    105 <code>build/core/Makefile</code> but is always available under
    106 <code>/device/*/BoardConfig</code>.)
    107 
    108 <br>
    109 <br>Example for the Pixel (sailfish) device in
    110 <code>/device/google/marlin/sailfish/BoardConfig.mk</code>:
    111 <pre class="devsite-click-to-copy">
    112 - BOARD_KERNEL_CMDLINE := ....  androidboot.hardware=sailfish ...
    113 +BOARD_KERNEL_CMDLINE := ....  androidboot.hardware=sailfish ...  androidboot.selinux=permissive
    114 </pre>
    115 After making the change, build and flash the boot image and the device will boot
    116 in permissive mode.
    117 </li>
    118 </ul>
    119 
    120   </body>
    121 </html>
    122