Split bugreport identifier into id and pid.

Although 'pid' is more useful when diagnosing problems with the
bugreport workflow, it could be confusing to the end user. Hence, a
sequential id (started at 1 after a reboot) would be more useful.

BUG: 27076108
Change-Id: I6fd7ea8c703e8040034af183ef5cd35adfbcb4ef
diff --git a/cmds/dumpstate/dumpstate.cpp b/cmds/dumpstate/dumpstate.cpp
index 98617f4..6917c21 100644
--- a/cmds/dumpstate/dumpstate.cpp
+++ b/cmds/dumpstate/dumpstate.cpp
@@ -56,6 +56,7 @@
 static const char *dump_traces_path = NULL;
 
 // TODO: should be part of dumpstate object
+static unsigned long id;
 static char build_type[PROPERTY_VALUE_MAX];
 static time_t now;
 static std::unique_ptr<ZipWriter> zip_writer;
@@ -477,6 +478,7 @@
     dump_file(NULL, "/proc/version");
     printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
     printf("Bugreport format version: %s\n", version.c_str());
+    printf("Dumpstate info: id=%lu pid=%d\n", id, getpid());
     printf("\n");
 }
 
@@ -1034,6 +1036,14 @@
 
     MYLOGI("begin\n");
 
+    /* gets the sequential id */
+    char last_id[PROPERTY_VALUE_MAX];
+    property_get("dumpstate.last_id", last_id, "0");
+    id = strtoul(last_id, NULL, 10) + 1;
+    sprintf(last_id, "%lu", id);
+    property_set("dumpstate.last_id", last_id);
+    MYLOGI("dumpstate id: %lu\n", id);
+
     /* clear SIGPIPE handler */
     memset(&sigact, 0, sizeof(sigact));
     sigact.sa_handler = sigpipe_handler;
@@ -1174,6 +1184,7 @@
             std::vector<std::string> am_args = {
                  "--receiver-permission", "android.permission.DUMP",
                  "--es", "android.intent.extra.NAME", suffix,
+                 "--ei", "android.intent.extra.ID", std::to_string(id),
                  "--ei", "android.intent.extra.PID", std::to_string(getpid()),
                  "--ei", "android.intent.extra.MAX", std::to_string(WEIGHT_TOTAL),
             };
@@ -1317,6 +1328,7 @@
             MYLOGI("Final bugreport path: %s\n", path.c_str());
             std::vector<std::string> am_args = {
                  "--receiver-permission", "android.permission.DUMP", "--receiver-foreground",
+                 "--ei", "android.intent.extra.ID", std::to_string(id),
                  "--ei", "android.intent.extra.PID", std::to_string(getpid()),
                  "--es", "android.intent.extra.BUGREPORT", path,
                  "--es", "android.intent.extra.DUMPSTATE_LOG", log_path