Replace copyreloc-main.c with copyreloc-main.S
[platform/upstream/binutils.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2014 Free Software Foundation, Inc.
4
5    Contributed by Lynx Real-Time Systems, Inc.  Los Gatos, CA.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "environ.h"
27 #include "value.h"
28 #include "target.h"
29 #include "gdbthread.h"
30 #include "command.h"
31 #include "gdbcmd.h"
32 #include "regcache.h"
33 #include "gdb.h"
34 #include "btrace.h"
35
36 #include <ctype.h>
37 #include <sys/types.h>
38 #include <signal.h>
39 #include "ui-out.h"
40 #include "observer.h"
41 #include "annotate.h"
42 #include "cli/cli-decode.h"
43 #include "gdb_regex.h"
44 #include "cli/cli-utils.h"
45 #include "continuations.h"
46
47 /* Definition of struct thread_info exported to gdbthread.h.  */
48
49 /* Prototypes for exported functions.  */
50
51 void _initialize_thread (void);
52
53 /* Prototypes for local functions.  */
54
55 struct thread_info *thread_list = NULL;
56 static int highest_thread_num;
57
58 /* True if any thread is, or may be executing.  We need to track this
59    separately because until we fully sync the thread list, we won't
60    know whether the target is fully stopped, even if we see stop
61    events for all known threads, because any of those threads may have
62    spawned new threads we haven't heard of yet.  */
63 static int threads_executing;
64
65 static void thread_command (char *tidstr, int from_tty);
66 static void thread_apply_all_command (char *, int);
67 static int thread_alive (struct thread_info *);
68 static void info_threads_command (char *, int);
69 static void thread_apply_command (char *, int);
70 static void restore_current_thread (ptid_t);
71 static void prune_threads (void);
72
73 /* Data to cleanup thread array.  */
74
75 struct thread_array_cleanup
76 {
77   /* Array of thread pointers used to set
78      reference count.  */
79   struct thread_info **tp_array;
80
81   /* Thread count in the array.  */
82   int count;
83 };
84
85
86 struct thread_info*
87 inferior_thread (void)
88 {
89   struct thread_info *tp = find_thread_ptid (inferior_ptid);
90   gdb_assert (tp);
91   return tp;
92 }
93
94 void
95 delete_step_resume_breakpoint (struct thread_info *tp)
96 {
97   if (tp && tp->control.step_resume_breakpoint)
98     {
99       delete_breakpoint (tp->control.step_resume_breakpoint);
100       tp->control.step_resume_breakpoint = NULL;
101     }
102 }
103
104 void
105 delete_exception_resume_breakpoint (struct thread_info *tp)
106 {
107   if (tp && tp->control.exception_resume_breakpoint)
108     {
109       delete_breakpoint (tp->control.exception_resume_breakpoint);
110       tp->control.exception_resume_breakpoint = NULL;
111     }
112 }
113
114 static void
115 clear_thread_inferior_resources (struct thread_info *tp)
116 {
117   /* NOTE: this will take care of any left-over step_resume breakpoints,
118      but not any user-specified thread-specific breakpoints.  We can not
119      delete the breakpoint straight-off, because the inferior might not
120      be stopped at the moment.  */
121   if (tp->control.step_resume_breakpoint)
122     {
123       tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
124       tp->control.step_resume_breakpoint = NULL;
125     }
126
127   if (tp->control.exception_resume_breakpoint)
128     {
129       tp->control.exception_resume_breakpoint->disposition
130         = disp_del_at_next_stop;
131       tp->control.exception_resume_breakpoint = NULL;
132     }
133
134   delete_longjmp_breakpoint_at_next_stop (tp->num);
135
136   bpstat_clear (&tp->control.stop_bpstat);
137
138   btrace_teardown (tp);
139
140   do_all_intermediate_continuations_thread (tp, 1);
141   do_all_continuations_thread (tp, 1);
142 }
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
155   xfree (tp->name);
156   xfree (tp);
157 }
158
159 void
160 init_thread_list (void)
161 {
162   struct thread_info *tp, *tpnext;
163
164   highest_thread_num = 0;
165
166   if (!thread_list)
167     return;
168
169   for (tp = thread_list; tp; tp = tpnext)
170     {
171       tpnext = tp->next;
172       free_thread (tp);
173     }
174
175   thread_list = NULL;
176   threads_executing = 0;
177 }
178
179 /* Allocate a new thread with target id PTID and add it to the thread
180    list.  */
181
182 static struct thread_info *
183 new_thread (ptid_t ptid)
184 {
185   struct thread_info *tp;
186
187   tp = xcalloc (1, sizeof (*tp));
188
189   tp->ptid = ptid;
190   tp->num = ++highest_thread_num;
191   tp->next = thread_list;
192   thread_list = tp;
193
194   /* Nothing to follow yet.  */
195   tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
196   tp->state = THREAD_STOPPED;
197
198   return tp;
199 }
200
201 struct thread_info *
202 add_thread_silent (ptid_t ptid)
203 {
204   struct thread_info *tp;
205
206   tp = find_thread_ptid (ptid);
207   if (tp)
208     /* Found an old thread with the same id.  It has to be dead,
209        otherwise we wouldn't be adding a new thread with the same id.
210        The OS is reusing this id --- delete it, and recreate a new
211        one.  */
212     {
213       /* In addition to deleting the thread, if this is the current
214          thread, then we need to take care that delete_thread doesn't
215          really delete the thread if it is inferior_ptid.  Create a
216          new template thread in the list with an invalid ptid, switch
217          to it, delete the original thread, reset the new thread's
218          ptid, and switch to it.  */
219
220       if (ptid_equal (inferior_ptid, ptid))
221         {
222           tp = new_thread (null_ptid);
223
224           /* Make switch_to_thread not read from the thread.  */
225           tp->state = THREAD_EXITED;
226           switch_to_thread (null_ptid);
227
228           /* Now we can delete it.  */
229           delete_thread (ptid);
230
231           /* Now reset its ptid, and reswitch inferior_ptid to it.  */
232           tp->ptid = ptid;
233           tp->state = THREAD_STOPPED;
234           switch_to_thread (ptid);
235
236           observer_notify_new_thread (tp);
237
238           /* All done.  */
239           return tp;
240         }
241       else
242         /* Just go ahead and delete it.  */
243         delete_thread (ptid);
244     }
245
246   tp = new_thread (ptid);
247   observer_notify_new_thread (tp);
248
249   return tp;
250 }
251
252 struct thread_info *
253 add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
254 {
255   struct thread_info *result = add_thread_silent (ptid);
256
257   result->private = private;
258
259   if (print_thread_events)
260     printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
261
262   annotate_new_thread ();
263   return result;
264 }
265
266 struct thread_info *
267 add_thread (ptid_t ptid)
268 {
269   return add_thread_with_info (ptid, NULL);
270 }
271
272 /* Delete thread PTID.  If SILENT, don't notify the observer of this
273    exit.  */
274 static void
275 delete_thread_1 (ptid_t ptid, int silent)
276 {
277   struct thread_info *tp, *tpprev;
278
279   tpprev = NULL;
280
281   for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
282     if (ptid_equal (tp->ptid, ptid))
283       break;
284
285   if (!tp)
286     return;
287
288   /* If this is the current thread, or there's code out there that
289      relies on it existing (refcount > 0) we can't delete yet.  Mark
290      it as exited, and notify it.  */
291   if (tp->refcount > 0
292       || ptid_equal (tp->ptid, inferior_ptid))
293     {
294       if (tp->state != THREAD_EXITED)
295         {
296           observer_notify_thread_exit (tp, silent);
297
298           /* Tag it as exited.  */
299           tp->state = THREAD_EXITED;
300
301           /* Clear breakpoints, etc. associated with this thread.  */
302           clear_thread_inferior_resources (tp);
303         }
304
305        /* Will be really deleted some other time.  */
306        return;
307      }
308
309   /* Notify thread exit, but only if we haven't already.  */
310   if (tp->state != THREAD_EXITED)
311     observer_notify_thread_exit (tp, silent);
312
313   /* Tag it as exited.  */
314   tp->state = THREAD_EXITED;
315   clear_thread_inferior_resources (tp);
316
317   if (tpprev)
318     tpprev->next = tp->next;
319   else
320     thread_list = tp->next;
321
322   free_thread (tp);
323 }
324
325 /* Delete thread PTID and notify of thread exit.  If this is
326    inferior_ptid, don't actually delete it, but tag it as exited and
327    do the notification.  If PTID is the user selected thread, clear
328    it.  */
329 void
330 delete_thread (ptid_t ptid)
331 {
332   delete_thread_1 (ptid, 0 /* not silent */);
333 }
334
335 void
336 delete_thread_silent (ptid_t ptid)
337 {
338   delete_thread_1 (ptid, 1 /* silent */);
339 }
340
341 struct thread_info *
342 find_thread_id (int num)
343 {
344   struct thread_info *tp;
345
346   for (tp = thread_list; tp; tp = tp->next)
347     if (tp->num == num)
348       return tp;
349
350   return NULL;
351 }
352
353 /* Find a thread_info by matching PTID.  */
354 struct thread_info *
355 find_thread_ptid (ptid_t ptid)
356 {
357   struct thread_info *tp;
358
359   for (tp = thread_list; tp; tp = tp->next)
360     if (ptid_equal (tp->ptid, ptid))
361       return tp;
362
363   return NULL;
364 }
365
366 /*
367  * Thread iterator function.
368  *
369  * Calls a callback function once for each thread, so long as
370  * the callback function returns false.  If the callback function
371  * returns true, the iteration will end and the current thread
372  * will be returned.  This can be useful for implementing a 
373  * search for a thread with arbitrary attributes, or for applying
374  * some operation to every thread.
375  *
376  * FIXME: some of the existing functionality, such as 
377  * "Thread apply all", might be rewritten using this functionality.
378  */
379
380 struct thread_info *
381 iterate_over_threads (int (*callback) (struct thread_info *, void *),
382                       void *data)
383 {
384   struct thread_info *tp, *next;
385
386   for (tp = thread_list; tp; tp = next)
387     {
388       next = tp->next;
389       if ((*callback) (tp, data))
390         return tp;
391     }
392
393   return NULL;
394 }
395
396 int
397 thread_count (void)
398 {
399   int result = 0;
400   struct thread_info *tp;
401
402   for (tp = thread_list; tp; tp = tp->next)
403     ++result;
404
405   return result;  
406 }
407
408 int
409 valid_thread_id (int num)
410 {
411   struct thread_info *tp;
412
413   for (tp = thread_list; tp; tp = tp->next)
414     if (tp->num == num)
415       return 1;
416
417   return 0;
418 }
419
420 int
421 pid_to_thread_id (ptid_t ptid)
422 {
423   struct thread_info *tp;
424
425   for (tp = thread_list; tp; tp = tp->next)
426     if (ptid_equal (tp->ptid, ptid))
427       return tp->num;
428
429   return 0;
430 }
431
432 ptid_t
433 thread_id_to_pid (int num)
434 {
435   struct thread_info *thread = find_thread_id (num);
436
437   if (thread)
438     return thread->ptid;
439   else
440     return pid_to_ptid (-1);
441 }
442
443 int
444 in_thread_list (ptid_t ptid)
445 {
446   struct thread_info *tp;
447
448   for (tp = thread_list; tp; tp = tp->next)
449     if (ptid_equal (tp->ptid, ptid))
450       return 1;
451
452   return 0;                     /* Never heard of 'im.  */
453 }
454
455 /* Finds the first thread of the inferior given by PID.  If PID is -1,
456    return the first thread in the list.  */
457
458 struct thread_info *
459 first_thread_of_process (int pid)
460 {
461   struct thread_info *tp, *ret = NULL;
462
463   for (tp = thread_list; tp; tp = tp->next)
464     if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
465       if (ret == NULL || tp->num < ret->num)
466         ret = tp;
467
468   return ret;
469 }
470
471 struct thread_info *
472 any_thread_of_process (int pid)
473 {
474   struct thread_info *tp;
475
476   gdb_assert (pid != 0);
477
478   /* Prefer the current thread.  */
479   if (ptid_get_pid (inferior_ptid) == pid)
480     return inferior_thread ();
481
482   ALL_NON_EXITED_THREADS (tp)
483     if (ptid_get_pid (tp->ptid) == pid)
484       return tp;
485
486   return NULL;
487 }
488
489 struct thread_info *
490 any_live_thread_of_process (int pid)
491 {
492   struct thread_info *curr_tp = NULL;
493   struct thread_info *tp;
494   struct thread_info *tp_executing = NULL;
495
496   gdb_assert (pid != 0);
497
498   /* Prefer the current thread if it's not executing.  */
499   if (ptid_get_pid (inferior_ptid) == pid)
500     {
501       /* If the current thread is dead, forget it.  If it's not
502          executing, use it.  Otherwise, still choose it (below), but
503          only if no other non-executing thread is found.  */
504       curr_tp = inferior_thread ();
505       if (curr_tp->state == THREAD_EXITED)
506         curr_tp = NULL;
507       else if (!curr_tp->executing)
508         return curr_tp;
509     }
510
511   ALL_NON_EXITED_THREADS (tp)
512     if (ptid_get_pid (tp->ptid) == pid)
513       {
514         if (!tp->executing)
515           return tp;
516
517         tp_executing = tp;
518       }
519
520   /* If both the current thread and all live threads are executing,
521      prefer the current thread.  */
522   if (curr_tp != NULL)
523     return curr_tp;
524
525   /* Otherwise, just return an executing thread, if any.  */
526   return tp_executing;
527 }
528
529 /* Print a list of thread ids currently known, and the total number of
530    threads.  To be used from within catch_errors.  */
531 static int
532 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
533 {
534   struct thread_info *tp;
535   int num = 0;
536   struct cleanup *cleanup_chain;
537   int current_thread = -1;
538
539   update_thread_list ();
540
541   cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
542
543   for (tp = thread_list; tp; tp = tp->next)
544     {
545       if (tp->state == THREAD_EXITED)
546         continue;
547
548       if (ptid_equal (tp->ptid, inferior_ptid))
549         current_thread = tp->num;
550
551       num++;
552       ui_out_field_int (uiout, "thread-id", tp->num);
553     }
554
555   do_cleanups (cleanup_chain);
556
557   if (current_thread != -1)
558     ui_out_field_int (uiout, "current-thread-id", current_thread);
559   ui_out_field_int (uiout, "number-of-threads", num);
560   return GDB_RC_OK;
561 }
562
563 /* Official gdblib interface function to get a list of thread ids and
564    the total number.  */
565 enum gdb_rc
566 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
567 {
568   if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
569                                  error_message, RETURN_MASK_ALL) < 0)
570     return GDB_RC_FAIL;
571   return GDB_RC_OK;
572 }
573
574 /* Return true if TP is an active thread.  */
575 static int
576 thread_alive (struct thread_info *tp)
577 {
578   if (tp->state == THREAD_EXITED)
579     return 0;
580   if (!target_thread_alive (tp->ptid))
581     return 0;
582   return 1;
583 }
584
585 static void
586 prune_threads (void)
587 {
588   struct thread_info *tp, *next;
589
590   for (tp = thread_list; tp; tp = next)
591     {
592       next = tp->next;
593       if (!thread_alive (tp))
594         delete_thread (tp->ptid);
595     }
596 }
597
598 void
599 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
600 {
601   struct inferior *inf;
602   struct thread_info *tp;
603
604   /* It can happen that what we knew as the target inferior id
605      changes.  E.g, target remote may only discover the remote process
606      pid after adding the inferior to GDB's list.  */
607   inf = find_inferior_pid (ptid_get_pid (old_ptid));
608   inf->pid = ptid_get_pid (new_ptid);
609
610   tp = find_thread_ptid (old_ptid);
611   tp->ptid = new_ptid;
612
613   observer_notify_thread_ptid_changed (old_ptid, new_ptid);
614 }
615
616 void
617 set_running (ptid_t ptid, int running)
618 {
619   struct thread_info *tp;
620   int all = ptid_equal (ptid, minus_one_ptid);
621
622   /* We try not to notify the observer if no thread has actually changed 
623      the running state -- merely to reduce the number of messages to 
624      frontend.  Frontend is supposed to handle multiple *running just fine.  */
625   if (all || ptid_is_pid (ptid))
626     {
627       int any_started = 0;
628
629       for (tp = thread_list; tp; tp = tp->next)
630         if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
631           {
632             if (tp->state == THREAD_EXITED)
633               continue;
634             if (running && tp->state == THREAD_STOPPED)
635               any_started = 1;
636             tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
637           }
638       if (any_started)
639         observer_notify_target_resumed (ptid);
640     }
641   else
642     {
643       int started = 0;
644
645       tp = find_thread_ptid (ptid);
646       gdb_assert (tp);
647       gdb_assert (tp->state != THREAD_EXITED);
648       if (running && tp->state == THREAD_STOPPED)
649         started = 1;
650       tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
651       if (started)
652         observer_notify_target_resumed (ptid);
653     }
654 }
655
656 static int
657 is_thread_state (ptid_t ptid, enum thread_state state)
658 {
659   struct thread_info *tp;
660
661   tp = find_thread_ptid (ptid);
662   gdb_assert (tp);
663   return tp->state == state;
664 }
665
666 int
667 is_stopped (ptid_t ptid)
668 {
669   return is_thread_state (ptid, THREAD_STOPPED);
670 }
671
672 int
673 is_exited (ptid_t ptid)
674 {
675   return is_thread_state (ptid, THREAD_EXITED);
676 }
677
678 int
679 is_running (ptid_t ptid)
680 {
681   return is_thread_state (ptid, THREAD_RUNNING);
682 }
683
684 int
685 is_executing (ptid_t ptid)
686 {
687   struct thread_info *tp;
688
689   tp = find_thread_ptid (ptid);
690   gdb_assert (tp);
691   return tp->executing;
692 }
693
694 void
695 set_executing (ptid_t ptid, int executing)
696 {
697   struct thread_info *tp;
698   int all = ptid_equal (ptid, minus_one_ptid);
699
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->executing = executing;
705     }
706   else
707     {
708       tp = find_thread_ptid (ptid);
709       gdb_assert (tp);
710       tp->executing = executing;
711     }
712
713   /* It only takes one running thread to spawn more threads.*/
714   if (executing)
715     threads_executing = 1;
716   /* Only clear the flag if the caller is telling us everything is
717      stopped.  */
718   else if (ptid_equal (minus_one_ptid, ptid))
719     threads_executing = 0;
720 }
721
722 /* See gdbthread.h.  */
723
724 int
725 threads_are_executing (void)
726 {
727   return threads_executing;
728 }
729
730 void
731 set_stop_requested (ptid_t ptid, int stop)
732 {
733   struct thread_info *tp;
734   int all = ptid_equal (ptid, minus_one_ptid);
735
736   if (all || ptid_is_pid (ptid))
737     {
738       for (tp = thread_list; tp; tp = tp->next)
739         if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
740           tp->stop_requested = stop;
741     }
742   else
743     {
744       tp = find_thread_ptid (ptid);
745       gdb_assert (tp);
746       tp->stop_requested = stop;
747     }
748
749   /* Call the stop requested observer so other components of GDB can
750      react to this request.  */
751   if (stop)
752     observer_notify_thread_stop_requested (ptid);
753 }
754
755 void
756 finish_thread_state (ptid_t ptid)
757 {
758   struct thread_info *tp;
759   int all;
760   int any_started = 0;
761
762   all = ptid_equal (ptid, minus_one_ptid);
763
764   if (all || ptid_is_pid (ptid))
765     {
766       for (tp = thread_list; tp; tp = tp->next)
767         {
768           if (tp->state == THREAD_EXITED)
769             continue;
770           if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
771             {
772               if (tp->executing && tp->state == THREAD_STOPPED)
773                 any_started = 1;
774               tp->state = tp->executing ? THREAD_RUNNING : THREAD_STOPPED;
775             }
776         }
777     }
778   else
779     {
780       tp = find_thread_ptid (ptid);
781       gdb_assert (tp);
782       if (tp->state != THREAD_EXITED)
783         {
784           if (tp->executing && tp->state == THREAD_STOPPED)
785             any_started = 1;
786           tp->state = tp->executing ? THREAD_RUNNING : THREAD_STOPPED;
787         }
788     }
789
790   if (any_started)
791     observer_notify_target_resumed (ptid);
792 }
793
794 void
795 finish_thread_state_cleanup (void *arg)
796 {
797   ptid_t *ptid_p = arg;
798
799   gdb_assert (arg);
800
801   finish_thread_state (*ptid_p);
802 }
803
804 int
805 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
806 {
807   return (pc >= thread->control.step_range_start
808           && pc < thread->control.step_range_end);
809 }
810
811 /* Prints the list of threads and their details on UIOUT.
812    This is a version of 'info_threads_command' suitable for
813    use from MI.
814    If REQUESTED_THREAD is not -1, it's the GDB id of the thread
815    that should be printed.  Otherwise, all threads are
816    printed.
817    If PID is not -1, only print threads from the process PID.
818    Otherwise, threads from all attached PIDs are printed.
819    If both REQUESTED_THREAD and PID are not -1, then the thread
820    is printed if it belongs to the specified process.  Otherwise,
821    an error is raised.  */
822 void
823 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
824 {
825   struct thread_info *tp;
826   ptid_t current_ptid;
827   struct cleanup *old_chain;
828   char *extra_info, *name, *target_id;
829   int current_thread = -1;
830
831   update_thread_list ();
832   current_ptid = inferior_ptid;
833
834   /* We'll be switching threads temporarily.  */
835   old_chain = make_cleanup_restore_current_thread ();
836
837   /* For backward compatibility, we make a list for MI.  A table is
838      preferable for the CLI, though, because it shows table
839      headers.  */
840   if (ui_out_is_mi_like_p (uiout))
841     make_cleanup_ui_out_list_begin_end (uiout, "threads");
842   else
843     {
844       int n_threads = 0;
845
846       for (tp = thread_list; tp; tp = tp->next)
847         {
848           if (!number_is_in_list (requested_threads, tp->num))
849             continue;
850
851           if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
852             continue;
853
854           if (tp->state == THREAD_EXITED)
855             continue;
856
857           ++n_threads;
858         }
859
860       if (n_threads == 0)
861         {
862           if (requested_threads == NULL || *requested_threads == '\0')
863             ui_out_message (uiout, 0, _("No threads.\n"));
864           else
865             ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
866                             requested_threads);
867           do_cleanups (old_chain);
868           return;
869         }
870
871       make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
872
873       ui_out_table_header (uiout, 1, ui_left, "current", "");
874       ui_out_table_header (uiout, 4, ui_left, "id", "Id");
875       ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
876       ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
877       ui_out_table_body (uiout);
878     }
879
880   for (tp = thread_list; tp; tp = tp->next)
881     {
882       struct cleanup *chain2;
883       int core;
884
885       if (!number_is_in_list (requested_threads, tp->num))
886         continue;
887
888       if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
889         {
890           if (requested_threads != NULL && *requested_threads != '\0')
891             error (_("Requested thread not found in requested process"));
892           continue;
893         }
894
895       if (ptid_equal (tp->ptid, current_ptid))
896         current_thread = tp->num;
897
898       if (tp->state == THREAD_EXITED)
899         continue;
900
901       chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
902
903       if (ui_out_is_mi_like_p (uiout))
904         {
905           /* Compatibility.  */
906           if (ptid_equal (tp->ptid, current_ptid))
907             ui_out_text (uiout, "* ");
908           else
909             ui_out_text (uiout, "  ");
910         }
911       else
912         {
913           if (ptid_equal (tp->ptid, current_ptid))
914             ui_out_field_string (uiout, "current", "*");
915           else
916             ui_out_field_skip (uiout, "current");
917         }
918
919       ui_out_field_int (uiout, "id", tp->num);
920
921       /* For the CLI, we stuff everything into the target-id field.
922          This is a gross hack to make the output come out looking
923          correct.  The underlying problem here is that ui-out has no
924          way to specify that a field's space allocation should be
925          shared by several fields.  For MI, we do the right thing
926          instead.  */
927
928       target_id = target_pid_to_str (tp->ptid);
929       extra_info = target_extra_thread_info (tp);
930       name = tp->name ? tp->name : target_thread_name (tp);
931
932       if (ui_out_is_mi_like_p (uiout))
933         {
934           ui_out_field_string (uiout, "target-id", target_id);
935           if (extra_info)
936             ui_out_field_string (uiout, "details", extra_info);
937           if (name)
938             ui_out_field_string (uiout, "name", name);
939         }
940       else
941         {
942           struct cleanup *str_cleanup;
943           char *contents;
944
945           if (extra_info && name)
946             contents = xstrprintf ("%s \"%s\" (%s)", target_id,
947                                    name, extra_info);
948           else if (extra_info)
949             contents = xstrprintf ("%s (%s)", target_id, extra_info);
950           else if (name)
951             contents = xstrprintf ("%s \"%s\"", target_id, name);
952           else
953             contents = xstrdup (target_id);
954           str_cleanup = make_cleanup (xfree, contents);
955
956           ui_out_field_string (uiout, "target-id", contents);
957           do_cleanups (str_cleanup);
958         }
959
960       if (tp->state == THREAD_RUNNING)
961         ui_out_text (uiout, "(running)\n");
962       else
963         {
964           /* The switch below puts us at the top of the stack (leaf
965              frame).  */
966           switch_to_thread (tp->ptid);
967           print_stack_frame (get_selected_frame (NULL),
968                              /* For MI output, print frame level.  */
969                              ui_out_is_mi_like_p (uiout),
970                              LOCATION, 0);
971         }
972
973       if (ui_out_is_mi_like_p (uiout))
974         {
975           char *state = "stopped";
976
977           if (tp->state == THREAD_RUNNING)
978             state = "running";
979           ui_out_field_string (uiout, "state", state);
980         }
981
982       core = target_core_of_thread (tp->ptid);
983       if (ui_out_is_mi_like_p (uiout) && core != -1)
984         ui_out_field_int (uiout, "core", core);
985
986       do_cleanups (chain2);
987     }
988
989   /* Restores the current thread and the frame selected before
990      the "info threads" command.  */
991   do_cleanups (old_chain);
992
993   if (pid == -1 && requested_threads == NULL)
994     {
995       gdb_assert (current_thread != -1
996                   || !thread_list
997                   || ptid_equal (inferior_ptid, null_ptid));
998       if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
999         ui_out_field_int (uiout, "current-thread-id", current_thread);
1000
1001       if (current_thread != -1 && is_exited (current_ptid))
1002         ui_out_message (uiout, 0, "\n\
1003 The current thread <Thread ID %d> has terminated.  See `help thread'.\n",
1004                         current_thread);
1005       else if (thread_list
1006                && current_thread == -1
1007                && ptid_equal (current_ptid, null_ptid))
1008         ui_out_message (uiout, 0, "\n\
1009 No selected thread.  See `help thread'.\n");
1010     }
1011 }
1012
1013 /* Print information about currently known threads 
1014
1015    Optional ARG is a thread id, or list of thread ids.
1016
1017    Note: this has the drawback that it _really_ switches
1018          threads, which frees the frame cache.  A no-side
1019          effects info-threads command would be nicer.  */
1020
1021 static void
1022 info_threads_command (char *arg, int from_tty)
1023 {
1024   print_thread_info (current_uiout, arg, -1);
1025 }
1026
1027 /* Switch from one thread to another.  */
1028
1029 void
1030 switch_to_thread (ptid_t ptid)
1031 {
1032   /* Switch the program space as well, if we can infer it from the now
1033      current thread.  Otherwise, it's up to the caller to select the
1034      space it wants.  */
1035   if (!ptid_equal (ptid, null_ptid))
1036     {
1037       struct inferior *inf;
1038
1039       inf = find_inferior_pid (ptid_get_pid (ptid));
1040       gdb_assert (inf != NULL);
1041       set_current_program_space (inf->pspace);
1042       set_current_inferior (inf);
1043     }
1044
1045   if (ptid_equal (ptid, inferior_ptid))
1046     return;
1047
1048   inferior_ptid = ptid;
1049   reinit_frame_cache ();
1050
1051   /* We don't check for is_stopped, because we're called at times
1052      while in the TARGET_RUNNING state, e.g., while handling an
1053      internal event.  */
1054   if (!ptid_equal (inferior_ptid, null_ptid)
1055       && !is_exited (ptid)
1056       && !is_executing (ptid))
1057     stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1058   else
1059     stop_pc = ~(CORE_ADDR) 0;
1060 }
1061
1062 static void
1063 restore_current_thread (ptid_t ptid)
1064 {
1065   switch_to_thread (ptid);
1066 }
1067
1068 static void
1069 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1070 {
1071   struct frame_info *frame = NULL;
1072   int count;
1073
1074   /* This means there was no selected frame.  */
1075   if (frame_level == -1)
1076     {
1077       select_frame (NULL);
1078       return;
1079     }
1080
1081   gdb_assert (frame_level >= 0);
1082
1083   /* Restore by level first, check if the frame id is the same as
1084      expected.  If that fails, try restoring by frame id.  If that
1085      fails, nothing to do, just warn the user.  */
1086
1087   count = frame_level;
1088   frame = find_relative_frame (get_current_frame (), &count);
1089   if (count == 0
1090       && frame != NULL
1091       /* The frame ids must match - either both valid or both outer_frame_id.
1092          The latter case is not failsafe, but since it's highly unlikely
1093          the search by level finds the wrong frame, it's 99.9(9)% of
1094          the time (for all practical purposes) safe.  */
1095       && frame_id_eq (get_frame_id (frame), a_frame_id))
1096     {
1097       /* Cool, all is fine.  */
1098       select_frame (frame);
1099       return;
1100     }
1101
1102   frame = frame_find_by_id (a_frame_id);
1103   if (frame != NULL)
1104     {
1105       /* Cool, refound it.  */
1106       select_frame (frame);
1107       return;
1108     }
1109
1110   /* Nothing else to do, the frame layout really changed.  Select the
1111      innermost stack frame.  */
1112   select_frame (get_current_frame ());
1113
1114   /* Warn the user.  */
1115   if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
1116     {
1117       warning (_("Couldn't restore frame #%d in "
1118                  "current thread.  Bottom (innermost) frame selected:"),
1119                frame_level);
1120       /* For MI, we should probably have a notification about
1121          current frame change.  But this error is not very
1122          likely, so don't bother for now.  */
1123       print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1124     }
1125 }
1126
1127 struct current_thread_cleanup
1128 {
1129   ptid_t inferior_ptid;
1130   struct frame_id selected_frame_id;
1131   int selected_frame_level;
1132   int was_stopped;
1133   int inf_id;
1134   int was_removable;
1135 };
1136
1137 static void
1138 do_restore_current_thread_cleanup (void *arg)
1139 {
1140   struct thread_info *tp;
1141   struct current_thread_cleanup *old = arg;
1142
1143   tp = find_thread_ptid (old->inferior_ptid);
1144
1145   /* If the previously selected thread belonged to a process that has
1146      in the mean time been deleted (due to normal exit, detach, etc.),
1147      then don't revert back to it, but instead simply drop back to no
1148      thread selected.  */
1149   if (tp
1150       && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL)
1151     restore_current_thread (old->inferior_ptid);
1152   else
1153     {
1154       restore_current_thread (null_ptid);
1155       set_current_inferior (find_inferior_id (old->inf_id));
1156     }
1157
1158   /* The running state of the originally selected thread may have
1159      changed, so we have to recheck it here.  */
1160   if (!ptid_equal (inferior_ptid, null_ptid)
1161       && old->was_stopped
1162       && is_stopped (inferior_ptid)
1163       && target_has_registers
1164       && target_has_stack
1165       && target_has_memory)
1166     restore_selected_frame (old->selected_frame_id,
1167                             old->selected_frame_level);
1168 }
1169
1170 static void
1171 restore_current_thread_cleanup_dtor (void *arg)
1172 {
1173   struct current_thread_cleanup *old = arg;
1174   struct thread_info *tp;
1175   struct inferior *inf;
1176
1177   tp = find_thread_ptid (old->inferior_ptid);
1178   if (tp)
1179     tp->refcount--;
1180   inf = find_inferior_id (old->inf_id);
1181   if (inf != NULL)
1182     inf->removable = old->was_removable;
1183   xfree (old);
1184 }
1185
1186 /* Set the thread reference count.  */
1187
1188 static void
1189 set_thread_refcount (void *data)
1190 {
1191   int k;
1192   struct thread_array_cleanup *ta_cleanup = data;
1193
1194   for (k = 0; k != ta_cleanup->count; k++)
1195     ta_cleanup->tp_array[k]->refcount--;
1196 }
1197
1198 struct cleanup *
1199 make_cleanup_restore_current_thread (void)
1200 {
1201   struct thread_info *tp;
1202   struct frame_info *frame;
1203   struct current_thread_cleanup *old;
1204
1205   old = xmalloc (sizeof (struct current_thread_cleanup));
1206   old->inferior_ptid = inferior_ptid;
1207   old->inf_id = current_inferior ()->num;
1208   old->was_removable = current_inferior ()->removable;
1209
1210   if (!ptid_equal (inferior_ptid, null_ptid))
1211     {
1212       old->was_stopped = is_stopped (inferior_ptid);
1213       if (old->was_stopped
1214           && target_has_registers
1215           && target_has_stack
1216           && target_has_memory)
1217         {
1218           /* When processing internal events, there might not be a
1219              selected frame.  If we naively call get_selected_frame
1220              here, then we can end up reading debuginfo for the
1221              current frame, but we don't generally need the debuginfo
1222              at this point.  */
1223           frame = get_selected_frame_if_set ();
1224         }
1225       else
1226         frame = NULL;
1227
1228       old->selected_frame_id = get_frame_id (frame);
1229       old->selected_frame_level = frame_relative_level (frame);
1230
1231       tp = find_thread_ptid (inferior_ptid);
1232       if (tp)
1233         tp->refcount++;
1234     }
1235
1236   current_inferior ()->removable = 0;
1237
1238   return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1239                             restore_current_thread_cleanup_dtor);
1240 }
1241
1242 /* Apply a GDB command to a list of threads.  List syntax is a whitespace
1243    seperated list of numbers, or ranges, or the keyword `all'.  Ranges consist
1244    of two numbers seperated by a hyphen.  Examples:
1245
1246    thread apply 1 2 7 4 backtrace       Apply backtrace cmd to threads 1,2,7,4
1247    thread apply 2-7 9 p foo(1)  Apply p foo(1) cmd to threads 2->7 & 9
1248    thread apply all p x/i $pc   Apply x/i $pc cmd to all threads.  */
1249
1250 static void
1251 thread_apply_all_command (char *cmd, int from_tty)
1252 {
1253   struct cleanup *old_chain;
1254   char *saved_cmd;
1255   int tc;
1256   struct thread_array_cleanup ta_cleanup;
1257
1258   if (cmd == NULL || *cmd == '\000')
1259     error (_("Please specify a command following the thread ID list"));
1260
1261   update_thread_list ();
1262
1263   old_chain = make_cleanup_restore_current_thread ();
1264
1265   /* Save a copy of the command in case it is clobbered by
1266      execute_command.  */
1267   saved_cmd = xstrdup (cmd);
1268   make_cleanup (xfree, saved_cmd);
1269   tc = thread_count ();
1270
1271   if (tc)
1272     {
1273       struct thread_info **tp_array;
1274       struct thread_info *tp;
1275       int i = 0, k;
1276
1277       /* Save a copy of the thread_list in case we execute detach
1278          command.  */
1279       tp_array = xmalloc (sizeof (struct thread_info *) * tc);
1280       make_cleanup (xfree, tp_array);
1281       ta_cleanup.tp_array = tp_array;
1282       ta_cleanup.count = tc;
1283
1284       ALL_NON_EXITED_THREADS (tp)
1285         {
1286           tp_array[i] = tp;
1287           tp->refcount++;
1288           i++;
1289         }
1290
1291       make_cleanup (set_thread_refcount, &ta_cleanup);
1292
1293       for (k = 0; k != i; k++)
1294         if (thread_alive (tp_array[k]))
1295           {
1296             switch_to_thread (tp_array[k]->ptid);
1297             printf_filtered (_("\nThread %d (%s):\n"), 
1298                              tp_array[k]->num,
1299                              target_pid_to_str (inferior_ptid));
1300             execute_command (cmd, from_tty);
1301
1302             /* Restore exact command used previously.  */
1303             strcpy (cmd, saved_cmd);
1304           }
1305     }
1306
1307   do_cleanups (old_chain);
1308 }
1309
1310 static void
1311 thread_apply_command (char *tidlist, int from_tty)
1312 {
1313   char *cmd;
1314   struct cleanup *old_chain;
1315   char *saved_cmd;
1316   struct get_number_or_range_state state;
1317
1318   if (tidlist == NULL || *tidlist == '\000')
1319     error (_("Please specify a thread ID list"));
1320
1321   for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1322
1323   if (*cmd == '\000')
1324     error (_("Please specify a command following the thread ID list"));
1325
1326   /* Save a copy of the command in case it is clobbered by
1327      execute_command.  */
1328   saved_cmd = xstrdup (cmd);
1329   old_chain = make_cleanup (xfree, saved_cmd);
1330
1331   init_number_or_range (&state, tidlist);
1332   while (!state.finished && state.string < cmd)
1333     {
1334       struct thread_info *tp;
1335       int start;
1336
1337       start = get_number_or_range (&state);
1338
1339       make_cleanup_restore_current_thread ();
1340
1341       tp = find_thread_id (start);
1342
1343       if (!tp)
1344         warning (_("Unknown thread %d."), start);
1345       else if (!thread_alive (tp))
1346         warning (_("Thread %d has terminated."), start);
1347       else
1348         {
1349           switch_to_thread (tp->ptid);
1350
1351           printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1352                            target_pid_to_str (inferior_ptid));
1353           execute_command (cmd, from_tty);
1354
1355           /* Restore exact command used previously.  */
1356           strcpy (cmd, saved_cmd);
1357         }
1358     }
1359
1360   do_cleanups (old_chain);
1361 }
1362
1363 /* Switch to the specified thread.  Will dispatch off to thread_apply_command
1364    if prefix of arg is `apply'.  */
1365
1366 static void
1367 thread_command (char *tidstr, int from_tty)
1368 {
1369   if (!tidstr)
1370     {
1371       if (ptid_equal (inferior_ptid, null_ptid))
1372         error (_("No thread selected"));
1373
1374       if (target_has_stack)
1375         {
1376           if (is_exited (inferior_ptid))
1377             printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1378                              pid_to_thread_id (inferior_ptid),
1379                              target_pid_to_str (inferior_ptid));
1380           else
1381             printf_filtered (_("[Current thread is %d (%s)]\n"),
1382                              pid_to_thread_id (inferior_ptid),
1383                              target_pid_to_str (inferior_ptid));
1384         }
1385       else
1386         error (_("No stack."));
1387       return;
1388     }
1389
1390   gdb_thread_select (current_uiout, tidstr, NULL);
1391 }
1392
1393 /* Implementation of `thread name'.  */
1394
1395 static void
1396 thread_name_command (char *arg, int from_tty)
1397 {
1398   struct thread_info *info;
1399
1400   if (ptid_equal (inferior_ptid, null_ptid))
1401     error (_("No thread selected"));
1402
1403   arg = skip_spaces (arg);
1404
1405   info = inferior_thread ();
1406   xfree (info->name);
1407   info->name = arg ? xstrdup (arg) : NULL;
1408 }
1409
1410 /* Find thread ids with a name, target pid, or extra info matching ARG.  */
1411
1412 static void
1413 thread_find_command (char *arg, int from_tty)
1414 {
1415   struct thread_info *tp;
1416   char *tmp;
1417   unsigned long match = 0;
1418
1419   if (arg == NULL || *arg == '\0')
1420     error (_("Command requires an argument."));
1421
1422   tmp = re_comp (arg);
1423   if (tmp != 0)
1424     error (_("Invalid regexp (%s): %s"), tmp, arg);
1425
1426   update_thread_list ();
1427   for (tp = thread_list; tp; tp = tp->next)
1428     {
1429       if (tp->name != NULL && re_exec (tp->name))
1430         {
1431           printf_filtered (_("Thread %d has name '%s'\n"),
1432                            tp->num, tp->name);
1433           match++;
1434         }
1435
1436       tmp = target_thread_name (tp);
1437       if (tmp != NULL && re_exec (tmp))
1438         {
1439           printf_filtered (_("Thread %d has target name '%s'\n"),
1440                            tp->num, tmp);
1441           match++;
1442         }
1443
1444       tmp = target_pid_to_str (tp->ptid);
1445       if (tmp != NULL && re_exec (tmp))
1446         {
1447           printf_filtered (_("Thread %d has target id '%s'\n"),
1448                            tp->num, tmp);
1449           match++;
1450         }
1451
1452       tmp = target_extra_thread_info (tp);
1453       if (tmp != NULL && re_exec (tmp))
1454         {
1455           printf_filtered (_("Thread %d has extra info '%s'\n"),
1456                            tp->num, tmp);
1457           match++;
1458         }
1459     }
1460   if (!match)
1461     printf_filtered (_("No threads match '%s'\n"), arg);
1462 }
1463
1464 /* Print notices when new threads are attached and detached.  */
1465 int print_thread_events = 1;
1466 static void
1467 show_print_thread_events (struct ui_file *file, int from_tty,
1468                           struct cmd_list_element *c, const char *value)
1469 {
1470   fprintf_filtered (file,
1471                     _("Printing of thread events is %s.\n"),
1472                     value);
1473 }
1474
1475 static int
1476 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1477 {
1478   int num;
1479   struct thread_info *tp;
1480
1481   num = value_as_long (parse_and_eval (tidstr));
1482
1483   tp = find_thread_id (num);
1484
1485   if (!tp)
1486     error (_("Thread ID %d not known."), num);
1487
1488   if (!thread_alive (tp))
1489     error (_("Thread ID %d has terminated."), num);
1490
1491   switch_to_thread (tp->ptid);
1492
1493   annotate_thread_changed ();
1494
1495   ui_out_text (uiout, "[Switching to thread ");
1496   ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1497   ui_out_text (uiout, " (");
1498   ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1499   ui_out_text (uiout, ")]");
1500
1501   /* Note that we can't reach this with an exited thread, due to the
1502      thread_alive check above.  */
1503   if (tp->state == THREAD_RUNNING)
1504     ui_out_text (uiout, "(running)\n");
1505   else
1506     {
1507       ui_out_text (uiout, "\n");
1508       print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1509     }
1510
1511   /* Since the current thread may have changed, see if there is any
1512      exited thread we can now delete.  */
1513   prune_threads ();
1514
1515   return GDB_RC_OK;
1516 }
1517
1518 enum gdb_rc
1519 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1520 {
1521   if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1522                                  error_message, RETURN_MASK_ALL) < 0)
1523     return GDB_RC_FAIL;
1524   return GDB_RC_OK;
1525 }
1526
1527 /* Update the 'threads_executing' global based on the threads we know
1528    about right now.  */
1529
1530 static void
1531 update_threads_executing (void)
1532 {
1533   struct thread_info *tp;
1534
1535   threads_executing = 0;
1536   ALL_NON_EXITED_THREADS (tp)
1537     {
1538       if (tp->executing)
1539         {
1540           threads_executing = 1;
1541           break;
1542         }
1543     }
1544 }
1545
1546 void
1547 update_thread_list (void)
1548 {
1549   prune_threads ();
1550   target_find_new_threads ();
1551   update_threads_executing ();
1552 }
1553
1554 /* Return a new value for the selected thread's id.  Return a value of 0 if
1555    no thread is selected, or no threads exist.  */
1556
1557 static struct value *
1558 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1559                       void *ignore)
1560 {
1561   struct thread_info *tp = find_thread_ptid (inferior_ptid);
1562
1563   return value_from_longest (builtin_type (gdbarch)->builtin_int,
1564                              (tp ? tp->num : 0));
1565 }
1566
1567 /* Commands with a prefix of `thread'.  */
1568 struct cmd_list_element *thread_cmd_list = NULL;
1569
1570 /* Implementation of `thread' variable.  */
1571
1572 static const struct internalvar_funcs thread_funcs =
1573 {
1574   thread_id_make_value,
1575   NULL,
1576   NULL
1577 };
1578
1579 void
1580 _initialize_thread (void)
1581 {
1582   static struct cmd_list_element *thread_apply_list = NULL;
1583
1584   add_info ("threads", info_threads_command, 
1585             _("Display currently known threads.\n\
1586 Usage: info threads [ID]...\n\
1587 Optional arguments are thread IDs with spaces between.\n\
1588 If no arguments, all threads are displayed."));
1589
1590   add_prefix_cmd ("thread", class_run, thread_command, _("\
1591 Use this command to switch between threads.\n\
1592 The new thread ID must be currently known."),
1593                   &thread_cmd_list, "thread ", 1, &cmdlist);
1594
1595   add_prefix_cmd ("apply", class_run, thread_apply_command,
1596                   _("Apply a command to a list of threads."),
1597                   &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1598
1599   add_cmd ("all", class_run, thread_apply_all_command,
1600            _("Apply a command to all threads."), &thread_apply_list);
1601
1602   add_cmd ("name", class_run, thread_name_command,
1603            _("Set the current thread's name.\n\
1604 Usage: thread name [NAME]\n\
1605 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
1606
1607   add_cmd ("find", class_run, thread_find_command, _("\
1608 Find threads that match a regular expression.\n\
1609 Usage: thread find REGEXP\n\
1610 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1611            &thread_cmd_list);
1612
1613   if (!xdb_commands)
1614     add_com_alias ("t", "thread", class_run, 1);
1615
1616   add_setshow_boolean_cmd ("thread-events", no_class,
1617          &print_thread_events, _("\
1618 Set printing of thread events (such as thread start and exit)."), _("\
1619 Show printing of thread events (such as thread start and exit)."), NULL,
1620          NULL,
1621          show_print_thread_events,
1622          &setprintlist, &showprintlist);
1623
1624   create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
1625 }