* configure: Regenerate to track ../common/common.m4 changes.
[deliverable/binutils-gdb.git] / gdb / thread.c
index 5b1b563fa14247a4618282e43fec0601ccf8b5e6..1ae9adfebef23fb3f9c6f5f665a2d7cf6bc6582b 100644 (file)
@@ -42,6 +42,8 @@
 #include "observer.h"
 #include "annotate.h"
 
+#include "cli/cli-decode.h"
+
 /* Definition of struct thread_info exported to gdbthread.h */
 
 /* Prototypes for exported functions. */
@@ -63,6 +65,9 @@ static void thread_apply_command (char *, int);
 static void restore_current_thread (ptid_t);
 static void prune_threads (void);
 
+static int main_thread_running = 0;
+static int main_thread_executing = 0;
+
 void
 delete_step_resume_breakpoint (void *arg)
 {
@@ -90,6 +95,8 @@ free_thread (struct thread_info *tp)
   if (tp->step_resume_breakpoint)
     tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
 
+  bpstat_clear (&tp->stop_bpstat);
+
   /* FIXME: do I ever need to call the back-end to give it a
      chance at this private data before deleting the thread?  */
   if (tp->private)
@@ -104,6 +111,9 @@ init_thread_list (void)
   struct thread_info *tp, *tpnext;
 
   highest_thread_num = 0;
+  main_thread_running = 0;
+  main_thread_executing = 0;
+
   if (!thread_list)
     return;
 
@@ -153,8 +163,10 @@ add_thread (ptid_t ptid)
   return add_thread_with_info (ptid, NULL);
 }
 
-void
-delete_thread (ptid_t ptid)
+/* Delete thread PTID.  If SILENT, don't notify the observer of this
+   exit.  */
+static void
+delete_thread_1 (ptid_t ptid, int silent)
 {
   struct thread_info *tp, *tpprev;
 
@@ -172,11 +184,24 @@ delete_thread (ptid_t ptid)
   else
     thread_list = tp->next;
 
-  observer_notify_thread_exit (tp);
+  if (!silent)
+    observer_notify_thread_exit (tp);
 
   free_thread (tp);
 }
 
+void
+delete_thread (ptid_t ptid)
+{
+  delete_thread_1 (ptid, 0 /* not silent */);
+}
+
+void
+delete_thread_silent (ptid_t ptid)
+{
+  delete_thread_1 (ptid, 1 /* silent */);
+}
+
 static struct thread_info *
 find_thread_id (int num)
 {
@@ -337,7 +362,15 @@ load_infrun_state (ptid_t ptid,
                   int *stepping_through_solib_after_catch,
                   bpstat *stepping_through_solib_catchpoints,
                   int *current_line,
-                  struct symtab **current_symtab)
+                  struct symtab **current_symtab,
+                  struct continuation **continuations,
+                  struct continuation **intermediate_continuations,
+                  int *proceed_to_finish,
+                  enum step_over_calls_kind *step_over_calls,
+                  int *stop_step,
+                  int *step_multi,
+                  enum target_signal *stop_signal,
+                  bpstat *stop_bpstat)
 {
   struct thread_info *tp;
 
@@ -360,6 +393,26 @@ load_infrun_state (ptid_t ptid,
     tp->stepping_through_solib_catchpoints;
   *current_line = tp->current_line;
   *current_symtab = tp->current_symtab;
+
+  /* In all-stop mode, these are global state, while in non-stop mode,
+     they are per thread.  */
+  if (non_stop)
+    {
+      *continuations = tp->continuations;
+      tp->continuations = NULL;
+      *intermediate_continuations = tp->intermediate_continuations;
+      tp->intermediate_continuations = NULL;
+      *proceed_to_finish = tp->proceed_to_finish;
+      *step_over_calls = tp->step_over_calls;
+      *stop_step = tp->stop_step;
+      *step_multi = tp->step_multi;
+      *stop_signal = tp->stop_signal;
+
+      /* Swap instead of copy, so we only have to update one of
+        them.  */
+      *stop_bpstat = tp->stop_bpstat;
+      tp->stop_bpstat = 0;
+    }
 }
 
 /* Save infrun state for the thread PID.  */
@@ -376,7 +429,15 @@ save_infrun_state (ptid_t ptid,
                   int stepping_through_solib_after_catch,
                   bpstat stepping_through_solib_catchpoints,
                   int current_line,
-                  struct symtab *current_symtab)
+                  struct symtab *current_symtab,
+                  struct continuation *continuations,
+                  struct continuation *intermediate_continuations,
+                  int proceed_to_finish,
+                  enum step_over_calls_kind step_over_calls,
+                  int stop_step,
+                  int step_multi,
+                  enum target_signal stop_signal,
+                  bpstat stop_bpstat)
 {
   struct thread_info *tp;
 
@@ -397,6 +458,20 @@ save_infrun_state (ptid_t ptid,
   tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
   tp->current_line = current_line;
   tp->current_symtab = current_symtab;
+
+  /* In all-stop mode, these are global state, while in non-stop mode,
+     they are per thread.  */
+  if (non_stop)
+    {
+      tp->continuations = continuations;
+      tp->intermediate_continuations = intermediate_continuations;
+      tp->proceed_to_finish = proceed_to_finish;
+      tp->step_over_calls = step_over_calls;
+      tp->stop_step = stop_step;
+      tp->step_multi = step_multi;
+      tp->stop_signal = stop_signal;
+      tp->stop_bpstat = stop_bpstat;
+    }
 }
 
 /* Return true if TP is an active thread. */
@@ -426,8 +501,6 @@ prune_threads (void)
     }
 }
 
