From patchwork Thu Jan 22 18:46:55 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kratochvil X-Patchwork-Id: 4764 Received: (qmail 15580 invoked by alias); 22 Jan 2015 18:49:13 -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 15346 invoked by uid 89); 22 Jan 2015 18:48:54 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.9 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_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; Thu, 22 Jan 2015 18:47:17 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t0MIkxpq009351 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Thu, 22 Jan 2015 13:47:00 -0500 Received: from host2.jankratochvil.net (ovpn-116-51.ams2.redhat.com [10.36.116.51]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t0MIktEA007283 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Thu, 22 Jan 2015 13:46:58 -0500 Date: Thu, 22 Jan 2015 19:46:55 +0100 From: Jan Kratochvil To: Eli Zaretskii Cc: gdb-patches , Doug Evans Subject: [patchv3] Sort threads for thread apply all (bt) Message-ID: <20150122184655.GA15064@host2.jankratochvil.net> References: <20150115183316.GA16405@host2.jankratochvil.net> <20150116233802.GA8732@host2.jankratochvil.net> <21696.17350.451535.337528@ruffy2.mtv.corp.google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <21696.17350.451535.337528@ruffy2.mtv.corp.google.com> User-Agent: Mutt/1.5.23 (2014-03-12) X-IsSubscribed: yes On Thu, 22 Jan 2015 01:26:46 +0100, Doug Evans wrote: > Just some nits, no need to resubmit for review. Resubmitting it as a request for doc approval from Eli. > > +static int tp_array_compar_asc; > > This should probably have a comment. Done, I forgot. > > + > > +/* Sort an array for struct thread_info pointers by their ascending NUM. */ > > + > > +static int > > +tp_array_compar (const void *ap_voidp, const void *bp_voidp) [...] > > + return ((((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num)) > > + * (tp_array_compar_asc ? +1 : -1)); > > +} > > This triggers my "passing parameters as global variables" alarm, > and while one could instead have two different functions, I believe qsort_r() is more appropriate, using two different functions I would feel rather as a workaround of missing qsort_r(). But I guess (I cannot easily test due to missing slaves and no patch testing feature in Sergio's Buildbot yet) some of the supported platforms do not provide qsort_r() so it would need a new gdb/gnulib/ module. But at least one of my gdb/gnulib/ patches is still under review so it would create dependency between unrelated patchsets. > > + if (cmd && (check_for_argument (&cmd, "-asc", strlen ("-asc")))) > > cmd != NULL BTW this has been copy-paste but yes, my fault, I am aware of it. Thanks, Jan gdb/ChangeLog 2015-01-16 Jan Kratochvil * thread.c (tp_array_compar_ascending, tp_array_compar): New. (thread_apply_all_command): Parse CMD for tp_array_compar_ascending. Sort tp_array using tp_array_compar. (_initialize_thread): Extend thread_apply_all_command help. gdb/doc/ChangeLog 2015-01-16 Jan Kratochvil * gdb.texinfo (Threads): Describe -ascending for thread apply all. diff --git a/gdb/thread.c b/gdb/thread.c index 4bce212..870a956 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -1381,6 +1381,24 @@ make_cleanup_restore_current_thread (void) restore_current_thread_cleanup_dtor); } +/* If non-zero tp_array_compar should sort in ascending order, otherwise in + descending order. */ + +static int tp_array_compar_ascending; + +/* Sort an array for struct thread_info pointers by their NUM, order is + determined by TP_ARRAY_COMPAR_ASCENDING. */ + +static int +tp_array_compar (const void *ap_voidp, const void *bp_voidp) +{ + const struct thread_info *const *ap = ap_voidp; + const struct thread_info *const *bp = bp_voidp; + + return ((((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num)) + * (tp_array_compar_ascending ? +1 : -1)); +} + /* Apply a GDB command to a list of threads. List syntax is a whitespace seperated list of numbers, or ranges, or the keyword `all'. Ranges consist of two numbers seperated by a hyphen. Examples: @@ -1397,6 +1415,13 @@ thread_apply_all_command (char *cmd, int from_tty) int tc; struct thread_array_cleanup ta_cleanup; + tp_array_compar_ascending = 0; + if (cmd != NULL && (check_for_argument (&cmd, "-ascending", strlen ("-asc")))) + { + cmd = skip_spaces (cmd); + tp_array_compar_ascending = 1; + } + if (cmd == NULL || *cmd == '\000') error (_("Please specify a command following the thread ID list")); @@ -1430,6 +1455,8 @@ thread_apply_all_command (char *cmd, int from_tty) i++; } + qsort (tp_array, i, sizeof (*tp_array), tp_array_compar); + make_cleanup (set_thread_refcount, &ta_cleanup); for (k = 0; k != i; k++) @@ -1738,7 +1765,14 @@ The new thread ID must be currently known."), &thread_apply_list, "thread apply ", 1, &thread_cmd_list); add_cmd ("all", class_run, thread_apply_all_command, - _("Apply a command to all threads."), &thread_apply_list); + _("\ +Apply a command to all threads.\n\ +\n\ +Usage: thread apply all [-ascending] \n\ +-ascending: Call for all threads in ascending order.\n\ + The default is descending order.\ +"), + &thread_apply_list); add_cmd ("name", class_run, thread_name_command, _("Set the current thread's name.\n\ diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index f413e23..277df25 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -2959,14 +2959,17 @@ information on convenience variables. @kindex thread apply @cindex apply command to several threads -@item thread apply [@var{threadno} | all] @var{command} +@item thread apply [@var{threadno} | all [-ascending]] @var{command} The @code{thread apply} command allows you to apply the named @var{command} to one or more threads. Specify the numbers of the threads that you want affected with the command argument @var{threadno}. It can be a single thread number, one of the numbers shown in the first field of the @samp{info threads} display; or it -could be a range of thread numbers, as in @code{2-4}. To apply a -command to all threads, type @kbd{thread apply all @var{command}}. +could be a range of thread numbers, as in @code{2-4}. To apply +a command to all threads in descending order, type @kbd{thread apply all +@var{command}}. To apply a command to all threads in ascending order, +type @kbd{thread apply all -ascending @var{command}}. + @kindex thread name @cindex name a thread