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/metrics_library.h" | |
14 | |
15 #pragma GCC diagnostic ignored "-Wstrict-aliasing" | |
16 DEFINE_bool(init, false, "Initialize crash logging"); | |
17 DEFINE_bool(clean_shutdown, false, "Signal clean shutdown"); | |
18 DEFINE_bool(crash_test, false, "Crash test"); | |
19 DEFINE_string(exec, "", "Executable name crashed"); | |
20 DEFINE_int32(pid, -1, "Crashing PID"); | |
21 DEFINE_int32(signal, -1, "Signal causing crash"); | |
22 DEFINE_bool(unclean_check, true, "Check for unclean shutdown"); | |
23 #pragma GCC diagnostic error "-Wstrict-aliasing" | |
24 | |
25 static const char kCrashCounterHistogram[] = "Logging.CrashCounter"; | |
26 static const char kUncleanShutdownFile[] = | |
27 "/var/lib/crash_reporter/pending_clean_shutdown"; | |
28 | |
29 // Enumeration of kinds of crashes to be used in the CrashCounter histogram. | |
30 enum CrashKinds { | |
31 CRASH_KIND_KERNEL = 1, | |
32 CRASH_KIND_USER = 2, | |
33 CRASH_KIND_MAX | |
34 }; | |
35 | |
36 static MetricsLibrary s_metrics_lib; | |
37 static SystemLoggingImpl s_system_log; | |
38 | |
39 static bool IsMetricsCollectionAllowed() { | |
40 // TODO(kmixter): Eventually check system tainted state and | |
41 // move this down in metrics library where it would be explicitly | |
42 // checked when asked to send stats. | |
43 return true; | |
44 } | |
45 | |
46 static void CheckUncleanShutdown() { | |
47 FilePath unclean_file_path(kUncleanShutdownFile); | |
48 if (!file_util::PathExists(unclean_file_path)) { | |
49 return; | |
50 } | |
51 s_system_log.LogWarning("Last shutdown was not clean"); | |
52 if (IsMetricsCollectionAllowed()) { | |
53 s_metrics_lib.SendEnumToUMA(std::string(kCrashCounterHistogram), | |
54 CRASH_KIND_KERNEL, | |
55 CRASH_KIND_MAX); | |
56 } | |
57 if (!file_util::Delete(unclean_file_path, false)) { | |
58 s_system_log.LogError("Failed to delete unclean shutdown file %s", | |
59 kUncleanShutdownFile); | |
60 } | |
61 } | |
62 | |
63 static bool PrepareUncleanShutdownCheck() { | |
64 static const char empty[] = ""; | |
65 FilePath file_path(kUncleanShutdownFile); | |
66 file_util::CreateDirectory(file_path.DirName()); | |
67 return file_util::WriteFile(file_path, empty, 0) == 0; | |
68 } | |
69 | |
70 static void SignalCleanShutdown() { | |
71 s_system_log.LogInfo("Clean shutdown signalled"); | |
72 file_util::Delete(FilePath(kUncleanShutdownFile), false); | |
73 } | |
74 | |
75 static void CountUserCrash() { | |
76 CHECK(IsMetricsCollectionAllowed()); | |
77 s_metrics_lib.SendEnumToUMA(std::string(kCrashCounterHistogram), | |
78 CRASH_KIND_USER, | |
79 CRASH_KIND_MAX); | |
80 | |
81 // Announce through D-Bus whenever a user crash happens. This is | |
82 // used by the metrics daemon to log active use time between | |
83 // crashes. | |
84 // | |
85 // This could be done more efficiently by explicit fork/exec or | |
86 // using a dbus library directly. However, this should run | |
87 // relatively rarely and longer term we may need to implement a | |
88 // better way to do this that doesn't rely on D-Bus. | |
89 int status __attribute__((unused)) = | |
90 system("/usr/bin/dbus-send --type=signal --system / " | |
91 "org.chromium.CrashReporter.UserCrash"); | |
92 } | |
93 | |
94 int main(int argc, char *argv[]) { | |
95 google::ParseCommandLineFlags(&argc, &argv, true); | |
96 FilePath my_path(argv[0]); | |
97 file_util::AbsolutePath(&my_path); | |
98 s_metrics_lib.Init(); | |
99 s_system_log.Initialize(my_path.BaseName().value().c_str()); | |
100 UserCollector user_collector; | |
101 user_collector.Initialize(CountUserCrash, | |
102 my_path.value(), | |
103 IsMetricsCollectionAllowed, | |
104 &s_system_log); | |
105 | |
106 if (FLAGS_init) { | |
107 CHECK(!FLAGS_clean_shutdown) << "Incompatible options"; | |
108 user_collector.Enable(); | |
109 if (FLAGS_unclean_check) { | |
110 CheckUncleanShutdown(); | |
111 if (!PrepareUncleanShutdownCheck()) { | |
112 s_system_log.LogError("Unable to create shutdown check file"); | |
113 } | |
114 } | |
115 return 0; | |
116 } | |
117 | |
118 if (FLAGS_clean_shutdown) { | |
119 SignalCleanShutdown(); | |
120 user_collector.Disable(); | |
121 return 0; | |
122 } | |
123 | |
124 // Handle a specific user space crash. | |
125 CHECK(FLAGS_signal != -1) << "Signal must be set"; | |
126 CHECK(FLAGS_pid != -1) << "PID must be set"; | |
127 CHECK(FLAGS_exec != "") << "Executable name must be set"; | |
128 | |
129 // Make it possible to test what happens when we crash while | |
130 // handling a crash. | |
131 if (FLAGS_crash_test) { | |
132 *(char *)0 = 0; | |
133 return 0; | |
134 } | |
135 | |
136 user_collector.HandleCrash(FLAGS_signal, FLAGS_pid, FLAGS_exec); | |
137 | |
138 return 0; | |
139 } | |
OLD | NEW |