Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Around this time last year, I said on the mailing list [1] that I wanted to to transform the reproducers into something that resembles a sysdiagnose on Apple platforms: a collection of files containing a variety of information to help diagnose bugs or troubleshoot issues. This patch adds that framework. Based on lessons learned from the reproducers, I've intentionally tried to keep it small and simple. Different parts of LLDB can register callbacks (this is necessary for layering purposes) that will get called when the diagnostics should be generated. [1] https://lists.llvm.org/pipermail/lldb-dev/2021-September/017045.html Differential revision: https://reviews.llvm.org/D134991
- Loading branch information
1 parent
1f8ac37
commit 84ea6b6
Showing
8 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//===-- Diagnostics.h -------------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLDB_UTILITY_DIAGNOSTICS_H | ||
#define LLDB_UTILITY_DIAGNOSTICS_H | ||
|
||
#include "lldb/Utility/FileSpec.h" | ||
#include "lldb/Utility/Log.h" | ||
#include "llvm/ADT/Optional.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/StringSet.h" | ||
#include "llvm/Support/Error.h" | ||
|
||
#include <functional> | ||
#include <mutex> | ||
#include <vector> | ||
|
||
namespace lldb_private { | ||
|
||
/// Diagnostics are a collection of files to help investigate bugs and | ||
/// troubleshoot issues. Any part of the debugger can register itself with the | ||
/// help of a callback to emit one or more files into the diagnostic directory. | ||
class Diagnostics { | ||
public: | ||
Diagnostics(); | ||
~Diagnostics(); | ||
|
||
/// Gather diagnostics in the given directory. | ||
llvm::Error Create(const FileSpec &dir); | ||
|
||
/// Gather diagnostics and print a message to the given output stream. | ||
bool Dump(llvm::raw_ostream &stream); | ||
|
||
using Callback = std::function<llvm::Error(const FileSpec &)>; | ||
|
||
void AddCallback(Callback callback); | ||
|
||
static Diagnostics &Instance(); | ||
static void Initialize(); | ||
static void Terminate(); | ||
|
||
private: | ||
static llvm::Optional<Diagnostics> &InstanceImpl(); | ||
|
||
llvm::SmallVector<Callback, 4> m_callbacks; | ||
std::mutex m_callbacks_mutex; | ||
}; | ||
|
||
} // namespace lldb_private | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//===-- Diagnostics.cpp ---------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "lldb/Utility/Diagnostics.h" | ||
#include "lldb/Utility/LLDBAssert.h" | ||
|
||
#include "llvm/Support/Error.h" | ||
#include "llvm/Support/FileSystem.h" | ||
#include "llvm/Support/raw_ostream.h" | ||
|
||
using namespace lldb_private; | ||
using namespace lldb; | ||
using namespace llvm; | ||
|
||
void Diagnostics::Initialize() { | ||
lldbassert(!InstanceImpl() && "Already initialized."); | ||
InstanceImpl().emplace(); | ||
} | ||
|
||
void Diagnostics::Terminate() { | ||
lldbassert(InstanceImpl() && "Already terminated."); | ||
InstanceImpl().reset(); | ||
} | ||
|
||
Optional<Diagnostics> &Diagnostics::InstanceImpl() { | ||
static Optional<Diagnostics> g_diagnostics; | ||
return g_diagnostics; | ||
} | ||
|
||
Diagnostics &Diagnostics::Instance() { return *InstanceImpl(); } | ||
|
||
Diagnostics::Diagnostics() {} | ||
|
||
Diagnostics::~Diagnostics() {} | ||
|
||
void Diagnostics::AddCallback(Callback callback) { | ||
std::lock_guard<std::mutex> guard(m_callbacks_mutex); | ||
m_callbacks.push_back(callback); | ||
} | ||
|
||
bool Diagnostics::Dump(raw_ostream &stream) { | ||
SmallString<128> diagnostics_dir; | ||
std::error_code ec = | ||
sys::fs::createUniqueDirectory("diagnostics", diagnostics_dir); | ||
if (ec) { | ||
stream << "unable to create diagnostic dir: " | ||
<< toString(errorCodeToError(ec)) << '\n'; | ||
return false; | ||
} | ||
|
||
stream << "LLDB diagnostics written to " << diagnostics_dir << "\n"; | ||
stream << "Please include the directory content when filing a bug report\n"; | ||
|
||
Error error = Create(FileSpec(diagnostics_dir.str())); | ||
if (error) { | ||
stream << toString(std::move(error)) << '\n'; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
Error Diagnostics::Create(const FileSpec &dir) { | ||
for (Callback c : m_callbacks) { | ||
if (Error err = c(dir)) | ||
return err; | ||
} | ||
return Error::success(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters