public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: add threads debugging switch
@ 2021-12-11 10:15 Andrew Burgess
  2021-12-22 15:04 ` Andrew Burgess
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Burgess @ 2021-12-11 10:15 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess

Add new commands:

  set debug threads on|off
  show debug threads

Prints additional debug information relating to thread creation and
deletion.

GDB already announces when threads are created of course.... most of
the time, but sometimes threads are added silently, in which case this
debug message is the only mechanism to see the thread being added.
Also, though GDB does announce when a thread exits, it doesn't
announce when the thread object is deleted, I've added a debug message
for that.

Additionally, having message printed through the debug system will
cause the messages to be nested to an appropriate depth when other
debug sub-systems are turned on (especially things like `infrun` and
`lin-lwp`).
---
 gdb/NEWS            |  4 ++++
 gdb/doc/gdb.texinfo |  8 ++++++++
 gdb/gdbthread.h     | 11 +++++++++++
 gdb/inferior.c      |  2 ++
 gdb/thread.c        | 38 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 63 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 13b66286876..267945ad145 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -45,6 +45,10 @@ set logging enabled on|off
 show logging enabled
   These commands set or show whether logging is enabled or disabled.
 
+set debug threads on|off
+show debug threads
+  Print additional debug messages about thread creation and deletion.
+
 * Changed commands
 
 maint packet
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index d0c5bcf18e1..1f19149b876 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -3897,6 +3897,14 @@
 @itemx show debug libthread-db
 Turns on or off display of @code{libthread_db}-related events.
 Use @code{1} to enable, @code{0} to disable.
+
+@kindex set debug threads
+@kindex show debug threads
+@cindex debugging @code{threads}
+@item set debug threads @r{[}on@r{|}off@r{]}
+@itemx show debug threads
+When @samp{on} @value{GDBN} will print additional messages when
+threads are created and deleted.
 @end table
 
 @node Forks
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index e229a5f09bd..66c525b42a7 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -38,6 +38,16 @@ struct symtab;
 struct inferior;
 struct process_stratum_target;
 
+/* When true, print debug messages related to GDB thread creation and
+   deletion.  */
+
+extern bool debug_threads;
+
+/* Print a "threads" debug statement.  */
+
+#define threads_debug_printf(fmt, ...) \
+  debug_prefixed_printf_cond (debug_threads, "threads", fmt, ##__VA_ARGS__)
+
 /* Frontend view of the thread state.  Possible extensions: stepping,
    finishing, until(ling),...
 
@@ -235,6 +245,7 @@ class thread_info : public refcounted_object,
 {
 public:
   explicit thread_info (inferior *inf, ptid_t ptid);
+  ~thread_info ();
 
   bool deletable () const;
 
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 40ab3e7e90b..72b7c6181ad 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -179,6 +179,8 @@ inferior::clear_thread_list (bool silent)
 {
   thread_list.clear_and_dispose ([=] (thread_info *thr)
     {
+      threads_debug_printf ("deleting thread %s, silent = %d",
+			    thr->ptid.to_string ().c_str (), silent);
       set_thread_exited (thr, silent);
       if (thr->deletable ())
 	delete thr;
diff --git a/gdb/thread.c b/gdb/thread.c
index 6c792eccb8f..be1af78e0ed 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -49,6 +49,19 @@
 #include "inline-frame.h"
 #include "stack.h"
 
+/* See gdbthread.h.  */
+
+bool debug_threads = false;
+
+/* Implement 'show debug threads'.  */
+
+static void
+show_debug_threads (struct ui_file *file, int from_tty,
+		    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("GDB thread debugging is \"%s\".\n"), value);
+}
+
 /* Definition of struct thread_info exported to gdbthread.h.  */
 
 /* Prototypes for local functions.  */
@@ -234,6 +247,9 @@ new_thread (struct inferior *inf, ptid_t ptid)
 {
   thread_info *tp = new thread_info (inf, ptid);
 
+  threads_debug_printf ("creating a new thread object, inferior %d, ptid %s",
+			inf->num, ptid.to_string ().c_str ());
+
   inf->thread_list.push_back (*tp);
 
   /* A thread with this ptid should not exist in the map yet.  */
@@ -251,6 +267,10 @@ add_thread_silent (process_stratum_target *targ, ptid_t ptid)
 
   inferior *inf = find_inferior_ptid (targ, ptid);
 
+  threads_debug_printf ("add thread to inferior %d, ptid %s, target %s",
+			inf->num, ptid.to_string ().c_str (),
+			targ->shortname ());
+
   /* We may have an old thread with the same id in the thread list.
      If we do, it must be dead, otherwise we wouldn't be adding a new
      thread with the same id.  The OS is reusing this id --- delete
@@ -300,6 +320,13 @@ thread_info::thread_info (struct inferior *inf_, ptid_t ptid_)
   this->pending_follow.set_spurious ();
 }
 
+/* Destructor.  Just gives some debug output for now.  */
+
+thread_info::~thread_info ()
+{
+  threads_debug_printf ("thread %s", this->ptid.to_string ().c_str ());
+}
+
 /* See gdbthread.h.  */
 
 bool
@@ -434,6 +461,9 @@ delete_thread_1 (thread_info *thr, bool silent)
 {
   gdb_assert (thr != nullptr);
 
+  threads_debug_printf ("deleting thread %s, silent = %d",
+			thr->ptid.to_string ().c_str (), silent);
+
   set_thread_exited (thr, silent);
 
   if (!thr->deletable ())
@@ -2192,6 +2222,14 @@ Show printing of thread events (such as thread start and exit)."), NULL,
 			   show_print_thread_events,
 			   &setprintlist, &showprintlist);
 
+  add_setshow_boolean_cmd ("threads", class_maintenance, &debug_threads, _("\
+Set thread debugging."), _("\
+Show thread debugging."), _("\
+When on messages about thread creation and deletion are printed."),
+			   nullptr,
+			   show_debug_threads,
+			   &setdebuglist, &showdebuglist);
+
   create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
   create_internalvar_type_lazy ("_gthread", &gthread_funcs, NULL);
 }
-- 
2.25.4


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] gdb: add threads debugging switch
  2021-12-11 10:15 [PATCH] gdb: add threads debugging switch Andrew Burgess
@ 2021-12-22 15:04 ` Andrew Burgess
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Burgess @ 2021-12-22 15:04 UTC (permalink / raw)
  To: gdb-patches

