2008-05-03 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / thread.c
index d2a6e6fc2bdc6b04b210bbb87c3546e35b43984d..3de3289a480341ca53783fc7acb7b151654b8250 100644 (file)
@@ -1,7 +1,7 @@
 /* Multi-process/thread control for GDB, the GNU debugger.
 
    Copyright (C) 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-   2000, 2001, 2002, 2003, 2004, 2007 Free Software Foundation, Inc.
+   2000, 2001, 2002, 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
 
    Contributed by Lynx Real-Time Systems, Inc.  Los Gatos, CA.
 
@@ -9,7 +9,7 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -18,9 +18,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
 #include "symtab.h"
@@ -41,6 +39,7 @@
 #include <sys/types.h>
 #include <signal.h>
 #include "ui-out.h"
+#include "observer.h"
 
 /* Definition of struct thread_info exported to gdbthread.h */
 
@@ -61,10 +60,7 @@ static int thread_alive (struct thread_info *);
 static void info_threads_command (char *, int);
 static void thread_apply_command (char *, int);
 static void restore_current_thread (ptid_t);
-static void switch_to_thread (ptid_t ptid);
 static void prune_threads (void);
-static struct cleanup *make_cleanup_restore_current_thread (ptid_t,
-                                                            struct frame_id);
 
 void
 delete_step_resume_breakpoint (void *arg)
@@ -87,9 +83,11 @@ static void
 free_thread (struct thread_info *tp)
 {
   /* NOTE: this will take care of any left-over step_resume breakpoints,
-     but not any user-specified thread-specific breakpoints. */
+     but not any user-specified thread-specific breakpoints.  We can not
+     delete the breakpoint straight-off, because the inferior might not
+     be stopped at the moment.  */
   if (tp->step_resume_breakpoint)
-    delete_breakpoint (tp->step_resume_breakpoint);
+    tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
 
   /* FIXME: do I ever need to call the back-end to give it a
      chance at this private data before deleting the thread?  */
@@ -117,11 +115,8 @@ init_thread_list (void)
   thread_list = NULL;
 }
 
-/* add_thread now returns a pointer to the new thread_info, 
-   so that back_ends can initialize their private data.  */
-
 struct thread_info *
-add_thread (ptid_t ptid)
+add_thread_silent (ptid_t ptid)
 {
   struct thread_info *tp;
 
@@ -131,9 +126,31 @@ add_thread (ptid_t ptid)
   tp->num = ++highest_thread_num;
   tp->next = thread_list;
   thread_list = tp;
+
+  observer_notify_new_thread (tp);
+
   return tp;
 }
 
+struct thread_info *
+add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
+{
+  struct thread_info *result = add_thread_silent (ptid);
+
+  result->private = private;
+
+  if (print_thread_events)
+    printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
+  
+  return result;
+}
+
+struct thread_info *
+add_thread (ptid_t ptid)
+{
+  return add_thread_with_info (ptid, NULL);
+}
+
 void
 delete_thread (ptid_t ptid)
 {
@@ -153,6 +170,8 @@ delete_thread (ptid_t ptid)
   else
     thread_list = tp->next;
 
+  observer_notify_thread_exit (tp);
+
   free_thread (tp);
 }
 
