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