crash_logging.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2012 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. #include "base/debug/crash_logging.h"
  5. #include "base/strings/string_piece.h"
  6. #include "build/build_config.h"
  7. namespace base::debug {
  8. namespace {
  9. CrashKeyImplementation* g_crash_key_impl = nullptr;
  10. } // namespace
  11. CrashKeyString* AllocateCrashKeyString(const char name[],
  12. CrashKeySize value_length) {
  13. if (!g_crash_key_impl)
  14. return nullptr;
  15. // TODO(https://crbug.com/1341077): It would be great if the DCHECKs below
  16. // could also be enabled on Android, but debugging tryjob failures was a bit
  17. // difficult... :-/
  18. #if DCHECK_IS_ON() && !BUILDFLAG(IS_ANDROID)
  19. base::StringPiece name_piece = name;
  20. // Some `CrashKeyImplementation`s reserve certain characters and disallow
  21. // using them in crash key names. See also https://crbug.com/1341077.
  22. DCHECK_EQ(base::StringPiece::npos, name_piece.find(':'))
  23. << "; name_piece = " << name_piece;
  24. // Some `CrashKeyImplementation`s support only short crash key names (e.g. see
  25. // the DCHECK in crash_reporter::internal::CrashKeyStringImpl::Set).
  26. // Enforcing this restrictions here ensures that crash keys will work for all
  27. // `CrashKeyStringImpl`s.
  28. DCHECK_LT(name_piece.size(), 40u);
  29. #endif
  30. return g_crash_key_impl->Allocate(name, value_length);
  31. }
  32. void SetCrashKeyString(CrashKeyString* crash_key, base::StringPiece value) {
  33. if (!g_crash_key_impl || !crash_key)
  34. return;
  35. g_crash_key_impl->Set(crash_key, value);
  36. }
  37. void ClearCrashKeyString(CrashKeyString* crash_key) {
  38. if (!g_crash_key_impl || !crash_key)
  39. return;
  40. g_crash_key_impl->Clear(crash_key);
  41. }
  42. void OutputCrashKeysToStream(std::ostream& out) {
  43. if (!g_crash_key_impl)
  44. return;
  45. g_crash_key_impl->OutputCrashKeysToStream(out);
  46. }
  47. ScopedCrashKeyString::ScopedCrashKeyString(CrashKeyString* crash_key,
  48. base::StringPiece value)
  49. : crash_key_(crash_key) {
  50. SetCrashKeyString(crash_key_, value);
  51. }
  52. ScopedCrashKeyString::ScopedCrashKeyString(ScopedCrashKeyString&& other)
  53. : crash_key_(std::exchange(other.crash_key_, nullptr)) {}
  54. ScopedCrashKeyString::~ScopedCrashKeyString() {
  55. ClearCrashKeyString(crash_key_);
  56. }
  57. void SetCrashKeyImplementation(std::unique_ptr<CrashKeyImplementation> impl) {
  58. delete g_crash_key_impl;
  59. g_crash_key_impl = impl.release();
  60. }
  61. } // namespace base::debug