The proto field is set based on which header the data belongs to.

Bug: 64371071
Test: tested manually using native test
Change-Id: Ic7671041981d9a722ef00ce6c50cdb12b3e5414d
diff --git a/cmds/incident/main.cpp b/cmds/incident/main.cpp
index 180342c..47f1db8 100644
--- a/cmds/incident/main.cpp
+++ b/cmds/incident/main.cpp
@@ -240,6 +240,7 @@
 
         if (!status.isOk()) {
             fprintf(stderr, "reportIncident returned \"%s\"\n", status.toString8().string());
+            return 1;
         }
 
         // Wait for the result and print out the data they send.
diff --git a/cmds/incident_helper/IncidentHelper.cpp b/cmds/incident_helper/IncidentHelper.cpp
index aa06595..664c48f 100644
--- a/cmds/incident_helper/IncidentHelper.cpp
+++ b/cmds/incident_helper/IncidentHelper.cpp
@@ -49,21 +49,32 @@
 }
 
 // ================================================================================
-// This list must be in order and sync with kernelwake.proto
-const char* kernel_wake_headers[] = {
-    "name",                  // id:  1
-    "active_count",          // id:  2
-    "event_count",           // id:  3
-    "wakeup_count",          // id:  4
-    "expire_count",          // id:  5
-    "active_since",          // id:  6
-    "total_time",            // id:  7
-    "max_time",              // id:  8
-    "last_change",           // id:  9
-    "prevent_suspend_time",  // id: 10
-};
+static const string KERNEL_WAKEUP_LINE_DELIMITER = "\t";
 
-const string KERNEL_WAKEUP_LINE_DELIMITER = "\t";
+static void SetWakeupSourceField(WakeupSourceProto* source, string name, string value) {
+    if (name == "name") {
+        source->set_name(value.c_str());
+    } else if (name == "active_count") {
+        source->set_active_count(atoi(value.c_str()));
+    } else if (name == "event_count") {
+        source->set_event_count(atoi(value.c_str()));
+    } else if (name == "wakeup_count") {
+        source->set_wakeup_count(atoi(value.c_str()));
+    } else if (name == "expire_count") {
+        source->set_expire_count(atoi(value.c_str()));
+    } else if (name == "active_count") {
+        source->set_active_since(atol(value.c_str()));
+    } else if (name == "total_time") {
+        source->set_total_time(atol(value.c_str()));
+    } else if (name == "max_time") {
+        source->set_max_time(atol(value.c_str()));
+    } else if (name == "last_change") {
+        source->set_last_change(atol(value.c_str()));
+    } else if (name == "prevent_suspend_time") {
+        source->set_prevent_suspend_time(atol(value.c_str()));
+    }
+    // add new fields
+}
 
 status_t KernelWakesParser::Parse(const int in, const int out) const {
     Reader reader(in);
@@ -80,10 +91,6 @@
         // parse head line
         if (nline++ == 0) {
             split(line, header, KERNEL_WAKEUP_LINE_DELIMITER);
-            if (!assertHeaders(kernel_wake_headers, header)) {
-                fprintf(stderr, "[%s]Bad header:\n%s\n", this->name.string(), line.c_str());
-                return BAD_VALUE;
-            }
             continue;
         }
 
@@ -97,18 +104,9 @@
         }
 
         WakeupSourceProto* source = proto.add_wakeup_sources();
-        source->set_name(record.at(0).c_str());
-        // below are int32
-        source->set_active_count(atoi(record.at(1).c_str()));
-        source->set_event_count(atoi(record.at(2).c_str()));
-        source->set_wakeup_count(atoi(record.at(3).c_str()));
-        source->set_expire_count(atoi(record.at(4).c_str()));
-        // below are int64
-        source->set_active_since(atol(record.at(5).c_str()));
-        source->set_total_time(atol(record.at(6).c_str()));
-        source->set_max_time(atol(record.at(7).c_str()));
-        source->set_last_change(atol(record.at(8).c_str()));
-        source->set_prevent_suspend_time(atol(record.at(9).c_str()));
+        for (int i=0; i<(int)record.size(); i++) {
+            SetWakeupSourceField(source, header[i], record[i]);
+        }
     }
 
     if (!reader.ok(line)) {
@@ -125,18 +123,31 @@
 }
 
 // ================================================================================
