aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ftrace_reader/ftrace_procfs.cc2
-rw-r--r--src/ftrace_reader/ftrace_procfs_unittest.cc36
2 files changed, 38 insertions, 0 deletions
diff --git a/src/ftrace_reader/ftrace_procfs.cc b/src/ftrace_reader/ftrace_procfs.cc
index 70aa050e0..bcc255748 100644
--- a/src/ftrace_reader/ftrace_procfs.cc
+++ b/src/ftrace_reader/ftrace_procfs.cc
@@ -170,6 +170,8 @@ std::set<std::string> FtraceProcfs::AvailableClocks() {
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 bccf2e052..6c86339dc 100644
--- a/src/ftrace_reader/ftrace_procfs_unittest.cc
+++ b/src/ftrace_reader/ftrace_procfs_unittest.cc
@@ -56,8 +56,44 @@ TEST(FtraceProcfsTest, ParseAvailableClocks) {
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