From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f45.google.com (mail-wr1-f45.google.com [209.85.221.45]) by sourceware.org (Postfix) with ESMTPS id CAE0B398BC1A for ; Thu, 17 Sep 2020 18:00:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CAE0B398BC1A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=alves.ped@gmail.com Received: by mail-wr1-f45.google.com with SMTP id z4so3024271wrr.4 for ; Thu, 17 Sep 2020 11:00:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=PgXiwHfnFlFka1wpbmek1YSY++/kmBJLZGOwas2qWys=; b=qqmYfBsAqO9S9jhkPtcrs0jSP9NLLfap2y+dupJmvonByTNoAdxwp6AuiEiqoSGcCB 4Eqd8Osu49JD44GBrzEITXUid8w+4LwqUUnzP3OJfGRHkyT1jUgiQBovOkoiKhYDYVfw cbPHULtHmlTo9IZ6AS/9K/tTQQazaOwnkiDOtQVeLUnBlu7y8wrLeJOZLsNsZlrhP+qZ lr8gmBPuve120j+/fkEWeqhZC8I5DdX+yqTlXb9hd40mwAfWbQ0S8YGk4okDwgYFMC+V /5OpVaexoqO5ISxXJJ0gG/spY+6nTZheYmJzFB9zeHE2x1/1r9NPh+aYfHdpbQG2eFEy P6aw== X-Gm-Message-State: AOAM531j+RY58fOUlxNKtQoxHUC0OmW6FWhqgg3eVWndFjvlBovovWqc V26iIxsoMwpQT2m7LFN4/YlheLoXqF9Itw== X-Google-Smtp-Source: ABdhPJxYlUpV6gnElfg/Btk4H99yku10rxj9M1NB5+doAW6Z1tR3dqvGcWRFOVUGKxFH2feYvrJRiQ== X-Received: by 2002:adf:dd51:: with SMTP id u17mr33360960wrm.355.1600365653855; Thu, 17 Sep 2020 11:00:53 -0700 (PDT) Received: from localhost ([2001:8a0:f905:5600:eefd:c63:53e0:3e8a]) by smtp.gmail.com with ESMTPSA id w81sm314919wmg.47.2020.09.17.11.00.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 17 Sep 2020 11:00:52 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 2/2] Fix "thread find" with multiple inferiors/targets (PR gdb/26631) Date: Thu, 17 Sep 2020 19:00:38 +0100 Message-Id: <20200917180038.29226-3-pedro@palves.net> X-Mailer: git-send-email 2.14.5 In-Reply-To: <20200917180038.29226-1-pedro@palves.net> References: <20200917180038.29226-1-pedro@palves.net> X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, 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: Thu, 17 Sep 2020 18:00:57 -0000 "thread find" with multiple inferiors got broken with the multi-target work: Thread 1 "gdb" hit Breakpoint 1, internal_error (...) at ../../src/gdbsupport/errors.cc:51 51 { (top-gdb) bt #0 internal_error (file=0xffffd4d0 , line=0, fmt=0x555556330320 "en_US.UTF-8") at ../../src/gdbsupport/errors.cc:51 #1 0x0000555555bca4c7 in target_thread_name (info=0x555556801290) at ../../src/gdb/target.c:2035 #2 0x0000555555beb07a in thread_find_command (arg=0x7fffffffe08e "1", from_tty=0) at ../../src/gdb/thread.c:1959 #3 0x000055555572ec49 in do_const_cfunc (c=0x555556786bc0, args=0x7fffffffe08e "1", from_tty=0) at ../../src/gdb/cli/cli-decode.c:95 #4 0x0000555555732abd in cmd_func (cmd=0x555556786bc0, args=0x7fffffffe08e "1", from_tty=0) at ../../src/gdb/cli/cli-decode.c:2181 #5 0x0000555555bf1245 in execute_command (p=0x7fffffffe08e "1", from_tty=0) at ../../src/gdb/top.c:664 #6 0x00005555559cad10 in catch_command_errors (command=0x555555bf0c31 , arg=0x7fffffffe082 "thread find 1", from_tty=0) at ../../src/gdb/main.c:457 #7 0x00005555559cc33d in captured_main_1 (context=0x7fffffffdb60) at ../../src/gdb/main.c:1218 #8 0x00005555559cc571 in captured_main (data=0x7fffffffdb60) at ../../src/gdb/main.c:1243 #9 0x00005555559cc5e8 in gdb_main (args=0x7fffffffdb60) at ../../src/gdb/main.c:1268 #10 0x0000555555623816 in main (argc=17, argv=0x7fffffffdc78) at ../../src/gdb/gdb.c:32 The problem is that we're not switching to the inferior/target before calling target methods, which trips on an assertion put in place exactly to catch this sort of problem. gdb/testsuite/ChangeLog: * gdb.multi/mtarg-thread-find.exp: New file. gdb/ChangeLog: * thread.c (thread_find_command): Switch inferior before calling target methods. --- gdb/testsuite/gdb.multi/mtarg-thread-find.exp | 106 ++++++++++++++++++++++++++ gdb/thread.c | 6 ++ 2 files changed, 112 insertions(+) create mode 100644 gdb/testsuite/gdb.multi/mtarg-thread-find.exp diff --git a/gdb/testsuite/gdb.multi/mtarg-thread-find.exp b/gdb/testsuite/gdb.multi/mtarg-thread-find.exp new file mode 100644 index 00000000000..faca16e7aef --- /dev/null +++ b/gdb/testsuite/gdb.multi/mtarg-thread-find.exp @@ -0,0 +1,106 @@ +# Copyright 2020 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 "thread find" with multiple inferiors/targets. Regression test +# for PR gdb/26631. + +source $srcdir/$subdir/multi-target.exp.tcl + +if {![mtarg_prepare]} { + return +} + +proc test_thread_find {} { + global decimal gdb_prompt + + set NUM_INFS 6 + + if {![setup "off"]} { + untested "setup failed" + return + } + + # This makes sure we don't crash. See PR gdb/26631. + gdb_test "thread find xxxyyyzzz" \ + "No threads match 'xxxyyyzzz'" + + # Create thread names. + for {set i 1} {$i <= $NUM_INFS} {incr i} { + gdb_test "thread apply $i.1 thread name threadname_$i" \ + "Thread $i.1 .*" \ + "name thread $i" + } + + # Collect target ids. + + for {set i 1} {$i <= $NUM_INFS} {incr i} { + set target_id($i) "" + } + set any "\[^\r\n\]*" + gdb_test_multiple "info threads" "collect thread id" { + -re ". ($decimal).$decimal (Thread ${any}) \"threadname_\[0-9\]+\" $any" { + set thr_num $expect_out(1,string) + set target_id($thr_num) $expect_out(2,string) + exp_continue + } + -re ".*$gdb_prompt $" { + pass "collect target id" + } + } + + # Find the threads by name. Note we repeat the search with each + # inferior selected, so that we're sure that GDB doesn't get + # confused with which target stack to consult. + + with_test_prefix "find by name" { + for {set sel_inf 1} {$sel_inf <= $NUM_INFS} {incr sel_inf} { + with_test_prefix "inf $sel_inf" { + + gdb_test "inferior $sel_inf" \ + "Switching to inferior $sel_inf .*" + + for {set find_inf 1} {$find_inf <= $NUM_INFS} {incr find_inf} { + gdb_test "thread find threadname_$find_inf" \ + "Thread $find_inf.1 has name 'threadname_$find_inf'" \ + "find thread name $find_inf" + } + } + } + } + + # Find the threads by target id. Likewise we repeat the search + # with each inferior selected. + + with_test_prefix "find by target id" { + for {set sel_inf 1} {$sel_inf <= $NUM_INFS} {incr sel_inf} { + with_test_prefix "inf $sel_inf" { + + gdb_test "inferior $sel_inf" \ + "Switching to inferior $sel_inf .*" + + for {set find_inf 1} {$find_inf <= $NUM_INFS} {incr find_inf} { + set target_id_re [string_to_regexp $target_id($find_inf)] + gdb_test "thread find $target_id($find_inf)" \ + "Thread $find_inf.1 has target id '$target_id_re'.*" \ + "find thread target id $find_inf" + } + } + } + } +} + +test_thread_find + +mtarg_cleanup diff --git a/gdb/thread.c b/gdb/thread.c index c915407581f..0217f3b54f7 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -1946,9 +1946,15 @@ thread_find_command (const char *arg, int from_tty) if (tmp != 0) error (_("Invalid regexp (%s): %s"), tmp, arg); + /* We're going to be switching threads. */ + scoped_restore_current_thread restore_thread; + update_thread_list (); + for (thread_info *tp : all_threads ()) { + switch_to_inferior_no_thread (tp->inf); + if (tp->name != NULL && re_exec (tp->name)) { printf_filtered (_("Thread %s has name '%s'\n"), -- 2.14.5