一、Logcat
二、Dumpsys
C:\Users\pengcheng.ding>adb shell dumpsys --help
usage: dumpsys
To dump all services.
or:
dumpsys [-t TIMEOUT] [--priority LEVEL] [--clients] [--dump] [--pid] [--thread] [--help | -l | --skip SERVICES | SERVICE [ARGS]]
--help: shows this help
-l: only list services, do not dump them
-t TIMEOUT_SEC: TIMEOUT to use in seconds instead of default 10 seconds
-T TIMEOUT_MS: TIMEOUT to use in milliseconds instead of default 10 seconds
--clients: dump client PIDs instead of usual dump
--dump: ask the service to dump itself (this is the default)
--pid: dump PID instead of usual dump
--proto: filter services that support dumping data in proto format. Dumps
will be in proto format.
--priority LEVEL: filter services based on specified priority
LEVEL must be one of CRITICAL | HIGH | NORMAL
--skip SERVICES: dumps all services but SERVICES (comma-separated list)
--stability: dump binder stability information instead of usual dump
--thread: dump thread usage instead of usual dump
SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it
dumpsys基本命令如上,adb shell dumpsys xxx,其中xxx通常为android servicemanager里面注册的服务,只要adb shell dumpsys -l能输出的服务都可以通过此命令来dumpsys一些关键信息。如下依次来介绍dumpsys实现原理。
1、dumpsys进程
//frameworks/native/cmds/dumpsys/Android.bp
cc_binary {
name: "dumpsys",
defaults: ["dumpsys_defaults"],
srcs: [ "main.cpp", ],
shared_libs: [ "packagemanager_aidl-cpp", ],
}
cc_defaults {
name: "dumpsys_defaults",
cflags: [ "-Wall", "-Werror", ],
srcs: [ "dumpsys.cpp", ],
shared_libs: [
"libbase",
"libutils",
"liblog",
"libbinder",
"libbinderdebug",
],
static_libs: [ "libserviceutils", ],
}
//frameworks/native/cmds/dumpsys/main.cpp
int main(int argc, char* const argv[]) {
signal(SIGPIPE, SIG_IGN);
sp<IServiceManager> sm = defaultServiceManager();
fflush(stdout);
if (sm == nullptr) {
ALOGE("Unable to get default service manager!");
std::cerr << "dumpsys: Unable to get default service manager!" << std::endl;
return 20;
}
Dumpsys dumpsys(sm.get()); //核心逻辑
return dumpsys.main(argc, argv);
}
如上代码dumpsys就是一个普通的native进程,核心的逻辑还是frameworks/native/cmds/dumpsys/dumpsys.cpp的main函数:
因此dumpsys xxx最后是通过servicemanager让系统Service进行了dump,常用的dumpsys命令如下:
adb shell dumpsys activity //查询AMS服务相关信息
adb shell dumpsys package //查询包管理相关信息
adb shell dumpsys window //查询WMS服务相关信息
adb shell dumpsys meminfo/cpuinfo/gfxinfo //查询内存/CPU/帧率相关信息
adb shell dumpsys power/batterystats/battery //查询电源相关信息/电池状态/电池
adb shell dumpsys alarm/location //查询闹钟/位置相关信息
adb shell dumpsys user/device_policy //查询用户相关信息
adb shell dumpsys connectivity/netpolicy/netstats //查询网络连接/策略/状态
adb shell dumpsys network_management //查询网络管理相关信息
所有的service定义和service注册的字符串全在如下文件进行定义Context.java - OpenGrok cross reference for /frameworks/base/core/java/android/content/Context.java