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