blob: 6f2855ae1ebe511387cacd0e356e94634dc4cf8e [file] [log] [blame]
Ken Mixter9ee1f5f2011-10-25 02:15:05 +00001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Ken Mixter03403162010-08-18 15:23:16 -07002// 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 Mixter03403162010-08-18 15:23:16 -070010
Ken Mixterafcf8082010-10-26 14:45:01 -070011static const char kDefaultKernelStackSignature[] =
12 "kernel-UnspecifiedStackSignature";
Kees Cookce9556e2011-11-04 20:49:09 +000013static const char kDumpPath[] = "/dev/pstore";
14static const char kDumpFormat[] = "dmesg-ramoops-%d";
Ken Mixterafcf8082010-10-26 14:45:01 -070015static const char kKernelExecName[] = "kernel";
Kees Cookce9556e2011-11-04 20:49:09 +000016// Maximum number of records to examine in the kDumpPath.
17static const size_t kMaxDumpRecords = 100;
Ken Mixterafcf8082010-10-26 14:45:01 -070018const pid_t kKernelPid = 0;
19static const char kKernelSignatureKey[] = "sig";
20// Byte length of maximum human readable portion of a kernel crash signature.
21static const int kMaxHumanStringLength = 40;
Ken Mixterafcf8082010-10-26 14:45:01 -070022const 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.
25static const int kSignatureTimestampWindow = 2;
26// Kernel log timestamp regular expression.
27static const std::string kTimestampRegex("^<.*>\\[\\s*(\\d+\\.\\d+)\\]");
Ken Mixter03403162010-08-18 15:23:16 -070028
Simon Glassd74cc092011-04-06 10:47:01 -070029/*
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 */
40static const char *s_pc_regex[] = {
41 0,
42 " PC is at ([^\\+ ]+).*",
43 " EIP: \\[<.*>\\] ([^\\+ ]+).*", // X86 uses EIP for the program counter
44};
45
46COMPILE_ASSERT(arraysize(s_pc_regex) == KernelCollector::archCount,
47 missing_arch_pc_regexp);
48
Ken Mixter03403162010-08-18 15:23:16 -070049KernelCollector::KernelCollector()
50 : is_enabled_(false),
Kees Cookce9556e2011-11-04 20:49:09 +000051 ramoops_dump_path_(kDumpPath) {
Simon Glassd74cc092011-04-06 10:47:01 -070052 // We expect crash dumps in the format of the architecture we are built for.
53 arch_ = GetCompilerArch();
Ken Mixter03403162010-08-18 15:23:16 -070054}
55
56KernelCollector::~KernelCollector() {
57}
58
59void KernelCollector::OverridePreservedDumpPath(const FilePath &file_path) {
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070060 ramoops_dump_path_ = file_path;
61}
62
63bool KernelCollector::ReadRecordToString(std::string *contents,
Kees Cookce9556e2011-11-04 20:49:09 +000064 size_t current_record,
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070065 bool *record_found) {
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070066 // A record is a ramoops dump. It has an associated size of "record_size".
67 std::string record;
68 std::string captured;
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070069
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 Cookce9556e2011-11-04 20:49:09 +000075 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 Iordache1ea8abe2011-08-03 16:11:36 -070079 return false;
80 }
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070081
82 if (record_re.FullMatch(record, &captured)){
Kees Cook5825d5a2012-02-22 17:15:15 -080083 // Found a match, append it to the content, and remove from pstore.
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070084 contents->append(captured);
Kees Cook5825d5a2012-02-22 17:15:15 -080085 file_util::Delete(ramoops_record, false);
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070086 *record_found = true;
87 } else {
88 *record_found = false;
89 }
90
91 return true;
92}
93
Kees Cookce9556e2011-11-04 20:49:09 +000094void KernelCollector::GetRamoopsRecordPath(FilePath *path,
95 size_t record) {
96 *path = ramoops_dump_path_.Append(StringPrintf(kDumpFormat, record));
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070097}
98
99bool KernelCollector::LoadParameters() {
Kees Cookce9556e2011-11-04 20:49:09 +0000100 // Discover how many ramoops records are being exported by the driver.
101 size_t count;
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -0700102
Kees Cookce9556e2011-11-04 20:49:09 +0000103 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 Iordache1ea8abe2011-08-03 16:11:36 -0700109 }
110
Kees Cookce9556e2011-11-04 20:49:09 +0000111 records_ = count;
112 return (records_ > 0);
Ken Mixter03403162010-08-18 15:23:16 -0700113}
114
115bool KernelCollector::LoadPreservedDump(std::string *contents) {
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -0700116 // 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 Mixter03403162010-08-18 15:23:16 -0700122 // clear contents since ReadFileToString actually appends to the string.
123 contents->clear();
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -0700124
Kees Cookce9556e2011-11-04 20:49:09 +0000125 for (size_t i = 0; i < records_; ++i) {
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -0700126 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 Mixter03403162010-08-18 15:23:16 -0700136 return false;
137 }
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -0700138
Ken Mixter03403162010-08-18 15:23:16 -0700139 return true;
140}
141
Doug Anderson1e6b8bd2011-04-07 09:40:05 -0700142void 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 Mixter03403162010-08-18 15:23:16 -0700217bool KernelCollector::Enable() {
Simon Glassd74cc092011-04-06 10:47:01 -0700218 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 Cookce9556e2011-11-04 20:49:09 +0000223 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 Mixter03403162010-08-18 15:23:16 -0700230 }
231
232 // To enable crashes, we will eventually need to set
233 // the chnv bit in BIOS, but it does not yet work.
Ken Mixtera3249322011-03-03 08:47:38 -0800234 LOG(INFO) << "Enabling kernel crash handling";
Ken Mixter03403162010-08-18 15:23:16 -0700235 is_enabled_ = true;
236 return true;
237}
238
Ken Mixterafcf8082010-10-26 14:45:01 -0700239// 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).
243static 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
250void KernelCollector::ProcessStackTrace(
251 pcrecpp::StringPiece kernel_dump,
252 bool print_diagnostics,
253 unsigned *hash,
Luigi Semenzatof6400992011-12-29 13:18:35 -0800254 float *last_stack_timestamp,
255 bool *is_watchdog_crash) {
Ken Mixterafcf8082010-10-26 14:45:01 -0700256 pcrecpp::RE line_re("(.+)", pcrecpp::MULTILINE());
Simon Glassd74cc092011-04-06 10:47:01 -0700257 pcrecpp::RE stack_trace_start_re(kTimestampRegex +
258 " (Call Trace|Backtrace):$");
259
Luigi Semenzatof6400992011-12-29 13:18:35 -0800260 // Match lines such as the following and grab out "function_name".
261 // The ? may or may not be present.
262 //
Simon Glassd74cc092011-04-06 10:47:01 -0700263 // For ARM:
Luigi Semenzatof6400992011-12-29 13:18:35 -0800264 // <4>[ 3498.731164] [<c0057220>] ? (function_name+0x20/0x2c) from
265 // [<c018062c>] (foo_bar+0xdc/0x1bc)
Simon Glassd74cc092011-04-06 10:47:01 -0700266 //
267 // For X86:
Luigi Semenzatof6400992011-12-29 13:18:35 -0800268 // <4>[ 6066.849504] [<7937bcee>] ? function_name+0x66/0x6c
269 //
Ken Mixterafcf8082010-10-26 14:45:01 -0700270 pcrecpp::RE stack_entry_re(kTimestampRegex +
Simon Glassd74cc092011-04-06 10:47:01 -0700271 "\\s+\\[<[[:xdigit:]]+>\\]" // Matches " [<7937bcee>]"
272 "([\\s\\?(]+)" // Matches " ? (" (ARM) or " ? " (X86)
273 "([^\\+ )]+)"); // Matches until delimiter reached
Ken Mixterafcf8082010-10-26 14:45:01 -0700274 std::string line;
275 std::string hashable;
Luigi Semenzatof6400992011-12-29 13:18:35 -0800276 std::string previous_hashable;
277 bool is_watchdog = false;
Ken Mixterafcf8082010-10-26 14:45:01 -0700278
279 *hash = 0;
280 *last_stack_timestamp = 0;
281
Luigi Semenzatof6400992011-12-29 13:18:35 -0800282 // Find the last and second-to-last stack traces. The latter is used when
283 // the panic is from a watchdog timeout.
Ken Mixterafcf8082010-10-26 14:45:01 -0700284 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 Semenzatof6400992011-12-29 13:18:35 -0800289 printf("Stack trace starting.%s\n",
290 hashable.empty() ? "" : " Saving prior trace.");
Ken Mixterafcf8082010-10-26 14:45:01 -0700291 }
Luigi Semenzatof6400992011-12-29 13:18:35 -0800292 previous_hashable = hashable;
Ken Mixterafcf8082010-10-26 14:45:01 -0700293 hashable.clear();
Luigi Semenzatof6400992011-12-29 13:18:35 -0800294 is_watchdog = false;
Ken Mixterafcf8082010-10-26 14:45:01 -0700295 } 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 Semenzatof6400992011-12-29 13:18:35 -0800311 if (function_name == "watchdog_timer_fn" ||
312 function_name == "watchdog") {
313 is_watchdog = true;
314 }
Ken Mixterafcf8082010-10-26 14:45:01 -0700315 hashable.append(function_name);
316 }
317 }
318
Luigi Semenzatof6400992011-12-29 13:18:35 -0800319 // 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 Mixterafcf8082010-10-26 14:45:01 -0700331 *hash = HashString(hashable);
Luigi Semenzatof6400992011-12-29 13:18:35 -0800332 *is_watchdog_crash = is_watchdog;
Ken Mixterafcf8082010-10-26 14:45:01 -0700333
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 Glassd74cc092011-04-06 10:47:01 -0700340enum 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
351void KernelCollector::SetArch(enum ArchKind arch)
352{
353 arch_ = arch;
354}
355
Ken Mixterafcf8082010-10-26 14:45:01 -0700356bool KernelCollector::FindCrashingFunction(
Simon Glassd74cc092011-04-06 10:47:01 -0700357 pcrecpp::StringPiece kernel_dump,
358 bool print_diagnostics,
359 float stack_trace_timestamp,
360 std::string *crashing_function) {
Ken Mixterafcf8082010-10-26 14:45:01 -0700361 float timestamp = 0;
Simon Glassd74cc092011-04-06 10:47:01 -0700362
363 // Use the correct regex for this architecture.
364 pcrecpp::RE eip_re(kTimestampRegex + s_pc_regex[arch_],
365 pcrecpp::MULTILINE());
366
Ken Mixterafcf8082010-10-26 14:45:01 -0700367 while (eip_re.FindAndConsume(&kernel_dump, &timestamp, 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
393bool 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 &timestamp,
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
420bool 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 Semenzatof6400992011-12-29 13:18:35 -0800427 bool is_watchdog_crash;
Ken Mixterafcf8082010-10-26 14:45:01 -0700428
429 ProcessStackTrace(kernel_dump,
430 print_diagnostics,
431 &stack_hash,
Luigi Semenzatof6400992011-12-29 13:18:35 -0800432 &last_stack_timestamp,
433 &is_watchdog_crash);
Ken Mixterafcf8082010-10-26 14:45:01 -0700434
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 Semenzatof6400992011-12-29 13:18:35 -0800455 *kernel_signature = StringPrintf("%s-%s%s-%08X",
Ken Mixterafcf8082010-10-26 14:45:01 -0700456 kKernelExecName,
Luigi Semenzatof6400992011-12-29 13:18:35 -0800457 (is_watchdog_crash ? "(HANG)-" : ""),
Ken Mixterafcf8082010-10-26 14:45:01 -0700458 human_string.c_str(),
459 stack_hash);
460 return true;
461}
462
Ken Mixter03403162010-08-18 15:23:16 -0700463bool KernelCollector::Collect() {
464 std::string kernel_dump;
465 FilePath root_crash_directory;
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -0700466
467 if (!LoadParameters()) {
468 return false;
469 }
Ken Mixter03403162010-08-18 15:23:16 -0700470 if (!LoadPreservedDump(&kernel_dump)) {
471 return false;
472 }
Doug Anderson1e6b8bd2011-04-07 09:40:05 -0700473 StripSensitiveData(&kernel_dump);
Ken Mixter03403162010-08-18 15:23:16 -0700474 if (kernel_dump.empty()) {
475 return false;
476 }
Ken Mixterafcf8082010-10-26 14:45:01 -0700477 std::string signature;
478 if (!ComputeKernelStackSignature(kernel_dump, &signature, false)) {
479 signature = kDefaultKernelStackSignature;
480 }
Ken Mixteree849c52010-09-30 15:30:10 -0700481
Ken Mixter9ee1f5f2011-10-25 02:15:05 +0000482 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 Mixterafcf8082010-10-26 14:45:01 -0700491
Ken Mixtera3249322011-03-03 08:47:38 -0800492 LOG(INFO) << "Received prior crash notification from "
Ken Mixter9ee1f5f2011-10-25 02:15:05 +0000493 << "kernel (signature " << signature << ") (" << reason << ")";
Ken Mixterafcf8082010-10-26 14:45:01 -0700494
495 if (feedback) {
Ken Mixter03403162010-08-18 15:23:16 -0700496 count_crash_function_();
497
498 if (!GetCreatedCrashDirectoryByEuid(kRootUid,
Ken Mixter207694d2010-10-28 15:42:37 -0700499 &root_crash_directory,
500 NULL)) {
Ken Mixter03403162010-08-18 15:23:16 -0700501 return true;
502 }
503
Ken Mixteree849c52010-09-30 15:30:10 -0700504 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 Mixter9b346472010-11-07 13:45:45 -0800511 // 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 Mixter03403162010-08-18 15:23:16 -0700517 static_cast<int>(kernel_dump.length())) {
Ken Mixtera3249322011-03-03 08:47:38 -0800518 LOG(INFO) << "Failed to write kernel dump to "
519 << kernel_crash_path.value().c_str();
Ken Mixter03403162010-08-18 15:23:16 -0700520 return true;
521 }
522
Ken Mixterafcf8082010-10-26 14:45:01 -0700523 AddCrashMetaData(kKernelSignatureKey, signature);
Ken Mixteree849c52010-09-30 15:30:10 -0700524 WriteCrashMetaData(
525 root_crash_directory.Append(
526 StringPrintf("%s.meta", dump_basename.c_str())),
Ken Mixterc909b692010-10-18 12:26:05 -0700527 kKernelExecName,
528 kernel_crash_path.value());
Ken Mixteree849c52010-09-30 15:30:10 -0700529
Ken Mixtera3249322011-03-03 08:47:38 -0800530 LOG(INFO) << "Stored kcrash to " << kernel_crash_path.value();
Ken Mixter03403162010-08-18 15:23:16 -0700531 }
Ken Mixter03403162010-08-18 15:23:16 -0700532
533 return true;
534}