@@ -301,7 +320,7 @@ load_infrun_state (ptid_t ptid,
                   CORE_ADDR *step_range_end,
                   struct frame_id *step_frame_id,
                   int *handling_longjmp,
-                  int *another_trap,
+                  int *stepping_over_breakpoint,
                   int *stepping_through_solib_after_catch,
                   bpstat *stepping_through_solib_catchpoints,
                   int *current_line,
@@ -322,7 +341,7 @@ load_infrun_state (ptid_t ptid,
   *step_range_end = tp->step_range_end;
   *step_frame_id = tp->step_frame_id;
   *handling_longjmp = tp->handling_longjmp;
-  *another_trap = tp->another_trap;
+  *stepping_over_breakpoint = tp->stepping_over_breakpoint;
   *stepping_through_solib_after_catch =
     tp->stepping_through_solib_after_catch;
   *stepping_through_solib_catchpoints =
@@ -342,7 +361,7 @@ save_infrun_state (ptid_t ptid,
                   CORE_ADDR step_range_end,
                   const struct frame_id *step_frame_id,
                   int handling_longjmp,
-                  int another_trap,
+                  int stepping_over_breakpoint,
                   int stepping_through_solib_after_catch,
                   bpstat stepping_through_solib_catchpoints,
                   int current_line,
@@ -363,7 +382,7 @@ save_infrun_state (ptid_t ptid,
   tp->step_range_end = step_range_end;
   tp->step_frame_id = (*step_frame_id);
   tp->handling_longjmp = handling_longjmp;
-  tp->another_trap = another_trap;
+  tp->stepping_over_breakpoint = stepping_over_breakpoint;
   tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
   tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
   tp->current_line = current_line;
@@ -397,15 +416,14 @@ prune_threads (void)
     }
 }
 
-/* Print information about currently known threads 
-
- * Note: this has the drawback that it _really_ switches
- *       threads, which frees the frame cache.  A no-side
- *       effects info-threads command would be nicer.
- */
-
-static void
-info_threads_command (char *arg, int from_tty)
+/* Prints the list of threads and their details on UIOUT.
+   This is a version of 'info_thread_command' suitable for
+   use from MI.  
+   If REQESTED_THREAD is not -1, it's the GDB id of the thread
+   that should be printed.  Otherwise, all threads are
+   printed.  */
+void
+print_thread_info (struct ui_out *uiout, int requested_thread)
 {
   struct thread_info *tp;
   ptid_t current_ptid;
@@ -413,48 +431,97 @@ info_threads_command (char *arg, int from_tty)
   struct cleanup *old_chain;
   struct frame_id saved_frame_id;
   char *extra_info;
+  int current_thread = -1;
 
   /* Backup current thread and selected frame.  */
   saved_frame_id = get_frame_id (get_selected_frame (NULL));
   old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
 
+  make_cleanup_ui_out_list_begin_end (uiout, "threads");
+
   prune_threads ();
   target_find_new_threads ();
   current_ptid = inferior_ptid;
   for (tp = thread_list; tp; tp = tp->next)
     {
+      struct cleanup *chain2;
+
+      if (requested_thread != -1 && tp->num != requested_thread)
+       continue;
+
+      chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
+
       if (ptid_equal (tp->ptid, current_ptid))
-       printf_filtered ("* ");
+       {
+         current_thread = tp->num;
+         ui_out_text (uiout, "* ");
+       }
       else
-       printf_filtered ("  ");
+       ui_out_text (uiout, "  ");
 
-      printf_filtered ("%d %s", tp->num, target_tid_to_str (tp->ptid));
+      ui_out_field_int (uiout, "id", tp->num);
+      ui_out_text (uiout, " ");
+      ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid));
 
       extra_info = target_extra_thread_info (tp);
       if (extra_info)
-       printf_filtered (" (%s)", extra_info);
-      puts_filtered ("  ");
+       {
+         ui_out_text (uiout, " (");
+         ui_out_field_string (uiout, "details", extra_info);
+         ui_out_text (uiout, ")");
+       }
+      ui_out_text (uiout, "  ");
       /* That switch put us at the top of the stack (leaf frame).  */
       switch_to_thread (tp->ptid);
-      print_stack_frame (get_selected_frame (NULL), 0, LOCATION);
+      print_stack_frame (get_selected_frame (NULL), 
+                        /* For MI output, print frame level.  */
+                        ui_out_is_mi_like_p (uiout),
+                        LOCATION);
+
+      do_cleanups (chain2);
     }
 
   /* Restores the current thread and the frame selected before
      the "info threads" command.  */
   do_cleanups (old_chain);
 
+  if (requested_thread == -1)
+    {
+      gdb_assert (current_thread != -1 || !thread_list);
+      if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
+       ui_out_field_int (uiout, "current-thread-id", current_thread);
+    }
+
   /*  If case we were not able to find the original frame, print the
       new selected frame.  */
   if (frame_find_by_id (saved_frame_id) == NULL)
     {
       warning (_("Couldn't restore frame in current thread, at frame 0"));
-      print_stack_frame (get_selected_frame (NULL), 0, LOCATION);
+      /* For MI, we should probably have a notification about
+        current frame change.  But this error is not very likely, so
+        don't bother for now.  */
+      if (!ui_out_is_mi_like_p (uiout))
+       print_stack_frame (get_selected_frame (NULL), 0, LOCATION);
     }
 }
 
-/* Switch from one thread to another. */
+
+/* Print information about currently known threads 
+
+ * Note: this has the drawback that it _really_ switches
+ *       threads, which frees the frame cache.  A no-side
+ *       effects info-threads command would be nicer.
+ */
 
 static void
+info_threads_command (char *arg, int from_tty)
+{
+  print_thread_info (uiout, -1);
+}
+
+/* Switch from one thread to another. */
+
+void
 switch_to_thread (ptid_t ptid)
 {
   if (ptid_equal (ptid, inferior_ptid))
@@ -504,7 +571,7 @@ do_restore_current_thread_cleanup (void *arg)
   xfree (old);
 }
 
-static struct cleanup *
+struct cleanup *
 make_cleanup_restore_current_thread (ptid_t inferior_ptid, 
                                      struct frame_id a_frame_id)
 {
@@ -676,6 +743,17 @@ thread_command (char *tidstr, int from_tty)
   gdb_thread_select (uiout, tidstr, NULL);
 }
 
+/* Print notices when new threads are attached and detached.  */
+int print_thread_events = 1;
+static void
+show_print_thread_events (struct ui_file *file, int from_tty,
+                          struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("\
+Printing of thread events is %s.\n"),
+                    value);
+}
+
 static int
 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
 {
@@ -738,4 +816,12 @@ The new thread ID must be currently known."),
 
   if (!xdb_commands)
     add_com_alias ("t", "thread", class_run, 1);
+
+  add_setshow_boolean_cmd ("thread-events", no_class,
+         &print_thread_events, _("\
+Set printing of thread events (e.g., thread start and exit)."), _("\
+Show printing of thread events (e.g., thread start and exit)."), NULL,
+         NULL,
+         show_print_thread_events,
+         &setprintlist, &showprintlist);
 }
This page took 0.028574 seconds and 4 git commands to generate.