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