public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Jan Kratochvil <jan.kratochvil@redhat.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: gdb-patches@sourceware.org, dje@google.com
Subject: [patchv4] Sort threads for thread apply all (bt)
Date: Thu, 22 Jan 2015 19:24:00 -0000	[thread overview]
Message-ID: <20150122190141.GB16253@host2.jankratochvil.net> (raw)
In-Reply-To: <83zj9atzpu.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 418 bytes --]

On Thu, 22 Jan 2015 19:52:29 +0100, Eli Zaretskii wrote:
> (Do we want a NEWS entry?)

OK, I think so.

I have taken this entry as a template:
------------------------------------------------------------------------------
* The command 'tsave' can now support new option '-ctf' to save trace
  buffer in Common Trace Format.
------------------------------------------------------------------------------


Thanks,
Jan

[-- Attachment #2: tparray4.patch --]
[-- Type: text/plain, Size: 4386 bytes --]

gdb/ChangeLog
2015-01-16  Jan Kratochvil  <jan.kratochvil@redhat.com>

	* NEWS (Changes since GDB 7.9): Add 'thread apply all' option
	'-ascending'.
	* 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  <jan.kratochvil@redhat.com>

	* gdb.texinfo (Threads): Describe -ascending for thread apply all.

diff --git a/gdb/NEWS b/gdb/NEWS
index 2d2c941..c0cf706 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,11 @@
 
 *** Changes since GDB 7.9
 
+* New options
+
+* The command 'thread apply all' can now support new option '-ascending'
+  to call its specified command for all threads in ascending order.
+
 *** Changes in GDB 7.9
 
 * GDB now supports hardware watchpoints on x86 GNU Hurd.
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] <command>\n\
+-ascending: Call <command> 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

  reply	other threads:[~2015-01-22 19:23 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-15 18:33 [patch] " Jan Kratochvil
2015-01-15 19:29 ` Doug Evans
2015-01-16 23:38   ` [patchv2] " Jan Kratochvil
2015-01-22  0:26     ` Doug Evans
2015-01-22 11:18       ` Pedro Alves
2015-01-22 16:43         ` Jan Kratochvil
2015-01-22 17:07           ` Pedro Alves
2015-01-22 17:18             ` Jan Kratochvil
2015-01-22 18:09               ` Doug Evans
2015-01-22 18:13                 ` Jan Kratochvil
2015-01-22 18:49       ` [patchv3] " Jan Kratochvil
2015-01-22 18:52         ` Eli Zaretskii
2015-01-22 19:24           ` Jan Kratochvil [this message]
2015-01-22 19:34             ` [patchv4] " Doug Evans
2015-01-22 20:08               ` Doug Evans
2015-01-22 21:15               ` [commit] [patchv5] " Jan Kratochvil
2015-01-22 20:52             ` [patchv4] " Eli Zaretskii
2015-01-22 19:10         ` [patchv3] " Doug Evans
2015-01-22 19:23           ` Doug Evans
2015-01-22 19:24           ` [patchv5] " Jan Kratochvil
2015-01-22 20:37           ` [patchv3] " Doug Evans
2015-01-17 20:59   ` [patch] Print current thread after loading a core file [Re: [patch] Sort threads for thread apply all (bt)] Jan Kratochvil
2015-01-22  0:36     ` Doug Evans
2015-01-22 18:36       ` [commit] " Jan Kratochvil

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150122190141.GB16253@host2.jankratochvil.net \
    --to=jan.kratochvil@redhat.com \
    --cc=dje@google.com \
    --cc=eliz@gnu.org \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).