LLDB  mainline
Diagnostics.cpp
Go to the documentation of this file.
1 //===-- Diagnostics.cpp ---------------------------------------------------===//
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 
11 
12 #include "llvm/Support/Error.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <optional>
16 
17 using namespace lldb_private;
18 using namespace lldb;
19 using namespace llvm;
20 
21 static constexpr size_t g_num_log_messages = 100;
22 
24  lldbassert(!InstanceImpl() && "Already initialized.");
25  InstanceImpl().emplace();
26 }
27 
29  lldbassert(InstanceImpl() && "Already terminated.");
30  InstanceImpl().reset();
31 }
32 
33 bool Diagnostics::Enabled() { return InstanceImpl().operator bool(); }
34 
35 std::optional<Diagnostics> &Diagnostics::InstanceImpl() {
36  static std::optional<Diagnostics> g_diagnostics;
37  return g_diagnostics;
38 }
39 
40 Diagnostics &Diagnostics::Instance() { return *InstanceImpl(); }
41 
43 
45 
47  std::lock_guard<std::mutex> guard(m_callbacks_mutex);
48  m_callbacks.push_back(callback);
49 }
50 
51 bool Diagnostics::Dump(raw_ostream &stream) {
52  Expected<FileSpec> diagnostics_dir = CreateUniqueDirectory();
53  if (!diagnostics_dir) {
54  stream << "unable to create diagnostic dir: "
55  << toString(diagnostics_dir.takeError()) << '\n';
56  return false;
57  }
58 
59  return Dump(stream, *diagnostics_dir);
60 }
61 
62 bool Diagnostics::Dump(raw_ostream &stream, const FileSpec &dir) {
63  stream << "LLDB diagnostics will be written to " << dir.GetPath() << "\n";
64  stream << "Please include the directory content when filing a bug report\n";
65 
66  if (Error error = Create(dir)) {
67  stream << toString(std::move(error)) << '\n';
68  return false;
69  }
70 
71  return true;
72 }
73 
74 llvm::Expected<FileSpec> Diagnostics::CreateUniqueDirectory() {
75  SmallString<128> diagnostics_dir;
76  std::error_code ec =
77  sys::fs::createUniqueDirectory("diagnostics", diagnostics_dir);
78  if (ec)
79  return errorCodeToError(ec);
80  return FileSpec(diagnostics_dir.str());
81 }
82 
84  if (Error err = DumpDiangosticsLog(dir))
85  return err;
86 
87  for (Callback c : m_callbacks) {
88  if (Error err = c(dir))
89  return err;
90  }
91 
92  return Error::success();
93 }
94 
96  FileSpec log_file = dir.CopyByAppendingPathComponent("diagnostics.log");
97  std::error_code ec;
98  llvm::raw_fd_ostream stream(log_file.GetPath(), ec, llvm::sys::fs::OF_None);
99  if (ec)
100  return errorCodeToError(ec);
101  m_log_handler.Dump(stream);
102  return Error::success();
103 }
104 
105 void Diagnostics::Report(llvm::StringRef message) {
107 }
lldb_private::toString
const char * toString(AppleArm64ExceptionClass EC)
Definition: AppleArm64ExceptionClass.h:38
lldb_private::Diagnostics::Initialize
static void Initialize()
Definition: Diagnostics.cpp:23
llvm
Definition: Debugger.h:51
lldb_private::Diagnostics::m_log_handler
RotatingLogHandler m_log_handler
Definition: Diagnostics.h:62
lldb_private::Diagnostics::Instance
static Diagnostics & Instance()
Definition: Diagnostics.cpp:40
lldb_private::Diagnostics::m_callbacks_mutex
std::mutex m_callbacks_mutex
Definition: Diagnostics.h:65
lldb_private::Diagnostics::CreateUniqueDirectory
static llvm::Expected< FileSpec > CreateUniqueDirectory()
Create a unique diagnostic directory.
Definition: Diagnostics.cpp:74
lldb_private::Diagnostics
Diagnostics are a collection of files to help investigate bugs and troubleshoot issues.
Definition: Diagnostics.h:28
lldb_private::Diagnostics::Terminate
static void Terminate()
Definition: Diagnostics.cpp:28
lldb_private::RotatingLogHandler::Emit
void Emit(llvm::StringRef message) override
Definition: Log.cpp:399
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Diagnostics::Report
void Report(llvm::StringRef message)
Definition: Diagnostics.cpp:105
LLDBAssert.h
lldb_private::Diagnostics::AddCallback
void AddCallback(Callback callback)
Definition: Diagnostics.cpp:46
lldb_private::Diagnostics::Callback
std::function< llvm::Error(const FileSpec &)> Callback
Definition: Diagnostics.h:44
lldb_private::Diagnostics::m_callbacks
llvm::SmallVector< Callback, 4 > m_callbacks
Definition: Diagnostics.h:64
Diagnostics.h
lldb_private::FileSpec::CopyByAppendingPathComponent
FileSpec CopyByAppendingPathComponent(llvm::StringRef component) const
Definition: FileSpec.cpp:418
lldb_private::Diagnostics::InstanceImpl
static std::optional< Diagnostics > & InstanceImpl()
Definition: Diagnostics.cpp:35
lldbassert
#define lldbassert(x)
Definition: LLDBAssert.h:15
lldb_private::Diagnostics::Diagnostics
Diagnostics()
Definition: Diagnostics.cpp:42
message
message(FATAL_ERROR "invalid libipt include path provided") endif() include_directories($
Definition: Plugins/Trace/intel-pt/CMakeLists.txt:6
lldb_private::Diagnostics::Dump
bool Dump(llvm::raw_ostream &stream)
Gather diagnostics and print a message to the given output stream.
Definition: Diagnostics.cpp:51
lldb_private::Diagnostics::Enabled
static bool Enabled()
Definition: Diagnostics.cpp:33
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:35
lldb_private::Diagnostics::~Diagnostics
~Diagnostics()
Definition: Diagnostics.cpp:44
lldb_private::Diagnostics::Create
llvm::Error Create(const FileSpec &dir)
Gather diagnostics in the given directory.
Definition: Diagnostics.cpp:83
lldb_private::Diagnostics::DumpDiangosticsLog
llvm::Error DumpDiangosticsLog(const FileSpec &dir) const
Definition: Diagnostics.cpp:95
lldb_private::RotatingLogHandler::Dump
void Dump(llvm::raw_ostream &stream) const
Definition: Log.cpp:417
g_num_log_messages
static constexpr size_t g_num_log_messages
Definition: Diagnostics.cpp:21
lldb_private::FileSpec::GetPath
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:366
lldb
Definition: SBAddress.h:15