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