OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "components/crash/content/app/crashpad.h" | 5 #include "components/crash/content/app/crashpad.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 | 45 |
46 void SetCrashKeyValue(const base::StringPiece& key, | 46 void SetCrashKeyValue(const base::StringPiece& key, |
47 const base::StringPiece& value) { | 47 const base::StringPiece& value) { |
48 g_simple_string_dictionary->SetKeyValue(key.data(), value.data()); | 48 g_simple_string_dictionary->SetKeyValue(key.data(), value.data()); |
49 } | 49 } |
50 | 50 |
51 void ClearCrashKey(const base::StringPiece& key) { | 51 void ClearCrashKey(const base::StringPiece& key) { |
52 g_simple_string_dictionary->RemoveKey(key.data()); | 52 g_simple_string_dictionary->RemoveKey(key.data()); |
53 } | 53 } |
54 | 54 |
55 bool LogMessageHandler(int severity, | 55 class LogMessageListener : logging::LogMessageListener { |
56 const char* file, | 56 void OnMessage(int severity, |
57 int line, | 57 const char* file, |
58 size_t message_start, | 58 int line, |
59 const std::string& string) { | 59 size_t message_start, |
| 60 const std::string& string) override; |
| 61 }; |
| 62 |
| 63 void LogMessageListener::OnMessage(int severity, |
| 64 const char* file, |
| 65 int line, |
| 66 size_t message_start, |
| 67 const std::string& string) { |
60 // Only handle FATAL. | 68 // Only handle FATAL. |
61 if (severity != logging::LOG_FATAL) { | 69 if (severity != logging::LOG_FATAL) { |
62 return false; | 70 return; |
63 } | 71 } |
64 | 72 |
65 // In case of an out-of-memory condition, this code could be reentered when | 73 // In case of an out-of-memory condition, this code could be reentered when |
66 // constructing and storing the key. Using a static is not thread-safe, but if | 74 // constructing and storing the key. Using a static is not thread-safe, but if |
67 // multiple threads are in the process of a fatal crash at the same time, this | 75 // multiple threads are in the process of a fatal crash at the same time, this |
68 // should work. | 76 // should work. |
69 static bool guarded = false; | 77 static bool guarded = false; |
70 if (guarded) { | 78 if (guarded) { |
71 return false; | 79 return; |
72 } | 80 } |
73 base::AutoReset<bool> guard(&guarded, true); | 81 base::AutoReset<bool> guard(&guarded, true); |
74 | 82 |
75 // Only log last path component. This matches logging.cc. | 83 // Only log last path component. This matches logging.cc. |
76 if (file) { | 84 if (file) { |
77 const char* slash = strrchr(file, '/'); | 85 const char* slash = strrchr(file, '/'); |
78 if (slash) { | 86 if (slash) { |
79 file = slash + 1; | 87 file = slash + 1; |
80 } | 88 } |
81 } | 89 } |
82 | 90 |
83 CHECK_LE(message_start, string.size()); | 91 CHECK_LE(message_start, string.size()); |
84 std::string message = base::StringPrintf("%s:%d: %s", file, line, | 92 std::string message = base::StringPrintf("%s:%d: %s", file, line, |
85 string.c_str() + message_start); | 93 string.c_str() + message_start); |
86 SetCrashKeyValue("LOG_FATAL", message); | 94 SetCrashKeyValue("LOG_FATAL", message); |
87 | 95 |
88 // Rather than including the code to force the crash here, allow the caller to | 96 // Rather than including the code to force the crash here, allow the caller to |
89 // do it. | 97 // do it. |
90 return false; | |
91 } | 98 } |
92 | 99 |
93 void DumpWithoutCrashing() { | 100 void DumpWithoutCrashing() { |
94 CRASHPAD_SIMULATE_CRASH(); | 101 CRASHPAD_SIMULATE_CRASH(); |
95 } | 102 } |
96 | 103 |
97 void InitializeCrashpadImpl(bool initial_client, | 104 void InitializeCrashpadImpl(bool initial_client, |
98 const std::string& process_type, | 105 const std::string& process_type, |
99 bool embedded_handler) { | 106 bool embedded_handler) { |
100 static bool initialized = false; | 107 static bool initialized = false; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 crash_reporter_client->RegisterCrashKeys(); | 164 crash_reporter_client->RegisterCrashKeys(); |
158 | 165 |
159 SetCrashKeyValue("ptype", browser_process ? base::StringPiece("browser") | 166 SetCrashKeyValue("ptype", browser_process ? base::StringPiece("browser") |
160 : base::StringPiece(process_type)); | 167 : base::StringPiece(process_type)); |
161 #if defined(OS_POSIX) | 168 #if defined(OS_POSIX) |
162 SetCrashKeyValue("pid", base::IntToString(getpid())); | 169 SetCrashKeyValue("pid", base::IntToString(getpid())); |
163 #elif defined(OS_WIN) | 170 #elif defined(OS_WIN) |
164 SetCrashKeyValue("pid", base::IntToString(::GetCurrentProcessId())); | 171 SetCrashKeyValue("pid", base::IntToString(::GetCurrentProcessId())); |
165 #endif | 172 #endif |
166 | 173 |
167 logging::SetLogMessageHandler(LogMessageHandler); | 174 // Intentionally leak the listener. |
| 175 new LogMessageListener(); |
168 | 176 |
169 // If clients called CRASHPAD_SIMULATE_CRASH() instead of | 177 // If clients called CRASHPAD_SIMULATE_CRASH() instead of |
170 // base::debug::DumpWithoutCrashing(), these dumps would appear as crashes in | 178 // base::debug::DumpWithoutCrashing(), these dumps would appear as crashes in |
171 // the correct function, at the correct file and line. This would be | 179 // the correct function, at the correct file and line. This would be |
172 // preferable to having all occurrences show up in DumpWithoutCrashing() at | 180 // preferable to having all occurrences show up in DumpWithoutCrashing() at |
173 // the same file and line. | 181 // the same file and line. |
174 base::debug::SetDumpWithoutCrashingFunction(DumpWithoutCrashing); | 182 base::debug::SetDumpWithoutCrashingFunction(DumpWithoutCrashing); |
175 | 183 |
176 #if defined(OS_MACOSX) | 184 #if defined(OS_MACOSX) |
177 // On Mac, we only want the browser to initialize the database, but not the | 185 // On Mac, we only want the browser to initialize the database, but not the |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 | 346 |
339 // This helper is invoked by code in chrome.dll to wait for the handler start to | 347 // This helper is invoked by code in chrome.dll to wait for the handler start to |
340 // complete. | 348 // complete. |
341 void __declspec(dllexport) BlockUntilHandlerStartedImpl() { | 349 void __declspec(dllexport) BlockUntilHandlerStartedImpl() { |
342 crash_reporter::BlockUntilHandlerStarted(); | 350 crash_reporter::BlockUntilHandlerStarted(); |
343 } | 351 } |
344 | 352 |
345 } // extern "C" | 353 } // extern "C" |
346 | 354 |
347 #endif // OS_WIN | 355 #endif // OS_WIN |
OLD | NEW |