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