From patchwork Tue Nov 8 21:20:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 60236 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 4809E3858426 for ; Tue, 8 Nov 2022 21:20:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4809E3858426 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1667942450; bh=fuYnqs25AVxs9hQpVf0auhLFr3j7Xr/aedWpxetM+Fo=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=BAERZdBECoDkn8rHd6a9PbWtAbjV+O2q5aDo8rhQg4wKnm/waWHYJ0Wn/kBw0iXHj QwUTfAHSBsAERR3zKQXvAwm0TNZbkhiER8nsE7uAgO6119ANdA7ZHGrB/GKpTx0lo3 HuxodZMSh7t/Cy4tjSCy4qdcRzfjfxtvqQHAj4Z4= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id 298913858403 for ; Tue, 8 Nov 2022 21:20:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 298913858403 X-ASG-Debug-ID: 1667942408-0c856e167f3e8c90001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id 4EIOYGsvSP8PImNw (version=TLSv1 cipher=AES128-SHA bits=128 verify=NO); Tue, 08 Nov 2022 16:20:08 -0500 (EST) X-Barracuda-Envelope-From: simon.marchi@efficios.com X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from epycamd.internal.efficios.com (192-222-180-24.qc.cable.ebox.net [192.222.180.24]) by smtp.ebox.ca (Postfix) with ESMTP id D4F02441B21; Tue, 8 Nov 2022 16:20:08 -0500 (EST) X-Barracuda-RBL-IP: 192.222.180.24 X-Barracuda-Effective-Source-IP: 192-222-180-24.qc.cable.ebox.net[192.222.180.24] X-Barracuda-Apparent-Source-IP: 192.222.180.24 To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH v2] gdb: make "start" breakpoint inferior-specific Date: Tue, 8 Nov 2022 16:20:08 -0500 X-ASG-Orig-Subj: [PATCH v2] gdb: make "start" breakpoint inferior-specific Message-Id: <20221108212008.1792090-1-simon.marchi@efficios.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <691c5a58-68ae-5fe9-2f3d-34fb7af69ad0@palves.net> References: <691c5a58-68ae-5fe9-2f3d-34fb7af69ad0@palves.net> MIME-Version: 1.0 X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1667942408 X-Barracuda-Encrypted: AES128-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-Scan-Msg-Size: 11132 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=5.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.102001 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-Spam-Status: No, score=-3498.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Simon Marchi via Gdb-patches From: Simon Marchi Reply-To: Simon Marchi Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" New in v2: - Change the test so it doesn't call the main function I saw this failure on a CI: (gdb) add-inferior [New inferior 2] Added inferior 2 (gdb) PASS: gdb.threads/vfork-multi-inferior.exp: method=non-stop: add-inferior inferior 2 [Switching to inferior 2 [] ()] (gdb) PASS: gdb.threads/vfork-multi-inferior.exp: method=non-stop: inferior 2 kill The program is not being run. (gdb) file /home/jenkins/workspace/binutils-gdb_master_linuxbuild/platform/jammy-amd64/target_board/unix/tmp/tmp.GYATAXR8Ku/gdb/testsuite/outputs/gdb.threads/vfork-multi-inferior/vfork-multi-inferior-sleep Reading symbols from /home/jenkins/workspace/binutils-gdb_master_linuxbuild/platform/jammy-amd64/target_board/unix/tmp/tmp.GYATAXR8Ku/gdb/testsuite/outputs/gdb.threads/vfork-multi-inferior/vfork-multi-inferior-sleep... (gdb) run & Starting program: /home/jenkins/workspace/binutils-gdb_master_linuxbuild/platform/jammy-amd64/target_board/unix/tmp/tmp.GYATAXR8Ku/gdb/testsuite/outputs/gdb.threads/vfork-multi-inferior/vfork-multi-inferior-sleep (gdb) PASS: gdb.threads/vfork-multi-inferior.exp: method=non-stop: run inferior 2 inferior 1 [Switching to inferior 1 [] ()] (gdb) PASS: gdb.threads/vfork-multi-inferior.exp: method=non-stop: inferior 1 kill The program is not being run. (gdb) file /home/jenkins/workspace/binutils-gdb_master_linuxbuild/platform/jammy-amd64/target_board/unix/tmp/tmp.GYATAXR8Ku/gdb/testsuite/outputs/gdb.threads/vfork-multi-inferior/vfork-multi-inferior Reading symbols from /home/jenkins/workspace/binutils-gdb_master_linuxbuild/platform/jammy-amd64/target_board/unix/tmp/tmp.GYATAXR8Ku/gdb/testsuite/outputs/gdb.threads/vfork-multi-inferior/vfork-multi-inferior... (gdb) break should_break_here Breakpoint 1 at 0x11b1: file /home/jenkins/workspace/binutils-gdb_master_linuxbuild/platform/jammy-amd64/target_board/unix/src/binutils-gdb/gdb/testsuite/gdb.threads/vfork-multi-inferior.c, line 25. (gdb) PASS: gdb.threads/vfork-multi-inferior.exp: method=non-stop: break should_break_here [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". start Temporary breakpoint 2 at 0x11c0: -qualified main. (2 locations) Starting program: /home/jenkins/workspace/binutils-gdb_master_linuxbuild/platform/jammy-amd64/target_board/unix/tmp/tmp.GYATAXR8Ku/gdb/testsuite/outputs/gdb.threads/vfork-multi-inferior/vfork-multi-inferior [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Thread 2.1 "vfork-multi-inf" hit Temporary breakpoint 2, main () at /home/jenkins/workspace/binutils-gdb_master_linuxbuild/platform/jammy-amd64/target_board/unix/src/binutils-gdb/gdb/testsuite/gdb.threads/vfork-multi-inferior-sleep.c:23 23 sleep (30); (gdb) FAIL: gdb.threads/vfork-multi-inferior.exp: method=non-stop: start inferior 1 What happens is: 1. We start inferior 2 with "run&", it runs very slowly, takes time to get to main 2. We switch to inferior 1, and run "start" 3. The temporary breakpoint inserted by "start" applies to all inferiors 4. Inferior 2 hits that breakpoint and GDB reports that hit To avoid this, breakpoints inserted by "start" should be inferior-specific. However, we don't have a nice way to make inferior-specific breakpoints yet. It's possible to make pspace-specific breakpoints (for example how the internal_breakpoint constructor does) by creating a symtab_and_line manually. However, inferiors can share program spaces (usually on particular embedded targets), so we could have a situation where two inferiors run the same code in the same program space. In that case, it would just not be possible to insert a breakpoint in one inferior but not the other. A simple solution that should work all the time is to add a condition to the breakpoint inserted by "start", to check the inferior reporting the hit is the expected one. This is what this patch implements. Add a test that does: - start in background inferior 1 that sleeps 3 seconds before reaching its main function (using a sleep in a global C++ object constructor) - start inferior 2, which also sleeps 3 seconds before reaching its m ain function, with the "start" command - validate that we hit the breakpoint in inferior 2 Without the fix, we hit the breakpoint in inferior 1 pretty much all the time. There could be some unfortunate scheduling causing the test not to catch the bug, for instance if the scheduler decides not to schedule inferior 1 for a long time, but it would be really rare. If the bug is re-introduced, the test will catch it much more often than not, so it will be noticed. Change-Id: Ib0148498a476bfa634ed62353c95f163623c686a --- gdb/infcmd.c | 3 +- .../start-inferior-specific-other.cc | 35 +++++++++++ .../gdb.multi/start-inferior-specific.cc | 32 ++++++++++ .../gdb.multi/start-inferior-specific.exp | 61 +++++++++++++++++++ 4 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.multi/start-inferior-specific-other.cc create mode 100644 gdb/testsuite/gdb.multi/start-inferior-specific.cc create mode 100644 gdb/testsuite/gdb.multi/start-inferior-specific.exp diff --git a/gdb/infcmd.c b/gdb/infcmd.c index c03ca103c91..f91ae33eb25 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -423,7 +423,8 @@ run_command_1 (const char *args, int from_tty, enum run_how run_how) /* Insert temporary breakpoint in main function if requested. */ if (run_how == RUN_STOP_AT_MAIN) { - std::string arg = string_printf ("-qualified %s", main_name ()); + std::string arg = string_printf ("-qualified %s if $_inferior == %d", main_name (), + current_inferior ()->num); tbreak_command (arg.c_str (), 0); } diff --git a/gdb/testsuite/gdb.multi/start-inferior-specific-other.cc b/gdb/testsuite/gdb.multi/start-inferior-specific-other.cc new file mode 100644 index 00000000000..e6f55d326bb --- /dev/null +++ b/gdb/testsuite/gdb.multi/start-inferior-specific-other.cc @@ -0,0 +1,35 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2022 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 + +struct global +{ + global () + { + sleep (3); + } +} global; + +int +main (int argc, const char **argv) +{ + /* We don't want this program finishing and causing spurious "inferior + exited" notifications in GDB, so keep sleeping here. */ + sleep (60); + return 0; +} diff --git a/gdb/testsuite/gdb.multi/start-inferior-specific.cc b/gdb/testsuite/gdb.multi/start-inferior-specific.cc new file mode 100644 index 00000000000..432a6aa8ab8 --- /dev/null +++ b/gdb/testsuite/gdb.multi/start-inferior-specific.cc @@ -0,0 +1,32 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2022 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 + +struct global +{ + global () + { + sleep (3); + } +} global; + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.multi/start-inferior-specific.exp b/gdb/testsuite/gdb.multi/start-inferior-specific.exp new file mode 100644 index 00000000000..94d1a956b37 --- /dev/null +++ b/gdb/testsuite/gdb.multi/start-inferior-specific.exp @@ -0,0 +1,61 @@ +# Copyright 2022 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 that "start"ing an inferior does not inadvertently stop in another +# inferior. +# +# To achieve this, we start an inferior (the "other") in background, which +# sleeps for 3 seconds before reaching its main function. We then "start" a +# second inferior, which also sleeps for 3 seconds before reaching its main +# function. A buggy GDB would report a breakpoint hit in "other". + +standard_testfile .cc -other.cc + +if { [use_gdb_stub] } { + return +} + +set srcfile_other ${srcfile2} +set binfile_other ${binfile}-other + +if { [build_executable ${testfile}.exp ${binfile} "${srcfile}" \ + {debug c++}] != 0 } { + return -1 +} + +if { [build_executable ${testfile}.exp ${binfile_other} "${srcfile_other}" \ + {debug c++}] != 0 } { + return -1 +} + +proc do_test {} { + # With remote, to be able to start an inferior while another one is + # running, we need to use the non-stop variant of the protocol. + save_vars { ::GDBFLAGS } { + if { [target_info gdb_protocol] == "extended-remote"} { + append ::GDBFLAGS " -ex \"maintenance set target-non-stop on\"" + } + + clean_restart ${::binfile_other} + } + + gdb_test "run&" "Starting program: .*" "start background inferior" + gdb_test "add-inferior" "Added inferior 2.*" + gdb_test "inferior 2" "Switching to inferior 2.*" + gdb_file_cmd ${::binfile} + gdb_test "start" "Thread 2.1 .* hit Temporary breakpoint .*" +} + +do_test