Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 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> |
Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 12 | #include <binder/TextOutput.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 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 | |
| 24 | static int sort_func(const String16* lhs, const String16* rhs) |
| 25 | { |
| 26 | return lhs->compare(*rhs); |
| 27 | } |
| 28 | |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 29 | static void usage() { |
| 30 | fprintf(stderr, |
| 31 | "usage: dumpsys\n" |
| 32 | " To dump all services.\n" |
| 33 | "or:\n" |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame^] | 34 | " dumpsys [--help | -l | --skip SERVICES | SERVICE [ARGS]]\n" |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 35 | " --help: shows this help\n" |
| 36 | " -l: only list services, do not dump them\n" |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame^] | 37 | " --skip SERVICES: dumps all services but SERVICES (comma-separated list)\n" |
| 38 | " SERVICE [ARGS]: dumps only service SERVICE, optionally passing ARGS to it\n"); |
| 39 | } |
| 40 | |
| 41 | bool IsSkipped(const Vector<String16>& skipped, const String16& service) { |
| 42 | for (const auto& candidate : skipped) { |
| 43 | if (candidate == service) { |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | return false; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 50 | int main(int argc, char* const argv[]) |
| 51 | { |
JP Abgrall | 3e03d3f | 2012-05-11 14:14:09 -0700 | [diff] [blame] | 52 | signal(SIGPIPE, SIG_IGN); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 53 | sp<IServiceManager> sm = defaultServiceManager(); |
| 54 | fflush(stdout); |
| 55 | if (sm == NULL) { |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 56 | ALOGE("Unable to get default service manager!"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 57 | aerr << "dumpsys: Unable to get default service manager!" << endl; |
| 58 | return 20; |
| 59 | } |
| 60 | |
| 61 | Vector<String16> services; |
| 62 | Vector<String16> args; |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame^] | 63 | Vector<String16> skippedServices; |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 64 | bool showListOnly = false; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 65 | if (argc == 2) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame^] | 66 | // 1 argument: check for special cases (-l or --help) |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 67 | if (strcmp(argv[1], "--help") == 0) { |
| 68 | usage(); |
| 69 | return 0; |
| 70 | } |
| 71 | if (strcmp(argv[1], "-l") == 0) { |
| 72 | showListOnly = true; |
| 73 | } |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 74 | } |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame^] | 75 | if (argc == 3) { |
| 76 | // 2 arguments: check for special cases (--skip SERVICES) |
| 77 | if (strcmp(argv[1], "--skip") == 0) { |
| 78 | char* token = strtok(argv[2], ","); |
| 79 | while (token != NULL) { |
| 80 | skippedServices.add(String16(token)); |
| 81 | token = strtok(NULL, ","); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | bool dumpAll = argc == 1; |
| 86 | if (dumpAll || !skippedServices.empty() || showListOnly) { |
| 87 | // gets all services |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 88 | services = sm->listServices(); |
| 89 | services.sort(sort_func); |
| 90 | args.add(String16("-a")); |
| 91 | } else { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame^] | 92 | // gets a specific service: |
| 93 | // first check if its name is not a special argument... |
| 94 | if (strcmp(argv[1], "--skip") == 0 || strcmp(argv[1], "-l") == 0) { |
| 95 | usage(); |
| 96 | return -1; |
| 97 | } |
| 98 | // ...then gets its arguments |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 99 | services.add(String16(argv[1])); |
| 100 | for (int i=2; i<argc; i++) { |
| 101 | args.add(String16(argv[i])); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | const size_t N = services.size(); |
| 106 | |
| 107 | if (N > 1) { |
| 108 | // first print a list of the current services |
| 109 | aout << "Currently running services:" << endl; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame] | 110 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 111 | for (size_t i=0; i<N; i++) { |
| 112 | sp<IBinder> service = sm->checkService(services[i]); |
| 113 | if (service != NULL) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame^] | 114 | bool skipped = IsSkipped(skippedServices, services[i]); |
| 115 | aout << " " << services[i] << (skipped ? " (skipped)" : "") << endl; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 120 | if (showListOnly) { |
| 121 | return 0; |
| 122 | } |
| 123 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 124 | for (size_t i=0; i<N; i++) { |
Felipe Leme | 859aef6 | 2016-02-03 12:17:10 -0800 | [diff] [blame^] | 125 | if (IsSkipped(skippedServices, services[i])) continue; |
| 126 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 127 | sp<IBinder> service = sm->checkService(services[i]); |
| 128 | if (service != NULL) { |
| 129 | if (N > 1) { |
| 130 | aout << "------------------------------------------------------------" |
| 131 | "-------------------" << endl; |
| 132 | aout << "DUMP OF SERVICE " << services[i] << ":" << endl; |
| 133 | } |
| 134 | int err = service->dump(STDOUT_FILENO, args); |
| 135 | if (err != 0) { |
| 136 | aerr << "Error dumping service info: (" << strerror(err) |
| 137 | << ") " << services[i] << endl; |
| 138 | } |
| 139 | } else { |
| 140 | aerr << "Can't find service: " << services[i] << endl; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |