| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "crash-reporter/kernel_collector.h" |
| 10 #include "crash-reporter/system_logging.h" | 11 #include "crash-reporter/system_logging.h" |
| 12 #include "crash-reporter/unclean_shutdown_collector.h" |
| 11 #include "crash-reporter/user_collector.h" | 13 #include "crash-reporter/user_collector.h" |
| 12 #include "gflags/gflags.h" | 14 #include "gflags/gflags.h" |
| 13 #include "metrics/metrics_library.h" | 15 #include "metrics/metrics_library.h" |
| 14 | 16 |
| 15 #pragma GCC diagnostic ignored "-Wstrict-aliasing" | 17 #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
| 16 DEFINE_bool(init, false, "Initialize crash logging"); | 18 DEFINE_bool(init, false, "Initialize crash logging"); |
| 17 DEFINE_bool(clean_shutdown, false, "Signal clean shutdown"); | 19 DEFINE_bool(clean_shutdown, false, "Signal clean shutdown"); |
| 18 DEFINE_bool(crash_test, false, "Crash test"); | 20 DEFINE_bool(crash_test, false, "Crash test"); |
| 19 DEFINE_int32(pid, -1, "Crashing PID"); | 21 DEFINE_int32(pid, -1, "Crashing PID"); |
| 20 DEFINE_int32(signal, -1, "Signal causing crash"); | 22 DEFINE_int32(signal, -1, "Signal causing crash"); |
| 21 DEFINE_bool(unclean_check, true, "Check for unclean shutdown"); | 23 DEFINE_bool(unclean_check, true, "Check for unclean shutdown"); |
| 22 #pragma GCC diagnostic error "-Wstrict-aliasing" | 24 #pragma GCC diagnostic error "-Wstrict-aliasing" |
| 23 | 25 |
| 24 static const char kCrashCounterHistogram[] = "Logging.CrashCounter"; | 26 static const char kCrashCounterHistogram[] = "Logging.CrashCounter"; |
| 25 static const char kEmpty[] = ""; | 27 static const char kUserCrashSignal[] = |
| 28 "org.chromium.CrashReporter.UserCrash"; |
| 26 static const char kUncleanShutdownFile[] = | 29 static const char kUncleanShutdownFile[] = |
| 27 "/var/lib/crash_reporter/pending_clean_shutdown"; | 30 "/var/lib/crash_reporter/pending_clean_shutdown"; |
| 28 | 31 |
| 29 | 32 |
| 30 // Enumeration of kinds of crashes to be used in the CrashCounter histogram. | 33 // Enumeration of kinds of crashes to be used in the CrashCounter histogram. |
| 31 enum CrashKinds { | 34 enum CrashKinds { |
| 32 kCrashKindKernel = 1, | 35 kCrashKindUncleanShutdown = 1, |
| 33 kCrashKindUser = 2, | 36 kCrashKindUser = 2, |
| 37 kCrashKindKernel = 3, |
| 34 kCrashKindMax | 38 kCrashKindMax |
| 35 }; | 39 }; |
| 36 | 40 |
| 37 static MetricsLibrary s_metrics_lib; | 41 static MetricsLibrary s_metrics_lib; |
| 38 static SystemLoggingImpl s_system_log; | 42 static SystemLoggingImpl s_system_log; |
| 39 | 43 |
| 40 static bool IsMetricsCollectionAllowed() { | 44 static bool IsFeedbackAllowed() { |
| 41 // TODO(kmixter): Eventually check system tainted state and | 45 // Once crosbug.com/5814 is fixed, call the is opted in function |
| 42 // move this down in metrics library where it would be explicitly | 46 // here. |
| 43 // checked when asked to send stats. | |
| 44 return true; | 47 return true; |
| 45 } | 48 } |
| 46 | 49 |
| 47 static void CheckUncleanShutdown() { | 50 static bool TouchFile(const FilePath &file_path) { |
| 48 FilePath unclean_file_path(kUncleanShutdownFile); | 51 return file_util::WriteFile(file_path, "", 0) == 0; |
| 49 if (!file_util::PathExists(unclean_file_path)) { | |
| 50 return; | |
| 51 } | |
| 52 s_system_log.LogWarning("Last shutdown was not clean"); | |
| 53 if (IsMetricsCollectionAllowed()) { | |
| 54 s_metrics_lib.SendEnumToUMA(std::string(kCrashCounterHistogram), | |
| 55 kCrashKindKernel, | |
| 56 kCrashKindMax); | |
| 57 } | |
| 58 if (!file_util::Delete(unclean_file_path, false)) { | |
| 59 s_system_log.LogError("Failed to delete unclean shutdown file %s", | |
| 60 kUncleanShutdownFile); | |
| 61 } | |
| 62 | |
| 63 // Touch a file to notify the metrics daemon that a kernel crash has | |
| 64 // been detected so that it can log the time since the last kernel | |
| 65 // crash. | |
| 66 static const char kKernelCrashDetectedFile[] = "/tmp/kernel-crash-detected"; | |
| 67 FilePath crash_detected(kKernelCrashDetectedFile); | |
| 68 file_util::WriteFile(crash_detected, kEmpty, 0); | |
| 69 } | 52 } |
| 70 | 53 |
| 71 static bool PrepareUncleanShutdownCheck() { | 54 static void CountKernelCrash() { |
| 72 FilePath file_path(kUncleanShutdownFile); | 55 s_metrics_lib.SendEnumToUMA(std::string(kCrashCounterHistogram), |
| 73 file_util::CreateDirectory(file_path.DirName()); | 56 kCrashKindKernel, |
| 74 return file_util::WriteFile(file_path, kEmpty, 0) == 0; | 57 kCrashKindMax); |
| 75 } | 58 } |
| 76 | 59 |
| 77 static void SignalCleanShutdown() { | 60 static void CountUncleanShutdown() { |
| 78 s_system_log.LogInfo("Clean shutdown signalled"); | 61 s_metrics_lib.SendEnumToUMA(std::string(kCrashCounterHistogram), |
| 79 file_util::Delete(FilePath(kUncleanShutdownFile), false); | 62 kCrashKindUncleanShutdown, |
| 63 kCrashKindMax); |
| 80 } | 64 } |
| 81 | 65 |
| 82 static void CountUserCrash() { | 66 static void CountUserCrash() { |
| 83 CHECK(IsMetricsCollectionAllowed()); | |
| 84 s_metrics_lib.SendEnumToUMA(std::string(kCrashCounterHistogram), | 67 s_metrics_lib.SendEnumToUMA(std::string(kCrashCounterHistogram), |
| 85 kCrashKindUser, | 68 kCrashKindUser, |
| 86 kCrashKindMax); | 69 kCrashKindMax); |
| 87 | 70 std::string command = StringPrintf( |
| 71 "/usr/bin/dbus-send --type=signal --system / \"%s\"", |
| 72 kUserCrashSignal); |
| 88 // Announce through D-Bus whenever a user crash happens. This is | 73 // Announce through D-Bus whenever a user crash happens. This is |
| 89 // used by the metrics daemon to log active use time between | 74 // used by the metrics daemon to log active use time between |
| 90 // crashes. | 75 // crashes. |
| 91 // | 76 // |
| 92 // This could be done more efficiently by explicit fork/exec or | 77 // This could be done more efficiently by explicit fork/exec or |
| 93 // using a dbus library directly. However, this should run | 78 // using a dbus library directly. However, this should run |
| 94 // relatively rarely and longer term we may need to implement a | 79 // relatively rarely and longer term we may need to implement a |
| 95 // better way to do this that doesn't rely on D-Bus. | 80 // better way to do this that doesn't rely on D-Bus. |
| 96 int status __attribute__((unused)) = | 81 |
| 97 system("/usr/bin/dbus-send --type=signal --system / " | 82 int status __attribute__((unused)) = system(command.c_str()); |
| 98 "org.chromium.CrashReporter.UserCrash"); | |
| 99 } | 83 } |
| 100 | 84 |
| 101 int main(int argc, char *argv[]) { | 85 static int Initialize(KernelCollector *kernel_collector, |
| 102 google::ParseCommandLineFlags(&argc, &argv, true); | 86 UserCollector *user_collector, |
| 103 FilePath my_path(argv[0]); | 87 UncleanShutdownCollector *unclean_shutdown_collector) { |
| 104 file_util::AbsolutePath(&my_path); | 88 CHECK(!FLAGS_clean_shutdown) << "Incompatible options"; |
| 105 s_metrics_lib.Init(); | |
| 106 s_system_log.Initialize(my_path.BaseName().value().c_str()); | |
| 107 UserCollector user_collector; | |
| 108 user_collector.Initialize(CountUserCrash, | |
| 109 my_path.value(), | |
| 110 IsMetricsCollectionAllowed, | |
| 111 &s_system_log, | |
| 112 true); // generate_diagnostics | |
| 113 | 89 |
| 114 if (FLAGS_init) { | 90 bool was_kernel_crash = false; |
| 115 CHECK(!FLAGS_clean_shutdown) << "Incompatible options"; | 91 bool was_unclean_shutdown = false; |
| 116 user_collector.Enable(); | 92 if (kernel_collector->IsEnabled()) { |
| 117 if (FLAGS_unclean_check) { | 93 was_kernel_crash = kernel_collector->Collect(); |
| 118 CheckUncleanShutdown(); | |
| 119 if (!PrepareUncleanShutdownCheck()) { | |
| 120 s_system_log.LogError("Unable to create shutdown check file"); | |
| 121 } | |
| 122 } | |
| 123 return 0; | |
| 124 } | 94 } |
| 125 | 95 |
| 126 if (FLAGS_clean_shutdown) { | 96 if (FLAGS_unclean_check) { |
| 127 SignalCleanShutdown(); | 97 was_unclean_shutdown = unclean_shutdown_collector->Collect(); |
| 128 user_collector.Disable(); | |
| 129 return 0; | |
| 130 } | 98 } |
| 131 | 99 |
| 100 // Touch a file to notify the metrics daemon that a kernel |
| 101 // crash has been detected so that it can log the time since |
| 102 // the last kernel crash. |
| 103 if (IsFeedbackAllowed()) { |
| 104 if (was_kernel_crash) { |
| 105 TouchFile(FilePath("/tmp/kernel-crash-detected")); |
| 106 } else if (was_unclean_shutdown) { |
| 107 // We only count an unclean shutdown if it did not come with |
| 108 // an associated kernel crash. |
| 109 TouchFile(FilePath("/tmp/unclean-shutdown-detected")); |
| 110 } |
| 111 } |
| 112 |
| 113 // Must enable the unclean shutdown collector *after* collecting. |
| 114 kernel_collector->Enable(); |
| 115 unclean_shutdown_collector->Enable(); |
| 116 user_collector->Enable(); |
| 117 |
| 118 return 0; |
| 119 } |
| 120 |
| 121 static int HandleUserCrash(UserCollector *user_collector) { |
| 132 // Handle a specific user space crash. | 122 // Handle a specific user space crash. |
| 133 CHECK(FLAGS_signal != -1) << "Signal must be set"; | 123 CHECK(FLAGS_signal != -1) << "Signal must be set"; |
| 134 CHECK(FLAGS_pid != -1) << "PID must be set"; | 124 CHECK(FLAGS_pid != -1) << "PID must be set"; |
| 135 | 125 |
| 136 // Make it possible to test what happens when we crash while | 126 // Make it possible to test what happens when we crash while |
| 137 // handling a crash. | 127 // handling a crash. |
| 138 if (FLAGS_crash_test) { | 128 if (FLAGS_crash_test) { |
| 139 *(char *)0 = 0; | 129 *(char *)0 = 0; |
| 140 return 0; | 130 return 0; |
| 141 } | 131 } |
| 142 | 132 |
| 143 // Handle the crash, get the name of the process from procfs. | 133 // Handle the crash, get the name of the process from procfs. |
| 144 if (!user_collector.HandleCrash(FLAGS_signal, FLAGS_pid, NULL)) { | 134 if (!user_collector->HandleCrash(FLAGS_signal, FLAGS_pid, NULL)) { |
| 145 return 1; | 135 return 1; |
| 146 } | 136 } |
| 147 | |
| 148 return 0; | 137 return 0; |
| 149 } | 138 } |
| 139 |
| 140 |
| 141 int main(int argc, char *argv[]) { |
| 142 google::ParseCommandLineFlags(&argc, &argv, true); |
| 143 FilePath my_path(argv[0]); |
| 144 file_util::AbsolutePath(&my_path); |
| 145 s_metrics_lib.Init(); |
| 146 s_system_log.Initialize(my_path.BaseName().value().c_str()); |
| 147 KernelCollector kernel_collector; |
| 148 kernel_collector.Initialize(CountKernelCrash, |
| 149 IsFeedbackAllowed, |
| 150 &s_system_log); |
| 151 UserCollector user_collector; |
| 152 user_collector.Initialize(CountUserCrash, |
| 153 my_path.value(), |
| 154 IsFeedbackAllowed, |
| 155 &s_system_log, |
| 156 true); // generate_diagnostics |
| 157 UncleanShutdownCollector unclean_shutdown_collector; |
| 158 unclean_shutdown_collector.Initialize(CountUncleanShutdown, |
| 159 IsFeedbackAllowed, |
| 160 &s_system_log); |
| 161 |
| 162 if (FLAGS_init) { |
| 163 return Initialize(&kernel_collector, |
| 164 &user_collector, |
| 165 &unclean_shutdown_collector); |
| 166 } |
| 167 |
| 168 if (FLAGS_clean_shutdown) { |
| 169 unclean_shutdown_collector.Disable(); |
| 170 user_collector.Disable(); |
| 171 return 0; |
| 172 } |
| 173 |
| 174 return HandleUserCrash(&user_collector); |
| 175 } |
| OLD | NEW |