traced_probes: Fix clock bug

There was a bug when parsing the list of available clocks from
/d/tracing/trace_clock.

The contents of /d/tracing/trace_clock looks like:
uptime perf mono mono_raw boot\n
We acidentally included the newline in the name of the final clock when
reading. This means (if boot is unfortunate enough to be the last clock
listed) we fail to set the boot clock (since "boot" != "boot\n").

Fix the parsing of trace_clock to discount the newline.

Bug: 110356412
Change-Id: I5160637efec69844fe75f8e705d02a7c486ae990
Merged-In: I5160637efec69844fe75f8e705d02a7c486ae990
(cherry picked from commit 22dac14815a58d00ea87a0013bb638c703843f11)
diff --git a/src/ftrace_reader/ftrace_procfs.cc b/src/ftrace_reader/ftrace_procfs.cc
index 70aa050..bcc2557 100644
--- a/src/ftrace_reader/ftrace_procfs.cc
+++ b/src/ftrace_reader/ftrace_procfs.cc
@@ -170,6 +170,8 @@
     end = s.find(' ', start);
     if (end == std::string::npos)
       end = s.size();
+    while (end > start && s[end - 1] == '\n')
+      end--;
     if (start == end)
       break;
 
diff --git a/src/ftrace_reader/ftrace_procfs_unittest.cc b/src/ftrace_reader/ftrace_procfs_unittest.cc
index bccf2e0..6c86339 100644
--- a/src/ftrace_reader/ftrace_procfs_unittest.cc
+++ b/src/ftrace_reader/ftrace_procfs_unittest.cc
@@ -56,8 +56,44 @@
   EXPECT_THAT(ftrace.GetClock(), "global");
 
   EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
+      .WillOnce(Return("local global [boot]"));
+  EXPECT_THAT(ftrace.GetClock(), "boot");
+
+  EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
       .WillOnce(Return(""));
   EXPECT_THAT(ftrace.AvailableClocks(), IsEmpty());
+
+  // trace_clock text may end in a new line:
+  EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
+      .WillOnce(Return("[local] global boot\n"));
+  EXPECT_THAT(ftrace.AvailableClocks(),
+              UnorderedElementsAre("local", "global", "boot"));
+
+  EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
+      .WillOnce(Return("local global [boot]\n"));
+  EXPECT_THAT(ftrace.AvailableClocks(),
+              UnorderedElementsAre("local", "global", "boot"));
+
+  EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
+      .WillOnce(Return("local global [boot]\n"));
+  EXPECT_THAT(ftrace.GetClock(), "boot");
+
+  EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
+      .WillOnce(Return("\n"));
+  EXPECT_THAT(ftrace.AvailableClocks(), IsEmpty());
+
+  // We should handle many newlines (just in case):
+  EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
+      .WillOnce(Return("local global [boot]\n\n\n"));
+  EXPECT_THAT(ftrace.GetClock(), "boot");
+
+  EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
+      .WillOnce(Return("local global [boot]\n\n"));
+  EXPECT_THAT(ftrace.GetClock(), "boot");
+
+  EXPECT_CALL(ftrace, ReadFileIntoString("/root/trace_clock"))
+      .WillOnce(Return("\n\n\n\n"));
+  EXPECT_THAT(ftrace.AvailableClocks(), IsEmpty());
 }
 
 }  // namespace