crash_logging.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef BASE_DEBUG_CRASH_LOGGING_H_
  5. #define BASE_DEBUG_CRASH_LOGGING_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include "base/base_export.h"
  9. #include "base/macros.h"
  10. #include "base/strings/string_piece.h"
  11. namespace base {
  12. namespace debug {
  13. // A crash key is an annotation that is carried along with a crash report, to
  14. // provide additional debugging information beyond a stack trace. Crash keys
  15. // have a name and a string value.
  16. //
  17. // The preferred API is //components/crash/core/common:crash_key, however not
  18. // all clients can hold a direct dependency on that target. The API provided
  19. // in this file indirects the dependency.
  20. //
  21. // Example usage:
  22. // static CrashKeyString* crash_key =
  23. // AllocateCrashKeyString("name", CrashKeySize::Size32);
  24. // SetCrashKeyString(crash_key, "value");
  25. // ClearCrashKeyString(crash_key);
  26. // The maximum length for a crash key's value must be one of the following
  27. // pre-determined values.
  28. enum class CrashKeySize {
  29. Size32 = 32,
  30. Size64 = 64,
  31. Size256 = 256,
  32. };
  33. struct CrashKeyString;
  34. // Allocates a new crash key with the specified |name| with storage for a
  35. // value up to length |size|. This will return null if the crash key system is
  36. // not initialized.
  37. BASE_EXPORT CrashKeyString* AllocateCrashKeyString(const char name[],
  38. CrashKeySize size);
  39. // Stores |value| into the specified |crash_key|. The |crash_key| may be null
  40. // if AllocateCrashKeyString() returned null. If |value| is longer than the
  41. // size with which the key was allocated, it will be truncated.
  42. BASE_EXPORT void SetCrashKeyString(CrashKeyString* crash_key,
  43. base::StringPiece value);
  44. // Clears any value that was stored in |crash_key|. The |crash_key| may be
  45. // null.
  46. BASE_EXPORT void ClearCrashKeyString(CrashKeyString* crash_key);
  47. // A scoper that sets the specified key to value for the lifetime of the
  48. // object, and clears it on destruction.
  49. class BASE_EXPORT ScopedCrashKeyString {
  50. public:
  51. ScopedCrashKeyString(CrashKeyString* crash_key, base::StringPiece value);
  52. ~ScopedCrashKeyString();
  53. private:
  54. CrashKeyString* const crash_key_;
  55. DISALLOW_COPY_AND_ASSIGN(ScopedCrashKeyString);
  56. };
  57. ////////////////////////////////////////////////////////////////////////////////
  58. // The following declarations are used to initialize the crash key system
  59. // in //base by providing implementations for the above functions.
  60. // The virtual interface that provides the implementation for the crash key
  61. // API. This is implemented by a higher-layer component, and the instance is
  62. // set using the function below.
  63. class CrashKeyImplementation {
  64. public:
  65. virtual ~CrashKeyImplementation() = default;
  66. virtual CrashKeyString* Allocate(const char name[], CrashKeySize size) = 0;
  67. virtual void Set(CrashKeyString* crash_key, base::StringPiece value) = 0;
  68. virtual void Clear(CrashKeyString* crash_key) = 0;
  69. };
  70. // Initializes the crash key system in base by replacing the existing
  71. // implementation, if it exists, with |impl|. The |impl| is copied into base.
  72. BASE_EXPORT void SetCrashKeyImplementation(
  73. std::unique_ptr<CrashKeyImplementation> impl);
  74. // The base structure for a crash key, storing the allocation metadata.
  75. struct CrashKeyString {
  76. constexpr CrashKeyString(const char name[], CrashKeySize size)
  77. : name(name), size(size) {}
  78. const char* const name;
  79. const CrashKeySize size;
  80. };
  81. } // namespace debug
  82. } // namespace base
  83. #endif // BASE_DEBUG_CRASH_LOGGING_H_