-static int main_thread_running = 0;
-
 void
 set_running (ptid_t ptid, int running)
 {
@@ -479,6 +552,9 @@ is_running (ptid_t ptid)
 {
   struct thread_info *tp;
 
+  if (!target_has_execution)
+    return 0;
+
   if (!thread_list)
     return main_thread_running;
 
@@ -487,6 +563,67 @@ is_running (ptid_t ptid)
   return tp->running_;  
 }
 
+int
+any_running (void)
+{
+  struct thread_info *tp;
+
+  if (!target_has_execution)
+    return 0;
+
+  if (!thread_list)
+    return main_thread_running;
+
+  for (tp = thread_list; tp; tp = tp->next)
+    if (tp->running_)
+      return 1;
+
+  return 0;
+}
+
+int
+is_executing (ptid_t ptid)
+{
+  struct thread_info *tp;
+
+  if (!target_has_execution)
+    return 0;
+
+  if (!thread_list)
+    return main_thread_executing;
+
+  tp = find_thread_pid (ptid);
+  gdb_assert (tp);
+  return tp->executing_;
+}
+
+void
+set_executing (ptid_t ptid, int executing)
+{
+  struct thread_info *tp;
+
+  if (!thread_list)
+    {
+      /* This target does not add the main thread to the thread list.
+        Use a global flag to indicate that the thread is
+        executing.  */
+      main_thread_executing = executing;
+      return;
+    }
+
+  if (PIDGET (ptid) == -1)
+    {
+      for (tp = thread_list; tp; tp = tp->next)
+       tp->executing_ = executing;
+    }
+  else
+    {
+      tp = find_thread_pid (ptid);
+      gdb_assert (tp);
+      tp->executing_ = executing;
+    }
+}
+
 /* Prints the list of threads and their details on UIOUT.
    This is a version of 'info_thread_command' suitable for
    use from MI.  
@@ -505,9 +642,12 @@ print_thread_info (struct ui_out *uiout, int requested_thread)
   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);
+  if (!is_running (inferior_ptid))
+    saved_frame_id = get_frame_id (get_selected_frame (NULL));
+  else
+    saved_frame_id = null_frame_id;
 
+  old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
   make_cleanup_ui_out_list_begin_end (uiout, "threads");
 
   prune_threads ();
@@ -542,12 +682,18 @@ print_thread_info (struct ui_out *uiout, int requested_thread)
          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), 
-                        /* For MI output, print frame level.  */
-                        ui_out_is_mi_like_p (uiout),
-                        LOCATION);
+      if (tp->running_)
+       ui_out_text (uiout, "(running)\n");
+      else
+       {
+         /* The switch below puts us at the top of the stack (leaf
+            frame).  */
+         switch_to_thread (tp->ptid);
+         print_stack_frame (get_selected_frame (NULL),
+                            /* For MI output, print frame level.  */
+                            ui_out_is_mi_like_p (uiout),
+                            LOCATION);
+       }
 
       do_cleanups (chain2);
     }
@@ -563,6 +709,9 @@ print_thread_info (struct ui_out *uiout, int requested_thread)
        ui_out_field_int (uiout, "current-thread-id", current_thread);
     }
 
+  if (is_running (inferior_ptid))
+    return;
+
   /*  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)
@@ -601,7 +750,11 @@ switch_to_thread (ptid_t ptid)
   inferior_ptid = ptid;
   reinit_frame_cache ();
   registers_changed ();
-  stop_pc = read_pc ();
+
+  if (!is_executing (ptid))
+    stop_pc = read_pc ();
+  else
+    stop_pc = ~(CORE_ADDR) 0;
 }
 
 static void
@@ -638,7 +791,12 @@ do_restore_current_thread_cleanup (void *arg)
 {
   struct current_thread_cleanup *old = arg;
   restore_current_thread (old->inferior_ptid);
-  restore_selected_frame (old->selected_frame_id);
+
+  /* A command like 'thread apply all $exec_command&' may change the
+     running state of the originally selected thread, so we have to
+     recheck it here.  */
+  if (!is_running (old->inferior_ptid))
+    restore_selected_frame (old->selected_frame_id);
   xfree (old);
 }
 
