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 <utils/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
22using namespace android;
23
24static int sort_func(const String16* lhs, const String16* rhs)
25{
26    return lhs->compare(*rhs);
27}
28
29int 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    if (argc == 1) {
43        services = sm->listServices();
44        services.sort(sort_func);
45        args.add(String16("-a"));
46    } else {
47        services.add(String16(argv[1]));
48        for (int i=2; i<argc; i++) {
49            args.add(String16(argv[i]));
50        }
51    }
52
53    const size_t N = services.size();
54
55    if (N > 1) {
56        // first print a list of the current services
57        aout << "Currently running services:" << endl;
58
59        for (size_t i=0; i<N; i++) {
60            sp<IBinder> service = sm->checkService(services[i]);
61            if (service != NULL) {
62                aout << "  " << services[i] << endl;
63            }
64        }
65    }
66
67    for (size_t i=0; i<N; i++) {
68        sp<IBinder> service = sm->checkService(services[i]);
69        if (service != NULL) {
70            if (N > 1) {
71                aout << "------------------------------------------------------------"
72                        "-------------------" << endl;
73                aout << "DUMP OF SERVICE " << services[i] << ":" << endl;
74            }
75            int err = service->dump(STDOUT_FILENO, args);
76            if (err != 0) {
77                aerr << "Error dumping service info: (" << strerror(err)
78                        << ") " << services[i] << endl;
79            }
80        } else {
81            aerr << "Can't find service: " << services[i] << endl;
82        }
83    }
84
85    return 0;
86}
87