blob: 01af4eb0aac688962ea65eac846913edc58b90f6 [file] [log] [blame]
Chris Sosae4a86032010-06-16 17:08:34 -07001// 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 Mixter03403162010-08-18 15:23:16 -07005#include "crash-reporter/user_collector.h"
6
Ken Mixter777484c2010-07-23 16:22:44 -07007#include <grp.h> // For struct group.
Ken Mixter1b8fe012011-01-25 13:33:05 -08008#include <pcrecpp.h>
9#include <pcrecpp.h>
Ken Mixter777484c2010-07-23 16:22:44 -070010#include <pwd.h> // For struct passwd.
Ken Mixter2953c3a2010-10-18 14:42:20 -070011#include <sys/types.h> // For getpwuid_r, getgrnam_r, WEXITSTATUS.
Ken Mixter777484c2010-07-23 16:22:44 -070012
Chris Sosae4a86032010-06-16 17:08:34 -070013#include <string>
Ken Mixter2953c3a2010-10-18 14:42:20 -070014#include <vector>
Chris Sosae4a86032010-06-16 17:08:34 -070015
16#include "base/file_util.h"
17#include "base/logging.h"
Chris Masone8a68c7c2011-05-14 11:44:04 -070018#include "base/string_split.h"
Chris Sosae4a86032010-06-16 17:08:34 -070019#include "base/string_util.h"
Ken Mixtera3249322011-03-03 08:47:38 -080020#include "chromeos/process.h"
21#include "chromeos/syslog_logging.h"
Ken Mixter207694d2010-10-28 15:42:37 -070022#include "gflags/gflags.h"
Chris Sosae4a86032010-06-16 17:08:34 -070023
Ken Mixterc6a58e02010-11-01 18:05:30 -070024#pragma GCC diagnostic ignored "-Wstrict-aliasing"
Ken Mixter1b8fe012011-01-25 13:33:05 -080025DEFINE_bool(core2md_failure, false, "Core2md failure test");
26DEFINE_bool(directory_failure, false, "Spool directory failure test");
Ken Mixterc6a58e02010-11-01 18:05:30 -070027DEFINE_string(filter_in, "",
28 "Ignore all crashes but this for testing");
29#pragma GCC diagnostic error "-Wstrict-aliasing"
Ken Mixter207694d2010-10-28 15:42:37 -070030
31static const char kCollectionErrorSignature[] =
32 "crash_reporter-user-collection";
Chris Sosae4a86032010-06-16 17:08:34 -070033// 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.
36static const char kCorePatternFile[] = "/proc/sys/kernel/core_pattern";
Ken Mixterc49dbd42010-12-14 17:44:11 -080037static 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.
40static const char kCorePipeLimit[] = "4";
Ken Mixter777484c2010-07-23 16:22:44 -070041static const char kCoreToMinidumpConverterPath[] = "/usr/bin/core2md";
Ken Mixter777484c2010-07-23 16:22:44 -070042
Ken Mixterc49dbd42010-12-14 17:44:11 -080043static const char kDefaultLogConfig[] = "/etc/crash_reporter_logs.conf";
44
Ken Mixter777484c2010-07-23 16:22:44 -070045const char *UserCollector::kUserId = "Uid:\t";
46const char *UserCollector::kGroupId = "Gid:\t";
Chris Sosae4a86032010-06-16 17:08:34 -070047
48UserCollector::UserCollector()
Ken Mixter777484c2010-07-23 16:22:44 -070049 : generate_diagnostics_(false),
50 core_pattern_file_(kCorePatternFile),
Ken Mixterc49dbd42010-12-14 17:44:11 -080051 core_pipe_limit_file_(kCorePipeLimitFile),
Ken Mixter03403162010-08-18 15:23:16 -070052 initialized_(false) {
Chris Sosae4a86032010-06-16 17:08:34 -070053}
54
55void UserCollector::Initialize(
56 UserCollector::CountCrashFunction count_crash_function,
57 const std::string &our_path,
58 UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function,
Ken Mixter777484c2010-07-23 16:22:44 -070059 bool generate_diagnostics) {
Ken Mixter03403162010-08-18 15:23:16 -070060 CrashCollector::Initialize(count_crash_function,
Ken Mixtera3249322011-03-03 08:47:38 -080061 is_feedback_allowed_function);
Chris Sosae4a86032010-06-16 17:08:34 -070062 our_path_ = our_path;
Chris Sosae4a86032010-06-16 17:08:34 -070063 initialized_ = true;
Ken Mixter777484c2010-07-23 16:22:44 -070064 generate_diagnostics_ = generate_diagnostics;
Chris Sosae4a86032010-06-16 17:08:34 -070065}
66
67UserCollector::~UserCollector() {
68}
69
70std::string UserCollector::GetPattern(bool enabled) const {
71 if (enabled) {
Ken Mixter1b8fe012011-01-25 13:33:05 -080072 // 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 Sosae4a86032010-06-16 17:08:34 -070077 } else {
78 return "core";
79 }
80}
81
82bool UserCollector::SetUpInternal(bool enabled) {
83 CHECK(initialized_);
Ken Mixtera3249322011-03-03 08:47:38 -080084 LOG(INFO) << (enabled ? "Enabling" : "Disabling") << " user crash handling";
85
Ken Mixterc49dbd42010-12-14 17:44:11 -080086 if (file_util::WriteFile(FilePath(core_pipe_limit_file_),
87 kCorePipeLimit,
88 strlen(kCorePipeLimit)) !=
89 static_cast<int>(strlen(kCorePipeLimit))) {
Ken Mixtera3249322011-03-03 08:47:38 -080090 LOG(ERROR) << "Unable to write " << core_pipe_limit_file_;
Ken Mixterc49dbd42010-12-14 17:44:11 -080091 return false;
92 }
Chris Sosae4a86032010-06-16 17:08:34 -070093 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 Mixtera3249322011-03-03 08:47:38 -080098 LOG(ERROR) << "Unable to write " << core_pattern_file_;
Chris Sosae4a86032010-06-16 17:08:34 -070099 return false;
100 }
101 return true;
102}
103
Ken Mixter777484c2010-07-23 16:22:44 -0700104FilePath UserCollector::GetProcessPath(pid_t pid) {
105 return FilePath(StringPrintf("/proc/%d", pid));
106}
107
108bool 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 Mixterd49d3622011-02-09 18:23:00 -0800116 int saved_errno = errno;
Ken Mixtera3249322011-03-03 08:47:38 -0800117 LOG(ERROR) << "Readlink failed on " << symlink.value() << " with "
118 << saved_errno;
Ken Mixter777484c2010-07-23 16:22:44 -0700119 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
138bool UserCollector::GetExecutableBaseNameFromPid(uid_t pid,
139 std::string *base_name) {
140 FilePath target;
Ken Mixterd49d3622011-02-09 18:23:00 -0800141 FilePath process_path = GetProcessPath(pid);
142 FilePath exe_path = process_path.Append("exe");
143 if (!GetSymlinkTarget(exe_path, &target)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800144 LOG(INFO) << "GetSymlinkTarget failed - Path " << process_path.value()
145 << " DirectoryExists: "
146 << file_util::DirectoryExists(process_path);
Ken Mixterd49d3622011-02-09 18:23:00 -0800147 // 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 Mixtera3249322011-03-03 08:47:38 -0800152 LOG(INFO) << "stat " << exe_path.value() << " failed: " << stat_result
153 << " " << saved_errno;
Ken Mixterd49d3622011-02-09 18:23:00 -0800154 } else {
Ken Mixtera3249322011-03-03 08:47:38 -0800155 LOG(INFO) << "stat " << exe_path.value() << " succeeded: st_mode="
156 << buf.st_mode;
Ken Mixterd49d3622011-02-09 18:23:00 -0800157 }
Ken Mixter777484c2010-07-23 16:22:44 -0700158 return false;
Ken Mixterd49d3622011-02-09 18:23:00 -0800159 }
Ken Mixter777484c2010-07-23 16:22:44 -0700160 *base_name = target.BaseName().value();
161 return true;
162}
163
164bool 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 Masone3ba6c5b2011-05-13 16:57:09 -0700171 base::SplitString(status_contents, '\n', &status_lines);
Ken Mixter777484c2010-07-23 16:22:44 -0700172 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 Masone3ba6c5b2011-05-13 16:57:09 -0700185 base::SplitString(id_substring, '\t', &ids);
Ken Mixter777484c2010-07-23 16:22:44 -0700186 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 Mixter207694d2010-10-28 15:42:37 -0700197void UserCollector::EnqueueCollectionErrorLog(pid_t pid,
198 const std::string &exec) {
199 FilePath crash_path;
Ken Mixtera3249322011-03-03 08:47:38 -0800200 LOG(INFO) << "Writing conversion problems as separate crash report.";
Ken Mixter207694d2010-10-28 15:42:37 -0700201 if (!GetCreatedCrashDirectoryByEuid(0, &crash_path, NULL)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800202 LOG(ERROR) << "Could not even get log directory; out of space?";
Ken Mixter207694d2010-10-28 15:42:37 -0700203 return;
204 }
205 std::string dump_basename = FormatDumpBasename(exec, time(NULL), pid);
Ken Mixtera3249322011-03-03 08:47:38 -0800206 std::string error_log = chromeos::GetLog();
Ken Mixter1b8fe012011-01-25 13:33:05 -0800207 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 Mixter207694d2010-10-28 15:42:37 -0700218 FilePath log_path = GetCrashPath(crash_path, dump_basename, "log");
219 FilePath meta_path = GetCrashPath(crash_path, dump_basename, "meta");
Ken Mixter9b346472010-11-07 13:45:45 -0800220 // 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 Mixter1b8fe012011-01-25 13:33:05 -0800223 WriteNewFile(log_path, error_log.data(), error_log.length());
Ken Mixter207694d2010-10-28 15:42:37 -0700224 AddCrashMetaData("sig", kCollectionErrorSignature);
225 WriteCrashMetaData(meta_path, exec, log_path.value());
226}
227
Ken Mixter777484c2010-07-23 16:22:44 -0700228bool UserCollector::CopyOffProcFiles(pid_t pid,
229 const FilePath &container_dir) {
230 if (!file_util::CreateDirectory(container_dir)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800231 LOG(ERROR) << "Could not create " << container_dir.value().c_str();
Ken Mixter777484c2010-07-23 16:22:44 -0700232 return false;
233 }
234 FilePath process_path = GetProcessPath(pid);
235 if (!file_util::PathExists(process_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800236 LOG(ERROR) << "Path " << process_path.value() << " does not exist";
Ken Mixter777484c2010-07-23 16:22:44 -0700237 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 Mixtera3249322011-03-03 08:47:38 -0800249 LOG(ERROR) << "Could not copy " << proc_files[i] << " file";
Ken Mixter777484c2010-07-23 16:22:44 -0700250 return false;
251 }
252 }
253 return true;
254}
255
Ken Mixter777484c2010-07-23 16:22:44 -0700256bool UserCollector::GetCreatedCrashDirectory(pid_t pid,
Ken Mixter207694d2010-10-28 15:42:37 -0700257 FilePath *crash_file_path,
258 bool *out_of_capacity) {
Ken Mixter777484c2010-07-23 16:22:44 -0700259 FilePath process_path = GetProcessPath(pid);
260 std::string status;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800261 if (FLAGS_directory_failure) {
Ken Mixtera3249322011-03-03 08:47:38 -0800262 LOG(ERROR) << "Purposefully failing to create spool directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700263 return false;
264 }
Ken Mixter777484c2010-07-23 16:22:44 -0700265 if (!file_util::ReadFileToString(process_path.Append("status"),
266 &status)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800267 LOG(ERROR) << "Could not read status file";
268 LOG(INFO) << "Path " << process_path.value() << " DirectoryExists: "
269 << file_util::DirectoryExists(process_path);
Ken Mixter777484c2010-07-23 16:22:44 -0700270 return false;
271 }
272 int process_euid;
273 if (!GetIdFromStatus(kUserId, kIdEffective, status, &process_euid)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800274 LOG(ERROR) << "Could not find euid in status file";
Ken Mixter777484c2010-07-23 16:22:44 -0700275 return false;
276 }
Ken Mixter207694d2010-10-28 15:42:37 -0700277 if (!GetCreatedCrashDirectoryByEuid(process_euid,
278 crash_file_path,
279 out_of_capacity)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800280 LOG(ERROR) << "Could not create crash directory";
Ken Mixter207694d2010-10-28 15:42:37 -0700281 return false;
282 }
283 return true;
Ken Mixter777484c2010-07-23 16:22:44 -0700284}
285
286bool 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 Mixtera3249322011-03-03 08:47:38 -0800293 LOG(ERROR) << "Could not write core file";
Ken Mixter777484c2010-07-23 16:22:44 -0700294 // 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 Mixter207694d2010-10-28 15:42:37 -0700299bool UserCollector::RunCoreToMinidump(const FilePath &core_path,
300 const FilePath &procfs_directory,
301 const FilePath &minidump_path,
302 const FilePath &temp_directory) {
Ken Mixter777484c2010-07-23 16:22:44 -0700303 FilePath output_path = temp_directory.Append("output");
Ken Mixtera3249322011-03-03 08:47:38 -0800304 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 Mixter2953c3a2010-10-18 14:42:20 -0700309
Ken Mixtera3249322011-03-03 08:47:38 -0800310 if (!FLAGS_core2md_failure) {
311 core2md.AddArg(minidump_path.value());
312 } else {
Ken Mixter207694d2010-10-28 15:42:37 -0700313 // To test how core2md errors are propagaged, cause an error
314 // by forgetting a required argument.
Ken Mixter207694d2010-10-28 15:42:37 -0700315 }
316
Ken Mixtera3249322011-03-03 08:47:38 -0800317 int errorlevel = core2md.Run();
Ken Mixter777484c2010-07-23 16:22:44 -0700318
319 std::string output;
320 file_util::ReadFileToString(output_path, &output);
321 if (errorlevel != 0) {
Ken Mixtera3249322011-03-03 08:47:38 -0800322 LOG(ERROR) << "Problem during " << kCoreToMinidumpConverterPath
323 << " [result=" << errorlevel << "]: " << output;
Ken Mixter777484c2010-07-23 16:22:44 -0700324 return false;
325 }
326
327 if (!file_util::PathExists(minidump_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800328 LOG(ERROR) << "Minidump file " << minidump_path.value()
329 << " was not created";
Ken Mixter777484c2010-07-23 16:22:44 -0700330 return false;
331 }
332 return true;
333}
334
Ken Mixter207694d2010-10-28 15:42:37 -0700335bool UserCollector::ConvertCoreToMinidump(pid_t pid,
336 const FilePath &container_dir,
337 const FilePath &core_path,
338 const FilePath &minidump_path) {
Ken Mixter777484c2010-07-23 16:22:44 -0700339 if (!CopyOffProcFiles(pid, container_dir)) {
Ken Mixter777484c2010-07-23 16:22:44 -0700340 return false;
341 }
342
Ken Mixter777484c2010-07-23 16:22:44 -0700343 if (!CopyStdinToCoreFile(core_path)) {
Ken Mixter777484c2010-07-23 16:22:44 -0700344 return false;
345 }
346
Ken Mixter207694d2010-10-28 15:42:37 -0700347 bool conversion_result = RunCoreToMinidump(
348 core_path,
349 container_dir, // procfs directory
350 minidump_path,
351 container_dir); // temporary directory
Ken Mixteree849c52010-09-30 15:30:10 -0700352
Ken Mixter777484c2010-07-23 16:22:44 -0700353 if (conversion_result) {
Ken Mixtera3249322011-03-03 08:47:38 -0800354 LOG(INFO) << "Stored minidump to " << minidump_path.value();
Ken Mixter777484c2010-07-23 16:22:44 -0700355 }
356
Ken Mixter207694d2010-10-28 15:42:37 -0700357 return conversion_result;
358}
359
360bool 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 Mixtera3249322011-03-03 08:47:38 -0800365 LOG(ERROR) << "Unable to find/create process-specific crash path";
Ken Mixter207694d2010-10-28 15:42:37 -0700366 return false;
367 }
368
Ben Chan294d5d12012-01-04 20:40:15 -0800369 // Directory like /tmp/crash_reporter/1234 which contains the
Ken Mixter207694d2010-10-28 15:42:37 -0700370 // procfs entries and other temporary files used during conversion.
Ben Chan294d5d12012-01-04 20:40:15 -0800371 FilePath container_dir(StringPrintf("/tmp/crash_reporter/%d", pid));
Ken Mixter1b8fe012011-01-25 13:33:05 -0800372 // 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 Mixter207694d2010-10-28 15:42:37 -0700376 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 Mixterc49dbd42010-12-14 17:44:11 -0800380 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 Mixter207694d2010-10-28 15:42:37 -0700384
385 if (!ConvertCoreToMinidump(pid, container_dir, core_path,
386 minidump_path)) {
Ken Mixtera3249322011-03-03 08:47:38 -0800387 LOG(INFO) << "Leaving core file at " << core_path.value()
388 << " due to conversion error";
Ken Mixter207694d2010-10-28 15:42:37 -0700389 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 Krebs538ecbf2011-07-27 14:13:22 -0700399 if (!IsDeveloperImage()) {
Ken Mixter777484c2010-07-23 16:22:44 -0700400 file_util::Delete(core_path, false);
401 } else {
Ken Mixtera3249322011-03-03 08:47:38 -0800402 LOG(INFO) << "Leaving core file at " << core_path.value()
403 << " due to developer image";
Ken Mixter777484c2010-07-23 16:22:44 -0700404 }
405
Ken Mixter207694d2010-10-28 15:42:37 -0700406 file_util::Delete(container_dir, true);
407 return true;
Ken Mixter777484c2010-07-23 16:22:44 -0700408}
409
Ken Mixter1b8fe012011-01-25 13:33:05 -0800410bool 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 Mixter5d3a1a22011-03-16 12:47:20 -0700417bool UserCollector::ShouldDump(bool has_owner_consent,
418 bool is_developer,
Michael Krebs4fe30db2011-08-05 13:54:52 -0700419 bool handle_chrome_crashes,
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700420 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 Krebs538ecbf2011-07-27 14:13:22 -0700427 if ((exec == "chrome" || exec == "supplied_chrome") &&
Michael Krebs4fe30db2011-08-05 13:54:52 -0700428 !handle_chrome_crashes) {
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700429 *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 Krebs538ecbf2011-07-27 14:13:22 -0700436 if (is_developer) {
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700437 *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 Mixter1b8fe012011-01-25 13:33:05 -0800450bool UserCollector::HandleCrash(const std::string &crash_attributes,
451 const char *force_exec) {
Chris Sosae4a86032010-06-16 17:08:34 -0700452 CHECK(initialized_);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800453 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 Mixtera3249322011-03-03 08:47:38 -0800459 LOG(ERROR) << "Invalid parameter: --user=" << crash_attributes;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800460 return false;
461 }
462
Ken Mixter777484c2010-07-23 16:22:44 -0700463 std::string exec;
464 if (force_exec) {
465 exec.assign(force_exec);
466 } else if (!GetExecutableBaseNameFromPid(pid, &exec)) {
Ken Mixter1b8fe012011-01-25 13:33:05 -0800467 // 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 Mixter777484c2010-07-23 16:22:44 -0700471 }
Ken Mixterc6a58e02010-11-01 18:05:30 -0700472
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 Mixtera3249322011-03-03 08:47:38 -0800480 LOG(WARNING) << "Ignoring crash from " << exec << "[" << pid << "] while "
481 << "filter_in=" << FLAGS_filter_in << ".";
Ken Mixterc6a58e02010-11-01 18:05:30 -0700482 return true;
483 }
484
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700485 std::string reason;
486 bool dump = ShouldDump(is_feedback_allowed_function_(),
Michael Krebs538ecbf2011-07-27 14:13:22 -0700487 IsDeveloperImage(),
Michael Krebs4fe30db2011-08-05 13:54:52 -0700488 ShouldHandleChromeCrashes(),
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700489 exec,
490 &reason);
Ken Mixter2105b492010-11-09 16:14:38 -0800491
Ken Mixtera3249322011-03-03 08:47:38 -0800492 LOG(WARNING) << "Received crash notification for " << exec << "[" << pid
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700493 << "] sig " << signal << " (" << reason << ")";
Chris Sosae4a86032010-06-16 17:08:34 -0700494
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700495 if (dump) {
Chris Sosae4a86032010-06-16 17:08:34 -0700496 count_crash_function_();
Ken Mixter777484c2010-07-23 16:22:44 -0700497
Ken Mixter03403162010-08-18 15:23:16 -0700498 if (generate_diagnostics_) {
Ken Mixter207694d2010-10-28 15:42:37 -0700499 bool out_of_capacity = false;
Ken Mixter1b8fe012011-01-25 13:33:05 -0800500 bool convert_and_enqueue_result =
501 ConvertAndEnqueueCrash(pid, exec, &out_of_capacity);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800502 if (!convert_and_enqueue_result) {
Ken Mixter207694d2010-10-28 15:42:37 -0700503 if (!out_of_capacity)
504 EnqueueCollectionErrorLog(pid, exec);
505 return false;
506 }
Ken Mixter03403162010-08-18 15:23:16 -0700507 }
Ken Mixter777484c2010-07-23 16:22:44 -0700508 }
Ken Mixter207694d2010-10-28 15:42:37 -0700509
Ken Mixter777484c2010-07-23 16:22:44 -0700510 return true;
Chris Sosae4a86032010-06-16 17:08:34 -0700511}