OSDN Git Service

simpleperf: better support kernel symbols when running as root.
authorYabin Cui <yabinc@google.com>
Fri, 19 May 2017 19:02:15 +0000 (12:02 -0700)
committerYabin Cui <yabinc@google.com>
Fri, 19 May 2017 20:01:22 +0000 (13:01 -0700)
Instead of asking user to `echo 0 >/proc/sys/kernel/kptr_restrict`,
do it automatically in simpleperf.

Bug: None.
Test: run simpleperf as root.
Test: run simpleperf_unit_test.
Change-Id: If31aa03b6e30afb925cbf58835536ed4c969a635

simpleperf/cmd_record.cpp
simpleperf/cmd_record_test.cpp
simpleperf/environment.cpp

index 27c089c..486f360 100644 (file)
@@ -931,7 +931,9 @@ bool RecordCommand::DumpAdditionalFeatures(
     const std::vector<std::string>& args) {
   // Read data section of perf.data to collect hit file information.
   thread_tree_.ClearThreadAndMap();
-  Dso::ReadKernelSymbolsFromProc();
+  if (CheckKernelSymbolAddresses()) {
+    Dso::ReadKernelSymbolsFromProc();
+  }
   auto callback = [&](const Record* r) {
     thread_tree_.Update(*r);
     if (r->type() == PERF_RECORD_SAMPLE) {
index 0d0c44a..571a3ca 100644 (file)
@@ -331,7 +331,6 @@ TEST(record_cmd, dump_kernel_symbols) {
     GTEST_LOG_(INFO) << "Test requires root privilege";
     return;
   }
-  system("echo 0 >/proc/sys/kernel/kptr_restrict");
   TemporaryFile tmpfile;
   ASSERT_TRUE(RunRecordCmd({"-a", "-o", tmpfile.path, "sleep", "1"}));
   std::unique_ptr<RecordFileReader> reader = RecordFileReader::CreateInstance(tmpfile.path);
index ed161c2..ec926f9 100644 (file)
@@ -451,12 +451,18 @@ bool CheckKernelSymbolAddresses() {
     LOG(ERROR) << "failed to parse " << kptr_restrict_file << ": " << s;
     return false;
   }
+  // Accessible to everyone?
   if (value == 0) {
     return true;
   }
+  // Accessible to root?
   if (value == 1 && IsRoot()) {
     return true;
   }
+  // Can we make it accessible to us?
+  if (IsRoot() && android::base::WriteStringToFile("1", kptr_restrict_file)) {
+    return true;
+  }
   LOG(WARNING) << "Access to kernel symbol addresses is restricted. If "
       << "possible, please do `echo 0 >/proc/sys/kernel/kptr_restrict` "
       << "to fix this.";