blob: fe2c00b74c84c93400bd1663fe0673cd2745780e [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
Ben Chan7e776902014-06-18 13:19:51 -07005#ifndef CRASH_REPORTER_USER_COLLECTOR_H_
6#define CRASH_REPORTER_USER_COLLECTOR_H_
Chris Sosae4a86032010-06-16 17:08:34 -07007
8#include <string>
Ken Mixter2953c3a2010-10-18 14:42:20 -07009#include <vector>
Chris Sosae4a86032010-06-16 17:08:34 -070010
Ben Chan7e776902014-06-18 13:19:51 -070011#include <base/files/file_path.h>
Ben Chan895fa5d2014-09-02 20:58:20 -070012#include <base/macros.h>
Ben Chan7e776902014-06-18 13:19:51 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
14
Ken Mixter03403162010-08-18 15:23:16 -070015#include "crash-reporter/crash_collector.h"
Chris Sosae4a86032010-06-16 17:08:34 -070016
Ken Mixter03403162010-08-18 15:23:16 -070017class SystemLogging;
Chris Sosae4a86032010-06-16 17:08:34 -070018
19// User crash collector.
Ken Mixter03403162010-08-18 15:23:16 -070020class UserCollector : public CrashCollector {
Chris Sosae4a86032010-06-16 17:08:34 -070021 public:
Chris Sosae4a86032010-06-16 17:08:34 -070022 UserCollector();
23
24 // Initialize the user crash collector for detection of crashes,
25 // given a crash counting function, the path to this executable,
26 // metrics collection enabled oracle, and system logger facility.
Ken Mixter777484c2010-07-23 16:22:44 -070027 // Crash detection/reporting is not enabled until Enable is called.
28 // |generate_diagnostics| is used to indicate whether or not to try
29 // to generate a minidump from crashes.
Chris Sosae4a86032010-06-16 17:08:34 -070030 void Initialize(CountCrashFunction count_crash,
31 const std::string &our_path,
32 IsFeedbackAllowedFunction is_metrics_allowed,
Ken Mixter777484c2010-07-23 16:22:44 -070033 bool generate_diagnostics);
Chris Sosae4a86032010-06-16 17:08:34 -070034
Ben Chanefec0b32014-08-12 08:52:11 -070035 ~UserCollector() override;
Chris Sosae4a86032010-06-16 17:08:34 -070036
37 // Enable collection.
38 bool Enable() { return SetUpInternal(true); }
39
40 // Disable collection.
41 bool Disable() { return SetUpInternal(false); }
42
Ken Mixter777484c2010-07-23 16:22:44 -070043 // Handle a specific user crash. Returns true on success.
Ken Mixter1b8fe012011-01-25 13:33:05 -080044 bool HandleCrash(const std::string &crash_attributes,
45 const char *force_exec);
Chris Sosae4a86032010-06-16 17:08:34 -070046
47 // Set (override the default) core file pattern.
48 void set_core_pattern_file(const std::string &pattern) {
49 core_pattern_file_ = pattern;
50 }
51
Ken Mixterc49dbd42010-12-14 17:44:11 -080052 // Set (override the default) core pipe limit file.
53 void set_core_pipe_limit_file(const std::string &path) {
54 core_pipe_limit_file_ = path;
55 }
56
Chris Sosae4a86032010-06-16 17:08:34 -070057 private:
58 friend class UserCollectorTest;
Ken Mixter777484c2010-07-23 16:22:44 -070059 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPath);
60 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPid);
61 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesOK);
Ken Mixterd49d3622011-02-09 18:23:00 -080062 FRIEND_TEST(UserCollectorTest, GetExecutableBaseNameFromPid);
Ben Chanf13bb582012-01-06 08:22:07 -080063 FRIEND_TEST(UserCollectorTest, GetFirstLineWithPrefix);
Ken Mixter777484c2010-07-23 16:22:44 -070064 FRIEND_TEST(UserCollectorTest, GetIdFromStatus);
Ben Chanf13bb582012-01-06 08:22:07 -080065 FRIEND_TEST(UserCollectorTest, GetStateFromStatus);
Ken Mixter777484c2010-07-23 16:22:44 -070066 FRIEND_TEST(UserCollectorTest, GetProcessPath);
67 FRIEND_TEST(UserCollectorTest, GetSymlinkTarget);
68 FRIEND_TEST(UserCollectorTest, GetUserInfoFromName);
Ken Mixter1b8fe012011-01-25 13:33:05 -080069 FRIEND_TEST(UserCollectorTest, ParseCrashAttributes);
Ken Mixter5d3a1a22011-03-16 12:47:20 -070070 FRIEND_TEST(UserCollectorTest, ShouldDumpChromeOverridesDeveloperImage);
71 FRIEND_TEST(UserCollectorTest, ShouldDumpDeveloperImageOverridesConsent);
72 FRIEND_TEST(UserCollectorTest, ShouldDumpUseConsentProductionImage);
Ben Chanf13bb582012-01-06 08:22:07 -080073 FRIEND_TEST(UserCollectorTest, ValidateProcFiles);
Ben Chan6e709a12012-02-29 12:10:44 -080074 FRIEND_TEST(UserCollectorTest, ValidateCoreFile);
Ken Mixter777484c2010-07-23 16:22:44 -070075
76 // Enumeration to pass to GetIdFromStatus. Must match the order
77 // that the kernel lists IDs in the status file.
78 enum IdKind {
79 kIdReal = 0, // uid and gid
80 kIdEffective = 1, // euid and egid
81 kIdSet = 2, // suid and sgid
82 kIdFileSystem = 3, // fsuid and fsgid
83 kIdMax
84 };
Chris Sosae4a86032010-06-16 17:08:34 -070085
Ben Chan6e709a12012-02-29 12:10:44 -080086 enum ErrorType {
87 kErrorNone,
88 kErrorSystemIssue,
89 kErrorReadCoreData,
90 kErrorUnusableProcFiles,
91 kErrorInvalidCoreFile,
92 kErrorUnsupported32BitCoreFile,
93 kErrorCore2MinidumpConversion,
94 };
95
Ken Mixter2953c3a2010-10-18 14:42:20 -070096 static const int kForkProblem = 255;
97
Ben Chan6e709a12012-02-29 12:10:44 -080098 // Returns an error type signature for a given |error_type| value,
99 // which is reported to the crash server along with the
100 // crash_reporter-user-collection signature.
101 std::string GetErrorTypeSignature(ErrorType error_type) const;
102
Chris Sosae4a86032010-06-16 17:08:34 -0700103 std::string GetPattern(bool enabled) const;
104 bool SetUpInternal(bool enabled);
105
Ben Chanf13bb582012-01-06 08:22:07 -0800106 // Returns, via |line|, the first line in |lines| that starts with |prefix|.
107 // Returns true if a line is found, or false otherwise.
108 bool GetFirstLineWithPrefix(const std::vector<std::string> &lines,
109 const char *prefix, std::string *line);
110
111 // Returns the identifier of |kind|, via |id|, found in |status_lines| on
112 // the line starting with |prefix|. |status_lines| contains the lines in
113 // the status file. Returns true if the identifier can be determined.
Ken Mixter777484c2010-07-23 16:22:44 -0700114 bool GetIdFromStatus(const char *prefix,
115 IdKind kind,
Ben Chanf13bb582012-01-06 08:22:07 -0800116 const std::vector<std::string> &status_lines,
Ken Mixter777484c2010-07-23 16:22:44 -0700117 int *id);
Ken Mixter207694d2010-10-28 15:42:37 -0700118
Ben Chanf13bb582012-01-06 08:22:07 -0800119 // Returns the process state, via |state|, found in |status_lines|, which
120 // contains the lines in the status file. Returns true if the process state
121 // can be determined.
122 bool GetStateFromStatus(const std::vector<std::string> &status_lines,
123 std::string *state);
124
Ken Mixter207694d2010-10-28 15:42:37 -0700125 void LogCollectionError(const std::string &error_message);
Ben Chan6e709a12012-02-29 12:10:44 -0800126 void EnqueueCollectionErrorLog(pid_t pid, ErrorType error_type,
127 const std::string &exec_name);
Ken Mixter207694d2010-10-28 15:42:37 -0700128
Simon Que9f90aca2013-02-19 17:19:52 -0800129 bool CopyOffProcFiles(pid_t pid, const base::FilePath &container_dir);
Ben Chanf13bb582012-01-06 08:22:07 -0800130
131 // Validates the proc files at |container_dir| and returns true if they
132 // are usable for the core-to-minidump conversion later. For instance, if
133 // a process is reaped by the kernel before the copying of its proc files
134 // takes place, some proc files like /proc/<pid>/maps may contain nothing
135 // and thus become unusable.
Simon Que9f90aca2013-02-19 17:19:52 -0800136 bool ValidateProcFiles(const base::FilePath &container_dir) const;
Ben Chan6e709a12012-02-29 12:10:44 -0800137
138 // Validates the core file at |core_path| and returns kErrorNone if
139 // the file contains the ELF magic bytes and an ELF class that matches the
140 // platform (i.e. 32-bit ELF on a 32-bit platform or 64-bit ELF on a 64-bit
141 // platform), which is due to the limitation in core2md. It returns an error
142 // type otherwise.
Simon Que9f90aca2013-02-19 17:19:52 -0800143 ErrorType ValidateCoreFile(const base::FilePath &core_path) const;
Ben Chanf13bb582012-01-06 08:22:07 -0800144
Ken Mixter777484c2010-07-23 16:22:44 -0700145 // Determines the crash directory for given pid based on pid's owner,
146 // and creates the directory if necessary with appropriate permissions.
147 // Returns true whether or not directory needed to be created, false on
148 // any failure.
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700149 bool GetCreatedCrashDirectory(pid_t pid, uid_t supplied_ruid,
Simon Que9f90aca2013-02-19 17:19:52 -0800150 base::FilePath *crash_file_path,
Ken Mixter207694d2010-10-28 15:42:37 -0700151 bool *out_of_capacity);
Simon Que9f90aca2013-02-19 17:19:52 -0800152 bool CopyStdinToCoreFile(const base::FilePath &core_path);
153 bool RunCoreToMinidump(const base::FilePath &core_path,
154 const base::FilePath &procfs_directory,
155 const base::FilePath &minidump_path,
156 const base::FilePath &temp_directory);
Ben Chan6e709a12012-02-29 12:10:44 -0800157 ErrorType ConvertCoreToMinidump(pid_t pid,
Simon Que9f90aca2013-02-19 17:19:52 -0800158 const base::FilePath &container_dir,
159 const base::FilePath &core_path,
160 const base::FilePath &minidump_path);
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700161 ErrorType ConvertAndEnqueueCrash(pid_t pid, const std::string &exec_name,
162 uid_t supplied_ruid, bool *out_of_capacity);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800163 bool ParseCrashAttributes(const std::string &crash_attributes,
Michael Krebs1c57e9e2012-09-25 18:03:13 -0700164 pid_t *pid, int *signal, uid_t *uid,
Ken Mixter1b8fe012011-01-25 13:33:05 -0800165 std::string *kernel_supplied_name);
Michael Krebs538ecbf2011-07-27 14:13:22 -0700166
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700167 bool ShouldDump(bool has_owner_consent,
168 bool is_developer,
Michael Krebs4fe30db2011-08-05 13:54:52 -0700169 bool handle_chrome_crashes,
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700170 const std::string &exec,
171 std::string *reason);
Ken Mixter777484c2010-07-23 16:22:44 -0700172
173 bool generate_diagnostics_;
Chris Sosae4a86032010-06-16 17:08:34 -0700174 std::string core_pattern_file_;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800175 std::string core_pipe_limit_file_;
Chris Sosae4a86032010-06-16 17:08:34 -0700176 std::string our_path_;
177 bool initialized_;
Ken Mixter777484c2010-07-23 16:22:44 -0700178
179 static const char *kUserId;
180 static const char *kGroupId;
Ben Chan895fa5d2014-09-02 20:58:20 -0700181
182 DISALLOW_COPY_AND_ASSIGN(UserCollector);
Chris Sosae4a86032010-06-16 17:08:34 -0700183};
184
Ben Chan7e776902014-06-18 13:19:51 -0700185#endif // CRASH_REPORTER_USER_COLLECTOR_H_