blob: 21b919872c02f18737c0d4bae68968b33b5a9c1b [file] [log] [blame]
Steve Fung6c34c252015-08-20 00:27:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ken Mixter03403162010-08-18 15:23:16 -070016
Ben Chan7e776902014-06-18 13:19:51 -070017#ifndef CRASH_REPORTER_CRASH_COLLECTOR_H_
18#define CRASH_REPORTER_CRASH_COLLECTOR_H_
Ken Mixter03403162010-08-18 15:23:16 -070019
Ken Mixter03403162010-08-18 15:23:16 -070020#include <sys/stat.h>
21
Ken Mixteree849c52010-09-30 15:30:10 -070022#include <map>
23#include <string>
24
Ben Chan7e776902014-06-18 13:19:51 -070025#include <base/files/file_path.h>
Ben Chan895fa5d2014-09-02 20:58:20 -070026#include <base/macros.h>
Ben Chan7e776902014-06-18 13:19:51 -070027#include <gtest/gtest_prod.h> // for FRIEND_TEST
Ken Mixter03403162010-08-18 15:23:16 -070028
Ken Mixter03403162010-08-18 15:23:16 -070029// User crash collector.
30class CrashCollector {
31 public:
32 typedef void (*CountCrashFunction)();
33 typedef bool (*IsFeedbackAllowedFunction)();
34
35 CrashCollector();
36
37 virtual ~CrashCollector();
38
39 // Initialize the crash collector for detection of crashes, given a
Ken Mixtera3249322011-03-03 08:47:38 -080040 // crash counting function, and metrics collection enabled oracle.
Ken Mixter03403162010-08-18 15:23:16 -070041 void Initialize(CountCrashFunction count_crash,
Ken Mixtera3249322011-03-03 08:47:38 -080042 IsFeedbackAllowedFunction is_metrics_allowed);
Ken Mixter03403162010-08-18 15:23:16 -070043
44 protected:
45 friend class CrashCollectorTest;
Lei Zhang9b1f3002014-04-24 02:10:57 -070046 FRIEND_TEST(ChromeCollectorTest, HandleCrash);
Ken Mixteree849c52010-09-30 15:30:10 -070047 FRIEND_TEST(CrashCollectorTest, CheckHasCapacityCorrectBasename);
48 FRIEND_TEST(CrashCollectorTest, CheckHasCapacityStrangeNames);
49 FRIEND_TEST(CrashCollectorTest, CheckHasCapacityUsual);
Ken Mixter03403162010-08-18 15:23:16 -070050 FRIEND_TEST(CrashCollectorTest, GetCrashDirectoryInfo);
Ken Mixter207694d2010-10-28 15:42:37 -070051 FRIEND_TEST(CrashCollectorTest, GetCrashPath);
Ken Mixterc49dbd42010-12-14 17:44:11 -080052 FRIEND_TEST(CrashCollectorTest, GetLogContents);
Ken Mixter9b346472010-11-07 13:45:45 -080053 FRIEND_TEST(CrashCollectorTest, ForkExecAndPipe);
Ken Mixter03403162010-08-18 15:23:16 -070054 FRIEND_TEST(CrashCollectorTest, FormatDumpBasename);
55 FRIEND_TEST(CrashCollectorTest, Initialize);
Ken Mixterafcf8082010-10-26 14:45:01 -070056 FRIEND_TEST(CrashCollectorTest, MetaData);
Ken Mixteree849c52010-09-30 15:30:10 -070057 FRIEND_TEST(CrashCollectorTest, Sanitize);
Ken Mixter9b346472010-11-07 13:45:45 -080058 FRIEND_TEST(CrashCollectorTest, WriteNewFile);
Ken Mixter1b8fe012011-01-25 13:33:05 -080059 FRIEND_TEST(ForkExecAndPipeTest, Basic);
60 FRIEND_TEST(ForkExecAndPipeTest, NonZeroReturnValue);
61 FRIEND_TEST(ForkExecAndPipeTest, BadOutputFile);
62 FRIEND_TEST(ForkExecAndPipeTest, ExistingOutputFile);
63 FRIEND_TEST(ForkExecAndPipeTest, BadExecutable);
64 FRIEND_TEST(ForkExecAndPipeTest, StderrCaptured);
65 FRIEND_TEST(ForkExecAndPipeTest, NULLParam);
66 FRIEND_TEST(ForkExecAndPipeTest, NoParams);
67 FRIEND_TEST(ForkExecAndPipeTest, SegFaultHandling);
Ken Mixter03403162010-08-18 15:23:16 -070068
Ken Mixter04ec10f2010-08-26 16:02:02 -070069 // Set maximum enqueued crashes in a crash directory.
70 static const int kMaxCrashDirectorySize;
71
Ken Mixter9b346472010-11-07 13:45:45 -080072 // Writes |data| of |size| to |filename|, which must be a new file.
73 // If the file already exists or writing fails, return a negative value.
74 // Otherwise returns the number of bytes written.
Simon Que9f90aca2013-02-19 17:19:52 -080075 int WriteNewFile(const base::FilePath &filename, const char *data, int size);
Ken Mixter9b346472010-11-07 13:45:45 -080076
Ken Mixteree849c52010-09-30 15:30:10 -070077 // Return a filename that has only [a-z0-1_] characters by mapping
78 // all others into '_'.
79 std::string Sanitize(const std::string &name);
80
Ken Mixter03403162010-08-18 15:23:16 -070081 // For testing, set the directory always returned by
82 // GetCreatedCrashDirectoryByEuid.
Lei Zhang9b1f3002014-04-24 02:10:57 -070083 void ForceCrashDirectory(const base::FilePath &forced_directory) {
Ken Mixter03403162010-08-18 15:23:16 -070084 forced_crash_directory_ = forced_directory;
85 }
86
Steve Fungb3a48e12016-02-25 03:37:33 -080087 // For testing, set the root directory to read etc/os-release.d properties
88 // from.
89 void ForceOsReleaseDDirectory(const base::FilePath &forced_directory) {
90 forced_osreleased_directory_ = forced_directory;
91 }
92
Steve Fungab2ac7d2015-08-14 17:58:05 -070093 base::FilePath GetCrashDirectoryInfo(mode_t *mode,
94 uid_t *directory_owner,
95 gid_t *directory_group);
Ken Mixter03403162010-08-18 15:23:16 -070096 bool GetUserInfoFromName(const std::string &name,
97 uid_t *uid,
98 gid_t *gid);
Lei Zhang9b1f3002014-04-24 02:10:57 -070099
100 // Determines the crash directory for given euid, and creates the
Ken Mixter207694d2010-10-28 15:42:37 -0700101 // directory if necessary with appropriate permissions. If
Ben Chan262d7982014-09-18 08:05:20 -0700102 // |out_of_capacity| is not nullptr, it is set to indicate if the call
Ken Mixter207694d2010-10-28 15:42:37 -0700103 // failed due to not having capacity in the crash directory. Returns
Ken Mixter03403162010-08-18 15:23:16 -0700104 // true whether or not directory needed to be created, false on any
Ken Mixter207694d2010-10-28 15:42:37 -0700105 // failure. If the crash directory is at capacity, returns false.
Ken Mixter03403162010-08-18 15:23:16 -0700106 bool GetCreatedCrashDirectoryByEuid(uid_t euid,
Simon Que9f90aca2013-02-19 17:19:52 -0800107 base::FilePath *crash_file_path,
Ken Mixter207694d2010-10-28 15:42:37 -0700108 bool *out_of_capacity);
Ken Mixter04ec10f2010-08-26 16:02:02 -0700109
110 // Format crash name based on components.
Ken Mixter03403162010-08-18 15:23:16 -0700111 std::string FormatDumpBasename(const std::string &exec_name,
112 time_t timestamp,
113 pid_t pid);
114
Ken Mixter207694d2010-10-28 15:42:37 -0700115 // Create a file path to a file in |crash_directory| with the given
116 // |basename| and |extension|.
Simon Que9f90aca2013-02-19 17:19:52 -0800117 base::FilePath GetCrashPath(const base::FilePath &crash_directory,
118 const std::string &basename,
119 const std::string &extension);
Ken Mixter207694d2010-10-28 15:42:37 -0700120
Albert Chaulk426fcc02013-05-02 15:38:31 -0700121 base::FilePath GetProcessPath(pid_t pid);
122 bool GetSymlinkTarget(const base::FilePath &symlink,
123 base::FilePath *target);
124 bool GetExecutableBaseNameFromPid(pid_t pid,
125 std::string *base_name);
126
Ken Mixter04ec10f2010-08-26 16:02:02 -0700127 // Check given crash directory still has remaining capacity for another
128 // crash.
Simon Que9f90aca2013-02-19 17:19:52 -0800129 bool CheckHasCapacity(const base::FilePath &crash_directory);
Ken Mixter04ec10f2010-08-26 16:02:02 -0700130
Ken Mixterc49dbd42010-12-14 17:44:11 -0800131 // Write a log applicable to |exec_name| to |output_file| based on the
132 // log configuration file at |config_path|.
Simon Que9f90aca2013-02-19 17:19:52 -0800133 bool GetLogContents(const base::FilePath &config_path,
Ken Mixterc49dbd42010-12-14 17:44:11 -0800134 const std::string &exec_name,
Simon Que9f90aca2013-02-19 17:19:52 -0800135 const base::FilePath &output_file);
Ken Mixterc49dbd42010-12-14 17:44:11 -0800136
Ken Mixterafcf8082010-10-26 14:45:01 -0700137 // Add non-standard meta data to the crash metadata file. Call
138 // before calling WriteCrashMetaData. Key must not contain "=" or
139 // "\n" characters. Value must not contain "\n" characters.
140 void AddCrashMetaData(const std::string &key, const std::string &value);
141
Albert Chaulk33dfd472013-06-19 15:34:13 -0700142 // Add a file to be uploaded to the crash reporter server. The file must
143 // persist until the crash report is sent; ideally it should live in the same
144 // place as the .meta file, so it can be cleaned up automatically.
145 void AddCrashMetaUploadFile(const std::string &key, const std::string &path);
146
147 // Add non-standard meta data to the crash metadata file.
148 // Data added though this call will be uploaded to the crash reporter server,
149 // appearing as a form field.
150 void AddCrashMetaUploadData(const std::string &key, const std::string &value);
151
Ken Mixteree849c52010-09-30 15:30:10 -0700152 // Write a file of metadata about crash.
Simon Que9f90aca2013-02-19 17:19:52 -0800153 void WriteCrashMetaData(const base::FilePath &meta_path,
Ken Mixterc909b692010-10-18 12:26:05 -0700154 const std::string &exec_name,
155 const std::string &payload_path);
Ken Mixteree849c52010-09-30 15:30:10 -0700156
Thieu Le1652fb22011-03-03 12:14:43 -0800157 // Returns true if the a crash test is currently running.
158 bool IsCrashTestInProgress();
Michael Krebs4fe30db2011-08-05 13:54:52 -0700159 // Returns true if we should consider ourselves to be running on a
160 // developer image.
161 bool IsDeveloperImage();
Thieu Le1652fb22011-03-03 12:14:43 -0800162
Ken Mixter03403162010-08-18 15:23:16 -0700163 CountCrashFunction count_crash_function_;
164 IsFeedbackAllowedFunction is_feedback_allowed_function_;
Ken Mixterafcf8082010-10-26 14:45:01 -0700165 std::string extra_metadata_;
Lei Zhang9b1f3002014-04-24 02:10:57 -0700166 base::FilePath forced_crash_directory_;
Steve Fungb3a48e12016-02-25 03:37:33 -0800167 base::FilePath forced_osreleased_directory_;
Simon Que9f90aca2013-02-19 17:19:52 -0800168 base::FilePath log_config_path_;
Ben Chan895fa5d2014-09-02 20:58:20 -0700169
170 private:
171 DISALLOW_COPY_AND_ASSIGN(CrashCollector);
Ken Mixter03403162010-08-18 15:23:16 -0700172};
173
Ben Chan7e776902014-06-18 13:19:51 -0700174#endif // CRASH_REPORTER_CRASH_COLLECTOR_H_