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_UTILITY_DIAGNOSTICS_H
10#define LLDB_UTILITY_DIAGNOSTICS_H
11
12#include "lldb/Utility/FileSpec.h"
13#include "lldb/Utility/Log.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringSet.h"
16#include "llvm/Support/Error.h"
17
18#include <functional>
19#include <mutex>
20#include <optional>
21#include <vector>
22
23namespace lldb_private {
24
25/// Diagnostics are a collection of files to help investigate bugs and
26/// troubleshoot issues. Any part of the debugger can register itself with the
27/// help of a callback to emit one or more files into the diagnostic directory.
28class Diagnostics {
29public:
30 Diagnostics();
31 ~Diagnostics();
32
33 /// Gather diagnostics in the given directory.
34 llvm::Error Create(const FileSpec &dir);
35
36 /// Gather diagnostics and print a message to the given output stream.
37 /// @{
38 bool Dump(llvm::raw_ostream &stream);
39 bool Dump(llvm::raw_ostream &stream, const FileSpec &dir);
40 /// @}
41
42 void Report(llvm::StringRef message);
43
44 using Callback = std::function<llvm::Error(const FileSpec &)>;
45 using CallbackID = uint64_t;
46
47 CallbackID AddCallback(Callback callback);
48 void RemoveCallback(CallbackID id);
49
50 static Diagnostics &Instance();
51
52 static bool Enabled();
53 static void Initialize();
54 static void Terminate();
55
56 /// Create a unique diagnostic directory.
57 static llvm::Expected<FileSpec> CreateUniqueDirectory();
58
59private:
60 static std::optional<Diagnostics> &InstanceImpl();
61
62 llvm::Error DumpDiangosticsLog(const FileSpec &dir) const;
63
64 RotatingLogHandler m_log_handler;
65
66 struct CallbackEntry {
67 CallbackEntry(CallbackID id, Callback callback)
68 : id(id), callback(std::move(callback)) {}
69 CallbackID id;
70 Callback callback;
71 };
72
73 /// Monotonically increasing callback identifier. Unique per Diagnostic
74 /// instance.
75 CallbackID m_callback_id;
76
77 /// List of callback entries.
78 llvm::SmallVector<CallbackEntry, 4> m_callbacks;
79
80 /// Mutex to protect callback list and callback identifier.
81 std::mutex m_callbacks_mutex;
82};
83
84} // namespace lldb_private
85
86#endif
87

source code of lldb/include/lldb/Utility/Diagnostics.h