Add "executing" property to threads.
[external/binutils.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4    2000, 2001, 2002, 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
5
6    Contributed by Lynx Real-Time Systems, Inc.  Los Gatos, CA.
7
8    This file is part of GDB.
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "environ.h"
28 #include "value.h"
29 #include "target.h"
30 #include "gdbthread.h"
31 #include "exceptions.h"
32 #include "command.h"
33 #include "gdbcmd.h"
34 #include "regcache.h"
35 #include "gdb.h"
36 #include "gdb_string.h"
37
38 #include <ctype.h>
39 #include <sys/types.h>
40 #include <signal.h>
41 #include "ui-out.h"
42 #include "observer.h"
43 #include "annotate.h"
44
45 /* Definition of struct thread_info exported to gdbthread.h */
46
47 /* Prototypes for exported functions. */
48
49 void _initialize_thread (void);
50
51 /* Prototypes for local functions. */
52
53 static struct thread_info *thread_list = NULL;
54 static int highest_thread_num;
55
56 static struct thread_info *find_thread_id (int num);
57
58 static void thread_command (char *tidstr, int from_tty);
59 static void thread_apply_all_command (char *, int);
60 static int thread_alive (struct thread_info *);
61 static void info_threads_command (char *, int);
62 static void thread_apply_command (char *, int);
63 static void restore_current_thread (ptid_t);
64 static void prune_threads (void);
65
66 static int main_thread_running = 0;
67 static int main_thread_executing = 0;
68
69 void
70 delete_step_resume_breakpoint (void *arg)
71 {
72   struct breakpoint **breakpointp = (struct breakpoint **) arg;
73   struct thread_info *tp;
74
75   if (*breakpointp != NULL)
76     {
77       delete_breakpoint (*breakpointp);
78       for (tp = thread_list; tp; tp = tp->next)
79         if (tp->step_resume_breakpoint == *breakpointp)
80           tp->step_resume_breakpoint = NULL;
81
82       *breakpointp = NULL;
83     }
84 }
85
86 static void
87 free_thread (struct thread_info *tp)
88 {
89   /* NOTE: this will take care of any left-over step_resume breakpoints,
90      but not any user-specified thread-specific breakpoints.  We can not
91      delete the breakpoint straight-off, because the inferior might not
92      be stopped at the moment.  */
93   if (tp->step_resume_breakpoint)
94     tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
95
96   /* FIXME: do I ever need to call the back-end to give it a
97      chance at this private data before deleting the thread?  */
98   if (tp->private)
99     xfree (tp->private);
100
101   xfree (tp);
102 }
103
104 void
105 init_thread_list (void)
106 {
107   struct thread_info *tp, *tpnext;
108
109   highest_thread_num = 0;
110   main_thread_running = 0;
111   main_thread_executing = 0;
112
113   if (!thread_list)
114     return;
115
116   for (tp = thread_list; tp; tp = tpnext)
117     {
118       tpnext = tp->next;
119       free_thread (tp);
120     }
121
122   thread_list = NULL;
123 }
124
125 struct thread_info *
126 add_thread_silent (ptid_t ptid)
127 {
128   struct thread_info *tp;
129
130   tp = (struct thread_info *) xmalloc (sizeof (*tp));
131   memset (tp, 0, sizeof (*tp));
132   tp->ptid = ptid;
133   tp->num = ++highest_thread_num;
134   tp->next = thread_list;
135   thread_list = tp;
136
137   observer_notify_new_thread (tp);
138
139   return tp;
140 }
141
142 struct thread_info *
143 add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
144 {
145   struct thread_info *result = add_thread_silent (ptid);
146
147   result->private = private;
148
149   if (print_thread_events)
150     printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
151
152   annotate_new_thread ();
153   return result;
154 }
155
156 struct thread_info *
157 add_thread (ptid_t ptid)
158 {
159   return add_thread_with_info (ptid, NULL);
160 }
161
162 /* Delete thread PTID.  If SILENT, don't notify the observer of this
163    exit.  */
164 static void
165 delete_thread_1 (ptid_t ptid, int silent)
166 {
167   struct thread_info *tp, *tpprev;
168
169   tpprev = NULL;
170
171   for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
172     if (ptid_equal (tp->ptid, ptid))
173       break;
174
175   if (!tp)
176     return;
177
178   if (tpprev)
179     tpprev->next = tp->next;
180   else
181     thread_list = tp->next;
182
183   if (!silent)
184     observer_notify_thread_exit (tp);
185
186   free_thread (tp);
187 }
188
189 void
190 delete_thread (ptid_t ptid)
191 {
192   delete_thread_1 (ptid, 0 /* not silent */);
193 }
194
195 void
196 delete_thread_silent (ptid_t ptid)
197 {
198   delete_thread_1 (ptid, 1 /* silent */);
199 }
200
201 static struct thread_info *
202 find_thread_id (int num)
203 {
204   struct thread_info *tp;
205
206   for (tp = thread_list; tp; tp = tp->next)
207     if (tp->num == num)
208       return tp;
209
210   return NULL;
211 }
212
213 /* Find a thread_info by matching PTID.  */
214 struct thread_info *
215 find_thread_pid (ptid_t ptid)
216 {
217   struct thread_info *tp;
218
219   for (tp = thread_list; tp; tp = tp->next)
220     if (ptid_equal (tp->ptid, ptid))
221       return tp;
222
223   return NULL;
224 }
225
226 /*
227  * Thread iterator function.
228  *
229  * Calls a callback function once for each thread, so long as
230  * the callback function returns false.  If the callback function
231  * returns true, the iteration will end and the current thread
232  * will be returned.  This can be useful for implementing a 
233  * search for a thread with arbitrary attributes, or for applying
234  * some operation to every thread.
235  *
236  * FIXME: some of the existing functionality, such as 
237  * "Thread apply all", might be rewritten using this functionality.
238  */
239
240 struct thread_info *
241 iterate_over_threads (int (*callback) (struct thread_info *, void *),
242                       void *data)
243 {
244   struct thread_info *tp;
245
246   for (tp = thread_list; tp; tp = tp->next)
247     if ((*callback) (tp, data))
248       return tp;
249
250   return NULL;
251 }
252
253 int
254 thread_count (void)
255 {
256   int result = 0;
257   struct thread_info *tp;
258
259   for (tp = thread_list; tp; tp = tp->next)
260     ++result;
261
262   return result;  
263 }
264
265 int
266 valid_thread_id (int num)
267 {
268   struct thread_info *tp;
269
270   for (tp = thread_list; tp; tp = tp->next)
271     if (tp->num == num)
272       return 1;
273
274   return 0;
275 }
276
277 int
278 pid_to_thread_id (ptid_t ptid)
279 {
280   struct thread_info *tp;
281
282   for (tp = thread_list; tp; tp = tp->next)
283     if (ptid_equal (tp->ptid, ptid))
284       return tp->num;
285
286   return 0;
287 }
288
289 ptid_t
290 thread_id_to_pid (int num)
291 {
292   struct thread_info *thread = find_thread_id (num);
293   if (thread)
294     return thread->ptid;
295   else
296     return pid_to_ptid (-1);
297 }
298
299 int
300 in_thread_list (ptid_t ptid)
301 {
302   struct thread_info *tp;
303
304   for (tp = thread_list; tp; tp = tp->next)
305     if (ptid_equal (tp->ptid, ptid))
306       return 1;
307
308   return 0;                     /* Never heard of 'im */
309 }
310
311 /* Print a list of thread ids currently known, and the total number of
312    threads. To be used from within catch_errors. */
313 static int
314 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
315 {
316   struct thread_info *tp;
317   int num = 0;
318   struct cleanup *cleanup_chain;
319
320   prune_threads ();
321   target_find_new_threads ();
322
323   cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
324
325   for (tp = thread_list; tp; tp = tp->next)
326     {
327       num++;
328       ui_out_field_int (uiout, "thread-id", tp->num);
329     }
330
331   do_cleanups (cleanup_chain);
332   ui_out_field_int (uiout, "number-of-threads", num);
333   return GDB_RC_OK;
334 }
335
336 /* Official gdblib interface function to get a list of thread ids and
337    the total number. */
338 enum gdb_rc
339 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
340 {
341   if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
342                                  error_message, RETURN_MASK_ALL) < 0)
343     return GDB_RC_FAIL;
344   return GDB_RC_OK;
345 }
346
347 /* Load infrun state for the thread PID.  */
348
349 void
350 load_infrun_state (ptid_t ptid,
351                    CORE_ADDR *prev_pc,
352                    int *trap_expected,
353                    struct breakpoint **step_resume_breakpoint,
354                    CORE_ADDR *step_range_start,
355                    CORE_ADDR *step_range_end,
356                    struct frame_id *step_frame_id,
357                    int *stepping_over_breakpoint,
358                    int *stepping_through_solib_after_catch,
359                    bpstat *stepping_through_solib_catchpoints,
360                    int *current_line,
361                    struct symtab **current_symtab)
362 {
363   struct thread_info *tp;
364
365   /* If we can't find the thread, then we're debugging a single threaded
366      process.  No need to do anything in that case.  */
367   tp = find_thread_id (pid_to_thread_id (ptid));
368   if (tp == NULL)
369     return;
370
371   *prev_pc = tp->prev_pc;
372   *trap_expected = tp->trap_expected;
373   *step_resume_breakpoint = tp->step_resume_breakpoint;
374   *step_range_start = tp->step_range_start;
375   *step_range_end = tp->step_range_end;
376   *step_frame_id = tp->step_frame_id;
377   *stepping_over_breakpoint = tp->stepping_over_breakpoint;
378   *stepping_through_solib_after_catch =
379     tp->stepping_through_solib_after_catch;
380   *stepping_through_solib_catchpoints =
381     tp->stepping_through_solib_catchpoints;
382   *current_line = tp->current_line;
383   *current_symtab = tp->current_symtab;
384 }
385
386 /* Save infrun state for the thread PID.  */
387
388 void
389 save_infrun_state (ptid_t ptid,
390                    CORE_ADDR prev_pc,
391                    int trap_expected,
392                    struct breakpoint *step_resume_breakpoint,
393                    CORE_ADDR step_range_start,
394                    CORE_ADDR step_range_end,
395                    const struct frame_id *step_frame_id,
396                    int stepping_over_breakpoint,
397                    int stepping_through_solib_after_catch,
398                    bpstat stepping_through_solib_catchpoints,
399                    int current_line,
400                    struct symtab *current_symtab)
401 {
402   struct thread_info *tp;
403
404   /* If we can't find the thread, then we're debugging a single-threaded
405      process.  Nothing to do in that case.  */
406   tp = find_thread_id (pid_to_thread_id (ptid));
407   if (tp == NULL)
408     return;
409
410   tp->prev_pc = prev_pc;
411   tp->trap_expected = trap_expected;
412   tp->step_resume_breakpoint = step_resume_breakpoint;
413   tp->step_range_start = step_range_start;
414   tp->step_range_end = step_range_end;
415   tp->step_frame_id = (*step_frame_id);
416   tp->stepping_over_breakpoint = stepping_over_breakpoint;
417   tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
418   tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
419   tp->current_line = current_line;
420   tp->current_symtab = current_symtab;
421 }
422
423 /* Return true if TP is an active thread. */
424 static int
425 thread_alive (struct thread_info *tp)
426 {
427   if (PIDGET (tp->ptid) == -1)
428     return 0;
429   if (!target_thread_alive (tp->ptid))
430     {
431       tp->ptid = pid_to_ptid (-1);      /* Mark it as dead */
432       return 0;
433     }
434   return 1;
435 }
436
437 static void
438 prune_threads (void)
439 {
440   struct thread_info *tp, *next;
441
442   for (tp = thread_list; tp; tp = next)
443     {
444       next = tp->next;
445       if (!thread_alive (tp))
446         delete_thread (tp->ptid);
447     }
448 }
449
450 void
451 set_running (ptid_t ptid, int running)
452 {
453   struct thread_info *tp;
454
455   if (!thread_list)
456     {
457       /* This is one of the targets that does not add main
458          thread to the thread list.  Just use a single
459          global flag to indicate that a thread is running.  
460
461          This problem is unique to ST programs.  For MT programs,
462          the main thread is always present in the thread list.  If it's
463          not, the first call to context_switch will mess up GDB internal
464          state.  */
465       if (running && !main_thread_running && !suppress_resume_observer)
466         observer_notify_target_resumed (ptid);
467       main_thread_running = running;
468       return;
469     }
470
471   /* We try not to notify the observer if no thread has actually changed 
472      the running state -- merely to reduce the number of messages to 
473      frontend.  Frontend is supposed to handle multiple *running just fine.  */
474   if (PIDGET (ptid) == -1)
475     {
476       int any_started = 0;
477       for (tp = thread_list; tp; tp = tp->next)
478         {
479           if (running && !tp->running_)
480             any_started = 1;
481           tp->running_ = running;
482         }
483       if (any_started && !suppress_resume_observer)
484         observer_notify_target_resumed (ptid);      
485     }
486   else
487     {
488       tp = find_thread_pid (ptid);
489       gdb_assert (tp);
490       if (running && !tp->running_ && !suppress_resume_observer)
491         observer_notify_target_resumed (ptid);
492       tp->running_ = running;
493     }  
494 }
495
496 int
497 is_running (ptid_t ptid)
498 {
499   struct thread_info *tp;
500
501   if (!target_has_execution)
502     return 0;
503
504   if (!thread_list)
505     return main_thread_running;
506
507   tp = find_thread_pid (ptid);
508   gdb_assert (tp);
509   return tp->running_;  
510 }
511
512 int
513 any_running (void)
514 {
515   struct thread_info *tp;
516
517   if (!target_has_execution)
518     return 0;
519
520   if (!thread_list)
521     return main_thread_running;
522
523   for (tp = thread_list; tp; tp = tp->next)
524     if (tp->running_)
525       return 1;
526
527   return 0;
528 }
529
530 int
531 is_executing (ptid_t ptid)
532 {
533   struct thread_info *tp;
534
535   if (!target_has_execution)
536     return 0;
537
538   if (!thread_list)
539     return main_thread_executing;
540
541   tp = find_thread_pid (ptid);
542   gdb_assert (tp);
543   return tp->executing_;
544 }
545
546 void
547 set_executing (ptid_t ptid, int executing)
548 {
549   struct thread_info *tp;
550
551   if (!thread_list)
552     {
553       /* This target does not add the main thread to the thread list.
554          Use a global flag to indicate that the thread is
555          executing.  */
556       main_thread_executing = executing;
557       return;
558     }
559
560   if (PIDGET (ptid) == -1)
561     {
562       for (tp = thread_list; tp; tp = tp->next)
563         tp->executing_ = executing;
564     }
565   else
566     {
567       tp = find_thread_pid (ptid);
568       gdb_assert (tp);
569       tp->executing_ = executing;
570     }
571 }
572
573 /* Prints the list of threads and their details on UIOUT.
574    This is a version of 'info_thread_command' suitable for
575    use from MI.  
576    If REQESTED_THREAD is not -1, it's the GDB id of the thread
577    that should be printed.  Otherwise, all threads are
578    printed.  */
579 void
580 print_thread_info (struct ui_out *uiout, int requested_thread)
581 {
582   struct thread_info *tp;
583   ptid_t current_ptid;
584   struct frame_info *cur_frame;
585   struct cleanup *old_chain;
586   struct frame_id saved_frame_id;
587   char *extra_info;
588   int current_thread = -1;
589
590   /* Backup current thread and selected frame.  */
591   saved_frame_id = get_frame_id (get_selected_frame (NULL));
592   old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
593
594   make_cleanup_ui_out_list_begin_end (uiout, "threads");
595
596   prune_threads ();
597   target_find_new_threads ();
598   current_ptid = inferior_ptid;
599   for (tp = thread_list; tp; tp = tp->next)
600     {
601       struct cleanup *chain2;
602
603       if (requested_thread != -1 && tp->num != requested_thread)
604         continue;
605
606       chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
607
608       if (ptid_equal (tp->ptid, current_ptid))
609         {
610           current_thread = tp->num;
611           ui_out_text (uiout, "* ");
612         }
613       else
614         ui_out_text (uiout, "  ");
615
616       ui_out_field_int (uiout, "id", tp->num);
617       ui_out_text (uiout, " ");
618       ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid));
619
620       extra_info = target_extra_thread_info (tp);
621       if (extra_info)
622         {
623           ui_out_text (uiout, " (");
624           ui_out_field_string (uiout, "details", extra_info);
625           ui_out_text (uiout, ")");
626         }
627       ui_out_text (uiout, "  ");
628       /* That switch put us at the top of the stack (leaf frame).  */
629       switch_to_thread (tp->ptid);
630       print_stack_frame (get_selected_frame (NULL), 
631                          /* For MI output, print frame level.  */
632                          ui_out_is_mi_like_p (uiout),
633                          LOCATION);
634
635       do_cleanups (chain2);
636     }
637
638   /* Restores the current thread and the frame selected before
639      the "info threads" command.  */
640   do_cleanups (old_chain);
641
642   if (requested_thread == -1)
643     {
644       gdb_assert (current_thread != -1 || !thread_list);
645       if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
646         ui_out_field_int (uiout, "current-thread-id", current_thread);
647     }
648
649   /*  If case we were not able to find the original frame, print the
650       new selected frame.  */
651   if (frame_find_by_id (saved_frame_id) == NULL)
652     {
653       warning (_("Couldn't restore frame in current thread, at frame 0"));
654       /* For MI, we should probably have a notification about
655          current frame change.  But this error is not very likely, so
656          don't bother for now.  */
657       if (!ui_out_is_mi_like_p (uiout))
658         print_stack_frame (get_selected_frame (NULL), 0, LOCATION);
659     }
660 }
661
662
663 /* Print information about currently known threads 
664
665  * Note: this has the drawback that it _really_ switches
666  *       threads, which frees the frame cache.  A no-side
667  *       effects info-threads command would be nicer.
668  */
669
670 static void
671 info_threads_command (char *arg, int from_tty)
672 {
673   print_thread_info (uiout, -1);
674 }
675
676 /* Switch from one thread to another. */
677
678 void
679 switch_to_thread (ptid_t ptid)
680 {
681   if (ptid_equal (ptid, inferior_ptid))
682     return;
683
684   inferior_ptid = ptid;
685   reinit_frame_cache ();
686   registers_changed ();
687   stop_pc = read_pc ();
688 }
689
690 static void
691 restore_current_thread (ptid_t ptid)
692 {
693   if (!ptid_equal (ptid, inferior_ptid))
694     {
695       switch_to_thread (ptid);
696     }
697 }
698
699 static void
700 restore_selected_frame (struct frame_id a_frame_id)
701 {
702   struct frame_info *selected_frame_info = NULL;
703
704   if (frame_id_eq (a_frame_id, null_frame_id))
705     return;        
706
707   if ((selected_frame_info = frame_find_by_id (a_frame_id)) != NULL)
708     {
709       select_frame (selected_frame_info);
710     }
711 }
712
713 struct current_thread_cleanup
714 {
715   ptid_t inferior_ptid;
716   struct frame_id selected_frame_id;
717 };
718
719 static void
720 do_restore_current_thread_cleanup (void *arg)
721 {
722   struct current_thread_cleanup *old = arg;
723   restore_current_thread (old->inferior_ptid);
724   restore_selected_frame (old->selected_frame_id);
725   xfree (old);
726 }
727
728 struct cleanup *
729 make_cleanup_restore_current_thread (ptid_t inferior_ptid, 
730                                      struct frame_id a_frame_id)
731 {
732   struct current_thread_cleanup *old
733     = xmalloc (sizeof (struct current_thread_cleanup));
734   old->inferior_ptid = inferior_ptid;
735   old->selected_frame_id = a_frame_id;
736   return make_cleanup (do_restore_current_thread_cleanup, old);
737 }
738
739 /* Apply a GDB command to a list of threads.  List syntax is a whitespace
740    seperated list of numbers, or ranges, or the keyword `all'.  Ranges consist
741    of two numbers seperated by a hyphen.  Examples:
742
743    thread apply 1 2 7 4 backtrace       Apply backtrace cmd to threads 1,2,7,4
744    thread apply 2-7 9 p foo(1)  Apply p foo(1) cmd to threads 2->7 & 9
745    thread apply all p x/i $pc   Apply x/i $pc cmd to all threads
746  */
747
748 static void
749 thread_apply_all_command (char *cmd, int from_tty)
750 {
751   struct thread_info *tp;
752   struct cleanup *old_chain;
753   struct cleanup *saved_cmd_cleanup_chain;
754   char *saved_cmd;
755   struct frame_id saved_frame_id;
756   ptid_t current_ptid;
757   int thread_has_changed = 0;
758
759   if (cmd == NULL || *cmd == '\000')
760     error (_("Please specify a command following the thread ID list"));
761   
762   current_ptid = inferior_ptid;
763   saved_frame_id = get_frame_id (get_selected_frame (NULL));
764   old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
765
766   /* It is safe to update the thread list now, before
767      traversing it for "thread apply all".  MVS */
768   target_find_new_threads ();
769
770   /* Save a copy of the command in case it is clobbered by
771      execute_command */
772   saved_cmd = xstrdup (cmd);
773   saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
774   for (tp = thread_list; tp; tp = tp->next)
775     if (thread_alive (tp))
776       {
777         switch_to_thread (tp->ptid);
778         printf_filtered (_("\nThread %d (%s):\n"),
779                          tp->num, target_tid_to_str (inferior_ptid));
780         execute_command (cmd, from_tty);
781         strcpy (cmd, saved_cmd);        /* Restore exact command used previously */
782       }
783
784   if (!ptid_equal (current_ptid, inferior_ptid))
785     thread_has_changed = 1;
786
787   do_cleanups (saved_cmd_cleanup_chain);
788   do_cleanups (old_chain);
789   /* Print stack frame only if we changed thread.  */
790   if (thread_has_changed)
791     print_stack_frame (get_current_frame (), 1, SRC_LINE);
792
793 }
794
795 static void
796 thread_apply_command (char *tidlist, int from_tty)
797 {
798   char *cmd;
799   char *p;
800   struct cleanup *old_chain;
801   struct cleanup *saved_cmd_cleanup_chain;
802   char *saved_cmd;
803   struct frame_id saved_frame_id;
804   ptid_t current_ptid;
805   int thread_has_changed = 0;
806
807   if (tidlist == NULL || *tidlist == '\000')
808     error (_("Please specify a thread ID list"));
809
810   for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
811
812   if (*cmd == '\000')
813     error (_("Please specify a command following the thread ID list"));
814
815   current_ptid = inferior_ptid;
816   saved_frame_id = get_frame_id (get_selected_frame (NULL));
817   old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
818
819   /* Save a copy of the command in case it is clobbered by
820      execute_command */
821   saved_cmd = xstrdup (cmd);
822   saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
823   while (tidlist < cmd)
824     {
825       struct thread_info *tp;
826       int start, end;
827
828       start = strtol (tidlist, &p, 10);
829       if (p == tidlist)
830         error (_("Error parsing %s"), tidlist);
831       tidlist = p;
832
833       while (*tidlist == ' ' || *tidlist == '\t')
834         tidlist++;
835
836       if (*tidlist == '-')      /* Got a range of IDs? */
837         {
838           tidlist++;            /* Skip the - */
839           end = strtol (tidlist, &p, 10);
840           if (p == tidlist)
841             error (_("Error parsing %s"), tidlist);
842           tidlist = p;
843
844           while (*tidlist == ' ' || *tidlist == '\t')
845             tidlist++;
846         }
847       else
848         end = start;
849
850       for (; start <= end; start++)
851         {
852           tp = find_thread_id (start);
853
854           if (!tp)
855             warning (_("Unknown thread %d."), start);
856           else if (!thread_alive (tp))
857             warning (_("Thread %d has terminated."), start);
858           else
859             {
860               switch_to_thread (tp->ptid);
861               printf_filtered (_("\nThread %d (%s):\n"), tp->num,
862                                target_tid_to_str (inferior_ptid));
863               execute_command (cmd, from_tty);
864               strcpy (cmd, saved_cmd);  /* Restore exact command used previously */
865             }
866         }
867     }
868
869   if (!ptid_equal (current_ptid, inferior_ptid))
870     thread_has_changed = 1;
871
872   do_cleanups (saved_cmd_cleanup_chain);
873   do_cleanups (old_chain);
874   /* Print stack frame only if we changed thread.  */
875   if (thread_has_changed)
876     print_stack_frame (get_current_frame (), 1, SRC_LINE);
877 }
878
879 /* Switch to the specified thread.  Will dispatch off to thread_apply_command
880    if prefix of arg is `apply'.  */
881
882 static void
883 thread_command (char *tidstr, int from_tty)
884 {
885   if (!tidstr)
886     {
887       /* Don't generate an error, just say which thread is current. */
888       if (target_has_stack)
889         printf_filtered (_("[Current thread is %d (%s)]\n"),
890                          pid_to_thread_id (inferior_ptid),
891                          target_tid_to_str (inferior_ptid));
892       else
893         error (_("No stack."));
894       return;
895     }
896
897   annotate_thread_changed ();
898   gdb_thread_select (uiout, tidstr, NULL);
899 }
900
901 /* Print notices when new threads are attached and detached.  */
902 int print_thread_events = 1;
903 static void
904 show_print_thread_events (struct ui_file *file, int from_tty,
905                           struct cmd_list_element *c, const char *value)
906 {
907   fprintf_filtered (file, _("\
908 Printing of thread events is %s.\n"),
909                     value);
910 }
911
912 static int
913 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
914 {
915   int num;
916   struct thread_info *tp;
917
918   num = value_as_long (parse_and_eval (tidstr));
919
920   tp = find_thread_id (num);
921
922   if (!tp)
923     error (_("Thread ID %d not known."), num);
924
925   if (!thread_alive (tp))
926     error (_("Thread ID %d has terminated."), num);
927
928   switch_to_thread (tp->ptid);
929
930   ui_out_text (uiout, "[Switching to thread ");
931   ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
932   ui_out_text (uiout, " (");
933   ui_out_text (uiout, target_tid_to_str (inferior_ptid));
934   ui_out_text (uiout, ")]");
935
936   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
937   return GDB_RC_OK;
938 }
939
940 enum gdb_rc
941 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
942 {
943   if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
944                                  error_message, RETURN_MASK_ALL) < 0)
945     return GDB_RC_FAIL;
946   return GDB_RC_OK;
947 }
948
949 /* Commands with a prefix of `thread'.  */
950 struct cmd_list_element *thread_cmd_list = NULL;
951
952 void
953 _initialize_thread (void)
954 {
955   static struct cmd_list_element *thread_apply_list = NULL;
956
957   add_info ("threads", info_threads_command,
958             _("IDs of currently known threads."));
959
960   add_prefix_cmd ("thread", class_run, thread_command, _("\
961 Use this command to switch between threads.\n\
962 The new thread ID must be currently known."),
963                   &thread_cmd_list, "thread ", 1, &cmdlist);
964
965   add_prefix_cmd ("apply", class_run, thread_apply_command,
966                   _("Apply a command to a list of threads."),
967                   &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
968
969   add_cmd ("all", class_run, thread_apply_all_command,
970            _("Apply a command to all threads."), &thread_apply_list);
971
972   if (!xdb_commands)
973     add_com_alias ("t", "thread", class_run, 1);
974
975   add_setshow_boolean_cmd ("thread-events", no_class,
976          &print_thread_events, _("\
977 Set printing of thread events (such as thread start and exit)."), _("\
978 Show printing of thread events (such as thread start and exit)."), NULL,
979          NULL,
980          show_print_thread_events,
981          &setprintlist, &showprintlist);
982 }