-const char* procrank_headers[] = {
-    "PID",          // id:  1
-    "Vss",          // id:  2
-    "Rss",          // id:  3
-    "Pss",          // id:  4
-    "Uss",          // id:  5
-    "Swap",         // id:  6
-    "PSwap",        // id:  7
-    "USwap",        // id:  8
-    "ZSwap",        // id:  9
-    "cmdline",      // id: 10
-};
+// Remove K for numeric fields
+static void SetProcessField(ProcessProto* process, string name, string value) {
+    ssize_t len = value.size();
+    if (name == "PID") {
+        process->set_pid(atoi(value.c_str()));
+    } else if (name == "Vss") {
+        process->set_vss(atol(value.substr(0, len - 1).c_str()));
+    } else if (name == "Rss") {
+        process->set_rss(atol(value.substr(0, len - 1).c_str()));
+    } else if (name == "Pss") {
+        process->set_pss(atol(value.substr(0, len - 1).c_str()));
+    } else if (name == "Uss") {
+        process->set_uss(atol(value.substr(0, len - 1).c_str()));
+    } else if (name == "Swap") {
+        process->set_swap(atol(value.substr(0, len - 1).c_str()));
+    } else if (name == "PSwap") {
+        process->set_pswap(atol(value.substr(0, len - 1).c_str()));
+    } else if (name == "USwap") {
+        process->set_uswap(atol(value.substr(0, len - 1).c_str()));
+    } else if (name == "ZSwap") {
+        process->set_zswap(atol(value.substr(0, len - 1).c_str()));
+    } else if (name == "cmdline") {
+        process->set_cmdline(value);
+    }
+}
 
 status_t ProcrankParser::Parse(const int in, const int out) const {
     Reader reader(in);
@@ -154,10 +165,6 @@
         // parse head line
         if (nline++ == 0) {
             split(line, header);
-            if (!assertHeaders(procrank_headers, header)) {
-                fprintf(stderr, "[%s]Bad header:\n%s\n", this->name.string(), line.c_str());
-                return BAD_VALUE;
-            }
             continue;
         }
 
@@ -165,12 +172,9 @@
         if (record.size() != header.size()) {
             if (record[record.size() - 1] == "TOTAL") { // TOTAL record
                 ProcessProto* total = proto.mutable_summary()->mutable_total();
-                total->set_pss(atol(record.at(0).substr(0, record.at(0).size() - 1).c_str()));
-                total->set_uss(atol(record.at(1).substr(0, record.at(1).size() - 1).c_str()));
-                total->set_swap(atol(record.at(2).substr(0, record.at(2).size() - 1).c_str()));
-                total->set_pswap(atol(record.at(3).substr(0, record.at(3).size() - 1).c_str()));
-                total->set_uswap(atol(record.at(4).substr(0, record.at(4).size() - 1).c_str()));
-                total->set_zswap(atol(record.at(5).substr(0, record.at(5).size() - 1).c_str()));
+                for (int i=1; i<=(int)record.size(); i++) {
+                    SetProcessField(total, header[header.size() - i], record[record.size() - i]);
+                }
             } else if (record[0] == "ZRAM:") {
                 split(line, record, ":");
                 proto.mutable_summary()->mutable_zram()->set_raw_text(record[1]);
@@ -185,19 +189,9 @@
         }
 
         ProcessProto* process = proto.add_processes();
-        // int32
-        process->set_pid(atoi(record.at(0).c_str()));
-        // int64, remove 'K' at the end
-        process->set_vss(atol(record.at(1).substr(0, record.at(1).size() - 1).c_str()));
-        process->set_rss(atol(record.at(2).substr(0, record.at(2).size() - 1).c_str()));
-        process->set_pss(atol(record.at(3).substr(0, record.at(3).size() - 1).c_str()));
-        process->set_uss(atol(record.at(4).substr(0, record.at(4).size() - 1).c_str()));
-        process->set_swap(atol(record.at(5).substr(0, record.at(5).size() - 1).c_str()));
-        process->set_pswap(atol(record.at(6).substr(0, record.at(6).size() - 1).c_str()));
-        process->set_uswap(atol(record.at(7).substr(0, record.at(7).size() - 1).c_str()));
-        process->set_zswap(atol(record.at(8).substr(0, record.at(8).size() - 1).c_str()));
-        // string
-        process->set_cmdline(record.at(9));
+        for (int i=0; i<(int)record.size(); i++) {
+            SetProcessField(process, header[i], record[i]);
+        }
     }
 
     if (!reader.ok(line)) {
diff --git a/cmds/incident_helper/IncidentHelper.h b/cmds/incident_helper/IncidentHelper.h
index 736f848..f319c41 100644
--- a/cmds/incident_helper/IncidentHelper.h
+++ b/cmds/incident_helper/IncidentHelper.h
@@ -60,8 +60,6 @@
 /**
  * Kernel wakeup sources parser, parses text to protobuf in /d/wakeup_sources
  */
-extern const char* kernel_wake_headers[];
-
 class KernelWakesParser : public TextParserBase {
 public:
     KernelWakesParser() : TextParserBase(String8("KernelWakeSources")) {};
@@ -73,8 +71,6 @@
 /**
  * Procrank parser, parses text produced by command procrank
  */
-extern const char* procrank_headers[];
-
 class ProcrankParser : public TextParserBase {
 public:
     ProcrankParser() : TextParserBase(String8("ProcrankParser")) {};
diff --git a/cmds/incident_helper/testdata/kernel_wakeups_bad_headers.txt b/cmds/incident_helper/testdata/kernel_wakeups_bad_headers.txt
deleted file mode 100644
index 4914d2e..0000000
--- a/cmds/incident_helper/testdata/kernel_wakeups_bad_headers.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-THIS IS BAD HEADER
-ipc000000ab_ATFWD-daemon	8		8		0		0		0		0		0		131348		0
-ipc000000aa_ATFWD-daemon	143		143		0		0		0		123		3		2067286206		0
\ No newline at end of file
diff --git a/cmds/incident_helper/testdata/procrank_short.txt b/cmds/incident_helper/testdata/procrank_short.txt
new file mode 100644
index 0000000..44f1f18
--- /dev/null
+++ b/cmds/incident_helper/testdata/procrank_short.txt
@@ -0,0 +1,7 @@
+  PID       Vss      Rss      Pss      Uss    cmdline
+ 1119  2607640K  339564K  180278K  114216K    system_server
+  649    11016K    1448K      98K      48K    /vendor/bin/qseecomd
+                           ------   ------    ------
+                          1201993K  935300K   TOTAL
+
+RAM: 3843972K total, 281424K free, 116764K buffers, 1777452K cached, 1136K shmem, 217916K slab
\ No newline at end of file
diff --git a/cmds/incident_helper/tests/IncidentHelper_test.cpp b/cmds/incident_helper/tests/IncidentHelper_test.cpp
index ac3e84d..04109c9 100644
--- a/cmds/incident_helper/tests/IncidentHelper_test.cpp
+++ b/cmds/incident_helper/tests/IncidentHelper_test.cpp
@@ -106,21 +106,6 @@
     close(fd);
 }
 
-TEST_F(IncidentHelperTest, KernelWakesParserBadHeaders) {
-    const std::string testFile = kTestDataPath + "kernel_wakeups_bad_headers.txt";
-    KernelWakesParser parser;
-
-    int fd = open(testFile.c_str(), O_RDONLY, 0444);
-    ASSERT_TRUE(fd != -1);
-
-    CaptureStdout();
-    CaptureStderr();
-    ASSERT_EQ(BAD_VALUE, parser.Parse(fd, STDOUT_FILENO));
-    EXPECT_THAT(GetCapturedStdout(), StrEq(""));
-    EXPECT_THAT(GetCapturedStderr(), StrEq("[KernelWakeSources]Bad header:\nTHIS IS BAD HEADER\n"));
-    close(fd);
-}
-
 TEST_F(IncidentHelperTest, ProcrankParser) {
     const std::string testFile = kTestDataPath + "procrank.txt";
     ProcrankParser parser;
@@ -157,6 +142,7 @@
     total->set_pswap(31069);
     total->set_uswap(27612);
     total->set_zswap(6826);
+    total->set_cmdline("TOTAL");
 
     expected.mutable_summary()->mutable_zram()
         ->set_raw_text("6828K physical used for 31076K in swap (524284K total swap)");
@@ -171,3 +157,41 @@
     EXPECT_EQ(GetCapturedStdout(), getSerializedString(expected));
     close(fd);
 }
+
+TEST_F(IncidentHelperTest, ProcrankParserShortHeader) {
+    const std::string testFile = kTestDataPath + "procrank_short.txt";
+    ProcrankParser parser;
+    Procrank expected;
+
+    ProcessProto* process1 = expected.add_processes();
+    process1->set_pid(1119);
+    process1->set_vss(2607640);
+    process1->set_rss(339564);
+    process1->set_pss(180278);
+    process1->set_uss(114216);
+    process1->set_cmdline("system_server");
+
+    ProcessProto* process2 = expected.add_processes();
+    process2->set_pid(649);
+    process2->set_vss(11016);
+    process2->set_rss(1448);
+    process2->set_pss(98);
+    process2->set_uss(48);
+    process2->set_cmdline("/vendor/bin/qseecomd");
+
+    ProcessProto* total = expected.mutable_summary()->mutable_total();
+    total->set_pss(1201993);
+    total->set_uss(935300);
+    total->set_cmdline("TOTAL");
+
+    expected.mutable_summary()->mutable_ram()
+        ->set_raw_text("3843972K total, 281424K free, 116764K buffers, 1777452K cached, 1136K shmem, 217916K slab");
+
+    int fd = open(testFile.c_str(), O_RDONLY, 0444);
+    ASSERT_TRUE(fd != -1);
+
+    CaptureStdout();
+    ASSERT_EQ(NO_ERROR, parser.Parse(fd, STDOUT_FILENO));
+    EXPECT_EQ(GetCapturedStdout(), getSerializedString(expected));
+    close(fd);
+}
\ No newline at end of file
diff --git a/cmds/incidentd/incidentd.rc b/cmds/incidentd/incidentd.rc
index 250a7f3..c1eed7f 100644
--- a/cmds/incidentd/incidentd.rc
+++ b/cmds/incidentd/incidentd.rc
@@ -12,5 +12,5 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# service incidentd /system/bin/incidentd
-#    class main
+service incidentd /system/bin/incidentd
+    class main