Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: crash_reporter.cc

Issue 6517001: crash-reporter: Use standard logging and new libchromeos Process code (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crash-reporter.git@master
Patch Set: More comments Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « crash_collector_test.cc ('k') | kernel_collector.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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) populated. We populate them here.
186 static void OpenStandardFileDescriptors() {
187 int new_fd = -1;
188 // We open /dev/null to fill in any of the standard [0, 2] file
189 // descriptors. We leave these open for the duration of the
190 // process. This works because open returns the lowest numbered
191 // invalid fd.
192 do {
193 new_fd = open("/dev/null", 0);
194 CHECK(new_fd >= 0) << "Unable to open /dev/null";
195 } while (new_fd >= 0 && new_fd <= 2);
196 close(new_fd);
197 }
198
181 int main(int argc, char *argv[]) { 199 int main(int argc, char *argv[]) {
200 OpenStandardFileDescriptors();
182 google::ParseCommandLineFlags(&argc, &argv, true); 201 google::ParseCommandLineFlags(&argc, &argv, true);
183 FilePath my_path(argv[0]); 202 FilePath my_path(argv[0]);
184 file_util::AbsolutePath(&my_path); 203 file_util::AbsolutePath(&my_path);
185 s_metrics_lib.Init(); 204 s_metrics_lib.Init();
186 CommandLine::Init(argc, argv); 205 CommandLine::Init(argc, argv);
187 logging::InitLogging(NULL, 206 chromeos::OpenLog(my_path.BaseName().value().c_str(), true);
188 logging::LOG_ONLY_TO_SYSTEM_DEBUG_LOG, 207 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; 208 KernelCollector kernel_collector;
193 kernel_collector.Initialize(CountKernelCrash, 209 kernel_collector.Initialize(CountKernelCrash,
194 IsFeedbackAllowed, 210 IsFeedbackAllowed);
195 &s_system_log);
196 UserCollector user_collector; 211 UserCollector user_collector;
197 user_collector.Initialize(CountUserCrash, 212 user_collector.Initialize(CountUserCrash,
198 my_path.value(), 213 my_path.value(),
199 IsFeedbackAllowed, 214 IsFeedbackAllowed,
200 &s_system_log,
201 true); // generate_diagnostics 215 true); // generate_diagnostics
202 UncleanShutdownCollector unclean_shutdown_collector; 216 UncleanShutdownCollector unclean_shutdown_collector;
203 unclean_shutdown_collector.Initialize(CountUncleanShutdown, 217 unclean_shutdown_collector.Initialize(CountUncleanShutdown,
204 IsFeedbackAllowed, 218 IsFeedbackAllowed);
205 &s_system_log);
206 219
207 if (FLAGS_init) { 220 if (FLAGS_init) {
208 return Initialize(&kernel_collector, 221 return Initialize(&kernel_collector,
209 &user_collector, 222 &user_collector,
210 &unclean_shutdown_collector); 223 &unclean_shutdown_collector);
211 } 224 }
212 225
213 if (FLAGS_clean_shutdown) { 226 if (FLAGS_clean_shutdown) {
214 unclean_shutdown_collector.Disable(); 227 unclean_shutdown_collector.Disable();
215 user_collector.Disable(); 228 user_collector.Disable();
216 return 0; 229 return 0;
217 } 230 }
218 231
219 if (!FLAGS_generate_kernel_signature.empty()) { 232 if (!FLAGS_generate_kernel_signature.empty()) {
220 return GenerateKernelSignature(&kernel_collector); 233 return GenerateKernelSignature(&kernel_collector);
221 } 234 }
222 235
223 return HandleUserCrash(&user_collector); 236 return HandleUserCrash(&user_collector);
224 } 237 }
OLDNEW
« no previous file with comments | « crash_collector_test.cc ('k') | kernel_collector.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698