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