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

Side by Side Diff: base/debug/crash_logging.h

Issue 2137863002: Make it a compile error to use a crash key with no variable name. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 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
« no previous file with comments | « no previous file | base/debug/crash_logging.cc » ('j') | base/debug/crash_logging.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef BASE_DEBUG_CRASH_LOGGING_H_ 5 #ifndef BASE_DEBUG_CRASH_LOGGING_H_
6 #define BASE_DEBUG_CRASH_LOGGING_H_ 6 #define BASE_DEBUG_CRASH_LOGGING_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <string> 10 #include <string>
11 #include <type_traits>
11 #include <vector> 12 #include <vector>
12 13
13 #include "base/base_export.h" 14 #include "base/base_export.h"
14 #include "base/macros.h" 15 #include "base/macros.h"
15 #include "base/strings/string_piece.h" 16 #include "base/strings/string_piece.h"
16 17
17 // These functions add metadata to the upload payload when sending crash reports 18 // These functions add metadata to the upload payload when sending crash reports
18 // to the crash server. 19 // to the crash server.
19 // 20 //
20 // IMPORTANT: On OS X and Linux, the key/value pairs are only sent as part of 21 // IMPORTANT: On OS X and Linux, the key/value pairs are only sent as part of
(...skipping 14 matching lines...) Expand all
35 BASE_EXPORT void SetCrashKeyToStackTrace(const base::StringPiece& key, 36 BASE_EXPORT void SetCrashKeyToStackTrace(const base::StringPiece& key,
36 const StackTrace& trace); 37 const StackTrace& trace);
37 38
38 // Formats |count| instruction pointers from |addresses| using %p and 39 // Formats |count| instruction pointers from |addresses| using %p and
39 // sets the resulting string as a value for crash key |key|. A maximum of 23 40 // sets the resulting string as a value for crash key |key|. A maximum of 23
40 // items will be encoded, since breakpad limits values to 255 bytes. 41 // items will be encoded, since breakpad limits values to 255 bytes.
41 BASE_EXPORT void SetCrashKeyFromAddresses(const base::StringPiece& key, 42 BASE_EXPORT void SetCrashKeyFromAddresses(const base::StringPiece& key,
42 const void* const* addresses, 43 const void* const* addresses,
43 size_t count); 44 size_t count);
44 45
46 enum ScopedCrashKeyNeedsNameTag {
danakj 2016/07/19 21:02:57 nit: I'd nest this inside ScopedCrashKey, beside (
dcheng 2016/07/22 05:39:22 Done.
47 SCOPED_CRASH_KEY_NEEDS_NAME,
48 };
49
45 // A scoper that sets the specified key to value for the lifetime of the 50 // A scoper that sets the specified key to value for the lifetime of the
46 // object, and clears it on destruction. 51 // object, and clears it on destruction.
47 class BASE_EXPORT ScopedCrashKey { 52 class BASE_EXPORT ScopedCrashKey {
48 public: 53 public:
49 ScopedCrashKey(const base::StringPiece& key, const base::StringPiece& value); 54 ScopedCrashKey(const base::StringPiece& key, const base::StringPiece& value);
50 ~ScopedCrashKey(); 55 ~ScopedCrashKey();
51 56
57 // Helper to force a static_assert when instantiating a ScopedCrashKey
58 // temporary without a name. The usual idiom is to just #define a macro that
59 // static_asserts with the message; however, that doesn't work well when the
60 // type is in a namespace.
61 //
62 // Instead, we use a templated helper to trigger the static_assert, observing
63 // two rules:
64 // - The static_assert needs to be in a normally uninstantiated template;
65 // otherwise, it will fail to compile =)
66 // - Similarly, the static_assert must be dependent on the template argument,
67 // to prevent it from being evaluated until the template is instantiated.
68 //
69 // To prevent this constructor from being accidentally invoked, it takes a
70 // special enum as an argument.
71
72 // TODO(dcheng): For reasons unknown to me, we can't make this a template
73 // function taking just one parameter (the enum tag). While the build
74 // correctly fails, the static_assert is never triggered. I think this is
75 // because SFINAE removes the overload from consideration before it's ever
76 // instantiated, but I'm not sure...
77 template <typename... Args>
78 explicit ScopedCrashKey(ScopedCrashKeyNeedsNameTag, const Args&...) {
tzik 2016/07/14 07:39:53 Can we just disable this by "= delete"?
dcheng 2016/07/14 07:41:15 We can, but the idea is to provide a useful error
tzik 2016/07/14 07:45:41 Ok, so. How about define this: template <typename.
79 constexpr bool always_false = sizeof...(Args) == 0 && sizeof...(Args) != 0;
80 static_assert(
81 always_false,
82 "scoped crash key objects should not be unnamed temporaries.");
83 }
84
52 private: 85 private:
53 std::string key_; 86 std::string key_;
54 87
55 DISALLOW_COPY_AND_ASSIGN(ScopedCrashKey); 88 DISALLOW_COPY_AND_ASSIGN(ScopedCrashKey);
56 }; 89 };
57 90
91 // Disallow an instantation of ScopedCrashKey without a name, since this results
92 // in a temporary that is immediately destroyed. Doing so will trigger the
93 // static_assert in the templated constructor helper in ScopedCrashKey.
94 #define ScopedCrashKey(...) \
95 ScopedCrashKey(base::debug::SCOPED_CRASH_KEY_NEEDS_NAME, __VA_ARGS__)
96
58 // Before setting values for a key, all the keys must be registered. 97 // Before setting values for a key, all the keys must be registered.
59 struct BASE_EXPORT CrashKey { 98 struct BASE_EXPORT CrashKey {
60 // The name of the crash key, used in the above functions. 99 // The name of the crash key, used in the above functions.
61 const char* key_name; 100 const char* key_name;
62 101
63 // The maximum length for a value. If the value is longer than this, it will 102 // The maximum length for a value. If the value is longer than this, it will
64 // be truncated. If the value is larger than the |chunk_max_length| passed to 103 // be truncated. If the value is larger than the |chunk_max_length| passed to
65 // InitCrashKeys() but less than this value, it will be split into multiple 104 // InitCrashKeys() but less than this value, it will be split into multiple
66 // numbered chunks. 105 // numbered chunks.
67 size_t max_length; 106 size_t max_length;
(...skipping 29 matching lines...) Expand all
97 const base::StringPiece& value, 136 const base::StringPiece& value,
98 size_t chunk_max_length); 137 size_t chunk_max_length);
99 138
100 // Resets the crash key system so it can be reinitialized. For testing only. 139 // Resets the crash key system so it can be reinitialized. For testing only.
101 BASE_EXPORT void ResetCrashLoggingForTesting(); 140 BASE_EXPORT void ResetCrashLoggingForTesting();
102 141
103 } // namespace debug 142 } // namespace debug
104 } // namespace base 143 } // namespace base
105 144
106 #endif // BASE_DEBUG_CRASH_LOGGING_H_ 145 #endif // BASE_DEBUG_CRASH_LOGGING_H_
OLDNEW
« no previous file with comments | « no previous file | base/debug/crash_logging.cc » ('j') | base/debug/crash_logging.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698