blob: 16e70d8a85fc71cee1d7b49b1ec1d9bc7ef0d527 [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 */
Chris Sosae4a86032010-06-16 17:08:34 -070016
Ken Mixtera3249322011-03-03 08:47:38 -080017#include <fcntl.h> // for open
18
Chris Sosae4a86032010-06-16 17:08:34 -070019#include <string>
Simon Quef70060c2012-04-09 19:07:07 -070020#include <vector>
Chris Sosae4a86032010-06-16 17:08:34 -070021
Ben Chanab6cc902014-09-05 08:21:06 -070022#include <base/files/file_util.h>
Steve Fung12e61ca2015-09-15 16:36:33 -070023#include <base/guid.h>
Albert Chaulk426fcc02013-05-02 15:38:31 -070024#include <base/logging.h>
Mike Frysingera557c112014-02-05 22:55:39 -050025#include <base/strings/string_split.h>
26#include <base/strings/string_util.h>
27#include <base/strings/stringprintf.h>
Todd Poynor83619352015-12-02 14:38:43 -080028#include <binder/IServiceManager.h>
Alex Vakulenko74dc6242015-10-13 09:23:34 -070029#include <brillo/flag_helper.h>
Alex Vakulenko74dc6242015-10-13 09:23:34 -070030#include <brillo/syslog_logging.h>
Todd Poynor83619352015-12-02 14:38:43 -080031#include <metrics/metrics_collector_service_client.h>
Ben Chan7e776902014-06-18 13:19:51 -070032#include <metrics/metrics_library.h>
Todd Poynor83619352015-12-02 14:38:43 -080033#include <utils/String16.h>
34
Ben Chan7e776902014-06-18 13:19:51 -070035
Steve Fung129bea52015-07-23 13:11:15 -070036#include "kernel_collector.h"
37#include "kernel_warning_collector.h"
Steve Fung129bea52015-07-23 13:11:15 -070038#include "unclean_shutdown_collector.h"
39#include "user_collector.h"
Chris Sosae4a86032010-06-16 17:08:34 -070040
Steve Fungc490e0f2016-01-04 14:26:15 -080041#if !defined(__ANDROID__)
42#include "udev_collector.h"
43#endif
44
Chris Sosae4a86032010-06-16 17:08:34 -070045static const char kCrashCounterHistogram[] = "Logging.CrashCounter";
Steve Fung2bedc742016-02-02 16:11:43 -080046static const char kKernelCrashDetected[] =
47 "/data/misc/crash_reporter/run/kernel-crash-detected";
Chris Masoned3ac7962012-02-24 14:38:57 -080048static const char kUncleanShutdownDetected[] =
49 "/var/run/unclean-shutdown-detected";
Steve Fung12e61ca2015-09-15 16:36:33 -070050static const char kGUIDFileName[] = "/data/misc/crash_reporter/guid";
Ken Mixter777484c2010-07-23 16:22:44 -070051
Chris Sosae4a86032010-06-16 17:08:34 -070052// Enumeration of kinds of crashes to be used in the CrashCounter histogram.
53enum CrashKinds {
Ken Mixter03403162010-08-18 15:23:16 -070054 kCrashKindUncleanShutdown = 1,
55 kCrashKindUser = 2,
56 kCrashKindKernel = 3,
Simon Queacc79382012-05-04 18:10:09 -070057 kCrashKindUdev = 4,
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -070058 kCrashKindKernelWarning = 5,
Ken Mixter777484c2010-07-23 16:22:44 -070059 kCrashKindMax
Chris Sosae4a86032010-06-16 17:08:34 -070060};
61
62static MetricsLibrary s_metrics_lib;
Chris Sosae4a86032010-06-16 17:08:34 -070063
Todd Poynor83619352015-12-02 14:38:43 -080064using android::brillo::metrics::IMetricsCollectorService;
Simon Que9f90aca2013-02-19 17:19:52 -080065using base::FilePath;
Mike Frysingera557c112014-02-05 22:55:39 -050066using base::StringPrintf;
Simon Que9f90aca2013-02-19 17:19:52 -080067
Ken Mixter03403162010-08-18 15:23:16 -070068static bool IsFeedbackAllowed() {
Ken Mixteree849c52010-09-30 15:30:10 -070069 return s_metrics_lib.AreMetricsEnabled();
Chris Sosae4a86032010-06-16 17:08:34 -070070}
71
Ken Mixter03403162010-08-18 15:23:16 -070072static bool TouchFile(const FilePath &file_path) {
Ben Chanf30c6412014-05-22 23:09:01 -070073 return base::WriteFile(file_path, "", 0) == 0;
Chris Sosae4a86032010-06-16 17:08:34 -070074}
75
Simon Que6f035492012-06-27 17:28:14 -070076static void SendCrashMetrics(CrashKinds type, const char* name) {
Ken Mixter1d3c3042011-01-22 06:16:40 -080077 // TODO(kmixter): We can remove this histogram as part of
78 // crosbug.com/11163.
Simon Que6f035492012-06-27 17:28:14 -070079 s_metrics_lib.SendEnumToUMA(kCrashCounterHistogram, type, kCrashKindMax);
80 s_metrics_lib.SendCrashToUMA(name);
81}
82
83static void CountKernelCrash() {
84 SendCrashMetrics(kCrashKindKernel, "kernel");
Chris Sosae4a86032010-06-16 17:08:34 -070085}
86
Simon Queacc79382012-05-04 18:10:09 -070087static void CountUdevCrash() {
88 SendCrashMetrics(kCrashKindUdev, "udevcrash");
89}
90
Ken Mixter03403162010-08-18 15:23:16 -070091static void CountUncleanShutdown() {
Simon Que6f035492012-06-27 17:28:14 -070092 SendCrashMetrics(kCrashKindUncleanShutdown, "uncleanshutdown");
Chris Sosae4a86032010-06-16 17:08:34 -070093}
94
95static void CountUserCrash() {
Simon Que6f035492012-06-27 17:28:14 -070096 SendCrashMetrics(kCrashKindUser, "user");
Todd Poynor83619352015-12-02 14:38:43 -080097 // Tell the metrics collector about the user crash, in order to log active
98 // use time between crashes.
99 MetricsCollectorServiceClient metrics_collector_service;
Ken Mixter03403162010-08-18 15:23:16 -0700100
Todd Poynor83619352015-12-02 14:38:43 -0800101 if (metrics_collector_service.Init())
102 metrics_collector_service.notifyUserCrash();
103 else
104 LOG(ERROR) << "Failed to send user crash notification to metrics_collector";
Chris Sosae4a86032010-06-16 17:08:34 -0700105}
106
Albert Chaulk426fcc02013-05-02 15:38:31 -0700107
Ken Mixter03403162010-08-18 15:23:16 -0700108static int Initialize(KernelCollector *kernel_collector,
109 UserCollector *user_collector,
Steve Fungd6169a22014-08-11 15:52:23 -0700110 UncleanShutdownCollector *unclean_shutdown_collector,
111 const bool unclean_check,
112 const bool clean_shutdown) {
113 CHECK(!clean_shutdown) << "Incompatible options";
Chris Sosae4a86032010-06-16 17:08:34 -0700114
Steve Fung12e61ca2015-09-15 16:36:33 -0700115 // Try to read the GUID from kGUIDFileName. If the file doesn't exist, is
116 // blank, or the read fails, generate a new GUID and write it to the file.
117 std::string guid;
118 base::FilePath filepath(kGUIDFileName);
119 if (!base::ReadFileToString(filepath, &guid) || guid.empty()) {
120 guid = base::GenerateGUID();
121 // If we can't read or write the file, log an error. However it is not
122 // a fatal error, as the crash server will assign a random GUID based
123 // on a hash of the IP address if one is not provided in the report.
124 if (base::WriteFile(filepath, guid.c_str(), guid.size()) <= 0) {
125 LOG(ERROR) << "Could not write guid " << guid << " to file "
126 << filepath.value();
127 }
128 }
129
Ken Mixter03403162010-08-18 15:23:16 -0700130 bool was_kernel_crash = false;
131 bool was_unclean_shutdown = false;
Hugh Dickinsf174fc02010-09-08 20:55:26 -0700132 kernel_collector->Enable();
Ben Chan3c6b82c2014-07-23 14:52:14 -0700133 if (kernel_collector->is_enabled()) {
Ken Mixter03403162010-08-18 15:23:16 -0700134 was_kernel_crash = kernel_collector->Collect();
135 }
136
Steve Fungd6169a22014-08-11 15:52:23 -0700137 if (unclean_check) {
Ken Mixter03403162010-08-18 15:23:16 -0700138 was_unclean_shutdown = unclean_shutdown_collector->Collect();
139 }
140
141 // Touch a file to notify the metrics daemon that a kernel
142 // crash has been detected so that it can log the time since
143 // the last kernel crash.
144 if (IsFeedbackAllowed()) {
145 if (was_kernel_crash) {
Chris Masoned3ac7962012-02-24 14:38:57 -0800146 TouchFile(FilePath(kKernelCrashDetected));
Ken Mixter03403162010-08-18 15:23:16 -0700147 } else if (was_unclean_shutdown) {
148 // We only count an unclean shutdown if it did not come with
149 // an associated kernel crash.
Chris Masoned3ac7962012-02-24 14:38:57 -0800150 TouchFile(FilePath(kUncleanShutdownDetected));
Chris Sosae4a86032010-06-16 17:08:34 -0700151 }
Chris Sosae4a86032010-06-16 17:08:34 -0700152 }
153
Ken Mixter03403162010-08-18 15:23:16 -0700154 // Must enable the unclean shutdown collector *after* collecting.
Ken Mixter03403162010-08-18 15:23:16 -0700155 unclean_shutdown_collector->Enable();
156 user_collector->Enable();
Chris Sosae4a86032010-06-16 17:08:34 -0700157
Ken Mixter03403162010-08-18 15:23:16 -0700158 return 0;
159}
160
Steve Fungd6169a22014-08-11 15:52:23 -0700161static int HandleUserCrash(UserCollector *user_collector,
162 const std::string& user, const bool crash_test) {
Chris Sosae4a86032010-06-16 17:08:34 -0700163 // Handle a specific user space crash.
Steve Fungd6169a22014-08-11 15:52:23 -0700164 CHECK(!user.empty()) << "--user= must be set";
Chris Sosae4a86032010-06-16 17:08:34 -0700165
166 // Make it possible to test what happens when we crash while
167 // handling a crash.
Steve Fungd6169a22014-08-11 15:52:23 -0700168 if (crash_test) {
Mike Frysinger9a7ce9f2013-01-19 11:03:37 -0500169 *(volatile char *)0 = 0;
Chris Sosae4a86032010-06-16 17:08:34 -0700170 return 0;
171 }
172
Ken Mixterd49d3622011-02-09 18:23:00 -0800173 // Accumulate logs to help in diagnosing failures during user collection.
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700174 brillo::LogToString(true);
Ken Mixter777484c2010-07-23 16:22:44 -0700175 // Handle the crash, get the name of the process from procfs.
Steve Fungd6169a22014-08-11 15:52:23 -0700176 bool handled = user_collector->HandleCrash(user, nullptr);
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700177 brillo::LogToString(false);
Ken Mixterd49d3622011-02-09 18:23:00 -0800178 if (!handled)
Ken Mixter777484c2010-07-23 16:22:44 -0700179 return 1;
Chris Sosae4a86032010-06-16 17:08:34 -0700180 return 0;
181}
Ken Mixter03403162010-08-18 15:23:16 -0700182
Steve Fungc490e0f2016-01-04 14:26:15 -0800183#if !defined(__ANDROID__)
Steve Fungd6169a22014-08-11 15:52:23 -0700184static int HandleUdevCrash(UdevCollector *udev_collector,
185 const std::string& udev_event) {
Simon Quef70060c2012-04-09 19:07:07 -0700186 // Handle a crash indicated by a udev event.
Steve Fungd6169a22014-08-11 15:52:23 -0700187 CHECK(!udev_event.empty()) << "--udev= must be set";
Simon Quef70060c2012-04-09 19:07:07 -0700188
189 // Accumulate logs to help in diagnosing failures during user collection.
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700190 brillo::LogToString(true);
Steve Fungd6169a22014-08-11 15:52:23 -0700191 bool handled = udev_collector->HandleCrash(udev_event);
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700192 brillo::LogToString(false);
Simon Quef70060c2012-04-09 19:07:07 -0700193 if (!handled)
194 return 1;
195 return 0;
196}
Steve Fungc490e0f2016-01-04 14:26:15 -0800197#endif
Simon Quef70060c2012-04-09 19:07:07 -0700198
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -0700199static int HandleKernelWarning(KernelWarningCollector
200 *kernel_warning_collector) {
201 // Accumulate logs to help in diagnosing failures during collection.
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700202 brillo::LogToString(true);
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -0700203 bool handled = kernel_warning_collector->Collect();
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700204 brillo::LogToString(false);
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -0700205 if (!handled)
206 return 1;
207 return 0;
208}
209
Ken Mixterafcf8082010-10-26 14:45:01 -0700210// Interactive/diagnostics mode for generating kernel crash signatures.
Steve Fungd6169a22014-08-11 15:52:23 -0700211static int GenerateKernelSignature(KernelCollector *kernel_collector,
212 const std::string& kernel_signature_file) {
Ken Mixterafcf8082010-10-26 14:45:01 -0700213 std::string kcrash_contents;
214 std::string signature;
Steve Fungd6169a22014-08-11 15:52:23 -0700215 if (!base::ReadFileToString(FilePath(kernel_signature_file),
Mike Frysingera557c112014-02-05 22:55:39 -0500216 &kcrash_contents)) {
Ken Mixterafcf8082010-10-26 14:45:01 -0700217 fprintf(stderr, "Could not read file.\n");
218 return 1;
219 }
220 if (!kernel_collector->ComputeKernelStackSignature(
221 kcrash_contents,
222 &signature,
223 true)) {
224 fprintf(stderr, "Signature could not be generated.\n");
225 return 1;
226 }
227 printf("Kernel crash signature is \"%s\".\n", signature.c_str());
228 return 0;
229}
Ken Mixter03403162010-08-18 15:23:16 -0700230
Ken Mixtera3249322011-03-03 08:47:38 -0800231// Ensure stdout, stdin, and stderr are open file descriptors. If
232// they are not, any code which writes to stderr/stdout may write out
233// to files opened during execution. In particular, when
234// crash_reporter is run by the kernel coredump pipe handler (via
235// kthread_create/kernel_execve), it will not have file table entries
236// 1 and 2 (stdout and stderr) populated. We populate them here.
237static void OpenStandardFileDescriptors() {
238 int new_fd = -1;
239 // We open /dev/null to fill in any of the standard [0, 2] file
240 // descriptors. We leave these open for the duration of the
241 // process. This works because open returns the lowest numbered
242 // invalid fd.
243 do {
244 new_fd = open("/dev/null", 0);
Ben Chan7e776902014-06-18 13:19:51 -0700245 CHECK_GE(new_fd, 0) << "Unable to open /dev/null";
Ken Mixtera3249322011-03-03 08:47:38 -0800246 } while (new_fd >= 0 && new_fd <= 2);
247 close(new_fd);
248}
249
Ken Mixter03403162010-08-18 15:23:16 -0700250int main(int argc, char *argv[]) {
Steve Fungd6169a22014-08-11 15:52:23 -0700251 DEFINE_bool(init, false, "Initialize crash logging");
252 DEFINE_bool(clean_shutdown, false, "Signal clean shutdown");
253 DEFINE_string(generate_kernel_signature, "",
254 "Generate signature from given kcrash file");
255 DEFINE_bool(crash_test, false, "Crash test");
256 DEFINE_string(user, "", "User crash info (pid:signal:exec_name)");
257 DEFINE_bool(unclean_check, true, "Check for unclean shutdown");
Steve Fungc490e0f2016-01-04 14:26:15 -0800258
259#if !defined(__ANDROID__)
Steve Fungd6169a22014-08-11 15:52:23 -0700260 DEFINE_string(udev, "", "Udev event description (type:device:subsystem)");
Steve Fungc490e0f2016-01-04 14:26:15 -0800261#endif
262
Steve Fungd6169a22014-08-11 15:52:23 -0700263 DEFINE_bool(kernel_warning, false, "Report collected kernel warning");
Steve Fungd6169a22014-08-11 15:52:23 -0700264 DEFINE_string(pid, "", "PID of crashing process");
265 DEFINE_string(uid, "", "UID of crashing process");
266 DEFINE_string(exe, "", "Executable name of crashing process");
267 DEFINE_bool(core2md_failure, false, "Core2md failure test");
268 DEFINE_bool(directory_failure, false, "Spool directory failure test");
269 DEFINE_string(filter_in, "",
270 "Ignore all crashes but this for testing");
271
Ken Mixtera3249322011-03-03 08:47:38 -0800272 OpenStandardFileDescriptors();
Mike Frysingera557c112014-02-05 22:55:39 -0500273 FilePath my_path = base::MakeAbsoluteFilePath(FilePath(argv[0]));
Ken Mixter03403162010-08-18 15:23:16 -0700274 s_metrics_lib.Init();
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700275 brillo::FlagHelper::Init(argc, argv, "Chromium OS Crash Reporter");
276 brillo::OpenLog(my_path.BaseName().value().c_str(), true);
277 brillo::InitLog(brillo::kLogToSyslog);
Mike Frysingerf19b5182013-05-17 19:36:47 -0400278
Ken Mixter03403162010-08-18 15:23:16 -0700279 KernelCollector kernel_collector;
Lei Zhang9b1f3002014-04-24 02:10:57 -0700280 kernel_collector.Initialize(CountKernelCrash, IsFeedbackAllowed);
Ken Mixter03403162010-08-18 15:23:16 -0700281 UserCollector user_collector;
282 user_collector.Initialize(CountUserCrash,
283 my_path.value(),
284 IsFeedbackAllowed,
Steve Fungd6169a22014-08-11 15:52:23 -0700285 true, // generate_diagnostics
286 FLAGS_core2md_failure,
287 FLAGS_directory_failure,
288 FLAGS_filter_in);
Ken Mixter03403162010-08-18 15:23:16 -0700289 UncleanShutdownCollector unclean_shutdown_collector;
290 unclean_shutdown_collector.Initialize(CountUncleanShutdown,
Ken Mixtera3249322011-03-03 08:47:38 -0800291 IsFeedbackAllowed);
Steve Fungc490e0f2016-01-04 14:26:15 -0800292
293#if !defined(__ANDROID__)
Simon Queacc79382012-05-04 18:10:09 -0700294 UdevCollector udev_collector;
295 udev_collector.Initialize(CountUdevCrash, IsFeedbackAllowed);
Steve Fungc490e0f2016-01-04 14:26:15 -0800296#endif
Ken Mixter03403162010-08-18 15:23:16 -0700297
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -0700298 KernelWarningCollector kernel_warning_collector;
Luigi Semenzato20980d72013-05-28 14:29:43 -0700299 kernel_warning_collector.Initialize(CountUdevCrash, IsFeedbackAllowed);
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -0700300
Ken Mixter03403162010-08-18 15:23:16 -0700301 if (FLAGS_init) {
302 return Initialize(&kernel_collector,
303 &user_collector,
Steve Fungd6169a22014-08-11 15:52:23 -0700304 &unclean_shutdown_collector,
305 FLAGS_unclean_check,
306 FLAGS_clean_shutdown);
Ken Mixter03403162010-08-18 15:23:16 -0700307 }
308
309 if (FLAGS_clean_shutdown) {
310 unclean_shutdown_collector.Disable();
311 user_collector.Disable();
312 return 0;
313 }
314
Ken Mixterafcf8082010-10-26 14:45:01 -0700315 if (!FLAGS_generate_kernel_signature.empty()) {
Steve Fungd6169a22014-08-11 15:52:23 -0700316 return GenerateKernelSignature(&kernel_collector,
317 FLAGS_generate_kernel_signature);
Ken Mixterafcf8082010-10-26 14:45:01 -0700318 }
319
Steve Fungc490e0f2016-01-04 14:26:15 -0800320#if !defined(__ANDROID__)
Simon Quef70060c2012-04-09 19:07:07 -0700321 if (!FLAGS_udev.empty()) {
Steve Fungd6169a22014-08-11 15:52:23 -0700322 return HandleUdevCrash(&udev_collector, FLAGS_udev);
Simon Quef70060c2012-04-09 19:07:07 -0700323 }
Steve Fungc490e0f2016-01-04 14:26:15 -0800324#endif
Simon Quef70060c2012-04-09 19:07:07 -0700325
Luigi Semenzato6fdc0b42013-04-11 17:22:13 -0700326 if (FLAGS_kernel_warning) {
327 return HandleKernelWarning(&kernel_warning_collector);
328 }
329
Steve Fungd6169a22014-08-11 15:52:23 -0700330 return HandleUserCrash(&user_collector, FLAGS_user, FLAGS_crash_test);
Ken Mixter03403162010-08-18 15:23:16 -0700331}