* Andrew Burgess <aburgess@redhat.com> [2021-12-11 10:15:31 +0000]:

> Add new commands:
> 
>   set debug threads on|off
>   show debug threads
> 
> Prints additional debug information relating to thread creation and
> deletion.
> 
> GDB already announces when threads are created of course.... most of
> the time, but sometimes threads are added silently, in which case this
> debug message is the only mechanism to see the thread being added.
> Also, though GDB does announce when a thread exits, it doesn't
> announce when the thread object is deleted, I've added a debug message
> for that.
> 
> Additionally, having message printed through the debug system will
> cause the messages to be nested to an appropriate depth when other
> debug sub-systems are turned on (especially things like `infrun` and
> `lin-lwp`).

I updated some of the comments, and help text in this patch a little,
and pushed it.  The version of the patch I pushed is included below.

Thanks,
Andrew

---

commit 5b0a3d62423236100bac1e77f7cc084ad9ce0271
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Fri Nov 12 10:30:27 2021 +0000

    gdb: add threads debugging switch
    
    Add new commands:
    
      set debug threads on|off
      show debug threads
    
    Prints additional debug information relating to thread creation and
    deletion.
    
    GDB already announces when threads are created of course.... most of
    the time, but sometimes threads are added silently, in which case this
    debug message is the only mechanism to see the thread being added.
    Also, though GDB does announce when a thread exits, it doesn't
    announce when the thread object is deleted, I've added a debug message
    for that.
    
    Additionally, having message printed through the debug system will
    cause the messages to be nested to an appropriate depth when other
    debug sub-systems are turned on (especially things like `infrun` and
    `lin-lwp`).

diff --git a/gdb/NEWS b/gdb/NEWS
index 935a10c91eb..1e4afa91bc5 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -60,6 +60,10 @@ exit
   You can now exit GDB by using the new command "exit", in addition to
   the existing "quit" command.
 
+set debug threads on|off
+show debug threads
+  Print additional debug messages about thread creation and deletion.
+
 * Changed commands
 
 maint packet
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 0c517b464d8..2d3414175c1 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -3899,6 +3899,14 @@
 @itemx show debug libthread-db
 Turns on or off display of @code{libthread_db}-related events.
 Use @code{1} to enable, @code{0} to disable.
