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/user_collector.h" | |
11 #include "metrics/metrics_library.h" | |
12 | |
13 // This procfs file is used to cause kernel core file writing to | |
14 // instead pipe the core file into a user space process. See | |
15 // core(5) man page. | |
16 static const char kCorePatternFile[] = "/proc/sys/kernel/core_pattern"; | |
17 | |
18 UserCollector::UserCollector() | |
19 : core_pattern_file_(kCorePatternFile), | |
20 count_crash_function_(NULL), | |
21 initialized_(false), | |
22 is_feedback_allowed_function_(NULL), | |
23 logger_(NULL) { | |
24 } | |
25 | |
26 void UserCollector::Initialize( | |
27 UserCollector::CountCrashFunction count_crash_function, | |
28 const std::string &our_path, | |
29 UserCollector::IsFeedbackAllowedFunction is_feedback_allowed_function, | |
30 SystemLogging *logger) { | |
31 CHECK(count_crash_function != NULL); | |
32 CHECK(is_feedback_allowed_function != NULL); | |
33 CHECK(logger != NULL); | |
34 | |
35 count_crash_function_ = count_crash_function; | |
36 our_path_ = our_path; | |
37 is_feedback_allowed_function_ = is_feedback_allowed_function; | |
38 logger_ = logger; | |
39 initialized_ = true; | |
40 } | |
41 | |
42 UserCollector::~UserCollector() { | |
43 } | |
44 | |
45 std::string UserCollector::GetPattern(bool enabled) const { | |
46 if (enabled) { | |
47 return StringPrintf("|%s --signal=%%s --pid=%%p --exec=%%e", | |
48 our_path_.c_str()); | |
49 } else { | |
50 return "core"; | |
51 } | |
52 } | |
53 | |
54 bool UserCollector::SetUpInternal(bool enabled) { | |
55 CHECK(initialized_); | |
56 logger_->LogInfo("%s crash handling", enabled ? "Enabling" : "Disabling"); | |
57 std::string pattern = GetPattern(enabled); | |
58 if (file_util::WriteFile(FilePath(core_pattern_file_), | |
59 pattern.c_str(), | |
60 pattern.length()) != | |
61 static_cast<int>(pattern.length())) { | |
62 logger_->LogError("Unable to write %s", core_pattern_file_.c_str()); | |
63 return false; | |
64 } | |
65 return true; | |
66 } | |
67 | |
68 void UserCollector::HandleCrash(int signal, int pid, const std::string &exec) { | |
69 CHECK(initialized_); | |
70 logger_->LogWarning("Received crash notification for %s[%d] sig %d", | |
71 exec.c_str(), pid, signal); | |
72 | |
73 if (is_feedback_allowed_function_()) { | |
74 count_crash_function_(); | |
75 } | |
76 } | |
OLD | NEW |