Mike Frysinger | 57b261c | 2012-04-11 14:47:09 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -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 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 5 | #include "crash-reporter/user_collector.h" |
| 6 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 7 | #include <bits/wordsize.h> |
| 8 | #include <elf.h> |
| 9 | #include <fcntl.h> |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 10 | #include <grp.h> // For struct group. |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 11 | #include <pcrecpp.h> |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 12 | #include <pwd.h> // For struct passwd. |
Ben Chan | f84ea21 | 2014-08-06 17:27:48 -0700 | [diff] [blame] | 13 | #include <stdint.h> |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 14 | #include <sys/types.h> // For getpwuid_r, getgrnam_r, WEXITSTATUS. |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 15 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 16 | #include <set> |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 17 | #include <string> |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 18 | #include <vector> |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 19 | |
Ben Chan | ab6cc90 | 2014-09-05 08:21:06 -0700 | [diff] [blame^] | 20 | #include <base/files/file_util.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
| 22 | #include <base/posix/eintr_wrapper.h> |
| 23 | #include <base/stl_util.h> |
| 24 | #include <base/strings/string_split.h> |
| 25 | #include <base/strings/string_util.h> |
| 26 | #include <base/strings/stringprintf.h> |
| 27 | #include <chromeos/process.h> |
| 28 | #include <chromeos/syslog_logging.h> |
| 29 | #include <gflags/gflags.h> |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 30 | |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 31 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 32 | DEFINE_bool(core2md_failure, false, "Core2md failure test"); |
| 33 | DEFINE_bool(directory_failure, false, "Spool directory failure test"); |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 34 | DEFINE_string(filter_in, "", |
| 35 | "Ignore all crashes but this for testing"); |
| 36 | #pragma GCC diagnostic error "-Wstrict-aliasing" |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 37 | |
| 38 | static const char kCollectionErrorSignature[] = |
| 39 | "crash_reporter-user-collection"; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 40 | // This procfs file is used to cause kernel core file writing to |
| 41 | // instead pipe the core file into a user space process. See |
| 42 | // core(5) man page. |
| 43 | static const char kCorePatternFile[] = "/proc/sys/kernel/core_pattern"; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 44 | static const char kCorePipeLimitFile[] = "/proc/sys/kernel/core_pipe_limit"; |
| 45 | // Set core_pipe_limit to 4 so that we can catch a few unrelated concurrent |
| 46 | // crashes, but finite to avoid infinitely recursing on crash handling. |
| 47 | static const char kCorePipeLimit[] = "4"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 48 | static const char kCoreToMinidumpConverterPath[] = "/usr/bin/core2md"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 49 | |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 50 | static const char kStatePrefix[] = "State:\t"; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 51 | |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 52 | // Define an otherwise invalid value that represents an unknown UID. |
| 53 | static const uid_t kUnknownUid = -1; |
| 54 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 55 | const char *UserCollector::kUserId = "Uid:\t"; |
| 56 | const char *UserCollector::kGroupId = "Gid:\t"; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 57 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 58 | using base::FilePath; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 59 | using base::StringPrintf; |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 60 | |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 61 | UserCollector::UserCollector() |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 62 | : generate_diagnostics_(false), |
| 63 | core_pattern_file_(kCorePatternFile), |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 64 | core_pipe_limit_file_(kCorePipeLimitFile), |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 65 | initialized_(false) { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void UserCollector::Initialize( |
| 69 | UserCollector::CountCrashFunction count_crash_function, |
| 70 | const std::string &our_path, |
| 71 | UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function, |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 72 | bool generate_diagnostics) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 73 | CrashCollector::Initialize(count_crash_function, |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 74 | is_feedback_allowed_function); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 75 | our_path_ = our_path; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 76 | initialized_ = true; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 77 | generate_diagnostics_ = generate_diagnostics; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | UserCollector::~UserCollector() { |
| 81 | } |
| 82 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 83 | std::string UserCollector::GetErrorTypeSignature(ErrorType error_type) const { |
| 84 | switch (error_type) { |
| 85 | case kErrorSystemIssue: |
| 86 | return "system-issue"; |
| 87 | case kErrorReadCoreData: |
| 88 | return "read-core-data"; |
| 89 | case kErrorUnusableProcFiles: |
| 90 | return "unusable-proc-files"; |
| 91 | case kErrorInvalidCoreFile: |
| 92 | return "invalid-core-file"; |
| 93 | case kErrorUnsupported32BitCoreFile: |
| 94 | return "unsupported-32bit-core-file"; |
| 95 | case kErrorCore2MinidumpConversion: |
| 96 | return "core2md-conversion"; |
| 97 | default: |
| 98 | return ""; |
| 99 | } |
| 100 | } |
| 101 | |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 102 | // Return the string that should be used for the kernel's core_pattern file. |
| 103 | // Note that if you change the format of the enabled pattern, you'll probably |
| 104 | // also need to change the ParseCrashAttributes() function below, the |
| 105 | // user_collector_test.cc unittest, and the logging_UserCrash.py autotest. |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 106 | std::string UserCollector::GetPattern(bool enabled) const { |
| 107 | if (enabled) { |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 108 | // Combine the four crash attributes into one parameter to try to reduce |
| 109 | // the size of the invocation line for crash_reporter, since the kernel |
| 110 | // has a fixed-sized (128B) buffer for it (before parameter expansion). |
| 111 | // Note that the kernel does not support quoted arguments in core_pattern. |
Mike Frysinger | bfdf4a8 | 2014-03-14 09:31:33 -0400 | [diff] [blame] | 112 | return StringPrintf("|%s --user=%%P:%%s:%%u:%%e", our_path_.c_str()); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 113 | } else { |
| 114 | return "core"; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | bool UserCollector::SetUpInternal(bool enabled) { |
| 119 | CHECK(initialized_); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 120 | LOG(INFO) << (enabled ? "Enabling" : "Disabling") << " user crash handling"; |
| 121 | |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 122 | if (base::WriteFile(FilePath(core_pipe_limit_file_), kCorePipeLimit, |
| 123 | strlen(kCorePipeLimit)) != |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 124 | static_cast<int>(strlen(kCorePipeLimit))) { |
Chris Masone | b3fe6c3 | 2013-05-31 09:37:33 -0700 | [diff] [blame] | 125 | PLOG(ERROR) << "Unable to write " << core_pipe_limit_file_; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 126 | return false; |
| 127 | } |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 128 | std::string pattern = GetPattern(enabled); |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 129 | if (base::WriteFile(FilePath(core_pattern_file_), pattern.c_str(), |
| 130 | pattern.length()) != static_cast<int>(pattern.length())) { |
Chris Masone | b3fe6c3 | 2013-05-31 09:37:33 -0700 | [diff] [blame] | 131 | PLOG(ERROR) << "Unable to write " << core_pattern_file_; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 137 | bool UserCollector::GetFirstLineWithPrefix( |
| 138 | const std::vector<std::string> &lines, |
| 139 | const char *prefix, std::string *line) { |
| 140 | std::vector<std::string>::const_iterator line_iterator; |
| 141 | for (line_iterator = lines.begin(); line_iterator != lines.end(); |
| 142 | ++line_iterator) { |
| 143 | if (line_iterator->find(prefix) == 0) { |
| 144 | *line = *line_iterator; |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | bool UserCollector::GetIdFromStatus( |
| 152 | const char *prefix, IdKind kind, |
| 153 | const std::vector<std::string> &status_lines, int *id) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 154 | // From fs/proc/array.c:task_state(), this file contains: |
| 155 | // \nUid:\t<uid>\t<euid>\t<suid>\t<fsuid>\n |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 156 | std::string id_line; |
| 157 | if (!GetFirstLineWithPrefix(status_lines, prefix, &id_line)) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 158 | return false; |
| 159 | } |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 160 | std::string id_substring = id_line.substr(strlen(prefix), std::string::npos); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 161 | std::vector<std::string> ids; |
Chris Masone | 3ba6c5b | 2011-05-13 16:57:09 -0700 | [diff] [blame] | 162 | base::SplitString(id_substring, '\t', &ids); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 163 | if (ids.size() != kIdMax || kind < 0 || kind >= kIdMax) { |
| 164 | return false; |
| 165 | } |
| 166 | const char *number = ids[kind].c_str(); |
| 167 | char *end_number = NULL; |
| 168 | *id = strtol(number, &end_number, 10); |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 169 | if (*end_number != '\0') { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 170 | return false; |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 171 | } |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | bool UserCollector::GetStateFromStatus( |
| 176 | const std::vector<std::string> &status_lines, std::string *state) { |
| 177 | std::string state_line; |
| 178 | if (!GetFirstLineWithPrefix(status_lines, kStatePrefix, &state_line)) { |
| 179 | return false; |
| 180 | } |
| 181 | *state = state_line.substr(strlen(kStatePrefix), std::string::npos); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 182 | return true; |
| 183 | } |
| 184 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 185 | void UserCollector::EnqueueCollectionErrorLog(pid_t pid, |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 186 | ErrorType error_type, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 187 | const std::string &exec) { |
| 188 | FilePath crash_path; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 189 | LOG(INFO) << "Writing conversion problems as separate crash report."; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 190 | if (!GetCreatedCrashDirectoryByEuid(0, &crash_path, NULL)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 191 | LOG(ERROR) << "Could not even get log directory; out of space?"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 192 | return; |
| 193 | } |
Thiemo Nagel | 8fce285 | 2014-05-09 14:48:45 +0200 | [diff] [blame] | 194 | AddCrashMetaData("sig", kCollectionErrorSignature); |
| 195 | AddCrashMetaData("error_type", GetErrorTypeSignature(error_type)); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 196 | std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 197 | std::string error_log = chromeos::GetLog(); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 198 | FilePath diag_log_path = GetCrashPath(crash_path, dump_basename, "diaglog"); |
Simon Que | acc7938 | 2012-05-04 18:10:09 -0700 | [diff] [blame] | 199 | if (GetLogContents(FilePath(log_config_path_), kCollectionErrorSignature, |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 200 | diag_log_path)) { |
| 201 | // We load the contents of diag_log into memory and append it to |
| 202 | // the error log. We cannot just append to files because we need |
| 203 | // to always create new files to prevent attack. |
| 204 | std::string diag_log_contents; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 205 | base::ReadFileToString(diag_log_path, &diag_log_contents); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 206 | error_log.append(diag_log_contents); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 207 | base::DeleteFile(diag_log_path, false); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 208 | } |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 209 | FilePath log_path = GetCrashPath(crash_path, dump_basename, "log"); |
| 210 | FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta"); |
Ben Chan | f30c641 | 2014-05-22 23:09:01 -0700 | [diff] [blame] | 211 | // We must use WriteNewFile instead of base::WriteFile as we do |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 212 | // not want to write with root access to a symlink that an attacker |
| 213 | // might have created. |
Thiemo Nagel | 8fce285 | 2014-05-09 14:48:45 +0200 | [diff] [blame] | 214 | if (WriteNewFile(log_path, error_log.data(), error_log.length()) < 0) { |
| 215 | LOG(ERROR) << "Error writing new file " << log_path.value(); |
| 216 | return; |
| 217 | } |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 218 | WriteCrashMetaData(meta_path, exec, log_path.value()); |
| 219 | } |
| 220 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 221 | bool UserCollector::CopyOffProcFiles(pid_t pid, |
| 222 | const FilePath &container_dir) { |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 223 | if (!base::CreateDirectory(container_dir)) { |
Chris Masone | b3fe6c3 | 2013-05-31 09:37:33 -0700 | [diff] [blame] | 224 | PLOG(ERROR) << "Could not create " << container_dir.value().c_str(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | FilePath process_path = GetProcessPath(pid); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 228 | if (!base::PathExists(process_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 229 | LOG(ERROR) << "Path " << process_path.value() << " does not exist"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 230 | return false; |
| 231 | } |
| 232 | static const char *proc_files[] = { |
| 233 | "auxv", |
| 234 | "cmdline", |
| 235 | "environ", |
| 236 | "maps", |
| 237 | "status" |
| 238 | }; |
| 239 | for (unsigned i = 0; i < arraysize(proc_files); ++i) { |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 240 | if (!base::CopyFile(process_path.Append(proc_files[i]), |
| 241 | container_dir.Append(proc_files[i]))) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 242 | LOG(ERROR) << "Could not copy " << proc_files[i] << " file"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | } |
Ben Chan | ec7d783 | 2012-01-09 10:29:58 -0800 | [diff] [blame] | 246 | return true; |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 249 | bool UserCollector::ValidateProcFiles(const FilePath &container_dir) const { |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 250 | // Check if the maps file is empty, which could be due to the crashed |
| 251 | // process being reaped by the kernel before finishing a core dump. |
Ben Chan | f84ea21 | 2014-08-06 17:27:48 -0700 | [diff] [blame] | 252 | int64_t file_size = 0; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 253 | if (!base::GetFileSize(container_dir.Append("maps"), &file_size)) { |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 254 | LOG(ERROR) << "Could not get the size of maps file"; |
| 255 | return false; |
| 256 | } |
| 257 | if (file_size == 0) { |
| 258 | LOG(ERROR) << "maps file is empty"; |
| 259 | return false; |
| 260 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 261 | return true; |
| 262 | } |
| 263 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 264 | UserCollector::ErrorType UserCollector::ValidateCoreFile( |
| 265 | const FilePath &core_path) const { |
| 266 | int fd = HANDLE_EINTR(open(core_path.value().c_str(), O_RDONLY)); |
| 267 | if (fd < 0) { |
Chris Masone | b3fe6c3 | 2013-05-31 09:37:33 -0700 | [diff] [blame] | 268 | PLOG(ERROR) << "Could not open core file " << core_path.value(); |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 269 | return kErrorInvalidCoreFile; |
| 270 | } |
| 271 | |
| 272 | char e_ident[EI_NIDENT]; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 273 | bool read_ok = base::ReadFromFD(fd, e_ident, sizeof(e_ident)); |
Mike Frysinger | f1a5014 | 2014-05-14 16:05:09 -0400 | [diff] [blame] | 274 | IGNORE_EINTR(close(fd)); |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 275 | if (!read_ok) { |
| 276 | LOG(ERROR) << "Could not read header of core file"; |
| 277 | return kErrorInvalidCoreFile; |
| 278 | } |
| 279 | |
| 280 | if (e_ident[EI_MAG0] != ELFMAG0 || e_ident[EI_MAG1] != ELFMAG1 || |
| 281 | e_ident[EI_MAG2] != ELFMAG2 || e_ident[EI_MAG3] != ELFMAG3) { |
| 282 | LOG(ERROR) << "Invalid core file"; |
| 283 | return kErrorInvalidCoreFile; |
| 284 | } |
| 285 | |
| 286 | #if __WORDSIZE == 64 |
| 287 | // TODO(benchan, mkrebs): Remove this check once core2md can |
| 288 | // handles both 32-bit and 64-bit ELF on a 64-bit platform. |
| 289 | if (e_ident[EI_CLASS] == ELFCLASS32) { |
| 290 | LOG(ERROR) << "Conversion of 32-bit core file on 64-bit platform is " |
| 291 | << "currently not supported"; |
| 292 | return kErrorUnsupported32BitCoreFile; |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | return kErrorNone; |
| 297 | } |
| 298 | |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 299 | bool UserCollector::GetCreatedCrashDirectory(pid_t pid, uid_t supplied_ruid, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 300 | FilePath *crash_file_path, |
| 301 | bool *out_of_capacity) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 302 | FilePath process_path = GetProcessPath(pid); |
| 303 | std::string status; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 304 | if (FLAGS_directory_failure) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 305 | LOG(ERROR) << "Purposefully failing to create spool directory"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 306 | return false; |
| 307 | } |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 308 | |
| 309 | uid_t uid; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 310 | if (base::ReadFileToString(process_path.Append("status"), &status)) { |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 311 | std::vector<std::string> status_lines; |
| 312 | base::SplitString(status, '\n', &status_lines); |
| 313 | |
| 314 | std::string process_state; |
| 315 | if (!GetStateFromStatus(status_lines, &process_state)) { |
| 316 | LOG(ERROR) << "Could not find process state in status file"; |
| 317 | return false; |
| 318 | } |
| 319 | LOG(INFO) << "State of crashed process [" << pid << "]: " << process_state; |
| 320 | |
| 321 | // Get effective UID of crashing process. |
| 322 | int id; |
| 323 | if (!GetIdFromStatus(kUserId, kIdEffective, status_lines, &id)) { |
| 324 | LOG(ERROR) << "Could not find euid in status file"; |
| 325 | return false; |
| 326 | } |
| 327 | uid = id; |
| 328 | } else if (supplied_ruid != kUnknownUid) { |
| 329 | LOG(INFO) << "Using supplied UID " << supplied_ruid |
| 330 | << " for crashed process [" << pid |
| 331 | << "] due to error reading status file"; |
| 332 | uid = supplied_ruid; |
| 333 | } else { |
| 334 | LOG(ERROR) << "Could not read status file and kernel did not supply UID"; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 335 | LOG(INFO) << "Path " << process_path.value() << " DirectoryExists: " |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 336 | << base::DirectoryExists(process_path); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 337 | return false; |
| 338 | } |
Ben Chan | f13bb58 | 2012-01-06 08:22:07 -0800 | [diff] [blame] | 339 | |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 340 | if (!GetCreatedCrashDirectoryByEuid(uid, crash_file_path, out_of_capacity)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 341 | LOG(ERROR) << "Could not create crash directory"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 342 | return false; |
| 343 | } |
| 344 | return true; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | bool UserCollector::CopyStdinToCoreFile(const FilePath &core_path) { |
| 348 | // Copy off all stdin to a core file. |
| 349 | FilePath stdin_path("/dev/fd/0"); |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 350 | if (base::CopyFile(stdin_path, core_path)) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 351 | return true; |
| 352 | } |
| 353 | |
Chris Masone | b3fe6c3 | 2013-05-31 09:37:33 -0700 | [diff] [blame] | 354 | PLOG(ERROR) << "Could not write core file"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 355 | // If the file system was full, make sure we remove any remnants. |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 356 | base::DeleteFile(core_path, false); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 360 | bool UserCollector::RunCoreToMinidump(const FilePath &core_path, |
| 361 | const FilePath &procfs_directory, |
| 362 | const FilePath &minidump_path, |
| 363 | const FilePath &temp_directory) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 364 | FilePath output_path = temp_directory.Append("output"); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 365 | chromeos::ProcessImpl core2md; |
| 366 | core2md.RedirectOutput(output_path.value()); |
| 367 | core2md.AddArg(kCoreToMinidumpConverterPath); |
| 368 | core2md.AddArg(core_path.value()); |
| 369 | core2md.AddArg(procfs_directory.value()); |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 370 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 371 | if (!FLAGS_core2md_failure) { |
| 372 | core2md.AddArg(minidump_path.value()); |
| 373 | } else { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 374 | // To test how core2md errors are propagaged, cause an error |
| 375 | // by forgetting a required argument. |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 378 | int errorlevel = core2md.Run(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 379 | |
| 380 | std::string output; |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 381 | base::ReadFileToString(output_path, &output); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 382 | if (errorlevel != 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 383 | LOG(ERROR) << "Problem during " << kCoreToMinidumpConverterPath |
| 384 | << " [result=" << errorlevel << "]: " << output; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 385 | return false; |
| 386 | } |
| 387 | |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 388 | if (!base::PathExists(minidump_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 389 | LOG(ERROR) << "Minidump file " << minidump_path.value() |
| 390 | << " was not created"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 391 | return false; |
| 392 | } |
| 393 | return true; |
| 394 | } |
| 395 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 396 | UserCollector::ErrorType UserCollector::ConvertCoreToMinidump( |
| 397 | pid_t pid, |
| 398 | const FilePath &container_dir, |
| 399 | const FilePath &core_path, |
| 400 | const FilePath &minidump_path) { |
Ben Chan | ec7d783 | 2012-01-09 10:29:58 -0800 | [diff] [blame] | 401 | // If proc files are unuable, we continue to read the core file from stdin, |
| 402 | // but only skip the core-to-minidump conversion, so that we may still use |
| 403 | // the core file for debugging. |
| 404 | bool proc_files_usable = |
| 405 | CopyOffProcFiles(pid, container_dir) && ValidateProcFiles(container_dir); |
| 406 | |
| 407 | if (!CopyStdinToCoreFile(core_path)) { |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 408 | return kErrorReadCoreData; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Ben Chan | ec7d783 | 2012-01-09 10:29:58 -0800 | [diff] [blame] | 411 | if (!proc_files_usable) { |
| 412 | LOG(INFO) << "Skipped converting core file to minidump due to " |
| 413 | << "unusable proc files"; |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 414 | return kErrorUnusableProcFiles; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 417 | ErrorType error = ValidateCoreFile(core_path); |
| 418 | if (error != kErrorNone) { |
| 419 | return error; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 422 | if (!RunCoreToMinidump(core_path, |
| 423 | container_dir, // procfs directory |
| 424 | minidump_path, |
| 425 | container_dir)) { // temporary directory |
| 426 | return kErrorCore2MinidumpConversion; |
| 427 | } |
| 428 | |
| 429 | LOG(INFO) << "Stored minidump to " << minidump_path.value(); |
| 430 | return kErrorNone; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 433 | UserCollector::ErrorType UserCollector::ConvertAndEnqueueCrash( |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 434 | pid_t pid, const std::string &exec, uid_t supplied_ruid, |
| 435 | bool *out_of_capacity) { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 436 | FilePath crash_path; |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 437 | if (!GetCreatedCrashDirectory(pid, supplied_ruid, &crash_path, |
| 438 | out_of_capacity)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 439 | LOG(ERROR) << "Unable to find/create process-specific crash path"; |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 440 | return kErrorSystemIssue; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Ben Chan | 294d5d1 | 2012-01-04 20:40:15 -0800 | [diff] [blame] | 443 | // Directory like /tmp/crash_reporter/1234 which contains the |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 444 | // procfs entries and other temporary files used during conversion. |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 445 | FilePath container_dir(StringPrintf("/tmp/crash_reporter/%d", pid)); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 446 | // Delete a pre-existing directory from crash reporter that may have |
| 447 | // been left around for diagnostics from a failed conversion attempt. |
| 448 | // If we don't, existing files can cause forking to fail. |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 449 | base::DeleteFile(container_dir, true); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 450 | std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid); |
| 451 | FilePath core_path = GetCrashPath(crash_path, dump_basename, "core"); |
| 452 | FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta"); |
| 453 | FilePath minidump_path = GetCrashPath(crash_path, dump_basename, "dmp"); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 454 | FilePath log_path = GetCrashPath(crash_path, dump_basename, "log"); |
| 455 | |
Simon Que | acc7938 | 2012-05-04 18:10:09 -0700 | [diff] [blame] | 456 | if (GetLogContents(FilePath(log_config_path_), exec, log_path)) |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 457 | AddCrashMetaData("log", log_path.value()); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 458 | |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 459 | ErrorType error_type = |
| 460 | ConvertCoreToMinidump(pid, container_dir, core_path, minidump_path); |
| 461 | if (error_type != kErrorNone) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 462 | LOG(INFO) << "Leaving core file at " << core_path.value() |
| 463 | << " due to conversion error"; |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 464 | return error_type; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // Here we commit to sending this file. We must not return false |
| 468 | // after this point or we will generate a log report as well as a |
| 469 | // crash report. |
| 470 | WriteCrashMetaData(meta_path, |
| 471 | exec, |
| 472 | minidump_path.value()); |
| 473 | |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame] | 474 | if (!IsDeveloperImage()) { |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 475 | base::DeleteFile(core_path, false); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 476 | } else { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 477 | LOG(INFO) << "Leaving core file at " << core_path.value() |
| 478 | << " due to developer image"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Mike Frysinger | a557c11 | 2014-02-05 22:55:39 -0500 | [diff] [blame] | 481 | base::DeleteFile(container_dir, true); |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 482 | return kErrorNone; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 485 | bool UserCollector::ParseCrashAttributes(const std::string &crash_attributes, |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 486 | pid_t *pid, int *signal, uid_t *uid, |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 487 | std::string *kernel_supplied_name) { |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 488 | pcrecpp::RE re("(\\d+):(\\d+):(\\d+):(.*)"); |
| 489 | if (re.FullMatch(crash_attributes, pid, signal, uid, kernel_supplied_name)) |
| 490 | return true; |
| 491 | |
| 492 | LOG(INFO) << "Falling back to parsing crash attributes '" |
| 493 | << crash_attributes << "' without UID"; |
| 494 | pcrecpp::RE re_without_uid("(\\d+):(\\d+):(.*)"); |
| 495 | *uid = kUnknownUid; |
| 496 | return re_without_uid.FullMatch(crash_attributes, pid, signal, |
| 497 | kernel_supplied_name); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 500 | // Returns true if the given executable name matches that of Chrome. This |
| 501 | // includes checks for threads that Chrome has renamed. |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 502 | static bool IsChromeExecName(const std::string &exec) { |
| 503 | static const char *kChromeNames[] = { |
| 504 | "chrome", |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 505 | // These are additional thread names seen in http://crash/ |
Michael Krebs | b1b91a5 | 2012-11-26 14:26:17 -0800 | [diff] [blame] | 506 | "MediaPipeline", |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 507 | // These come from the use of base::PlatformThread::SetName() directly |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 508 | "CrBrowserMain", "CrRendererMain", "CrUtilityMain", "CrPPAPIMain", |
| 509 | "CrPPAPIBrokerMain", "CrPluginMain", "CrWorkerMain", "CrGpuMain", |
| 510 | "BrokerEvent", "CrVideoRenderer", "CrShutdownDetector", |
| 511 | "UsbEventHandler", "CrNaClMain", "CrServiceMain", |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 512 | // These thread names come from the use of base::Thread |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 513 | "Gamepad polling thread", "Chrome_InProcGpuThread", |
| 514 | "Chrome_DragDropThread", "Renderer::FILE", "VC manager", |
| 515 | "VideoCaptureModuleImpl", "JavaBridge", "VideoCaptureManagerThread", |
| 516 | "Geolocation", "Geolocation_wifi_provider", |
| 517 | "Device orientation polling thread", "Chrome_InProcRendererThread", |
| 518 | "NetworkChangeNotifier", "Watchdog", "inotify_reader", |
| 519 | "cf_iexplore_background_thread", "BrowserWatchdog", |
| 520 | "Chrome_HistoryThread", "Chrome_SyncThread", "Chrome_ShellDialogThread", |
| 521 | "Printing_Worker", "Chrome_SafeBrowsingThread", "SimpleDBThread", |
| 522 | "D-Bus thread", "AudioThread", "NullAudioThread", "V4L2Thread", |
| 523 | "ChromotingClientDecodeThread", "Profiling_Flush", |
| 524 | "worker_thread_ticker", "AudioMixerAlsa", "AudioMixerCras", |
| 525 | "FakeAudioRecordingThread", "CaptureThread", |
| 526 | "Chrome_WebSocketproxyThread", "ProcessWatcherThread", |
| 527 | "Chrome_CameraThread", "import_thread", "NaCl_IOThread", |
| 528 | "Chrome_CloudPrintJobPrintThread", "Chrome_CloudPrintProxyCoreThread", |
| 529 | "DaemonControllerFileIO", "ChromotingMainThread", |
| 530 | "ChromotingEncodeThread", "ChromotingDesktopThread", |
| 531 | "ChromotingIOThread", "ChromotingFileIOThread", |
| 532 | "Chrome_libJingle_WorkerThread", "Chrome_ChildIOThread", |
| 533 | "GLHelperThread", "RemotingHostPlugin", |
| 534 | // "PAC thread #%d", // not easy to check because of "%d" |
| 535 | "Chrome_DBThread", "Chrome_WebKitThread", "Chrome_FileThread", |
| 536 | "Chrome_FileUserBlockingThread", "Chrome_ProcessLauncherThread", |
| 537 | "Chrome_CacheThread", "Chrome_IOThread", "Cache Thread", "File Thread", |
| 538 | "ServiceProcess_IO", "ServiceProcess_File", |
| 539 | "extension_crash_uploader", "gpu-process_crash_uploader", |
| 540 | "plugin_crash_uploader", "renderer_crash_uploader", |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 541 | // These come from the use of webkit_glue::WebThreadImpl |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 542 | "Compositor", "Browser Compositor", |
| 543 | // "WorkerPool/%d", // not easy to check because of "%d" |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 544 | // These come from the use of base::Watchdog |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 545 | "Startup watchdog thread Watchdog", "Shutdown watchdog thread Watchdog", |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 546 | // These come from the use of AudioDeviceThread::Start |
Michael Krebs | a1cc383 | 2012-09-13 13:24:12 -0700 | [diff] [blame] | 547 | "AudioDevice", "AudioInputDevice", "AudioOutputDevice", |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 548 | // These come from the use of MessageLoopFactory::GetMessageLoop |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 549 | "GpuVideoDecoder", "RtcVideoDecoderThread", "PipelineThread", |
| 550 | "AudioDecoderThread", "VideoDecoderThread", |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 551 | // These come from the use of MessageLoopFactory::GetMessageLoopProxy |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 552 | "CaptureVideoDecoderThread", "CaptureVideoDecoder", |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 553 | // These come from the use of base::SimpleThread |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 554 | "LocalInputMonitor/%d", // "%d" gets lopped off for kernel-supplied |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 555 | // These come from the use of base::DelegateSimpleThread |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 556 | "ipc_channel_nacl reader thread/%d", "plugin_audio_input_thread/%d", |
| 557 | "plugin_audio_thread/%d", |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 558 | // These come from the use of base::SequencedWorkerPool |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 559 | "BrowserBlockingWorker%d/%d", // "%d" gets lopped off for kernel-supplied |
| 560 | }; |
| 561 | static std::set<std::string> chrome_names; |
| 562 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 563 | // Initialize a set of chrome names, for efficient lookup |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 564 | if (chrome_names.empty()) { |
| 565 | for (size_t i = 0; i < arraysize(kChromeNames); i++) { |
| 566 | std::string check_name(kChromeNames[i]); |
| 567 | chrome_names.insert(check_name); |
| 568 | // When checking a kernel-supplied name, it should be truncated to 15 |
| 569 | // chars. See PR_SET_NAME in |
| 570 | // http://www.kernel.org/doc/man-pages/online/pages/man2/prctl.2.html, |
| 571 | // although that page misleads by saying "16 bytes". |
| 572 | chrome_names.insert("supplied_" + std::string(check_name, 0, 15)); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | return ContainsKey(chrome_names, exec); |
| 577 | } |
| 578 | |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 579 | bool UserCollector::ShouldDump(bool has_owner_consent, |
| 580 | bool is_developer, |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 581 | bool handle_chrome_crashes, |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 582 | const std::string &exec, |
| 583 | std::string *reason) { |
| 584 | reason->clear(); |
| 585 | |
| 586 | // Treat Chrome crashes as if the user opted-out. We stop counting Chrome |
| 587 | // crashes towards user crashes, so user crashes really mean non-Chrome |
| 588 | // user-space crashes. |
Michael Krebs | 2f3ed03 | 2012-08-21 20:17:03 -0700 | [diff] [blame] | 589 | if (!handle_chrome_crashes && IsChromeExecName(exec)) { |
Mike Frysinger | d2db5ff | 2013-10-08 19:03:23 -0400 | [diff] [blame] | 590 | *reason = "ignoring call by kernel - chrome crash; " |
| 591 | "waiting for chrome to call us directly"; |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 592 | return false; |
| 593 | } |
| 594 | |
| 595 | // For developer builds, we always want to keep the crash reports unless |
| 596 | // we're testing the crash facilities themselves. This overrides |
| 597 | // feedback. Crash sending still obeys consent. |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame] | 598 | if (is_developer) { |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 599 | *reason = "developer build - not testing - always dumping"; |
| 600 | return true; |
| 601 | } |
| 602 | |
| 603 | if (!has_owner_consent) { |
| 604 | *reason = "ignoring - no consent"; |
| 605 | return false; |
| 606 | } |
| 607 | |
| 608 | *reason = "handling"; |
| 609 | return true; |
| 610 | } |
| 611 | |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 612 | bool UserCollector::HandleCrash(const std::string &crash_attributes, |
| 613 | const char *force_exec) { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 614 | CHECK(initialized_); |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 615 | pid_t pid = 0; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 616 | int signal = 0; |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 617 | uid_t supplied_ruid = kUnknownUid; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 618 | std::string kernel_supplied_name; |
| 619 | |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 620 | if (!ParseCrashAttributes(crash_attributes, &pid, &signal, &supplied_ruid, |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 621 | &kernel_supplied_name)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 622 | LOG(ERROR) << "Invalid parameter: --user=" << crash_attributes; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 623 | return false; |
| 624 | } |
| 625 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 626 | std::string exec; |
| 627 | if (force_exec) { |
| 628 | exec.assign(force_exec); |
| 629 | } else if (!GetExecutableBaseNameFromPid(pid, &exec)) { |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 630 | // If we cannot find the exec name, use the kernel supplied name. |
| 631 | // We don't always use the kernel's since it truncates the name to |
| 632 | // 16 characters. |
| 633 | exec = StringPrintf("supplied_%s", kernel_supplied_name.c_str()); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 634 | } |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 635 | |
| 636 | // Allow us to test the crash reporting mechanism successfully even if |
| 637 | // other parts of the system crash. |
| 638 | if (!FLAGS_filter_in.empty() && |
| 639 | (FLAGS_filter_in == "none" || |
| 640 | FLAGS_filter_in != exec)) { |
| 641 | // We use a different format message to make it more obvious in tests |
| 642 | // which crashes are test generated and which are real. |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 643 | LOG(WARNING) << "Ignoring crash from " << exec << "[" << pid << "] while " |
| 644 | << "filter_in=" << FLAGS_filter_in << "."; |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 645 | return true; |
| 646 | } |
| 647 | |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 648 | std::string reason; |
| 649 | bool dump = ShouldDump(is_feedback_allowed_function_(), |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame] | 650 | IsDeveloperImage(), |
Michael Krebs | 4fe30db | 2011-08-05 13:54:52 -0700 | [diff] [blame] | 651 | ShouldHandleChromeCrashes(), |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 652 | exec, |
| 653 | &reason); |
Ken Mixter | 2105b49 | 2010-11-09 16:14:38 -0800 | [diff] [blame] | 654 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 655 | LOG(WARNING) << "Received crash notification for " << exec << "[" << pid |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 656 | << "] sig " << signal << ", user " << supplied_ruid |
| 657 | << " (" << reason << ")"; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 658 | |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 659 | if (dump) { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 660 | count_crash_function_(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 661 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 662 | if (generate_diagnostics_) { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 663 | bool out_of_capacity = false; |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 664 | ErrorType error_type = |
Michael Krebs | 1c57e9e | 2012-09-25 18:03:13 -0700 | [diff] [blame] | 665 | ConvertAndEnqueueCrash(pid, exec, supplied_ruid, &out_of_capacity); |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 666 | if (error_type != kErrorNone) { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 667 | if (!out_of_capacity) |
Ben Chan | 6e709a1 | 2012-02-29 12:10:44 -0800 | [diff] [blame] | 668 | EnqueueCollectionErrorLog(pid, error_type, exec); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 669 | return false; |
| 670 | } |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 671 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 672 | } |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 673 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 674 | return true; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 675 | } |