[email protected] | fcb30f7b | 2011-05-19 22:28:25 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_DEBUG_ALIAS_H_ |
| 6 | #define BASE_DEBUG_ALIAS_H_ |
[email protected] | fcb30f7b | 2011-05-19 22:28:25 | [diff] [blame] | 7 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 8 | #include "base/base_export.h" |
Lukasz Anforowicz | 68c2177 | 2018-01-13 03:42:44 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
[email protected] | b29700d | 2011-05-24 17:57:46 | [diff] [blame] | 10 | |
[email protected] | fcb30f7b | 2011-05-19 22:28:25 | [diff] [blame] | 11 | namespace base { |
| 12 | namespace debug { |
| 13 | |
| 14 | // Make the optimizer think that var is aliased. This is to prevent it from |
Bruce Dawson | 263423ff | 2017-08-18 03:32:26 | [diff] [blame] | 15 | // optimizing out local variables that would not otherwise be live at the point |
[email protected] | fcb30f7b | 2011-05-19 22:28:25 | [diff] [blame] | 16 | // of a potential crash. |
Bruce Dawson | 263423ff | 2017-08-18 03:32:26 | [diff] [blame] | 17 | // base::debug::Alias should only be used for local variables, not globals, |
| 18 | // object members, or function return values - these must be copied to locals if |
| 19 | // you want to ensure they are recorded in crash dumps. |
| 20 | // Note that if the local variable is a pointer then its value will be retained |
| 21 | // but the memory that it points to will probably not be saved in the crash |
| 22 | // dump - by default only stack memory is saved. Therefore the aliasing |
| 23 | // technique is usually only worthwhile with non-pointer variables. If you have |
| 24 | // a pointer to an object and you want to retain the object's state you need to |
| 25 | // copy the object or its fields to local variables. Example usage: |
| 26 | // int last_error = err_; |
| 27 | // base::debug::Alias(&last_error); |
Lukasz Anforowicz | 68c2177 | 2018-01-13 03:42:44 | [diff] [blame] | 28 | // DEBUG_ALIAS_FOR_CSTR(name_copy, p->name, 16); |
Bruce Dawson | 263423ff | 2017-08-18 03:32:26 | [diff] [blame] | 29 | // CHECK(false); |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 30 | void BASE_EXPORT Alias(const void* var); |
[email protected] | fcb30f7b | 2011-05-19 22:28:25 | [diff] [blame] | 31 | |
| 32 | } // namespace debug |
| 33 | } // namespace base |
| 34 | |
Lukasz Anforowicz | 68c2177 | 2018-01-13 03:42:44 | [diff] [blame] | 35 | // Convenience macro that copies the null-terminated string from |c_str| into a |
| 36 | // stack-allocated char array named |var_name| that holds up to |char_count| |
| 37 | // characters and should be preserved in memory dumps. |
| 38 | #define DEBUG_ALIAS_FOR_CSTR(var_name, c_str, char_count) \ |
| 39 | char var_name[char_count]; \ |
| 40 | ::base::strlcpy(var_name, (c_str), arraysize(var_name)); \ |
| 41 | ::base::debug::Alias(var_name); |
| 42 | |
[email protected] | fcb30f7b | 2011-05-19 22:28:25 | [diff] [blame] | 43 | #endif // BASE_DEBUG_ALIAS_H_ |