分类: Android_java 2013-09-20 13:58 2416人阅读 评论(1) 收藏 举报
目录(?)[+]
我们知道Android使用ServiceManager服务进程来管理系统所有的服务,在系统启动时,每个服务必须注册到 ServiceManager进程中,那如何查看系统运行了那些服务呢?ServiceManager提供了listServices接口来罗列出系统注 册的所有服务。Android提供了dumpsys工具来dump出所有的服务信息,通过以下命令可以查看系统注册的所有服务:
[java] view plaincopy
adb shell dumpsys
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指定服务信息:
[java] view plaincopy
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工具比较简单,就是调用ServiceManager服务的listServices来查询系统注册的所有服务,并且通过checkService接口来获取服务的Binder远程代理对象,使用每个服务的dump函数来打印该服务的相关信息。
frameworks\native\cmds\dumpsys\dumpsys.cpp
[java] view plaincopy
int main(int argc, char* const argv[])
{
signal(SIGPIPE, SIG_IGN);
//取得ServiceManager服务的远程Binder代理对象
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;
//如果命令行参数只有1个
if (argc == 1) {
//使用ServiceManager服务的Binder远程代理对象查询所有的服务
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]));
}
}
//根据服务名称查找服务对应的Binder代理对象,如果存在,打印该服务
const size_t N = services.size();
if (N > 1) {
// first print a list of the current services
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;
}
}
}
//调用服务的dump函数打印服务相关信息
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;
}