欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android中dumpsys函数介绍与使用

程序员文章站 2022-05-10 22:18:21
...

dumpsys是系统中重要的函数,我们来看看它如何使用,都能dump出来一些什么东西

 

首先我们来看看dumpsys的源码,在4.1中,它在framework/native目录下面

frameworks/native/cmds/dumpsys/dumpsys.cpp

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) {
	// 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;
		}
	}
}

for (size_t i=0; i<N; i++) {
	sp<IBinder> service = sm->checkService(services[i]);
	if (service != NULL) {
		if (N > 1) {//dump多个Service时,Service信息之间的分隔
			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;
	}
}

我只会java,上面的代码还是留给大家自己阅读吧,我就不详细解释了……

如果参数个数=1,则在ServiceManager中查找注册的Service

否则查找指定的Service,并且将剩余参数传给Serivce

 

 

如果执行dumpsys命令,那么将会dump所有Service,每个Service之间会分隔开

如果dumpsys一个没有注册的Service,那么则提示找不到Service,比如:

# dumpsys adb                                                   

Can't find service: adb

 

 

 

那么我们到底都可以dump一些什么呢?

其实最简单的办法就是执行dumpsys,看看其结果,下面是我手机中执行的结果(HTCg14)

# dumpsys
Currently running services:
  DeviceManager3LM
  Encryption3LMService
  HtcAppUsageStatsService
  SurfaceFlinger
  accessibility
  account
  activity
  alarm
  appwidget
  audio
  backup
  battery
  batteryinfo
  bluetooth
  bluetooth_a2dp
  bluetooth_hid
  bt_fm_management
  clipboard
  connectivity
  content
  country_detector
  cpuinfo
  device_policy
  devicestoragemonitor
  diskstats
  display
  drm.drmManager
  dropbox
  entropy
  gfxinfo
  hardware
  htc_checkin
  htchardware
  htctelephony
  htctelephonyinternal
  input_method
  iphonesubinfo
  isms
  location
  media.audio_flinger
  media.audio_policy
  media.camera
  media.player
  meminfo
  mount
  netpolicy
  netstats
  network_management
  notification
  package
  permission
  phone
  power
  samplingprofiler
  search
  secfirewall
  secloader
  sensorservice
  simphonebook
  sip
  statusbar
  stub_activity
  stub_audio
  stub_iphonesubinfo
  stub_isms
  stub_location
  stub_phone
  stub_telephony.registry
  telephony.registry
  textservices
  throttle
  uimode
  usagestats
  usb
  usbnet
  userbehavior
  vibrator
  wallpaper
  wifi
  wifip2p
  window
-------------------------------------------------------------------------------
DUMP OF SERVICE DeviceManager3LM:
-------------------------------------------------------------------------------
DUMP OF SERVICE Encryption3LMService:

由于dump service的log实在太长,我就截取了最前面的“Currently running services”和两个Service的dump信息

为什么DeviceManager3LM和Encryption3LMService后面没有内容?这和Service的内部实现有关,也许它只是写了一个空的dump函数,也许有什么其他条件。

这个问题一会再说,我们先看看这些Service都是在哪注册的

其中大部分Service都是在SystemServer中的main函数里注册的,它调用了一个叫init2的函数

public static final void init2() {
    Slog.i(TAG, "Entered the Android system server!");
    Thread thr = new ServerThread();
    thr.setName("android.server.ServerThread");
    thr.start();
}

init2非常简单,所有任务都交给新线程ServerThread来处理

 

在ServerThread的run函数中,注册了系统大部分的Service,有兴趣的同学可以看看这部分代码

这部分代码有点长(大概800行),因为它一直重复这建立各种Service,然后注册,输出log

比如下面的WifiP2pService

try {
    Slog.i(TAG, "Wi-Fi P2pService");
    wifiP2p = new WifiP2pService(context);
    ServiceManager.addService(Context.WIFI_P2P_SERVICE, wifiP2p);
} catch (Throwable e) {
    reportWtf("starting Wi-Fi P2pService", e);
}

其中WIFI_P2P_SERVICE就是服务的名字

public static final String WIFI_P2P_SERVICE = "wifip2p";

 

 

当执行dumpsys wifip2p的时候,并没有任何输出,为什么呢?我们看看WifiP2pService的代码

@Override
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
    if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
            != PackageManager.PERMISSION_GRANTED) {
        pw.println("Permission Denial: can't dump WifiP2pService from from pid="
                + Binder.getCallingPid()
                + ", uid=" + Binder.getCallingUid());
        return;
    }
}

现在似乎可以理解为什么dumpsys wifip2p的时候没有输出了

 

 

我们再来看一个简单的例子

# dumpsys diskstats

Latency: 5ms [512B Data Write]

Data-Free: 362888K / 1161104K total = 31% free

Cache-Free: 116756K / 120900K total = 96% free

System-Free: 133036K / 806284K total = 16% free

ServiceManager.addService("diskstats", new DiskStatsService(context));
这回大家知道去哪看源码了吧,下面是DiskStatsService的dump函数
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
    ……
    reportFreeSpace(Environment.getDataDirectory(), "Data", pw);
    reportFreeSpace(Environment.getDownloadCacheDirectory(), "Cache", pw);
    reportFreeSpace(new File("/system"), "System", pw);
}
 
private void reportFreeSpace(File path, String name, PrintWriter pw) {
    try {
        StatFs statfs = new StatFs(path.getPath());
        long bsize = statfs.getBlockSize();
        long avail = statfs.getAvailableBlocks();
        long total = statfs.getBlockCount();
        if (bsize <= 0 || total <= 0) {
            throw new IllegalArgumentException(
                    "Invalid stat: bsize=" + bsize + " avail=" + avail + " total=" + total);
        }

        pw.print(name);
        pw.print("-Free: ");
        pw.print(avail * bsize / 1024);
        pw.print("K / ");
        pw.print(total * bsize / 1024);
        pw.print("K total = ");
        pw.print(avail * 100 / total);
        pw.println("% free");
    } catch (IllegalArgumentException e) {
        pw.print(name);
        pw.print("-Error: ");
        pw.println(e.toString());
        return;
    }
}
大家肯定还对其他的Service感兴趣,比如notification!
之前我的一篇关于android安全的文章提到过用dumpsys notification能作些什么
android手机root后的安全问题 (二)

 

 

 

 

 

 

转贴请保留以下链接

本人blog地址

http://su1216.iteye.com/

http://blog.csdn.net/su1216/