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" |
| 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 Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 40 | int main(int argc, char* const argv[]) |
| 41 | { |
JP Abgrall | 3e03d3f | 2012-05-11 14:14:09 -0700 | [diff] [blame] | 42 | signal(SIGPIPE, SIG_IGN); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 43 | sp<IServiceManager> sm = defaultServiceManager(); |
| 44 | fflush(stdout); |
| 45 | if (sm == NULL) { |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame^] | 46 | ALOGE("Unable to get default service manager!"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 47 | aerr << "dumpsys: Unable to get default service manager!" << endl; |
| 48 | return 20; |
| 49 | } |
| 50 | |
| 51 | Vector<String16> services; |
| 52 | Vector<String16> args; |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 53 | bool showListOnly = false; |
Felipe Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame^] | 54 | 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 | } |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 62 | } |
| 63 | if ((argc == 1) || showListOnly) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 64 | 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 Leme | bbfd2b8 | 2016-02-03 11:16:27 -0800 | [diff] [blame^] | 79 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 80 | 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 | |
keunyoung | caad555 | 2013-06-13 15:08:51 -0700 | [diff] [blame] | 88 | if (showListOnly) { |
| 89 | return 0; |
| 90 | } |
| 91 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 92 | 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 | } |