OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <string> |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" |
| 10 #include "crash/system_logging.h" |
| 11 #include "crash/user_collector.h" |
| 12 #include "gflags/gflags.h" |
| 13 #include "metrics_library.h" |
| 14 |
| 15 DEFINE_bool(init, false, "Initialize crash logging"); |
| 16 DEFINE_bool(clean_shutdown, false, "Signal clean shutdown"); |
| 17 DEFINE_string(exec, "", "Executable name crashed"); |
| 18 DEFINE_int32(pid, -1, "Crashing PID"); |
| 19 DEFINE_int32(signal, -1, "Signal causing crash"); |
| 20 DEFINE_bool(unclean_check, true, "Check for unclean shutdown"); |
| 21 |
| 22 static const char kCrashCounterHistogram[] = "Logging.CrashCounter"; |
| 23 static const char kUncleanShutdownFile[] = |
| 24 "/var/lib/crash_reporter/pending_clean_shutdown"; |
| 25 |
| 26 // Enumeration of kinds of crashes to be used in the CrashCounter histogram. |
| 27 enum CrashKinds { |
| 28 CRASH_KIND_KERNEL = 1, |
| 29 CRASH_KIND_USER = 2, |
| 30 CRASH_KIND_MAX |
| 31 }; |
| 32 |
| 33 static SystemLoggingImpl s_system_log; |
| 34 |
| 35 static bool IsMetricsCollectionAllowed() { |
| 36 // TODO(kmixter): Eventually check system tainted state and |
| 37 // move this down in metrics library where it would be explicitly |
| 38 // checked when asked to send stats. |
| 39 return true; |
| 40 } |
| 41 |
| 42 static void CheckUncleanShutdown() { |
| 43 FilePath unclean_file_path(kUncleanShutdownFile); |
| 44 if (!file_util::PathExists(unclean_file_path)) { |
| 45 return; |
| 46 } |
| 47 s_system_log.LogWarning("Last shutdown was not clean"); |
| 48 if (IsMetricsCollectionAllowed()) { |
| 49 MetricsLibrary::SendEnumToChrome(std::string(kCrashCounterHistogram), |
| 50 CRASH_KIND_KERNEL, |
| 51 CRASH_KIND_MAX); |
| 52 } |
| 53 if (!file_util::Delete(unclean_file_path, false)) { |
| 54 s_system_log.LogError("Failed to delete unclean shutdown file %s", |
| 55 kUncleanShutdownFile); |
| 56 } |
| 57 } |
| 58 |
| 59 static bool PrepareUncleanShutdownCheck() { |
| 60 static const char empty[] = ""; |
| 61 FilePath file_path(kUncleanShutdownFile); |
| 62 file_util::CreateDirectory(file_path.DirName()); |
| 63 return file_util::WriteFile(file_path, empty, 0) == 0; |
| 64 } |
| 65 |
| 66 static void SignalCleanShutdown() { |
| 67 s_system_log.LogInfo("Clean shutdown signalled"); |
| 68 file_util::Delete(FilePath(kUncleanShutdownFile), false); |
| 69 } |
| 70 |
| 71 static void CountUserCrash() { |
| 72 CHECK(IsMetricsCollectionAllowed()); |
| 73 MetricsLibrary::SendEnumToChrome(std::string(kCrashCounterHistogram), |
| 74 CRASH_KIND_USER, |
| 75 CRASH_KIND_MAX); |
| 76 } |
| 77 |
| 78 int main(int argc, char *argv[]) { |
| 79 google::ParseCommandLineFlags(&argc, &argv, true); |
| 80 FilePath my_path(argv[0]); |
| 81 file_util::AbsolutePath(&my_path); |
| 82 s_system_log.Initialize(my_path.BaseName().value().c_str()); |
| 83 UserCollector user_collector; |
| 84 user_collector.Initialize(CountUserCrash, |
| 85 my_path.value(), |
| 86 IsMetricsCollectionAllowed, |
| 87 &s_system_log); |
| 88 |
| 89 if (FLAGS_init) { |
| 90 CHECK(!FLAGS_clean_shutdown) << "Incompatible options"; |
| 91 user_collector.Enable(); |
| 92 if (FLAGS_unclean_check) { |
| 93 CheckUncleanShutdown(); |
| 94 if (!PrepareUncleanShutdownCheck()) { |
| 95 s_system_log.LogError("Unable to create shutdown check file"); |
| 96 } |
| 97 } |
| 98 return 0; |
| 99 } |
| 100 |
| 101 if (FLAGS_clean_shutdown) { |
| 102 SignalCleanShutdown(); |
| 103 user_collector.Disable(); |
| 104 return 0; |
| 105 } |
| 106 |
| 107 // Handle a specific user space crash. |
| 108 CHECK(FLAGS_signal != -1) << "Signal must be set"; |
| 109 CHECK(FLAGS_pid != -1) << "PID must be set"; |
| 110 CHECK(FLAGS_exec != "") << "Executable name must be set"; |
| 111 |
| 112 user_collector.HandleCrash(FLAGS_signal, FLAGS_pid, FLAGS_exec); |
| 113 |
| 114 return 0; |
| 115 } |
OLD | NEW |