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