[Lldb-commits] [lldb] [lldb] Add LanguageRuntime hooks for uncaught top-level errors (NFC) (PR #230331)
Adrian Prantl via lldb-commits
lldb-commits at lists.llvm.org
Fri Oct 9 11:50:01 PDT 2026
https://github.com/adrian-prantl updated https://github.com/llvm/llvm-project/pull/230331
>From bf5e450517337dbada94b8333dc5a2b6fe3c8036 Mon Sep 17 00:00:00 2001
From: Adrian Prantl <aprantl at apple.com>
Date: Thu, 8 Oct 2026 17:15:20 -0700
Subject: [PATCH] [lldb] Add LanguageRuntime hooks for uncaught top-level
errors (NFC)
Code that runs as top-level code, such as a REPL line, has no
enclosing handler for an error it throws. In Swift, such an error
reaches swift_errorInMain, which ends the process. The Swift language
plugin sets an internal breakpoint there and completes the expression
with the error as its result, but does so by calling into the Swift
runtime from ThreadPlanCallFunction.
Add CreateErrorBackstopBreakpoint and GetErrorValueAtBackstop to
LanguageRuntime. ThreadPlanCallFunction sets the breakpoint for REPL
expressions, and LLVMUserExpression makes a caught error the result.
No runtime on the main branch implements the hooks, so behavior is
unchanged.
Assisted-by: Claude
---
lldb/include/lldb/Target/LanguageRuntime.h | 11 ++++++++
.../lldb/Target/ThreadPlanCallFunction.h | 9 +++++++
lldb/source/Expression/LLVMUserExpression.cpp | 22 ++++++++++++++++
lldb/source/Target/ThreadPlanCallFunction.cpp | 25 +++++++++++++++++++
4 files changed, 67 insertions(+)
diff --git a/lldb/include/lldb/Target/LanguageRuntime.h b/lldb/include/lldb/Target/LanguageRuntime.h
index 4af0957ae8b836..fa266d8dfc29d4 100644
--- a/lldb/include/lldb/Target/LanguageRuntime.h
+++ b/lldb/include/lldb/Target/LanguageRuntime.h
@@ -153,6 +153,17 @@ class LanguageRuntime : public Runtime, public PluginInterface {
return false;
}
+ /// Create a breakpoint that catches an error escaping an expression
+ /// that runs as top-level code, such as a REPL line, where no
+ /// handler encloses it.
+ virtual lldb::BreakpointSP CreateErrorBackstopBreakpoint() { return {}; }
+
+ /// Return the error value that reached the breakpoint from
+ /// CreateErrorBackstopBreakpoint, with \p frame stopped there.
+ virtual lldb::ValueObjectSP GetErrorValueAtBackstop(StackFrame &frame) {
+ return {};
+ }
+
static lldb::BreakpointSP
CreateExceptionBreakpoint(Target &target, lldb::LanguageType language,
bool catch_bp, bool throw_bp,
diff --git a/lldb/include/lldb/Target/ThreadPlanCallFunction.h b/lldb/include/lldb/Target/ThreadPlanCallFunction.h
index cb6e7caebb4adf..5ddd3160a72a6c 100644
--- a/lldb/include/lldb/Target/ThreadPlanCallFunction.h
+++ b/lldb/include/lldb/Target/ThreadPlanCallFunction.h
@@ -96,6 +96,10 @@ class ThreadPlanCallFunction : public ThreadPlan {
void SetStopOthers(bool new_value) override;
+ /// Whether the call completed by throwing an error that reached the
+ /// language runtime's error backstop.
+ bool HitErrorBackstop() const { return m_hit_error_backstop; }
+
protected:
void ReportRegisterState(const char *message);
@@ -143,6 +147,11 @@ class ThreadPlanCallFunction : public ThreadPlan {
bool m_should_clear_cxx_exception_bp;
lldb::addr_t m_stop_address; // This is the address we stopped at. Also set
// in DoTakedown;
+ /// The runtime that catches errors the expression doesn't handle, if it
+ /// runs as top-level code.
+ LanguageRuntime *m_error_backstop_runtime = nullptr;
+ lldb::BreakpointSP m_error_backstop_bp_sp;
+ bool m_hit_error_backstop = false;
private:
CompilerType m_return_type;
diff --git a/lldb/source/Expression/LLVMUserExpression.cpp b/lldb/source/Expression/LLVMUserExpression.cpp
index 8c9a5fefc17038..b4ec20ed32556b 100644
--- a/lldb/source/Expression/LLVMUserExpression.cpp
+++ b/lldb/source/Expression/LLVMUserExpression.cpp
@@ -93,6 +93,8 @@ LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
lldb::addr_t function_stack_bottom = LLDB_INVALID_ADDRESS;
lldb::addr_t function_stack_top = LLDB_INVALID_ADDRESS;
+ lldb::ValueObjectSP error_backstop_result_sp;
+
if (m_can_interpret) {
llvm::Module *module = m_execution_unit_sp->GetModule();
llvm::Function *function = m_execution_unit_sp->GetFunction();
@@ -237,6 +239,14 @@ LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
return execution_result;
}
+ if (execution_result == lldb::eExpressionCompleted &&
+ user_expression_plan->HitErrorBackstop()) {
+ lldb::ValueObjectSP error_sp =
+ user_expression_plan->GetReturnValueObject();
+ if (error_sp && error_sp->GetError().Success())
+ error_backstop_result_sp = error_sp;
+ }
+
if (execution_result == lldb::eExpressionThreadVanished) {
diagnostic_manager.Printf(lldb::eSeverityError,
"Couldn't execute expression: the thread on "
@@ -254,6 +264,18 @@ LLVMUserExpression::DoExecute(DiagnosticManager &diagnostic_manager,
}
}
+ if (error_backstop_result_sp) {
+ Target *target = exe_ctx.GetTargetPtr();
+ PersistentExpressionState *expression_state =
+ target->GetPersistentExpressionStateForLanguage(
+ Language().AsLanguageType());
+ if (expression_state)
+ result_sp =
+ expression_state->CreatePersistentVariable(error_backstop_result_sp);
+
+ return lldb::eExpressionCompleted;
+ }
+
if (FinalizeJITExecution(diagnostic_manager, exe_ctx, result_sp,
function_stack_bottom, function_stack_top))
return lldb::eExpressionCompleted;
diff --git a/lldb/source/Target/ThreadPlanCallFunction.cpp b/lldb/source/Target/ThreadPlanCallFunction.cpp
index c7e798e566c17b..cbcf8a1b190ce7 100644
--- a/lldb/source/Target/ThreadPlanCallFunction.cpp
+++ b/lldb/source/Target/ThreadPlanCallFunction.cpp
@@ -111,6 +111,11 @@ ThreadPlanCallFunction::ThreadPlanCallFunction(
m_should_clear_objc_exception_bp(false),
m_should_clear_cxx_exception_bp(false),
m_stop_address(LLDB_INVALID_ADDRESS), m_return_type(return_type) {
+ bool runs_as_top_level_code = options.GetREPLEnabled();
+ if (runs_as_top_level_code)
+ m_error_backstop_runtime =
+ m_process.GetLanguageRuntime(options.GetLanguage().AsLanguageType());
+
lldb::addr_t start_load_addr = LLDB_INVALID_ADDRESS;
lldb::addr_t function_load_addr = LLDB_INVALID_ADDRESS;
ABI *abi = nullptr;
@@ -439,6 +444,9 @@ void ThreadPlanCallFunction::SetBreakpoints() {
m_objc_language_runtime->SetExceptionBreakpoints();
}
}
+ if (m_error_backstop_runtime)
+ m_error_backstop_bp_sp =
+ m_error_backstop_runtime->CreateErrorBackstopBreakpoint();
}
void ThreadPlanCallFunction::ClearBreakpoints() {
@@ -448,6 +456,8 @@ void ThreadPlanCallFunction::ClearBreakpoints() {
if (m_objc_language_runtime && m_should_clear_objc_exception_bp)
m_objc_language_runtime->ClearExceptionBreakpoints();
}
+ if (m_error_backstop_bp_sp)
+ GetTarget().RemoveBreakpointByID(m_error_backstop_bp_sp->GetID());
}
bool ThreadPlanCallFunction::BreakpointsExplainStop() {
@@ -473,6 +483,21 @@ bool ThreadPlanCallFunction::BreakpointsExplainStop() {
return true;
}
}
+ if (m_error_backstop_bp_sp && stop_info_sp &&
+ stop_info_sp->GetStopReason() == eStopReasonBreakpoint &&
+ m_process.GetBreakpointSiteList().StopPointSiteContainsBreakpoint(
+ stop_info_sp->GetValue(), m_error_backstop_bp_sp->GetID())) {
+ // This was top-level code that threw an error.
+ SetPlanComplete(true);
+ if (StackFrameSP frame_sp = GetThread().GetStackFrameAtIndex(0)) {
+ m_return_valobj_sp =
+ m_error_backstop_runtime->GetErrorValueAtBackstop(*frame_sp);
+ if (m_return_valobj_sp) {
+ m_hit_error_backstop = true;
+ return true;
+ }
+ }
+ }
return false;
}
More information about the lldb-commits
mailing list