dumpsys工具使用
我们知道Android使用ServiceManager服务进程来管理系统所有的服务,在系统启动时,每个服务必须注册到ServiceManager进程中,那如何查看系统运行了那些服务呢?ServiceManager提供了listServices接口来罗列出系统注册的所有服务。Android提供了dumpsys工具来dump出所有的服务信息,通过以下命令可以查看系统注册的所有服务:
Currently running services:
SurfaceFlinger
country_detector
cpuinfo
dbinfo
device_policy
devicestoragemonitor
diskstats
drm.drmManager
dropbox
entropy
fm
gfxinfo
hardware
input
isms
location
lock_settings
meminfo
network_management
notification
package
permission
phone
power
samplingprofiler
scheduling_policy
search
sensorservice
serial
servicediscovery
sim_manager
simphonebook
...
-------------------------------------------------------------------------------
DUMP OF SERVICE SurfaceFlinger:
Visible layers (count = 4)
...
也可以dump指定服务信息:
adb shell dumpsys SurfaceFlinger
Visible layers (count = 4)
+ Layer 0x41ba5008 (com.android.settings/com.android.settings.SprdUsbSettings)
Region transparentRegion (this=0x41ba51e4, count=0)
Region transparentRegionScreen (this=0x41ba503c, count=0)
Region visibleRegionScreen (this=0x41ba5018, count=0)
z= 21005, pos=(0,0), size=( 480, 854), crop=( 0, 38, 480, 854), isOp
aque=1, needsDithering=0, invalidate=0, alpha=0x00, flags=0x00000001, tr=[1.00,
0.00][0.00, 1.00]
client=0x41c14ee0, identity=7
format= 2, activeBuffer=[ 480x 854: 480, 2], transform-hint=0x00, queued-
frames=0, mRefreshPending=0
...
dumpsys源码分析
dumpsys工具比较简单,就是调用ServiceManager服务的listServices来查询系统注册的所有服务,并且通过checkService接口来获取服务的Binder远程代理对象,使用每个服务的dump函数来打印该服务的相关信息。
frameworks\native\cmds\dumpsys\dumpsys.cpp
int main(int argc, char* const argv[])
- {
- signal(SIGPIPE, SIG_IGN);
-
- sp<IServiceManager> sm = defaultServiceManager();
- fflush(stdout);
- if (sm == NULL) {
- ALOGE("Unable to get default service manager!");
- aerr << "dumpsys: Unable to get default service manager!" << endl;
- return 20;
- }
- Vector<String16> services;
- Vector<String16> args;
-
- if (argc == 1) {
-
- services = sm->listServices();
- services.sort(sort_func);
- args.add(String16("-a"));
- } else {
-
- services.add(String16(argv[1]));
- for (int i=2; i<argc; i++) {
- args.add(String16(argv[i]));
- }
- }
-
- const size_t N = services.size();
- if (N > 1) {
-
- aout << "Currently running services:" << endl;
- for (size_t i=0; i<N; i++) {
- sp<IBinder> service = sm->checkService(services[i]);
- if (service != NULL) {
- aout << " " << services[i] << endl;
- }
- }
- }
-
- for (size_t i=0; i<N; i++) {
- sp<IBinder> service = sm->checkService(services[i]);
- if (service != NULL) {
- if (N > 1) {
- aout << "------------------------------------------------------------"
- "-------------------" << endl;
- aout << "DUMP OF SERVICE " << services[i] << ":" << endl;
- }
- int err = service->dump(STDOUT_FILENO, args);
- if (err != 0) {
- aerr << "Error dumping service info: (" << strerror(err)
- << ") " << services[i] << endl;
- }
- } else {
- aerr << "Can't find service: " << services[i] << endl;
- }
- }
- return 0;
- }