1 /*
2  * Command that dumps interesting system state to the log.
3  *
4  */
5 
6 #define LOG_TAG "dumpsys"
7 
8 #include <utils/Log.h>
9 #include <binder/Parcel.h>
10 #include <binder/ProcessState.h>
11 #include <binder/IServiceManager.h>
12 #include <binder/TextOutput.h>
13 #include <utils/Vector.h>
14 
15 #include <getopt.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/time.h>
21 
22 using namespace android;
23 
sort_func(const String16 * lhs,const String16 * rhs)24 static int sort_func(const String16* lhs, const String16* rhs)
25 {
26     return lhs->compare(*rhs);
27 }
28 
main(int argc,char * const argv[])29 int main(int argc, char* const argv[])
30 {
31     signal(SIGPIPE, SIG_IGN);
32     sp<IServiceManager> sm = defaultServiceManager();
33     fflush(stdout);
34     if (sm == NULL) {
35 		ALOGE("Unable to get default service manager!");
36         aerr << "dumpsys: Unable to get default service manager!" << endl;
37         return 20;
38     }
39 
40     Vector<String16> services;
41     Vector<String16> args;
42     bool showListOnly = false;
43     if ((argc == 2) && (strcmp(argv[1], "-l") == 0)) {
44         showListOnly = true;
45     }
46     if ((argc == 1) || showListOnly) {
47         services = sm->listServices();
48         services.sort(sort_func);
49         args.add(String16("-a"));
50     } else {
51         services.add(String16(argv[1]));
52         for (int i=2; i<argc; i++) {
53             args.add(String16(argv[i]));
54         }
55     }
56 
57     const size_t N = services.size();
58 
59     if (N > 1) {
60         // first print a list of the current services
61         aout << "Currently running services:" << endl;
62 
63         for (size_t i=0; i<N; i++) {
64             sp<IBinder> service = sm->checkService(services[i]);
65             if (service != NULL) {
66                 aout << "  " << services[i] << endl;
67             }
68         }
69     }
70 
71     if (showListOnly) {
72         return 0;
73     }
74 
75     for (size_t i=0; i<N; i++) {
76         sp<IBinder> service = sm->checkService(services[i]);
77         if (service != NULL) {
78             if (N > 1) {
79                 aout << "------------------------------------------------------------"
80                         "-------------------" << endl;
81                 aout << "DUMP OF SERVICE " << services[i] << ":" << endl;
82             }
83             int err = service->dump(STDOUT_FILENO, args);
84             if (err != 0) {
85                 aerr << "Error dumping service info: (" << strerror(err)
86                         << ") " << services[i] << endl;
87             }
88         } else {
89             aerr << "Can't find service: " << services[i] << endl;
90         }
91     }
92 
93     return 0;
94 }
95