Add priority based dumpsys support to dumpstate

Adds a new version of dumpstate which calls dumpsys with different
priorities. Current version will remain INITIAL_VERSION (1.0) until tools
support the new version and services register with supported priorities.
Modified dumpsys to pass prioirty args to services.

BugReport format changes:
- removed service specific dumps and dump order changed
- Start of dumpsys section changed to
  DUMPSYS CRITICAL/HIGH/NORMAL
- Start of service dump changed to
  DUMP OF SERVICE CRITICAL/HIGH/NORMAL <servicename>

Bug: 27429130

Test: adb shell setprop dumpstate.version "1.0" && \
            adb bugreport ~/tmp_old.zip
Test: adb shell setprop dumpstate.version "2.0-dev-split-anr" && \
            adb bugreport ~/tmp_anr.zip
Test: adb shell setprop dumpstate.version "2.0-dev-priority-dumps" && \
            adb bugreport ~/tmp_new.zip

Change-Id: I9fd0f2d0e6d73b36cc8ee0f8239092ce83da9560
diff --git a/cmds/dumpsys/dumpsys.cpp b/cmds/dumpsys/dumpsys.cpp
index 8fe246b..239a2d5 100644
--- a/cmds/dumpsys/dumpsys.cpp
+++ b/cmds/dumpsys/dumpsys.cpp
@@ -25,6 +25,7 @@
 #include <binder/Parcel.h>
 #include <binder/ProcessState.h>
 #include <binder/TextOutput.h>
+#include <serviceutils/PriorityDumper.h>
 #include <utils/Log.h>
 #include <utils/Vector.h>
 
@@ -76,9 +77,26 @@
     return false;
 }
 
+static bool ConvertPriorityTypeToBitmask(String16& type, int& bitmask) {
+    if (type == PRIORITY_ARG_CRITICAL) {
+        bitmask = IServiceManager::DUMP_PRIORITY_CRITICAL;
+        return true;
+    }
+    if (type == PRIORITY_ARG_HIGH) {
+        bitmask = IServiceManager::DUMP_PRIORITY_HIGH;
+        return true;
+    }
+    if (type == PRIORITY_ARG_NORMAL) {
+        bitmask = IServiceManager::DUMP_PRIORITY_NORMAL;
+        return true;
+    }
+    return false;
+}
+
 int Dumpsys::main(int argc, char* const argv[]) {
     Vector<String16> services;
     Vector<String16> args;
+    String16 priorityType;
     Vector<String16> skippedServices;
     bool showListOnly = false;
     bool skipServices = false;
@@ -110,13 +128,8 @@
                 usage();
                 return 0;
             } else if (!strcmp(longOptions[optionIndex].name, "priority")) {
-                if (!strcmp(optarg, "CRITICAL")) {
-                    dumpPriority = IServiceManager::DUMP_PRIORITY_CRITICAL;
-                } else if (!strcmp(optarg, "HIGH")) {
-                    dumpPriority = IServiceManager::DUMP_PRIORITY_HIGH;
-                } else if (!strcmp(optarg, "NORMAL")) {
-                    dumpPriority = IServiceManager::DUMP_PRIORITY_NORMAL;
-                } else {
+                priorityType = String16(String8(optarg));
+                if (!ConvertPriorityTypeToBitmask(priorityType, dumpPriority)) {
                     fprintf(stderr, "\n");
                     usage();
                     return -1;
@@ -168,7 +181,12 @@
         // gets all services
         services = sm_->listServices(dumpPriority);
         services.sort(sort_func);
-        args.add(String16("-a"));
+        if (dumpPriority != IServiceManager::DUMP_PRIORITY_ALL) {
+            args.insertAt(String16(PRIORITY_ARG), 0);
+            args.insertAt(priorityType, 1);
+        } else {
+            args.add(String16("-a"));
+        }
     }
 
     const size_t N = services.size();
@@ -212,7 +230,11 @@
             if (N > 1) {
                 aout << "------------------------------------------------------------"
                         "-------------------" << endl;
-                aout << "DUMP OF SERVICE " << service_name << ":" << endl;
+                if (dumpPriority == IServiceManager::DUMP_PRIORITY_ALL) {
+                    aout << "DUMP OF SERVICE " << service_name << ":" << endl;
+                } else {
+                    aout << "DUMP OF SERVICE " << priorityType << " " << service_name << ":" << endl;
+                }
             }
 
             // dump blocks until completion, so spawn a thread..