gdb/thread.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Multi-process/thread control for GDB, the GNU debugger.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    Contributed by Lynx Real-Time Systems, Inc.  Los Gatos, CA.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "frame.h"
  18. #include "inferior.h"
  19. #include "environ.h"
  20. #include "value.h"
  21. #include "target.h"
  22. #include "gdbthread.h"
  23. #include "command.h"
  24. #include "gdbcmd.h"
  25. #include "regcache.h"
  26. #include "gdb.h"
  27. #include "btrace.h"

  28. #include <ctype.h>
  29. #include <sys/types.h>
  30. #include <signal.h>
  31. #include "ui-out.h"
  32. #include "observer.h"
  33. #include "annotate.h"
  34. #include "cli/cli-decode.h"
  35. #include "gdb_regex.h"
  36. #include "cli/cli-utils.h"
  37. #include "continuations.h"

  38. /* Definition of struct thread_info exported to gdbthread.h.  */

  39. /* Prototypes for exported functions.  */

  40. void _initialize_thread (void);

  41. /* Prototypes for local functions.  */

  42. struct thread_info *thread_list = NULL;
  43. static int highest_thread_num;

  44. /* True if any thread is, or may be executing.  We need to track this
  45.    separately because until we fully sync the thread list, we won't
  46.    know whether the target is fully stopped, even if we see stop
  47.    events for all known threads, because any of those threads may have
  48.    spawned new threads we haven't heard of yet.  */
  49. static int threads_executing;

  50. static void thread_command (char *tidstr, int from_tty);
  51. static void thread_apply_all_command (char *, int);
  52. static int thread_alive (struct thread_info *);
  53. static void info_threads_command (char *, int);
  54. static void thread_apply_command (char *, int);
  55. static void restore_current_thread (ptid_t);

  56. /* Data to cleanup thread array.  */

  57. struct thread_array_cleanup
  58. {
  59.   /* Array of thread pointers used to set
  60.      reference count.  */
  61.   struct thread_info **tp_array;

  62.   /* Thread count in the array.  */
  63.   int count;
  64. };


  65. struct thread_info*
  66. inferior_thread (void)
  67. {
  68.   struct thread_info *tp = find_thread_ptid (inferior_ptid);
  69.   gdb_assert (tp);
  70.   return tp;
  71. }

  72. /* Delete the breakpoint pointed at by BP_P, if there's one.  */

  73. static void
  74. delete_thread_breakpoint (struct breakpoint **bp_p)
  75. {
  76.   if (*bp_p != NULL)
  77.     {
  78.       delete_breakpoint (*bp_p);
  79.       *bp_p = NULL;
  80.     }
  81. }

  82. void
  83. delete_step_resume_breakpoint (struct thread_info *tp)
  84. {
  85.   if (tp != NULL)
  86.     delete_thread_breakpoint (&tp->control.step_resume_breakpoint);
  87. }

  88. void
  89. delete_exception_resume_breakpoint (struct thread_info *tp)
  90. {
  91.   if (tp != NULL)
  92.     delete_thread_breakpoint (&tp->control.exception_resume_breakpoint);
  93. }

  94. /* See gdbthread.h.  */

  95. void
  96. delete_single_step_breakpoints (struct thread_info *tp)
  97. {
  98.   if (tp != NULL)
  99.     delete_thread_breakpoint (&tp->control.single_step_breakpoints);
  100. }

  101. /* Delete the breakpoint pointed at by BP_P at the next stop, if
  102.    there's one.  */

  103. static void
  104. delete_at_next_stop (struct breakpoint **bp)
  105. {
  106.   if (*bp != NULL)
  107.     {
  108.       (*bp)->disposition = disp_del_at_next_stop;
  109.       *bp = NULL;
  110.     }
  111. }

  112. /* See gdbthread.h.  */

  113. int
  114. thread_has_single_step_breakpoints_set (struct thread_info *tp)
  115. {
  116.   return tp->control.single_step_breakpoints != NULL;
  117. }

  118. /* See gdbthread.h.  */

  119. int
  120. thread_has_single_step_breakpoint_here (struct thread_info *tp,
  121.                                         struct address_space *aspace,
  122.                                         CORE_ADDR addr)
  123. {
  124.   struct breakpoint *ss_bps = tp->control.single_step_breakpoints;

  125.   return (ss_bps != NULL
  126.           && breakpoint_has_location_inserted_here (ss_bps, aspace, addr));
  127. }

  128. static void
  129. clear_thread_inferior_resources (struct thread_info *tp)
  130. {
  131.   /* NOTE: this will take care of any left-over step_resume breakpoints,
  132.      but not any user-specified thread-specific breakpoints.  We can not
  133.      delete the breakpoint straight-off, because the inferior might not
  134.      be stopped at the moment.  */
  135.   delete_at_next_stop (&tp->control.step_resume_breakpoint);
  136.   delete_at_next_stop (&tp->control.exception_resume_breakpoint);
  137.   delete_at_next_stop (&tp->control.single_step_breakpoints);

  138.   delete_longjmp_breakpoint_at_next_stop (tp->num);

  139.   bpstat_clear (&tp->control.stop_bpstat);

  140.   btrace_teardown (tp);

  141.   do_all_intermediate_continuations_thread (tp, 1);
  142.   do_all_continuations_thread (tp, 1);
  143. }

  144. static void
  145. free_thread (struct thread_info *tp)
  146. {
  147.   if (tp->private)
  148.     {
  149.       if (tp->private_dtor)
  150.         tp->private_dtor (tp->private);
  151.       else
  152.         xfree (tp->private);
  153.     }

  154.   xfree (tp->name);
  155.   xfree (tp);
  156. }

  157. void
  158. init_thread_list (void)
  159. {
  160.   struct thread_info *tp, *tpnext;

  161.   highest_thread_num = 0;

  162.   if (!thread_list)
  163.     return;

  164.   for (tp = thread_list; tp; tp = tpnext)
  165.     {
  166.       tpnext = tp->next;
  167.       free_thread (tp);
  168.     }

  169.   thread_list = NULL;
  170.   threads_executing = 0;
  171. }

  172. /* Allocate a new thread with target id PTID and add it to the thread
  173.    list.  */

  174. static struct thread_info *
  175. new_thread (ptid_t ptid)
  176. {
  177.   struct thread_info *tp;

  178.   tp = xcalloc (1, sizeof (*tp));

  179.   tp->ptid = ptid;
  180.   tp->num = ++highest_thread_num;
  181.   tp->next = thread_list;
  182.   thread_list = tp;

  183.   /* Nothing to follow yet.  */
  184.   tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
  185.   tp->state = THREAD_STOPPED;

  186.   return tp;
  187. }

  188. struct thread_info *
  189. add_thread_silent (ptid_t ptid)
  190. {
  191.   struct thread_info *tp;

  192.   tp = find_thread_ptid (ptid);
  193.   if (tp)
  194.     /* Found an old thread with the same id.  It has to be dead,
  195.        otherwise we wouldn't be adding a new thread with the same id.
  196.        The OS is reusing this id --- delete it, and recreate a new
  197.        one.  */
  198.     {
  199.       /* In addition to deleting the thread, if this is the current
  200.          thread, then we need to take care that delete_thread doesn't
  201.          really delete the thread if it is inferior_ptid.  Create a
  202.          new template thread in the list with an invalid ptid, switch
  203.          to it, delete the original thread, reset the new thread's
  204.          ptid, and switch to it.  */

  205.       if (ptid_equal (inferior_ptid, ptid))
  206.         {
  207.           tp = new_thread (null_ptid);

  208.           /* Make switch_to_thread not read from the thread.  */
  209.           tp->state = THREAD_EXITED;
  210.           switch_to_thread (null_ptid);

  211.           /* Now we can delete it.  */
  212.           delete_thread (ptid);

  213.           /* Now reset its ptid, and reswitch inferior_ptid to it.  */
  214.           tp->ptid = ptid;
  215.           tp->state = THREAD_STOPPED;
  216.           switch_to_thread (ptid);

  217.           observer_notify_new_thread (tp);

  218.           /* All done.  */
  219.           return tp;
  220.         }
  221.       else
  222.         /* Just go ahead and delete it.  */
  223.         delete_thread (ptid);
  224.     }

  225.   tp = new_thread (ptid);
  226.   observer_notify_new_thread (tp);

  227.   return tp;
  228. }

  229. struct thread_info *
  230. add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
  231. {
  232.   struct thread_info *result = add_thread_silent (ptid);

  233.   result->private = private;

  234.   if (print_thread_events)
  235.     printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));

  236.   annotate_new_thread ();
  237.   return result;
  238. }

  239. struct thread_info *
  240. add_thread (ptid_t ptid)
  241. {
  242.   return add_thread_with_info (ptid, NULL);
  243. }

  244. /* Delete thread PTID.  If SILENT, don't notify the observer of this
  245.    exit.  */
  246. static void
  247. delete_thread_1 (ptid_t ptid, int silent)
  248. {
  249.   struct thread_info *tp, *tpprev;

  250.   tpprev = NULL;

  251.   for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
  252.     if (ptid_equal (tp->ptid, ptid))
  253.       break;

  254.   if (!tp)
  255.     return;

  256.   /* If this is the current thread, or there's code out there that
  257.      relies on it existing (refcount > 0) we can't delete yet.  Mark
  258.      it as exited, and notify it.  */
  259.   if (tp->refcount > 0
  260.       || ptid_equal (tp->ptid, inferior_ptid))
  261.     {
  262.       if (tp->state != THREAD_EXITED)
  263.         {
  264.           observer_notify_thread_exit (tp, silent);

  265.           /* Tag it as exited.  */
  266.           tp->state = THREAD_EXITED;

  267.           /* Clear breakpoints, etc. associated with this thread.  */
  268.           clear_thread_inferior_resources (tp);
  269.         }

  270.        /* Will be really deleted some other time.  */
  271.        return;
  272.      }

  273.   /* Notify thread exit, but only if we haven't already.  */
  274.   if (tp->state != THREAD_EXITED)
  275.     observer_notify_thread_exit (tp, silent);

  276.   /* Tag it as exited.  */
  277.   tp->state = THREAD_EXITED;
  278.   clear_thread_inferior_resources (tp);

  279.   if (tpprev)
  280.     tpprev->next = tp->next;
  281.   else
  282.     thread_list = tp->next;

  283.   free_thread (tp);
  284. }

  285. /* Delete thread PTID and notify of thread exit.  If this is
  286.    inferior_ptid, don't actually delete it, but tag it as exited and
  287.    do the notification.  If PTID is the user selected thread, clear
  288.    it.  */
  289. void
  290. delete_thread (ptid_t ptid)
  291. {
  292.   delete_thread_1 (ptid, 0 /* not silent */);
  293. }

  294. void
  295. delete_thread_silent (ptid_t ptid)
  296. {
  297.   delete_thread_1 (ptid, 1 /* silent */);
  298. }

  299. struct thread_info *
  300. find_thread_id (int num)
  301. {
  302.   struct thread_info *tp;

  303.   for (tp = thread_list; tp; tp = tp->next)
  304.     if (tp->num == num)
  305.       return tp;

  306.   return NULL;
  307. }

  308. /* Find a thread_info by matching PTID.  */
  309. struct thread_info *
  310. find_thread_ptid (ptid_t ptid)
  311. {
  312.   struct thread_info *tp;

  313.   for (tp = thread_list; tp; tp = tp->next)
  314.     if (ptid_equal (tp->ptid, ptid))
  315.       return tp;

  316.   return NULL;
  317. }

  318. /*
  319. * Thread iterator function.
  320. *
  321. * Calls a callback function once for each thread, so long as
  322. * the callback function returns false.  If the callback function
  323. * returns true, the iteration will end and the current thread
  324. * will be returned.  This can be useful for implementing a
  325. * search for a thread with arbitrary attributes, or for applying
  326. * some operation to every thread.
  327. *
  328. * FIXME: some of the existing functionality, such as
  329. * "Thread apply all", might be rewritten using this functionality.
  330. */

  331. struct thread_info *
  332. iterate_over_threads (int (*callback) (struct thread_info *, void *),
  333.                       void *data)
  334. {
  335.   struct thread_info *tp, *next;

  336.   for (tp = thread_list; tp; tp = next)
  337.     {
  338.       next = tp->next;
  339.       if ((*callback) (tp, data))
  340.         return tp;
  341.     }

  342.   return NULL;
  343. }

  344. int
  345. thread_count (void)
  346. {
  347.   int result = 0;
  348.   struct thread_info *tp;

  349.   for (tp = thread_list; tp; tp = tp->next)
  350.     ++result;

  351.   return result;
  352. }

  353. int
  354. valid_thread_id (int num)
  355. {
  356.   struct thread_info *tp;

  357.   for (tp = thread_list; tp; tp = tp->next)
  358.     if (tp->num == num)
  359.       return 1;

  360.   return 0;
  361. }

  362. int
  363. pid_to_thread_id (ptid_t ptid)
  364. {
  365.   struct thread_info *tp;

  366.   for (tp = thread_list; tp; tp = tp->next)
  367.     if (ptid_equal (tp->ptid, ptid))
  368.       return tp->num;

  369.   return 0;
  370. }

  371. ptid_t
  372. thread_id_to_pid (int num)
  373. {
  374.   struct thread_info *thread = find_thread_id (num);

  375.   if (thread)
  376.     return thread->ptid;
  377.   else
  378.     return pid_to_ptid (-1);
  379. }

  380. int
  381. in_thread_list (ptid_t ptid)
  382. {
  383.   struct thread_info *tp;

  384.   for (tp = thread_list; tp; tp = tp->next)
  385.     if (ptid_equal (tp->ptid, ptid))
  386.       return 1;

  387.   return 0;                        /* Never heard of 'im.  */
  388. }

  389. /* Finds the first thread of the inferior given by PID.  If PID is -1,
  390.    return the first thread in the list.  */

  391. struct thread_info *
  392. first_thread_of_process (int pid)
  393. {
  394.   struct thread_info *tp, *ret = NULL;

  395.   for (tp = thread_list; tp; tp = tp->next)
  396.     if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
  397.       if (ret == NULL || tp->num < ret->num)
  398.         ret = tp;

  399.   return ret;
  400. }

  401. struct thread_info *
  402. any_thread_of_process (int pid)
  403. {
  404.   struct thread_info *tp;

  405.   gdb_assert (pid != 0);

  406.   /* Prefer the current thread.  */
  407.   if (ptid_get_pid (inferior_ptid) == pid)
  408.     return inferior_thread ();

  409.   ALL_NON_EXITED_THREADS (tp)
  410.     if (ptid_get_pid (tp->ptid) == pid)
  411.       return tp;

  412.   return NULL;
  413. }

  414. struct thread_info *
  415. any_live_thread_of_process (int pid)
  416. {
  417.   struct thread_info *curr_tp = NULL;
  418.   struct thread_info *tp;
  419.   struct thread_info *tp_executing = NULL;

  420.   gdb_assert (pid != 0);

  421.   /* Prefer the current thread if it's not executing.  */
  422.   if (ptid_get_pid (inferior_ptid) == pid)
  423.     {
  424.       /* If the current thread is dead, forget it.  If it's not
  425.          executing, use it.  Otherwise, still choose it (below), but
  426.          only if no other non-executing thread is found.  */
  427.       curr_tp = inferior_thread ();
  428.       if (curr_tp->state == THREAD_EXITED)
  429.         curr_tp = NULL;
  430.       else if (!curr_tp->executing)
  431.         return curr_tp;
  432.     }

  433.   ALL_NON_EXITED_THREADS (tp)
  434.     if (ptid_get_pid (tp->ptid) == pid)
  435.       {
  436.         if (!tp->executing)
  437.           return tp;

  438.         tp_executing = tp;
  439.       }

  440.   /* If both the current thread and all live threads are executing,
  441.      prefer the current thread.  */
  442.   if (curr_tp != NULL)
  443.     return curr_tp;

  444.   /* Otherwise, just return an executing thread, if any.  */
  445.   return tp_executing;
  446. }

  447. /* Print a list of thread ids currently known, and the total number of
  448.    threads.  To be used from within catch_errors.  */
  449. static int
  450. do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
  451. {
  452.   struct thread_info *tp;
  453.   int num = 0;
  454.   struct cleanup *cleanup_chain;
  455.   int current_thread = -1;

  456.   update_thread_list ();

  457.   cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");

  458.   for (tp = thread_list; tp; tp = tp->next)
  459.     {
  460.       if (tp->state == THREAD_EXITED)
  461.         continue;

  462.       if (ptid_equal (tp->ptid, inferior_ptid))
  463.         current_thread = tp->num;

  464.       num++;
  465.       ui_out_field_int (uiout, "thread-id", tp->num);
  466.     }

  467.   do_cleanups (cleanup_chain);

  468.   if (current_thread != -1)
  469.     ui_out_field_int (uiout, "current-thread-id", current_thread);
  470.   ui_out_field_int (uiout, "number-of-threads", num);
  471.   return GDB_RC_OK;
  472. }

  473. /* Official gdblib interface function to get a list of thread ids and
  474.    the total number.  */
  475. enum gdb_rc
  476. gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
  477. {
  478.   if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
  479.                                  error_message, RETURN_MASK_ALL) < 0)
  480.     return GDB_RC_FAIL;
  481.   return GDB_RC_OK;
  482. }

  483. /* Return true if TP is an active thread.  */
  484. static int
  485. thread_alive (struct thread_info *tp)
  486. {
  487.   if (tp->state == THREAD_EXITED)
  488.     return 0;
  489.   if (!target_thread_alive (tp->ptid))
  490.     return 0;
  491.   return 1;
  492. }

  493. /* See gdbthreads.h.  */

  494. void
  495. prune_threads (void)
  496. {
  497.   struct thread_info *tp, *next;

  498.   for (tp = thread_list; tp; tp = next)
  499.     {
  500.       next = tp->next;
  501.       if (!thread_alive (tp))
  502.         delete_thread (tp->ptid);
  503.     }
  504. }

  505. /* Disable storing stack temporaries for the thread whose id is
  506.    stored in DATA.  */

  507. static void
  508. disable_thread_stack_temporaries (void *data)
  509. {
  510.   ptid_t *pd = data;
  511.   struct thread_info *tp = find_thread_ptid (*pd);

  512.   if (tp != NULL)
  513.     {
  514.       tp->stack_temporaries_enabled = 0;
  515.       VEC_free (value_ptr, tp->stack_temporaries);
  516.     }

  517.   xfree (pd);
  518. }

  519. /* Enable storing stack temporaries for thread with id PTID and return a
  520.    cleanup which can disable and clear the stack temporaries.  */

  521. struct cleanup *
  522. enable_thread_stack_temporaries (ptid_t ptid)
  523. {
  524.   struct thread_info *tp = find_thread_ptid (ptid);
  525.   ptid_t  *data;
  526.   struct cleanup *c;

  527.   gdb_assert (tp != NULL);

  528.   tp->stack_temporaries_enabled = 1;
  529.   tp->stack_temporaries = NULL;
  530.   data = (ptid_t *) xmalloc (sizeof (ptid_t));
  531.   *data = ptid;
  532.   c = make_cleanup (disable_thread_stack_temporaries, data);

  533.   return c;
  534. }

  535. /* Return non-zero value if stack temporaies are enabled for the thread
  536.    with id PTID.  */

  537. int
  538. thread_stack_temporaries_enabled_p (ptid_t ptid)
  539. {
  540.   struct thread_info *tp = find_thread_ptid (ptid);

  541.   if (tp == NULL)
  542.     return 0;
  543.   else
  544.     return tp->stack_temporaries_enabled;
  545. }

  546. /* Push V on to the stack temporaries of the thread with id PTID.  */

  547. void
  548. push_thread_stack_temporary (ptid_t ptid, struct value *v)
  549. {
  550.   struct thread_info *tp = find_thread_ptid (ptid);

  551.   gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
  552.   VEC_safe_push (value_ptr, tp->stack_temporaries, v);
  553. }

  554. /* Return 1 if VAL is among the stack temporaries of the thread
  555.    with id PTID.  Return 0 otherwise.  */

  556. int
  557. value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
  558. {
  559.   struct thread_info *tp = find_thread_ptid (ptid);

  560.   gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
  561.   if (!VEC_empty (value_ptr, tp->stack_temporaries))
  562.     {
  563.       struct value *v;
  564.       int i;

  565.       for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
  566.         if (v == val)
  567.           return 1;
  568.     }

  569.   return 0;
  570. }

  571. /* Return the last of the stack temporaries for thread with id PTID.
  572.    Return NULL if there are no stack temporaries for the thread.  */

  573. struct value *
  574. get_last_thread_stack_temporary (ptid_t ptid)
  575. {
  576.   struct value *lastval = NULL;
  577.   struct thread_info *tp = find_thread_ptid (ptid);

  578.   gdb_assert (tp != NULL);
  579.   if (!VEC_empty (value_ptr, tp->stack_temporaries))
  580.     lastval = VEC_last (value_ptr, tp->stack_temporaries);

  581.   return lastval;
  582. }

  583. void
  584. thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
  585. {
  586.   struct inferior *inf;
  587.   struct thread_info *tp;

  588.   /* It can happen that what we knew as the target inferior id
  589.      changes.  E.g, target remote may only discover the remote process
  590.      pid after adding the inferior to GDB's list.  */
  591.   inf = find_inferior_ptid (old_ptid);
  592.   inf->pid = ptid_get_pid (new_ptid);

  593.   tp = find_thread_ptid (old_ptid);
  594.   tp->ptid = new_ptid;

  595.   observer_notify_thread_ptid_changed (old_ptid, new_ptid);
  596. }

  597. void
  598. set_running (ptid_t ptid, int running)
  599. {
  600.   struct thread_info *tp;
  601.   int all = ptid_equal (ptid, minus_one_ptid);

  602.   /* We try not to notify the observer if no thread has actually changed
  603.      the running state -- merely to reduce the number of messages to
  604.      frontend.  Frontend is supposed to handle multiple *running just fine.  */
  605.   if (all || ptid_is_pid (ptid))
  606.     {
  607.       int any_started = 0;

  608.       for (tp = thread_list; tp; tp = tp->next)
  609.         if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
  610.           {
  611.             if (tp->state == THREAD_EXITED)
  612.               continue;
  613.             if (running && tp->state == THREAD_STOPPED)
  614.               any_started = 1;
  615.             tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
  616.           }
  617.       if (any_started)
  618.         observer_notify_target_resumed (ptid);
  619.     }
  620.   else
  621.     {
  622.       int started = 0;

  623.       tp = find_thread_ptid (ptid);
  624.       gdb_assert (tp);
  625.       gdb_assert (tp->state != THREAD_EXITED);
  626.       if (running && tp->state == THREAD_STOPPED)
  627.          started = 1;
  628.       tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
  629.       if (started)
  630.           observer_notify_target_resumed (ptid);
  631.     }
  632. }

  633. static int
  634. is_thread_state (ptid_t ptid, enum thread_state state)
  635. {
  636.   struct thread_info *tp;

  637.   tp = find_thread_ptid (ptid);
  638.   gdb_assert (tp);
  639.   return tp->state == state;
  640. }

  641. int
  642. is_stopped (ptid_t ptid)
  643. {
  644.   return is_thread_state (ptid, THREAD_STOPPED);
  645. }

  646. int
  647. is_exited (ptid_t ptid)
  648. {
  649.   return is_thread_state (ptid, THREAD_EXITED);
  650. }

  651. int
  652. is_running (ptid_t ptid)
  653. {
  654.   return is_thread_state (ptid, THREAD_RUNNING);
  655. }

  656. int
  657. is_executing (ptid_t ptid)
  658. {
  659.   struct thread_info *tp;

  660.   tp = find_thread_ptid (ptid);
  661.   gdb_assert (tp);
  662.   return tp->executing;
  663. }

  664. void
  665. set_executing (ptid_t ptid, int executing)
  666. {
  667.   struct thread_info *tp;
  668.   int all = ptid_equal (ptid, minus_one_ptid);

  669.   if (all || ptid_is_pid (ptid))
  670.     {
  671.       for (tp = thread_list; tp; tp = tp->next)
  672.         if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
  673.           tp->executing = executing;
  674.     }
  675.   else
  676.     {
  677.       tp = find_thread_ptid (ptid);
  678.       gdb_assert (tp);
  679.       tp->executing = executing;
  680.     }

  681.   /* It only takes one running thread to spawn more threads.*/
  682.   if (executing)
  683.     threads_executing = 1;
  684.   /* Only clear the flag if the caller is telling us everything is
  685.      stopped.  */
  686.   else if (ptid_equal (minus_one_ptid, ptid))
  687.     threads_executing = 0;
  688. }

  689. /* See gdbthread.h.  */

  690. int
  691. threads_are_executing (void)
  692. {
  693.   return threads_executing;
  694. }

  695. void
  696. set_stop_requested (ptid_t ptid, int stop)
  697. {
  698.   struct thread_info *tp;
  699.   int all = ptid_equal (ptid, minus_one_ptid);

  700.   if (all || ptid_is_pid (ptid))
  701.     {
  702.       for (tp = thread_list; tp; tp = tp->next)
  703.         if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
  704.           tp->stop_requested = stop;
  705.     }
  706.   else
  707.     {
  708.       tp = find_thread_ptid (ptid);
  709.       gdb_assert (tp);
  710.       tp->stop_requested = stop;
  711.     }

  712.   /* Call the stop requested observer so other components of GDB can
  713.      react to this request.  */
  714.   if (stop)
  715.     observer_notify_thread_stop_requested (ptid);
  716. }

  717. void
  718. finish_thread_state (ptid_t ptid)
  719. {
  720.   struct thread_info *tp;
  721.   int all;
  722.   int any_started = 0;

  723.   all = ptid_equal (ptid, minus_one_ptid);

  724.   if (all || ptid_is_pid (ptid))
  725.     {
  726.       for (tp = thread_list; tp; tp = tp->next)
  727.         {
  728.            if (tp->state == THREAD_EXITED)
  729.               continue;
  730.           if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
  731.             {
  732.               if (tp->executing && tp->state == THREAD_STOPPED)
  733.                 any_started = 1;
  734.               tp->state = tp->executing ? THREAD_RUNNING : THREAD_STOPPED;
  735.             }
  736.         }
  737.     }
  738.   else
  739.     {
  740.       tp = find_thread_ptid (ptid);
  741.       gdb_assert (tp);
  742.       if (tp->state != THREAD_EXITED)
  743.         {
  744.           if (tp->executing && tp->state == THREAD_STOPPED)
  745.             any_started = 1;
  746.           tp->state = tp->executing ? THREAD_RUNNING : THREAD_STOPPED;
  747.         }
  748.     }

  749.   if (any_started)
  750.     observer_notify_target_resumed (ptid);
  751. }

  752. void
  753. finish_thread_state_cleanup (void *arg)
  754. {
  755.   ptid_t *ptid_p = arg;

  756.   gdb_assert (arg);

  757.   finish_thread_state (*ptid_p);
  758. }

  759. int
  760. pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
  761. {
  762.   return (pc >= thread->control.step_range_start
  763.           && pc < thread->control.step_range_end);
  764. }

  765. /* Prints the list of threads and their details on UIOUT.
  766.    This is a version of 'info_threads_command' suitable for
  767.    use from MI.
  768.    If REQUESTED_THREAD is not -1, it's the GDB id of the thread
  769.    that should be printed.  Otherwise, all threads are
  770.    printed.
  771.    If PID is not -1, only print threads from the process PID.
  772.    Otherwise, threads from all attached PIDs are printed.
  773.    If both REQUESTED_THREAD and PID are not -1, then the thread
  774.    is printed if it belongs to the specified process.  Otherwise,
  775.    an error is raised.  */
  776. void
  777. print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
  778. {
  779.   struct thread_info *tp;
  780.   ptid_t current_ptid;
  781.   struct cleanup *old_chain;
  782.   char *extra_info, *name, *target_id;
  783.   int current_thread = -1;

  784.   update_thread_list ();
  785.   current_ptid = inferior_ptid;

  786.   /* We'll be switching threads temporarily.  */
  787.   old_chain = make_cleanup_restore_current_thread ();

  788.   /* For backward compatibility, we make a list for MI.  A table is
  789.      preferable for the CLI, though, because it shows table
  790.      headers.  */
  791.   if (ui_out_is_mi_like_p (uiout))
  792.     make_cleanup_ui_out_list_begin_end (uiout, "threads");
  793.   else
  794.     {
  795.       int n_threads = 0;

  796.       for (tp = thread_list; tp; tp = tp->next)
  797.         {
  798.           if (!number_is_in_list (requested_threads, tp->num))
  799.             continue;

  800.           if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
  801.             continue;

  802.           if (tp->state == THREAD_EXITED)
  803.             continue;

  804.           ++n_threads;
  805.         }

  806.       if (n_threads == 0)
  807.         {
  808.           if (requested_threads == NULL || *requested_threads == '\0')
  809.             ui_out_message (uiout, 0, _("No threads.\n"));
  810.           else
  811.             ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
  812.                             requested_threads);
  813.           do_cleanups (old_chain);
  814.           return;
  815.         }

  816.       make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");

  817.       ui_out_table_header (uiout, 1, ui_left, "current", "");
  818.       ui_out_table_header (uiout, 4, ui_left, "id", "Id");
  819.       ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
  820.       ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
  821.       ui_out_table_body (uiout);
  822.     }

  823.   for (tp = thread_list; tp; tp = tp->next)
  824.     {
  825.       struct cleanup *chain2;
  826.       int core;

  827.       if (!number_is_in_list (requested_threads, tp->num))
  828.         continue;

  829.       if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
  830.         {
  831.           if (requested_threads != NULL && *requested_threads != '\0')
  832.             error (_("Requested thread not found in requested process"));
  833.           continue;
  834.         }

  835.       if (ptid_equal (tp->ptid, current_ptid))
  836.         current_thread = tp->num;

  837.       if (tp->state == THREAD_EXITED)
  838.         continue;

  839.       chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  840.       if (ui_out_is_mi_like_p (uiout))
  841.         {
  842.           /* Compatibility.  */
  843.           if (ptid_equal (tp->ptid, current_ptid))
  844.             ui_out_text (uiout, "* ");
  845.           else
  846.             ui_out_text (uiout, "  ");
  847.         }
  848.       else
  849.         {
  850.           if (ptid_equal (tp->ptid, current_ptid))
  851.             ui_out_field_string (uiout, "current", "*");
  852.           else
  853.             ui_out_field_skip (uiout, "current");
  854.         }

  855.       ui_out_field_int (uiout, "id", tp->num);

  856.       /* For the CLI, we stuff everything into the target-id field.
  857.          This is a gross hack to make the output come out looking
  858.          correct.  The underlying problem here is that ui-out has no
  859.          way to specify that a field's space allocation should be
  860.          shared by several fields.  For MI, we do the right thing
  861.          instead.  */

  862.       target_id = target_pid_to_str (tp->ptid);
  863.       extra_info = target_extra_thread_info (tp);
  864.       name = tp->name ? tp->name : target_thread_name (tp);

  865.       if (ui_out_is_mi_like_p (uiout))
  866.         {
  867.           ui_out_field_string (uiout, "target-id", target_id);
  868.           if (extra_info)
  869.             ui_out_field_string (uiout, "details", extra_info);
  870.           if (name)
  871.             ui_out_field_string (uiout, "name", name);
  872.         }
  873.       else
  874.         {
  875.           struct cleanup *str_cleanup;
  876.           char *contents;

  877.           if (extra_info && name)
  878.             contents = xstrprintf ("%s \"%s\" (%s)", target_id,
  879.                                    name, extra_info);
  880.           else if (extra_info)
  881.             contents = xstrprintf ("%s (%s)", target_id, extra_info);
  882.           else if (name)
  883.             contents = xstrprintf ("%s \"%s\"", target_id, name);
  884.           else
  885.             contents = xstrdup (target_id);
  886.           str_cleanup = make_cleanup (xfree, contents);

  887.           ui_out_field_string (uiout, "target-id", contents);
  888.           do_cleanups (str_cleanup);
  889.         }

  890.       if (tp->state == THREAD_RUNNING)
  891.         ui_out_text (uiout, "(running)\n");
  892.       else
  893.         {
  894.           /* The switch below puts us at the top of the stack (leaf
  895.              frame).  */
  896.           switch_to_thread (tp->ptid);
  897.           print_stack_frame (get_selected_frame (NULL),
  898.                              /* For MI output, print frame level.  */
  899.                              ui_out_is_mi_like_p (uiout),
  900.                              LOCATION, 0);
  901.         }

  902.       if (ui_out_is_mi_like_p (uiout))
  903.         {
  904.           char *state = "stopped";

  905.           if (tp->state == THREAD_RUNNING)
  906.             state = "running";
  907.           ui_out_field_string (uiout, "state", state);
  908.         }

  909.       core = target_core_of_thread (tp->ptid);
  910.       if (ui_out_is_mi_like_p (uiout) && core != -1)
  911.         ui_out_field_int (uiout, "core", core);

  912.       do_cleanups (chain2);
  913.     }

  914.   /* Restores the current thread and the frame selected before
  915.      the "info threads" command.  */
  916.   do_cleanups (old_chain);

  917.   if (pid == -1 && requested_threads == NULL)
  918.     {
  919.       gdb_assert (current_thread != -1
  920.                   || !thread_list
  921.                   || ptid_equal (inferior_ptid, null_ptid));
  922.       if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
  923.         ui_out_field_int (uiout, "current-thread-id", current_thread);

  924.       if (current_thread != -1 && is_exited (current_ptid))
  925.         ui_out_message (uiout, 0, "\n\
  926. The current thread <Thread ID %d> has terminated.  See `help thread'.\n",
  927.                         current_thread);
  928.       else if (thread_list
  929.                && current_thread == -1
  930.                && ptid_equal (current_ptid, null_ptid))
  931.         ui_out_message (uiout, 0, "\n\
  932. No selected thread.  See `help thread'.\n");
  933.     }
  934. }

  935. /* Print information about currently known threads

  936.    Optional ARG is a thread id, or list of thread ids.

  937.    Note: this has the drawback that it _really_ switches
  938.          threads, which frees the frame cache.  A no-side
  939.          effects info-threads command would be nicer.  */

  940. static void
  941. info_threads_command (char *arg, int from_tty)
  942. {
  943.   print_thread_info (current_uiout, arg, -1);
  944. }

  945. /* Switch from one thread to another.  */

  946. void
  947. switch_to_thread (ptid_t ptid)
  948. {
  949.   /* Switch the program space as well, if we can infer it from the now
  950.      current thread.  Otherwise, it's up to the caller to select the
  951.      space it wants.  */
  952.   if (!ptid_equal (ptid, null_ptid))
  953.     {
  954.       struct inferior *inf;

  955.       inf = find_inferior_ptid (ptid);
  956.       gdb_assert (inf != NULL);
  957.       set_current_program_space (inf->pspace);
  958.       set_current_inferior (inf);
  959.     }

  960.   if (ptid_equal (ptid, inferior_ptid))
  961.     return;

  962.   inferior_ptid = ptid;
  963.   reinit_frame_cache ();

  964.   /* We don't check for is_stopped, because we're called at times
  965.      while in the TARGET_RUNNING state, e.g., while handling an
  966.      internal event.  */
  967.   if (!ptid_equal (inferior_ptid, null_ptid)
  968.       && !is_exited (ptid)
  969.       && !is_executing (ptid))
  970.     stop_pc = regcache_read_pc (get_thread_regcache (ptid));
  971.   else
  972.     stop_pc = ~(CORE_ADDR) 0;
  973. }

  974. static void
  975. restore_current_thread (ptid_t ptid)
  976. {
  977.   switch_to_thread (ptid);
  978. }

  979. static void
  980. restore_selected_frame (struct frame_id a_frame_id, int frame_level)
  981. {
  982.   struct frame_info *frame = NULL;
  983.   int count;

  984.   /* This means there was no selected frame.  */
  985.   if (frame_level == -1)
  986.     {
  987.       select_frame (NULL);
  988.       return;
  989.     }

  990.   gdb_assert (frame_level >= 0);

  991.   /* Restore by level first, check if the frame id is the same as
  992.      expected.  If that fails, try restoring by frame id.  If that
  993.      fails, nothing to do, just warn the user.  */

  994.   count = frame_level;
  995.   frame = find_relative_frame (get_current_frame (), &count);
  996.   if (count == 0
  997.       && frame != NULL
  998.       /* The frame ids must match - either both valid or both outer_frame_id.
  999.          The latter case is not failsafe, but since it's highly unlikely
  1000.          the search by level finds the wrong frame, it's 99.9(9)% of
  1001.          the time (for all practical purposes) safe.  */
  1002.       && frame_id_eq (get_frame_id (frame), a_frame_id))
  1003.     {
  1004.       /* Cool, all is fine.  */
  1005.       select_frame (frame);
  1006.       return;
  1007.     }

  1008.   frame = frame_find_by_id (a_frame_id);
  1009.   if (frame != NULL)
  1010.     {
  1011.       /* Cool, refound it.  */
  1012.       select_frame (frame);
  1013.       return;
  1014.     }

  1015.   /* Nothing else to do, the frame layout really changed.  Select the
  1016.      innermost stack frame.  */
  1017.   select_frame (get_current_frame ());

  1018.   /* Warn the user.  */
  1019.   if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
  1020.     {
  1021.       warning (_("Couldn't restore frame #%d in "
  1022.                  "current thread.  Bottom (innermost) frame selected:"),
  1023.                frame_level);
  1024.       /* For MI, we should probably have a notification about
  1025.          current frame change.  But this error is not very
  1026.          likely, so don't bother for now.  */
  1027.       print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  1028.     }
  1029. }

  1030. struct current_thread_cleanup
  1031. {
  1032.   ptid_t inferior_ptid;
  1033.   struct frame_id selected_frame_id;
  1034.   int selected_frame_level;
  1035.   int was_stopped;
  1036.   int inf_id;
  1037.   int was_removable;
  1038. };

  1039. static void
  1040. do_restore_current_thread_cleanup (void *arg)
  1041. {
  1042.   struct thread_info *tp;
  1043.   struct current_thread_cleanup *old = arg;

  1044.   tp = find_thread_ptid (old->inferior_ptid);

  1045.   /* If the previously selected thread belonged to a process that has
  1046.      in the mean time been deleted (due to normal exit, detach, etc.),
  1047.      then don't revert back to it, but instead simply drop back to no
  1048.      thread selected.  */
  1049.   if (tp
  1050.       && find_inferior_ptid (tp->ptid) != NULL)
  1051.     restore_current_thread (old->inferior_ptid);
  1052.   else
  1053.     {
  1054.       restore_current_thread (null_ptid);
  1055.       set_current_inferior (find_inferior_id (old->inf_id));
  1056.     }

  1057.   /* The running state of the originally selected thread may have
  1058.      changed, so we have to recheck it here.  */
  1059.   if (!ptid_equal (inferior_ptid, null_ptid)
  1060.       && old->was_stopped
  1061.       && is_stopped (inferior_ptid)
  1062.       && target_has_registers
  1063.       && target_has_stack
  1064.       && target_has_memory)
  1065.     restore_selected_frame (old->selected_frame_id,
  1066.                             old->selected_frame_level);
  1067. }

  1068. static void
  1069. restore_current_thread_cleanup_dtor (void *arg)
  1070. {
  1071.   struct current_thread_cleanup *old = arg;
  1072.   struct thread_info *tp;
  1073.   struct inferior *inf;

  1074.   tp = find_thread_ptid (old->inferior_ptid);
  1075.   if (tp)
  1076.     tp->refcount--;
  1077.   inf = find_inferior_id (old->inf_id);
  1078.   if (inf != NULL)
  1079.     inf->removable = old->was_removable;
  1080.   xfree (old);
  1081. }

  1082. /* Set the thread reference count.  */

  1083. static void
  1084. set_thread_refcount (void *data)
  1085. {
  1086.   int k;
  1087.   struct thread_array_cleanup *ta_cleanup = data;

  1088.   for (k = 0; k != ta_cleanup->count; k++)
  1089.     ta_cleanup->tp_array[k]->refcount--;
  1090. }

  1091. struct cleanup *
  1092. make_cleanup_restore_current_thread (void)
  1093. {
  1094.   struct thread_info *tp;
  1095.   struct frame_info *frame;
  1096.   struct current_thread_cleanup *old;

  1097.   old = xmalloc (sizeof (struct current_thread_cleanup));
  1098.   old->inferior_ptid = inferior_ptid;
  1099.   old->inf_id = current_inferior ()->num;
  1100.   old->was_removable = current_inferior ()->removable;

  1101.   if (!ptid_equal (inferior_ptid, null_ptid))
  1102.     {
  1103.       old->was_stopped = is_stopped (inferior_ptid);
  1104.       if (old->was_stopped
  1105.           && target_has_registers
  1106.           && target_has_stack
  1107.           && target_has_memory)
  1108.         {
  1109.           /* When processing internal events, there might not be a
  1110.              selected frame.  If we naively call get_selected_frame
  1111.              here, then we can end up reading debuginfo for the
  1112.              current frame, but we don't generally need the debuginfo
  1113.              at this point.  */
  1114.           frame = get_selected_frame_if_set ();
  1115.         }
  1116.       else
  1117.         frame = NULL;

  1118.       old->selected_frame_id = get_frame_id (frame);
  1119.       old->selected_frame_level = frame_relative_level (frame);

  1120.       tp = find_thread_ptid (inferior_ptid);
  1121.       if (tp)
  1122.         tp->refcount++;
  1123.     }

  1124.   current_inferior ()->removable = 0;

  1125.   return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
  1126.                             restore_current_thread_cleanup_dtor);
  1127. }

  1128. /* Apply a GDB command to a list of threads.  List syntax is a whitespace
  1129.    seperated list of numbers, or ranges, or the keyword `all'.  Ranges consist
  1130.    of two numbers seperated by a hyphen.  Examples:

  1131.    thread apply 1 2 7 4 backtrace       Apply backtrace cmd to threads 1,2,7,4
  1132.    thread apply 2-7 9 p foo(1)  Apply p foo(1) cmd to threads 2->7 & 9
  1133.    thread apply all p x/i $pc   Apply x/i $pc cmd to all threads.  */

  1134. static void
  1135. thread_apply_all_command (char *cmd, int from_tty)
  1136. {
  1137.   struct cleanup *old_chain;
  1138.   char *saved_cmd;
  1139.   int tc;
  1140.   struct thread_array_cleanup ta_cleanup;

  1141.   if (cmd == NULL || *cmd == '\000')
  1142.     error (_("Please specify a command following the thread ID list"));

  1143.   update_thread_list ();

  1144.   old_chain = make_cleanup_restore_current_thread ();

  1145.   /* Save a copy of the command in case it is clobbered by
  1146.      execute_command.  */
  1147.   saved_cmd = xstrdup (cmd);
  1148.   make_cleanup (xfree, saved_cmd);
  1149.   tc = thread_count ();

  1150.   if (tc)
  1151.     {
  1152.       struct thread_info **tp_array;
  1153.       struct thread_info *tp;
  1154.       int i = 0, k;

  1155.       /* Save a copy of the thread_list in case we execute detach
  1156.          command.  */
  1157.       tp_array = xmalloc (sizeof (struct thread_info *) * tc);
  1158.       make_cleanup (xfree, tp_array);
  1159.       ta_cleanup.tp_array = tp_array;
  1160.       ta_cleanup.count = tc;

  1161.       ALL_NON_EXITED_THREADS (tp)
  1162.         {
  1163.           tp_array[i] = tp;
  1164.           tp->refcount++;
  1165.           i++;
  1166.         }

  1167.       make_cleanup (set_thread_refcount, &ta_cleanup);

  1168.       for (k = 0; k != i; k++)
  1169.         if (thread_alive (tp_array[k]))
  1170.           {
  1171.             switch_to_thread (tp_array[k]->ptid);
  1172.             printf_filtered (_("\nThread %d (%s):\n"),
  1173.                              tp_array[k]->num,
  1174.                              target_pid_to_str (inferior_ptid));
  1175.             execute_command (cmd, from_tty);

  1176.             /* Restore exact command used previously.  */
  1177.             strcpy (cmd, saved_cmd);
  1178.           }
  1179.     }

  1180.   do_cleanups (old_chain);
  1181. }

  1182. static void
  1183. thread_apply_command (char *tidlist, int from_tty)
  1184. {
  1185.   char *cmd;
  1186.   struct cleanup *old_chain;
  1187.   char *saved_cmd;
  1188.   struct get_number_or_range_state state;

  1189.   if (tidlist == NULL || *tidlist == '\000')
  1190.     error (_("Please specify a thread ID list"));

  1191.   for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);

  1192.   if (*cmd == '\000')
  1193.     error (_("Please specify a command following the thread ID list"));

  1194.   /* Save a copy of the command in case it is clobbered by
  1195.      execute_command.  */
  1196.   saved_cmd = xstrdup (cmd);
  1197.   old_chain = make_cleanup (xfree, saved_cmd);

  1198.   init_number_or_range (&state, tidlist);
  1199.   while (!state.finished && state.string < cmd)
  1200.     {
  1201.       struct thread_info *tp;
  1202.       int start;

  1203.       start = get_number_or_range (&state);

  1204.       make_cleanup_restore_current_thread ();

  1205.       tp = find_thread_id (start);

  1206.       if (!tp)
  1207.         warning (_("Unknown thread %d."), start);
  1208.       else if (!thread_alive (tp))
  1209.         warning (_("Thread %d has terminated."), start);
  1210.       else
  1211.         {
  1212.           switch_to_thread (tp->ptid);

  1213.           printf_filtered (_("\nThread %d (%s):\n"), tp->num,
  1214.                            target_pid_to_str (inferior_ptid));
  1215.           execute_command (cmd, from_tty);

  1216.           /* Restore exact command used previously.  */
  1217.           strcpy (cmd, saved_cmd);
  1218.         }
  1219.     }

  1220.   do_cleanups (old_chain);
  1221. }

  1222. /* Switch to the specified thread.  Will dispatch off to thread_apply_command
  1223.    if prefix of arg is `apply'.  */

  1224. static void
  1225. thread_command (char *tidstr, int from_tty)
  1226. {
  1227.   if (!tidstr)
  1228.     {
  1229.       if (ptid_equal (inferior_ptid, null_ptid))
  1230.         error (_("No thread selected"));

  1231.       if (target_has_stack)
  1232.         {
  1233.           if (is_exited (inferior_ptid))
  1234.             printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
  1235.                              pid_to_thread_id (inferior_ptid),
  1236.                              target_pid_to_str (inferior_ptid));
  1237.           else
  1238.             printf_filtered (_("[Current thread is %d (%s)]\n"),
  1239.                              pid_to_thread_id (inferior_ptid),
  1240.                              target_pid_to_str (inferior_ptid));
  1241.         }
  1242.       else
  1243.         error (_("No stack."));
  1244.       return;
  1245.     }

  1246.   gdb_thread_select (current_uiout, tidstr, NULL);
  1247. }

  1248. /* Implementation of `thread name'.  */

  1249. static void
  1250. thread_name_command (char *arg, int from_tty)
  1251. {
  1252.   struct thread_info *info;

  1253.   if (ptid_equal (inferior_ptid, null_ptid))
  1254.     error (_("No thread selected"));

  1255.   arg = skip_spaces (arg);

  1256.   info = inferior_thread ();
  1257.   xfree (info->name);
  1258.   info->name = arg ? xstrdup (arg) : NULL;
  1259. }

  1260. /* Find thread ids with a name, target pid, or extra info matching ARG.  */

  1261. static void
  1262. thread_find_command (char *arg, int from_tty)
  1263. {
  1264.   struct thread_info *tp;
  1265.   char *tmp;
  1266.   unsigned long match = 0;

  1267.   if (arg == NULL || *arg == '\0')
  1268.     error (_("Command requires an argument."));

  1269.   tmp = re_comp (arg);
  1270.   if (tmp != 0)
  1271.     error (_("Invalid regexp (%s): %s"), tmp, arg);

  1272.   update_thread_list ();
  1273.   for (tp = thread_list; tp; tp = tp->next)
  1274.     {
  1275.       if (tp->name != NULL && re_exec (tp->name))
  1276.         {
  1277.           printf_filtered (_("Thread %d has name '%s'\n"),
  1278.                            tp->num, tp->name);
  1279.           match++;
  1280.         }

  1281.       tmp = target_thread_name (tp);
  1282.       if (tmp != NULL && re_exec (tmp))
  1283.         {
  1284.           printf_filtered (_("Thread %d has target name '%s'\n"),
  1285.                            tp->num, tmp);
  1286.           match++;
  1287.         }

  1288.       tmp = target_pid_to_str (tp->ptid);
  1289.       if (tmp != NULL && re_exec (tmp))
  1290.         {
  1291.           printf_filtered (_("Thread %d has target id '%s'\n"),
  1292.                            tp->num, tmp);
  1293.           match++;
  1294.         }

  1295.       tmp = target_extra_thread_info (tp);
  1296.       if (tmp != NULL && re_exec (tmp))
  1297.         {
  1298.           printf_filtered (_("Thread %d has extra info '%s'\n"),
  1299.                            tp->num, tmp);
  1300.           match++;
  1301.         }
  1302.     }
  1303.   if (!match)
  1304.     printf_filtered (_("No threads match '%s'\n"), arg);
  1305. }

  1306. /* Print notices when new threads are attached and detached.  */
  1307. int print_thread_events = 1;
  1308. static void
  1309. show_print_thread_events (struct ui_file *file, int from_tty,
  1310.                           struct cmd_list_element *c, const char *value)
  1311. {
  1312.   fprintf_filtered (file,
  1313.                     _("Printing of thread events is %s.\n"),
  1314.                     value);
  1315. }

  1316. static int
  1317. do_captured_thread_select (struct ui_out *uiout, void *tidstr)
  1318. {
  1319.   int num;
  1320.   struct thread_info *tp;

  1321.   num = value_as_long (parse_and_eval (tidstr));

  1322.   tp = find_thread_id (num);

  1323.   if (!tp)
  1324.     error (_("Thread ID %d not known."), num);

  1325.   if (!thread_alive (tp))
  1326.     error (_("Thread ID %d has terminated."), num);

  1327.   switch_to_thread (tp->ptid);

  1328.   annotate_thread_changed ();

  1329.   ui_out_text (uiout, "[Switching to thread ");
  1330.   ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
  1331.   ui_out_text (uiout, " (");
  1332.   ui_out_text (uiout, target_pid_to_str (inferior_ptid));
  1333.   ui_out_text (uiout, ")]");

  1334.   /* Note that we can't reach this with an exited thread, due to the
  1335.      thread_alive check above.  */
  1336.   if (tp->state == THREAD_RUNNING)
  1337.     ui_out_text (uiout, "(running)\n");
  1338.   else
  1339.     {
  1340.       ui_out_text (uiout, "\n");
  1341.       print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
  1342.     }

  1343.   /* Since the current thread may have changed, see if there is any
  1344.      exited thread we can now delete.  */
  1345.   prune_threads ();

  1346.   return GDB_RC_OK;
  1347. }

  1348. enum gdb_rc
  1349. gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
  1350. {
  1351.   if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
  1352.                                  error_message, RETURN_MASK_ALL) < 0)
  1353.     return GDB_RC_FAIL;
  1354.   return GDB_RC_OK;
  1355. }

  1356. /* Update the 'threads_executing' global based on the threads we know
  1357.    about right now.  */

  1358. static void
  1359. update_threads_executing (void)
  1360. {
  1361.   struct thread_info *tp;

  1362.   threads_executing = 0;
  1363.   ALL_NON_EXITED_THREADS (tp)
  1364.     {
  1365.       if (tp->executing)
  1366.         {
  1367.           threads_executing = 1;
  1368.           break;
  1369.         }
  1370.     }
  1371. }

  1372. void
  1373. update_thread_list (void)
  1374. {
  1375.   target_update_thread_list ();
  1376.   update_threads_executing ();
  1377. }

  1378. /* Return a new value for the selected thread's id.  Return a value of 0 if
  1379.    no thread is selected, or no threads exist.  */

  1380. static struct value *
  1381. thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
  1382.                       void *ignore)
  1383. {
  1384.   struct thread_info *tp = find_thread_ptid (inferior_ptid);

  1385.   return value_from_longest (builtin_type (gdbarch)->builtin_int,
  1386.                              (tp ? tp->num : 0));
  1387. }

  1388. /* Commands with a prefix of `thread'.  */
  1389. struct cmd_list_element *thread_cmd_list = NULL;

  1390. /* Implementation of `thread' variable.  */

  1391. static const struct internalvar_funcs thread_funcs =
  1392. {
  1393.   thread_id_make_value,
  1394.   NULL,
  1395.   NULL
  1396. };

  1397. void
  1398. _initialize_thread (void)
  1399. {
  1400.   static struct cmd_list_element *thread_apply_list = NULL;

  1401.   add_info ("threads", info_threads_command,
  1402.             _("Display currently known threads.\n\
  1403. Usage: info threads [ID]...\n\
  1404. Optional arguments are thread IDs with spaces between.\n\
  1405. If no arguments, all threads are displayed."));

  1406.   add_prefix_cmd ("thread", class_run, thread_command, _("\
  1407. Use this command to switch between threads.\n\
  1408. The new thread ID must be currently known."),
  1409.                   &thread_cmd_list, "thread ", 1, &cmdlist);

  1410.   add_prefix_cmd ("apply", class_run, thread_apply_command,
  1411.                   _("Apply a command to a list of threads."),
  1412.                   &thread_apply_list, "thread apply ", 1, &thread_cmd_list);

  1413.   add_cmd ("all", class_run, thread_apply_all_command,
  1414.            _("Apply a command to all threads."), &thread_apply_list);

  1415.   add_cmd ("name", class_run, thread_name_command,
  1416.            _("Set the current thread's name.\n\
  1417. Usage: thread name [NAME]\n\
  1418. If NAME is not given, then any existing name is removed."), &thread_cmd_list);

  1419.   add_cmd ("find", class_run, thread_find_command, _("\
  1420. Find threads that match a regular expression.\n\
  1421. Usage: thread find REGEXP\n\
  1422. Will display thread ids whose name, target ID, or extra info matches REGEXP."),
  1423.            &thread_cmd_list);

  1424.   if (!xdb_commands)
  1425.     add_com_alias ("t", "thread", class_run, 1);

  1426.   add_setshow_boolean_cmd ("thread-events", no_class,
  1427.          &print_thread_events, _("\
  1428. Set printing of thread events (such as thread start and exit)."), _("\
  1429. Show printing of thread events (such as thread start and exit)."), NULL,
  1430.          NULL,
  1431.          show_print_thread_events,
  1432.          &setprintlist, &showprintlist);

  1433.   create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
  1434. }