dumpsys.cpp 2.4 KB

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