Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 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 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 7 | #include <grp.h> // For struct group. |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 8 | #include <pcrecpp.h> |
| 9 | #include <pcrecpp.h> |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 10 | #include <pwd.h> // For struct passwd. |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 11 | #include <sys/types.h> // For getpwuid_r, getgrnam_r, WEXITSTATUS. |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 12 | |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 13 | #include <string> |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 14 | #include <vector> |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 15 | |
| 16 | #include "base/file_util.h" |
| 17 | #include "base/logging.h" |
Chris Masone | 8a68c7c | 2011-05-14 11:44:04 -0700 | [diff] [blame] | 18 | #include "base/string_split.h" |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 19 | #include "base/string_util.h" |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 20 | #include "chromeos/process.h" |
| 21 | #include "chromeos/syslog_logging.h" |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 22 | #include "gflags/gflags.h" |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 23 | |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 24 | #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 25 | DEFINE_bool(core2md_failure, false, "Core2md failure test"); |
| 26 | DEFINE_bool(directory_failure, false, "Spool directory failure test"); |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 27 | DEFINE_string(filter_in, "", |
| 28 | "Ignore all crashes but this for testing"); |
| 29 | #pragma GCC diagnostic error "-Wstrict-aliasing" |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 30 | |
| 31 | static const char kCollectionErrorSignature[] = |
| 32 | "crash_reporter-user-collection"; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 33 | // This procfs file is used to cause kernel core file writing to |
| 34 | // instead pipe the core file into a user space process. See |
| 35 | // core(5) man page. |
| 36 | static const char kCorePatternFile[] = "/proc/sys/kernel/core_pattern"; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 37 | static const char kCorePipeLimitFile[] = "/proc/sys/kernel/core_pipe_limit"; |
| 38 | // Set core_pipe_limit to 4 so that we can catch a few unrelated concurrent |
| 39 | // crashes, but finite to avoid infinitely recursing on crash handling. |
| 40 | static const char kCorePipeLimit[] = "4"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 41 | static const char kCoreToMinidumpConverterPath[] = "/usr/bin/core2md"; |
Ken Mixter | 7ac7a70 | 2010-08-13 15:43:34 -0700 | [diff] [blame] | 42 | static const char kLeaveCoreFile[] = "/root/.leave_core"; |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame^] | 43 | static const char kCollectChromeFile[] = |
| 44 | "/mnt/stateful_partition/etc/collect_chrome_crashes"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 45 | |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 46 | static const char kDefaultLogConfig[] = "/etc/crash_reporter_logs.conf"; |
| 47 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 48 | const char *UserCollector::kUserId = "Uid:\t"; |
| 49 | const char *UserCollector::kGroupId = "Gid:\t"; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 50 | |
| 51 | UserCollector::UserCollector() |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 52 | : generate_diagnostics_(false), |
| 53 | core_pattern_file_(kCorePatternFile), |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 54 | core_pipe_limit_file_(kCorePipeLimitFile), |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 55 | initialized_(false) { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void UserCollector::Initialize( |
| 59 | UserCollector::CountCrashFunction count_crash_function, |
| 60 | const std::string &our_path, |
| 61 | UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function, |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 62 | bool generate_diagnostics) { |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 63 | CrashCollector::Initialize(count_crash_function, |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 64 | is_feedback_allowed_function); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 65 | our_path_ = our_path; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 66 | initialized_ = true; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 67 | generate_diagnostics_ = generate_diagnostics; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | UserCollector::~UserCollector() { |
| 71 | } |
| 72 | |
| 73 | std::string UserCollector::GetPattern(bool enabled) const { |
| 74 | if (enabled) { |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 75 | // Combine the three crash attributes into one parameter to try to reduce |
| 76 | // the size of the invocation line for crash_reporter since the kernel |
| 77 | // has a fixed-sized (128B) buffer that it will truncate into. Note that |
| 78 | // the kernel does not support quoted arguments in core_pattern. |
| 79 | return StringPrintf("|%s --user=%%p:%%s:%%e", our_path_.c_str()); |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 80 | } else { |
| 81 | return "core"; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | bool UserCollector::SetUpInternal(bool enabled) { |
| 86 | CHECK(initialized_); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 87 | LOG(INFO) << (enabled ? "Enabling" : "Disabling") << " user crash handling"; |
| 88 | |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 89 | if (file_util::WriteFile(FilePath(core_pipe_limit_file_), |
| 90 | kCorePipeLimit, |
| 91 | strlen(kCorePipeLimit)) != |
| 92 | static_cast<int>(strlen(kCorePipeLimit))) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 93 | LOG(ERROR) << "Unable to write " << core_pipe_limit_file_; |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 94 | return false; |
| 95 | } |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 96 | std::string pattern = GetPattern(enabled); |
| 97 | if (file_util::WriteFile(FilePath(core_pattern_file_), |
| 98 | pattern.c_str(), |
| 99 | pattern.length()) != |
| 100 | static_cast<int>(pattern.length())) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 101 | LOG(ERROR) << "Unable to write " << core_pattern_file_; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 107 | FilePath UserCollector::GetProcessPath(pid_t pid) { |
| 108 | return FilePath(StringPrintf("/proc/%d", pid)); |
| 109 | } |
| 110 | |
| 111 | bool UserCollector::GetSymlinkTarget(const FilePath &symlink, |
| 112 | FilePath *target) { |
| 113 | int max_size = 32; |
| 114 | scoped_array<char> buffer; |
| 115 | while (true) { |
| 116 | buffer.reset(new char[max_size + 1]); |
| 117 | ssize_t size = readlink(symlink.value().c_str(), buffer.get(), max_size); |
| 118 | if (size < 0) { |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 119 | int saved_errno = errno; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 120 | LOG(ERROR) << "Readlink failed on " << symlink.value() << " with " |
| 121 | << saved_errno; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | buffer[size] = 0; |
| 125 | if (size == max_size) { |
| 126 | // Avoid overflow when doubling. |
| 127 | if (max_size * 2 > max_size) { |
| 128 | max_size *= 2; |
| 129 | continue; |
| 130 | } else { |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | *target = FilePath(buffer.get()); |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | bool UserCollector::GetExecutableBaseNameFromPid(uid_t pid, |
| 142 | std::string *base_name) { |
| 143 | FilePath target; |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 144 | FilePath process_path = GetProcessPath(pid); |
| 145 | FilePath exe_path = process_path.Append("exe"); |
| 146 | if (!GetSymlinkTarget(exe_path, &target)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 147 | LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value() |
| 148 | << " DirectoryExists: " |
| 149 | << file_util::DirectoryExists(process_path); |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 150 | // Try to further diagnose exe readlink failure cause. |
| 151 | struct stat buf; |
| 152 | int stat_result = stat(exe_path.value().c_str(), &buf); |
| 153 | int saved_errno = errno; |
| 154 | if (stat_result < 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 155 | LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result |
| 156 | << " " << saved_errno; |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 157 | } else { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 158 | LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode=" |
| 159 | << buf.st_mode; |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 160 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 161 | return false; |
Ken Mixter | d49d362 | 2011-02-09 18:23:00 -0800 | [diff] [blame] | 162 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 163 | *base_name = target.BaseName().value(); |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | bool UserCollector::GetIdFromStatus(const char *prefix, |
| 168 | IdKind kind, |
| 169 | const std::string &status_contents, |
| 170 | int *id) { |
| 171 | // From fs/proc/array.c:task_state(), this file contains: |
| 172 | // \nUid:\t<uid>\t<euid>\t<suid>\t<fsuid>\n |
| 173 | std::vector<std::string> status_lines; |
Chris Masone | 3ba6c5b | 2011-05-13 16:57:09 -0700 | [diff] [blame] | 174 | base::SplitString(status_contents, '\n', &status_lines); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 175 | std::vector<std::string>::iterator line_iterator; |
| 176 | for (line_iterator = status_lines.begin(); |
| 177 | line_iterator != status_lines.end(); |
| 178 | ++line_iterator) { |
| 179 | if (line_iterator->find(prefix) == 0) |
| 180 | break; |
| 181 | } |
| 182 | if (line_iterator == status_lines.end()) { |
| 183 | return false; |
| 184 | } |
| 185 | std::string id_substring = line_iterator->substr(strlen(prefix), |
| 186 | std::string::npos); |
| 187 | std::vector<std::string> ids; |
Chris Masone | 3ba6c5b | 2011-05-13 16:57:09 -0700 | [diff] [blame] | 188 | base::SplitString(id_substring, '\t', &ids); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 189 | if (ids.size() != kIdMax || kind < 0 || kind >= kIdMax) { |
| 190 | return false; |
| 191 | } |
| 192 | const char *number = ids[kind].c_str(); |
| 193 | char *end_number = NULL; |
| 194 | *id = strtol(number, &end_number, 10); |
| 195 | if (*end_number != '\0') |
| 196 | return false; |
| 197 | return true; |
| 198 | } |
| 199 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 200 | void UserCollector::EnqueueCollectionErrorLog(pid_t pid, |
| 201 | const std::string &exec) { |
| 202 | FilePath crash_path; |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 203 | LOG(INFO) << "Writing conversion problems as separate crash report."; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 204 | if (!GetCreatedCrashDirectoryByEuid(0, &crash_path, NULL)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 205 | LOG(ERROR) << "Could not even get log directory; out of space?"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 209 | std::string error_log = chromeos::GetLog(); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 210 | FilePath diag_log_path = GetCrashPath(crash_path, dump_basename, "diaglog"); |
| 211 | if (GetLogContents(FilePath(kDefaultLogConfig), kCollectionErrorSignature, |
| 212 | diag_log_path)) { |
| 213 | // We load the contents of diag_log into memory and append it to |
| 214 | // the error log. We cannot just append to files because we need |
| 215 | // to always create new files to prevent attack. |
| 216 | std::string diag_log_contents; |
| 217 | file_util::ReadFileToString(diag_log_path, &diag_log_contents); |
| 218 | error_log.append(diag_log_contents); |
| 219 | file_util::Delete(diag_log_path, false); |
| 220 | } |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 221 | FilePath log_path = GetCrashPath(crash_path, dump_basename, "log"); |
| 222 | FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta"); |
Ken Mixter | 9b34647 | 2010-11-07 13:45:45 -0800 | [diff] [blame] | 223 | // We must use WriteNewFile instead of file_util::WriteFile as we do |
| 224 | // not want to write with root access to a symlink that an attacker |
| 225 | // might have created. |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 226 | WriteNewFile(log_path, error_log.data(), error_log.length()); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 227 | AddCrashMetaData("sig", kCollectionErrorSignature); |
| 228 | WriteCrashMetaData(meta_path, exec, log_path.value()); |
| 229 | } |
| 230 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 231 | bool UserCollector::CopyOffProcFiles(pid_t pid, |
| 232 | const FilePath &container_dir) { |
| 233 | if (!file_util::CreateDirectory(container_dir)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 234 | LOG(ERROR) << "Could not create " << container_dir.value().c_str(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | FilePath process_path = GetProcessPath(pid); |
| 238 | if (!file_util::PathExists(process_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 239 | LOG(ERROR) << "Path " << process_path.value() << " does not exist"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | static const char *proc_files[] = { |
| 243 | "auxv", |
| 244 | "cmdline", |
| 245 | "environ", |
| 246 | "maps", |
| 247 | "status" |
| 248 | }; |
| 249 | for (unsigned i = 0; i < arraysize(proc_files); ++i) { |
| 250 | if (!file_util::CopyFile(process_path.Append(proc_files[i]), |
| 251 | container_dir.Append(proc_files[i]))) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 252 | LOG(ERROR) << "Could not copy " << proc_files[i] << " file"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | } |
| 256 | return true; |
| 257 | } |
| 258 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 259 | bool UserCollector::GetCreatedCrashDirectory(pid_t pid, |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 260 | FilePath *crash_file_path, |
| 261 | bool *out_of_capacity) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 262 | FilePath process_path = GetProcessPath(pid); |
| 263 | std::string status; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 264 | if (FLAGS_directory_failure) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 265 | LOG(ERROR) << "Purposefully failing to create spool directory"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 266 | return false; |
| 267 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 268 | if (!file_util::ReadFileToString(process_path.Append("status"), |
| 269 | &status)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 270 | LOG(ERROR) << "Could not read status file"; |
| 271 | LOG(INFO) << "Path " << process_path.value() << " DirectoryExists: " |
| 272 | << file_util::DirectoryExists(process_path); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | int process_euid; |
| 276 | if (!GetIdFromStatus(kUserId, kIdEffective, status, &process_euid)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 277 | LOG(ERROR) << "Could not find euid in status file"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 278 | return false; |
| 279 | } |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 280 | if (!GetCreatedCrashDirectoryByEuid(process_euid, |
| 281 | crash_file_path, |
| 282 | out_of_capacity)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 283 | LOG(ERROR) << "Could not create crash directory"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | return true; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | bool UserCollector::CopyStdinToCoreFile(const FilePath &core_path) { |
| 290 | // Copy off all stdin to a core file. |
| 291 | FilePath stdin_path("/dev/fd/0"); |
| 292 | if (file_util::CopyFile(stdin_path, core_path)) { |
| 293 | return true; |
| 294 | } |
| 295 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 296 | LOG(ERROR) << "Could not write core file"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 297 | // If the file system was full, make sure we remove any remnants. |
| 298 | file_util::Delete(core_path, false); |
| 299 | return false; |
| 300 | } |
| 301 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 302 | bool UserCollector::RunCoreToMinidump(const FilePath &core_path, |
| 303 | const FilePath &procfs_directory, |
| 304 | const FilePath &minidump_path, |
| 305 | const FilePath &temp_directory) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 306 | FilePath output_path = temp_directory.Append("output"); |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 307 | chromeos::ProcessImpl core2md; |
| 308 | core2md.RedirectOutput(output_path.value()); |
| 309 | core2md.AddArg(kCoreToMinidumpConverterPath); |
| 310 | core2md.AddArg(core_path.value()); |
| 311 | core2md.AddArg(procfs_directory.value()); |
Ken Mixter | 2953c3a | 2010-10-18 14:42:20 -0700 | [diff] [blame] | 312 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 313 | if (!FLAGS_core2md_failure) { |
| 314 | core2md.AddArg(minidump_path.value()); |
| 315 | } else { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 316 | // To test how core2md errors are propagaged, cause an error |
| 317 | // by forgetting a required argument. |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 320 | int errorlevel = core2md.Run(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 321 | |
| 322 | std::string output; |
| 323 | file_util::ReadFileToString(output_path, &output); |
| 324 | if (errorlevel != 0) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 325 | LOG(ERROR) << "Problem during " << kCoreToMinidumpConverterPath |
| 326 | << " [result=" << errorlevel << "]: " << output; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 327 | return false; |
| 328 | } |
| 329 | |
| 330 | if (!file_util::PathExists(minidump_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 331 | LOG(ERROR) << "Minidump file " << minidump_path.value() |
| 332 | << " was not created"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 333 | return false; |
| 334 | } |
| 335 | return true; |
| 336 | } |
| 337 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 338 | bool UserCollector::ConvertCoreToMinidump(pid_t pid, |
| 339 | const FilePath &container_dir, |
| 340 | const FilePath &core_path, |
| 341 | const FilePath &minidump_path) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 342 | if (!CopyOffProcFiles(pid, container_dir)) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 346 | if (!CopyStdinToCoreFile(core_path)) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 347 | return false; |
| 348 | } |
| 349 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 350 | bool conversion_result = RunCoreToMinidump( |
| 351 | core_path, |
| 352 | container_dir, // procfs directory |
| 353 | minidump_path, |
| 354 | container_dir); // temporary directory |
Ken Mixter | ee849c5 | 2010-09-30 15:30:10 -0700 | [diff] [blame] | 355 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 356 | if (conversion_result) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 357 | LOG(INFO) << "Stored minidump to " << minidump_path.value(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 360 | return conversion_result; |
| 361 | } |
| 362 | |
| 363 | bool UserCollector::ConvertAndEnqueueCrash(int pid, |
| 364 | const std::string &exec, |
| 365 | bool *out_of_capacity) { |
| 366 | FilePath crash_path; |
| 367 | if (!GetCreatedCrashDirectory(pid, &crash_path, out_of_capacity)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 368 | LOG(ERROR) << "Unable to find/create process-specific crash path"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | |
| 372 | // Directory like /tmp/crash_reporter.1234 which contains the |
| 373 | // procfs entries and other temporary files used during conversion. |
| 374 | FilePath container_dir = FilePath("/tmp").Append( |
| 375 | StringPrintf("crash_reporter.%d", pid)); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 376 | // Delete a pre-existing directory from crash reporter that may have |
| 377 | // been left around for diagnostics from a failed conversion attempt. |
| 378 | // If we don't, existing files can cause forking to fail. |
| 379 | file_util::Delete(container_dir, true); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 380 | std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid); |
| 381 | FilePath core_path = GetCrashPath(crash_path, dump_basename, "core"); |
| 382 | FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta"); |
| 383 | FilePath minidump_path = GetCrashPath(crash_path, dump_basename, "dmp"); |
Ken Mixter | c49dbd4 | 2010-12-14 17:44:11 -0800 | [diff] [blame] | 384 | FilePath log_path = GetCrashPath(crash_path, dump_basename, "log"); |
| 385 | |
| 386 | if (GetLogContents(FilePath(kDefaultLogConfig), exec, log_path)) |
| 387 | AddCrashMetaData("log", log_path.value()); |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 388 | |
| 389 | if (!ConvertCoreToMinidump(pid, container_dir, core_path, |
| 390 | minidump_path)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 391 | LOG(INFO) << "Leaving core file at " << core_path.value() |
| 392 | << " due to conversion error"; |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | |
| 396 | // Here we commit to sending this file. We must not return false |
| 397 | // after this point or we will generate a log report as well as a |
| 398 | // crash report. |
| 399 | WriteCrashMetaData(meta_path, |
| 400 | exec, |
| 401 | minidump_path.value()); |
| 402 | |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame^] | 403 | if (!IsDeveloperImage()) { |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 404 | file_util::Delete(core_path, false); |
| 405 | } else { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 406 | LOG(INFO) << "Leaving core file at " << core_path.value() |
| 407 | << " due to developer image"; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 410 | file_util::Delete(container_dir, true); |
| 411 | return true; |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 414 | bool UserCollector::ParseCrashAttributes(const std::string &crash_attributes, |
| 415 | pid_t *pid, int *signal, |
| 416 | std::string *kernel_supplied_name) { |
| 417 | pcrecpp::RE re("(\\d+):(\\d+):(.*)"); |
| 418 | return re.FullMatch(crash_attributes, pid, signal, kernel_supplied_name); |
| 419 | } |
| 420 | |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame^] | 421 | bool UserCollector::IsDeveloperImage() { |
| 422 | // If we're testing crash reporter itself, we don't want to special-case |
| 423 | // for developer images. |
| 424 | if (IsCrashTestInProgress()) |
| 425 | return false; |
| 426 | return file_util::PathExists(FilePath(kLeaveCoreFile)); |
| 427 | } |
| 428 | |
| 429 | bool UserCollector::ShouldIgnoreChromeCrashes() { |
| 430 | // If we're testing crash reporter itself, we don't want to allow an |
| 431 | // override for chrome crashes. And, let's be conservative and only |
| 432 | // allow an override for developer images. |
| 433 | if (!IsCrashTestInProgress() && IsDeveloperImage()) { |
| 434 | // Check if there's an override to indicate we should indeed collect |
| 435 | // chrome crashes. This allows the crashes to still be tracked when |
| 436 | // they occur in autotests. See "crosbug.com/17987". |
| 437 | if (file_util::PathExists(FilePath(kCollectChromeFile))) |
| 438 | return false; |
| 439 | } |
| 440 | // We default to ignoring chrome crashes. |
| 441 | return true; |
| 442 | } |
| 443 | |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 444 | bool UserCollector::ShouldDump(bool has_owner_consent, |
| 445 | bool is_developer, |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame^] | 446 | bool ignore_chrome_crashes, |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 447 | const std::string &exec, |
| 448 | std::string *reason) { |
| 449 | reason->clear(); |
| 450 | |
| 451 | // Treat Chrome crashes as if the user opted-out. We stop counting Chrome |
| 452 | // crashes towards user crashes, so user crashes really mean non-Chrome |
| 453 | // user-space crashes. |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame^] | 454 | if ((exec == "chrome" || exec == "supplied_chrome") && |
| 455 | ignore_chrome_crashes) { |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 456 | *reason = "ignoring - chrome crash"; |
| 457 | return false; |
| 458 | } |
| 459 | |
| 460 | // For developer builds, we always want to keep the crash reports unless |
| 461 | // we're testing the crash facilities themselves. This overrides |
| 462 | // feedback. Crash sending still obeys consent. |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame^] | 463 | if (is_developer) { |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 464 | *reason = "developer build - not testing - always dumping"; |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | if (!has_owner_consent) { |
| 469 | *reason = "ignoring - no consent"; |
| 470 | return false; |
| 471 | } |
| 472 | |
| 473 | *reason = "handling"; |
| 474 | return true; |
| 475 | } |
| 476 | |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 477 | bool UserCollector::HandleCrash(const std::string &crash_attributes, |
| 478 | const char *force_exec) { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 479 | CHECK(initialized_); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 480 | int pid = 0; |
| 481 | int signal = 0; |
| 482 | std::string kernel_supplied_name; |
| 483 | |
| 484 | if (!ParseCrashAttributes(crash_attributes, &pid, &signal, |
| 485 | &kernel_supplied_name)) { |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 486 | LOG(ERROR) << "Invalid parameter: --user=" << crash_attributes; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 490 | std::string exec; |
| 491 | if (force_exec) { |
| 492 | exec.assign(force_exec); |
| 493 | } else if (!GetExecutableBaseNameFromPid(pid, &exec)) { |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 494 | // If we cannot find the exec name, use the kernel supplied name. |
| 495 | // We don't always use the kernel's since it truncates the name to |
| 496 | // 16 characters. |
| 497 | exec = StringPrintf("supplied_%s", kernel_supplied_name.c_str()); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 498 | } |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 499 | |
| 500 | // Allow us to test the crash reporting mechanism successfully even if |
| 501 | // other parts of the system crash. |
| 502 | if (!FLAGS_filter_in.empty() && |
| 503 | (FLAGS_filter_in == "none" || |
| 504 | FLAGS_filter_in != exec)) { |
| 505 | // We use a different format message to make it more obvious in tests |
| 506 | // which crashes are test generated and which are real. |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 507 | LOG(WARNING) << "Ignoring crash from " << exec << "[" << pid << "] while " |
| 508 | << "filter_in=" << FLAGS_filter_in << "."; |
Ken Mixter | c6a58e0 | 2010-11-01 18:05:30 -0700 | [diff] [blame] | 509 | return true; |
| 510 | } |
| 511 | |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 512 | std::string reason; |
| 513 | bool dump = ShouldDump(is_feedback_allowed_function_(), |
Michael Krebs | 538ecbf | 2011-07-27 14:13:22 -0700 | [diff] [blame^] | 514 | IsDeveloperImage(), |
| 515 | ShouldIgnoreChromeCrashes(), |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 516 | exec, |
| 517 | &reason); |
Ken Mixter | 2105b49 | 2010-11-09 16:14:38 -0800 | [diff] [blame] | 518 | |
Ken Mixter | a324932 | 2011-03-03 08:47:38 -0800 | [diff] [blame] | 519 | LOG(WARNING) << "Received crash notification for " << exec << "[" << pid |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 520 | << "] sig " << signal << " (" << reason << ")"; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 521 | |
Ken Mixter | 5d3a1a2 | 2011-03-16 12:47:20 -0700 | [diff] [blame] | 522 | if (dump) { |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 523 | count_crash_function_(); |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 524 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 525 | if (generate_diagnostics_) { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 526 | bool out_of_capacity = false; |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 527 | bool convert_and_enqueue_result = |
| 528 | ConvertAndEnqueueCrash(pid, exec, &out_of_capacity); |
Ken Mixter | 1b8fe01 | 2011-01-25 13:33:05 -0800 | [diff] [blame] | 529 | if (!convert_and_enqueue_result) { |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 530 | if (!out_of_capacity) |
| 531 | EnqueueCollectionErrorLog(pid, exec); |
| 532 | return false; |
| 533 | } |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 534 | } |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 535 | } |
Ken Mixter | 207694d | 2010-10-28 15:42:37 -0700 | [diff] [blame] | 536 | |
Ken Mixter | 777484c | 2010-07-23 16:22:44 -0700 | [diff] [blame] | 537 | return true; |
Chris Sosa | e4a8603 | 2010-06-16 17:08:34 -0700 | [diff] [blame] | 538 | } |