dumpsys 的作用
dumpsys 是 Android 系统中一个功能强大的诊断工具,主要用于:
- 系统服务状态查询:获取正在运行的 system services 的状态信息
- 调试分析:检查内存、电池、网络、Activity、进程等状态
- 性能分析:分析应用性能问题,检查服务运行状况
- 故障排查:诊断系统或应用异常
- 信息导出:生成系统状态快照供后续分析
常用命令示例:
bash
# 查看所有可用的系统服务
dumpsys -l
# 查看 Activity 状态
dumpsys activity activities
# 查看内存信息
dumpsys meminfo
# 查看电池状态
dumpsys batterystats
# 查看特定包名的信息
dumpsys package com.example.app
Android 默认的日志记录方式
1. Logcat(最常用)
- 系统日志:内核、系统服务、应用日志
- 实时查看 :
adb logcat - 过滤级别:V/D/I/W/E(Verbose/Debug/Info/Warning/Error)
- 缓冲区:main, system, events, radio, crash 等
2. System Tracing (Perfetto/Systrace)
-
性能跟踪:CPU、GPU、内存、功耗、应用启动等
-
图形化分析 :Chrome 浏览器打开
.perfetto-trace或.html文件 -
启用方式:
bash# 命令行捕获 adb shell perfetto --config :test --out /data/misc/perfetto-traces/trace.perfetto-trace # 或使用 Systrace python systrace.py -o trace.html sched gfx view wm
3. Bug Report
-
完整系统快照:包含 logs、dumpsys、tombstones、ANR traces 等
-
生成方式:
bashadb bugreport # 或设备上:设置 → 开发者选项 → 错误报告
4. ANR Traces
-
应用无响应 :
/data/anr/traces.txt -
获取方式:
bashadb pull /data/anr/traces.txt
5. Tombstones
- Native 层崩溃 :
/data/tombstones/ - 用于分析 C/C++ 层崩溃
6. Dropbox
- 系统异常收集:crash、ANR、watchdog、低内存等
- 位置:
/data/system/dropbox/
7. Event Log (events 缓冲区)
- 系统事件:安装、启动、点击等系统事件
- 查看:
adb logcat -b events
8. Kernel Log (kmsg/dmesg)
-
内核日志:驱动程序、内核事件
-
查看:
bashadb shell dmesg adb shell cat /proc/kmsg
9. Mainline Logs
- 模块化组件日志:Google Play 系统更新模块日志
控制台常用打印/调试命令
系统状态类
bash
# 进程和内存
adb shell ps -A # 所有进程
adb shell top # 实时进程状态
adb shell procrank # 进程内存排名
adb shell showmap <pid> # 进程内存映射
# 系统属性
adb shell getprop # 所有系统属性
adb shell wm size # 屏幕分辨率
adb shell dumpsys window displays # 显示信息
# 包管理
adb shell pm list packages # 所有应用包名
adb shell dumpsys package <pkg> # 包详细信息
性能分析类
bash
# CPU/GPU
adb shell cpuinfo # CPU信息
adb shell dumpsys gfxinfo # 图形性能
adb shell dumpsys SurfaceFlinger --latency # 帧率
# 电池和功耗
adb shell dumpsys batterystats --reset
adb shell dumpsys battery # 电池状态
# 网络
adb shell netstat # 网络连接
adb shell dumpsys netstats # 网络统计
adb shell dumpsys connectivity # 连接状态
存储和文件
bash
adb shell df # 磁盘空间
adb shell du -sh /path # 目录大小
adb shell ls -la /data/logs/ # 日志文件列表
调试辅助
bash
# 输入事件
adb shell input keyevent KEYCODE_HOME # 模拟按键
adb shell input tap x y # 模拟点击
adb shell input swipe x1 y1 x2 y2 # 模拟滑动
# 截图和录屏
adb shell screencap /sdcard/screen.png
adb shell screenrecord /sdcard/demo.mp4
广播和组件
bash
# 发送广播
adb shell am broadcast -a <action>
# 启动组件
adb shell am start -n <component>
adb shell am startservice <component>
# Activity 栈
adb shell dumpsys activity intents
adb shell dumpsys activity broadcasts
实用技巧
bash
# 同时查看多个日志源
adb logcat -b main -b system -b events -b crash
# 按优先级过滤
adb logcat *:E # 只显示错误
adb logcat *:W # 警告及以上
# 按标签过滤
adb logcat -s ActivityManager PackageManager
# 清空日志
adb logcat -c
这些工具组合使用可以全面诊断 Android 系统的各种问题,从应用崩溃到性能优化,从系统异常到功耗分析。