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