Skip to content

[lldb] Log errors to the system log if they would otherwise get dropped #111911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lldb/include/lldb/Utility/Log.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,15 @@ template <typename Cat> Log *GetLog(Cat mask) {
return LogChannelFor<Cat>().GetLog(Log::MaskType(mask));
}

/// Getter and setter for the error log (see g_error_log).
/// The error log is set to the system log in SystemInitializerFull. We can't
/// use the system log directly because that would violate the layering between
/// Utility and Host.
/// @{
void SetLLDBErrorLog(Log *log);
Log *GetLLDBErrorLog();
/// @}

} // namespace lldb_private

/// The LLDB_LOG* macros defined below are the way to emit log messages.
Expand Down Expand Up @@ -384,6 +393,8 @@ template <typename Cat> Log *GetLog(Cat mask) {
do { \
::lldb_private::Log *log_private = (log); \
::llvm::Error error_private = (error); \
if (!log_private) \
log_private = lldb_private::GetLLDBErrorLog(); \
if (log_private && error_private) { \
log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
__VA_ARGS__); \
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/API/SystemInitializerFull.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ llvm::Error SystemInitializerFull::Initialize() {
// Use the Debugger's LLDBAssert callback.
SetLLDBAssertCallback(Debugger::AssertCallback);

// Use the system log to report errors that would otherwise get dropped.
SetLLDBErrorLog(GetLog(SystemLog::System));

LLDB_LOG(GetLog(SystemLog::System), "{0}", GetVersion());

return llvm::Error::success();
Expand Down
8 changes: 8 additions & 0 deletions lldb/source/Utility/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ char TeeLogHandler::ID;

llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;

// The error log is used by LLDB_LOG_ERROR. If the given log channel passed to
// LLDB_LOG_ERROR is not enabled, error messages are logged to the error log.
static std::atomic<Log *> g_error_log = nullptr;

void Log::ForEachCategory(
const Log::ChannelMap::value_type &entry,
llvm::function_ref<void(llvm::StringRef, llvm::StringRef)> lambda) {
Expand Down Expand Up @@ -460,3 +464,7 @@ void TeeLogHandler::Emit(llvm::StringRef message) {
m_first_log_handler->Emit(message);
m_second_log_handler->Emit(message);
}

void lldb_private::SetLLDBErrorLog(Log *log) { g_error_log.store(log); }

Log *lldb_private::GetLLDBErrorLog() { return g_error_log; }
Loading