OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "crash-reporter/kernel_collector.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" |
| 10 #include "crash-reporter/system_logging.h" |
| 11 |
| 12 static const char kKernelExecName[] = "kernel"; |
| 13 static const char kPreservedDumpPath[] = "/sys/kernel/debug/preserved/kcrash"; |
| 14 const pid_t kKernelPid = 0; |
| 15 const uid_t kRootUid = 0; |
| 16 |
| 17 const char KernelCollector::kClearingSequence[] = " "; |
| 18 |
| 19 KernelCollector::KernelCollector() |
| 20 : is_enabled_(false), |
| 21 preserved_dump_path_(kPreservedDumpPath) { |
| 22 } |
| 23 |
| 24 KernelCollector::~KernelCollector() { |
| 25 } |
| 26 |
| 27 void KernelCollector::OverridePreservedDumpPath(const FilePath &file_path) { |
| 28 preserved_dump_path_ = file_path; |
| 29 } |
| 30 |
| 31 bool KernelCollector::LoadPreservedDump(std::string *contents) { |
| 32 // clear contents since ReadFileToString actually appends to the string. |
| 33 contents->clear(); |
| 34 if (!file_util::ReadFileToString(preserved_dump_path_, contents)) { |
| 35 logger_->LogError("Unable to read %s", |
| 36 preserved_dump_path_.value().c_str()); |
| 37 return false; |
| 38 } |
| 39 return true; |
| 40 } |
| 41 |
| 42 bool KernelCollector::Enable() { |
| 43 if (!file_util::PathExists(preserved_dump_path_)) { |
| 44 logger_->LogWarning("Kernel does not support crash dumping"); |
| 45 return false; |
| 46 } |
| 47 |
| 48 // To enable crashes, we will eventually need to set |
| 49 // the chnv bit in BIOS, but it does not yet work. |
| 50 logger_->LogInfo("Enabling kernel crash handling"); |
| 51 is_enabled_ = true; |
| 52 return true; |
| 53 } |
| 54 |
| 55 bool KernelCollector::ClearPreservedDump() { |
| 56 // It is necessary to write at least one byte to the kcrash file for |
| 57 // the log to actually be cleared. |
| 58 if (file_util::WriteFile( |
| 59 preserved_dump_path_, |
| 60 kClearingSequence, |
| 61 strlen(kClearingSequence)) != strlen(kClearingSequence)) { |
| 62 logger_->LogError("Failed to clear kernel crash dump"); |
| 63 return false; |
| 64 } |
| 65 logger_->LogInfo("Cleared kernel crash diagnostics"); |
| 66 return true; |
| 67 } |
| 68 |
| 69 FilePath KernelCollector::GetKernelCrashPath( |
| 70 const FilePath &root_crash_path, |
| 71 time_t timestamp) { |
| 72 std::string dump_basename = |
| 73 FormatDumpBasename(kKernelExecName, |
| 74 timestamp, |
| 75 kKernelPid); |
| 76 return root_crash_path.Append( |
| 77 StringPrintf("%s.kcrash", dump_basename.c_str())); |
| 78 } |
| 79 |
| 80 bool KernelCollector::Collect() { |
| 81 std::string kernel_dump; |
| 82 FilePath root_crash_directory; |
| 83 if (!LoadPreservedDump(&kernel_dump)) { |
| 84 return false; |
| 85 } |
| 86 if (kernel_dump.empty()) { |
| 87 return false; |
| 88 } |
| 89 if (is_feedback_allowed_function_()) { |
| 90 count_crash_function_(); |
| 91 |
| 92 if (!GetCreatedCrashDirectoryByEuid(kRootUid, |
| 93 &root_crash_directory)) { |
| 94 return true; |
| 95 } |
| 96 |
| 97 FilePath kernel_crash_path = GetKernelCrashPath(root_crash_directory, |
| 98 time(NULL)); |
| 99 if (file_util::WriteFile(kernel_crash_path, |
| 100 kernel_dump.data(), |
| 101 kernel_dump.length()) != |
| 102 static_cast<int>(kernel_dump.length())) { |
| 103 logger_->LogInfo("Failed to write kernel dump to %s", |
| 104 kernel_crash_path.value().c_str()); |
| 105 return true; |
| 106 } |
| 107 |
| 108 logger_->LogInfo("Collected kernel crash diagnostics into %s", |
| 109 kernel_crash_path.value().c_str()); |
| 110 } else { |
| 111 logger_->LogInfo("Crash not saved since metrics disabled"); |
| 112 } |
| 113 if (!ClearPreservedDump()) { |
| 114 return false; |
| 115 } |
| 116 |
| 117 return true; |
| 118 } |
OLD | NEW |