From patchwork Sat Dec 2 10:52:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 81194 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 A01D63861803 for ; Sat, 2 Dec 2023 10:53:11 +0000 (GMT) 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 5F5DD385842E for ; Sat, 2 Dec 2023 10:52:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5F5DD385842E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5F5DD385842E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514364; cv=none; b=Et4ZjSLtGukhnddMNkulhvytnvckemK0eUWpVPqJBa502OTWnm/6pBU4PbPHXm9zQipT1FwhvPtxqQsFzSRtYp1qoGk/tm6uGKrV4jy5ZaDiT3HgJz+HKd9du/cbFO5TX5O/ko9cWGWrnFHbBmoaJYTIoyxIIPoLd+cSnID5c58= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514364; c=relaxed/simple; bh=37bgEsICtTrrqZK/dRqYH/z2o+Ydr3AHF2SF6Gwu2MY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=LD/gxyzedkPhitXiEpIYPwrSNdhuR1rhuRBarB0HgfypCKj8zILsq6Ja8g/29HoFfXlGmIHHamDwRpbMAZKxYJ2EZDpGX7pCbVfaGL4rq3Qxth6wZ/a1puj63YmP6EMdCcGuFwQjJaAoNz+JK9szKeXZuRFoCvwzuKBn1gmWT3M= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701514363; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pJMEDZO/YfIvQ8BxnYGw7L8d6eGBsR9Lw+57W9O+Jmc=; b=ZBm4FRD/zdlESsrIGMh5+tuaGQqfmdA8yoTjV9kgVZitwvXgzyU9lKMHbyg7Dd1Ir+xAdn o3Upe8zbw1XXnHExK3bHHdltMRQzmwhyPlF6Vmd4Zd3wCoOvIPMkljbMImGlQiAw+68SBg 9AhbDMyzTUkdnnJ7uz6urhGJaITrhc4= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-391-UsV8W55qOtaEe3PIg3ibcw-1; Sat, 02 Dec 2023 05:52:41 -0500 X-MC-Unique: UsV8W55qOtaEe3PIg3ibcw-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-40b357e2a01so19235465e9.1 for ; Sat, 02 Dec 2023 02:52:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701514360; x=1702119160; 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=pJMEDZO/YfIvQ8BxnYGw7L8d6eGBsR9Lw+57W9O+Jmc=; b=IyTlTGgZLmsMVS1Hngk/ABGLFBK41j8c8w1JGWH+P5mLwoTrJvzKrpfsn0yQuYmWyf 1SZ3H8H8KdigO538/43TH1JYCIX7t6gwrdrulEtn8e8vv6UNJtBZI4s7gZGfoBp82Qgf vK2IHX4KIZgN9DKmatUbP7qJ31UQ6TRuawtoi9xY/yM8NwCD74PfPca9GxG7WDU4Qzj2 lOFkyCy3mMiGj/oEIlNrEOB1nEaAD0X8nhXyR4YGrCZRGP8y0bSpaDKKy0kSzkRbTCjc ud/2nPsylj3jXMrXjrm5Vp4jL6iCDDH4h9hfLTewBjnP0XCM+X19RFqigVNIoD5u2t8I X2JQ== X-Gm-Message-State: AOJu0YwnOFjmvQlOMyiLhFDKOsOwBJxMIuDyZH2NS5YXHLXwNRL3M7og n6Xg5MDTWND/TnPd8zzAgtv3iCzkLSlxPUIZEmbcZP8FNSyckMGRGnJ2KgoFv+M3gezXNj4YjKg JOhL4hAaMLxCiXPV5r4Dze3EcUfNl7P80fRqxsO6T8i09Fo18xP8XbODz5+1HVAuUQmeIOP8GWS sElMbzeQ== X-Received: by 2002:a7b:cc0c:0:b0:40b:5e4a:40b0 with SMTP id f12-20020a7bcc0c000000b0040b5e4a40b0mr759643wmh.208.1701514360581; Sat, 02 Dec 2023 02:52:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IEpruTLvQ6kLY/GhkDOSqXtciFqMNVqk1Vn88AwvEiAAmGGuZwK0+eV76hBNzjPX2UX1t6Hxw== X-Received: by 2002:a7b:cc0c:0:b0:40b:5e4a:40b0 with SMTP id f12-20020a7bcc0c000000b0040b5e4a40b0mr759638wmh.208.1701514360267; Sat, 02 Dec 2023 02:52:40 -0800 (PST) Received: from localhost (92.40.184.5.threembb.co.uk. [92.40.184.5]) by smtp.gmail.com with ESMTPSA id gq18-20020a170906e25200b00a0988d69549sm2919490ejb.189.2023.12.02.02.52.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 02:52:39 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Tankut Baris Aktemur Subject: [PATCHv9 1/5] Revert "gdb: remove unnecessary parameter wait_ptid from do_target_wait" Date: Sat, 2 Dec 2023 10:52:30 +0000 Message-Id: <50f6f3a71303cb84646a6761dd53a56a3b6ca8c3.1701514066.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=-8.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This reverts commit ac0d67ed1dcf470bad6a3bc4800c2ddc9bedecca. There was nothing wrong with the commit which I'm reverting here, but it removed some functionality that will be needed for a later commit; that is, the ability for GDB to ask for events from a specific ptid_t via the do_target_wait function. In a follow up commit, this functionality will be used to implement inferior function calls in multi-threaded inferiors. This is not a straight revert of the above commit. Reverting the above commit replaces a 'nullptr' with 'NULL', I've gone in and changed that, preserving the 'nullptr'. Reviewed-By: Tankut Baris Aktemur --- gdb/infrun.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/gdb/infrun.c b/gdb/infrun.c index 45c1b4a79bb..8ef22c6453a 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -4118,7 +4118,8 @@ do_target_wait_1 (inferior *inf, ptid_t ptid, more events. Polls for events from all inferiors/targets. */ static bool -do_target_wait (execution_control_state *ecs, target_wait_flags options) +do_target_wait (ptid_t wait_ptid, execution_control_state *ecs, + target_wait_flags options) { int num_inferiors = 0; int random_selector; @@ -4128,9 +4129,10 @@ do_target_wait (execution_control_state *ecs, target_wait_flags options) polling the rest of the inferior list starting from that one in a circular fashion until the whole list is polled once. */ - auto inferior_matches = [] (inferior *inf) + auto inferior_matches = [&wait_ptid] (inferior *inf) { - return inf->process_target () != nullptr; + return (inf->process_target () != nullptr + && ptid_t (inf->pid).matches (wait_ptid)); }; /* First see how many matching inferiors we have. */ @@ -4169,7 +4171,7 @@ do_target_wait (execution_control_state *ecs, target_wait_flags options) auto do_wait = [&] (inferior *inf) { - ecs->ptid = do_target_wait_1 (inf, minus_one_ptid, &ecs->ws, options); + ecs->ptid = do_target_wait_1 (inf, wait_ptid, &ecs->ws, options); ecs->target = inf->process_target (); return (ecs->ws.kind () != TARGET_WAITKIND_IGNORE); }; @@ -4619,7 +4621,7 @@ fetch_inferior_event () the event. */ scoped_disable_commit_resumed disable_commit_resumed ("handling event"); - if (!do_target_wait (&ecs, TARGET_WNOHANG)) + if (!do_target_wait (minus_one_ptid, &ecs, TARGET_WNOHANG)) { infrun_debug_printf ("do_target_wait returned no event"); disable_commit_resumed.reset_and_commit (); From patchwork Sat Dec 2 10:52:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 81195 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 AD538386189B for ; Sat, 2 Dec 2023 10:53:11 +0000 (GMT) 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 682003857BAB for ; Sat, 2 Dec 2023 10:52:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 682003857BAB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 682003857BAB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514373; cv=none; b=uepiCtWWVtwA/ug79E2DoMo93fssMxVHzsXxXv3+EuJhxgIU/MJWIPdThu9eBDg0G7kLCcOBXXs1B41VGIi4gHCHJYvD1ZQ1WIqNhjqtN49EhqoJMelhJkNYOfjSMhuZXw7iKQeH8ay5hhPxGPEiVGtzJCoDuoIt94Y37FVUaK8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514373; c=relaxed/simple; bh=LY5r5gF/jC6Kv+cTNNz6UMYPGU3cInOoXpZ7B5EYHkw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=LRty1Fbm//OMWauJRdef+Kl+2kZeV+failxkNRxGfV6a9/7fPs62RHFfKh2t473n/JArZVcZOfBjf4i/x6aKU/4S/QbN/3fZfdlyUpsODG2/OPxDgK++4y2q5CMaNMjF/JpWTjd45W09NlFk3gvXneAW+dwNYj4MtWBQaL5cc2U= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701514368; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KjJ8AtH25wlLFH69tVHuWs6GueAFRL+Uf5SXL/5JQY4=; b=BEW84E2MYlDaQ7xcLwksatwexx8NVdM/zh0XHUs0OpKl5ZVn9zzOwkDAQsi/CJRE2G3N7w F5KJ3E6MNelQgVZGcD3cMxDqFmqt1JwSRioSLzwMRJ+g8x6k3qWGIn793jVI8wHaRyWwcG iQAeuV4rYIk6jzXNeACmop0tiUVapFg= Received: from mail-lf1-f71.google.com (mail-lf1-f71.google.com [209.85.167.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-634-ApNZPKYHMLupBhTC-iM4GQ-1; Sat, 02 Dec 2023 05:52:46 -0500 X-MC-Unique: ApNZPKYHMLupBhTC-iM4GQ-1 Received: by mail-lf1-f71.google.com with SMTP id 2adb3069b0e04-50ba80696b3so3245108e87.0 for ; Sat, 02 Dec 2023 02:52:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701514364; x=1702119164; 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=KjJ8AtH25wlLFH69tVHuWs6GueAFRL+Uf5SXL/5JQY4=; b=szOXtxs6k7ieBlnqu6/MZPPkOuU/j/CL7js2CD92tQh2JL7+kSk0vZeVDSi2ZAFu3K 9tWhGlM4zPClO/qhfSvIjYGXzPFFxQM3g4gPWL/vzf+iQJ21mXJwG+Icq0BgEcyR7oLN 4CfKit+s3WaxYvLu+wwkAGaO41LO77Z7l44cwczszGOW0bAzDsMZOseqYMJEJExGih38 ubbHCksFdYlfFeoK1kOiKKoN0E5Zd1Muq5gBFmzeK65GUSB27whL+wD3mDdcOxYnmG91 BQSSAbJ3AD9ngVyyRrMMDFF0Oh/t2lV78MPU1KrJfnDxdFznZ5WAfDrZUUl2MalxIrPG ekdA== X-Gm-Message-State: AOJu0YwQoYDvL9/Pxd2x2YwYns1L4kZjjSaWKQjjuNJBq0qFtQSG+bXn PwGWpHrGonl0ZLSIN2qkH1kDEx2xNgK3uvavBXDHgGqtxrwrAAv9vyCZDzzwELvP/jscgrNuU5f nEzmCmNnxlwwvKpKVQVymrs7WrSx81Dz7w/xOe66bUn9jgdLej3bAH1kpuFxyTLa15144wq+t4F xHOpfYMQ== X-Received: by 2002:a05:6512:e9a:b0:50b:c8b5:ae96 with SMTP id bi26-20020a0565120e9a00b0050bc8b5ae96mr2050662lfb.57.1701514363786; Sat, 02 Dec 2023 02:52:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IHY+x9g9w9T2hdkbcb/SRmgYSlVx85vf5loYVHkoCq/xxdqk00tWbvJQct6Zch0dgDf2wD5/Q== X-Received: by 2002:a05:6512:e9a:b0:50b:c8b5:ae96 with SMTP id bi26-20020a0565120e9a00b0050bc8b5ae96mr2050651lfb.57.1701514362780; Sat, 02 Dec 2023 02:52:42 -0800 (PST) Received: from localhost (92.40.184.5.threembb.co.uk. [92.40.184.5]) by smtp.gmail.com with ESMTPSA id o18-20020aa7dd52000000b0054b50b024b1sm2503038edw.89.2023.12.02.02.52.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 02:52:42 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Natalia Saiapova , Tankut Baris Aktemur Subject: [PATCHv9 2/5] gdb: fix b/p conditions with infcalls in multi-threaded inferiors Date: Sat, 2 Dec 2023 10:52:31 +0000 Message-Id: <797b97cd8882fae95a52ba3634e39c646171b09f.1701514066.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=-8.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org This commit fixes bug PR 28942, that is, creating a conditional breakpoint in a multi-threaded inferior, where the breakpoint condition includes an inferior function call. Currently, when a user tries to create such a breakpoint, then GDB will fail with: (gdb) break infcall-from-bp-cond-single.c:61 if (return_true ()) Breakpoint 2 at 0x4011fa: file /tmp/build/gdb/testsuite/../../../src/gdb/testsuite/gdb.threads/infcall-from-bp-cond-single.c, line 61. (gdb) continue Continuing. [New Thread 0x7ffff7c5d700 (LWP 2460150)] [New Thread 0x7ffff745c700 (LWP 2460151)] [New Thread 0x7ffff6c5b700 (LWP 2460152)] [New Thread 0x7ffff645a700 (LWP 2460153)] [New Thread 0x7ffff5c59700 (LWP 2460154)] Error in testing breakpoint condition: Couldn't get registers: No such process. An error occurred while in a function called from GDB. Evaluation of the expression containing the function (return_true) will be abandoned. When the function is done executing, GDB will silently stop. Selected thread is running. (gdb) Or, in some cases, like this: (gdb) break infcall-from-bp-cond-simple.c:56 if (is_matching_tid (arg, 1)) Breakpoint 2 at 0x401194: file /tmp/build/gdb/testsuite/../../../src/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.c, line 56. (gdb) continue Continuing. [New Thread 0x7ffff7c5d700 (LWP 2461106)] [New Thread 0x7ffff745c700 (LWP 2461107)] ../../src.release/gdb/nat/x86-linux-dregs.c:146: internal-error: x86_linux_update_debug_registers: Assertion `lwp_is_stopped (lwp)' failed. A problem internal to GDB has been detected, further debugging may prove unreliable. The precise error depends on the exact thread state; so there's race conditions depending on which threads have fully started, and which have not. But the underlying problem is always the same; when GDB tries to execute the inferior function call from within the breakpoint condition, GDB will, incorrectly, try to resume threads that are already running - GDB doesn't realise that some threads might already be running. The solution proposed in this patch requires an additional member variable thread_info::in_cond_eval. This flag is set to true (in breakpoint.c) when GDB is evaluating a breakpoint condition. In user_visible_resume_ptid (infrun.c), when the in_cond_eval flag is true, then GDB will only try to resume the current thread, that is, the thread for which the breakpoint condition is being evaluated. This solves the problem of GDB trying to resume threads that are already running. The next problem is that inferior function calls are assumed to be synchronous, that is, GDB doesn't expect to start an inferior function call in thread #1, then receive a stop from thread #2 for some other, unrelated reason. To prevent GDB responding to an event from another thread, we update fetch_inferior_event and do_target_wait in infrun.c, so that, when an inferior function call (on behalf of a breakpoint condition) is in progress, we only wait for events from the current thread (the one evaluating the condition). In do_target_wait I had to change the inferior_matches lambda function, which is used to select which inferior to wait on. Previously the logic was this: auto inferior_matches = [&wait_ptid] (inferior *inf) { return (inf->process_target () != nullptr && ptid_t (inf->pid).matches (wait_ptid)); }; This compares the pid of the inferior against the complete ptid we want to wait on. Before this commit wait_ptid was only ever minus_one_ptid (which is special, and means any process), and so every inferior would match. After this commit though wait_ptid might represent a specific thread in a specific inferior. If we compare the pid of the inferior to a specific ptid then these will not match. The fix is to compare against the pid extracted from the wait_ptid, not against the complete wait_ptid itself. In fetch_inferior_event, after receiving the event, we only want to stop all the other threads, and call inferior_event_handler with INF_EXEC_COMPLETE, if we are not evaluating a conditional breakpoint. If we are, then all the other threads should be left doing whatever they were before. The inferior_event_handler call will be performed once the breakpoint condition has finished being evaluated, and GDB decides to stop or not. The final problem that needs solving relates to GDB's commit-resume mechanism, which allows GDB to collect resume requests into a single packet in order to reduce traffic to a remote target. The problem is that the commit-resume mechanism will not send any resume requests for an inferior if there are already events pending on the GDB side. Imagine an inferior with two threads. Both threads hit a breakpoint, maybe the same conditional breakpoint. At this point there are two pending events, one for each thread. GDB selects one of the events and spots that this is a conditional breakpoint, GDB evaluates the condition. The condition includes an inferior function call, so GDB sets up for the call and resumes the one thread, the resume request is added to the commit-resume queue. When the commit-resume queue is committed GDB sees that there is a pending event from another thread, and so doesn't send any resume requests to the actual target, GDB is assuming that when we wait we will select the event from the other thread. However, as this is an inferior function call for a condition evaluation, we will not select the event from the other thread, we only care about events from the thread that is evaluating the condition - and the resume for this thread was never sent to the target. And so, GDB hangs, waiting for an event from a thread that was never fully resumed. To fix this issue I have added the concept of "forcing" the commit-resume queue. When enabling commit resume, if the force flag is true, then any resumes will be committed to the target, even if there are other threads with pending events. A note on authorship: this patch was based on some work done by Natalia Saiapova and Tankut Baris Aktemur from Intel[1]. I have made some changes to their work in this version. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=28942 [1] https://sourceware.org/pipermail/gdb-patches/2020-October/172454.html Co-authored-by: Natalia Saiapova Co-authored-by: Tankut Baris Aktemur Reviewed-By: Tankut Baris Aktemur --- gdb/breakpoint.c | 2 + gdb/gdbthread.h | 3 + gdb/infcall.c | 6 + gdb/infrun.c | 64 +++-- gdb/infrun.h | 3 +- .../infcall-from-bp-cond-other-thread-event.c | 135 ++++++++++ ...nfcall-from-bp-cond-other-thread-event.exp | 174 +++++++++++++ .../gdb.threads/infcall-from-bp-cond-simple.c | 89 +++++++ .../infcall-from-bp-cond-simple.exp | 235 ++++++++++++++++++ .../gdb.threads/infcall-from-bp-cond-single.c | 139 +++++++++++ .../infcall-from-bp-cond-single.exp | 117 +++++++++ 11 files changed, 952 insertions(+), 15 deletions(-) create mode 100644 gdb/testsuite/gdb.threads/infcall-from-bp-cond-other-thread-event.c create mode 100644 gdb/testsuite/gdb.threads/infcall-from-bp-cond-other-thread-event.exp create mode 100644 gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.c create mode 100644 gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.exp create mode 100644 gdb/testsuite/gdb.threads/infcall-from-bp-cond-single.c create mode 100644 gdb/testsuite/gdb.threads/infcall-from-bp-cond-single.exp diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 699919e32b3..252bc72e011 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -5675,6 +5675,8 @@ bpstat_check_breakpoint_conditions (bpstat *bs, thread_info *thread) { try { + scoped_restore reset_in_cond_eval + = make_scoped_restore (&thread->control.in_cond_eval, true); condition_result = breakpoint_cond_eval (cond); } catch (const gdb_exception_error &ex) diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h index b2c6baf4432..1bd6a8f508c 100644 --- a/gdb/gdbthread.h +++ b/gdb/gdbthread.h @@ -172,6 +172,9 @@ struct thread_control_state command. This is used to decide whether "set scheduler-locking step" behaves like "on" or "off". */ int stepping_command = 0; + + /* True if the thread is evaluating a BP condition. */ + bool in_cond_eval = false; }; /* Inferior thread specific part of `struct infcall_suspend_state'. */ diff --git a/gdb/infcall.c b/gdb/infcall.c index 8252feea074..935a555e817 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -672,6 +672,12 @@ run_inferior_call (std::unique_ptr sm, proceed (real_pc, GDB_SIGNAL_0); + /* Enable commit resume, but pass true for the force flag. This + ensures any thread we set running in proceed will actually be + committed to the target, even if some other thread in the current + target has a pending event. */ + scoped_enable_commit_resumed enable ("infcall", true); + infrun_debug_show_threads ("non-exited threads after proceed for inferior-call", all_non_exited_threads ()); diff --git a/gdb/infrun.c b/gdb/infrun.c index 8ef22c6453a..ae99fe17b0c 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -2399,6 +2399,14 @@ user_visible_resume_ptid (int step) mode. */ resume_ptid = inferior_ptid; } + else if (inferior_ptid != null_ptid + && inferior_thread ()->control.in_cond_eval) + { + /* The inferior thread is evaluating a BP condition. Other threads + might be stopped or running and we do not want to change their + state, thus, resume only the current thread. */ + resume_ptid = inferior_ptid; + } else if (!sched_multi && target_supports_multi_process ()) { /* Resume all threads of the current process (and none of other @@ -3194,12 +3202,24 @@ schedlock_applies (struct thread_info *tp) execution_direction))); } -/* Set process_stratum_target::COMMIT_RESUMED_STATE in all target - stacks that have threads executing and don't have threads with - pending events. */ +/* When FORCE_P is false, set process_stratum_target::COMMIT_RESUMED_STATE + in all target stacks that have threads executing and don't have threads + with pending events. + + When FORCE_P is true, set process_stratum_target::COMMIT_RESUMED_STATE + in all target stacks that have threads executing regardless of whether + there are pending events or not. + + Passing FORCE_P as false makes sense when GDB is going to wait for + events from all threads and will therefore spot the pending events. + However, if GDB is only going to wait for events from select threads + (i.e. when performing an inferior call) then a pending event on some + other thread will not be spotted, and if we fail to commit the resume + state for the thread performing the inferior call, then the inferior + call will never complete (or even start). */ static void -maybe_set_commit_resumed_all_targets () +maybe_set_commit_resumed_all_targets (bool force_p) { scoped_restore_current_thread restore_thread; @@ -3228,7 +3248,7 @@ maybe_set_commit_resumed_all_targets () status to report, handle it before requiring the target to commit its resumed threads: handling the status might lead to resuming more threads. */ - if (proc_target->has_resumed_with_pending_wait_status ()) + if (!force_p && proc_target->has_resumed_with_pending_wait_status ()) { infrun_debug_printf ("not requesting commit-resumed for target %s, a" " thread has a pending waitstatus", @@ -3238,7 +3258,7 @@ maybe_set_commit_resumed_all_targets () switch_to_inferior_no_thread (inf); - if (target_has_pending_events ()) + if (!force_p && target_has_pending_events ()) { infrun_debug_printf ("not requesting commit-resumed for target %s, " "target has pending events", @@ -3331,7 +3351,7 @@ scoped_disable_commit_resumed::reset () { /* This is the outermost instance, re-enable COMMIT_RESUMED_STATE on the targets where it's possible. */ - maybe_set_commit_resumed_all_targets (); + maybe_set_commit_resumed_all_targets (false); } else { @@ -3364,7 +3384,7 @@ scoped_disable_commit_resumed::reset_and_commit () /* See infrun.h. */ scoped_enable_commit_resumed::scoped_enable_commit_resumed - (const char *reason) + (const char *reason, bool force_p) : m_reason (reason), m_prev_enable_commit_resumed (enable_commit_resumed) { @@ -3376,7 +3396,7 @@ scoped_enable_commit_resumed::scoped_enable_commit_resumed /* Re-enable COMMIT_RESUMED_STATE on the targets where it's possible. */ - maybe_set_commit_resumed_all_targets (); + maybe_set_commit_resumed_all_targets (force_p); maybe_call_commit_resumed_all_targets (); } @@ -4129,10 +4149,11 @@ do_target_wait (ptid_t wait_ptid, execution_control_state *ecs, polling the rest of the inferior list starting from that one in a circular fashion until the whole list is polled once. */ - auto inferior_matches = [&wait_ptid] (inferior *inf) + ptid_t wait_ptid_pid {wait_ptid.pid ()}; + auto inferior_matches = [&wait_ptid_pid] (inferior *inf) { return (inf->process_target () != nullptr - && ptid_t (inf->pid).matches (wait_ptid)); + && ptid_t (inf->pid).matches (wait_ptid_pid)); }; /* First see how many matching inferiors we have. */ @@ -4621,7 +4642,17 @@ fetch_inferior_event () the event. */ scoped_disable_commit_resumed disable_commit_resumed ("handling event"); - if (!do_target_wait (minus_one_ptid, &ecs, TARGET_WNOHANG)) + /* Is the current thread performing an inferior function call as part + of a breakpoint condition evaluation? */ + bool in_cond_eval = (inferior_ptid != null_ptid + && inferior_thread ()->control.in_cond_eval); + + /* If the thread is in the middle of the condition evaluation, wait for + an event from the current thread. Otherwise, wait for an event from + any thread. */ + ptid_t waiton_ptid = in_cond_eval ? inferior_ptid : minus_one_ptid; + + if (!do_target_wait (waiton_ptid, &ecs, TARGET_WNOHANG)) { infrun_debug_printf ("do_target_wait returned no event"); disable_commit_resumed.reset_and_commit (); @@ -4679,7 +4710,12 @@ fetch_inferior_event () bool should_notify_stop = true; bool proceeded = false; - stop_all_threads_if_all_stop_mode (); + /* If the thread that stopped just completed an inferior + function call as part of a condition evaluation, then we + don't want to stop all the other threads. */ + if (ecs.event_thread == nullptr + || !ecs.event_thread->control.in_cond_eval) + stop_all_threads_if_all_stop_mode (); clean_up_just_stopped_threads_fsms (&ecs); @@ -4706,7 +4742,7 @@ fetch_inferior_event () proceeded = normal_stop (); } - if (!proceeded) + if (!proceeded && !in_cond_eval) { inferior_event_handler (INF_EXEC_COMPLETE); cmd_done = 1; diff --git a/gdb/infrun.h b/gdb/infrun.h index a343d27f72d..d8db153ecc6 100644 --- a/gdb/infrun.h +++ b/gdb/infrun.h @@ -406,7 +406,8 @@ extern void maybe_call_commit_resumed_all_targets (); struct scoped_enable_commit_resumed { - explicit scoped_enable_commit_resumed (const char *reason); + explicit scoped_enable_commit_resumed (const char *reason, + bool force_p = false); ~scoped_enable_commit_resumed (); DISABLE_COPY_AND_ASSIGN (scoped_enable_commit_resumed); diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-other-thread-event.c b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-other-thread-event.c new file mode 100644 index 00000000000..e2a8ccb4ebe --- /dev/null +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-other-thread-event.c @@ -0,0 +1,135 @@ +/* Copyright 2022-2023 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include +#include +#include + +#define NUM_THREADS 2 + +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +/* Some global variables to poke, just for something to do. */ +volatile int global_var_0 = 0; +volatile int global_var_1 = 0; + +/* This flag is updated from GDB. */ +volatile int raise_signal = 0; + +/* Implement the breakpoint condition function. Release the other thread + and try to give the other thread a chance to run. Then return ANSWER. */ +int +condition_core_func (int answer) +{ + /* This unlock should release the other thread. */ + if (pthread_mutex_unlock (&mutex) != 0) + abort (); + + /* And this yield and sleep should (hopefully) give the other thread a + chance to run. This isn't guaranteed of course, but once the other + thread does run it should hit a breakpoint, which GDB should + (temporarily) ignore, so there's no easy way for us to know the other + thread has done what it needs to, thus, yielding and sleeping is the + best we can do. */ + sched_yield (); + sleep (2); + + return answer; +} + +void +stop_marker () +{ + int a = 100; /* Final breakpoint here. */ +} + +/* A breakpoint condition function that always returns true. */ +int +condition_true_func () +{ + return condition_core_func (1); +} + +/* A breakpoint condition function that always returns false. */ +int +condition_false_func () +{ + return condition_core_func (0); +} + +void * +worker_func (void *arg) +{ + volatile int *ptr = 0; + int tid = *((int *) arg); + + switch (tid) + { + case 0: + global_var_0 = 11; /* First thread breakpoint. */ + break; + + case 1: + if (pthread_mutex_lock (&mutex) != 0) + abort (); + if (raise_signal) + global_var_1 = *ptr; /* Signal here. */ + else + global_var_1 = 99; /* Other thread breakpoint. */ + break; + + default: + abort (); + } + + return NULL; +} + +int +main () +{ + pthread_t threads[NUM_THREADS]; + int args[NUM_THREADS]; + + /* Set an alarm, just in case the test deadlocks. */ + alarm (300); + + /* We want the mutex to start locked. */ + if (pthread_mutex_lock (&mutex) != 0) + abort (); + + for (int i = 0; i < NUM_THREADS; i++) + { + args[i] = i; + pthread_create (&threads[i], NULL, worker_func, &args[i]); + } + + for (int i = 0; i < NUM_THREADS; i++) + { + void *retval; + pthread_join (threads[i], &retval); + } + + /* Unlock once we're done, just for cleanliness. */ + if (pthread_mutex_unlock (&mutex) != 0) + abort (); + + stop_marker (); + + return 0; +} diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-other-thread-event.exp b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-other-thread-event.exp new file mode 100644 index 00000000000..1453575e4ad --- /dev/null +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-other-thread-event.exp @@ -0,0 +1,174 @@ +# Copyright 2022-2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test for conditional breakpoints where the breakpoint condition includes +# an inferior function call. +# +# The tests in this script are testing what happens when an event arrives in +# another thread while GDB is waiting for the inferior function call (in the +# breakpoint condition) to finish. +# +# The expectation is that GDB will queue events for other threads and wait +# for the inferior function call to complete, if the condition is true, then +# the conditional breakpoint should be reported first. The other thread +# event should of course, not get lost, and should be reported as soon as +# the user tries to continue the inferior. +# +# If the conditional breakpoint ends up not being taken (the condition is +# false), then the other thread event should be reported immediately. +# +# This script tests what happens when the other thread event is (a) the +# other thread hitting a breakpoint, and (b) the other thread taking a +# signal (SIGSEGV in this case). + +standard_testfile + +if { [build_executable "failed to prepare" ${binfile} "${srcfile}" \ + {debug pthreads}] == -1 } { + return +} + +set cond_bp_line [gdb_get_line_number "First thread breakpoint"] +set other_bp_line [gdb_get_line_number "Other thread breakpoint"] +set final_bp_line [gdb_get_line_number "Final breakpoint here"] +set signal_line [gdb_get_line_number "Signal here"] + +# Start GDB based on TARGET_ASYNC and TARGET_NON_STOP, and then runto main. +proc start_gdb_and_runto_main { target_async target_non_stop } { + save_vars { ::GDBFLAGS } { + append ::GDBFLAGS \ + " -ex \"maint set target-non-stop $target_non_stop\"" + append ::GDBFLAGS \ + " -ex \"maintenance set target-async ${target_async}\"" + + clean_restart ${::binfile} + } + + if { ![runto_main] } { + return -1 + } + + return 0 +} + +# Run a test of GDB's conditional breakpoints, where the conditions include +# inferior function calls. While the inferior function call is executing +# another thread will hit a breakpoint (when OTHER_THREAD_SIGNAL is false), +# or receive a signal (when OTHER_THREAD_SIGNAL is true). GDB should report +# the conditional breakpoint first (if the condition is true), and then +# report the second thread event once the inferior is continued again. +# +# When STOP_AT_COND is true then the conditional breakpoint will have a +# condition that evaluates to true (and GDB will stop at the breakpoint), +# otherwise, the condition will evaluate to false (and GDB will not stop at +# the breakpoint). +proc run_condition_test { stop_at_cond other_thread_signal \ + target_async target_non_stop } { + if { [start_gdb_and_runto_main $target_async \ + $target_non_stop] == -1 } { + return + } + + # Setup the conditional breakpoint. + if { $stop_at_cond } { + set cond_func "condition_true_func" + } else { + set cond_func "condition_false_func" + } + gdb_breakpoint \ + "${::srcfile}:${::cond_bp_line} if (${cond_func} ())" + set cond_bp_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number for conditional breakpoint"] + + if { $other_thread_signal } { + # Arrange for the other thread to raise a signal while GDB is + # evaluating the breakpoint condition. + gdb_test_no_output "set raise_signal = 1" + } else { + # And a breakpoint that will be hit by another thread only once the + # breakpoint condition starts to be evaluated. + gdb_breakpoint "${::srcfile}:${::other_bp_line}" + set other_bp_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number for other breakpoint"] + } + + # A final breakpoint once the test has completed. + gdb_breakpoint "${::srcfile}:${::final_bp_line}" + set final_bp_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number for final breakpoint"] + + if { $stop_at_cond } { + # Continue. The first breakpoint we hit should be the conditional + # breakpoint. The other thread will have hit its breakpoint, but + # that will have been deferred until the conditional breakpoint is + # reported. + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "" \ + "Thread ${::decimal} \"\[^\"\r\n\]+\" hit Breakpoint ${cond_bp_num}, worker_func \[^\r\n\]+:${::cond_bp_line}" \ + "${::decimal}\\s+\[^\r\n\]+First thread breakpoint\[^\r\n\]+"] \ + "hit the conditional breakpoint" + } + + if { $other_thread_signal } { + # Now continue again, the other thread will now report that it + # received a signal. + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "Thread ${::decimal} \"\[^\"\r\n\]+\" received signal SIGSEGV, Segmentation fault\\." \ + "\\\[Switching to Thread \[^\r\n\]+\\\]" \ + "${::hex} in worker_func \[^\r\n\]+:${::signal_line}" \ + "${::decimal}\\s+\[^\r\n\]+Signal here\[^\r\n\]+"] \ + "received signal in other thread" + } else { + # Now continue again, the other thread will now report its + # breakpoint. + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "" \ + "Thread ${::decimal} \"\[^\"\r\n\]+\" hit Breakpoint ${other_bp_num}, worker_func \[^\r\n\]+:${::other_bp_line}" \ + "${::decimal}\\s+\[^\r\n\]+Other thread breakpoint\[^\r\n\]+"] \ + "hit the breakpoint in other thread" + + # Run to the stop marker. + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "" \ + "Thread ${::decimal} \"\[^\"\r\n\]+\" hit Breakpoint ${final_bp_num}, stop_marker \[^\r\n\]+:${::final_bp_line}" \ + "${::decimal}\\s+\[^\r\n\]+Final breakpoint here\[^\r\n\]+"] \ + "hit the final breakpoint" + } + + gdb_exit +} + +foreach_with_prefix target_async { "on" "off" } { + foreach_with_prefix target_non_stop { "on" "off" } { + foreach_with_prefix other_thread_signal { true false } { + foreach_with_prefix stop_at_cond { true false } { + run_condition_test $stop_at_cond $other_thread_signal \ + $target_async $target_non_stop + } + } + } +} diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.c b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.c new file mode 100644 index 00000000000..9d746d8be49 --- /dev/null +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.c @@ -0,0 +1,89 @@ +/* Copyright 2022-2023 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include + +#define NUM_THREADS 3 + +int +is_matching_tid (int *tid_ptr, int tid_value) +{ + return *tid_ptr == tid_value; +} + +int +return_true () +{ + return 1; +} + +int +return_false () +{ + return 0; +} + +int +function_that_segfaults () +{ + int *p = 0; + *p = 1; /* Segfault happens here. */ +} + +int +function_with_breakpoint () +{ + return 1; /* Nested breakpoint. */ +} + +void * +worker_func (void *arg) +{ + int a = 42; /* Breakpoint here. */ +} + +void +stop_marker () +{ + int b = 99; /* Stop marker. */ +} + +int +main () +{ + pthread_t threads[NUM_THREADS]; + int args[NUM_THREADS]; + + alarm (300); + + for (int i = 0; i < NUM_THREADS; i++) + { + args[i] = i; + pthread_create (&threads[i], NULL, worker_func, &args[i]); + } + + for (int i = 0; i < NUM_THREADS; i++) + { + void *retval; + pthread_join (threads[i], &retval); + } + + stop_marker (); + + return 0; +} diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.exp b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.exp new file mode 100644 index 00000000000..c46f8cdb981 --- /dev/null +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.exp @@ -0,0 +1,235 @@ +# Copyright 2022-2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Some simple tests of inferior function calls from breakpoint +# conditions, in multi-threaded inferiors. +# +# This test sets up a multi-threaded inferior, and places a breakpoint +# at a location that many of the threads will reach. We repeat the +# test with different conditions, sometimes a single thread should +# stop at the breakpoint, sometimes multiple threads should stop, and +# sometimes no threads should stop. + +standard_testfile + +if { [build_executable "failed to prepare" ${binfile} "${srcfile}" \ + {debug pthreads}] == -1 } { + return +} + +set cond_bp_line [gdb_get_line_number "Breakpoint here"] +set stop_bp_line [gdb_get_line_number "Stop marker"] +set nested_bp_line [gdb_get_line_number "Nested breakpoint"] +set segv_line [gdb_get_line_number "Segfault happens here"] + +# Start GDB based on TARGET_ASYNC and TARGET_NON_STOP, and then runto main. +proc start_gdb_and_runto_main { target_async target_non_stop } { + save_vars { ::GDBFLAGS } { + append ::GDBFLAGS \ + " -ex \"maint set target-non-stop $target_non_stop\"" + append ::GDBFLAGS \ + " -ex \"maintenance set target-async ${target_async}\"" + + clean_restart ${::binfile} + } + + if { ![runto_main] } { + return -1 + } + + return 0 +} + +# Run a test of GDB's conditional breakpoints, where the conditions include +# inferior function calls. +# +# CONDITION is the expression to be used as the breakpoint condition. +# +# N_EXPECTED_HITS is the number of threads that we expect to stop due to +# CONDITON. +# +# MESSAGE is used as a test name prefix. +proc run_condition_test { message n_expected_hits condition \ + target_async target_non_stop } { + with_test_prefix $message { + + if { [start_gdb_and_runto_main $target_async \ + $target_non_stop] == -1 } { + return + } + + # Use this convenience variable to track how often the + # breakpoint condition has been evaluated. This should be + # once per thread. + gdb_test "set \$n_cond_eval = 0" + + # Setup the conditional breakpoint. + gdb_breakpoint \ + "${::srcfile}:${::cond_bp_line} if ((++\$n_cond_eval) && (${condition}))" + + # And a breakpoint that we hit when the test is over, this one is + # not conditional. Only the main thread gets here once all the + # other threads have finished. + gdb_breakpoint "${::srcfile}:${::stop_bp_line}" + + # The number of times we stop at the conditional breakpoint. + set n_hit_condition 0 + + # Now keep 'continue'-ing GDB until all the threads have finished + # and we reach the stop_marker breakpoint. + gdb_test_multiple "continue" "spot all breakpoint hits" { + -re " worker_func \[^\r\n\]+${::srcfile}:${::cond_bp_line}\r\n${::decimal}\\s+\[^\r\n\]+Breakpoint here\[^\r\n\]+\r\n${::gdb_prompt} $" { + incr n_hit_condition + send_gdb "continue\n" + exp_continue + } + + -re " stop_marker \[^\r\n\]+${::srcfile}:${::stop_bp_line}\r\n${::decimal}\\s+\[^\r\n\]+Stop marker\[^\r\n\]+\r\n${::gdb_prompt} $" { + pass $gdb_test_name + } + } + + gdb_assert { $n_hit_condition == $n_expected_hits } \ + "stopped at breakpoint the expected number of times" + + # Ensure the breakpoint condition was evaluated once per thread. + gdb_test "print \$n_cond_eval" "= 3" \ + "condition was evaluated in each thread" + } +} + +# Check that after handling a conditional breakpoint (where the condition +# includes an inferior call), it is still possible to kill the running +# inferior, and then restart the inferior. +# +# At once point doing this would result in GDB giving an assertion error. +proc_with_prefix run_kill_and_restart_test { target_async target_non_stop } { + # This test relies on the 'start' command, which is not possible with + # the plain 'remote' target. + if { [target_info gdb_protocol] == "remote" } { + return + } + + if { [start_gdb_and_runto_main $target_async \ + $target_non_stop] == -1 } { + return + } + + # Setup the conditional breakpoint. + gdb_breakpoint \ + "${::srcfile}:${::cond_bp_line} if (is_matching_tid (arg, 1))" + gdb_continue_to_breakpoint "worker_func" + + # Now kill the program being debugged. + gdb_test "kill" "" "kill process" \ + "Kill the program being debugged.*y or n. $" "y" + + # Check we can restart the inferior. At one point this would trigger an + # assertion. + gdb_start_cmd +} + +# Create a conditional breakpoint which includes a call to a function that +# segfaults. Run GDB and check what happens when the inferior segfaults +# during the inferior call. +proc_with_prefix run_bp_cond_segfaults { target_async target_non_stop } { + if { [start_gdb_and_runto_main $target_async \ + $target_non_stop] == -1 } { + return + } + + # This test relies on the inferior segfaulting when trying to + # access address zero. + if { [is_address_zero_readable] } { + return + } + + # Setup the conditional breakpoint, include a call to + # 'function_that_segfaults', which triggers the segfault. + gdb_breakpoint \ + "${::srcfile}:${::cond_bp_line} if (is_matching_tid (arg, 0) && function_that_segfaults ())" + set bp_1_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of conditional breakpoint"] + + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "Thread ${::decimal} \"infcall-from-bp\" received signal SIGSEGV, Segmentation fault\\." \ + "${::hex} in function_that_segfaults \\(\\) at \[^\r\n\]+:${::segv_line}" \ + "${::decimal}\\s+\[^\r\n\]+Segfault happens here\[^\r\n\]+" \ + "Error in testing condition for breakpoint ${bp_1_num}:" \ + "The program being debugged was signaled while in a function called from GDB\\." \ + "GDB remains in the frame where the signal was received\\." \ + "To change this behavior use \"set unwindonsignal on\"\\." \ + "Evaluation of the expression containing the function" \ + "\\(function_that_segfaults\\) will be abandoned\\." \ + "When the function is done executing, GDB will silently stop\\."] +} + +# Create a conditional breakpoint which includes a call to a function that +# itself has a breakpoint set within it. Run GDB and check what happens +# when GDB hits the nested breakpoint. +proc_with_prefix run_bp_cond_hits_breakpoint { target_async target_non_stop } { + if { [start_gdb_and_runto_main $target_async \ + $target_non_stop] == -1 } { + return + } + + # Setup the conditional breakpoint, include a call to + # 'function_with_breakpoint' in which we will shortly place a + # breakpoint. + gdb_breakpoint \ + "${::srcfile}:${::cond_bp_line} if (is_matching_tid (arg, 0) && function_with_breakpoint ())" + set bp_1_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of conditional breakpoint"] + + gdb_breakpoint "${::srcfile}:${::nested_bp_line}" + set bp_2_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of nested breakpoint"] + + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "Thread ${::decimal} \"infcall-from-bp\" hit Breakpoint ${bp_2_num}, function_with_breakpoint \\(\\) at \[^\r\n\]+:${::nested_bp_line}" \ + "${::decimal}\\s+\[^\r\n\]+Nested breakpoint\[^\r\n\]+" \ + "Error in testing condition for breakpoint ${bp_1_num}:" \ + "The program being debugged stopped while in a function called from GDB\\." \ + "Evaluation of the expression containing the function" \ + "\\(function_with_breakpoint\\) will be abandoned\\." \ + "When the function is done executing, GDB will silently stop\\."] +} + +foreach_with_prefix target_async { "on" "off" } { + foreach_with_prefix target_non_stop { "on" "off" } { + run_condition_test "exactly one thread is hit" \ + 1 "is_matching_tid (arg, 1)" \ + $target_async $target_non_stop + run_condition_test "exactly two threads are hit" \ + 2 "(is_matching_tid (arg, 0) || is_matching_tid (arg, 2))" \ + $target_async $target_non_stop + run_condition_test "all three threads are hit" \ + 3 "return_true ()" \ + $target_async $target_non_stop + run_condition_test "no thread is hit" \ + 0 "return_false ()" \ + $target_async $target_non_stop + + run_kill_and_restart_test $target_async $target_non_stop + run_bp_cond_segfaults $target_async $target_non_stop + run_bp_cond_hits_breakpoint $target_async $target_non_stop + } +} diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-single.c b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-single.c new file mode 100644 index 00000000000..835c72f03cf --- /dev/null +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-single.c @@ -0,0 +1,139 @@ +/* Copyright 2022-2023 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include +#include +#include + +#define NUM_THREADS 5 + +/* Semaphores, used to track when threads have started, and to control + when the threads finish. */ +sem_t startup_semaphore; +sem_t finish_semaphore; + +/* Mutex to control when the first worker thread hit a breakpoint + location. */ +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +/* Global variable to poke, just so threads have something to do. */ +volatile int global_var = 0; + +int +return_true () +{ + return 1; +} + +int +return_false () +{ + return 0; +} + +void * +worker_func (void *arg) +{ + int tid = *((int *) arg); + + switch (tid) + { + case 0: + /* Wait for MUTEX to become available, then pass through the + conditional breakpoint location. */ + if (pthread_mutex_lock (&mutex) != 0) + abort (); + global_var = 99; /* Conditional breakpoint here. */ + if (pthread_mutex_unlock (&mutex) != 0) + abort (); + break; + + default: + /* Notify the main thread that the thread has started, then wait for + the main thread to tell us to finish. */ + sem_post (&startup_semaphore); + if (sem_wait (&finish_semaphore) != 0) + abort (); + break; + } +} + +void +stop_marker () +{ + global_var = 99; /* Stop marker. */ +} + +int +main () +{ + pthread_t threads[NUM_THREADS]; + int args[NUM_THREADS]; + void *retval; + + /* An alarm, just in case the thread deadlocks. */ + alarm (300); + + /* Semaphore initialization. */ + if (sem_init (&startup_semaphore, 0, 0) != 0) + abort (); + if (sem_init (&finish_semaphore, 0, 0) != 0) + abort (); + + /* Lock MUTEX, this prevents the first worker thread from rushing ahead. */ + if (pthread_mutex_lock (&mutex) != 0) + abort (); + + /* Worker thread creation. */ + for (int i = 0; i < NUM_THREADS; i++) + { + args[i] = i; + pthread_create (&threads[i], NULL, worker_func, &args[i]); + } + + /* Wait for every thread (other than the first) to tell us it has started + up. */ + for (int i = 1; i < NUM_THREADS; i++) + { + if (sem_wait (&startup_semaphore) != 0) + abort (); + } + + /* Unlock the first thread so it can proceed. */ + if (pthread_mutex_unlock (&mutex) != 0) + abort (); + + /* Wait for the first thread only. */ + pthread_join (threads[0], &retval); + + /* Now post FINISH_SEMAPHORE to allow all the other threads to finish. */ + for (int i = 1; i < NUM_THREADS; i++) + sem_post (&finish_semaphore); + + /* Now wait for the remaining threads to complete. */ + for (int i = 1; i < NUM_THREADS; i++) + pthread_join (threads[i], &retval); + + /* Semaphore cleanup. */ + sem_destroy (&finish_semaphore); + sem_destroy (&startup_semaphore); + + stop_marker (); + + return 0; +} diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-single.exp b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-single.exp new file mode 100644 index 00000000000..787dee3aa8e --- /dev/null +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-single.exp @@ -0,0 +1,117 @@ +# Copyright 2022-2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This test reprocuces bug gdb/28942, performing an inferior function +# call from a breakpoint condition in a multi-threaded inferior. +# +# The important part of this test is that, when the conditional +# breakpoint is hit, and the condition (which includes an inferior +# function call) is evaluated, the other threads are running. + +standard_testfile + +if { [build_executable "failed to prepare" ${binfile} "${srcfile}" \ + {debug pthreads}] == -1 } { + return +} + +set cond_bp_line [gdb_get_line_number "Conditional breakpoint here"] +set final_bp_line [gdb_get_line_number "Stop marker"] + +# Start GDB based on TARGET_ASYNC and TARGET_NON_STOP, and then runto main. +proc start_gdb_and_runto_main { target_async target_non_stop } { + save_vars { ::GDBFLAGS } { + append ::GDBFLAGS \ + " -ex \"maint set target-non-stop $target_non_stop\"" + append ::GDBFLAGS \ + " -ex \"maintenance set target-async ${target_async}\"" + + clean_restart ${::binfile} + } + + if { ![runto_main] } { + return -1 + } + + return 0 +} + +# Run a test of GDB's conditional breakpoints, where the conditions include +# inferior function calls. +# +# TARGET_ASYNC and TARGET_NON_STOP are used when starting up GDB. +# +# When STOP_AT_COND is true the breakpoint condtion will evaluate to +# true, and GDB will stop at the breakpoint. Otherwise, the +# breakpoint condition will evaluate to false and GDB will not stop at +# the breakpoint. +proc run_condition_test { stop_at_cond \ + target_async target_non_stop } { + if { [start_gdb_and_runto_main $target_async \ + $target_non_stop] == -1 } { + return + } + + # Setup the conditional breakpoint. + if { $stop_at_cond } { + set cond_func "return_true" + } else { + set cond_func "return_false" + } + gdb_breakpoint \ + "${::srcfile}:${::cond_bp_line} if (${cond_func} ())" + set cond_bp_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number for conditional breakpoint"] + + # And a breakpoint that we hit when the test is over, this one is + # not conditional. + gdb_breakpoint "${::srcfile}:${::final_bp_line}" + set final_bp_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number for final breakpoint"] + + if { $stop_at_cond } { + # Continue. The first breakpoint we hit should be the conditional + # breakpoint. The other thread will have hit its breakpoint, but + # that will have been deferred until the conditional breakpoint is + # reported. + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "" \ + "Thread ${::decimal} \"\[^\"\r\n\]+\" hit Breakpoint ${cond_bp_num}, worker_func \[^\r\n\]+:${::cond_bp_line}" \ + "${::decimal}\\s+\[^\r\n\]+Conditional breakpoint here\[^\r\n\]+"] \ + "hit the conditional breakpoint" + } + + # Run to the stop marker. + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "" \ + "Thread ${::decimal} \"\[^\"\r\n\]+\" hit Breakpoint ${final_bp_num}, stop_marker \[^\r\n\]+:${::final_bp_line}" \ + "${::decimal}\\s+\[^\r\n\]+Stop marker\[^\r\n\]+"] \ + "hit the final breakpoint" +} + +foreach_with_prefix target_async { "on" "off" } { + foreach_with_prefix target_non_stop { "on" "off" } { + foreach_with_prefix stop_at_cond { true false } { + run_condition_test $stop_at_cond \ + $target_async $target_non_stop + } + } +} From patchwork Sat Dec 2 10:52:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 81198 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 D0658385DC3A for ; Sat, 2 Dec 2023 10:53:48 +0000 (GMT) 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 A7F363861877 for ; Sat, 2 Dec 2023 10:52:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A7F363861877 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A7F363861877 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514374; cv=none; b=qV9ogFo+gs8pZ2n2TpNM1fW8f95A8Fnqd+1Yx/TfEgK66wSA+O7JbjTq7a92znwTZJTw+ZyCM066CfWFUmCNoKlIVdcI7qgoxbCfGk+mrs5aoDsIW1WinY1qU7q/dzohyutMWU9+pSRSxLTxu5aCgr57TPd8ZbUXOnDb7fDYTBc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514374; c=relaxed/simple; bh=o/ydWskbDYLMr0ECiwgR+NsW9QXAezeLYdbjLUmeXeI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=eK6jjobGgXUMCcZLs1rkR0pczOeJi2WWTmWimf2PW6JTy1h5y73Fz7DOIG4FSDOUjj0wFajUy2JMQTjsp1eQ0EjYoPkRNgVVX2GXxiC+Peg2iHGD++YTdvCysUfwYOpbqiUUar7fJj7OGhnuZNIeSDkjovl58ayQx+xy/GjtttE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701514369; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=j766avArwvJSBmEMHVUV/rdS++oB/8Em66au1vOxvzo=; b=N+ssJgJrfs5iE4litKzKJtH+a6odPV9pb9Q+dS7nwnw+3jmQr0ceZcDOJO4t/9E3kGRLTR a9CcJ82ivvFvbTHc/egoEwZeOJFvu7lPNH4WdufrALWWTeZOM8rKwSSJ6XSAlWdu+IpTZJ WFySSmJVOW6BQz2JvY3ucSyX+VvCyYA= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-358-o5V5TOVcMj23WgB1Rl03eg-1; Sat, 02 Dec 2023 05:52:48 -0500 X-MC-Unique: o5V5TOVcMj23WgB1Rl03eg-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-4083717431eso20999125e9.1 for ; Sat, 02 Dec 2023 02:52:47 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701514366; x=1702119166; 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=j766avArwvJSBmEMHVUV/rdS++oB/8Em66au1vOxvzo=; b=kBwtr3nHv5XjzWsFvESIBqt7gJq4xR2dh0KZkYZxjWiN09UOoH/bVO42qkgWvdEvvC XhVWOPUR3U3yXBfjAh5SOwttOUozqb8qhDvueeETE9KHo/IyVNonZUKfJtf+79trIi3N g+Dd8jO2PN8iDi7ggsazIPK5tysgueK15mEwWolIP7evWIspc6dWlpFx8tRHmkSWe9Ii t+VVdf/RlueiLHFh3r6l2w5nYyi4IocnN08vhL9pE32VDdRC3AZ6G3+r5tvN3jjy2XYq W7cJaqvk6m5t7UO3Qp1+Y0wN30rPSn1Jzapp2ALkAxkQCf+UFizUK5D6zzc6BKcy+jG1 F75g== X-Gm-Message-State: AOJu0Yy+bQDa72FS6VEkU/fvevsSSBsTXhNU3yH2hAoyriQQYHxDH7UA pBbgjmWZXrTEsp+r9dAsZRSDEnbqGyL8xQt9AXRSDUZvLUCmYWoxI4ebAOm438xuPH1kfXGFM4P lpLRwuKRwK0vnOX8uBsLtqYTOEHwh2Ysmb+xrPbx2eolcgp4/5S5Q2f1R0bc7j+nsackOb8ECZh dKTM69lQ== X-Received: by 2002:a05:600c:2117:b0:40b:5641:4377 with SMTP id u23-20020a05600c211700b0040b56414377mr1399509wml.5.1701514366070; Sat, 02 Dec 2023 02:52:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IFuEZkNftf7ZzDMscvJkdXmAb37xkNWgm3W0oOr1HdT+06mY59Kdw+Nx/a0+3FYF9RWE4FJGw== X-Received: by 2002:a05:600c:2117:b0:40b:5641:4377 with SMTP id u23-20020a05600c211700b0040b56414377mr1399494wml.5.1701514365146; Sat, 02 Dec 2023 02:52:45 -0800 (PST) Received: from localhost (92.40.184.5.threembb.co.uk. [92.40.184.5]) by smtp.gmail.com with ESMTPSA id w24-20020a17090633d800b009f2b7282387sm2895132eja.46.2023.12.02.02.52.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 02:52:44 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Tankut Baris Aktemur , Eli Zaretskii Subject: [PATCHv9 3/5] gdb: add timeouts for inferior function calls Date: Sat, 2 Dec 2023 10:52:32 +0000 Message-Id: <98fbe6b2997bebf8c1956525cbab65675e738511.1701514067.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=-8.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Eli has already approved the NEWS/doc changes in this file. --- In the previous commits I have been working on improving inferior function call support. One thing that worries me about using inferior function calls from a conditional breakpoint is: what happens if the inferior function call fails? If the failure is obvious, e.g. the thread performing the call crashes, or hits a breakpoint, then this case is already well handled, and the error is reported to the user. But what if the thread performing the inferior call just deadlocks? If the user made the call from a 'print' or 'call' command, then the user might have some expectation of when the function call should complete, and, when this time limit is exceeded, the user will (hopefully) interrupt GDB and regain control of the debug session. But, when the inferior function call is from a breakpoint condition it is much harder to understand that GDB is deadlocked within an inferior call. Maybe the breakpoint hasn't been hit yet? Or maybe the condition was always false? Or maybe GDB is deadlocked in an inferior call? The only way to know for sure is for the user to periodically interrupt the inferior, check on the state of all the threads, and then continue. Additionally, the focus of the previous commit was inferior function calls, from a conditional breakpoint, in a multi-threaded inferior. This opens up a whole new set of potential failure conditions. For example, what if the function called relies on interaction with some other thread, and the other thread crashes? Or hits a breakpoint? Given how inferior function calls work (in a synchronous manner), a stop event in some other thread is going to be ignored while the inferior function call is being executed as part of a breakpoint condition, and this means that GDB could get stuck waiting for the original condition thread, which will now never complete. In this commit I propose a solution to this problem. A timeout. For targets that support async-mode we can install an event-loop timer before starting the inferior function call. When the timer expires we will stop the thread performing the inferior function call. With this mechanism in place a user can be sure that any inferior call they make will either complete, or timeout eventually. Adding a timer like this is obviously a change in behaviour for the more common 'call' and 'print' uses of inferior function calls, so, in this patch, I propose having two different timers. One I call the 'direct-call-timeout', which is used for 'call' and 'print' commands. This timeout is by default set to unlimited, which, not surprisingly, means there is no timeout in place. A second timer, which I've called 'indirect-call-timeout', is used for inferior function calls from breakpoint conditions. This timeout has a default value of 30 seconds. This is a reasonably long time to wait, and hopefully should be enough in most cases to allow the inferior call to complete. An inferior call that takes more than 30 seconds, which is installed on a breakpoint condition is really going to slow down the debug session, so hopefully this is not a common use case. The user is, of course, free to reduce, or increase the timeout value, and can always use Ctrl-c to interrupt an inferior function call, but this timeout will ensure that GDB will stop at some point. The new commands added by this commit are: set direct-call-timeout SECONDS show direct-call-timeout set indirect-call-timeout SECONDS show indirect-call-timeout These new timeouts do depend on async-mode, so, if async-mode is disabled (maint set target-async off), or not supported (e.g. target sim), then the timeout is treated as unlimited (that is, no timeout is set). For targets that "fake" non-async mode, e.g. Linux native, where non-async mode is really just async mode, but then we park the target in a sissuspend, we could easily fix things so that the timeouts still work, however, for targets that really are not async aware, like the simulator, fixing things so that timeouts work correctly would be a much bigger task - that effort would be better spent just making the target async-aware. And so, I'm happy for now that this feature will only work on async targets. The two new show commands will display slightly different text if the current target is a non-async target, which should allow users to understand what's going on. There's a somewhat random test adjustment needed in gdb.base/help.exp, the test uses a regexp with the apropos command, and expects to find a single result. Turns out the new settings I added also matched the regexp, which broke the test. I've updated the regexp a little to exclude my new settings. Reviewed-By: Tankut Baris Aktemur Reviewed-By: Eli Zaretskii --- gdb/NEWS | 18 ++ gdb/doc/gdb.texinfo | 66 ++++++ gdb/infcall.c | 210 +++++++++++++++++- gdb/testsuite/gdb.base/help.exp | 2 +- gdb/testsuite/gdb.base/infcall-timeout.c | 36 +++ gdb/testsuite/gdb.base/infcall-timeout.exp | 94 ++++++++ .../infcall-from-bp-cond-timeout.c | 169 ++++++++++++++ .../infcall-from-bp-cond-timeout.exp | 166 ++++++++++++++ 8 files changed, 756 insertions(+), 5 deletions(-) create mode 100644 gdb/testsuite/gdb.base/infcall-timeout.c create mode 100644 gdb/testsuite/gdb.base/infcall-timeout.exp create mode 100644 gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.c create mode 100644 gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.exp diff --git a/gdb/NEWS b/gdb/NEWS index 1073e38dfc6..3500acd42b6 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -38,6 +38,24 @@ set remote thread-options-packet show remote thread-options-packet Set/show the use of the thread options packet. +set direct-call-timeout SECONDS +show direct-call-timeout +set indirect-call-timeout SECONDS +show indirect-call-timeout + These new settings can be used to limit how long GDB will wait for + an inferior function call to complete. The direct timeout is used + for inferior function calls from e.g. 'call' and 'print' commands, + while the indirect timeout is used for inferior function calls from + within a conditional breakpoint expression. + + The default for the direct timeout is unlimited, while the default + for the indirect timeout is 30 seconds. + + These timeouts will only have an effect for targets that are + operating in async mode. For non-async targets the timeouts are + ignored, GDB will wait indefinitely for an inferior function to + complete, unless interrupted by the user using Ctrl-C. + * Python API ** New function gdb.notify_mi(NAME, DATA), that emits custom diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index e4c00143fd1..5725aeb0713 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -20997,6 +20997,72 @@ @code{step}, etc). In this case, when the inferior finally returns to the dummy-frame, @value{GDBN} will once again halt the inferior. +On targets that support asynchronous execution (@pxref{Background +Execution}) @value{GDBN} can place a timeout on any functions called +from @value{GDBN}. If the timeout expires and the function call is +still ongoing, then @value{GDBN} will interrupt the program. + +For targets that don't support asynchronous execution +(@pxref{Background Execution}) then timeouts for functions called from +@value{GDBN} are not supported, the timeout settings described below +will be treated as @code{unlimited}, meaning @value{GDBN} will wait +indefinitely for function call to complete, unless interrupted by the +user using @kbd{Ctrl-C}. + +@table @code +@item set direct-call-timeout @var{seconds} +@kindex set direct-call-timeout +@cindex timeout for called functions +Set the timeout used when calling functions in the program to +@var{seconds}, which should be an integer greater than zero, or the +special value @code{unlimited}, which indicates no timeout should be +used. The default for this setting is @code{unlimited}. + +This setting is used when the user calls a function directly from the +command prompt, for example with a @code{call} or @code{print} +command. + +This setting only works for targets that support asynchronous +execution (@pxref{Background Execution}), for any other target the +setting is treated as @code{unlimited}. + +@item show direct-call-timeout +@kindex show direct-call-timeout +@cindex timeout for called functions +Show the timeout used when calling functions in the program with a +@code{call} or @code{print} command. +@end table + +It is also possible to call functions within the program from the +condition of a conditional breakpoint (@pxref{Conditions, ,Break +Conditions}). A different setting controls the timeout used for +function calls made from a breakpoint condition. + +@table @code +@item set indirect-call-timeout @var{seconds} +@kindex set indirect-call-timeout +@cindex timeout for called functions +Set the timeout used when calling functions in the program from a +breakpoint or watchpoint condition to @var{seconds}, which should be +an integer greater than zero, or the special value @code{unlimited}, +which indicates no timeout should be used. The default for this +setting is @code{30} seconds. + +This setting only works for targets that support asynchronous +execution (@pxref{Background Execution}), for any other target the +setting is treated as @code{unlimited}. + +If a function called from a breakpoint or watchpoint condition times +out, then @value{GDBN} will stop at the point where the timeout +occurred. The breakpoint condition evaluation will be abandoned. + +@item show indirect-call-timeout +@kindex show indirect-call-timeout +@cindex timeout for called functions +Show the timeout used when calling functions in the program from a +breakpoint or watchpoint condition. +@end table + @subsection Calling functions with no debug info @cindex no debug info functions diff --git a/gdb/infcall.c b/gdb/infcall.c index 935a555e817..a295b1e7090 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -96,6 +96,53 @@ show_may_call_functions_p (struct ui_file *file, int from_tty, value); } +/* A timeout (in seconds) for direct inferior calls. A direct inferior + call is one the user triggers from the prompt, e.g. with a 'call' or + 'print' command. Compare with the definition of indirect calls below. */ + +static unsigned int direct_call_timeout = UINT_MAX; + +/* Implement 'show direct-call-timeout'. */ + +static void +show_direct_call_timeout (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + if (target_has_execution () && !target_can_async_p ()) + gdb_printf (file, _("Current target does not support async mode, timeout " + "for direct inferior calls is \"unlimited\".\n")); + else if (direct_call_timeout == UINT_MAX) + gdb_printf (file, _("Timeout for direct inferior function calls " + "is \"unlimited\".\n")); + else + gdb_printf (file, _("Timeout for direct inferior function calls " + "is \"%s seconds\".\n"), value); +} + +/* A timeout (in seconds) for indirect inferior calls. An indirect inferior + call is one that originates from within GDB, for example, when + evaluating an expression for a conditional breakpoint. Compare with + the definition of direct calls above. */ + +static unsigned int indirect_call_timeout = 30; + +/* Implement 'show indirect-call-timeout'. */ + +static void +show_indirect_call_timeout (struct ui_file *file, int from_tty, + struct cmd_list_element *c, const char *value) +{ + if (target_has_execution () && !target_can_async_p ()) + gdb_printf (file, _("Current target does not support async mode, timeout " + "for indirect inferior calls is \"unlimited\".\n")); + else if (indirect_call_timeout == UINT_MAX) + gdb_printf (file, _("Timeout for indirect inferior function calls " + "is \"unlimited\".\n")); + else + gdb_printf (file, _("Timeout for indirect inferior function calls " + "is \"%s seconds\".\n"), value); +} + /* How you should pass arguments to a function depends on whether it was defined in K&R style or prototype style. If you define a function using the K&R syntax that takes a `float' argument, then @@ -620,6 +667,85 @@ call_thread_fsm::should_notify_stop () return true; } +/* A class to control creation of a timer that will interrupt a thread + during an inferior call. */ +struct infcall_timer_controller +{ + /* Setup an event-loop timer that will interrupt PTID if the inferior + call takes too long. DIRECT_CALL_P is true when this inferior call is + a result of the user using a 'print' or 'call' command, and false when + this inferior call is a result of e.g. a conditional breakpoint + expression, this is used to select which timeout to use. */ + infcall_timer_controller (thread_info *thr, bool direct_call_p) + : m_thread (thr) + { + unsigned int timeout + = direct_call_p ? direct_call_timeout : indirect_call_timeout; + if (timeout < UINT_MAX && target_can_async_p ()) + { + int ms = timeout * 1000; + int id = create_timer (ms, infcall_timer_controller::timed_out, this); + m_timer_id.emplace (id); + infcall_debug_printf ("Setting up infcall timeout timer for " + "ptid %s: %d milliseconds", + m_thread->ptid.to_string ().c_str (), ms); + } + } + + /* Destructor. Ensure that the timer is removed from the event loop. */ + ~infcall_timer_controller () + { + /* If the timer has already triggered, then it will have already been + deleted from the event loop. If the timer has not triggered, then + delete it now. */ + if (m_timer_id.has_value () && !m_triggered) + delete_timer (*m_timer_id); + + /* Just for clarity, discard the timer id now. */ + m_timer_id.reset (); + } + + /* Return true if there was a timer in place, and the timer triggered, + otherwise, return false. */ + bool triggered_p () + { + gdb_assert (!m_triggered || m_timer_id.has_value ()); + return m_triggered; + } + +private: + /* The thread we should interrupt. */ + thread_info *m_thread; + + /* Set true when the timer is triggered. */ + bool m_triggered = false; + + /* Given a value when a timer is in place. */ + std::optional m_timer_id; + + /* Callback for the timer, forwards to ::trigger below. */ + static void + timed_out (gdb_client_data context) + { + infcall_timer_controller *ctrl + = static_cast (context); + ctrl->trigger (); + } + + /* Called when the timer goes off. Stop thread M_THREAD. */ + void + trigger () + { + m_triggered = true; + + scoped_disable_commit_resumed disable_commit_resumed ("infcall timeout"); + + infcall_debug_printf ("Stopping thread %s", + m_thread->ptid.to_string ().c_str ()); + target_stop (m_thread->ptid); + } +}; + /* Subroutine of call_function_by_hand to simplify it. Start up the inferior and wait for it to stop. Return the exception if there's an error, or an exception with @@ -630,13 +756,15 @@ call_thread_fsm::should_notify_stop () static struct gdb_exception run_inferior_call (std::unique_ptr sm, - struct thread_info *call_thread, CORE_ADDR real_pc) + struct thread_info *call_thread, CORE_ADDR real_pc, + bool *timed_out_p) { INFCALL_SCOPED_DEBUG_ENTER_EXIT; struct gdb_exception caught_error; ptid_t call_thread_ptid = call_thread->ptid; int was_running = call_thread->state == THREAD_RUNNING; + *timed_out_p = false; infcall_debug_printf ("call function at %s in thread %s, was_running = %d", core_addr_to_string (real_pc), @@ -681,11 +809,23 @@ run_inferior_call (std::unique_ptr sm, infrun_debug_show_threads ("non-exited threads after proceed for inferior-call", all_non_exited_threads ()); + /* Setup a timer (if possible, and if the settings allow) to prevent + the inferior call running forever. */ + bool direct_call_p = !call_thread->control.in_cond_eval; + infcall_timer_controller infcall_timer (call_thread, direct_call_p); + /* Inferior function calls are always synchronous, even if the target supports asynchronous execution. */ wait_sync_command_done (); - infcall_debug_printf ("inferior call completed successfully"); + /* If the timer triggered then the inferior call failed. */ + if (infcall_timer.triggered_p ()) + { + infcall_debug_printf ("inferior call timed out"); + *timed_out_p = true; + } + else + infcall_debug_printf ("inferior call completed successfully"); } catch (gdb_exception &e) { @@ -1358,6 +1498,10 @@ call_function_by_hand_dummy (struct value *function, scoped_restore restore_stopped_by_random_signal = make_scoped_restore (&stopped_by_random_signal, 0); + /* Set to true by the call to run_inferior_call below if the inferior + call is artificially interrupted by GDB due to taking too long. */ + bool timed_out_p = false; + /* - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - SNIP - If you're looking to implement asynchronous dummy-frames, then just below is the place to chop this function in two.. */ @@ -1384,7 +1528,8 @@ call_function_by_hand_dummy (struct value *function, struct_addr); { std::unique_ptr sm_up (sm); - e = run_inferior_call (std::move (sm_up), call_thread.get (), real_pc); + e = run_inferior_call (std::move (sm_up), call_thread.get (), real_pc, + &timed_out_p); } if (e.reason < 0) @@ -1543,7 +1688,10 @@ When the function is done executing, GDB will silently stop."), std::string name = get_function_name (funaddr, name_buf, sizeof (name_buf)); - if (stopped_by_random_signal) + /* If the inferior call timed out then it will have been interrupted + by a signal, but we want to report this differently to the user, + which is done later in this function. */ + if (stopped_by_random_signal && !timed_out_p) { /* We stopped inside the FUNCTION because of a random signal. Further execution of the FUNCTION is not @@ -1594,6 +1742,36 @@ GDB remains in the frame where the signal was received.\n\ To change this behavior use \"set unwindonsignal on\".\n\ Evaluation of the expression containing the function\n\ (%s) will be abandoned.\n\ +When the function is done executing, GDB will silently stop."), + name.c_str ()); + } + } + + if (timed_out_p) + { + /* 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. + + 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. */ + { + /* The user wants to stay in the frame where we stopped + (default). Discard inferior status, we're not at the same + point we started at. */ + discard_infcall_control_state (inf_status.release ()); + + error (_("\ +The program being debugged timed out while in a function called from GDB.\n\ +GDB remains in the frame where the timeout occurred.\n\ +To change this behavior use \"set unwind-on-timeout on\".\n\ +Evaluation of the expression containing the function\n\ +(%s) will be abandoned.\n\ When the function is done executing, GDB will silently stop."), name.c_str ()); } @@ -1707,6 +1885,30 @@ The default is to unwind the frame."), show_unwind_on_terminating_exception_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."), _("\ +Show the timeout, for direct calls to inferior function calls."), _("\ +If running on a target that supports, and is running in, async mode\n\ +then this timeout is used for any inferior function calls triggered\n\ +directly from the prompt, i.e. from a 'call' or 'print' command. The\n\ +timeout is specified in seconds."), + nullptr, + show_direct_call_timeout, + &setlist, &showlist); + + add_setshow_uinteger_cmd ("indirect-call-timeout", no_class, + &indirect_call_timeout, _("\ +Set the timeout, for indirect calls to inferior function calls."), _("\ +Show the timeout, for indirect calls to inferior function calls."), _("\ +If running on a target that supports, and is running in, async mode\n\ +then this timeout is used for any inferior function calls triggered\n\ +indirectly, i.e. being made as part of a breakpoint, or watchpoint,\n\ +condition expression. The timeout is specified in seconds."), + nullptr, + show_indirect_call_timeout, + &setlist, &showlist); + add_setshow_boolean_cmd ("infcall", class_maintenance, &debug_infcall, _("Set inferior call debugging."), diff --git a/gdb/testsuite/gdb.base/help.exp b/gdb/testsuite/gdb.base/help.exp index 87919a819ab..504bf90cc15 100644 --- a/gdb/testsuite/gdb.base/help.exp +++ b/gdb/testsuite/gdb.base/help.exp @@ -121,7 +121,7 @@ gdb_test "help info bogus-gdb-command" "Undefined info command: \"bogus-gdb-comm gdb_test "help gotcha" "Undefined command: \"gotcha\"\. Try \"help\"\." # Test apropos regex. -gdb_test "apropos \\\(print\[\^\[ bsiedf\\\".-\]\\\)" "handle -- Specify how to handle signals\." +gdb_test "apropos \\\(print\[\^\[ bsiedf\\\"'.-\]\\\)" "handle -- Specify how to handle signals\." # Test apropos >1 word string. gdb_test "apropos handle signal" "handle -- Specify how to handle signals\." # Test apropos apropos. diff --git a/gdb/testsuite/gdb.base/infcall-timeout.c b/gdb/testsuite/gdb.base/infcall-timeout.c new file mode 100644 index 00000000000..12774ca2599 --- /dev/null +++ b/gdb/testsuite/gdb.base/infcall-timeout.c @@ -0,0 +1,36 @@ +/* Copyright 2022-2023 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +/* This function is called from GDB. */ +int +function_that_never_returns () +{ + while (1) + sleep (1); + + return 0; +} + +int +main () +{ + alarm (300); + + return 0; +} diff --git a/gdb/testsuite/gdb.base/infcall-timeout.exp b/gdb/testsuite/gdb.base/infcall-timeout.exp new file mode 100644 index 00000000000..c6bf8f8ec2d --- /dev/null +++ b/gdb/testsuite/gdb.base/infcall-timeout.exp @@ -0,0 +1,94 @@ +# Copyright 2022-2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test GDB's direct-call-timeout setting, that is, ensure that if an +# inferior function call, invoked from e.g. a 'print' command, takes +# too long, then GDB can interrupt it, and return control to the user. + +standard_testfile + +if { [build_executable "failed to prepare" ${binfile} "${srcfile}" \ + {debug}] == -1 } { + return +} + +# Start GDB according to TARGET_ASYNC, TARGET_NON_STOP, and NON_STOP, +# then adjust the direct-call-timeout, and make an inferior function +# call that will never return. GDB should eventually timeout and stop +# the inferior. +proc run_test { target_async target_non_stop non_stop } { + save_vars { ::GDBFLAGS } { + append ::GDBFLAGS \ + " -ex \"maint set target-non-stop $target_non_stop\"" + append ::GDBFLAGS \ + " -ex \"set non-stop $non_stop\"" + append ::GDBFLAGS \ + " -ex \"maintenance set target-async ${target_async}\"" + + clean_restart ${::binfile} + } + + if {![runto_main]} { + return + } + + gdb_test_no_output "set direct-call-timeout 5" + + # When non-stop mode is off we get slightly different output from GDB. + if { ([target_info gdb_protocol] == "remote" + || [target_info gdb_protocol] == "extended-remote") + && !$target_non_stop } { + set stopped_line_pattern "Program received signal SIGINT, Interrupt\\." + } else { + set stopped_line_pattern "Program stopped\\." + } + + gdb_test "print function_that_never_returns ()" \ + [multi_line \ + $stopped_line_pattern \ + ".*" \ + "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 .*.*" +} + +foreach_with_prefix target_async { "on" "off" } { + + if { !$target_async } { + # GDB can't timeout while waiting for a thread if the target + # runs with async-mode turned off; once the target is running + # GDB is effectively blocked until the target stops for some + # reason. + continue + } + + foreach_with_prefix target_non_stop { "on" "off" } { + foreach_with_prefix non_stop { "on" "off" } { + if { $non_stop && !$target_non_stop } { + # It doesn't make sense to operate GDB in non-stop + # mode when the target has (in theory) non-stop mode + # disabled. + continue + } + + run_test $target_async $target_non_stop $non_stop + } + } +} diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.c b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.c new file mode 100644 index 00000000000..4da4245746e --- /dev/null +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.c @@ -0,0 +1,169 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2022-2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include +#include +#include +#include +#include +#include + +#define NUM_THREADS 5 + +/* Semaphores, used to track when threads have started, and to control + when the threads finish. */ +sem_t startup_semaphore; +sem_t finish_semaphore; +sem_t thread_1_semaphore; +sem_t thread_2_semaphore; + +/* Mutex to control when the first worker thread hit a breakpoint + location. */ +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + +/* Global variable to poke, just so threads have something to do. */ +volatile int global_var = 0; + +int +condition_func () +{ + /* Let thread 2 run. */ + if (sem_post (&thread_2_semaphore) != 0) + abort (); + + /* Wait for thread 2 to complete its actions. */ + if (sem_wait (&thread_1_semaphore) != 0) + abort (); + + return 1; +} + +void +do_segfault () +{ + volatile int *p = 0; + *p = 0; /* Segfault here. */ +} + +void * +worker_func (void *arg) +{ + int tid = *((int *) arg); + + /* Let the main thread know that this worker has started. */ + if (sem_post (&startup_semaphore) != 0) + abort (); + + switch (tid) + { + case 0: + /* Wait for MUTEX to become available, then pass through the + conditional breakpoint location. */ + if (pthread_mutex_lock (&mutex) != 0) + abort (); + global_var = 99; /* Conditional breakpoint here. */ + if (pthread_mutex_unlock (&mutex) != 0) + abort (); + break; + + case 1: + if (sem_wait (&thread_2_semaphore) != 0) + abort (); + do_segfault (); + if (sem_post (&thread_1_semaphore) != 0) + abort (); + + /* Fall through. */ + default: + /* Wait until we are allowed to finish. */ + if (sem_wait (&finish_semaphore) != 0) + abort (); + break; + } +} + +void +stop_marker () +{ + global_var = 99; /* Stop marker. */ +} + +/* The main program entry point. */ + +int +main () +{ + pthread_t threads[NUM_THREADS]; + int args[NUM_THREADS]; + void *retval; + + /* An alarm, just in case the thread deadlocks. */ + alarm (300); + + /* Semaphore initialization. */ + if (sem_init (&startup_semaphore, 0, 0) != 0) + abort (); + if (sem_init (&finish_semaphore, 0, 0) != 0) + abort (); + if (sem_init (&thread_1_semaphore, 0, 0) != 0) + abort (); + if (sem_init (&thread_2_semaphore, 0, 0) != 0) + abort (); + + /* Lock MUTEX, this prevents the first worker thread from rushing ahead. */ + if (pthread_mutex_lock (&mutex) != 0) + abort (); + + /* Worker thread creation. */ + for (int i = 0; i < NUM_THREADS; i++) + { + args[i] = i; + pthread_create (&threads[i], NULL, worker_func, &args[i]); + } + + /* Wait for every thread to start. */ + for (int i = 0; i < NUM_THREADS; i++) + { + if (sem_wait (&startup_semaphore) != 0) + abort (); + } + + /* Unlock the first thread so it can proceed. */ + if (pthread_mutex_unlock (&mutex) != 0) + abort (); + + /* Wait for the first thread only. */ + pthread_join (threads[0], &retval); + + /* Now post FINISH_SEMAPHORE to allow all the other threads to finish. */ + for (int i = 1; i < NUM_THREADS; i++) + sem_post (&finish_semaphore); + + /* Now wait for the remaining threads to complete. */ + for (int i = 1; i < NUM_THREADS; i++) + pthread_join (threads[i], &retval); + + /* Semaphore cleanup. */ + sem_destroy (&finish_semaphore); + sem_destroy (&startup_semaphore); + sem_destroy (&thread_1_semaphore); + sem_destroy (&thread_2_semaphore); + + stop_marker (); + + return 0; +} diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.exp b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.exp new file mode 100644 index 00000000000..7bb6b4db813 --- /dev/null +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-timeout.exp @@ -0,0 +1,166 @@ +# Copyright 2022-2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Tests inferior calls executed from a breakpoint condition in +# a multi-threaded program. +# +# This test has the inferior function call timeout, and checks how GDB +# handles this situation. + +standard_testfile + +if { [build_executable "failed to prepare" ${binfile} "${srcfile}" \ + {debug pthreads}] } { + return +} + +set cond_bp_line [gdb_get_line_number "Conditional breakpoint here"] +set final_bp_line [gdb_get_line_number "Stop marker"] +set segfault_line [gdb_get_line_number "Segfault here"] + +# Setup GDB based on TARGET_ASYNC, TARGET_NON_STOP, and NON_STOP. +# Setup some breakpoints in the inferior, one of which has an inferior +# call within its condition. +# +# Continue GDB, the breakpoint with inferior call will be hit, but the +# inferior call will never return. We expect GDB to timeout. +# +# The reason that the inferior call never completes is that a second +# 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 non_stop other_thread_bp } { + save_vars { ::GDBFLAGS } { + append ::GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\"" + append ::GDBFLAGS " -ex \"maint non-stop $non_stop\"" + append ::GDBFLAGS " -ex \"maintenance set target-async ${target_async}\"" + + clean_restart ${::binfile} + } + + if {![runto_main]} { + return + } + + # The default timeout for indirect inferior calls (e.g. inferior + # calls for conditional breakpoint expressions) is pretty high. + # We don't want the test to take too long, so reduce this. + # + # However, the test relies on a second thread hitting some event + # (either a breakpoint or signal) before this timeout expires. + # + # There is a chance that on a really slow system this might not + # happen, in which case the test might fail. + # + # However, we still allocate 5 seconds, which feels like it should + # be enough time in most cases, but maybe we need to do something + # smarter here? Possibly we could have some initial run where the + # inferior doesn't timeout, but does perform the same interaction + # between threads, we could time that, and use that as the basis + # for this timeout. For now though, we just hope 5 seconds is + # enough. + gdb_test_no_output "set indirect-call-timeout 5" + + gdb_breakpoint \ + "${::srcfile}:${::cond_bp_line} if (condition_func ())" + set bp_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number for conditional breakpoint"] + + gdb_breakpoint "${::srcfile}:${::final_bp_line}" + set final_bp_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number for final breakpoint"] + + # The thread performing an inferior call relies on a second + # thread. The second thread will segfault unless it hits a + # breakpoint first. In either case the initial thread will not + # complete its inferior call. + if { $other_thread_bp } { + gdb_breakpoint "${::srcfile}:${::segfault_line}" + set segfault_bp_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number for segfault breakpoint"] + } + + # When non-stop mode is off we get slightly different output from GDB. + if { ([target_info gdb_protocol] == "remote" + || [target_info gdb_protocol] == "extended-remote") + && !$target_non_stop} { + set stopped_line_pattern "Thread ${::decimal} \"\[^\r\n\"\]+\" received signal SIGINT, Interrupt\\." + } else { + set stopped_line_pattern "Thread ${::decimal} \"\[^\r\n\"\]+\" stopped\\." + } + + gdb_test "continue" \ + [multi_line \ + $stopped_line_pattern \ + ".*" \ + "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, + # if we try to resume then we should see the pending event from + # that other thread. + if { $other_thread_bp } { + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "" \ + "Thread ${::decimal} \"\[^\"\r\n\]+\" hit Breakpoint ${segfault_bp_num}, do_segfault \[^\r\n\]+:${::segfault_line}" \ + "${::decimal}\\s+\[^\r\n\]+Segfault here\[^\r\n\]+"] \ + "hit the segfault breakpoint" + } else { + gdb_test "continue" \ + [multi_line \ + "Continuing\\." \ + ".*" \ + "Thread ${::decimal} \"infcall-from-bp\" received signal SIGSEGV, Segmentation fault\\." \ + "\\\[Switching to Thread \[^\r\n\]+\\\]" \ + "${::hex} in do_segfault \\(\\) at \[^\r\n\]+:${::segfault_line}" \ + "${::decimal}\\s+\[^\r\n\]+Segfault here\[^\r\n\]+"] \ + "hit the segfault" + } +} + +foreach_with_prefix target_async {"on" "off" } { + + if { !$target_async } { + # GDB can't timeout while waiting for a thread if the target + # runs with async-mode turned off; once the target is running + # GDB is effectively blocked until the target stops for some + # reason. + continue + } + + foreach_with_prefix target_non_stop {"off" "on"} { + foreach_with_prefix non_stop {"off" "on"} { + if { $non_stop && !$target_non_stop } { + # It doesn't make sense to operate GDB in non-stop + # mode when the target has (in theory) non-stop mode + # disabled. + continue + } + foreach_with_prefix other_thread_bp { true false } { + run_test $target_async $target_non_stop $non_stop $other_thread_bp + } + } + } +} From patchwork Sat Dec 2 10:52:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 81196 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 477AA385DC0D for ; Sat, 2 Dec 2023 10:53:29 +0000 (GMT) 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 15508386183C for ; Sat, 2 Dec 2023 10:52:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 15508386183C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 15508386183C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514373; cv=none; b=JUFXVM6qhQ6tz9Xrs12zBLukO3jG09XQeziCD7he4voDcVNt32GtopivWWwFQ/i/Tq83yoGj9XiaSOu743Ydw9nPstbuOijqFh1B22TDPX/3avz94o9Js3OLWOO4cO0g9nh3gFWu68f73ISQVklp+ag88ZgFZKAp+M5+q1AoBMY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514373; c=relaxed/simple; bh=5AzenzRtXTo7xmwRMf6LimqbBoxyStGPpso5ueeYN0c=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=U0QrczayJFDLB0GxUV3lJa/Ai6BZ6dYLEovqxQEsdrjI9AxyIOmcLfOe3OFh3+VhgZAvcri+1JAEZV8yE0/1QZAls1Gb9BW/N9r6sj33emOxhIEQyPKdtLozjwhZy3bVVuKRNJ5q5QeiYxvriRcN2a/WNQvjIuDnqVOXFG4SJkY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701514370; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=k2j+KtNBM6jl0tA+lkvAYFbguFRqLkh6m7RvLy5vPC0=; b=KimAoPJNZDeOu+DI3A0SgDtrkhzfRE8FNFRkYa3UhNAsvtdD0V0CBueL5XV9BDK8e06AjX cBWSCux1Z659qyyQjglCBmXhZsu1uxQU6QBmJgL+Hy8+q2frzoA//tfLQ/7uyMH/+Y3/pI RdSMGm3AqHiYdfNIcBBAkehZeOiKEWE= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-78-pHHRsN3jOGqmI0v3Hm8HhA-1; Sat, 02 Dec 2023 05:52:49 -0500 X-MC-Unique: pHHRsN3jOGqmI0v3Hm8HhA-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a19494e36bbso193984466b.0 for ; Sat, 02 Dec 2023 02:52:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701514368; x=1702119168; 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=k2j+KtNBM6jl0tA+lkvAYFbguFRqLkh6m7RvLy5vPC0=; b=KXxJ2sgpsbzOqSMRJDZUzjZI2VVIs2Xv7n5o67XZHydkp3AOKaSTkSMDxC/zvdToQB 0Gt2by7Na7ZhBOBtp2btITcrYUNrsMzcOWmvq4thwHVz4uFuM8AolVQqVogtKunkdS9H 9ji4A3l0ErXV5TCefZeALCUHrsHRUYt2IGiENlhlGIV5j4qqkxOvcyqFB1tx4mRXjqmY I/l3jJFp1bCu/324WmumE8d9p3S428Gl0LoErD+UjplnirdbXQkEg5E6aUL9H7gVdkiO HooSGai7XHw2M5A3oK8HG5DLRO1GeHtHCQv4i2oihpKOaGQui6yRANx7JTc1g5YNfQfz tEyQ== X-Gm-Message-State: AOJu0YzbW85LxvRhyXn12gT6M18tbv8NncKftkmlATxhfvMrNOIZlXSE xqRXqrzxleguSrQAgihis7Pes3U7mFKxCq/vvHqKBv9SU4vLktbM6bdQiWcsN6EbrQHvze9rJOl mU0ZVHS3zu+eb636A+kYOjDbAdEGQ69dL2FFrFi4kuKDfyhgh7iUxdb9jqB/Mt7tds/LJQ5K+PE aXQWdIgQ== X-Received: by 2002:a17:906:73c2:b0:a11:1925:8d3b with SMTP id n2-20020a17090673c200b00a1119258d3bmr1769788ejl.3.1701514368053; Sat, 02 Dec 2023 02:52:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IEZmVHeF7tHexWyy7mQa+fd/Vs7Lz9P4qLYBQ1P58qDNoPpa2/uTx42EmmThbnFNtIR45yFcg== X-Received: by 2002:a17:906:73c2:b0:a11:1925:8d3b with SMTP id n2-20020a17090673c200b00a1119258d3bmr1769781ejl.3.1701514367525; Sat, 02 Dec 2023 02:52:47 -0800 (PST) Received: from localhost (92.40.184.5.threembb.co.uk. [92.40.184.5]) by smtp.gmail.com with ESMTPSA id re17-20020a170906d8d100b009fc0c42098csm2962003ejb.173.2023.12.02.02.52.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 02:52:47 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii Subject: [PATCHv9 4/5] gdb: introduce unwind-on-timeout setting Date: Sat, 2 Dec 2023 10:52:33 +0000 Message-Id: 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=-8.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Eli has already approved the NEWS/doc changes in this file. --- 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. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 9 +++ gdb/doc/gdb.texinfo | 31 ++++++-- gdb/infcall.c | 78 +++++++++++++++++-- gdb/testsuite/gdb.base/infcall-timeout.exp | 63 ++++++++++----- .../infcall-from-bp-cond-timeout.exp | 69 ++++++++++------ 5 files changed, 194 insertions(+), 56 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 3500acd42b6..b5ac1b848d6 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -56,6 +56,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 removing the + dummy frame that was added for the inferior call, and restoring the + inferior state to how it was before the inferior call started. + * Python API ** New function gdb.notify_mi(NAME, DATA), that emits custom diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 5725aeb0713..9ba9e898977 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -20942,6 +20942,22 @@ Show the current setting of stack unwinding in the functions called by @value{GDBN}. +@anchor{set unwind-on-timeout} +@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 @@ -20973,11 +20989,11 @@ If a called function is interrupted for any reason, including hitting a breakpoint, or triggering a watchpoint, and the stack is not unwound -due to @code{set unwind-on-terminating-exception on} or @code{set -unwindonsignal on} (@pxref{stack unwind settings}), -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: +due to @code{set unwind-on-terminating-exception on}, @code{set +unwind-on-timeout on}, or @code{set unwindonsignal on} (@pxref{stack +unwind settings}), 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 @@ -21002,6 +21018,11 @@ from @value{GDBN}. If the timeout expires and the function call is still ongoing, then @value{GDBN} will interrupt the program. +If a function called from @value{GDBN} is interrupted by a timeout, +then by default the inferior is left in the frame where the timeout +occurred, this behaviour can be adjusted with @samp{set +unwind-on-timeout} (@pxref{set unwind-on-timeout}). + For targets that don't support asynchronous execution (@pxref{Background Execution}) then timeouts for functions called from @value{GDBN} are not supported, the timeout settings described below diff --git a/gdb/infcall.c b/gdb/infcall.c index a295b1e7090..84c6bf2cdd2 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -218,6 +218,27 @@ show_unwind_on_terminating_exception_p (struct ui_file *file, int from_tty, value); } +/* This boolean tells GDB what to do if an inferior function, called from + GDB, times out. If true, GDB unwinds the stack and restores the context + to what it was before the call. When false, GDB leaves the thread as it + is at the point of the timeout. + + The default is to stop in the frame where the timeout occurred. */ + +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. @@ -574,6 +595,16 @@ struct call_thread_fsm : public thread_fsm bool should_stop (struct thread_info *thread) override; bool should_notify_stop () override; + + /* Record that this thread timed out while performing an infcall. */ + void timed_out () + { + m_timed_out = true; + } + +private: + /* Set true if the thread timed out while performing an infcall. */ + bool m_timed_out = false; }; /* Allocate a new call_thread_fsm object. */ @@ -649,7 +680,8 @@ call_thread_fsm::should_notify_stop () infcall_debug_printf ("inferior call didn't complete fully"); - if (stopped_by_random_signal && unwind_on_signal_p) + if ((stopped_by_random_signal && unwind_on_signal_p) + || (m_timed_out && unwind_on_timeout_p)) { infcall_debug_printf ("unwind-on-signal is on, don't notify"); return false; @@ -742,6 +774,9 @@ struct infcall_timer_controller infcall_debug_printf ("Stopping thread %s", m_thread->ptid.to_string ().c_str ()); + call_thread_fsm *fsm + = gdb::checked_static_cast (m_thread->thread_fsm ()); + fsm->timed_out (); target_stop (m_thread->ptid); } }; @@ -1752,14 +1787,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 ()); - 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. */ + /* We also need to restore inferior status to that before the + dummy call. */ + restore_infcall_control_state (inf_status.release ()); + + 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 @@ -1885,6 +1933,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 c6bf8f8ec2d..cfb9a907633 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}" \ # then adjust the direct-call-timeout, and make an inferior function # call that will never return. GDB should eventually timeout and stop # the inferior. -proc run_test { target_async target_non_stop 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 run_test { target_async target_non_stop non_stop unwind } { save_vars { ::GDBFLAGS } { append ::GDBFLAGS \ " -ex \"maint set target-non-stop $target_non_stop\"" @@ -45,28 +49,43 @@ proc run_test { target_async target_non_stop 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 \ + "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\\."] - # When non-stop mode is off we get slightly different output from GDB. - if { ([target_info gdb_protocol] == "remote" - || [target_info gdb_protocol] == "extended-remote") - && !$target_non_stop } { - set stopped_line_pattern "Program received signal SIGINT, Interrupt\\." + gdb_test "bt" \ + "#0\\s+main \\(\\).*" } else { - set stopped_line_pattern "Program stopped\\." - } + # When non-stop mode is off we get slightly different output from GDB. + if { ([target_info gdb_protocol] == "remote" + || [target_info gdb_protocol] == "extended-remote") + && !$target_non_stop } { + set stopped_line_pattern "Program received signal SIGINT, Interrupt\\." + } else { + set stopped_line_pattern "Program stopped\\." + } - gdb_test "print function_that_never_returns ()" \ - [multi_line \ - $stopped_line_pattern \ - ".*" \ - "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 "print function_that_never_returns ()" \ + [multi_line \ + $stopped_line_pattern \ + ".*" \ + "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 .*.*" + } } foreach_with_prefix target_async { "on" "off" } { @@ -88,7 +107,9 @@ foreach_with_prefix target_async { "on" "off" } { continue } - run_test $target_async $target_non_stop $non_stop + foreach_with_prefix unwind { "on" "off" } { + run_test $target_async $target_non_stop $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 7bb6b4db813..f3168b89231 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 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 non_stop other_thread_bp unwind } { save_vars { ::GDBFLAGS } { append ::GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\"" append ::GDBFLAGS " -ex \"maint non-stop $non_stop\"" @@ -72,6 +77,7 @@ proc run_test { target_async target_non_stop 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,27 +98,43 @@ proc run_test { target_async target_non_stop non_stop other_thread_bp } { "get number for segfault breakpoint"] } - # When non-stop mode is off we get slightly different output from GDB. - if { ([target_info gdb_protocol] == "remote" - || [target_info gdb_protocol] == "extended-remote") - && !$target_non_stop} { - set stopped_line_pattern "Thread ${::decimal} \"\[^\r\n\"\]+\" received signal SIGINT, Interrupt\\." + if { $unwind } { + gdb_test "continue" \ + [multi_line \ + "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 { - set stopped_line_pattern "Thread ${::decimal} \"\[^\r\n\"\]+\" stopped\\." - } + # When non-stop mode is off we get slightly different output from GDB. + if { ([target_info gdb_protocol] == "remote" + || [target_info gdb_protocol] == "extended-remote") + && !$target_non_stop} { + set stopped_line_pattern \ + "Thread ${::decimal} \"\[^\r\n\"\]+\" received signal SIGINT, Interrupt\\." + } else { + set stopped_line_pattern "Thread ${::decimal} \"\[^\r\n\"\]+\" stopped\\." + } - gdb_test "continue" \ - [multi_line \ - $stopped_line_pattern \ - ".*" \ - "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" + gdb_test "continue" \ + [multi_line \ + "$stopped_line_pattern" \ + ".*" \ + "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, @@ -158,8 +180,11 @@ foreach_with_prefix target_async {"on" "off" } { # disabled. continue } - foreach_with_prefix other_thread_bp { true false } { - run_test $target_async $target_non_stop $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 $non_stop \ + $other_thread_bp $unwind + } } } } From patchwork Sat Dec 2 10:52:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 81197 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 82627385C6D9 for ; Sat, 2 Dec 2023 10:53:36 +0000 (GMT) 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 8FC073861884 for ; Sat, 2 Dec 2023 10:52:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8FC073861884 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8FC073861884 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514377; cv=none; b=lWen0Oa9Z/sp8iaEaZyK0XtXP1nUzU2RAlUNP1BiHtyWv8npS/+Ewo8UfDtV6ZyC8kWPxgdkxRQODvpGGlp0/o/98ydaoykEngIh241YP/+FYCZeKeA23LL4avaucLeT1T/FLYlGixFa7b+c04+7CJn6axnmloMIhcy497ccw4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701514377; c=relaxed/simple; bh=YVk4QO7lkBIAWlL2nZMNCtXdPa3JEubrw/qsWk2ITJU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=dakF7wJfyIsSy8TmKzutPCaaREQqM/gxGoxxz4QTFK6V4F2O2xykMkWFRAQzgqDlxC9YatCb0TYdbkpGMNDXtqpxDrvaHDrSQ0Qcz1dM6hzueryb4BoOIe270Ho9R7XGAcEk/+7rTa7/WybNyQQbysqpLqAgsNVwb7xU5hQViCg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701514373; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aRWJENcoYOGbgx9v7fnuwmZtKMk2GMZHrb7UuuefJdk=; b=RiB6rP/hzaMdrvgJtathAcoo7L+zgj6rH3MSoo9hblRgegTfgRNy7fQab1DHuUd4owee0+ ObQo5ixqHRsSRxnJlrZdJXM7V7dZ1uoN0B/2C49XFSUU9fLvHhfFUU48xrQUsQQgpC7m6v aW8SVYhcmBCaTAqXM1Kxa+E2LNs3SmM= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-124-vo3pwRBwPJ2NeRo_x9ylEw-1; Sat, 02 Dec 2023 05:52:52 -0500 X-MC-Unique: vo3pwRBwPJ2NeRo_x9ylEw-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a19bff73c2eso141421766b.3 for ; Sat, 02 Dec 2023 02:52:51 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701514371; x=1702119171; 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=aRWJENcoYOGbgx9v7fnuwmZtKMk2GMZHrb7UuuefJdk=; b=Kx9vBwl3tAj4OpcNWe0FM/MhCS2Ygb7/lix9M9LNJGGCoao4eKn2CbalFU4Zo4vVk5 LVE7sc84lxN9vO1Kecjl7uUfxvvYvagUdxiDpSvf/Bn9h+pIxc9VwvT5UgRot5BNPPJ6 aOERlKc+BFDUa+eoi+y9fkHqt1IzLcHhbEEzXo2R+YsmjbYp2glLinQ0FDtgGB5GObW+ MBT5HCvLXbAtBRTpOnTC6ktN01pI4pk6hZvTK+6jGRrkh4AUhEHzDnKyteUHNCtmcUor ddwk2mcPJr9pY9KAln64WPzp5/4B2TQ0O1JbM43ZguhxqFQaUKDAB2sczbbxdgv/dY/a fbUQ== X-Gm-Message-State: AOJu0YwwhWDy746OYWNDiTXEDiTsY/APfYib4rjSo8nLRVYS9k6gH4gN ak6pnRI5WV2KJoUADYD6pIxS6fmPVfZ/+epFYcxdRRFVAsY+UFUwB7iIGUeySvuRaOFS42rnc+U 5GXJhvjl1F2g3x0TFNXyI+fIjxwxNfo1rBbTBEhRQs6727mQ+B+FY/CEIICiJ6CbPSBS2Z7bTGr sjwMH1TQ== X-Received: by 2002:a17:906:5953:b0:a19:a19b:c717 with SMTP id g19-20020a170906595300b00a19a19bc717mr1838480ejr.103.1701514370425; Sat, 02 Dec 2023 02:52:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IEAtzWRhjedNy95Mwq8AG5xGvEjYJ+DMcTa8lazYD1SauSwUH0/OfizujixNw+kJ32cWx3+dA== X-Received: by 2002:a17:906:5953:b0:a19:a19b:c717 with SMTP id g19-20020a170906595300b00a19a19bc717mr1838468ejr.103.1701514369735; Sat, 02 Dec 2023 02:52:49 -0800 (PST) Received: from localhost (92.40.184.5.threembb.co.uk. [92.40.184.5]) by smtp.gmail.com with ESMTPSA id j21-20020a170906279500b009dddec5a96fsm2951763ejc.170.2023.12.02.02.52.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 02 Dec 2023 02:52:49 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess , Eli Zaretskii Subject: [PATCHv9 5/5] gdb: rename unwindonsignal to unwind-on-signal Date: Sat, 2 Dec 2023 10:52:34 +0000 Message-Id: <4e19c32e7fa75e90a0131a7aa2c96bcbfad96926.1701514067.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=-8.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_ABUSEAT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SBL_CSS, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Eli has already approved the NEWS/doc changes in this file. --- We now have unwind-on-timeout and unwind-on-terminating-exception, and then the odd one out unwindonsignal. I'm not a great fan of these squashed together command names, so in this commit I propose renaming this to unwind-on-signal. Obviously I've added the hidden alias unwindonsignal so any existing GDB scripts will keep working. There's one test that I've extended to test the alias works, but in most of the other test scripts I've changed over to use the new name. The docs are updated to reference the new name. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 9 +++++ gdb/doc/gdb.texinfo | 16 ++++++--- gdb/infcall.c | 21 ++++++----- gdb/testsuite/gdb.base/callfuncs.exp | 4 +-- gdb/testsuite/gdb.base/infcall-failure.exp | 4 +-- gdb/testsuite/gdb.base/unwindonsignal.exp | 36 +++++++++++++------ gdb/testsuite/gdb.compile/compile-cplus.exp | 6 ++-- gdb/testsuite/gdb.compile/compile.exp | 6 ++-- gdb/testsuite/gdb.cp/gdb2495.exp | 16 ++++----- gdb/testsuite/gdb.fortran/function-calls.exp | 2 +- gdb/testsuite/gdb.mi/mi-condbreak-fail.exp | 6 ++-- gdb/testsuite/gdb.mi/mi-condbreak-throw.exp | 2 +- gdb/testsuite/gdb.mi/mi-syn-frame.exp | 2 +- .../infcall-from-bp-cond-simple.exp | 2 +- .../gdb.threads/thread-unwindonsignal.exp | 8 ++--- 15 files changed, 89 insertions(+), 51 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index b5ac1b848d6..592984f21e8 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -16,6 +16,10 @@ disassemble command will now give an error. Previously the 'b' flag would always override the 'r' flag. +set unwindonsignal on|off +show unwindonsignal + These commands are now aliases for the new set/show unwind-on-signal. + * New commands info missing-debug-handler @@ -65,6 +69,11 @@ show unwind-on-timeout dummy frame that was added for the inferior call, and restoring the inferior state to how it was before the inferior call started. +set unwind-on-signal on|off +show unwind-on-signal + These new commands replaces the existing set/show unwindonsignal. The + old command is maintained as an alias. + * Python API ** New function gdb.notify_mi(NAME, DATA), that emits custom diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 9ba9e898977..3c254b259dd 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -20896,7 +20896,7 @@ It is possible for the function you call via the @code{print} or @code{call} command to generate a signal (e.g., if there's a bug in the function, or if you passed it incorrect arguments). What happens -in that case is controlled by the @code{set unwindonsignal} command. +in that case is controlled by the @code{set unwind-on-signal} command. Similarly, with a C@t{++} program it is possible for the function you call via the @code{print} or @code{call} command to generate an @@ -20910,7 +20910,8 @@ @anchor{stack unwind settings} @table @code -@item set unwindonsignal +@item set unwind-on-signal +@kindex set unwind-on-signal @kindex set unwindonsignal @cindex unwind stack in called functions @cindex call dummy stack unwinding @@ -20921,11 +20922,18 @@ default), @value{GDBN} stops in the frame where the signal was received. -@item show unwindonsignal +The command @code{set unwindonsignal} is an alias for this command, +and is maintained for backward compatibility. + +@item show unwind-on-signal +@kindex show unwind-on-signal @kindex show unwindonsignal Show the current setting of stack unwinding in the functions called by @value{GDBN}. +The command @code{show unwindonsignal} is an alias for this command, +and is maintained for backward compatibility. + @item set unwind-on-terminating-exception @kindex set unwind-on-terminating-exception @cindex unwind stack in called functions with unhandled exceptions @@ -20990,7 +20998,7 @@ If a called function is interrupted for any reason, including hitting a breakpoint, or triggering a watchpoint, and the stack is not unwound due to @code{set unwind-on-terminating-exception on}, @code{set -unwind-on-timeout on}, or @code{set unwindonsignal on} (@pxref{stack +unwind-on-timeout on}, or @code{set unwind-on-signal on} (@pxref{stack unwind settings}), 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: diff --git a/gdb/infcall.c b/gdb/infcall.c index 84c6bf2cdd2..bf17a1a21d6 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -1755,7 +1755,7 @@ When the function is done executing, GDB will silently stop."), The program being debugged received signal %s, %s\n\ while in a function called from GDB. GDB has restored the context\n\ to what it was before the call. To change this behavior use\n\ -\"set unwindonsignal off\". Evaluation of the expression containing\n\ +\"set unwind-on-signal off\". Evaluation of the expression containing\n\ the function (%s) will be abandoned."), gdb_signal_to_name (stop_signal), gdb_signal_to_string (stop_signal), @@ -1774,7 +1774,7 @@ the function (%s) will be abandoned."), error (_("\ The program being debugged was signaled while in a function called from GDB.\n\ GDB remains in the frame where the signal was received.\n\ -To change this behavior use \"set unwindonsignal on\".\n\ +To change this behavior use \"set unwind-on-signal on\".\n\ Evaluation of the expression containing the function\n\ (%s) will be abandoned.\n\ When the function is done executing, GDB will silently stop."), @@ -1906,17 +1906,22 @@ The default is to perform the conversion."), show_coerce_float_to_double_p, &setlist, &showlist); - add_setshow_boolean_cmd ("unwindonsignal", no_class, - &unwind_on_signal_p, _("\ + set_show_commands setshow_unwind_on_signal_cmds + = add_setshow_boolean_cmd ("unwind-on-signal", no_class, + &unwind_on_signal_p, _("\ Set unwinding of stack if a signal is received while in a call dummy."), _("\ Show unwinding of stack if a signal is received while in a call dummy."), _("\ -The unwindonsignal lets the user determine what gdb should do if a signal\n\ +The unwind-on-signal lets the user determine what gdb should do if a signal\n\ is received while in a function called from gdb (call dummy). If set, gdb\n\ unwinds the stack and restore the context to what as it was before the call.\n\ The default is to stop in the frame where the signal was received."), - NULL, - show_unwind_on_signal_p, - &setlist, &showlist); + NULL, + show_unwind_on_signal_p, + &setlist, &showlist); + add_alias_cmd ("unwindonsignal", setshow_unwind_on_signal_cmds.set, + no_class, 1, &setlist); + add_alias_cmd ("unwindonsignal", setshow_unwind_on_signal_cmds.show, + no_class, 1, &showlist); add_setshow_boolean_cmd ("unwind-on-terminating-exception", no_class, &unwind_on_terminating_exception_p, _("\ diff --git a/gdb/testsuite/gdb.base/callfuncs.exp b/gdb/testsuite/gdb.base/callfuncs.exp index 2c797a224a7..d9f63e944bf 100644 --- a/gdb/testsuite/gdb.base/callfuncs.exp +++ b/gdb/testsuite/gdb.base/callfuncs.exp @@ -46,7 +46,7 @@ proc do_function_calls {prototypes} { # If any of these calls segv we don't want to affect subsequent tests. # E.g., we want to ensure register values are restored. - gdb_test_no_output "set unwindonsignal on" + gdb_test_no_output "set unwind-on-signal on" gdb_test "p t_char_values(0,0)" " = 0" gdb_test "p t_char_values('a','b')" " = 1" @@ -237,7 +237,7 @@ proc do_function_calls {prototypes} { "call inferior func with struct - returns char *" # Restore default value. - gdb_test_no_output "set unwindonsignal off" + gdb_test_no_output "set unwind-on-signal off" } # Procedure to get current content of all registers. diff --git a/gdb/testsuite/gdb.base/infcall-failure.exp b/gdb/testsuite/gdb.base/infcall-failure.exp index 5ad179a089d..e2e71649dac 100644 --- a/gdb/testsuite/gdb.base/infcall-failure.exp +++ b/gdb/testsuite/gdb.base/infcall-failure.exp @@ -137,7 +137,7 @@ proc_with_prefix run_cond_hits_segfault_test { async_p non_stop_p } { "Error in testing condition for breakpoint ${bp_1_num}:" \ "The program being debugged was signaled while in a function called from GDB\\." \ "GDB remains in the frame where the signal was received\\." \ - "To change this behavior use \"set unwindonsignal on\"\\." \ + "To change this behavior use \"set unwind-on-signal on\"\\." \ "Evaluation of the expression containing the function" \ "\\(func_segfault\\) will be abandoned\\." \ "When the function is done executing, GDB will silently stop\\."] @@ -166,7 +166,7 @@ proc_with_prefix run_call_hits_segfault_test { async_p non_stop_p } { "${::decimal}\\s+\[^\r\n\]+Segfault here\[^\r\n\]+" \ "The program being debugged was signaled while in a function called from GDB\\." \ "GDB remains in the frame where the signal was received\\." \ - "To change this behavior use \"set unwindonsignal on\"\\." \ + "To change this behavior use \"set unwind-on-signal on\"\\." \ "Evaluation of the expression containing the function" \ "\\(func_segfault\\) will be abandoned\\." \ "When the function is done executing, GDB will silently stop\\."] diff --git a/gdb/testsuite/gdb.base/unwindonsignal.exp b/gdb/testsuite/gdb.base/unwindonsignal.exp index 5c2243236ba..bf876fb42ba 100644 --- a/gdb/testsuite/gdb.base/unwindonsignal.exp +++ b/gdb/testsuite/gdb.base/unwindonsignal.exp @@ -34,13 +34,29 @@ gdb_test "break stop_here" "Breakpoint \[0-9\]* at .*" gdb_test "continue" "Continuing.*Breakpoint \[0-9\]*, stop_here.*" \ "continue to breakpoint at stop_here" -# Turn on unwindonsignal. -gdb_test_no_output "set unwindonsignal on" \ - "setting unwindonsignal" +# Turn on unwind-on-signal. +gdb_test_no_output "set unwind-on-signal on" \ + "setting unwind-on-signal" -gdb_test "show unwindonsignal" \ +gdb_test "show unwind-on-signal" \ "Unwinding of stack .* is on." \ - "showing unwindonsignal" + "showing unwind-on-signal" + +# For backward compatibility we maintain a 'unwindonsignal' alias for +# 'unwind-on-signal', check it now. +gdb_test "show unwindonsignal" \ + "Unwinding of stack .* is on\\." \ + "showing unwindonsignal alias" + +gdb_test_no_output "set unwindonsignal off" \ + "setting unwindonsignal alias to off" + +gdb_test "show unwind-on-signal" \ + "Unwinding of stack .* is off\\." \ + "showing unwind-on-signal after setting via alias" + +gdb_test_no_output "set unwindonsignal on" \ + "setting unwindonsignal alias to on" # Call function (causing the program to get a signal), and see if gdb handles # it properly. @@ -49,7 +65,7 @@ if {[gdb_test "call gen_signal ()" \ "The program being debugged received signal SIGABRT, Aborted" \ "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 unwindonsignal off\"\\. Evaluation of the expression containing" \ + "\"set unwind-on-signal off\"\\. Evaluation of the expression containing" \ "the function \\(gen_signal\\) will be abandoned\\."] \ "unwindonsignal, inferior function call signaled"] != 0} { return 0 @@ -58,15 +74,15 @@ if {[gdb_test "call gen_signal ()" \ # Verify the stack got unwound. gdb_test "bt" \ "#0 *\[x0-9a-f in\]*stop_here \\(.*\\) at .*#1 *\[x0-9a-f in\]*main \\(.*\\) at .*" \ - "unwindonsignal, stack unwound" + "stack unwound" # Verify the dummy frame got removed from dummy_frame_stack. gdb_test_multiple "maint print dummy-frames" \ - "unwindonsignal, dummy frame removed" { + "unwind-on-signal, dummy frame removed" { -re "\[\r\n\]*.*stack=.*code=.*\[\r\n\]+$gdb_prompt $" { - fail "unwindonsignal, dummy frame removed" + fail $gdb_test_name } -re "\[\r\n\]+$gdb_prompt $" { - pass "unwindonsignal, dummy frame removed" + pass $gdb_test_name } } diff --git a/gdb/testsuite/gdb.compile/compile-cplus.exp b/gdb/testsuite/gdb.compile/compile-cplus.exp index 48fb75c3d78..c938c60ee98 100644 --- a/gdb/testsuite/gdb.compile/compile-cplus.exp +++ b/gdb/testsuite/gdb.compile/compile-cplus.exp @@ -131,13 +131,13 @@ gdb_test "return" "\r\n#0 main .*" "return" \ "Make _gdb_expr\\(__gdb_regs\\*\\) return now\\? \\(y or n\\) " "y" gdb_test "info sym $infcall_pc" "\r\nNo symbol matches .*" "info sym not found" -gdb_test_no_output "set unwindonsignal on" +gdb_test_no_output "set unwind-on-signal on" gdb_test "compile code *(volatile int *) 0 = 0;" \ [multi_line \ "The program being debugged received signal SIGSEGV, Segmentation fault" \ "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 unwindonsignal off\"\\. Evaluation of the expression containing" \ + "\"set unwind-on-signal off\"\\. Evaluation of the expression containing" \ "the function \\(_gdb_expr\\(__gdb_regs\\*\\)\\) will be abandoned\\."] \ "compile code segfault second" @@ -313,7 +313,7 @@ gdb_test "compile code static const int readonly = 1; *(int *) &readonly = 2;" \ "The program being debugged received signal SIGSEGV, Segmentation fault" \ "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 unwindonsignal off\"\\. Evaluation of the expression containing" \ + "\"set unwind-on-signal off\"\\. Evaluation of the expression containing" \ "the function \\(_gdb_expr\\(__gdb_regs\\*\\)\\) will be abandoned\\."] gdb_test_no_output "set debug compile off" diff --git a/gdb/testsuite/gdb.compile/compile.exp b/gdb/testsuite/gdb.compile/compile.exp index 86521e99dac..cd409f5a48a 100644 --- a/gdb/testsuite/gdb.compile/compile.exp +++ b/gdb/testsuite/gdb.compile/compile.exp @@ -157,13 +157,13 @@ gdb_test "return" "\r\n#0 main .*" "return" \ "Make _gdb_expr return now\\? \\(y or n\\) " "y" gdb_test "info sym $infcall_pc" "\r\nNo symbol matches .*" "info sym not found" -gdb_test_no_output "set unwindonsignal on" +gdb_test_no_output "set unwind-on-signal on" gdb_test "compile code *(volatile int *) 0 = 0;" \ [multi_line \ "The program being debugged received signal SIGSEGV, Segmentation fault" \ "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 unwindonsignal off\"\\. Evaluation of the expression containing" \ + "\"set unwind-on-signal off\"\\. Evaluation of the expression containing" \ "the function \\(_gdb_expr\\) will be abandoned\\."] \ "compile code segfault second" @@ -321,7 +321,7 @@ gdb_test "compile code static const int readonly = 1; *(int *) &readonly = 2;" \ "The program being debugged received signal SIGSEGV, Segmentation fault" \ "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 unwindonsignal off\"\\. Evaluation of the expression containing" \ + "\"set unwind-on-signal off\"\\. Evaluation of the expression containing" \ "the function \\(_gdb_expr\\) will be abandoned\\."] gdb_test_no_output "set debug compile off" diff --git a/gdb/testsuite/gdb.cp/gdb2495.exp b/gdb/testsuite/gdb.cp/gdb2495.exp index 93b046ad3a6..b888924ec63 100644 --- a/gdb/testsuite/gdb.cp/gdb2495.exp +++ b/gdb/testsuite/gdb.cp/gdb2495.exp @@ -98,10 +98,10 @@ if {![runto_main]} { # behaviour; it should not. Test both on and off states. # Turn on unwind on signal behaviour. -gdb_test_no_output "set unwindonsignal on" +gdb_test_no_output "set unwind-on-signal on" # Check that it is turned on. -gdb_test "show unwindonsignal" \ +gdb_test "show unwind-on-signal" \ "signal is received while in a call dummy is on.*" \ "turn on unwind on signal" @@ -112,20 +112,20 @@ gdb_test "p exceptions.raise_signal(1)" \ "The program being debugged received signal SIGABRT, Aborted" \ "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 unwindonsignal off\"\\. Evaluation of the expression containing" \ + "\"set unwind-on-signal off\"\\. Evaluation of the expression containing" \ "the function \\(SimpleException::raise_signal\\(int\\)\\) will be abandoned\\."]\ - "check for unwindonsignal off message" + "check for unwind-on-signal off message" # And reverse - turn off again. -gdb_test_no_output "set unwindonsignal off" +gdb_test_no_output "set unwind-on-signal off" # Check that it is actually turned off. -gdb_test "show unwindonsignal" \ +gdb_test "show unwind-on-signal" \ "signal is received while in a call dummy is off.*" \ "turn off unwind on signal" # Check to see if new behaviour interferes with # normal signal handling in inferior function calls. gdb_test "p exceptions.raise_signal(1)" \ - "To change this behavior use \"set unwindonsignal on\".*" \ - "check for unwindonsignal on message" + "To change this behavior use \"set unwind-on-signal on\".*" \ + "check for unwind-on-signal on message" diff --git a/gdb/testsuite/gdb.fortran/function-calls.exp b/gdb/testsuite/gdb.fortran/function-calls.exp index 646fbb06435..27dd68d8cd9 100644 --- a/gdb/testsuite/gdb.fortran/function-calls.exp +++ b/gdb/testsuite/gdb.fortran/function-calls.exp @@ -44,7 +44,7 @@ if {![runto [gdb_get_line_number "post_init"]]} { } # Use inspired by gdb.base/callfuncs.exp. -gdb_test_no_output "set unwindonsignal on" +gdb_test_no_output "set unwind-on-signal on" # Baseline: function and subroutine call with no arguments. gdb_test "p no_arg()" " = .TRUE." diff --git a/gdb/testsuite/gdb.mi/mi-condbreak-fail.exp b/gdb/testsuite/gdb.mi/mi-condbreak-fail.exp index 3ccca4c2e9b..d91bef420af 100644 --- a/gdb/testsuite/gdb.mi/mi-condbreak-fail.exp +++ b/gdb/testsuite/gdb.mi/mi-condbreak-fail.exp @@ -43,7 +43,7 @@ proc run_test { unwind_on_signal } { return } - mi_gdb_test "-gdb-set unwindonsignal ${unwind_on_signal}" {\^done} \ + mi_gdb_test "-gdb-set unwind-on-signal ${unwind_on_signal}" {\^done} \ "set unwind-on-signal" # Create the conditional breakpoint. @@ -70,7 +70,7 @@ proc run_test { unwind_on_signal } { "&\"The program being debugged received signal SIGSEGV, Segmentation fault\\\\n\"" \ "&\"while in a function called from GDB\\. GDB has restored the context\\\\n\"" \ "&\"to what it was before the call\\. To change this behavior use\\\\n\"" \ - "&\"\\\\\"set unwindonsignal off\\\\\"\\. Evaluation of the expression containing\\\\n\"" \ + "&\"\\\\\"set unwind-on-signal off\\\\\"\\. Evaluation of the expression containing\\\\n\"" \ "&\"the function \\(cond_fail\\) will be abandoned\\.\\\\n\"" \ "=breakpoint-modified,bkpt={number=\"$bpnum\",type=\"breakpoint\",\[^\r\n\]+times=\"1\",\[^\r\n\]+}" \ "~\"\\\\n\"" \ @@ -92,7 +92,7 @@ proc run_test { unwind_on_signal } { "&\"Error in testing condition for breakpoint $bpnum:\\\\n\"" \ "&\"The program being debugged was signaled while in a function called from GDB\\.\\\\n\"" \ "&\"GDB remains in the frame where the signal was received\\.\\\\n\"" \ - "&\"To change this behavior use \\\\\"set unwindonsignal on\\\\\"\\.\\\\n\"" \ + "&\"To change this behavior use \\\\\"set unwind-on-signal on\\\\\"\\.\\\\n\"" \ "&\"Evaluation of the expression containing the function\\\\n\"" \ "&\"\\(cond_fail\\) will be abandoned\\.\\\\n\"" \ "&\"When the function is done executing, GDB will silently stop\\.\\\\n\"" \ diff --git a/gdb/testsuite/gdb.mi/mi-condbreak-throw.exp b/gdb/testsuite/gdb.mi/mi-condbreak-throw.exp index 7f291244e53..437825eea47 100644 --- a/gdb/testsuite/gdb.mi/mi-condbreak-throw.exp +++ b/gdb/testsuite/gdb.mi/mi-condbreak-throw.exp @@ -103,7 +103,7 @@ proc run_test { unwind_on_exception } { "&\"Error in testing condition for breakpoint $bpnum:\\\\n\"" \ "&\"The program being debugged was signaled while in a function called from GDB\\.\\\\n\"" \ "&\"GDB remains in the frame where the signal was received\\.\\\\n\"" \ - "&\"To change this behavior use \\\\\"set unwindonsignal on\\\\\"\\.\\\\n\"" \ + "&\"To change this behavior use \\\\\"set unwind-on-signal on\\\\\"\\.\\\\n\"" \ "&\"Evaluation of the expression containing the function\\\\n\"" \ "&\"\\(cond_throw\\(\\)\\) will be abandoned\\.\\\\n\"" \ "&\"When the function is done executing, GDB will silently stop\\.\\\\n\"" \ diff --git a/gdb/testsuite/gdb.mi/mi-syn-frame.exp b/gdb/testsuite/gdb.mi/mi-syn-frame.exp index 40df312a1cc..1324226412c 100644 --- a/gdb/testsuite/gdb.mi/mi-syn-frame.exp +++ b/gdb/testsuite/gdb.mi/mi-syn-frame.exp @@ -94,7 +94,7 @@ mi_gdb_test "409-stack-list-frames 0 0" \ # mi_gdb_test "410-data-evaluate-expression bar()" \ - ".*410\\^error,msg=\"The program being debugged was signaled while in a function called from GDB.\\\\nGDB remains in the frame where the signal was received.\\\\nTo change this behavior use \\\\\"set unwindonsignal on\\\\\".\\\\nEvaluation of the expression containing the function\\\\n\\(bar\\) will be abandoned.\\\\nWhen the function is done executing, GDB will silently stop.\"" \ + ".*410\\^error,msg=\"The program being debugged was signaled while in a function called from GDB.\\\\nGDB remains in the frame where the signal was received.\\\\nTo change this behavior use \\\\\"set unwind-on-signal on\\\\\".\\\\nEvaluation of the expression containing the function\\\\n\\(bar\\) will be abandoned.\\\\nWhen the function is done executing, GDB will silently stop.\"" \ "call inferior function which raises exception" mi_gdb_test "411-stack-list-frames" "411\\^done,stack=\\\[frame=\{level=\"0\",addr=\"$hex\",func=\"bar\",file=\".*mi-syn-frame.c\",fullname=\"${fullname_syntax}${srcfile}\",line=\"$decimal\",arch=\"$any\"},frame=\{level=\"1\",addr=\"$hex\",func=\"\"\},frame=\{level=\"2\",addr=\"$hex\",func=\"main\",file=\".*mi-syn-frame.c\",fullname=\"${fullname_syntax}${srcfile}\",line=\"$decimal\",arch=\"$any\"}.*\\\]" "backtrace from inferior function at exception" diff --git a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.exp b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.exp index c46f8cdb981..26ce59d6335 100644 --- a/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.exp +++ b/gdb/testsuite/gdb.threads/infcall-from-bp-cond-simple.exp @@ -173,7 +173,7 @@ proc_with_prefix run_bp_cond_segfaults { target_async target_non_stop } { "Error in testing condition for breakpoint ${bp_1_num}:" \ "The program being debugged was signaled while in a function called from GDB\\." \ "GDB remains in the frame where the signal was received\\." \ - "To change this behavior use \"set unwindonsignal on\"\\." \ + "To change this behavior use \"set unwind-on-signal on\"\\." \ "Evaluation of the expression containing the function" \ "\\(function_that_segfaults\\) will be abandoned\\." \ "When the function is done executing, GDB will silently stop\\."] diff --git a/gdb/testsuite/gdb.threads/thread-unwindonsignal.exp b/gdb/testsuite/gdb.threads/thread-unwindonsignal.exp index 68a99f66f1c..a9cedc0c466 100644 --- a/gdb/testsuite/gdb.threads/thread-unwindonsignal.exp +++ b/gdb/testsuite/gdb.threads/thread-unwindonsignal.exp @@ -13,7 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -# Test use of unwindonsignal when a hand function call that gets interrupted +# Test use of unwind-on-signal when a hand function call that gets interrupted # by a signal in another thread. set NR_THREADS 4 @@ -49,12 +49,12 @@ gdb_test "continue" \ # We want the main thread (hand_call_with_signal) and # thread 1 (sigabrt_handler) to both run. -# Do turn on unwindonsignal. +# Do turn on unwind-on-signal. # We want to test gdb handling of the current thread changing when # unwindonsignal is in effect. -gdb_test_no_output "set unwindonsignal on" \ +gdb_test_no_output "set unwind-on-signal on" \ "setting unwindonsignal" -gdb_test "show unwindonsignal" \ +gdb_test "show unwind-on-signal" \ "Unwinding of stack .* is on." \ "showing unwindonsignal"