From patchwork Wed Jan 18 16:18:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 63353 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E4C1A38493D8 for ; Wed, 18 Jan 2023 16:19:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E4C1A38493D8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1674058796; bh=n7SVXPwMyHAjtQvoW/Zp88CWi0lLb7wYMJ6UA8AhtNQ=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=yiJrBvKDSWt8g7hZ4wkQrTTKTJuDfeMKPm5IPMTkpHnFqMHpnlt3qB7SszN5Kex3W CpRhyo3BrpoDm4SizLFN/P616xWJpTkBNNQB1J5IDPLmsZgJBA/2DlshLZZdH3Ii62 lmJ0HnkY9UoBaycjlP/RrC8hBSjAHzFTdgQ8+6u8= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4BD16385842E for ; Wed, 18 Jan 2023 16:18:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4BD16385842E Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-80-SWAs52n5OHigcEid5wEYnQ-1; Wed, 18 Jan 2023 11:18:41 -0500 X-MC-Unique: SWAs52n5OHigcEid5wEYnQ-1 Received: by mail-qk1-f199.google.com with SMTP id bi3-20020a05620a318300b00702545f73d5so25059673qkb.8 for ; Wed, 18 Jan 2023 08:18:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=n7SVXPwMyHAjtQvoW/Zp88CWi0lLb7wYMJ6UA8AhtNQ=; b=zmjrBFdO+EjAEuM/L3JyC9OsDLE8/7/yD7DFRsSWx/oKD9OGr3v8irCinqwy955rEH AlsScy/AQDDOV/o+MleYv4X5+3fxX/gO9I1EC2+m0Ugu/Zw12J0zW3/1/gRrHju2iDEz eLotKXDXzL5CgaeJp8h8AhoICoFHAmEJQ2TBipKXgcWBD4svMAGXi/3KT/1IW5cWDlnE PZOSwqRq4XtLrdtk1L0kyXrePALPePtoGGXp6Q7GvpPYfwcTn3jdUP9j4UXkNjv+osBN 4a1YBVeR48pgC1f70bq0r6tBKrB7pIimwy+LNO0L8Zav/UWmhPAMb/xZsP63R0OqoGcS Z/Rg== X-Gm-Message-State: AFqh2kq9SpWbzKlrdAMcHO96GjK+MqaM/4DOm8fKHaVftRpWJRP3EAe3 nihSaGWeInjxABQvV5AjDq10Tm8TMtD7pOGNIULeFI/vADqsN7+LyctVtpl3XwN4Hg9nUYNqyLp RnQP/o5hO+Dunb9uBMgdrZIqYhQ1fJ1Mqq5WSMAUwVapU6uZpTUhooe+jk+LVBmWrZ/5neQBwnA == X-Received: by 2002:a05:622a:598b:b0:3b6:30e1:f480 with SMTP id gb11-20020a05622a598b00b003b630e1f480mr10178272qtb.32.1674058720340; Wed, 18 Jan 2023 08:18:40 -0800 (PST) X-Google-Smtp-Source: AMrXdXuqsSpBL4QFDacqEM/eekaKo0WLKuDzVVALA4yt0EPEEEHi80OKLqWfu+xhqyfrx2EDiU3MCg== X-Received: by 2002:a05:622a:598b:b0:3b6:30e1:f480 with SMTP id gb11-20020a05622a598b00b003b630e1f480mr10178229qtb.32.1674058719751; Wed, 18 Jan 2023 08:18:39 -0800 (PST) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id o24-20020ac85558000000b003b646a99aa6sm1429889qtr.77.2023.01.18.08.18.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 08:18:39 -0800 (PST) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 12/13] gdb: introduce unwind-on-timeout setting Date: Wed, 18 Jan 2023 16:18:08 +0000 Message-Id: <0a8172f909c477fc7bab48c99df5860c887b8024.1674058360.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" Now that inferior function calls can timeout (see the recent introduction of direct-call-timeout and indirect-call-timeout), this commit adds a new setting unwind-on-timeout. This new setting is just like the existing unwindonsignal and unwind-on-terminating-exception, but the new setting will cause GDB to unwind the stack if an inferior function call times out. The existing inferior function call timeout tests have been updated to cover the new setting. --- gdb/NEWS | 9 +++ gdb/doc/gdb.texinfo | 33 +++++++--- gdb/infcall.c | 62 ++++++++++++++++--- gdb/testsuite/gdb.base/infcall-timeout.exp | 49 +++++++++++---- .../infcall-from-bp-cond-timeout.exp | 55 +++++++++++----- 5 files changed, 164 insertions(+), 44 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 2689569a8f3..511a750407a 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -35,6 +35,15 @@ show indirect-call-timeout ignored, GDB will wait indefinitely for an inferior function to complete, unless interrupted by the user using Ctrl-C. +set unwind-on-timeout on|off +show unwind-on-timeout + These commands control whether GDB should unwind the stack when a + timeout occurs during an inferior function call. The default is + off, in which case the inferior will remain in the frame where the + timeout occurred. When on GDB will unwind the stack remocing the + dummy frame that was added for the inferior call, and restoring the + inferior state to how it was before the inferior call started. + * MI changes ** mi now reports 'no-history' as a stop reason when hitting the end of the diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 9eb8b1db00e..aa88221677e 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -5719,10 +5719,10 @@ the called function, or the called function may receive a signal (e.g.@ a @code{SIGSEGV}) as a result of some undefined behaviour, if this happens then @value{GDBN} will stop. Depending on the settings -@code{unwindonsignal} and @code{unwind-on-terminating-exception} -(@pxref{Calling,,Calling Program Functions}) @value{GDBN} may unwind -the stack back to the breakpoint location, or may leave the program at -the frame where the stop occurred. +@code{unwindonsignal}, @code{unwind-on-terminating-exception}, and +@code{unwind-on-timeout} (@pxref{Calling,,Calling Program Functions}) +@value{GDBN} may unwind the stack back to the breakpoint location, or +may leave the program at the frame where the stop occurred. Breakpoint conditions can also be evaluated on the target's side if the target supports it. Instead of evaluating the conditions locally, @@ -20718,6 +20718,21 @@ Show the current setting of stack unwinding in the functions called by @value{GDBN}. +@item set unwind-on-timeout +@kindex set unwind-on-timeout +@cindex unwind stack in called functions when timing out +@cindex call dummy stack unwinding on timeout. +Set unwinding of the stack if a function called from @value{GDBN} +times out. If set to @code{off} (the default), @value{GDBN} stops in +the frame where the timeout occurred. If set to @code{on}, +@value{GDBN} unwinds the stack it created for the call and restores +the context to what it was before the call. + +@item show unwind-on-timeout +@kindex show unwind-on-timeout +Show whether @value{GDBN} will unwind the stack if a function called +from @value{GDBN} times out. + @item set may-call-functions @kindex set may-call-functions @cindex disabling calling functions in the program @@ -20748,11 +20763,11 @@ call by typing the interrupt character (often @kbd{Ctrl-c}). If a called function is interrupted for any reason, and the stack is -not unwound (due to @code{set unwind-on-terminating-exception on} or -@code{set unwindonsignal on}), then the dummy-frame, created by -@value{GDBN} to facilitate the call to the program function, will be -visible in the backtrace, for example frame @code{#3} in the following -backtrace: +not unwound (due to @code{set unwind-on-terminating-exception on}, +@code{set unwind-on-timeout}, or @code{set unwindonsignal on}), then +the dummy-frame, created by @value{GDBN} to facilitate the call to the +program function, will be visible in the backtrace, for example frame +@code{#3} in the following backtrace: @smallexample (@value{GDBP}) backtrace diff --git a/gdb/infcall.c b/gdb/infcall.c index 9afca4f5bc6..272e3cccf87 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -217,6 +217,27 @@ show_unwind_on_terminating_exception_p (struct ui_file *file, int from_tty, value); } +/* This boolean tells what gdb should do if a signal is received while + in a function called from gdb (call dummy). If set, gdb unwinds + the stack and restore the context to what as it was before the + call. + + The default is to stop in the frame where the signal was received. */ + +static bool unwind_on_timeout_p = false; + +/* Implement 'show unwind-on-timeout'. */ + +static void +show_unwind_on_timeout_p (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + gdb_printf (file, + _("Unwinding of stack if a timeout occurs " + "while in a call dummy is %s.\n"), + value); +} + /* Perform the standard coercions that are specified for arguments to be passed to C, Ada or Fortran functions. @@ -1691,14 +1712,27 @@ When the function is done executing, GDB will silently stop."), /* A timeout results in a signal being sent to the inferior. */ gdb_assert (stopped_by_random_signal); - /* Indentation is weird here. A later patch is going to move the - following block into an if/else, so I'm leaving the indentation - here to minimise the later patch. + if (unwind_on_timeout_p) + { + /* The user wants the context restored. */ + + /* We must get back to the frame we were before the + dummy call. */ + dummy_frame_pop (dummy_id, call_thread.get ()); + + /* We also need to restore inferior status to that before the + dummy call. */ + restore_infcall_control_state (inf_status.release ()); - Also, the error message used below refers to 'set - unwind-on-timeout' which doesn't exist yet. This will be added - in a later commit, I'm leaving this in for now to minimise the - churn caused by the commit that adds unwind-on-timeout. */ + error (_("\ +The program being debugged timed out while in a function called from GDB.\n\ +GDB has restored the context to what it was before the call.\n\ +To change this behavior use \"set unwind-on-timeout off\".\n\ +Evaluation of the expression containing the function\n\ +(%s) will be abandoned."), + name.c_str ()); + } + else { /* The user wants to stay in the frame where we stopped (default). Discard inferior status, we're not at the same @@ -1824,6 +1858,20 @@ The default is to unwind the frame."), show_unwind_on_terminating_exception_p, &setlist, &showlist); + add_setshow_boolean_cmd ("unwind-on-timeout", no_class, + &unwind_on_timeout_p, _("\ +Set unwinding of stack if a timeout occurs while in a call dummy."), _("\ +Show unwinding of stack if a timeout occurs while in a call dummy."), + _("\ +The unwind on timeout flag lets the user determine what gdb should do if\n\ +gdb times out while in a function called from gdb. If set, gdb unwinds\n\ +the stack and restores the context to what it was before the call. If\n\ +unset, gdb leaves the inferior in the frame where the timeout occurred.\n\ +The default is to stop in the frame where the timeout occurred."), + NULL, + show_unwind_on_timeout_p, + &setlist, &showlist); + add_setshow_uinteger_cmd ("direct-call-timeout", no_class, &direct_call_timeout, _("\ Set the timeout, for direct calls to inferior function calls."), _("\ diff --git a/gdb/testsuite/gdb.base/infcall-timeout.exp b/gdb/testsuite/gdb.base/infcall-timeout.exp index bd6b2bfac3e..a4d9fd6ee61 100644 --- a/gdb/testsuite/gdb.base/infcall-timeout.exp +++ b/gdb/testsuite/gdb.base/infcall-timeout.exp @@ -28,7 +28,11 @@ if { [build_executable "failed to prepare" ${binfile} "${srcfile}" \ # the direct-call-timeout, and make an inferior function call that # will never return. GDB should eventually timeout and stop the # inferior. -proc_with_prefix run_test { target_async target_non_stop } { +# +# When UNWIND is "off" the inferior wil be left in the frame where the +# timeout occurs, otherwise, when UNWIND is "on", GDB should unwind +# back to the frame where the inferior call was made. +proc_with_prefix run_test { target_async target_non_stop unwind } { save_vars { ::GDBFLAGS } { append ::GDBFLAGS \ " -ex \"maint set target-non-stop $target_non_stop\"" @@ -44,19 +48,36 @@ proc_with_prefix run_test { target_async target_non_stop } { } gdb_test_no_output "set direct-call-timeout 5" + gdb_test_no_output "set unwind-on-timeout $unwind" + + if { $unwind } { + gdb_test "print function_that_never_returns ()" \ + [multi_line \ + "Program stopped\\." \ + ".*" \ + "The program being debugged timed out while in a function called from GDB\\." \ + "GDB has restored the context to what it was before the call\\." \ + "To change this behavior use \"set unwind-on-timeout off\"\\." \ + "Evaluation of the expression containing the function" \ + "\\(function_that_never_returns\\) will be abandoned\\."] - gdb_test "print function_that_never_returns ()" \ - [multi_line \ - "Program stopped\\." \ - ".*" \ - "The program being debugged timed out while in a function called from GDB\\." \ - "GDB remains in the frame where the timeout occurred\\." \ - "To change this behavior use \"set unwind-on-timeout on\"\\." \ - "Evaluation of the expression containing the function" \ - "\\(function_that_never_returns\\) will be abandoned\\." \ - "When the function is done executing, GDB will silently stop\\."] + gdb_test "bt" \ + "#0\\s+main \\(\\).*" + } else { + gdb_test "print function_that_never_returns ()" \ + [multi_line \ + "Program stopped\\." \ + ".*" \ + "The program being debugged timed out while in a function called from GDB\\." \ + "GDB remains in the frame where the timeout occurred\\." \ + "To change this behavior use \"set unwind-on-timeout on\"\\." \ + "Evaluation of the expression containing the function" \ + "\\(function_that_never_returns\\) will be abandoned\\." \ + "When the function is done executing, GDB will silently stop\\."] - gdb_test "bt" ".* function_that_never_returns .*.*" + gdb_test "bt" \ + ".* function_that_never_returns .*.*" + } } foreach_with_prefix target_async { "on" "off" } { @@ -70,6 +91,8 @@ foreach_with_prefix target_async { "on" "off" } { } foreach_with_prefix target_non_stop { "on" "off" } { - run_test $target_async $target_non_stop + foreach_with_prefix unwind { "on" "off" } { + run_test $target_async $target_non_stop $unwind + } } } diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.exp b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.exp index 9ba38e6896a..847a850673d 100644 --- a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.exp +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.exp @@ -41,7 +41,12 @@ set segfault_line [gdb_get_line_number "Segfault here"] # thread, on which the inferior call relies, either hits a breakpoint # (when OTHER_THREAD_BP is true), or crashes (when OTHER_THREAD_BP is # false). -proc run_test { target_async target_non_stop other_thread_bp } { +# +# When UNWIND is "on" GDB will unwind the thread which performed the +# inferior function call back to the state where the inferior call was +# made (when the inferior call times out). Otherwise, when UNWIND is +# "off", the inferior is left in the frame where the timeout occurred. +proc run_test { target_async target_non_stop other_thread_bp unwind } { save_vars { ::GDBFLAGS } { append ::GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\"" append ::GDBFLAGS " -ex \"maintenance set target-async ${target_async}\"" @@ -72,6 +77,7 @@ proc run_test { target_async target_non_stop other_thread_bp } { # for this timeout. For now though, we just hope 5 seconds is # enough. gdb_test_no_output "set indirect-call-timeout 5" + gdb_test_no_output "set unwind-on-timeout $unwind" gdb_breakpoint \ "${::srcfile}:${::cond_bp_line} if (condition_func ())" @@ -92,18 +98,35 @@ proc run_test { target_async target_non_stop other_thread_bp } { "get number for segfault breakpoint"] } - gdb_test "continue" \ - [multi_line \ - "Thread ${::decimal} \"\[^\r\n\"\]+\" stopped\\." \ - ".*" \ - "Error in testing condition for breakpoint ${bp_num}:" \ - "The program being debugged timed out while in a function called from GDB\\." \ - "GDB remains in the frame where the timeout occurred\\." \ - "To change this behavior use \"set unwind-on-timeout on\"\\." \ - "Evaluation of the expression containing the function" \ - "\\(condition_func\\) will be abandoned\\." \ - "When the function is done executing, GDB will silently stop\\."] \ - "expected timeout waiting for inferior call to complete" + if { $unwind } { + gdb_test "continue" \ + [multi_line \ + "Thread ${::decimal} \"\[^\r\n\"\]+\" stopped\\." \ + ".*" \ + "Error in testing condition for breakpoint ${bp_num}:" \ + "The program being debugged timed out while in a function called from GDB\\." \ + "GDB has restored the context to what it was before the call\\." \ + "To change this behavior use \"set unwind-on-timeout off\"\\." \ + "Evaluation of the expression containing the function" \ + "\\(condition_func\\) will be abandoned\\." \ + "" \ + "Thread ${::decimal}\[^\r\n\]*hit Breakpoint ${bp_num}, \[^\r\n\]+" \ + "\[^\r\n\]+ Conditional breakpoint here\\. \[^\r\n\]+"] \ + "expected timeout waiting for inferior call to complete" + } else { + gdb_test "continue" \ + [multi_line \ + "Thread ${::decimal} \"\[^\r\n\"\]+\" stopped\\." \ + ".*" \ + "Error in testing condition for breakpoint ${bp_num}:" \ + "The program being debugged timed out while in a function called from GDB\\." \ + "GDB remains in the frame where the timeout occurred\\." \ + "To change this behavior use \"set unwind-on-timeout on\"\\." \ + "Evaluation of the expression containing the function" \ + "\\(condition_func\\) will be abandoned\\." \ + "When the function is done executing, GDB will silently stop\\."] \ + "expected timeout waiting for inferior call to complete" + } # Remember that other thread that either crashed (with a segfault) # or hit a breakpoint? Now that the inferior call has timed out, @@ -142,8 +165,10 @@ foreach_with_prefix target_async {"on" "off" } { } foreach_with_prefix target_non_stop {"off" "on"} { - foreach_with_prefix other_thread_bp { true false } { - run_test $target_async $target_non_stop $other_thread_bp + foreach_with_prefix unwind {"off" "on"} { + foreach_with_prefix other_thread_bp { true false } { + run_test $target_async $target_non_stop $other_thread_bp $unwind + } } } }