From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id A66B0394201D for ; Mon, 25 Jan 2021 04:57:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A66B0394201D X-ASG-Debug-ID: 1611550651-0c856e6cd577fe00001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id 7AoewOpH9IFtOleX (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 24 Jan 2021 23:57:31 -0500 (EST) X-Barracuda-Envelope-From: simon.marchi@polymtl.ca X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from simark.localdomain (192-222-157-6.qc.cable.ebox.net [192.222.157.6]) by smtp.ebox.ca (Postfix) with ESMTP id 747BB441D64; Sun, 24 Jan 2021 23:57:31 -0500 (EST) From: Simon Marchi X-Barracuda-RBL-IP: 192.222.157.6 X-Barracuda-Effective-Source-IP: 192-222-157-6.qc.cable.ebox.net[192.222.157.6] X-Barracuda-Apparent-Source-IP: 192.222.157.6 To: gdb-patches@sourceware.org Subject: [PATCH v4 1/3] gdb/testsuite: add test for run/attach while program is running Date: Sun, 24 Jan 2021 23:57:28 -0500 X-ASG-Orig-Subj: [PATCH v4 1/3] gdb/testsuite: add test for run/attach while program is running Message-Id: <20210125045730.1739754-2-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210125045730.1739754-1-simon.marchi@polymtl.ca> References: <20210125045730.1739754-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1611550651 X-Barracuda-Encrypted: DHE-RSA-AES256-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: 7657 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.50 X-Barracuda-Spam-Status: No, SCORE=0.50 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests=BSF_RULE7568M X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.87494 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_RULE7568M Custom Rule 7568M X-Spam-Status: No, score=-21.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_QUARANTINE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Mon, 25 Jan 2021 04:57:34 -0000 A previous version of this patch series broke the use case of doing "run" or "attach" while the program is running, but it wasn't caught by the testsuite, which means it's not covered. Add a test for that. gdb/testsuite/ChangeLog: * gdb.base/run-attach-while-running.exp: New. * gdb.base/run-attach-while-running.c: New. Change-Id: I77f098ec0b28dc2d4575ea80e941f6a75273e431 --- .../gdb.base/run-attach-while-running.c | 69 +++++++++ .../gdb.base/run-attach-while-running.exp | 131 ++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 gdb/testsuite/gdb.base/run-attach-while-running.c create mode 100644 gdb/testsuite/gdb.base/run-attach-while-running.exp diff --git a/gdb/testsuite/gdb.base/run-attach-while-running.c b/gdb/testsuite/gdb.base/run-attach-while-running.c new file mode 100644 index 000000000000..57bebbe6456e --- /dev/null +++ b/gdb/testsuite/gdb.base/run-attach-while-running.c @@ -0,0 +1,69 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2021 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 + +#ifndef WITH_THREADS +# error "WITH_THREADS must be defined." +#endif + +#if WITH_THREADS +# include + +static pthread_barrier_t barrier; + +static void * +thread_func (void *p) +{ + pthread_barrier_wait (&barrier); + + for (int i = 0; i < 30; i++) + sleep (1); + + return NULL; +} + +#endif /* WITH_THREADS */ + +static void +all_started (void) +{} + +int +main (void) +{ + alarm (30); + +#if WITH_THREADS + int ret = pthread_barrier_init (&barrier, NULL, 2); + assert (ret == 0); + + pthread_t thread; + ret = pthread_create (&thread, NULL, thread_func, NULL); + assert (ret == 0); + + pthread_barrier_wait (&barrier); +#endif /* WITH_THREADS */ + + all_started (); + + for (int i = 0; i < 30; i++) + sleep (1); + + return 0; +} diff --git a/gdb/testsuite/gdb.base/run-attach-while-running.exp b/gdb/testsuite/gdb.base/run-attach-while-running.exp new file mode 100644 index 000000000000..385bbc410dbf --- /dev/null +++ b/gdb/testsuite/gdb.base/run-attach-while-running.exp @@ -0,0 +1,131 @@ +# Copyright 2021 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 doing a "run" or an "attach" while the program is running. +# +# We test a non-threaded and a threaded configuration, so that targets that +# don't support threads get some testing, but we also test with threads when +# possible in case that triggers some multi-thread-specific bugs. + +standard_testfile + +set binfile_threads ${binfile}-threads +set binfile_nothreads ${binfile}-nothreads +unset binfile + +# Valid parameter / axis values: +# +# - non-stop: "off" of "on" +# - threaded: 0 or 1 +# - run-or-attach: "run" or "attach" + +proc_with_prefix test { non-stop threaded run-or-attach } { + if { ${run-or-attach} == "attach" && ![can_spawn_for_attach] } { + unsupported "attach not supported" + return + } + + # Choose the right (threaded or not) binfile. + if { $threaded } { + set binfile $::binfile_threads + } else { + set binfile $::binfile_nothreads + } + + save_vars ::GDBFLAGS { + set ::GDBFLAGS "$::GDBFLAGS -ex \"set non-stop ${non-stop}\"" + + # The test doesn't work when the remote target uses the synchronous + # remote protocol, because GDB can't kill the remote inferior while it + # is running, when we "run" or "attach" again. When aswering "yes" to + # the "Start it from the beginning?" question, we otherwise get: + # + # Cannot execute this command while the target is running. Use the + # "interrupt" command to stop the target and then try again. + # + # Interrupting the target would defeat the purpose of the test. So + # when non-stop is off and using the remote target, force the target + # to use the async / non-stop version of the protocol. + if { [target_info exists gdb_protocol] && ${non-stop} == "off" } { + set ::GDBFLAGS "$::GDBFLAGS -ex \"maint set target-non-stop on\"" + } + + clean_restart $binfile + } + + if { ![runto_main] } { + untested "could not run to main" + return + } + + gdb_breakpoint "all_started" "temporary" + gdb_continue_to_breakpoint "continue to all_started" + + # If all-stop, everything stopped when we hit the all_started breakpoint, + # so resume execution in background. If running the non-threaded version, + # our only thread is stopped in any case, so resume as well. But if we are + # in non-stop with two threads, we have one running and one stopped, leave + # it like this, it makes an interesting test case. + if { ${non-stop} == "off" || !${threaded} } { + gdb_test "continue &" "Continuing." + } + + gdb_test_no_output "set confirm off" + + # Run again (or, connect to a new stub if using a stub), take advantage + # of the fact that runto_main leaves the breakpoint on main in place. + if { ${run-or-attach} == "run" } { + gdb_run_cmd + gdb_test "" "Breakpoint $::decimal, .*main.*" "hit main breakpoint after re-run" + } elseif { ${run-or-attach} == "attach" } { + set test_spawn_id [spawn_wait_for_attach $binfile] + set test_pid [spawn_id_get_pid $test_spawn_id] + + gdb_test "attach $test_pid" "Attaching to program: .*" "attach to process" + + gdb_exit + kill_wait_spawned_process $test_spawn_id + } else { + error "Invalid value for run-or-attach" + } +} + +# Build and test with the non-threaded version. +if { [build_executable "failed to prepare" ${binfile_nothreads} ${srcfile} \ + {debug additional_flags=-DWITH_THREADS=0} ] } { + return +} + +with_test_prefix {threaded=0} { + foreach_with_prefix run-or-attach {run attach} { + foreach_with_prefix non-stop {off on} { + test ${non-stop} 0 ${run-or-attach} + } + } +} + +# Build and test with the threaded version. +if { [build_executable "failed to prepare" ${binfile_threads} ${srcfile} \ + {debug pthreads additional_flags=-DWITH_THREADS=1} ] } { + return +} + +with_test_prefix {threaded=1} { + foreach_with_prefix run-or-attach {run attach} { + foreach_with_prefix non-stop {off on} { + test ${non-stop} 1 ${run-or-attach} + } + } +} -- 2.30.0