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