OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/mac/crash_logging.h" | 5 #include "base/mac/crash_logging.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
| 9 #include "base/logging.h" |
| 10 |
9 namespace base { | 11 namespace base { |
10 namespace mac { | 12 namespace mac { |
11 | 13 |
12 static SetCrashKeyValueFuncPtr g_set_key_func; | 14 static SetCrashKeyValueFuncPtr g_set_key_func; |
13 static ClearCrashKeyValueFuncPtr g_clear_key_func; | 15 static ClearCrashKeyValueFuncPtr g_clear_key_func; |
14 | 16 |
15 void SetCrashKeyFunctions(SetCrashKeyValueFuncPtr set_key_func, | 17 void SetCrashKeyFunctions(SetCrashKeyValueFuncPtr set_key_func, |
16 ClearCrashKeyValueFuncPtr clear_key_func) { | 18 ClearCrashKeyValueFuncPtr clear_key_func) { |
17 g_set_key_func = set_key_func; | 19 g_set_key_func = set_key_func; |
18 g_clear_key_func = clear_key_func; | 20 g_clear_key_func = clear_key_func; |
19 } | 21 } |
20 | 22 |
21 void SetCrashKeyValue(NSString* key, NSString* val) { | 23 void SetCrashKeyValue(NSString* key, NSString* val) { |
22 if (g_set_key_func) | 24 if (g_set_key_func) |
23 g_set_key_func(key, val); | 25 g_set_key_func(key, val); |
24 } | 26 } |
25 | 27 |
26 void ClearCrashKey(NSString* key) { | 28 void ClearCrashKey(NSString* key) { |
27 if (g_clear_key_func) | 29 if (g_clear_key_func) |
28 g_clear_key_func(key); | 30 g_clear_key_func(key); |
29 } | 31 } |
30 | 32 |
| 33 void SetCrashKeyFromAddresses(NSString* key, |
| 34 const void* const* addresses, |
| 35 size_t count) { |
| 36 NSString* value = @"<null>"; |
| 37 if (addresses && count) { |
| 38 const size_t kBreakpadValueMax = 255; |
| 39 |
| 40 NSMutableArray* hexBacktrace = [NSMutableArray arrayWithCapacity:count]; |
| 41 size_t length = 0; |
| 42 for (size_t i = 0; i < count; ++i) { |
| 43 NSString* s = [NSString stringWithFormat:@"%p", addresses[i]]; |
| 44 length += 1 + [s length]; |
| 45 if (length > kBreakpadValueMax) |
| 46 break; |
| 47 [hexBacktrace addObject:s]; |
| 48 } |
| 49 value = [hexBacktrace componentsJoinedByString:@" "]; |
| 50 |
| 51 // Warn someone if this exceeds the breakpad limits. |
| 52 DCHECK_LE(strlen([value UTF8String]), kBreakpadValueMax); |
| 53 } |
| 54 base::mac::SetCrashKeyValue(key, value); |
| 55 } |
| 56 |
31 ScopedCrashKey::ScopedCrashKey(NSString* key, NSString* value) | 57 ScopedCrashKey::ScopedCrashKey(NSString* key, NSString* value) |
32 : crash_key_([key retain]) { | 58 : crash_key_([key retain]) { |
33 if (g_set_key_func) | 59 if (g_set_key_func) |
34 g_set_key_func(crash_key_, value); | 60 g_set_key_func(crash_key_, value); |
35 } | 61 } |
36 | 62 |
37 ScopedCrashKey::~ScopedCrashKey() { | 63 ScopedCrashKey::~ScopedCrashKey() { |
38 if (g_clear_key_func) | 64 if (g_clear_key_func) |
39 g_clear_key_func(crash_key_); | 65 g_clear_key_func(crash_key_); |
40 } | 66 } |
41 | 67 |
42 } // namespace mac | 68 } // namespace mac |
43 } // namespace base | 69 } // namespace base |
OLD | NEW |