blob: 128fdaa05d6f0485b51f16b94349b90f8572d4a2 [file] [log] [blame]
[email protected]fcb30f7b2011-05-19 22:28:251// 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]fcb30f7b2011-05-19 22:28:257
[email protected]0bea7252011-08-05 15:34:008#include "base/base_export.h"
Lukasz Anforowicz68c21772018-01-13 03:42:449#include "base/strings/string_util.h"
[email protected]b29700d2011-05-24 17:57:4610
[email protected]fcb30f7b2011-05-19 22:28:2511namespace base {
12namespace debug {
13
14// Make the optimizer think that var is aliased. This is to prevent it from
Bruce Dawson263423ff2017-08-18 03:32:2615// optimizing out local variables that would not otherwise be live at the point
[email protected]fcb30f7b2011-05-19 22:28:2516// of a potential crash.
Bruce Dawson263423ff2017-08-18 03:32:2617// 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 Anforowicz68c21772018-01-13 03:42:4428// DEBUG_ALIAS_FOR_CSTR(name_copy, p->name, 16);
Bruce Dawson263423ff2017-08-18 03:32:2629// CHECK(false);
[email protected]0bea7252011-08-05 15:34:0030void BASE_EXPORT Alias(const void* var);
[email protected]fcb30f7b2011-05-19 22:28:2531
32} // namespace debug
33} // namespace base
34
Lukasz Anforowicz68c21772018-01-13 03:42:4435// 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]fcb30f7b2011-05-19 22:28:2543#endif // BASE_DEBUG_ALIAS_H_