@@ -666,8 +824,7 @@ static void
 thread_apply_all_command (char *cmd, int from_tty)
 {
   struct thread_info *tp;
-  struct cleanup *old_chain;
-  struct cleanup *saved_cmd_cleanup_chain;
+  struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
   char *saved_cmd;
   struct frame_id saved_frame_id;
   ptid_t current_ptid;
@@ -677,8 +834,12 @@ thread_apply_all_command (char *cmd, int from_tty)
     error (_("Please specify a command following the thread ID list"));
   
   current_ptid = inferior_ptid;
-  saved_frame_id = get_frame_id (get_selected_frame (NULL));
-  old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
+
+  if (!is_running (inferior_ptid))
+    saved_frame_id = get_frame_id (get_selected_frame (NULL));
+  else
+    saved_frame_id = null_frame_id;
+  make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
 
   /* It is safe to update the thread list now, before
      traversing it for "thread apply all".  MVS */
@@ -687,11 +848,15 @@ thread_apply_all_command (char *cmd, int from_tty)
   /* Save a copy of the command in case it is clobbered by
      execute_command */
   saved_cmd = xstrdup (cmd);
-  saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
+  make_cleanup (xfree, saved_cmd);
   for (tp = thread_list; tp; tp = tp->next)
     if (thread_alive (tp))
       {
-       switch_to_thread (tp->ptid);
+       if (non_stop)
+         context_switch_to (tp->ptid);
+       else
+         switch_to_thread (tp->ptid);
+
        printf_filtered (_("\nThread %d (%s):\n"),
                         tp->num, target_tid_to_str (inferior_ptid));
        execute_command (cmd, from_tty);
@@ -701,12 +866,10 @@ thread_apply_all_command (char *cmd, int from_tty)
   if (!ptid_equal (current_ptid, inferior_ptid))
     thread_has_changed = 1;
 
-  do_cleanups (saved_cmd_cleanup_chain);
   do_cleanups (old_chain);
   /* Print stack frame only if we changed thread.  */
-  if (thread_has_changed)
+  if (thread_has_changed && !is_running (inferior_ptid))
     print_stack_frame (get_current_frame (), 1, SRC_LINE);
-
 }
 
 static void
@@ -730,7 +893,11 @@ thread_apply_command (char *tidlist, int from_tty)
     error (_("Please specify a command following the thread ID list"));
 
   current_ptid = inferior_ptid;
-  saved_frame_id = get_frame_id (get_selected_frame (NULL));
+
+  if (!is_running (inferior_ptid))
+    saved_frame_id = get_frame_id (get_selected_frame (NULL));
+  else
+    saved_frame_id = null_frame_id;
   old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
 
   /* Save a copy of the command in case it is clobbered by
@@ -774,7 +941,10 @@ thread_apply_command (char *tidlist, int from_tty)
            warning (_("Thread %d has terminated."), start);
          else
            {
-             switch_to_thread (tp->ptid);
+             if (non_stop)
+               context_switch_to (tp->ptid);
+             else
+               switch_to_thread (tp->ptid);
              printf_filtered (_("\nThread %d (%s):\n"), tp->num,
                               target_tid_to_str (inferior_ptid));
              execute_command (cmd, from_tty);
@@ -842,7 +1012,10 @@ do_captured_thread_select (struct ui_out *uiout, void *tidstr)
   if (!thread_alive (tp))
     error (_("Thread ID %d has terminated."), num);
 
-  switch_to_thread (tp->ptid);
+  if (non_stop)
+    context_switch_to (tp->ptid);
+  else
+    switch_to_thread (tp->ptid);
 
   ui_out_text (uiout, "[Switching to thread ");
   ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
@@ -850,7 +1023,11 @@ do_captured_thread_select (struct ui_out *uiout, void *tidstr)
   ui_out_text (uiout, target_tid_to_str (inferior_ptid));
   ui_out_text (uiout, ")]");
 
-  print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
+  if (!tp->running_)
+    print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
+  else
+    ui_out_text (uiout, "(running)\n");
+
   return GDB_RC_OK;
 }
 
@@ -870,14 +1047,17 @@ void
 _initialize_thread (void)
 {
   static struct cmd_list_element *thread_apply_list = NULL;
+  struct cmd_list_element *c;
 
-  add_info ("threads", info_threads_command,
-           _("IDs of currently known threads."));
+  c = add_info ("threads", info_threads_command,
+               _("IDs of currently known threads."));
+  set_cmd_async_ok (c);
 
-  add_prefix_cmd ("thread", class_run, thread_command, _("\
+  c = add_prefix_cmd ("thread", class_run, thread_command, _("\
 Use this command to switch between threads.\n\
 The new thread ID must be currently known."),
-                 &thread_cmd_list, "thread ", 1, &cmdlist);
+                     &thread_cmd_list, "thread ", 1, &cmdlist);
+  set_cmd_async_ok (c);
 
   add_prefix_cmd ("apply", class_run, thread_apply_command,
                  _("Apply a command to a list of threads."),
This page took 0.02835 seconds and 4 git commands to generate.