OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium 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 #import "components/crash/content/app/breakpad_mac.h" | 5 #import "components/crash/content/app/breakpad_mac.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 base::SysUTF8ToNSString(value.as_string())); | 66 base::SysUTF8ToNSString(value.as_string())); |
67 } | 67 } |
68 } | 68 } |
69 | 69 |
70 void ClearCrashKeyValueImpl(const base::StringPiece& key) { | 70 void ClearCrashKeyValueImpl(const base::StringPiece& key) { |
71 @autoreleasepool { | 71 @autoreleasepool { |
72 ClearCrashKeyValue(base::SysUTF8ToNSString(key.as_string())); | 72 ClearCrashKeyValue(base::SysUTF8ToNSString(key.as_string())); |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 bool FatalMessageHandler(int severity, const char* file, int line, | 76 class FatalMessageListener : logging::LogMessageListener { |
77 size_t message_start, const std::string& str) { | 77 void OnMessage(int severity, |
| 78 const char* file, |
| 79 int line, |
| 80 size_t message_start, |
| 81 const std::string& str) override; |
| 82 }; |
| 83 |
| 84 void FatalMessageListener::OnMessage(int severity, |
| 85 const char* file, |
| 86 int line, |
| 87 size_t message_start, |
| 88 const std::string& str) { |
78 // Do not handle non-FATAL. | 89 // Do not handle non-FATAL. |
79 if (severity != logging::LOG_FATAL) | 90 if (severity != logging::LOG_FATAL) |
80 return false; | 91 return; |
81 | 92 |
82 // In case of OOM condition, this code could be reentered when | 93 // In case of OOM condition, this code could be reentered when |
83 // constructing and storing the key. Using a static is not | 94 // constructing and storing the key. Using a static is not |
84 // thread-safe, but if multiple threads are in the process of a | 95 // thread-safe, but if multiple threads are in the process of a |
85 // fatal crash at the same time, this should work. | 96 // fatal crash at the same time, this should work. |
86 static bool guarded = false; | 97 static bool guarded = false; |
87 if (guarded) | 98 if (guarded) |
88 return false; | 99 return; |
89 | 100 |
90 base::AutoReset<bool> guard(&guarded, true); | 101 base::AutoReset<bool> guard(&guarded, true); |
91 | 102 |
92 // Only log last path component. This matches logging.cc. | 103 // Only log last path component. This matches logging.cc. |
93 if (file) { | 104 if (file) { |
94 const char* slash = strrchr(file, '/'); | 105 const char* slash = strrchr(file, '/'); |
95 if (slash) | 106 if (slash) |
96 file = slash + 1; | 107 file = slash + 1; |
97 } | 108 } |
98 | 109 |
99 NSString* fatal_key = @"LOG_FATAL"; | 110 NSString* fatal_key = @"LOG_FATAL"; |
100 NSString* fatal_value = | 111 NSString* fatal_value = |
101 [NSString stringWithFormat:@"%s:%d: %s", | 112 [NSString stringWithFormat:@"%s:%d: %s", |
102 file, line, str.c_str() + message_start]; | 113 file, line, str.c_str() + message_start]; |
103 SetCrashKeyValue(fatal_key, fatal_value); | 114 SetCrashKeyValue(fatal_key, fatal_value); |
104 | 115 |
105 // Rather than including the code to force the crash here, allow the | 116 // Rather than including the code to force the crash here, allow the |
106 // caller to do it. | 117 // caller to do it. |
107 return false; | 118 return; |
108 } | 119 } |
109 | 120 |
110 // BreakpadGenerateAndSendReport() does not report the current | 121 // BreakpadGenerateAndSendReport() does not report the current |
111 // thread. This class can be used to spin up a thread to run it. | 122 // thread. This class can be used to spin up a thread to run it. |
112 class DumpHelper : public base::PlatformThread::Delegate { | 123 class DumpHelper : public base::PlatformThread::Delegate { |
113 public: | 124 public: |
114 static void DumpWithoutCrashing() { | 125 static void DumpWithoutCrashing() { |
115 DumpHelper dumper; | 126 DumpHelper dumper; |
116 base::PlatformThreadHandle handle; | 127 base::PlatformThreadHandle handle; |
117 if (base::PlatformThread::Create(0, &dumper, &handle)) { | 128 if (base::PlatformThread::Create(0, &dumper, &handle)) { |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 base::debug::SetCrashKeyReportingFunctions(&SetCrashKeyValueImpl, | 268 base::debug::SetCrashKeyReportingFunctions(&SetCrashKeyValueImpl, |
258 &ClearCrashKeyValueImpl); | 269 &ClearCrashKeyValueImpl); |
259 GetCrashReporterClient()->RegisterCrashKeys(); | 270 GetCrashReporterClient()->RegisterCrashKeys(); |
260 | 271 |
261 // Set Breakpad metadata values. These values are added to Info.plist during | 272 // Set Breakpad metadata values. These values are added to Info.plist during |
262 // the branded Google Chrome.app build. | 273 // the branded Google Chrome.app build. |
263 SetCrashKeyValue(@"ver", [info_dictionary objectForKey:@BREAKPAD_VERSION]); | 274 SetCrashKeyValue(@"ver", [info_dictionary objectForKey:@BREAKPAD_VERSION]); |
264 SetCrashKeyValue(@"prod", [info_dictionary objectForKey:@BREAKPAD_PRODUCT]); | 275 SetCrashKeyValue(@"prod", [info_dictionary objectForKey:@BREAKPAD_PRODUCT]); |
265 SetCrashKeyValue(@"plat", @"OS X"); | 276 SetCrashKeyValue(@"plat", @"OS X"); |
266 | 277 |
267 logging::SetLogMessageHandler(&FatalMessageHandler); | 278 // Intentionally leak the listener. |
| 279 new FatalMessageListener(); |
| 280 |
268 base::debug::SetDumpWithoutCrashingFunction(&DumpHelper::DumpWithoutCrashing); | 281 base::debug::SetDumpWithoutCrashingFunction(&DumpHelper::DumpWithoutCrashing); |
269 | 282 |
270 // abort() sends SIGABRT, which breakpad does not intercept. | 283 // abort() sends SIGABRT, which breakpad does not intercept. |
271 // Register a signal handler to crash in a way breakpad will | 284 // Register a signal handler to crash in a way breakpad will |
272 // intercept. | 285 // intercept. |
273 struct sigaction sigact; | 286 struct sigaction sigact; |
274 memset(&sigact, 0, sizeof(sigact)); | 287 memset(&sigact, 0, sizeof(sigact)); |
275 sigact.sa_handler = SIGABRTHandler; | 288 sigact.sa_handler = SIGABRTHandler; |
276 CHECK(0 == sigaction(SIGABRT, &sigact, NULL)); | 289 CHECK(0 == sigaction(SIGABRT, &sigact, NULL)); |
277 } | 290 } |
(...skipping 11 matching lines...) Expand all Loading... |
289 | 302 |
290 // Store process type in crash dump. | 303 // Store process type in crash dump. |
291 SetCrashKeyValue(@"ptype", process_type); | 304 SetCrashKeyValue(@"ptype", process_type); |
292 | 305 |
293 NSString* pid_value = | 306 NSString* pid_value = |
294 [NSString stringWithFormat:@"%d", static_cast<unsigned int>(getpid())]; | 307 [NSString stringWithFormat:@"%d", static_cast<unsigned int>(getpid())]; |
295 SetCrashKeyValue(@"pid", pid_value); | 308 SetCrashKeyValue(@"pid", pid_value); |
296 } | 309 } |
297 | 310 |
298 } // namespace breakpad | 311 } // namespace breakpad |
OLD | NEW |