From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id iOw2LHTcvWD1GwAAWB0awg (envelope-from ) for ; Mon, 07 Jun 2021 04:44:36 -0400 Received: by simark.ca (Postfix, from userid 112) id B1A0E1F163; Mon, 7 Jun 2021 04:44:36 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 926AF1E813 for ; Mon, 7 Jun 2021 04:44:35 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 554FB3839C5B for ; Mon, 7 Jun 2021 08:44:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 554FB3839C5B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1623055475; bh=2zUvNRIjRUWV55CEREPkypQQoMxe8dG97swodLKX0yg=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=O80812YKnYWJEwNfYtkMlRcSpLcuRkOQ+9TsgAR5coC8tTPNwif4myuvZlkuRZnHK bfleX4kdORY28KTF+r9MAXVbiBYzivnpWxT+j03YSQHiF/YhOM/OCAPWqwDw2ChknK 6sNeZb149y6q1srWqFsVhHGsjsWLnn/TpK01p1Vs= Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by sourceware.org (Postfix) with ESMTPS id 773B7383B41B for ; Mon, 7 Jun 2021 08:43:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 773B7383B41B IronPort-SDR: tGPzmBYEXa6VnlvAiXErhSjDZJ8wqlFVEbLZDVE8fmMcqW2le2PSRlbCoV2DXGBHFaq7J6+7f1 +PGZ+0zAKYmw== X-IronPort-AV: E=McAfee;i="6200,9189,10007"; a="265739432" X-IronPort-AV: E=Sophos;i="5.83,254,1616482800"; d="scan'208";a="265739432" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Jun 2021 01:43:04 -0700 IronPort-SDR: LO2PbM2IBs5trjP1dK/QiBHNkVzRYOwoKFDMhgcqlRMbPSUp9HwJtG4sq5MWA2S5Q+1PIVo8vl XWsJ8Np+Gt0g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,254,1616482800"; d="scan'208";a="484694847" Received: from irvmail001.ir.intel.com ([10.43.11.63]) by fmsmga002.fm.intel.com with ESMTP; 07 Jun 2021 01:43:02 -0700 Received: from ulvlx001.iul.intel.com (ulvlx001.iul.intel.com [172.28.207.17]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id 1578h1PS027308; Mon, 7 Jun 2021 09:43:01 +0100 Received: from ulvlx001.iul.intel.com (localhost [127.0.0.1]) by ulvlx001.iul.intel.com with ESMTP id 1578h1Qe003627; Mon, 7 Jun 2021 10:43:01 +0200 Received: (from apaunovi@localhost) by ulvlx001.iul.intel.com with LOCAL id 1578h1Fo003623; Mon, 7 Jun 2021 10:43:01 +0200 To: gdb-patches@sourceware.org Subject: [PATCH 1/2] gdb: Fix deleted thread when issuing next command Date: Mon, 7 Jun 2021 10:42:02 +0200 Message-Id: <1623055323-3331-2-git-send-email-aleksandar.paunovic@intel.com> X-Mailer: git-send-email 1.7.0.7 In-Reply-To: <1623055323-3331-1-git-send-email-aleksandar.paunovic@intel.com> References: <1623055323-3331-1-git-send-email-aleksandar.paunovic@intel.com> 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: , From: "aleksandar.paunovic via Gdb-patches" Reply-To: "aleksandar.paunovic" Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" From: Aleksandar Paunovic When issuing "next" command the thread got deleted even though it was still alive and running. This happened because the thread was examined under a wrong inferior. The fixed scenario: ~~~ $ gdb -q breakpoint-running-inferior-1-exe (gdb) set schedule-multiple off (gdb) break breakpoint-running-inferior-1.c:26 (gdb) run (gdb) add-inferior -no-connection (gdb) inferior 2 (gdb) spawn gdbserver :2346 breakpoint-running-inferior-2-exe (gdb) target remote :2346 (gdb) break breakpoint-running-inferior-2.c:26 (gdb) continue (gdb) thread 1.1 (gdb) clear breakpoint-running-inferior-2.c:26 (gdb) set schedule-multiple on (gdb) next (gdb) thread 1.1 ~~~ Before: ~~~ Thread ID 1.1 has terminated. ~~~ Now: ~~~ Switching to thread 1.1 ~~~ gdb/ChangeLog: 2021-04-30 Aleksandar Paunovic * infrun.c (keep_going_stepped_thread): Switch to correct inferior and check if thread is executing. gdb/testsuite/ChangeLog: 2021-04-30 Aleksandar Paunovic * gdb.base/breakpoint-running-inferior-1.c: New file. * gdb.base/breakpoint-running-inferior-2.c: New file. * gdb.base/breakpoint-running-inferior.exp: New file. 2021-06-07 Aleksandar Paunovic --- gdb/infrun.c | 2 + .../gdb.base/breakpoint-running-inferior-1.c | 39 +++++++++ .../gdb.base/breakpoint-running-inferior-2.c | 39 +++++++++ .../gdb.base/breakpoint-running-inferior.exp | 84 +++++++++++++++++++ 4 files changed, 164 insertions(+) create mode 100644 gdb/testsuite/gdb.base/breakpoint-running-inferior-1.c create mode 100644 gdb/testsuite/gdb.base/breakpoint-running-inferior-2.c create mode 100644 gdb/testsuite/gdb.base/breakpoint-running-inferior.exp diff --git a/gdb/infrun.c b/gdb/infrun.c index 488bcc1e10..78da1f0e72 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -7556,6 +7556,8 @@ keep_going_stepped_thread (struct thread_info *tp) stepping thread is still alive. For that reason, we need to synchronously query the target now. */ + /* Make sure that we are within a correct inferior. */ + switch_to_inferior_no_thread (tp->inf); if (tp->state == THREAD_EXITED || !target_thread_alive (tp->ptid)) { infrun_debug_printf ("not resuming previously stepped thread, it has " diff --git a/gdb/testsuite/gdb.base/breakpoint-running-inferior-1.c b/gdb/testsuite/gdb.base/breakpoint-running-inferior-1.c new file mode 100644 index 0000000000..475b2c4126 --- /dev/null +++ b/gdb/testsuite/gdb.base/breakpoint-running-inferior-1.c @@ -0,0 +1,39 @@ +/* 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 + +void * +forever () +{ + /* Wait for alarm. */ + while (1) + sleep (1); /* break here */ +} + +int +main () +{ + alarm (30); + + pthread_t forever_thread; + pthread_create (&forever_thread, NULL, *forever, NULL); + pthread_join (forever_thread, NULL); + + return 0; +} diff --git a/gdb/testsuite/gdb.base/breakpoint-running-inferior-2.c b/gdb/testsuite/gdb.base/breakpoint-running-inferior-2.c new file mode 100644 index 0000000000..475b2c4126 --- /dev/null +++ b/gdb/testsuite/gdb.base/breakpoint-running-inferior-2.c @@ -0,0 +1,39 @@ +/* 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 + +void * +forever () +{ + /* Wait for alarm. */ + while (1) + sleep (1); /* break here */ +} + +int +main () +{ + alarm (30); + + pthread_t forever_thread; + pthread_create (&forever_thread, NULL, *forever, NULL); + pthread_join (forever_thread, NULL); + + return 0; +} diff --git a/gdb/testsuite/gdb.base/breakpoint-running-inferior.exp b/gdb/testsuite/gdb.base/breakpoint-running-inferior.exp new file mode 100644 index 0000000000..bb0406bc65 --- /dev/null +++ b/gdb/testsuite/gdb.base/breakpoint-running-inferior.exp @@ -0,0 +1,84 @@ +# 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 . + +# Create two inferiors with different targets. The first runs on top of +# a native target while the second on a remote target. Both inferiors +# use the copy of the same source code. The copy is done in order to make +# sure that a breakpoint is only in inferior 2. While in inferior 1, do +# a "next" which should break in a thread in inferior 2. +# Both executables will run total of 4 threads (2 per executable) and +# we will put a breakpoint only in the second executable to achieve this. + +load_lib gdbserver-support.exp + +standard_testfile breakpoint-running-inferior-1.c breakpoint-running-inferior-2.c +set binfile2 ${binfile}2 + +if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } { + untested "failed to compile first testcase" + return -1 +} + +if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } { + untested "failed to compile second testcase" + return -1 +} + +clean_restart ${binfile} + +gdb_test_no_output "set schedule-multiple off" + +if {![runto_main]} { + untested "failed to run to main" + return -1 +} +set break_pos [gdb_get_line_number "break here" ${srcfile}] +gdb_breakpoint "$srcfile:$break_pos" +gdb_continue_to_breakpoint "continue to breakpoint in inf 1" + +gdb_test "add-inferior -no-connection" "Added inferior 2.*" "add empty inferior 2" +gdb_test "inferior 2" "Switching to inferior 2.*" "switch to inferior 2" + +# Start gdbserver and connect. +set res [gdbserver_start "" $binfile2] +set gdbserver_protocol [lindex $res 0] +set gdbserver_gdbport [lindex $res 1] +set res [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] +if ![gdb_assert {$res == 0} "connect"] { + return -1 +} + +set break_pos [gdb_get_line_number "break here" ${srcfile2}] +gdb_breakpoint "$srcfile2:$break_pos" + +gdb_continue_to_breakpoint "continue to breakpoint in inf 2" + +gdb_test "thread 1.1" "Switching to thread 1.1.*" "first switch to thread 1.1" + +# Remove the breakpoint from inf 1. Now only a breakpoint in inf 2 remains. +gdb_test "clear $srcfile:$break_pos" "Deleted breakpoint 2 " "remove inf 1 breakpoint" +gdb_test_no_output "set schedule-multiple on" +gdb_test "next" ".*Thread 2.*hit Breakpoint.*" "next while in inferior 1" + +# We should be able to normally switch to thread 1.1. +# In case of a bad GDB flow the GDB was losing the thread. +gdb_test_multiple "thread 1.1" "Switching to thread 1.1" { + -re "\\\[Switching to thread 1.1 \\(Thread .*\\)\\\]" { + pass $gdb_test_name + } + -re ".*Thread ID 1.1 has terminated.*" { + fail $gdb_test_name + } +} -- 2.17.1