Android dumpsys命令用法

zhuls1990 8年前
   <blockquote>     <p>dumpsys命令功能很强大,能dump系统服务的各种状态,非常有必要熟悉该命令的用法以及含义。</p>    </blockquote>    <h2>一、 dumpsys命令</h2>    <h3>1.1 服务列表</h3>    <p>不同的Android系统版本支持的命令有所不同,可通过下面命令查看当前手机所支持的dump服务,先进入adb shell,再执行如下命令:<code>dumpsys -l</code>。 这些服务名或许你并看不出其调用的哪个服务,那么这时可以通过下面指令:<code>service list</code>。</p>    <p>表一:</p>    <table>     <thead>      <tr>       <th>服务名</th>       <th>类名</th>       <th>功能</th>      </tr>     </thead>     <tbody>      <tr>       <td>activity</td>       <td>ActivityManagerService</td>       <td>AMS相关信息</td>      </tr>      <tr>       <td>package</td>       <td>PackageManagerService</td>       <td>PMS相关信息</td>      </tr>      <tr>       <td>window</td>       <td>WindowManagerService</td>       <td>WMS相关信息</td>      </tr>      <tr>       <td>input</td>       <td>InputManagerService</td>       <td>IMS相关信息</td>      </tr>      <tr>       <td>power</td>       <td>PowerManagerService</td>       <td>PMS相关信息</td>      </tr>      <tr>       <td>batterystats</td>       <td>BatterystatsService</td>       <td>电池统计信息</td>      </tr>      <tr>       <td>battery</td>       <td>BatteryService</td>       <td>电池信息</td>      </tr>      <tr>       <td>alarm</td>       <td>AlarmManagerService</td>       <td>闹钟信息</td>      </tr>      <tr>       <td>dropbox</td>       <td>DropboxManagerService</td>       <td>调试相关</td>      </tr>      <tr>       <td>procstats</td>       <td>ProcessStatsService</td>       <td>进程统计</td>      </tr>      <tr>       <td>cpuinfo</td>       <td>CpuBinder</td>       <td>CPU</td>      </tr>      <tr>       <td>meminfo</td>       <td>MemBinder</td>       <td>内存</td>      </tr>      <tr>       <td>gfxinfo</td>       <td>GraphicsBinder</td>       <td>图像</td>      </tr>      <tr>       <td>dbinfo</td>       <td>DbBinder</td>       <td>数据库</td>      </tr>     </tbody>    </table>    <p>表二:</p>    <table>     <thead>      <tr>       <th>服务名</th>       <th>功能</th>       <th> </th>      </tr>     </thead>     <tbody>      <tr>       <td>SurfaceFlinger</td>       <td>图像相关</td>       <td> </td>      </tr>      <tr>       <td>appops</td>       <td>app使用情况</td>       <td> </td>      </tr>      <tr>       <td>permission</td>       <td>权限</td>       <td> </td>      </tr>      <tr>       <td>processinfo</td>       <td>进程服务</td>       <td> </td>      </tr>      <tr>       <td>batteryproperties</td>       <td>电池相关</td>       <td> </td>      </tr>      <tr>       <td>audio</td>       <td>查看声音信息</td>       <td> </td>      </tr>      <tr>       <td>netstats</td>       <td>查看网络统计信息</td>       <td> </td>      </tr>      <tr>       <td>diskstats</td>       <td>查看空间free状态</td>       <td> </td>      </tr>      <tr>       <td>jobscheduler</td>       <td>查看任务计划</td>       <td> </td>      </tr>      <tr>       <td>wifi</td>       <td>wifi信息</td>       <td> </td>      </tr>      <tr>       <td>diskstats</td>       <td> </td>       <td>磁盘情况</td>      </tr>      <tr>       <td>usagestats</td>       <td>用户使用情况</td>       <td> </td>      </tr>      <tr>       <td>devicestoragemonitor</td>       <td>设备信息</td>       <td> </td>      </tr>      <tr>       <td>。。。</td>       <td>。。。</td>       <td> </td>      </tr>     </tbody>    </table>    <p>未完待续…</p>    <h3>1.2 查询服务</h3>    <p>通过下面命令可打印具体某一项服务:<code>dumpsys <service></code>,其中便是前面表格中的服务名,比如:</p>    <pre>  <code>dumpsys cpuinfo //打印一段时间进程的CPU使用百分比排行榜  dumpsys meminfo -h  //查看dump内存的帮助信息  dumpsys package <packagename> //查看指定包的信息  </code></pre>    <p>系统服务非常之多,那么接下来将重点说说其中之一:<code>dumpsys activity</code>用法.</p>    <h2>二、 Activity</h2>    <pre>  <code>dumpsys activity [options] [cmd]  </code></pre>    <p>下面分别说说options和cmd有哪些可选值</p>    <h3>2.1 options</h3>    <p>options可选值:</p>    <ul>     <li><code>-a</code>:dump所有;</li>     <li><code>-c</code>:dump客户端;</li>     <li><code>-p [package]</code>:dump指定的包名;</li>     <li><code>-h</code>:输出帮助信息;</li>    </ul>    <p><code>dumpsys activity</code>等价于依次输出下面7条指令:</p>    <pre>  <code>dumpsys activity intents  dumpsys activity broadcasts  dumpsys activity providers  dumpsys activity services  dumpsys activity recents  dumpsys activity activities  dumpsys activity processes  </code></pre>    <h3>2.2 cmd</h3>    <p>cmd可选值</p>    <table>     <thead>      <tr>       <th>cmd</th>       <th>解释</th>       <th>缩写</th>      </tr>     </thead>     <tbody>      <tr>       <td>activities</td>       <td>activity状态</td>       <td>a</td>      </tr>      <tr>       <td><strong>broadcasts</strong></td>       <td>广播</td>       <td>b</td>      </tr>      <tr>       <td><strong>intents</strong></td>       <td>pending intent状态</td>       <td>i</td>      </tr>      <tr>       <td><strong>processes</strong></td>       <td>进程</td>       <td>p</td>      </tr>      <tr>       <td>oom</td>       <td>内存溢出</td>       <td>o</td>      </tr>      <tr>       <td><strong>services</strong></td>       <td>Service状态</td>       <td>s</td>      </tr>      <tr>       <td>service</td>       <td>service状态(Client端)</td>       <td> </td>      </tr>      <tr>       <td><strong>providers</strong></td>       <td>ContentProvider状态</td>       <td>prov</td>      </tr>      <tr>       <td>provider</td>       <td>ContentProvider状态(Client端)</td>       <td> </td>      </tr>      <tr>       <td>associations</td>       <td>tracked app associations</td>       <td>as</td>      </tr>      <tr>       <td>permissions</td>       <td>URI permission grant state</td>       <td>perm</td>      </tr>      <tr>       <td><strong>package</strong></td>       <td>package相关信息</td>       <td> </td>      </tr>      <tr>       <td>all</td>       <td>所有的activities信息</td>       <td> </td>      </tr>      <tr>       <td>recents</td>       <td>recent activity状态</td>       <td>r</td>      </tr>      <tr>       <td>top</td>       <td>top activity信息</td>       <td> </td>      </tr>      <tr>       <td>write</td>       <td>将状态持久化到存储区</td>       <td> </td>      </tr>      <tr>       <td>track-associations</td>       <td>使能association tracking</td>       <td> </td>      </tr>      <tr>       <td>untrack-associations</td>       <td>禁止和清空association tracking</td>       <td> </td>      </tr>     </tbody>    </table>    <ul>     <li>cmd:上表加粗项是指直接跟<code>包名</code>,另外services和providers还可以跟<code>组件名</code>;</li>     <li>缩写:基本都是cmd首字母或者前几个字母,用cmd和缩写是等效: dumpsys activity broadcasts dumpsys activity b //等效</li>    </ul>    <h2>三、场景</h2>    <p>下面以新浪微博App作为实例,由于输出结果较多,每个场景截图只挑选部分重要的信息。</p>    <p><strong>场景1:查询某个App所有的Service状态</strong></p>    <pre>  <code>dumpsys activity s com.sina.weibo  </code></pre>    <p><img alt="Android dumpsys命令用法" src="https://simg.open-open.com/show/c3ef062ea0378deceeeb1d38bf0fc8b9.png" width="892" height="324"></p>    <p>解读:</p>    <ul>     <li>Service类名为<code>com.morgoo.droidplugin.PluginManagerService</code>;</li>     <li>运行在进程pid=<code>7220</code>,进程名为<code>com.sina.weibo</code>,uid=<code>10094</code>;</li>     <li>通过bindService连接该服务的进程pid=<code>7306</code>,进程名为<code>com.sina.weibo:PluginP03</code>。</li>    </ul>    <p>当然还有packageName,baseDir(apk路径),dataDir(apk数据路径),createTime等各种信息。另外,新浪微博采用的是360开源的Android插件机制(<code>com.morgoo.droidplugin</code>),主要用于hotfix等功能。</p>    <p><strong>场景2:查询某个App所有的广播状态</strong></p>    <pre>  <code> dumpsys activity s com.sina.weibo  </code></pre>    <p><img alt="Android dumpsys命令用法" src="https://simg.open-open.com/show/4c644391af8a5039cc152e488ab26dab.png" width="860" height="176"></p>    <p>解读:</p>    <ul>     <li>android.intent.action.SCREEN_ON代表手机亮屏广播;</li>     <li>接收该广播的receiver有很多个,其中一个所在进程为pid=<code>7220</code>,进程名为<code>com.sina.weibo</code></li>    </ul>    <p><strong>场景3:查询某个App所有的Activity状态</strong></p>    <p>输出结果较多,尤其是<code>View Hierarchy</code>,下面截取部分:</p>    <pre>  <code>dumpsys activity a com.sina.weibo  </code></pre>    <p><img alt="Android dumpsys命令用法" src="https://simg.open-open.com/show/bb042caac1893fdb9781a01687c9f81a.png" width="963" height="198"></p>    <p>解读:</p>    <ul>     <li>格式:TaskRecord{Hashcode #TaskId Affinity UserId=0 Activity个数=1};所以上图信息解析后就是TaskId=<code>1802</code>,Affinity=<code>com.sina.weibo</code>,当前Task中Activity个数为1。</li>     <li>effectiveUid为当前task所属Uid,mCallingUid为调用者Uid=u0a94,mCallingPackage为调用者包名,这里是<code>com.sina.weibo</code>;</li>     <li>realActivity:task中的已启动的Activity组件名<code>com.sina.weibo/.SplashActivity</code>。</li>    </ul>    <p><strong>场景4:查询某个App的进程状态</strong></p>    <pre>  <code>dumpsys activity p com.sina.weibo  </code></pre>    <p><img alt="Android dumpsys命令用法" src="https://simg.open-open.com/show/506eb1cefe48515efdc97b786af2ae01.png" width="954" height="531"></p>    <ul>     <li>格式:ProcessRecord{Hashcode pid:进程名/uid},进程pid=7306,进程名为<code>com.sina.weibo:PluginP03</code>,uid=10094.</li>     <li>该进程中还有Services,Connections, Providers, Receivers,可以看出该进程是没有Activity的进程。</li>    </ul>    <p><strong>其他</strong></p>    <p>还有很多场景,会用到不同的参数,这里就不再一一列举,建议大家多去尝试,慢慢地就更加熟练,再比如:</p>    <pre>  <code>dumpsys activity top //当前界面app状态  dumpsys activity oom //进程oom状态</code></pre>    <p>via: http://gityuan.com/2016/05/14/dumpsys-command/ </p>