LLDB mainline
Diagnostics.h
Go to the documentation of this file.
1//===-- Diagnostics.h -------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_CORE_DIAGNOSTICS_H
10#define LLDB_CORE_DIAGNOSTICS_H
11
14#include "lldb/Utility/Log.h"
15#include "llvm/Support/Error.h"
16
17#include <functional>
18#include <mutex>
19#include <optional>
20#include <string>
21#include <vector>
22
23namespace llvm {
24namespace json {
25class Value;
26} // namespace json
27} // namespace llvm
28
29namespace lldb_private {
30
31class Debugger;
32class ExecutionContext;
33
34/// The global diagnostics settings, exposed under `diagnostics` in the settings
35/// hierarchy.
37public:
39
40 bool GetCollectBinaries() const;
41 bool SetCollectBinaries(bool collect);
42};
43
44/// Diagnostics maintain an always-on, in-memory log of recent diagnostic
45/// messages that can be written out to help investigate bugs and troubleshoot
46/// issues.
48public:
51
52 /// The bundle directory and the files written into it, recorded as each one
53 /// is created so a file that could not be written is simply absent.
54 struct Attachments {
55 std::string directory;
56 std::vector<std::string> files;
57 };
58
59 /// The state a triager needs to make sense of a bug report. The full payload
60 /// is written into the bundle directory. These scalars are carried in the
61 /// terminal output and the report body rather than as redundant files. More
62 /// fields are expected to accrue over time.
63 struct Report {
64 std::string version;
65 std::string os;
66 std::string invocation;
68 };
69
70 /// Write the in-memory diagnostic log into the given directory.
71 llvm::Error Create(const FileSpec &dir);
72
73 /// Collect a full diagnostics bundle into \p dir and return its report.
74 ///
75 /// Writes the always-on log, the debugger's file-backed logs, statistics,
76 /// and a snapshot of the commands a triager runs first. When the
77 /// `collect-binaries` setting is enabled it also copies the executable, its
78 /// symbol file, and the core file. Collection is best-effort: a failure to
79 /// produce one artifact never aborts the rest, so a partial bundle is always
80 /// better than none.
81 llvm::Expected<Report> Collect(Debugger &debugger,
82 const ExecutionContext &exe_ctx,
83 const FileSpec &dir);
84
85 /// Write the diagnostic log into a directory and print a message to the given
86 /// output stream.
87 /// @{
88 bool Dump(llvm::raw_ostream &stream);
89 bool Dump(llvm::raw_ostream &stream, const FileSpec &dir);
90 /// @}
91
92 /// Record a diagnostic message into the always-on, in-memory log.
93 void Record(llvm::StringRef message);
94
95 /// Supplies an artifact's contents on demand. Subsystems register a provider
96 /// so Core need not depend on them. Each runs when a bundle is collected.
97 using ArtifactProvider = std::function<std::string()>;
98 using ArtifactProviderID = uint64_t;
99
100 /// Register \p provider to contribute file \p name. Returns an id for
101 /// RemoveArtifactProvider. Thread-safe.
103 ArtifactProvider provider);
104
105 /// Unregister a provider. Thread-safe.
107
108 static Diagnostics &Instance();
109
111
112 static bool Enabled();
113 static void Initialize();
114 static void Terminate();
115
116 /// Create a unique diagnostic directory.
117 static llvm::Expected<FileSpec> CreateUniqueDirectory();
118
119private:
120 static std::optional<Diagnostics> &InstanceImpl();
121
122 llvm::Error DumpDiangosticsLog(const FileSpec &dir) const;
123
124 /// Collect the individual parts of the bundle into \p dir, appending the name
125 /// of each file to \p files as it is written.
126 /// @{
127 void CollectLogs(Debugger &debugger, const FileSpec &dir,
128 std::vector<std::string> &files);
129 static void CollectStatistics(Debugger &debugger,
130 const ExecutionContext &exe_ctx,
131 const FileSpec &dir,
132 std::vector<std::string> &files);
133 static void CollectCommands(Debugger &debugger,
134 const ExecutionContext &exe_ctx,
135 const FileSpec &dir,
136 std::vector<std::string> &files);
137 static void CollectBinaries(const ExecutionContext &exe_ctx,
138 const FileSpec &dir,
139 std::vector<std::string> &files);
140 void CollectArtifactProviders(const FileSpec &dir,
141 std::vector<std::string> &files);
142 /// @}
143
144 /// Scalars carried in the report rather than written as files.
145 /// @{
146 static std::string GetHostDescription(const ExecutionContext &exe_ctx);
147 static std::string GetInvocation();
148 /// @}
149
151
157
158 /// Registered artifact providers, guarded by the mutex.
159 /// @{
161 std::vector<ArtifactProviderEntry> m_artifact_providers;
163 /// @}
164};
165
166/// Render a diagnostics report as JSON, for `diagnostics dump`'s terminal
167/// output.
168llvm::json::Value toJSON(const Diagnostics::Report &report);
169
170} // namespace lldb_private
171
172#endif
A class to manage flag bits.
Definition Debugger.h:100
The global diagnostics settings, exposed under diagnostics in the settings hierarchy.
Definition Diagnostics.h:36
std::mutex m_artifact_providers_mutex
RotatingLogHandler m_log_handler
std::vector< ArtifactProviderEntry > m_artifact_providers
bool Dump(llvm::raw_ostream &stream)
Write the diagnostic log into a directory and print a message to the given output stream.
static std::string GetInvocation()
static std::string GetHostDescription(const ExecutionContext &exe_ctx)
Scalars carried in the report rather than written as files.
static void CollectBinaries(const ExecutionContext &exe_ctx, const FileSpec &dir, std::vector< std::string > &files)
ArtifactProviderID AddArtifactProvider(std::string name, ArtifactProvider provider)
Register provider to contribute file name.
void CollectArtifactProviders(const FileSpec &dir, std::vector< std::string > &files)
llvm::Error DumpDiangosticsLog(const FileSpec &dir) const
void CollectLogs(Debugger &debugger, const FileSpec &dir, std::vector< std::string > &files)
Collect the individual parts of the bundle into dir, appending the name of each file to files as it i...
std::function< std::string()> ArtifactProvider
Supplies an artifact's contents on demand.
Definition Diagnostics.h:97
static DiagnosticsProperties & GetGlobalProperties()
void RemoveArtifactProvider(ArtifactProviderID id)
Unregister a provider. Thread-safe.
llvm::Error Create(const FileSpec &dir)
Write the in-memory diagnostic log into the given directory.
void Record(llvm::StringRef message)
Record a diagnostic message into the always-on, in-memory log.
static llvm::Expected< FileSpec > CreateUniqueDirectory()
Create a unique diagnostic directory.
static std::optional< Diagnostics > & InstanceImpl()
llvm::Expected< Report > Collect(Debugger &debugger, const ExecutionContext &exe_ctx, const FileSpec &dir)
Collect a full diagnostics bundle into dir and return its report.
ArtifactProviderID m_next_artifact_provider_id
Registered artifact providers, guarded by the mutex.
static void CollectCommands(Debugger &debugger, const ExecutionContext &exe_ctx, const FileSpec &dir, std::vector< std::string > &files)
static Diagnostics & Instance()
static void CollectStatistics(Debugger &debugger, const ExecutionContext &exe_ctx, const FileSpec &dir, std::vector< std::string > &files)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:57
A class that represents a running process on the host machine.
llvm::json::Value toJSON(const Diagnostics::Report &report)
Render a diagnostics report as JSON, for diagnostics dump's terminal output.
The bundle directory and the files written into it, recorded as each one is created so a file that co...
Definition Diagnostics.h:54
std::vector< std::string > files
Definition Diagnostics.h:56
The state a triager needs to make sense of a bug report.
Definition Diagnostics.h:63