From patchwork Sat Oct 21 16:37:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 23752 Received: (qmail 53268 invoked by alias); 21 Oct 2017 16:37:54 -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 53233 invoked by uid 89); 21 Oct 2017 16:37:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_SOFTFAIL autolearn=ham version=3.3.2 spammy= X-HELO: barracuda.ebox.ca Received: from barracuda.ebox.ca (HELO barracuda.ebox.ca) (96.127.255.19) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 21 Oct 2017 16:37:52 +0000 X-ASG-Debug-ID: 1508603867-0c856e65d43816960001-fS2M51 Received: from smtp.electronicbox.net (smtp.electronicbox.net [96.127.255.82]) by barracuda.ebox.ca with ESMTP id uJW1WG8O09LEDSEo (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sat, 21 Oct 2017 12:37:47 -0400 (EDT) X-Barracuda-Envelope-From: simon.marchi@polymtl.ca X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from simark.lan (cable-192.222.251.162.electronicbox.net [192.222.251.162]) by smtp.electronicbox.net (Postfix) with ESMTP id 6A459441D64; Sat, 21 Oct 2017 12:37:47 -0400 (EDT) From: Simon Marchi X-Barracuda-Effective-Source-IP: cable-192.222.251.162.electronicbox.net[192.222.251.162] X-Barracuda-Apparent-Source-IP: 192.222.251.162 X-Barracuda-RBL-IP: 192.222.251.162 To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 3/3] Remove find_inferior usage for thread_search Date: Sat, 21 Oct 2017 12:37:44 -0400 X-ASG-Orig-Subj: [PATCH 3/3] Remove find_inferior usage for thread_search Message-Id: <20171021163744.17005-3-simon.marchi@polymtl.ca> In-Reply-To: <20171021163744.17005-1-simon.marchi@polymtl.ca> References: <20171021163744.17005-1-simon.marchi@polymtl.ca> X-Barracuda-Connect: smtp.electronicbox.net[96.127.255.82] X-Barracuda-Start-Time: 1508603867 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Barracuda-Scan-Msg-Size: 4200 X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.44089 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- X-IsSubscribed: yes From: Simon Marchi Replace it with for_each_thread. While at it, we can inline the callback code. One little change is that I am using the prev_general_thread variable instead of current_gen_ptid, since they should have the same value. gdb/gdbserver/ChangeLog: * target.c (struct thread_search): Remove. (thread_search_callback): Remove. (prepare_to_access_memory): Use for_each_thread instead of find_inferior. Inline code from thread_search_callback. --- gdb/gdbserver/target.c | 88 ++++++++++++++++++-------------------------------- 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/gdb/gdbserver/target.c b/gdb/gdbserver/target.c index 8f757c0a5d..204f941f20 100644 --- a/gdb/gdbserver/target.c +++ b/gdb/gdbserver/target.c @@ -32,51 +32,6 @@ set_desired_thread () return (current_thread != NULL); } -/* Structure used to look up a thread to use as current when accessing - memory. */ - -struct thread_search -{ - /* The PTID of the current general thread. This is an input - parameter. */ - ptid_t current_gen_ptid; - - /* The first thread found. */ - struct thread_info *first; - - /* The first stopped thread found. */ - struct thread_info *stopped; - - /* The current general thread, if found. */ - struct thread_info *current; -}; - -/* Callback for find_inferior. Search for a thread to use as current - when accessing memory. */ - -static int -thread_search_callback (thread_info *thread, void *args) -{ - struct thread_search *s = (struct thread_search *) args; - - if (thread->id.pid () == ptid_get_pid (s->current_gen_ptid) - && mythread_alive (ptid_of (thread))) - { - if (s->stopped == NULL - && the_target->thread_stopped != NULL - && thread_stopped (thread)) - s->stopped = thread; - - if (s->first == NULL) - s->first = thread; - - if (s->current == NULL && s->current_gen_ptid == thread->id) - s->current = thread; - } - - return 0; -} - /* The thread that was current before prepare_to_access_memory was called. done_accessing_memory uses this to restore the previous selected thread. */ @@ -87,11 +42,15 @@ static ptid_t prev_general_thread; int prepare_to_access_memory (void) { - struct thread_search search; - struct thread_info *thread; + /* The first thread found. */ + struct thread_info *first = NULL; + /* The first stopped thread found. */ + struct thread_info *stopped = NULL; + /* The current general thread, if found. */ + struct thread_info *current = NULL; - memset (&search, 0, sizeof (search)); - search.current_gen_ptid = general_thread; + /* Save the general thread value, since prepare_to_access_memory could change + it. */ prev_general_thread = general_thread; if (the_target->prepare_to_access_memory != NULL) @@ -103,18 +62,35 @@ prepare_to_access_memory (void) return res; } - find_inferior (&all_threads, thread_search_callback, &search); + for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread) + { + if (mythread_alive (thread->id)) + { + if (stopped == NULL && the_target->thread_stopped != NULL + && thread_stopped (thread)) + stopped = thread; + + if (first == NULL) + first = thread; + + if (current == NULL && prev_general_thread == thread->id) + current = thread; + } + }); + + /* The thread we end up choosing. */ + struct thread_info *thread; /* Prefer a stopped thread. If none is found, try the current thread. Otherwise, take the first thread in the process. If none is found, undo the effects of target->prepare_to_access_memory() and return error. */ - if (search.stopped != NULL) - thread = search.stopped; - else if (search.current != NULL) - thread = search.current; - else if (search.first != NULL) - thread = search.first; + if (stopped != NULL) + thread = stopped; + else if (current != NULL) + thread = current; + else if (first != NULL) + thread = first; else { done_accessing_memory ();