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
13#include "lldb/Utility/Log.h"
14#include "llvm/Support/Error.h"
15
16#include <optional>
17#include <string>
18#include <vector>
19
20namespace llvm {
21namespace json {
22class Value;
23} // namespace json
24} // namespace llvm
25
26namespace lldb_private {
27
28class Debugger;
29class ExecutionContext;
30
31/// Diagnostics maintain an always-on, in-memory log of recent diagnostic
32/// messages that can be written out to help investigate bugs and troubleshoot
33/// issues.
35public:
38
39 /// The bundle directory and the files written into it, recorded as each one
40 /// is created so a file that could not be written is simply absent.
41 struct Attachments {
42 std::string directory;
43 std::vector<std::string> files;
44 };
45
46 /// The state a triager needs to make sense of a bug report. The full payload
47 /// is written into the bundle directory. These scalars are carried in the
48 /// terminal output and the report body rather than as redundant files. More
49 /// fields are expected to accrue over time.
50 struct Report {
51 std::string version;
52 std::string os;
53 std::string invocation;
55 };
56
57 /// Write the in-memory diagnostic log into the given directory.
58 llvm::Error Create(const FileSpec &dir);
59
60 /// Collect a full diagnostics bundle into \p dir and return its report.
61 ///
62 /// Writes the always-on log, the debugger's file-backed logs, statistics,
63 /// and a snapshot of the commands a triager runs first. Collection is
64 /// best-effort: a failure to produce one artifact never aborts the rest, so
65 /// a partial bundle is always better than none.
66 llvm::Expected<Report> Collect(Debugger &debugger,
67 const ExecutionContext &exe_ctx,
68 const FileSpec &dir);
69
70 /// Write the diagnostic log into a directory and print a message to the given
71 /// output stream.
72 /// @{
73 bool Dump(llvm::raw_ostream &stream);
74 bool Dump(llvm::raw_ostream &stream, const FileSpec &dir);
75 /// @}
76
77 /// Record a diagnostic message into the always-on, in-memory log.
78 void Record(llvm::StringRef message);
79
80 static Diagnostics &Instance();
81
82 static bool Enabled();
83 static void Initialize();
84 static void Terminate();
85
86 /// Create a unique diagnostic directory.
87 static llvm::Expected<FileSpec> CreateUniqueDirectory();
88
89private:
90 static std::optional<Diagnostics> &InstanceImpl();
91
92 llvm::Error DumpDiangosticsLog(const FileSpec &dir) const;
93
94 /// Collect the individual parts of the bundle into \p dir, appending the name
95 /// of each file to \p files as it is written.
96 /// @{
97 void CollectLogs(Debugger &debugger, const FileSpec &dir,
98 std::vector<std::string> &files);
99 static void CollectStatistics(Debugger &debugger,
100 const ExecutionContext &exe_ctx,
101 const FileSpec &dir,
102 std::vector<std::string> &files);
103 static void CollectCommands(Debugger &debugger,
104 const ExecutionContext &exe_ctx,
105 const FileSpec &dir,
106 std::vector<std::string> &files);
107 /// @}
108
109 /// Scalars carried in the report rather than written as files.
110 /// @{
111 static std::string GetHostDescription(const ExecutionContext &exe_ctx);
112 static std::string GetInvocation();
113 /// @}
114
116};
117
118/// Render a diagnostics report as JSON, for `diagnostics dump`'s terminal
119/// output.
120llvm::json::Value toJSON(const Diagnostics::Report &report);
121
122} // namespace lldb_private
123
124#endif
A class to manage flag bits.
Definition Debugger.h:100
RotatingLogHandler m_log_handler
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.
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...
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.
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:41
std::vector< std::string > files
Definition Diagnostics.h:43
The state a triager needs to make sense of a bug report.
Definition Diagnostics.h:50