[05/11,gdb/testsuite] Add transparent_uplevel

Message ID 20260824135855.1195963-6-tdevries@suse.de
State New
Headers
Series Refactor exception handling |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Tom de Vries Aug. 24, 2026, 1:58 p.m. UTC
  Having learned the trick of increasing -level, factor out a new proc
transparent_uplevel:
...
proc transparent_uplevel { body } {
    catch {uplevel 2 $body} result opts
    return -options [dict incr opts -level 2] $result
...
to be used in conjunction with try/finally to apply this pattern:
...
proc foo { body } {
    <do something>
    try {
	transparent_uplevel $body
    } finally {
      <do something else>
    }
}
...
such that "foo body" has the same effect as "body", apart from the
<do something>/<do something else> parts.

This proc has the fix for PR34553 designed into it, so (correctly) applying it
in a proc will fix the PR there.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34553
---
 gdb/testsuite/lib/gdb.exp | 47 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
  

Comments

Keith Seitz Sept. 1, 2026, 4:16 p.m. UTC | #1
Hi,

On 8/24/26 6:58 AM, Tom de Vries wrote:
> Having learned the trick of increasing -level, factor out a new proc
> transparent_uplevel:
> ...
> proc transparent_uplevel { body } {
>      catch {uplevel 2 $body} result opts
>      return -options [dict incr opts -level 2] $result
> ...
> to be used in conjunction with try/finally to apply this pattern:
> ...
> proc foo { body } {
>      <do something>
>      try {
> 	transparent_uplevel $body
>      } finally {
>        <do something else>
>      }
> }
> ...
> such that "foo body" has the same effect as "body", apart from the
> <do something>/<do something else> parts.
> 
> This proc has the fix for PR34553 designed into it, so (correctly) applying it
> in a proc will fix the PR there.
> 
> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34553
> ---
>   gdb/testsuite/lib/gdb.exp | 47 +++++++++++++++++++++++++++++++++++++++
>   1 file changed, 47 insertions(+)
> 
> diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
> index 88a42aff4ac..853b45f4a53 100644
> --- a/gdb/testsuite/lib/gdb.exp
> +++ b/gdb/testsuite/lib/gdb.exp
> @@ -3218,6 +3218,53 @@ gdb_caching_proc allow_tui_tests {} {
>       return [expr {[string first "--enable-tui" $output] != -1}]
>   }
>   
> +# Run BODY in the context of the callers caller.

Typo: "caller's caller".

> +#
> +# To be used in a proc foo:
> +#   proc foo {body} {
> +#       transparent_uplevel body
> +#   }

I think "body" here is missing a '$'?

Keith

> +# to make "foo body" have the same effect as "body".
> +#
> +# It ensures this by doing two things:
> +# - evaluating the body in the context of the caller of foo
> +# - propagating any return, exceptional or not to the caller of foo.
> +#
> +# Typically used in conjunction with try/finally, to ensure something
> +# happens before and after body.  For instance, this:
> +#   proc foo {body} {
> +#       try {
> +#           puts "foo: enter"
> +#           transparent_uplevel $body
> +#       } finally {
> +#           puts "foo: exit"
> +#       }
> +#   }
> +#
> +#   for {set i 0} {$i < 5} {incr i} {
> +#       puts $i
> +#       foo {
> +#           if {$i == 2} {
> +#               break
> +#           }
> +#       }
> +#   }
> +# produces:
> +#   0
> +#   foo: enter
> +#   foo: exit
> +#   1
> +#   foo: enter
> +#   foo: exit
> +#   2
> +#   foo: enter
> +#   foo: exit
> +
> +proc transparent_uplevel { body } {
> +    catch {uplevel 2 $body} result opts
> +    return -options [dict incr opts -level 2] $result
> +}
> +
>   # Test files shall make sure all the test result lines in gdb.sum are
>   # unique in a test run, so that comparing the gdb.sum files of two
>   # test runs gives correct results.  Test files that exercise
  

Patch

diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 88a42aff4ac..853b45f4a53 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -3218,6 +3218,53 @@  gdb_caching_proc allow_tui_tests {} {
     return [expr {[string first "--enable-tui" $output] != -1}]
 }
 
+# Run BODY in the context of the callers caller.
+#
+# To be used in a proc foo:
+#   proc foo {body} {
+#       transparent_uplevel body
+#   }
+# to make "foo body" have the same effect as "body".
+#
+# It ensures this by doing two things:
+# - evaluating the body in the context of the caller of foo
+# - propagating any return, exceptional or not to the caller of foo.
+#
+# Typically used in conjunction with try/finally, to ensure something
+# happens before and after body.  For instance, this:
+#   proc foo {body} {
+#       try {
+#           puts "foo: enter"
+#           transparent_uplevel $body
+#       } finally {
+#           puts "foo: exit"
+#       }
+#   }
+#
+#   for {set i 0} {$i < 5} {incr i} {
+#       puts $i
+#       foo {
+#           if {$i == 2} {
+#               break
+#           }
+#       }
+#   }
+# produces:
+#   0
+#   foo: enter
+#   foo: exit
+#   1
+#   foo: enter
+#   foo: exit
+#   2
+#   foo: enter
+#   foo: exit
+
+proc transparent_uplevel { body } {
+    catch {uplevel 2 $body} result opts
+    return -options [dict incr opts -level 2] $result
+}
+
 # Test files shall make sure all the test result lines in gdb.sum are
 # unique in a test run, so that comparing the gdb.sum files of two
 # test runs gives correct results.  Test files that exercise