Remove the global stop_step in favour of a per-thread
[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 #include "cli/cli-decode.h"
45
46 /* Definition of struct thread_info exported to gdbthread.h */
47
48 /* Prototypes for exported functions. */
49
50 void _initialize_thread (void);
51
52 /* Prototypes for local functions. */
53
54 static struct thread_info *thread_list = NULL;
55 static int highest_thread_num;
56
57 static void thread_command (char *tidstr, int from_tty);
58 static void thread_apply_all_command (char *, int);
59 static int thread_alive (struct thread_info *);
60 static void info_threads_command (char *, int);
61 static void thread_apply_command (char *, int);
62 static void restore_current_thread (ptid_t);
63 static void prune_threads (void);
64
65 /* Frontend view of the thread state. Possible extensions: stepping,
66 finishing, until(ling),... */
67 enum thread_state
68 {
69 THREAD_STOPPED,
70 THREAD_RUNNING,
71 THREAD_EXITED,
72 };
73
74 static enum thread_state main_thread_state = THREAD_STOPPED;
75 static int main_thread_executing = 0;
76
77 extern struct thread_info*
78 inferior_thread (void)
79 {
80 struct thread_info *tp = find_thread_pid (inferior_ptid);
81 gdb_assert (tp);
82 return tp;
83 }
84
85 void
86 delete_step_resume_breakpoint (struct thread_info *tp)
87 {
88 if (tp && tp->step_resume_breakpoint)
89 {
90 delete_breakpoint (tp->step_resume_breakpoint);
91 tp->step_resume_breakpoint = NULL;
92 }
93 }
94
95 static void
96 clear_thread_inferior_resources (struct thread_info *tp)
97 {
98 /* NOTE: this will take care of any left-over step_resume breakpoints,
99 but not any user-specified thread-specific breakpoints. We can not
100 delete the breakpoint straight-off, because the inferior might not
101 be stopped at the moment. */
102 if (tp->step_resume_breakpoint)
103 {
104 tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
105 tp->step_resume_breakpoint = NULL;
106 }
107
108 bpstat_clear (&tp->stop_bpstat);
109 }
110
111 static void
112 free_thread (struct thread_info *tp)
113 {
114 clear_thread_inferior_resources (tp);
115
116 /* FIXME: do I ever need to call the back-end to give it a
117 chance at this private data before deleting the thread? */
118 if (tp->private)
119 xfree (tp->private);
120
121 xfree (tp);
122 }
123
124 void
125 init_thread_list (void)
126 {
127 struct thread_info *tp, *tpnext;
128
129 highest_thread_num = 0;
130 main_thread_state = THREAD_STOPPED;
131 main_thread_executing = 0;
132
133 if (!thread_list)
134 return;
135
136 for (tp = thread_list; tp; tp = tpnext)
137 {
138 tpnext = tp->next;
139 free_thread (tp);
140 }
141
142 thread_list = NULL;
143 }
144
145 struct thread_info *
146 add_thread_silent (ptid_t ptid)
147 {
148 struct thread_info *tp;
149
150 tp = find_thread_pid (ptid);
151 if (tp)
152 /* Found an old thread with the same id. It has to be dead,
153 otherwise we wouldn't be adding a new thread with the same id.
154 The OS is reusing this id --- delete it, and recreate a new
155 one. */
156 {
157 /* In addition to deleting the thread, if this is the current
158 thread, then we need to also get rid of the current infrun
159 context, and take care that delete_thread doesn't really
160 delete the thread if it is inferior_ptid. Create a new
161 template thread in the list with an invalid ptid, context
162 switch to it, delete the original thread, reset the new
163 thread's ptid, and switch to it. */
164
165 if (ptid_equal (inferior_ptid, ptid))
166 {
167 tp = xmalloc (sizeof (*tp));
168 memset (tp, 0, sizeof (*tp));
169 tp->ptid = minus_one_ptid;
170 tp->num = ++highest_thread_num;
171 tp->next = thread_list;
172 thread_list = tp;
173 context_switch_to (minus_one_ptid);
174
175 /* Now we can delete it. */
176 delete_thread (ptid);
177
178 /* Since the context is already set to this new thread,
179 reset its ptid, and reswitch inferior_ptid to it. */
180 tp->ptid = ptid;
181 switch_to_thread (ptid);
182
183 observer_notify_new_thread (tp);
184
185 /* All done. */
186 return tp;
187 }
188 else
189 /* Just go ahead and delete it. */
190 delete_thread (ptid);
191 }
192
193 tp = (struct thread_info *) xmalloc (sizeof (*tp));
194 memset (tp, 0, sizeof (*tp));
195 tp->ptid = ptid;
196 tp->num = ++highest_thread_num;
197 tp->next = thread_list;
198 thread_list = tp;
199
200 observer_notify_new_thread (tp);
201
202 return tp;
203 }
204
205 struct thread_info *
206 add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
207 {
208 struct thread_info *result = add_thread_silent (ptid);
209
210 result->private = private;
211
212 if (print_thread_events)
213 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
214
215 annotate_new_thread ();
216 return result;
217 }
218
219 struct thread_info *
220 add_thread (ptid_t ptid)
221 {
222 return add_thread_with_info (ptid, NULL);
223 }
224
225 /* Delete thread PTID. If SILENT, don't notify the observer of this
226 exit. */
227 static void
228 delete_thread_1 (ptid_t ptid, int silent)
229 {
230 struct thread_info *tp, *tpprev;
231
232 tpprev = NULL;
233
234 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
235 if (ptid_equal (tp->ptid, ptid))
236 break;
237
238 if (!tp)
239 return;
240
241 /* If this is the current thread, or there's code out there that
242 relies on it existing (refcount > 0) we can't delete yet. Mark
243 it as exited, and notify it. */
244 if (tp->refcount > 0
245 || ptid_equal (tp->ptid, inferior_ptid))
246 {
247 if (tp->state_ != THREAD_EXITED)
248 {
249 if (!silent)
250 observer_notify_thread_exit (tp);
251
252 /* Tag it as exited. */
253 tp->state_ = THREAD_EXITED;
254
255 /* Clear breakpoints, etc. associated with this thread. */
256 clear_thread_inferior_resources (tp);
257 }
258
259 /* Will be really deleted some other time. */
260 return;
261 }
262
263 if (tpprev)
264 tpprev->next = tp->next;
265 else
266 thread_list = tp->next;
267
268 /* Notify thread exit, but only if we haven't already. */
269 if (!silent && tp->state_ != THREAD_EXITED)
270 observer_notify_thread_exit (tp);
271
272 free_thread (tp);
273 }
274
275 /* Delete thread PTID and notify of thread exit. If this is
276 inferior_ptid, don't actually delete it, but tag it as exited and
277 do the notification. If PTID is the user selected thread, clear
278 it. */
279 void
280 delete_thread (ptid_t ptid)
281 {
282 delete_thread_1 (ptid, 0 /* not silent */);
283 }
284
285 void
286 delete_thread_silent (ptid_t ptid)
287 {
288 delete_thread_1 (ptid, 1 /* silent */);
289 }
290
291 struct thread_info *
292 find_thread_id (int num)
293 {
294 struct thread_info *tp;
295
296 for (tp = thread_list; tp; tp = tp->next)
297 if (tp->num == num)
298 return tp;
299
300 return NULL;
301 }
302
303 /* Find a thread_info by matching PTID. */
304 struct thread_info *
305 find_thread_pid (ptid_t ptid)
306 {
307 struct thread_info *tp;
308
309 for (tp = thread_list; tp; tp = tp->next)
310 if (ptid_equal (tp->ptid, ptid))
311 return tp;
312
313 return NULL;
314 }
315
316 /*
317 * Thread iterator function.
318 *
319 * Calls a callback function once for each thread, so long as
320 * the callback function returns false. If the callback function
321 * returns true, the iteration will end and the current thread
322 * will be returned. This can be useful for implementing a
323 * search for a thread with arbitrary attributes, or for applying
324 * some operation to every thread.
325 *
326 * FIXME: some of the existing functionality, such as
327 * "Thread apply all", might be rewritten using this functionality.
328 */
329
330 struct thread_info *
331 iterate_over_threads (int (*callback) (struct thread_info *, void *),
332 void *data)
333 {
334 struct thread_info *tp, *next;
335
336 for (tp = thread_list; tp; tp = next)
337 {
338 next = tp->next;
339 if ((*callback) (tp, data))
340 return tp;
341 }
342
343 return NULL;
344 }
345
346 int
347 thread_count (void)
348 {
349 int result = 0;
350 struct thread_info *tp;
351
352 for (tp = thread_list; tp; tp = tp->next)
353 ++result;
354
355 return result;
356 }
357
358 int
359 valid_thread_id (int num)
360 {
361 struct thread_info *tp;
362
363 for (tp = thread_list; tp; tp = tp->next)
364 if (tp->num == num)
365 return 1;
366
367 return 0;
368 }
369
370 int
371 pid_to_thread_id (ptid_t ptid)
372 {
373 struct thread_info *tp;
374
375 for (tp = thread_list; tp; tp = tp->next)
376 if (ptid_equal (tp->ptid, ptid))
377 return tp->num;
378
379 return 0;
380 }
381
382 ptid_t
383 thread_id_to_pid (int num)
384 {
385 struct thread_info *thread = find_thread_id (num);
386 if (thread)
387 return thread->ptid;
388 else
389 return pid_to_ptid (-1);
390 }
391
392 int
393 in_thread_list (ptid_t ptid)
394 {
395 struct thread_info *tp;
396
397 for (tp = thread_list; tp; tp = tp->next)
398 if (ptid_equal (tp->ptid, ptid))
399 return 1;
400
401 return 0; /* Never heard of 'im */
402 }
403
404 /* Print a list of thread ids currently known, and the total number of
405 threads. To be used from within catch_errors. */
406 static int
407 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
408 {
409 struct thread_info *tp;
410 int num = 0;
411 struct cleanup *cleanup_chain;
412
413 prune_threads ();
414 target_find_new_threads ();
415
416 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
417
418 for (tp = thread_list; tp; tp = tp->next)
419 {
420 if (tp->state_ == THREAD_EXITED)
421 continue;
422 num++;
423 ui_out_field_int (uiout, "thread-id", tp->num);
424 }
425
426 do_cleanups (cleanup_chain);
427 ui_out_field_int (uiout, "number-of-threads", num);
428 return GDB_RC_OK;
429 }
430
431 /* Official gdblib interface function to get a list of thread ids and
432 the total number. */
433 enum gdb_rc
434 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
435 {
436 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
437 error_message, RETURN_MASK_ALL) < 0)
438 return GDB_RC_FAIL;
439 return GDB_RC_OK;
440 }
441
442 /* Load infrun state for the thread PID. */
443
444 void
445 load_infrun_state (ptid_t ptid,
446 struct continuation **continuations,
447 struct continuation **intermediate_continuations)
448 {
449 struct thread_info *tp;
450
451 /* If we can't find the thread, then we're debugging a single threaded
452 process. No need to do anything in that case. */
453 tp = find_thread_id (pid_to_thread_id (ptid));
454 if (tp == NULL)
455 return;
456
457 /* In all-stop mode, these are global state, while in non-stop mode,
458 they are per thread. */
459 if (non_stop)
460 {
461 *continuations = tp->continuations;
462 tp->continuations = NULL;
463 *intermediate_continuations = tp->intermediate_continuations;
464 tp->intermediate_continuations = NULL;
465 }
466 }
467
468 /* Save infrun state for the thread PID. */
469
470 void
471 save_infrun_state (ptid_t ptid,
472 struct continuation *continuations,
473 struct continuation *intermediate_continuations)
474 {
475 struct thread_info *tp;
476
477 /* If we can't find the thread, then we're debugging a single-threaded
478 process. Nothing to do in that case. */
479 tp = find_thread_id (pid_to_thread_id (ptid));
480 if (tp == NULL)
481 return;
482
483 /* In all-stop mode, these are global state, while in non-stop mode,
484 they are per thread. */
485 if (non_stop)
486 {
487 tp->continuations = continuations;
488 tp->intermediate_continuations = intermediate_continuations;
489 }
490 }
491
492 /* Return true if TP is an active thread. */
493 static int
494 thread_alive (struct thread_info *tp)
495 {
496 if (tp->state_ == THREAD_EXITED)
497 return 0;
498 if (!target_thread_alive (tp->ptid))
499 return 0;
500 return 1;
501 }
502
503 static void
504 prune_threads (void)
505 {
506 struct thread_info *tp, *next;
507
508 for (tp = thread_list; tp; tp = next)
509 {
510 next = tp->next;
511 if (!thread_alive (tp))
512 delete_thread (tp->ptid);
513 }
514 }
515
516 void
517 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
518 {
519 struct thread_info * tp = find_thread_pid (old_ptid);
520 tp->ptid = new_ptid;
521
522 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
523 }
524
525 void
526 set_running (ptid_t ptid, int running)
527 {
528 struct thread_info *tp;
529
530 if (!thread_list)
531 {
532 /* This is one of the targets that does not add main
533 thread to the thread list. Just use a single
534 global flag to indicate that a thread is running.
535
536 This problem is unique to ST programs. For MT programs,
537 the main thread is always present in the thread list. If it's
538 not, the first call to context_switch will mess up GDB internal
539 state. */
540 if (running
541 && main_thread_state != THREAD_RUNNING
542 && !suppress_resume_observer)
543 observer_notify_target_resumed (ptid);
544 main_thread_state = running ? THREAD_RUNNING : THREAD_STOPPED;
545 return;
546 }
547
548 /* We try not to notify the observer if no thread has actually changed
549 the running state -- merely to reduce the number of messages to
550 frontend. Frontend is supposed to handle multiple *running just fine. */
551 if (PIDGET (ptid) == -1)
552 {
553 int any_started = 0;
554 for (tp = thread_list; tp; tp = tp->next)
555 {
556 if (tp->state_ == THREAD_EXITED)
557 continue;
558 if (running && tp->state_ == THREAD_STOPPED)
559 any_started = 1;
560 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
561 }
562 if (any_started && !suppress_resume_observer)
563 observer_notify_target_resumed (ptid);
564 }
565 else
566 {
567 int started = 0;
568 tp = find_thread_pid (ptid);
569 gdb_assert (tp);
570 gdb_assert (tp->state_ != THREAD_EXITED);
571 if (running && tp->state_ == THREAD_STOPPED)
572 started = 1;
573 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
574 if (started && !suppress_resume_observer)
575 observer_notify_target_resumed (ptid);
576 }
577 }
578
579 static int
580 is_thread_state (ptid_t ptid, enum thread_state state)
581 {
582 struct thread_info *tp;
583
584 if (!target_has_execution)
585 return 0;
586
587 if (!thread_list)
588 return main_thread_state == state;
589
590 tp = find_thread_pid (ptid);
591 gdb_assert (tp);
592 return tp->state_ == state;
593 }
594
595 int
596 is_stopped (ptid_t ptid)
597 {
598 /* Without execution, this property is always true. */
599 if (!target_has_execution)
600 return 1;
601
602 return is_thread_state (ptid, THREAD_STOPPED);
603 }
604
605 int
606 is_exited (ptid_t ptid)
607 {
608 /* Without execution, this property is always false. */
609 if (!target_has_execution)
610 return 0;
611
612 return is_thread_state (ptid, THREAD_EXITED);
613 }
614
615 int
616 is_running (ptid_t ptid)
617 {
618 /* Without execution, this property is always false. */
619 if (!target_has_execution)
620 return 0;
621
622 return is_thread_state (ptid, THREAD_RUNNING);
623 }
624
625 int
626 any_running (void)
627 {
628 struct thread_info *tp;
629
630 if (!target_has_execution)
631 return 0;
632
633 if (!thread_list)
634 return main_thread_state == THREAD_RUNNING;
635
636 for (tp = thread_list; tp; tp = tp->next)
637 if (tp->state_ == THREAD_RUNNING)
638 return 1;
639
640 return 0;
641 }
642
643 int
644 is_executing (ptid_t ptid)
645 {
646 struct thread_info *tp;
647
648 if (!target_has_execution)
649 return 0;
650
651 if (!thread_list)
652 return main_thread_executing;
653
654 tp = find_thread_pid (ptid);
655 gdb_assert (tp);
656 return tp->executing_;
657 }
658
659 void
660 set_executing (ptid_t ptid, int executing)
661 {
662 struct thread_info *tp;
663
664 if (!thread_list)
665 {
666 /* This target does not add the main thread to the thread list.
667 Use a global flag to indicate that the thread is
668 executing. */
669 main_thread_executing = executing;
670 return;
671 }
672
673 if (PIDGET (ptid) == -1)
674 {
675 for (tp = thread_list; tp; tp = tp->next)
676 tp->executing_ = executing;
677 }
678 else
679 {
680 tp = find_thread_pid (ptid);
681 gdb_assert (tp);
682 tp->executing_ = executing;
683 }
684 }
685
686 /* Prints the list of threads and their details on UIOUT.
687 This is a version of 'info_thread_command' suitable for
688 use from MI.
689 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
690 that should be printed. Otherwise, all threads are
691 printed. */
692 void
693 print_thread_info (struct ui_out *uiout, int requested_thread)
694 {
695 struct thread_info *tp;
696 ptid_t current_ptid;
697 struct cleanup *old_chain;
698 char *extra_info;
699 int current_thread = -1;
700
701 prune_threads ();
702 target_find_new_threads ();
703 current_ptid = inferior_ptid;
704
705 /* We'll be switching threads temporarily. */
706 old_chain = make_cleanup_restore_current_thread ();
707
708 make_cleanup_ui_out_list_begin_end (uiout, "threads");
709 for (tp = thread_list; tp; tp = tp->next)
710 {
711 struct cleanup *chain2;
712
713 if (requested_thread != -1 && tp->num != requested_thread)
714 continue;
715
716 if (ptid_equal (tp->ptid, current_ptid))
717 current_thread = tp->num;
718
719 if (tp->state_ == THREAD_EXITED)
720 continue;
721
722 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
723
724 if (ptid_equal (tp->ptid, current_ptid))
725 ui_out_text (uiout, "* ");
726 else
727 ui_out_text (uiout, " ");
728
729 ui_out_field_int (uiout, "id", tp->num);
730 ui_out_text (uiout, " ");
731 ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid));
732
733 if (tp->state_ != THREAD_EXITED)
734 {
735 extra_info = target_extra_thread_info (tp);
736 if (extra_info)
737 {
738 ui_out_text (uiout, " (");
739 ui_out_field_string (uiout, "details", extra_info);
740 ui_out_text (uiout, ")");
741 }
742 ui_out_text (uiout, " ");
743 }
744
745 if (tp->state_ == THREAD_RUNNING)
746 ui_out_text (uiout, "(running)\n");
747 else
748 {
749 /* The switch below puts us at the top of the stack (leaf
750 frame). */
751 switch_to_thread (tp->ptid);
752 print_stack_frame (get_selected_frame (NULL),
753 /* For MI output, print frame level. */
754 ui_out_is_mi_like_p (uiout),
755 LOCATION);
756 }
757
758 if (ui_out_is_mi_like_p (uiout))
759 {
760 char *state = "stopped";
761 if (tp->state_ == THREAD_EXITED)
762 state = "exited";
763 else if (tp->state_ == THREAD_RUNNING)
764 state = "running";
765 ui_out_field_string (uiout, "state", state);
766 }
767
768 do_cleanups (chain2);
769 }
770
771 /* Restores the current thread and the frame selected before
772 the "info threads" command. */
773 do_cleanups (old_chain);
774
775 if (requested_thread == -1)
776 {
777 gdb_assert (current_thread != -1
778 || !thread_list);
779 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
780 ui_out_field_int (uiout, "current-thread-id", current_thread);
781
782 if (current_thread != -1 && is_exited (current_ptid))
783 ui_out_message (uiout, 0, "\n\
784 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
785 current_thread);
786 }
787 }
788
789
790 /* Print information about currently known threads
791
792 * Note: this has the drawback that it _really_ switches
793 * threads, which frees the frame cache. A no-side
794 * effects info-threads command would be nicer.
795 */
796
797 static void
798 info_threads_command (char *arg, int from_tty)
799 {
800 print_thread_info (uiout, -1);
801 }
802
803 /* Switch from one thread to another. */
804
805 void
806 switch_to_thread (ptid_t ptid)
807 {
808 if (ptid_equal (ptid, inferior_ptid))
809 return;
810
811 inferior_ptid = ptid;
812 reinit_frame_cache ();
813 registers_changed ();
814
815 /* We don't check for is_stopped, because we're called at times
816 while in the TARGET_RUNNING state, e.g., while handling an
817 internal event. */
818 if (!is_exited (ptid) && !is_executing (ptid))
819 stop_pc = read_pc ();
820 else
821 stop_pc = ~(CORE_ADDR) 0;
822 }
823
824 static void
825 restore_current_thread (ptid_t ptid)
826 {
827 if (!ptid_equal (ptid, inferior_ptid))
828 {
829 if (non_stop)
830 context_switch_to (ptid);
831 else
832 switch_to_thread (ptid);
833 }
834 }
835
836 static void
837 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
838 {
839 struct frame_info *frame = NULL;
840 int count;
841
842 gdb_assert (frame_level >= 0);
843
844 /* Restore by level first, check if the frame id is the same as
845 expected. If that fails, try restoring by frame id. If that
846 fails, nothing to do, just warn the user. */
847
848 count = frame_level;
849 frame = find_relative_frame (get_current_frame (), &count);
850 if (count == 0
851 && frame != NULL
852 /* Either the frame ids match, of they're both invalid. The
853 latter case is not failsafe, but since it's highly unlikely
854 the search by level finds the wrong frame, it's 99.9(9)% of
855 the time (for all practical purposes) safe. */
856 && (frame_id_eq (get_frame_id (frame), a_frame_id)
857 /* Note: could be better to check every frame_id
858 member for equality here. */
859 || (!frame_id_p (get_frame_id (frame))
860 && !frame_id_p (a_frame_id))))
861 {
862 /* Cool, all is fine. */
863 select_frame (frame);
864 return;
865 }
866
867 frame = frame_find_by_id (a_frame_id);
868 if (frame != NULL)
869 {
870 /* Cool, refound it. */
871 select_frame (frame);
872 return;
873 }
874
875 /* Nothing else to do, the frame layout really changed. Select the
876 innermost stack frame. */
877 select_frame (get_current_frame ());
878
879 /* Warn the user. */
880 if (!ui_out_is_mi_like_p (uiout))
881 {
882 warning (_("\
883 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
884 frame_level);
885 /* For MI, we should probably have a notification about
886 current frame change. But this error is not very
887 likely, so don't bother for now. */
888 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
889 }
890 }
891
892 struct current_thread_cleanup
893 {
894 ptid_t inferior_ptid;
895 struct frame_id selected_frame_id;
896 int selected_frame_level;
897 int was_stopped;
898 };
899
900 static void
901 do_restore_current_thread_cleanup (void *arg)
902 {
903 struct thread_info *tp;
904 struct current_thread_cleanup *old = arg;
905 restore_current_thread (old->inferior_ptid);
906
907 /* The running state of the originally selected thread may have
908 changed, so we have to recheck it here. */
909 if (old->was_stopped
910 && is_stopped (inferior_ptid)
911 && target_has_registers
912 && target_has_stack
913 && target_has_memory)
914 restore_selected_frame (old->selected_frame_id,
915 old->selected_frame_level);
916 }
917
918 static void
919 restore_current_thread_cleanup_dtor (void *arg)
920 {
921 struct current_thread_cleanup *old = arg;
922 struct thread_info *tp;
923 tp = find_thread_pid (old->inferior_ptid);
924 if (tp)
925 tp->refcount--;
926 xfree (old);
927 }
928
929 struct cleanup *
930 make_cleanup_restore_current_thread (void)
931 {
932 struct thread_info *tp;
933 struct frame_info *frame;
934 struct current_thread_cleanup *old;
935
936 old = xmalloc (sizeof (struct current_thread_cleanup));
937 old->inferior_ptid = inferior_ptid;
938 old->was_stopped = is_stopped (inferior_ptid);
939 if (old->was_stopped
940 && target_has_registers
941 && target_has_stack
942 && target_has_memory)
943 frame = get_selected_frame (NULL);
944 else
945 frame = NULL;
946
947 old->selected_frame_id = get_frame_id (frame);
948 old->selected_frame_level = frame_relative_level (frame);
949
950 tp = find_thread_pid (inferior_ptid);
951 if (tp)
952 tp->refcount++;
953
954 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
955 restore_current_thread_cleanup_dtor);
956 }
957
958 /* Apply a GDB command to a list of threads. List syntax is a whitespace
959 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
960 of two numbers seperated by a hyphen. Examples:
961
962 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
963 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
964 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
965 */
966
967 static void
968 thread_apply_all_command (char *cmd, int from_tty)
969 {
970 struct thread_info *tp;
971 struct cleanup *old_chain;
972 char *saved_cmd;
973
974 if (cmd == NULL || *cmd == '\000')
975 error (_("Please specify a command following the thread ID list"));
976
977 prune_threads ();
978 target_find_new_threads ();
979
980 old_chain = make_cleanup_restore_current_thread ();
981
982 /* Save a copy of the command in case it is clobbered by
983 execute_command */
984 saved_cmd = xstrdup (cmd);
985 make_cleanup (xfree, saved_cmd);
986 for (tp = thread_list; tp; tp = tp->next)
987 if (thread_alive (tp))
988 {
989 if (non_stop)
990 context_switch_to (tp->ptid);
991 else
992 switch_to_thread (tp->ptid);
993
994 printf_filtered (_("\nThread %d (%s):\n"),
995 tp->num, target_tid_to_str (inferior_ptid));
996 execute_command (cmd, from_tty);
997 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
998 }
999
1000 do_cleanups (old_chain);
1001 }
1002
1003 static void
1004 thread_apply_command (char *tidlist, int from_tty)
1005 {
1006 char *cmd;
1007 char *p;
1008 struct cleanup *old_chain;
1009 char *saved_cmd;
1010
1011 if (tidlist == NULL || *tidlist == '\000')
1012 error (_("Please specify a thread ID list"));
1013
1014 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1015
1016 if (*cmd == '\000')
1017 error (_("Please specify a command following the thread ID list"));
1018
1019 /* Save a copy of the command in case it is clobbered by
1020 execute_command */
1021 saved_cmd = xstrdup (cmd);
1022 old_chain = make_cleanup (xfree, saved_cmd);
1023 while (tidlist < cmd)
1024 {
1025 struct thread_info *tp;
1026 int start, end;
1027
1028 start = strtol (tidlist, &p, 10);
1029 if (p == tidlist)
1030 error (_("Error parsing %s"), tidlist);
1031 tidlist = p;
1032
1033 while (*tidlist == ' ' || *tidlist == '\t')
1034 tidlist++;
1035
1036 if (*tidlist == '-') /* Got a range of IDs? */
1037 {
1038 tidlist++; /* Skip the - */
1039 end = strtol (tidlist, &p, 10);
1040 if (p == tidlist)
1041 error (_("Error parsing %s"), tidlist);
1042 tidlist = p;
1043
1044 while (*tidlist == ' ' || *tidlist == '\t')
1045 tidlist++;
1046 }
1047 else
1048 end = start;
1049
1050 make_cleanup_restore_current_thread ();
1051
1052 for (; start <= end; start++)
1053 {
1054 tp = find_thread_id (start);
1055
1056 if (!tp)
1057 warning (_("Unknown thread %d."), start);
1058 else if (!thread_alive (tp))
1059 warning (_("Thread %d has terminated."), start);
1060 else
1061 {
1062 if (non_stop)
1063 context_switch_to (tp->ptid);
1064 else
1065 switch_to_thread (tp->ptid);
1066
1067 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1068 target_tid_to_str (inferior_ptid));
1069 execute_command (cmd, from_tty);
1070
1071 /* Restore exact command used previously. */
1072 strcpy (cmd, saved_cmd);
1073 }
1074 }
1075 }
1076
1077 do_cleanups (old_chain);
1078 }
1079
1080 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1081 if prefix of arg is `apply'. */
1082
1083 static void
1084 thread_command (char *tidstr, int from_tty)
1085 {
1086 if (!tidstr)
1087 {
1088 if (target_has_stack)
1089 {
1090 if (is_exited (inferior_ptid))
1091 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1092 pid_to_thread_id (inferior_ptid),
1093 target_tid_to_str (inferior_ptid));
1094 else
1095 printf_filtered (_("[Current thread is %d (%s)]\n"),
1096 pid_to_thread_id (inferior_ptid),
1097 target_tid_to_str (inferior_ptid));
1098 }
1099 else
1100 error (_("No stack."));
1101 return;
1102 }
1103
1104 annotate_thread_changed ();
1105 gdb_thread_select (uiout, tidstr, NULL);
1106 }
1107
1108 /* Print notices when new threads are attached and detached. */
1109 int print_thread_events = 1;
1110 static void
1111 show_print_thread_events (struct ui_file *file, int from_tty,
1112 struct cmd_list_element *c, const char *value)
1113 {
1114 fprintf_filtered (file, _("\
1115 Printing of thread events is %s.\n"),
1116 value);
1117 }
1118
1119 static int
1120 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1121 {
1122 int num;
1123 struct thread_info *tp;
1124
1125 num = value_as_long (parse_and_eval (tidstr));
1126
1127 tp = find_thread_id (num);
1128
1129 if (!tp)
1130 error (_("Thread ID %d not known."), num);
1131
1132 if (!thread_alive (tp))
1133 error (_("Thread ID %d has terminated."), num);
1134
1135 if (non_stop)
1136 context_switch_to (tp->ptid);
1137 else
1138 switch_to_thread (tp->ptid);
1139
1140 ui_out_text (uiout, "[Switching to thread ");
1141 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1142 ui_out_text (uiout, " (");
1143 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
1144 ui_out_text (uiout, ")]");
1145
1146 /* Note that we can't reach this with an exited thread, due to the
1147 thread_alive check above. */
1148 if (tp->state_ == THREAD_RUNNING)
1149 ui_out_text (uiout, "(running)\n");
1150 else
1151 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1152
1153 /* Since the current thread may have changed, see if there is any
1154 exited thread we can now delete. */
1155 prune_threads ();
1156
1157 return GDB_RC_OK;
1158 }
1159
1160 enum gdb_rc
1161 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1162 {
1163 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1164 error_message, RETURN_MASK_ALL) < 0)
1165 return GDB_RC_FAIL;
1166 return GDB_RC_OK;
1167 }
1168
1169 /* Commands with a prefix of `thread'. */
1170 struct cmd_list_element *thread_cmd_list = NULL;
1171
1172 void
1173 _initialize_thread (void)
1174 {
1175 static struct cmd_list_element *thread_apply_list = NULL;
1176 struct cmd_list_element *c;
1177
1178 c = add_info ("threads", info_threads_command,
1179 _("IDs of currently known threads."));
1180 set_cmd_no_selected_thread_ok (c);
1181
1182 c = add_prefix_cmd ("thread", class_run, thread_command, _("\
1183 Use this command to switch between threads.\n\
1184 The new thread ID must be currently known."),
1185 &thread_cmd_list, "thread ", 1, &cmdlist);
1186 set_cmd_no_selected_thread_ok (c);
1187
1188 c = add_prefix_cmd ("apply", class_run, thread_apply_command,
1189 _("Apply a command to a list of threads."),
1190 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1191 set_cmd_no_selected_thread_ok (c);
1192
1193 c = add_cmd ("all", class_run, thread_apply_all_command,
1194 _("Apply a command to all threads."), &thread_apply_list);
1195 set_cmd_no_selected_thread_ok (c);
1196
1197 if (!xdb_commands)
1198 add_com_alias ("t", "thread", class_run, 1);
1199
1200 add_setshow_boolean_cmd ("thread-events", no_class,
1201 &print_thread_events, _("\
1202 Set printing of thread events (such as thread start and exit)."), _("\
1203 Show printing of thread events (such as thread start and exit)."), NULL,
1204 NULL,
1205 show_print_thread_events,
1206 &setprintlist, &showprintlist);
1207 }
This page took 0.064303 seconds and 5 git commands to generate.