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 bool GetSuggestBugReport() const;
44};
45
46/// Diagnostics maintain an always-on, in-memory log of recent diagnostic
47/// messages that can be written out to help investigate bugs and troubleshoot
48/// issues.
50public:
53
54 /// The bundle directory and the files written into it, recorded as each one
55 /// is created so a file that could not be written is simply absent.
56 struct Attachments {
57 std::string directory;
58 std::vector<std::string> files;
59 };
60
61 /// The state a triager needs to make sense of a bug report. The full payload
62 /// is written into the bundle directory. These scalars are carried in the
63 /// terminal output and the report body rather than as redundant files. More
64 /// fields are expected to accrue over time.
65 struct Report {
66 std::string version;
67 std::string os;
68 std::string invocation;
70 };
71
72 /// Write the in-memory diagnostic log into the given directory.
73 llvm::Error Create(const FileSpec &dir);
74
75 /// Collect a full diagnostics bundle into \p dir and return its report.
76 ///
77 /// Writes the always-on log, the debugger's file-backed logs, statistics,
78 /// and a snapshot of the commands a triager runs first. When the
79 /// `collect-binaries` setting is enabled it also copies the executable, its
80 /// symbol file, and the core file. Collection is best-effort: a failure to
81 /// produce one artifact never aborts the rest, so a partial bundle is always
82 /// better than none.
83 llvm::Expected<Report> Collect(Debugger &debugger,
84 const ExecutionContext &exe_ctx,
85 const FileSpec &dir);
86
87 /// Write the diagnostic log into a directory and print a message to the given
88 /// output stream.
89 /// @{
90 bool Dump(llvm::raw_ostream &stream);
91 bool Dump(llvm::raw_ostream &stream, const FileSpec &dir);
92 /// @}
93
94 /// Record a diagnostic message into the always-on, in-memory log.
95 void Record(llvm::StringRef message);
96
97 /// Supplies an artifact's contents on demand. Subsystems register a provider
98 /// so Core need not depend on them. Each runs when a bundle is collected.
99 using ArtifactProvider = std::function<std::string()>;
100 using ArtifactProviderID = uint64_t;
101
102 /// Register \p provider to contribute file \p name. Returns an id for
103 /// RemoveArtifactProvider. Thread-safe.
105 ArtifactProvider provider);
106
107 /// Unregister a provider. Thread-safe.
109
110 static Diagnostics &Instance();
111
113
114 static bool Enabled();
115 static void Initialize();
116 static void Terminate();
117
118 /// Create a unique diagnostic directory.
119 static llvm::Expected<FileSpec> CreateUniqueDirectory();
120
121private:
122 static std::optional<Diagnostics> &InstanceImpl();
123
124 llvm::Error DumpDiangosticsLog(const FileSpec &dir) const;
125
126 /// Collect the individual parts of the bundle into \p dir, appending the name
127 /// of each file to \p files as it is written.
128 /// @{
129 void CollectLogs(Debugger &debugger, const FileSpec &dir,
130 std::vector<std::string> &files);
131 static void CollectStatistics(Debugger &debugger,
132 const ExecutionContext &exe_ctx,
133 const FileSpec &dir,
134 std::vector<std::string> &files);
135 static void CollectCommands(Debugger &debugger,
136 const ExecutionContext &exe_ctx,
137 const FileSpec &dir,
138 std::vector<std::string> &files);
139 static void CollectBinaries(const ExecutionContext &exe_ctx,
140 const FileSpec &dir,
141 std::vector<std::string> &files);
142 void CollectArtifactProviders(const FileSpec &dir,
143 std::vector<std::string> &files);
144 /// @}
145
146 /// Scalars carried in the report rather than written as files.
147 /// @{
148 static std::string GetHostDescription(const ExecutionContext &exe_ctx);
149 static std::string GetInvocation();
150 /// @}
151
153
159
160 /// Registered artifact providers, guarded by the mutex.
161 /// @{
163 std::vector<ArtifactProviderEntry> m_artifact_providers;
165 /// @}
166};
167
168/// Render a diagnostics report as JSON, for `diagnostics dump`'s terminal
169/// output.
170llvm::json::Value toJSON(const Diagnostics::Report &report);
171
172} // namespace lldb_private
173
174#endif
A class to manage flag bits.
Definition Debugger.h:106
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:99
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:56
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:56
std::vector< std::string > files
Definition Diagnostics.h:58
The state a triager needs to make sense of a bug report.
Definition Diagnostics.h:65