OSDN Git Service

Merge "simpleperf: better support kernel symbols when running as root."
authorYabin Cui <yabinc@google.com>
Mon, 22 May 2017 18:16:21 +0000 (18:16 +0000)
committerGerrit Code Review <noreply-gerritcodereview@google.com>
Mon, 22 May 2017 18:16:23 +0000 (18:16 +0000)
simpleperf/cmd_record.cpp
simpleperf/cmd_record_test.cpp
simpleperf/environment.cpp

index 1eff009..acba6ab 100644 (file)
@@ -934,7 +934,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 0ad2d6b..d0cb835 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.";