Mike Frysinger | 57b261c | 2012-04-11 14:47:09 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 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 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 7 | #include <map> |
| 8 | |
Ben Chan | ab6cc90 | 2014-09-05 08:21:06 -0700 | [diff] [blame^] | 9 | #include <base/files/file_util.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 10 | #include <base/logging.h> |
| 11 | #include <base/strings/string_util.h> |
| 12 | #include <base/strings/stringprintf.h> |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 13 | |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 14 | using base::FilePath; |
| 15 | using base::StringPrintf; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | const char kDefaultKernelStackSignature[] = "kernel-UnspecifiedStackSignature"; |
| 20 | const char kDumpPath[] = "/dev/pstore"; |
| 21 | const char kDumpFormat[] = "dmesg-ramoops-%zu"; |
| 22 | const char kKernelExecName[] = "kernel"; |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 23 | // Maximum number of records to examine in the kDumpPath. |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 24 | const size_t kMaxDumpRecords = 100; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 25 | const pid_t kKernelPid = 0; |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 26 | const char kKernelSignatureKey[] = "sig"; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 27 | // Byte length of maximum human readable portion of a kernel crash signature. |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 28 | const int kMaxHumanStringLength = 40; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 29 | const uid_t kRootUid = 0; |
| 30 | // Time in seconds from the final kernel log message for a call stack |
| 31 | // to count towards the signature of the kcrash. |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 32 | const int kSignatureTimestampWindow = 2; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 33 | // Kernel log timestamp regular expression. |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 34 | const char kTimestampRegex[] = "^<.*>\\[\\s*(\\d+\\.\\d+)\\]"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 35 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 36 | // |
| 37 | // These regular expressions enable to us capture the PC in a backtrace. |
| 38 | // The backtrace is obtained through dmesg or the kernel's preserved/kcrashmem |
| 39 | // feature. |
| 40 | // |
| 41 | // For ARM we see: |
| 42 | // "<5>[ 39.458982] PC is at write_breakme+0xd0/0x1b4" |
Ben Chan | 120c675 | 2014-07-22 21:06:09 -0700 | [diff] [blame] | 43 | // For MIPS we see: |
| 44 | // "<5>[ 3378.552000] epc : 804010f0 lkdtm_do_action+0x68/0x3f8" |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 45 | // For x86: |
| 46 | // "<0>[ 37.474699] EIP: [<790ed488>] write_breakme+0x80/0x108 |
| 47 | // SS:ESP 0068:e9dd3efc" |
| 48 | // |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 49 | const char* const kPCRegex[] = { |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 50 | 0, |
| 51 | " PC is at ([^\\+ ]+).*", |
Ben Chan | 120c675 | 2014-07-22 21:06:09 -0700 | [diff] [blame] | 52 | " epc\\s+:\\s+\\S+\\s+([^\\+ ]+).*", // MIPS has an exception program counter |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 53 | " EIP: \\[<.*>\\] ([^\\+ ]+).*", // X86 uses EIP for the program counter |
Bryan Freed | b873759 | 2012-04-02 17:05:48 -0700 | [diff] [blame] | 54 | " RIP \\[<.*>\\] ([^\\+ ]+).*", // X86_64 uses RIP for the program counter |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 57 | COMPILE_ASSERT(arraysize(kPCRegex) == KernelCollector::kArchCount, |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 58 | missing_arch_pc_regexp); |
| 59 | |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 60 | } // namespace |
| 61 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 62 | KernelCollector::KernelCollector() |
| 63 | : is_enabled_(false), |
Ben Chan | 2076b90 | 2012-02-29 22:26:54 -0800 | [diff] [blame] | 64 | ramoops_dump_path_(kDumpPath), |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 65 | records_(0), |
| 66 | // We expect crash dumps in the format of architecture we are built for. |
| 67 | arch_(GetCompilerArch()) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | KernelCollector::~KernelCollector() { |
| 71 | } |
| 72 | |
| 73 | void KernelCollector::OverridePreservedDumpPath(const FilePath &file_path) { |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 74 | ramoops_dump_path_ = file_path; |
| 75 | } |
| 76 | |
| 77 | bool KernelCollector::ReadRecordToString(std::string *contents, |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 78 | size_t current_record, |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 79 | bool *record_found) { |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 80 | // A record is a ramoops dump. It has an associated size of "record_size". |
| 81 | std::string record; |
| 82 | std::string captured; |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 83 | |
| 84 | // Ramoops appends a header to a crash which contains ==== followed by a |
| 85 | // timestamp. Ignore the header. |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 86 | pcrecpp::RE record_re( |
| 87 | "====\\d+\\.\\d+\n(.*)", |
| 88 | pcrecpp::RE_Options().set_multiline(true).set_dotall(true)); |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 89 | |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 90 | FilePath ramoops_record; |
| 91 | GetRamoopsRecordPath(&ramoops_record, current_record); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 92 | if (!base::ReadFileToString(ramoops_record, &record)) { |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 93 | LOG(ERROR) << "Unable to open " << ramoops_record.value(); |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 94 | return false; |
| 95 | } |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 96 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 97 | if (record_re.FullMatch(record, &captured)) { |
Kees Cook | 5825d5a | 2012-02-22 17:15:15 -0800 | [diff] [blame] | 98 | // Found a match, append it to the content, and remove from pstore. |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 99 | contents->append(captured); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 100 | base::DeleteFile(ramoops_record, false); |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 101 | *record_found = true; |
| 102 | } else { |
| 103 | *record_found = false; |
| 104 | } |
| 105 | |
| 106 | return true; |
| 107 | } |
| 108 | |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 109 | void KernelCollector::GetRamoopsRecordPath(FilePath *path, |
| 110 | size_t record) { |
Michael Krebs | 1e09a84 | 2012-04-18 12:34:13 -0700 | [diff] [blame] | 111 | // Disable error "format not a string literal, argument types not checked" |
| 112 | // because this is valid, but GNU apparently doesn't bother checking a const |
| 113 | // format string. |
| 114 | #pragma GCC diagnostic push |
| 115 | #pragma GCC diagnostic ignored "-Wformat-nonliteral" |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 116 | *path = ramoops_dump_path_.Append(StringPrintf(kDumpFormat, record)); |
Michael Krebs | 1e09a84 | 2012-04-18 12:34:13 -0700 | [diff] [blame] | 117 | #pragma GCC diagnostic pop |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | bool KernelCollector::LoadParameters() { |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 121 | // Discover how many ramoops records are being exported by the driver. |
| 122 | size_t count; |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 123 | |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 124 | for (count = 0; count < kMaxDumpRecords; ++count) { |
| 125 | FilePath ramoops_record; |
| 126 | GetRamoopsRecordPath(&ramoops_record, count); |
| 127 | |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 128 | if (!base::PathExists(ramoops_record)) |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 129 | break; |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 132 | records_ = count; |
| 133 | return (records_ > 0); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | bool KernelCollector::LoadPreservedDump(std::string *contents) { |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 137 | // Load dumps from the preserved memory and save them in contents. |
| 138 | // Since the system is set to restart on oops we won't actually ever have |
| 139 | // multiple records (only 0 or 1), but check in case we don't restart on |
| 140 | // oops in the future. |
| 141 | bool any_records_found = false; |
| 142 | bool record_found = false; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 143 | // clear contents since ReadFileToString actually appends to the string. |
| 144 | contents->clear(); |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 145 | |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 146 | for (size_t i = 0; i < records_; ++i) { |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 147 | if (!ReadRecordToString(contents, i, &record_found)) { |
| 148 | break; |
| 149 | } |
| 150 | if (record_found) { |
| 151 | any_records_found = true; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (!any_records_found) { |
| 156 | LOG(ERROR) << "No valid records found in " << ramoops_dump_path_.value(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 159 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 160 | return true; |
| 161 | } |
| 162 | |
Doug Anderson | 1e6b8bd | 2011-04-07 09:40:05 -0700 | [diff] [blame] | 163 | void KernelCollector::StripSensitiveData(std::string *kernel_dump) { |
| 164 | // Strip any data that the user might not want sent up to the crash servers. |
| 165 | // We'll read in from kernel_dump and also place our output there. |
| 166 | // |
| 167 | // At the moment, the only sensitive data we strip is MAC addresses. |
| 168 | |
| 169 | // Get rid of things that look like MAC addresses, since they could possibly |
| 170 | // give information about where someone has been. This is strings that look |
| 171 | // like this: 11:22:33:44:55:66 |
| 172 | // Complications: |
| 173 | // - Within a given kernel_dump, want to be able to tell when the same MAC |
| 174 | // was used more than once. Thus, we'll consistently replace the first |
| 175 | // MAC found with 00:00:00:00:00:01, the second with ...:02, etc. |
| 176 | // - ACPI commands look like MAC addresses. We'll specifically avoid getting |
| 177 | // rid of those. |
| 178 | std::ostringstream result; |
| 179 | std::string pre_mac_str; |
| 180 | std::string mac_str; |
| 181 | std::map<std::string, std::string> mac_map; |
| 182 | pcrecpp::StringPiece input(*kernel_dump); |
| 183 | |
| 184 | // This RE will find the next MAC address and can return us the data preceding |
| 185 | // the MAC and the MAC itself. |
| 186 | pcrecpp::RE mac_re("(.*?)(" |
| 187 | "[0-9a-fA-F][0-9a-fA-F]:" |
| 188 | "[0-9a-fA-F][0-9a-fA-F]:" |
| 189 | "[0-9a-fA-F][0-9a-fA-F]:" |
| 190 | "[0-9a-fA-F][0-9a-fA-F]:" |
| 191 | "[0-9a-fA-F][0-9a-fA-F]:" |
| 192 | "[0-9a-fA-F][0-9a-fA-F])", |
| 193 | pcrecpp::RE_Options() |
| 194 | .set_multiline(true) |
| 195 | .set_dotall(true)); |
| 196 | |
| 197 | // This RE will identify when the 'pre_mac_str' shows that the MAC address |
| 198 | // was really an ACPI cmd. The full string looks like this: |
| 199 | // ata1.00: ACPI cmd ef/10:03:00:00:00:a0 (SET FEATURES) filtered out |
| 200 | pcrecpp::RE acpi_re("ACPI cmd ef/$", |
| 201 | pcrecpp::RE_Options() |
| 202 | .set_multiline(true) |
| 203 | .set_dotall(true)); |
| 204 | |
| 205 | // Keep consuming, building up a result string as we go. |
| 206 | while (mac_re.Consume(&input, &pre_mac_str, &mac_str)) { |
| 207 | if (acpi_re.PartialMatch(pre_mac_str)) { |
| 208 | // We really saw an ACPI command; add to result w/ no stripping. |
| 209 | result << pre_mac_str << mac_str; |
| 210 | } else { |
| 211 | // Found a MAC address; look up in our hash for the mapping. |
| 212 | std::string replacement_mac = mac_map[mac_str]; |
| 213 | if (replacement_mac == "") { |
| 214 | // It wasn't present, so build up a replacement string. |
| 215 | int mac_id = mac_map.size(); |
| 216 | |
| 217 | // Handle up to 2^32 unique MAC address; overkill, but doesn't hurt. |
| 218 | replacement_mac = StringPrintf("00:00:%02x:%02x:%02x:%02x", |
| 219 | (mac_id & 0xff000000) >> 24, |
| 220 | (mac_id & 0x00ff0000) >> 16, |
| 221 | (mac_id & 0x0000ff00) >> 8, |
| 222 | (mac_id & 0x000000ff)); |
| 223 | mac_map[mac_str] = replacement_mac; |
| 224 | } |
| 225 | |
| 226 | // Dump the string before the MAC and the fake MAC address into result. |
| 227 | result << pre_mac_str << replacement_mac; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // One last bit of data might still be in the input. |
| 232 | result << input; |
| 233 | |
| 234 | // We'll just assign right back to kernel_dump. |
| 235 | *kernel_dump = result.str(); |
| 236 | } |
| 237 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 238 | bool KernelCollector::Enable() { |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 239 | if (arch_ == kArchUnknown || arch_ >= kArchCount || kPCRegex[arch_] == NULL) { |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 240 | LOG(WARNING) << "KernelCollector does not understand this architecture"; |
| 241 | return false; |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | FilePath ramoops_record; |
| 245 | GetRamoopsRecordPath(&ramoops_record, 0); |
| 246 | if (!base::PathExists(ramoops_record)) { |
| 247 | LOG(WARNING) << "Kernel does not support crash dumping"; |
| 248 | return false; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // To enable crashes, we will eventually need to set |
| 252 | // the chnv bit in BIOS, but it does not yet work. |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 253 | LOG(INFO) << "Enabling kernel crash handling"; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 254 | is_enabled_ = true; |
| 255 | return true; |
| 256 | } |
| 257 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 258 | // Hash a string to a number. We define our own hash function to not |
| 259 | // be dependent on a C++ library that might change. This function |
| 260 | // uses basically the same approach as tr1/functional_hash.h but with |
| 261 | // a larger prime number (16127 vs 131). |
| 262 | static unsigned HashString(const std::string &input) { |
| 263 | unsigned hash = 0; |
| 264 | for (size_t i = 0; i < input.length(); ++i) |
| 265 | hash = hash * 16127 + input[i]; |
| 266 | return hash; |
| 267 | } |
| 268 | |
| 269 | void KernelCollector::ProcessStackTrace( |
| 270 | pcrecpp::StringPiece kernel_dump, |
| 271 | bool print_diagnostics, |
| 272 | unsigned *hash, |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 273 | float *last_stack_timestamp, |
| 274 | bool *is_watchdog_crash) { |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 275 | pcrecpp::RE line_re("(.+)", pcrecpp::MULTILINE()); |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 276 | pcrecpp::RE stack_trace_start_re(std::string(kTimestampRegex) + |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 277 | " (Call Trace|Backtrace):$"); |
| 278 | |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 279 | // Match lines such as the following and grab out "function_name". |
| 280 | // The ? may or may not be present. |
| 281 | // |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 282 | // For ARM: |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 283 | // <4>[ 3498.731164] [<c0057220>] ? (function_name+0x20/0x2c) from |
| 284 | // [<c018062c>] (foo_bar+0xdc/0x1bc) |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 285 | // |
Ben Chan | 120c675 | 2014-07-22 21:06:09 -0700 | [diff] [blame] | 286 | // For MIPS: |
| 287 | // <5>[ 3378.656000] [<804010f0>] lkdtm_do_action+0x68/0x3f8 |
| 288 | // |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 289 | // For X86: |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 290 | // <4>[ 6066.849504] [<7937bcee>] ? function_name+0x66/0x6c |
| 291 | // |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 292 | pcrecpp::RE stack_entry_re(std::string(kTimestampRegex) + |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 293 | "\\s+\\[<[[:xdigit:]]+>\\]" // Matches " [<7937bcee>]" |
| 294 | "([\\s\\?(]+)" // Matches " ? (" (ARM) or " ? " (X86) |
| 295 | "([^\\+ )]+)"); // Matches until delimiter reached |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 296 | std::string line; |
| 297 | std::string hashable; |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 298 | std::string previous_hashable; |
| 299 | bool is_watchdog = false; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 300 | |
| 301 | *hash = 0; |
| 302 | *last_stack_timestamp = 0; |
| 303 | |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 304 | // Find the last and second-to-last stack traces. The latter is used when |
| 305 | // the panic is from a watchdog timeout. |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 306 | while (line_re.FindAndConsume(&kernel_dump, &line)) { |
| 307 | std::string certainty; |
| 308 | std::string function_name; |
| 309 | if (stack_trace_start_re.PartialMatch(line, last_stack_timestamp)) { |
| 310 | if (print_diagnostics) { |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 311 | printf("Stack trace starting.%s\n", |
| 312 | hashable.empty() ? "" : " Saving prior trace."); |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 313 | } |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 314 | previous_hashable = hashable; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 315 | hashable.clear(); |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 316 | is_watchdog = false; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 317 | } else if (stack_entry_re.PartialMatch(line, |
| 318 | last_stack_timestamp, |
| 319 | &certainty, |
| 320 | &function_name)) { |
| 321 | bool is_certain = certainty.find('?') == std::string::npos; |
| 322 | if (print_diagnostics) { |
| 323 | printf("@%f: stack entry for %s (%s)\n", |
| 324 | *last_stack_timestamp, |
| 325 | function_name.c_str(), |
| 326 | is_certain ? "certain" : "uncertain"); |
| 327 | } |
| 328 | // Do not include any uncertain (prefixed by '?') frames in our hash. |
| 329 | if (!is_certain) |
| 330 | continue; |
| 331 | if (!hashable.empty()) |
| 332 | hashable.append("|"); |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 333 | if (function_name == "watchdog_timer_fn" || |
| 334 | function_name == "watchdog") { |
| 335 | is_watchdog = true; |
| 336 | } |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 337 | hashable.append(function_name); |
| 338 | } |
| 339 | } |
| 340 | |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 341 | // If the last stack trace contains a watchdog function we assume the panic |
| 342 | // is from the watchdog timer, and we hash the previous stack trace rather |
| 343 | // than the last one, assuming that the previous stack is that of the hung |
| 344 | // thread. |
| 345 | // |
| 346 | // In addition, if the hashable is empty (meaning all frames are uncertain, |
| 347 | // for whatever reason) also use the previous frame, as it cannot be any |
| 348 | // worse. |
| 349 | if (is_watchdog || hashable.empty()) { |
| 350 | hashable = previous_hashable; |
| 351 | } |
| 352 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 353 | *hash = HashString(hashable); |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 354 | *is_watchdog_crash = is_watchdog; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 355 | |
| 356 | if (print_diagnostics) { |
| 357 | printf("Hash based on stack trace: \"%s\" at %f.\n", |
| 358 | hashable.c_str(), *last_stack_timestamp); |
| 359 | } |
| 360 | } |
| 361 | |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 362 | // static |
| 363 | KernelCollector::ArchKind KernelCollector::GetCompilerArch() { |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 364 | #if defined(COMPILER_GCC) && defined(ARCH_CPU_ARM_FAMILY) |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 365 | return kArchArm; |
Ben Chan | 120c675 | 2014-07-22 21:06:09 -0700 | [diff] [blame] | 366 | #elif defined(COMPILER_GCC) && defined(ARCH_CPU_MIPS_FAMILY) |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 367 | return kArchMips; |
Bryan Freed | b873759 | 2012-04-02 17:05:48 -0700 | [diff] [blame] | 368 | #elif defined(COMPILER_GCC) && defined(ARCH_CPU_X86_64) |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 369 | return kArchX86_64; |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 370 | #elif defined(COMPILER_GCC) && defined(ARCH_CPU_X86_FAMILY) |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 371 | return kArchX86; |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 372 | #else |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 373 | return kArchUnknown; |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 374 | #endif |
| 375 | } |
| 376 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 377 | bool KernelCollector::FindCrashingFunction( |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 378 | pcrecpp::StringPiece kernel_dump, |
| 379 | bool print_diagnostics, |
| 380 | float stack_trace_timestamp, |
| 381 | std::string *crashing_function) { |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 382 | float timestamp = 0; |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 383 | |
| 384 | // Use the correct regex for this architecture. |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 385 | pcrecpp::RE eip_re(std::string(kTimestampRegex) + kPCRegex[arch_], |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 386 | pcrecpp::MULTILINE()); |
| 387 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 388 | while (eip_re.FindAndConsume(&kernel_dump, ×tamp, crashing_function)) { |
| 389 | if (print_diagnostics) { |
| 390 | printf("@%f: found crashing function %s\n", |
| 391 | timestamp, |
| 392 | crashing_function->c_str()); |
| 393 | } |
| 394 | } |
| 395 | if (timestamp == 0) { |
| 396 | if (print_diagnostics) { |
| 397 | printf("Found no crashing function.\n"); |
| 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | if (stack_trace_timestamp != 0 && |
Yunlian Jiang | 9f52040 | 2014-02-27 14:26:15 -0800 | [diff] [blame] | 402 | abs(static_cast<int>(stack_trace_timestamp - timestamp)) |
| 403 | > kSignatureTimestampWindow) { |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 404 | if (print_diagnostics) { |
| 405 | printf("Found crashing function but not within window.\n"); |
| 406 | } |
| 407 | return false; |
| 408 | } |
| 409 | if (print_diagnostics) { |
| 410 | printf("Found crashing function %s\n", crashing_function->c_str()); |
| 411 | } |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | bool KernelCollector::FindPanicMessage(pcrecpp::StringPiece kernel_dump, |
| 416 | bool print_diagnostics, |
| 417 | std::string *panic_message) { |
| 418 | // Match lines such as the following and grab out "Fatal exception" |
| 419 | // <0>[ 342.841135] Kernel panic - not syncing: Fatal exception |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 420 | pcrecpp::RE kernel_panic_re(std::string(kTimestampRegex) + |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 421 | " Kernel panic[^\\:]*\\:\\s*(.*)", |
| 422 | pcrecpp::MULTILINE()); |
| 423 | float timestamp = 0; |
| 424 | while (kernel_panic_re.FindAndConsume(&kernel_dump, |
| 425 | ×tamp, |
| 426 | panic_message)) { |
| 427 | if (print_diagnostics) { |
| 428 | printf("@%f: panic message %s\n", |
| 429 | timestamp, |
| 430 | panic_message->c_str()); |
| 431 | } |
| 432 | } |
| 433 | if (timestamp == 0) { |
| 434 | if (print_diagnostics) { |
| 435 | printf("Found no panic message.\n"); |
| 436 | } |
| 437 | return false; |
| 438 | } |
| 439 | return true; |
| 440 | } |
| 441 | |
| 442 | bool KernelCollector::ComputeKernelStackSignature( |
| 443 | const std::string &kernel_dump, |
| 444 | std::string *kernel_signature, |
| 445 | bool print_diagnostics) { |
| 446 | unsigned stack_hash = 0; |
| 447 | float last_stack_timestamp = 0; |
| 448 | std::string human_string; |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 449 | bool is_watchdog_crash; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 450 | |
| 451 | ProcessStackTrace(kernel_dump, |
| 452 | print_diagnostics, |
| 453 | &stack_hash, |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 454 | &last_stack_timestamp, |
| 455 | &is_watchdog_crash); |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 456 | |
| 457 | if (!FindCrashingFunction(kernel_dump, |
| 458 | print_diagnostics, |
| 459 | last_stack_timestamp, |
| 460 | &human_string)) { |
| 461 | if (!FindPanicMessage(kernel_dump, print_diagnostics, &human_string)) { |
| 462 | if (print_diagnostics) { |
| 463 | printf("Found no human readable string, using empty string.\n"); |
| 464 | } |
| 465 | human_string.clear(); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | if (human_string.empty() && stack_hash == 0) { |
| 470 | if (print_diagnostics) { |
| 471 | printf("Found neither a stack nor a human readable string, failing.\n"); |
| 472 | } |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | human_string = human_string.substr(0, kMaxHumanStringLength); |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 477 | *kernel_signature = StringPrintf("%s-%s%s-%08X", |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 478 | kKernelExecName, |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 479 | (is_watchdog_crash ? "(HANG)-" : ""), |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 480 | human_string.c_str(), |
| 481 | stack_hash); |
| 482 | return true; |
| 483 | } |
| 484 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 485 | bool KernelCollector::Collect() { |
| 486 | std::string kernel_dump; |
| 487 | FilePath root_crash_directory; |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 488 | |
| 489 | if (!LoadParameters()) { |
| 490 | return false; |
| 491 | } |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 492 | if (!LoadPreservedDump(&kernel_dump)) { |
| 493 | return false; |
| 494 | } |
Doug Anderson | 1e6b8bd | 2011-04-07 09:40:05 -0700 | [diff] [blame] | 495 | StripSensitiveData(&kernel_dump); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 496 | if (kernel_dump.empty()) { |
| 497 | return false; |
| 498 | } |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 499 | std::string signature; |
| 500 | if (!ComputeKernelStackSignature(kernel_dump, &signature, false)) { |
| 501 | signature = kDefaultKernelStackSignature; |
| 502 | } |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 503 | |
Ken Mixter | 9ee1f5f | 2011-10-25 02:15:05 +0000 | [diff] [blame] | 504 | std::string reason = "handling"; |
| 505 | bool feedback = true; |
| 506 | if (IsDeveloperImage()) { |
| 507 | reason = "developer build - always dumping"; |
| 508 | feedback = true; |
| 509 | } else if (!is_feedback_allowed_function_()) { |
| 510 | reason = "ignoring - no consent"; |
| 511 | feedback = false; |
| 512 | } |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 513 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 514 | LOG(INFO) << "Received prior crash notification from " |
Ken Mixter | 9ee1f5f | 2011-10-25 02:15:05 +0000 | [diff] [blame] | 515 | << "kernel (signature " << signature << ") (" << reason << ")"; |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 516 | |
| 517 | if (feedback) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 518 | count_crash_function_(); |
| 519 | |
| 520 | if (!GetCreatedCrashDirectoryByEuid(kRootUid, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 521 | &root_crash_directory, |
| 522 | NULL)) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 523 | return true; |
| 524 | } |
| 525 | |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 526 | std::string dump_basename = |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 527 | FormatDumpBasename(kKernelExecName, time(NULL), kKernelPid); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 528 | FilePath kernel_crash_path = root_crash_directory.Append( |
| 529 | StringPrintf("%s.kcrash", dump_basename.c_str())); |
| 530 | |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 531 | // We must use WriteNewFile instead of base::WriteFile as we |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 532 | // do not want to write with root access to a symlink that an attacker |
| 533 | // might have created. |
| 534 | if (WriteNewFile(kernel_crash_path, |
| 535 | kernel_dump.data(), |
| 536 | kernel_dump.length()) != |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 537 | static_cast<int>(kernel_dump.length())) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 538 | LOG(INFO) << "Failed to write kernel dump to " |
| 539 | << kernel_crash_path.value().c_str(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 540 | return true; |
| 541 | } |
| 542 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 543 | AddCrashMetaData(kKernelSignatureKey, signature); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 544 | WriteCrashMetaData( |
| 545 | root_crash_directory.Append( |
| 546 | StringPrintf("%s.meta", dump_basename.c_str())), |
Ken Mixter | c909b69 | 2010-10-18 12:26:05 -0700 | [diff] [blame] | 547 | kKernelExecName, |
| 548 | kernel_crash_path.value()); |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 549 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 550 | LOG(INFO) << "Stored kcrash to " << kernel_crash_path.value(); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 551 | } |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 552 | |
| 553 | return true; |
| 554 | } |