blob: 043d38c09e1c2d1ac8ecc58c8b712b3f472ee5b4 [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 Mixter777484c2010-07-23 16:22:44 -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
Ken Mixter03403162010-08-18 15:23:16 -070011#include "crash-reporter/crash_collector.h"
Ken Mixter777484c2010-07-23 16:22:44 -070012#include "gtest/gtest_prod.h" // for FRIEND_TEST
Chris Sosae4a86032010-06-16 17:08:34 -070013
14class FilePath;
Ken Mixter03403162010-08-18 15:23:16 -070015class SystemLogging;
Chris Sosae4a86032010-06-16 17:08:34 -070016
17// User crash collector.
Ken Mixter03403162010-08-18 15:23:16 -070018class UserCollector : public CrashCollector {
Chris Sosae4a86032010-06-16 17:08:34 -070019 public:
Chris Sosae4a86032010-06-16 17:08:34 -070020 UserCollector();
21
22 // Initialize the user crash collector for detection of crashes,
23 // given a crash counting function, the path to this executable,
24 // metrics collection enabled oracle, and system logger facility.
Ken Mixter777484c2010-07-23 16:22:44 -070025 // Crash detection/reporting is not enabled until Enable is called.
26 // |generate_diagnostics| is used to indicate whether or not to try
27 // to generate a minidump from crashes.
Chris Sosae4a86032010-06-16 17:08:34 -070028 void Initialize(CountCrashFunction count_crash,
29 const std::string &our_path,
30 IsFeedbackAllowedFunction is_metrics_allowed,
Ken Mixter777484c2010-07-23 16:22:44 -070031 bool generate_diagnostics);
Chris Sosae4a86032010-06-16 17:08:34 -070032
33 virtual ~UserCollector();
34
35 // Enable collection.
36 bool Enable() { return SetUpInternal(true); }
37
38 // Disable collection.
39 bool Disable() { return SetUpInternal(false); }
40
Ken Mixter777484c2010-07-23 16:22:44 -070041 // Handle a specific user crash. Returns true on success.
Ken Mixter1b8fe012011-01-25 13:33:05 -080042 bool HandleCrash(const std::string &crash_attributes,
43 const char *force_exec);
Chris Sosae4a86032010-06-16 17:08:34 -070044
45 // Set (override the default) core file pattern.
46 void set_core_pattern_file(const std::string &pattern) {
47 core_pattern_file_ = pattern;
48 }
49
Ken Mixterc49dbd42010-12-14 17:44:11 -080050 // Set (override the default) core pipe limit file.
51 void set_core_pipe_limit_file(const std::string &path) {
52 core_pipe_limit_file_ = path;
53 }
54
Chris Sosae4a86032010-06-16 17:08:34 -070055 private:
56 friend class UserCollectorTest;
Ken Mixter777484c2010-07-23 16:22:44 -070057 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPath);
58 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesBadPid);
59 FRIEND_TEST(UserCollectorTest, CopyOffProcFilesOK);
Ken Mixterd49d3622011-02-09 18:23:00 -080060 FRIEND_TEST(UserCollectorTest, GetExecutableBaseNameFromPid);
Ben Chanf13bb582012-01-06 08:22:07 -080061 FRIEND_TEST(UserCollectorTest, GetFirstLineWithPrefix);
Ken Mixter777484c2010-07-23 16:22:44 -070062 FRIEND_TEST(UserCollectorTest, GetIdFromStatus);
Ben Chanf13bb582012-01-06 08:22:07 -080063 FRIEND_TEST(UserCollectorTest, GetStateFromStatus);
Ken Mixter777484c2010-07-23 16:22:44 -070064 FRIEND_TEST(UserCollectorTest, GetProcessPath);
65 FRIEND_TEST(UserCollectorTest, GetSymlinkTarget);
66 FRIEND_TEST(UserCollectorTest, GetUserInfoFromName);
Ken Mixter1b8fe012011-01-25 13:33:05 -080067 FRIEND_TEST(UserCollectorTest, ParseCrashAttributes);
Ken Mixter5d3a1a22011-03-16 12:47:20 -070068 FRIEND_TEST(UserCollectorTest, ShouldDumpChromeOverridesDeveloperImage);
69 FRIEND_TEST(UserCollectorTest, ShouldDumpDeveloperImageOverridesConsent);
70 FRIEND_TEST(UserCollectorTest, ShouldDumpUseConsentProductionImage);
Ben Chanf13bb582012-01-06 08:22:07 -080071 FRIEND_TEST(UserCollectorTest, ValidateProcFiles);
Ken Mixter777484c2010-07-23 16:22:44 -070072
73 // Enumeration to pass to GetIdFromStatus. Must match the order
74 // that the kernel lists IDs in the status file.
75 enum IdKind {
76 kIdReal = 0, // uid and gid
77 kIdEffective = 1, // euid and egid
78 kIdSet = 2, // suid and sgid
79 kIdFileSystem = 3, // fsuid and fsgid
80 kIdMax
81 };
Chris Sosae4a86032010-06-16 17:08:34 -070082
Ken Mixter2953c3a2010-10-18 14:42:20 -070083 static const int kForkProblem = 255;
84
Chris Sosae4a86032010-06-16 17:08:34 -070085 std::string GetPattern(bool enabled) const;
86 bool SetUpInternal(bool enabled);
87
Ken Mixter777484c2010-07-23 16:22:44 -070088 FilePath GetProcessPath(pid_t pid);
89 bool GetSymlinkTarget(const FilePath &symlink,
90 FilePath *target);
91 bool GetExecutableBaseNameFromPid(uid_t pid,
92 std::string *base_name);
Ben Chanf13bb582012-01-06 08:22:07 -080093 // Returns, via |line|, the first line in |lines| that starts with |prefix|.
94 // Returns true if a line is found, or false otherwise.
95 bool GetFirstLineWithPrefix(const std::vector<std::string> &lines,
96 const char *prefix, std::string *line);
97
98 // Returns the identifier of |kind|, via |id|, found in |status_lines| on
99 // the line starting with |prefix|. |status_lines| contains the lines in
100 // the status file. Returns true if the identifier can be determined.
Ken Mixter777484c2010-07-23 16:22:44 -0700101 bool GetIdFromStatus(const char *prefix,
102 IdKind kind,
Ben Chanf13bb582012-01-06 08:22:07 -0800103 const std::vector<std::string> &status_lines,
Ken Mixter777484c2010-07-23 16:22:44 -0700104 int *id);
Ken Mixter207694d2010-10-28 15:42:37 -0700105
Ben Chanf13bb582012-01-06 08:22:07 -0800106 // Returns the process state, via |state|, found in |status_lines|, which
107 // contains the lines in the status file. Returns true if the process state
108 // can be determined.
109 bool GetStateFromStatus(const std::vector<std::string> &status_lines,
110 std::string *state);
111
Ken Mixter207694d2010-10-28 15:42:37 -0700112 void LogCollectionError(const std::string &error_message);
113 void EnqueueCollectionErrorLog(pid_t pid, const std::string &exec_name);
114
Ben Chanf13bb582012-01-06 08:22:07 -0800115 bool CopyOffProcFiles(pid_t pid, const FilePath &container_dir);
116
117 // Validates the proc files at |container_dir| and returns true if they
118 // are usable for the core-to-minidump conversion later. For instance, if
119 // a process is reaped by the kernel before the copying of its proc files
120 // takes place, some proc files like /proc/<pid>/maps may contain nothing
121 // and thus become unusable.
122 bool ValidateProcFiles(const FilePath &container_dir);
123
Ken Mixter777484c2010-07-23 16:22:44 -0700124 // Determines the crash directory for given pid based on pid's owner,
125 // and creates the directory if necessary with appropriate permissions.
126 // Returns true whether or not directory needed to be created, false on
127 // any failure.
128 bool GetCreatedCrashDirectory(pid_t pid,
Ken Mixter207694d2010-10-28 15:42:37 -0700129 FilePath *crash_file_path,
130 bool *out_of_capacity);
Ken Mixter777484c2010-07-23 16:22:44 -0700131 bool CopyStdinToCoreFile(const FilePath &core_path);
Ken Mixter207694d2010-10-28 15:42:37 -0700132 bool RunCoreToMinidump(const FilePath &core_path,
133 const FilePath &procfs_directory,
134 const FilePath &minidump_path,
135 const FilePath &temp_directory);
136 bool ConvertCoreToMinidump(pid_t pid,
137 const FilePath &container_dir,
138 const FilePath &core_path,
139 const FilePath &minidump_path);
140 bool ConvertAndEnqueueCrash(int pid, const std::string &exec_name,
141 bool *out_of_capacity);
Ken Mixter1b8fe012011-01-25 13:33:05 -0800142 bool ParseCrashAttributes(const std::string &crash_attributes,
143 pid_t *pid, int *signal,
144 std::string *kernel_supplied_name);
Michael Krebs538ecbf2011-07-27 14:13:22 -0700145
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700146 bool ShouldDump(bool has_owner_consent,
147 bool is_developer,
Michael Krebs4fe30db2011-08-05 13:54:52 -0700148 bool handle_chrome_crashes,
Ken Mixter5d3a1a22011-03-16 12:47:20 -0700149 const std::string &exec,
150 std::string *reason);
Ken Mixter777484c2010-07-23 16:22:44 -0700151
152 bool generate_diagnostics_;
Chris Sosae4a86032010-06-16 17:08:34 -0700153 std::string core_pattern_file_;
Ken Mixterc49dbd42010-12-14 17:44:11 -0800154 std::string core_pipe_limit_file_;
Chris Sosae4a86032010-06-16 17:08:34 -0700155 std::string our_path_;
156 bool initialized_;
Ken Mixter777484c2010-07-23 16:22:44 -0700157
158 static const char *kUserId;
159 static const char *kGroupId;
Chris Sosae4a86032010-06-16 17:08:34 -0700160};
161
Ken Mixter777484c2010-07-23 16:22:44 -0700162#endif // _CRASH_REPORTER_USER_COLLECTOR_H_