blob: 1701f5377db0fee5a78615895e3c8808851fbcf9 [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
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>
Mathias Agopian002e1e52013-05-06 20:20:50 -070012#include <binder/TextOutput.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070013#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
Felipe Lemebbfd2b82016-02-03 11:16:27 -080029static void usage() {
30 fprintf(stderr,
31 "usage: dumpsys\n"
32 " To dump all services.\n"
33 "or:\n"
34 " dumpsys [--help | -l | SERVICE [ARGS]]\n"
35 " --help: shows this help\n"
36 " -l: only list services, do not dump them\n"
37 " SERVICE: dumps only service SERVICE, optionally passing ARGS to it\n");
38}
39
Colin Crossf45fa6b2012-03-26 12:38:26 -070040int main(int argc, char* const argv[])
41{
JP Abgrall3e03d3f2012-05-11 14:14:09 -070042 signal(SIGPIPE, SIG_IGN);
Colin Crossf45fa6b2012-03-26 12:38:26 -070043 sp<IServiceManager> sm = defaultServiceManager();
44 fflush(stdout);
45 if (sm == NULL) {
Felipe Lemebbfd2b82016-02-03 11:16:27 -080046 ALOGE("Unable to get default service manager!");
Colin Crossf45fa6b2012-03-26 12:38:26 -070047 aerr << "dumpsys: Unable to get default service manager!" << endl;
48 return 20;
49 }
50
51 Vector<String16> services;
52 Vector<String16> args;
keunyoungcaad5552013-06-13 15:08:51 -070053 bool showListOnly = false;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080054 if (argc == 2) {
55 if (strcmp(argv[1], "--help") == 0) {
56 usage();
57 return 0;
58 }
59 if (strcmp(argv[1], "-l") == 0) {
60 showListOnly = true;
61 }
keunyoungcaad5552013-06-13 15:08:51 -070062 }
63 if ((argc == 1) || showListOnly) {
Colin Crossf45fa6b2012-03-26 12:38:26 -070064 services = sm->listServices();
65 services.sort(sort_func);
66 args.add(String16("-a"));
67 } else {
68 services.add(String16(argv[1]));
69 for (int i=2; i<argc; i++) {
70 args.add(String16(argv[i]));
71 }
72 }
73
74 const size_t N = services.size();
75
76 if (N > 1) {
77 // first print a list of the current services
78 aout << "Currently running services:" << endl;
Felipe Lemebbfd2b82016-02-03 11:16:27 -080079
Colin Crossf45fa6b2012-03-26 12:38:26 -070080 for (size_t i=0; i<N; i++) {
81 sp<IBinder> service = sm->checkService(services[i]);
82 if (service != NULL) {
83 aout << " " << services[i] << endl;
84 }
85 }
86 }
87
keunyoungcaad5552013-06-13 15:08:51 -070088 if (showListOnly) {
89 return 0;
90 }
91
Colin Crossf45fa6b2012-03-26 12:38:26 -070092 for (size_t i=0; i<N; i++) {
93 sp<IBinder> service = sm->checkService(services[i]);
94 if (service != NULL) {
95 if (N > 1) {
96 aout << "------------------------------------------------------------"
97 "-------------------" << endl;
98 aout << "DUMP OF SERVICE " << services[i] << ":" << endl;
99 }
100 int err = service->dump(STDOUT_FILENO, args);
101 if (err != 0) {
102 aerr << "Error dumping service info: (" << strerror(err)
103 << ") " << services[i] << endl;
104 }
105 } else {
106 aerr << "Can't find service: " << services[i] << endl;
107 }
108 }
109
110 return 0;
111}