* [PATCH v2 1/1] gdb: discard saved state on error in recursive infcall
2026-11-25 14:10 [PATCH v2 0/1] gdb: discard saved state on error in recursive infcall Stephan Rohr
@ 2026-11-25 14:10 ` Stephan Rohr
0 siblings, 0 replies; 2+ messages in thread
From: Stephan Rohr @ 2026-11-25 14:10 UTC (permalink / raw)
To: gdb-patches
From: "Rohr, Stephan" <stephan.rohr@intel.com>
GDB invokes a ctor for all arguments not trivially copyable in an
inferior call. The caller's registers and other state are restored,
even if an exception inside the callee is raised. This hides the
correct frame from the user in certain scenarios, e.g., when switching
inferiors. The extra frame-cache reinit re-reads the restored registers
such that 'backtrace' only shows the 'main' frame instead of the
breakpoint hit in the inferior call.
Discard the saved state on gdb_exception_error when invoking the ctor in
inferior calls.
---
gdb/infcall.c | 23 +++++++++++++++++++++--
gdb/testsuite/gdb.cp/pass-by-ref-2.exp | 22 ++++++++++++++++++++++
2 files changed, 43 insertions(+), 2 deletions(-)
diff --git a/gdb/infcall.c b/gdb/infcall.c
index 5f27f5ad259..de641a05675 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -1410,8 +1410,27 @@ call_function_by_hand_dummy (struct value *function,
"constructor for the type '%s' could not be found "
"(maybe inlined?)"), param_type->name ());
- call_function_by_hand (copy_ctor, default_return_type,
- gdb::make_span (cctor_args, 2));
+ try
+ {
+ call_function_by_hand (copy_ctor, default_return_type,
+ gdb::make_span (cctor_args, 2));
+ }
+ catch (const gdb_exception_error &)
+ {
+ /* We hit an error in the ctor.
+
+ Discard inferior status, we're not at the same point
+ we started at. */
+ discard_infcall_control_state (inf_status.release ());
+
+ /* The nested call of the copy constructor may have set up a
+ dummy frame before throwing the error. Restoring the
+ caller's registers would leave that dummy frame unreachable
+ by the unwinder once the frame cache is invalidated. */
+ discard_infcall_suspend_state (caller_state.release ());
+
+ throw;
+ }
}
/* If the argument has a destructor, remember it so that we
diff --git a/gdb/testsuite/gdb.cp/pass-by-ref-2.exp b/gdb/testsuite/gdb.cp/pass-by-ref-2.exp
index 3d704635901..42b67c1fba1 100644
--- a/gdb/testsuite/gdb.cp/pass-by-ref-2.exp
+++ b/gdb/testsuite/gdb.cp/pass-by-ref-2.exp
@@ -109,8 +109,30 @@ gdb_breakpoint $bp_location
gdb_test "print cbvArrayContainerByRef (arrayContainerByRef)" \
".*The program being debugged stopped.*" \
"call cbvArrayContainerByRef with BP"
+
+# Switching inferiors reinitializes the frame cache.
+gdb_test "add-inferior"
+gdb_test "inferior 2"
+gdb_test "inferior 1"
+
gdb_test "backtrace" [multi_line \
"#0 ByRef\:\:ByRef .* at .*$srcfile:$bp_location" \
"#1 .* ArrayContainerByRef::ArrayContainerByRef .*" \
"#2 <function called from gdb>" \
"#3 main.*"]
+
+# Test GDB is able to finish the abandoned inferior call.
+
+delete_breakpoints
+
+gdb_test "finish" [multi_line \
+ "Run till exit from #0 ByRef\:\:ByRef .* at .*$srcfile:$bp_location" \
+ ".* in ArrayContainerByRef::ArrayContainerByRef .* at .*$srcfile:.*"] \
+ "finish frame #0"
+
+gdb_test "finish" [multi_line \
+ "Run till exit from #0 .* in ArrayContainerByRef::ArrayContainerByRef .* at .*$srcfile:.*" \
+ "main \\(\\) at .*$srcfile:.*"] \
+ "finish frame #1"
+
+gdb_continue_to_end
--
2.43.0
________________________________________
Intel Deutschland GmbH
Registered Address: Dornacher Strasse 1, 85622 Feldkirchen, Germany
Tel: +49 (89) 99143-0
www.intel.de
Managing Directors: Candice Moore, Jeffrey Schneiderman, Ramachandran Sitaraman
Chairperson of the Supervisory Board: Sonja Pierer
Registered Seat: Munich Commercial Register B: Amtsgericht Munich HRB 186928
This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.
^ permalink raw reply [flat|nested] 2+ messages in thread