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/strings/string_util.h" 10 11 namespace base { 12 namespace debug { 13 14 // Make the optimizer think that var is aliased. This is to prevent it from 15 // optimizing out local variables that would not otherwise be live at the point 16 // of a potential crash. 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); 28 // DEBUG_ALIAS_FOR_CSTR(name_copy, p->name, 16); 29 // CHECK(false); 30 void BASE_EXPORT Alias(const void* var); 31 32 } // namespace debug 33 } // namespace base 34 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 43 #endif // BASE_DEBUG_ALIAS_H_ 44