Home | History | Annotate | Download | only in debug
      1 <html devsite>
      2   <head>
      3     <title>Using Valgrind</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>The <a href="http://valgrind.org/">Valgrind</a> tool suite includes Memcheck
     25 (for detecting memory-related errors in C and C++), Cachegrind (a cache
     26 profiler), Massif (a heap profiler), and several other tools.</p>
     27 
     28 <p class="note"><strong>Note:</strong> While you can use Valgrind for Android
     29 debugging, most Android platform developers use
     30 <a href="/devices/tech/debug/asan.html">AddressSanitizer</a> instead.</p>
     31 
     32 <h2 id=build-valgrind>Building Valgrind</h2>
     33 <p>To build Valgrind:</p>
     34 <pre class="devsite-terminal devsite-click-to-copy">
     35 mmma -j6 external/valgrind
     36 </pre>
     37 
     38 <h2 id=app-valgrind>Using on an application</h2>
     39 <p>To use Valgrind on an application:</p>
     40 
     41 <pre class="devsite-click-to-copy">
     42 <code class="devsite-terminal">adb shell setprop wrap.<em>app_name</em> "logwrapper valgrind"</code>
     43 <code class="devsite-terminal">adb shell am start -a android.intent.action.MAIN -n <em>app_name</em>/.MainActivity</code>
     44 </pre>
     45 
     46 <code><em>app_name</em></code> must be a fully-qualified name such as
     47 <strong>com.google.example_app</strong>.
     48 
     49 <h2 id=server-valgrind>Using on the system server</h2>
     50 <p>To run the system server with Valgrind:</p>
     51 
     52 <pre class="devsite-click-to-copy">
     53 <code class="devsite-terminal">adb shell setprop wrap.system_server "logwrapper valgrind"</code>
     54 <code class="devsite-terminal">adb shell stop && adb shell start</code>
     55 </pre>
     56 
     57 
     58 <h2 id=symbols-valgrind>Getting debug symbols</h2>
     59 <p>For debug symbols, push unstripped libraries to <code>/data/local/symbols</code>:</p>
     60 <pre class="devsite-click-to-copy">
     61 <code class="devsite-terminal">adb shell mkdir /data/local/symbols</code>
     62 <code class="devsite-terminal">adb push $OUT/symbols /data/local/symbols</code>
     63 </pre>
     64 
     65 <h2 id=boot-valgrind>Using during boot up</h2>
     66 <p>To use Valgrind during boot up, edit
     67 <code>out/target/product/XXXX/root/init.rc</code> by changing</p>
     68 <pre class="devsite-click-to-copy">service example /system/bin/foo --arg1 --arg2</pre>
     69 <p>to:</p>
     70 <pre class="devsite-click-to-copy">>service example /system/bin/logwrapper /system/bin/valgrind /system/bin/foo --arg1 --arg2</pre>
     71 <p>To see the effects, create a <code>boot.img</code> and reflash the
     72 device.</p>
     73 
     74   </body>
     75 </html>
     76