From patchwork Fri Dec 11 12:25:05 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 9978 Received: (qmail 111287 invoked by alias); 11 Dec 2015 12:25:12 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 111253 invoked by uid 89); 11 Dec 2015 12:25:10 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Fri, 11 Dec 2015 12:25:08 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 3AEA3C803 for ; Fri, 11 Dec 2015 12:25:07 +0000 (UTC) Received: from brno.lan (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id tBBCP6SJ003164 for ; Fri, 11 Dec 2015 07:25:06 -0500 From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH] Fix PR threads/19354: "info threads" error with multiple inferiors Date: Fri, 11 Dec 2015 12:25:05 +0000 Message-Id: <1449836705-19410-1-git-send-email-palves@redhat.com> Note: this applies on top of: [PATCH] Remove support for LinuxThreads and vendor 2.4 kernels w/ backported NPTL https://sourceware.org/ml/gdb-patches/2015-12/msg00214.html We try to avoid using libthread_db.so to list threads in the inferior when debugging live processes, but the code that decides whether to use it decides incorrectly if you have more than one inferior, and the current inferior doesn't have execution yet. The result is visible as: (gdb) add-inferior Added inferior 2 (gdb) inferior 2 [Switching to inferior 2 [] ()] (gdb) info inferiors Num Description Executable 1 process 15397 /home/pedro/gdb/tests/threads * 2 (gdb) info threads Cannot find new threads: generic error (gdb) Fix this by checking whether each inferior has execution rather than just the current inferior. By moving the core updating to linux-nat.c's update_thread_list implementation, this also ends up fixing the lwp-last-seen-running-on-core updating in the case we're debugging a program that uses raw clone rather than pthreads, as linux-thread-db.c isn't pushed in the target stack in that scenario. Tested on x86_64 Fedora 20. gdb/ChangeLog: 2015-12-11 Pedro Alves PR threads/19354 * linux-nat.c (linux_nat_update_thread_list): Update process cores each lwp was last seen running on here. * linux-thread-db.c (update_thread_core): Delete. (thread_db_update_thread_list_td_ta_thr_iter): Rename to ... (thread_db_update_thread_list): ... this. Skip inferiors with execution. Also call the target beneath. (thread_db_update_thread_list): Delete. gdb/testsuite/ChangeLog: 2015-12-11 Pedro Alves PR threads/19354 * gdb.multi/info-threads.exp: New file. --- gdb/linux-nat.c | 7 +++++ gdb/linux-thread-db.c | 53 ++++++++++++-------------------- gdb/testsuite/gdb.multi/info-threads.exp | 39 +++++++++++++++++++++++ 3 files changed, 65 insertions(+), 34 deletions(-) create mode 100644 gdb/testsuite/gdb.multi/info-threads.exp diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index af1b764..1f9bb47 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -3719,10 +3719,17 @@ linux_nat_thread_alive (struct target_ops *ops, ptid_t ptid) static void linux_nat_update_thread_list (struct target_ops *ops) { + struct lwp_info *lwp; + /* We add/delete threads from the list as clone/exit events are processed, so just try deleting exited threads still in the thread list. */ delete_exited_threads (); + + /* Update the processor core that each lwp/thread was last seen + running on. */ + ALL_LWPS (lwp) + lwp->core = linux_common_core_of_thread (lwp->ptid); } static char * diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c index ce3f6a1..68b6988 100644 --- a/gdb/linux-thread-db.c +++ b/gdb/linux-thread-db.c @@ -1324,17 +1324,11 @@ thread_db_find_new_threads_1 (ptid_t ptid) thread_db_find_new_threads_2 (ptid, 0); } -static int -update_thread_core (struct lwp_info *info, void *closure) -{ - info->core = linux_common_core_of_thread (info->ptid); - return 0; -} - -/* Update the thread list using td_ta_thr_iter. */ +/* Implement the to_update_thread_list target method for this + target. */ static void -thread_db_update_thread_list_td_ta_thr_iter (struct target_ops *ops) +thread_db_update_thread_list (struct target_ops *ops) { struct thread_db_info *info; struct inferior *inf; @@ -1356,34 +1350,25 @@ thread_db_update_thread_list_td_ta_thr_iter (struct target_ops *ops) if (thread == NULL || thread->executing) continue; + /* It's best to avoid td_ta_thr_iter if possible. That walks + data structures in the inferior's address space that may be + corrupted, or, if the target is running, the list may change + while we walk it. In the latter case, it's possible that a + thread exits just at the exact time that causes GDB to get + stuck in an infinite loop. To avoid pausing all threads + whenever the core wants to refresh the thread list, we + instead use thread_from_lwp immediately when we see an LWP + stop. That uses thread_db entry points that do not walk + libpthread's thread list, so should be safe, as well as more + efficient. */ + if (target_has_execution_1 (thread->ptid)) + continue; + thread_db_find_new_threads_1 (thread->ptid); } -} -/* Implement the to_update_thread_list target method for this - target. */ - -static void -thread_db_update_thread_list (struct target_ops *ops) -{ - /* It's best to avoid td_ta_thr_iter if possible. That walks data - structures in the inferior's address space that may be corrupted, - or, if the target is running, the list may change while we walk - it. In the latter case, it's possible that a thread exits just - at the exact time that causes GDB to get stuck in an infinite - loop. To avoid pausing all threads whenever the core wants to - refresh the thread list, use thread_from_lwp immediately when we - see an LWP stop. That uses thread_db entry points that do not - walk libpthread's thread list, so should be safe, as well as - more efficient. */ - if (target_has_execution) - ops->beneath->to_update_thread_list (ops->beneath); - else - thread_db_update_thread_list_td_ta_thr_iter (ops); - - if (target_has_execution) - iterate_over_lwps (minus_one_ptid /* iterate over all */, - update_thread_core, NULL); + /* Give the beneath target a chance to do extra processing. */ + ops->beneath->to_update_thread_list (ops->beneath); } static char * diff --git a/gdb/testsuite/gdb.multi/info-threads.exp b/gdb/testsuite/gdb.multi/info-threads.exp new file mode 100644 index 0000000..c00b8d7 --- /dev/null +++ b/gdb/testsuite/gdb.multi/info-threads.exp @@ -0,0 +1,39 @@ +# Copyright 2009-2015 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 . + +# Regression test for PR threads/19354. If you have a threaded +# inferior running (thread_db must be loaded), switch to another +# inferior, one that is NOT running yet, and do "info threads", you +# would get back an error. + +standard_testfile hello.c + +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} {pthreads debug}] } { + return -1 +} + +clean_restart ${testfile} + +if { ![runto_main] } then { + return -1 +} + +# Add another inferior. +gdb_test "add-inferior" "Added inferior 2.*" "add empty inferior 2" +gdb_test "inferior 2" "Switching to inferior 2.*" "switch to inferior 2" + +# "info threads" while inferior 1 has execution and inferior 2 is not +# running yet should show inferior 1's thread, and give no error. +gdb_test "info threads" "1 .* main .* at .*$srcfile:.*No selected thread.*"