+
+@kindex set debug threads
+@kindex show debug threads
+@cindex debugging @code{threads}
+@item set debug threads @r{[}on@r{|}off@r{]}
+@itemx show debug threads
+When @samp{on} @value{GDBN} will print additional messages when
+threads are created and deleted.
 @end table
 
 @node Forks
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index e229a5f09bd..66c525b42a7 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -38,6 +38,16 @@ struct symtab;
 struct inferior;
 struct process_stratum_target;
 
+/* When true, print debug messages related to GDB thread creation and
+   deletion.  */
+
+extern bool debug_threads;
+
+/* Print a "threads" debug statement.  */
+
+#define threads_debug_printf(fmt, ...) \
+  debug_prefixed_printf_cond (debug_threads, "threads", fmt, ##__VA_ARGS__)
+
 /* Frontend view of the thread state.  Possible extensions: stepping,
    finishing, until(ling),...
 
@@ -235,6 +245,7 @@ class thread_info : public refcounted_object,
 {
 public:
   explicit thread_info (inferior *inf, ptid_t ptid);
+  ~thread_info ();
 
   bool deletable () const;
 
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 40ab3e7e90b..72b7c6181ad 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -179,6 +179,8 @@ inferior::clear_thread_list (bool silent)
 {
   thread_list.clear_and_dispose ([=] (thread_info *thr)
     {
+      threads_debug_printf ("deleting thread %s, silent = %d",
+			    thr->ptid.to_string ().c_str (), silent);
       set_thread_exited (thr, silent);
       if (thr->deletable ())
 	delete thr;
diff --git a/gdb/thread.c b/gdb/thread.c
index 6c792eccb8f..ebaed1c6ca4 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -49,6 +49,19 @@
 #include "inline-frame.h"
 #include "stack.h"
 
+/* See gdbthread.h.  */
+
+bool debug_threads = false;
+
+/* Implement 'show debug threads'.  */
+
+static void
+show_debug_threads (struct ui_file *file, int from_tty,
+		    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Thread debugging is \"%s\".\n"), value);
+}
+
 /* Definition of struct thread_info exported to gdbthread.h.  */
 
 /* Prototypes for local functions.  */
@@ -234,6 +247,9 @@ new_thread (struct inferior *inf, ptid_t ptid)
 {
   thread_info *tp = new thread_info (inf, ptid);
 
+  threads_debug_printf ("creating a new thread object, inferior %d, ptid %s",
+			inf->num, ptid.to_string ().c_str ());
+
   inf->thread_list.push_back (*tp);
 
   /* A thread with this ptid should not exist in the map yet.  */
@@ -251,6 +267,10 @@ add_thread_silent (process_stratum_target *targ, ptid_t ptid)
 
   inferior *inf = find_inferior_ptid (targ, ptid);
 
+  threads_debug_printf ("add thread to inferior %d, ptid %s, target %s",
+			inf->num, ptid.to_string ().c_str (),
+			targ->shortname ());
+
   /* We may have an old thread with the same id in the thread list.
      If we do, it must be dead, otherwise we wouldn't be adding a new
      thread with the same id.  The OS is reusing this id --- delete
@@ -302,6 +322,13 @@ thread_info::thread_info (struct inferior *inf_, ptid_t ptid_)
 
 /* See gdbthread.h.  */
 
+thread_info::~thread_info ()
+{
+  threads_debug_printf ("thread %s", this->ptid.to_string ().c_str ());
+}
+
+/* See gdbthread.h.  */
+
 bool
 thread_info::deletable () const
 {
@@ -434,6 +461,9 @@ delete_thread_1 (thread_info *thr, bool silent)
 {
   gdb_assert (thr != nullptr);
 
+  threads_debug_printf ("deleting thread %s, silent = %d",
+			thr->ptid.to_string ().c_str (), silent);
+
   set_thread_exited (thr, silent);
 
   if (!thr->deletable ())
@@ -2192,6 +2222,14 @@ Show printing of thread events (such as thread start and exit)."), NULL,
 			   show_print_thread_events,
 			   &setprintlist, &showprintlist);
 
+  add_setshow_boolean_cmd ("threads", class_maintenance, &debug_threads, _("\
+Set thread debugging."), _("\
+Show thread debugging."), _("\
+When on messages about thread creation and deletion are printed."),
+			   nullptr,
+			   show_debug_threads,
+			   &setdebuglist, &showdebuglist);
+
   create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
   create_internalvar_type_lazy ("_gthread", &gthread_funcs, NULL);
 }


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-12-22 15:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-11 10:15 [PATCH] gdb: add threads debugging switch Andrew Burgess
2021-12-22 15:04 ` Andrew Burgess

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).