Chromium Code Reviews| 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 <fcntl.h> // for open | |
| 6 | |
| 5 #include <string> | 7 #include <string> |
| 6 | 8 |
| 7 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "chromeos/syslog_logging.h" | |
| 11 #include "crash-reporter/kernel_collector.h" | 14 #include "crash-reporter/kernel_collector.h" |
| 12 #include "crash-reporter/system_logging.h" | |
| 13 #include "crash-reporter/unclean_shutdown_collector.h" | 15 #include "crash-reporter/unclean_shutdown_collector.h" |
| 14 #include "crash-reporter/user_collector.h" | 16 #include "crash-reporter/user_collector.h" |
| 15 #include "gflags/gflags.h" | 17 #include "gflags/gflags.h" |
| 16 #include "metrics/metrics_library.h" | 18 #include "metrics/metrics_library.h" |
| 17 | 19 |
| 18 #pragma GCC diagnostic ignored "-Wstrict-aliasing" | 20 #pragma GCC diagnostic ignored "-Wstrict-aliasing" |
| 19 DEFINE_bool(init, false, "Initialize crash logging"); | 21 DEFINE_bool(init, false, "Initialize crash logging"); |
| 20 DEFINE_bool(clean_shutdown, false, "Signal clean shutdown"); | 22 DEFINE_bool(clean_shutdown, false, "Signal clean shutdown"); |
| 21 DEFINE_string(generate_kernel_signature, "", | 23 DEFINE_string(generate_kernel_signature, "", |
| 22 "Generate signature from given kcrash file"); | 24 "Generate signature from given kcrash file"); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 34 | 36 |
| 35 // Enumeration of kinds of crashes to be used in the CrashCounter histogram. | 37 // Enumeration of kinds of crashes to be used in the CrashCounter histogram. |
| 36 enum CrashKinds { | 38 enum CrashKinds { |
| 37 kCrashKindUncleanShutdown = 1, | 39 kCrashKindUncleanShutdown = 1, |
| 38 kCrashKindUser = 2, | 40 kCrashKindUser = 2, |
| 39 kCrashKindKernel = 3, | 41 kCrashKindKernel = 3, |
| 40 kCrashKindMax | 42 kCrashKindMax |
| 41 }; | 43 }; |
| 42 | 44 |
| 43 static MetricsLibrary s_metrics_lib; | 45 static MetricsLibrary s_metrics_lib; |
| 44 static SystemLoggingImpl s_system_log; | |
| 45 | 46 |
| 46 static bool IsFeedbackAllowed() { | 47 static bool IsFeedbackAllowed() { |
| 47 return s_metrics_lib.AreMetricsEnabled(); | 48 return s_metrics_lib.AreMetricsEnabled(); |
| 48 } | 49 } |
| 49 | 50 |
| 50 static bool TouchFile(const FilePath &file_path) { | 51 static bool TouchFile(const FilePath &file_path) { |
| 51 return file_util::WriteFile(file_path, "", 0) == 0; | 52 return file_util::WriteFile(file_path, "", 0) == 0; |
| 52 } | 53 } |
| 53 | 54 |
| 54 static void CountKernelCrash() { | 55 static void CountKernelCrash() { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 // better way to do this that doesn't rely on D-Bus. | 90 // better way to do this that doesn't rely on D-Bus. |
| 90 // | 91 // |
| 91 // We run in the background in case dbus daemon itself is crashed | 92 // We run in the background in case dbus daemon itself is crashed |
| 92 // and not responding. This allows us to not block and potentially | 93 // and not responding. This allows us to not block and potentially |
| 93 // deadlock on a dbus-daemon crash. If dbus-daemon crashes without | 94 // deadlock on a dbus-daemon crash. If dbus-daemon crashes without |
| 94 // restarting, each crash will fork off a lot of dbus-send | 95 // restarting, each crash will fork off a lot of dbus-send |
| 95 // processes. Such a system is in a unusable state and will need | 96 // processes. Such a system is in a unusable state and will need |
| 96 // to be restarted anyway. | 97 // to be restarted anyway. |
| 97 | 98 |
| 98 int status = system(command.c_str()); | 99 int status = system(command.c_str()); |
| 99 if (status != 0) { | 100 LOG_IF(WARNING, status != 0) << "dbus-send running failed"; |
| 100 s_system_log.LogWarning("dbus-send running failed"); | |
| 101 } | |
| 102 } | 101 } |
| 103 | 102 |
| 104 static int Initialize(KernelCollector *kernel_collector, | 103 static int Initialize(KernelCollector *kernel_collector, |
| 105 UserCollector *user_collector, | 104 UserCollector *user_collector, |
| 106 UncleanShutdownCollector *unclean_shutdown_collector) { | 105 UncleanShutdownCollector *unclean_shutdown_collector) { |
| 107 CHECK(!FLAGS_clean_shutdown) << "Incompatible options"; | 106 CHECK(!FLAGS_clean_shutdown) << "Incompatible options"; |
| 108 | 107 |
| 109 bool was_kernel_crash = false; | 108 bool was_kernel_crash = false; |
| 110 bool was_unclean_shutdown = false; | 109 bool was_unclean_shutdown = false; |
| 111 kernel_collector->Enable(); | 110 kernel_collector->Enable(); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 142 CHECK(!FLAGS_user.empty()) << "--user= must be set"; | 141 CHECK(!FLAGS_user.empty()) << "--user= must be set"; |
| 143 | 142 |
| 144 // Make it possible to test what happens when we crash while | 143 // Make it possible to test what happens when we crash while |
| 145 // handling a crash. | 144 // handling a crash. |
| 146 if (FLAGS_crash_test) { | 145 if (FLAGS_crash_test) { |
| 147 *(char *)0 = 0; | 146 *(char *)0 = 0; |
| 148 return 0; | 147 return 0; |
| 149 } | 148 } |
| 150 | 149 |
| 151 // Accumulate logs to help in diagnosing failures during user collection. | 150 // Accumulate logs to help in diagnosing failures during user collection. |
| 152 s_system_log.set_accumulating(true); | 151 chromeos::LogToString(true); |
| 153 // Handle the crash, get the name of the process from procfs. | 152 // Handle the crash, get the name of the process from procfs. |
| 154 bool handled = user_collector->HandleCrash(FLAGS_user, NULL); | 153 bool handled = user_collector->HandleCrash(FLAGS_user, NULL); |
| 155 s_system_log.set_accumulating(false); | 154 chromeos::LogToString(false); |
| 156 if (!handled) | 155 if (!handled) |
| 157 return 1; | 156 return 1; |
| 158 return 0; | 157 return 0; |
| 159 } | 158 } |
| 160 | 159 |
| 161 // Interactive/diagnostics mode for generating kernel crash signatures. | 160 // Interactive/diagnostics mode for generating kernel crash signatures. |
| 162 static int GenerateKernelSignature(KernelCollector *kernel_collector) { | 161 static int GenerateKernelSignature(KernelCollector *kernel_collector) { |
| 163 std::string kcrash_contents; | 162 std::string kcrash_contents; |
| 164 std::string signature; | 163 std::string signature; |
| 165 if (!file_util::ReadFileToString(FilePath(FLAGS_generate_kernel_signature), | 164 if (!file_util::ReadFileToString(FilePath(FLAGS_generate_kernel_signature), |
| 166 &kcrash_contents)) { | 165 &kcrash_contents)) { |
| 167 fprintf(stderr, "Could not read file.\n"); | 166 fprintf(stderr, "Could not read file.\n"); |
| 168 return 1; | 167 return 1; |
| 169 } | 168 } |
| 170 if (!kernel_collector->ComputeKernelStackSignature( | 169 if (!kernel_collector->ComputeKernelStackSignature( |
| 171 kcrash_contents, | 170 kcrash_contents, |
| 172 &signature, | 171 &signature, |
| 173 true)) { | 172 true)) { |
| 174 fprintf(stderr, "Signature could not be generated.\n"); | 173 fprintf(stderr, "Signature could not be generated.\n"); |
| 175 return 1; | 174 return 1; |
| 176 } | 175 } |
| 177 printf("Kernel crash signature is \"%s\".\n", signature.c_str()); | 176 printf("Kernel crash signature is \"%s\".\n", signature.c_str()); |
| 178 return 0; | 177 return 0; |
| 179 } | 178 } |
| 180 | 179 |
| 180 // Ensure stdout, stdin, and stderr are open file descriptors. If | |
| 181 // they are not, any code which writes to stderr/stdout may write out | |
| 182 // to files opened during execution. In particular, when | |
| 183 // crash_reporter is run by the kernel coredump pipe handler (via | |
| 184 // kthread_create/kernel_execve), it will not have file table entries | |
| 185 // 1 and 2 (stdout and stderr). | |
| 186 static void OpenStandardFileDescriptors() { | |
| 187 int new_fd = -1; | |
| 188 do { | |
| 189 new_fd = open("/dev/null", 0); | |
| 190 CHECK(new_fd >= 0) << "Unable to open /dev/null"; | |
| 191 } while (new_fd >= 0 && new_fd <= 2); | |
| 192 close(new_fd); | |
|
petkov
2011/03/02 17:53:20
how does this work? don't you need to close inside
kmixter1
2011/03/02 19:08:58
Does the comment make sense now?
| |
| 193 } | |
| 194 | |
| 181 int main(int argc, char *argv[]) { | 195 int main(int argc, char *argv[]) { |
| 196 OpenStandardFileDescriptors(); | |
| 182 google::ParseCommandLineFlags(&argc, &argv, true); | 197 google::ParseCommandLineFlags(&argc, &argv, true); |
| 183 FilePath my_path(argv[0]); | 198 FilePath my_path(argv[0]); |
| 184 file_util::AbsolutePath(&my_path); | 199 file_util::AbsolutePath(&my_path); |
| 185 s_metrics_lib.Init(); | 200 s_metrics_lib.Init(); |
| 186 CommandLine::Init(argc, argv); | 201 CommandLine::Init(argc, argv); |
| 187 logging::InitLogging(NULL, | 202 chromeos::OpenLog(my_path.BaseName().value().c_str(), true); |
| 188 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, | 203 chromeos::InitLog(chromeos::kLogToSyslog); |
| 189 logging::DONT_LOCK_LOG_FILE, | |
| 190 logging::DELETE_OLD_LOG_FILE); | |
| 191 s_system_log.Initialize(my_path.BaseName().value().c_str()); | |
| 192 KernelCollector kernel_collector; | 204 KernelCollector kernel_collector; |
| 193 kernel_collector.Initialize(CountKernelCrash, | 205 kernel_collector.Initialize(CountKernelCrash, |
| 194 IsFeedbackAllowed, | 206 IsFeedbackAllowed); |
| 195 &s_system_log); | |
| 196 UserCollector user_collector; | 207 UserCollector user_collector; |
| 197 user_collector.Initialize(CountUserCrash, | 208 user_collector.Initialize(CountUserCrash, |
| 198 my_path.value(), | 209 my_path.value(), |
| 199 IsFeedbackAllowed, | 210 IsFeedbackAllowed, |
| 200 &s_system_log, | |
| 201 true); // generate_diagnostics | 211 true); // generate_diagnostics |
| 202 UncleanShutdownCollector unclean_shutdown_collector; | 212 UncleanShutdownCollector unclean_shutdown_collector; |
| 203 unclean_shutdown_collector.Initialize(CountUncleanShutdown, | 213 unclean_shutdown_collector.Initialize(CountUncleanShutdown, |
| 204 IsFeedbackAllowed, | 214 IsFeedbackAllowed); |
| 205 &s_system_log); | |
| 206 | 215 |
| 207 if (FLAGS_init) { | 216 if (FLAGS_init) { |
| 208 return Initialize(&kernel_collector, | 217 return Initialize(&kernel_collector, |
| 209 &user_collector, | 218 &user_collector, |
| 210 &unclean_shutdown_collector); | 219 &unclean_shutdown_collector); |
| 211 } | 220 } |
| 212 | 221 |
| 213 if (FLAGS_clean_shutdown) { | 222 if (FLAGS_clean_shutdown) { |
| 214 unclean_shutdown_collector.Disable(); | 223 unclean_shutdown_collector.Disable(); |
| 215 user_collector.Disable(); | 224 user_collector.Disable(); |
| 216 return 0; | 225 return 0; |
| 217 } | 226 } |
| 218 | 227 |
| 219 if (!FLAGS_generate_kernel_signature.empty()) { | 228 if (!FLAGS_generate_kernel_signature.empty()) { |
| 220 return GenerateKernelSignature(&kernel_collector); | 229 return GenerateKernelSignature(&kernel_collector); |
| 221 } | 230 } |
| 222 | 231 |
| 223 return HandleUserCrash(&user_collector); | 232 return HandleUserCrash(&user_collector); |
| 224 } | 233 } |
| OLD | NEW |