From patchwork Thu Sep 25 14:58:41 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 2969 Received: (qmail 5333 invoked by alias); 25 Sep 2014 14:58:51 -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 5254 invoked by uid 89); 25 Sep 2014 14:58:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS 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; Thu, 25 Sep 2014 14:58:49 +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 (8.14.4/8.14.4) with ESMTP id s8PEwl3a026793 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Thu, 25 Sep 2014 10:58:47 -0400 Received: from brno.lan (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s8PEwhD8006018 for ; Thu, 25 Sep 2014 10:58:46 -0400 From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 1/3] Reduce Hg packet (select remote general thread) bouncing Date: Thu, 25 Sep 2014 15:58:41 +0100 Message-Id: <1411657123-16228-2-git-send-email-palves@redhat.com> In-Reply-To: <1411657123-16228-1-git-send-email-palves@redhat.com> References: <1411657123-16228-1-git-send-email-palves@redhat.com> A patch I wrote made GDB pull the thread list sooner when debugging with target remote, and I noticed an intended consequence. GDB started bouncing around the currently selected remote/general thread more frequently. E.g.: Sending packet: $qTMinFTPILen#3b...Packet received: 5 +Sending packet: $Hgp726d.726d#53...Packet received: OK Sending packet: $m400680,40#2f...Packet received: 85c0741455bff00d60004889e5ffd05de97bffffff0f1f00e973ffffff0f1f00554889e5c745fc00000000c745fc01000000e900000000c745fc02000000b800 +Sending packet: $Hgp726d.7278#28...Packet received: OK Sending packet: $m4006b2,1#28...Packet received: e9 Fast tracepoint 2 at 0x4006b2: file gdb/testsuite/gdb.trace/range-stepping.c, line 53. Sending packet: $qTStatus#49...Packet received: T0;tnotrun:0;tframes:0;tcreated:0;tfree:500000;tsize:500000;circular:0;disconn:0;starttime:0;stoptime:0;username:;notes:: This ended up breaking "tstart" when one has fast tracepoints set, because gdbserver isn't expecting an Hg packet in response to qRelocInsn: (gdb) ftrace *set_point Fast tracepoint 3 at 0x4006b2: file gdb/testsuite/gdb.trace/range-stepping.c, line 53. (gdb) PASS: gdb.trace/range-stepping.exp: ftrace: ftrace *set_point tstart gdbserver: Malformed response to qRelocInsn, ignoring: Hgp2783.2783 Target does not support this command. (gdb) FAIL: gdb.trace/range-stepping.exp: ftrace: tstart remote_trace_start should probably start by making sure the remote current thread matches inferior_ptid (calling set_general_thread), but still, reducing unnecessary bouncing is a good idea. It happens because the memory/symbol/breakpoint routines use switch_to_program_space_and_thread to do something in the right context and then revert back to the previously current thread. The fix is to simply make any_thread_of_process, find_inferior_for_program_space, etc. give preference to the current thread/inferior it if matches. gdb/ 2014-09-25 Pedro Alves * gdbthread.h (any_thread_of_process, any_live_thread_of_process): Adjust comments. * inferior.c (find_inferior_for_program_space): Give preference to the current inferior. * inferior.h (find_inferior_for_program_space): Update comment. * progspace.c (switch_to_program_space_and_thread): Prefer the current inferior if it's bound to the program space requested. If the inferior found doesn't have a PID yet, don't bother looking up a thread. * progspace.h (switch_to_program_space_and_thread): Adjust comment. * thread.c (any_thread_of_process, any_live_thread_of_process): Give preference to the current thread. --- gdb/gdbthread.h | 7 ++++--- gdb/inferior.c | 7 +++++-- gdb/inferior.h | 3 ++- gdb/progspace.c | 5 ++--- gdb/progspace.h | 3 ++- gdb/thread.c | 40 ++++++++++++++++++++++++++++++++++------ 6 files changed, 49 insertions(+), 16 deletions(-) diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h index 522b674..6768491 100644 --- a/gdb/gdbthread.h +++ b/gdb/gdbthread.h @@ -306,11 +306,12 @@ struct thread_info *find_thread_id (int num); returns the first thread in the list. */ struct thread_info *first_thread_of_process (int pid); -/* Returns any thread of process PID. */ +/* Returns any thread of process PID, giving preference to the current + thread. */ extern struct thread_info *any_thread_of_process (int pid); -/* Returns any non-exited thread of process PID, giving preference for - not executing threads. */ +/* Returns any non-exited thread of process PID, giving preference to + the current thread, and to not executing threads. */ extern struct thread_info *any_live_thread_of_process (int pid); /* Change the ptid of thread OLD_PTID to NEW_PTID. */ diff --git a/gdb/inferior.c b/gdb/inferior.c index 23da0c7..fb02439 100644 --- a/gdb/inferior.c +++ b/gdb/inferior.c @@ -367,12 +367,15 @@ find_inferior_pid (int pid) return NULL; } -/* Find an inferior bound to PSPACE. */ +/* See inferior.h. */ struct inferior * find_inferior_for_program_space (struct program_space *pspace) { - struct inferior *inf; + struct inferior *inf = current_inferior (); + + if (inf->pspace == pspace) + return inf; for (inf = inferior_list; inf != NULL; inf = inf->next) { diff --git a/gdb/inferior.h b/gdb/inferior.h index 58557a4..e716005 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -470,7 +470,8 @@ extern struct inferior *find_inferior_pid (int pid); /* Search function to lookup an inferior by GDB 'num'. */ extern struct inferior *find_inferior_id (int num); -/* Find an inferior bound to PSPACE. */ +/* Find an inferior bound to PSPACE, giving preference to the current + inferior. */ extern struct inferior * find_inferior_for_program_space (struct program_space *pspace); diff --git a/gdb/progspace.c b/gdb/progspace.c index a74b6ab..1894559 100644 --- a/gdb/progspace.c +++ b/gdb/progspace.c @@ -463,8 +463,7 @@ save_current_space_and_thread (void) return old_chain; } -/* Switches full context to program space PSPACE. Switches to the - first thread found bound to PSPACE. */ +/* See progspace.h */ void switch_to_program_space_and_thread (struct program_space *pspace) @@ -472,7 +471,7 @@ switch_to_program_space_and_thread (struct program_space *pspace) struct inferior *inf; inf = find_inferior_for_program_space (pspace); - if (inf != NULL) + if (inf != NULL && inf->pid != 0) { struct thread_info *tp; diff --git a/gdb/progspace.h b/gdb/progspace.h index 08e04eb..cf2295c 100644 --- a/gdb/progspace.h +++ b/gdb/progspace.h @@ -264,7 +264,8 @@ extern void set_current_program_space (struct program_space *pspace); extern struct cleanup *save_current_space_and_thread (void); /* Switches full context to program space PSPACE. Switches to the - first thread found bound to PSPACE. */ + first thread found bound to PSPACE, giving preference to the + current thread, if there's one and it isn't executing. */ extern void switch_to_program_space_and_thread (struct program_space *pspace); /* Create a new address space object, and add it to the list. */ diff --git a/gdb/thread.c b/gdb/thread.c index 65890e1..bceaf49 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -466,7 +466,13 @@ any_thread_of_process (int pid) { struct thread_info *tp; - for (tp = thread_list; tp; tp = tp->next) + gdb_assert (pid != 0); + + /* Prefer the current thread. */ + if (ptid_get_pid (inferior_ptid) == pid) + return inferior_thread (); + + ALL_NON_EXITED_THREADS (tp) if (ptid_get_pid (tp->ptid) == pid) return tp; @@ -476,18 +482,40 @@ any_thread_of_process (int pid) struct thread_info * any_live_thread_of_process (int pid) { + struct thread_info *curr_tp = NULL; struct thread_info *tp; struct thread_info *tp_executing = NULL; - for (tp = thread_list; tp; tp = tp->next) - if (tp->state != THREAD_EXITED && ptid_get_pid (tp->ptid) == pid) + gdb_assert (pid != 0); + + /* Prefer the current thread if it's not executing. */ + if (ptid_get_pid (inferior_ptid) == pid) + { + /* If the current thread is dead, forget it. If it's not + executing, use it. Otherwise, still choose it (below), but + only if no other non-executing thread is found. */ + curr_tp = inferior_thread (); + if (curr_tp->state == THREAD_EXITED) + curr_tp = NULL; + else if (!curr_tp->executing) + return curr_tp; + } + + ALL_NON_EXITED_THREADS (tp) + if (ptid_get_pid (tp->ptid) == pid) { - if (tp->executing) - tp_executing = tp; - else + if (!tp->executing) return tp; + + tp_executing = tp; } + /* If both the current thread and all live threads are executing, + prefer the current thread. */ + if (curr_tp != NULL) + return curr_tp; + + /* Otherwise, just return an executing thread, if any. */ return tp_executing; }