Moving DEBUG_ALIAS_FOR_CSTR into //base/debug/alias.h
DEBUG_ALIAS_FOR_CSTR was introduced in r527458 as a helper macro
for implementing DEBUG_ALIAS_FOR_ORIGIN. This CL moves the
DEBUG_ALIAS_FOR_CSTR macrodefinition into //base/debug/alias.h.
Additionally, the CL:
- Uses the DEBUG_ALIAS_FOR_CSTR macro to replace some existing uses of
base::debug::Alias + strlcpu
- Introduces DEBUG_ALIAS_FOR_GURL macro (and unittests) and uses it in 5
places (where base::debug::Alias + strlcpy was used before)
Bug: 797968
Cq-Include-Trybots: master.tryserver.blink:linux_trusty_blink_rel;master.tryserver.chromium.android:android_optional_gpu_tests_rel
Change-Id: I986ea90243f58768177a9ea68a7e2440eaa317c4
Reviewed-on: https://chromium-review.googlesource.com/857815
Commit-Queue: Ćukasz Anforowicz <[email protected]>
Reviewed-by: vmpstr <[email protected]>
Reviewed-by: Victor Costan <[email protected]>
Reviewed-by: Alex Moshchuk <[email protected]>
Reviewed-by: Finnur Thorarinsson <[email protected]>
Reviewed-by: Matt Menke <[email protected]>
Reviewed-by: Brett Wilson <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Cr-Commit-Position: refs/heads/master@{#529155}
diff --git a/base/debug/alias.h b/base/debug/alias.h
index d38f12b6..128fdaa0 100644
--- a/base/debug/alias.h
+++ b/base/debug/alias.h
@@ -6,6 +6,7 @@
#define BASE_DEBUG_ALIAS_H_
#include "base/base_export.h"
+#include "base/strings/string_util.h"
namespace base {
namespace debug {
@@ -24,14 +25,19 @@
// copy the object or its fields to local variables. Example usage:
// int last_error = err_;
// base::debug::Alias(&last_error);
-// char name_copy[16];
-// strncpy(name_copy, p->name, sizeof(name_copy) - 1);
-// name_copy[sizeof(name_copy) - 1] = '\0';
-// base::debug::Alias(name_copy);
+// DEBUG_ALIAS_FOR_CSTR(name_copy, p->name, 16);
// CHECK(false);
void BASE_EXPORT Alias(const void* var);
} // namespace debug
} // namespace base
+// Convenience macro that copies the null-terminated string from |c_str| into a
+// stack-allocated char array named |var_name| that holds up to |char_count|
+// characters and should be preserved in memory dumps.
+#define DEBUG_ALIAS_FOR_CSTR(var_name, c_str, char_count) \
+ char var_name[char_count]; \
+ ::base::strlcpy(var_name, (c_str), arraysize(var_name)); \
+ ::base::debug::Alias(var_name);
+
#endif // BASE_DEBUG_ALIAS_H_