Implement -list-thread-groups.
[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 extern struct thread_info*
75 inferior_thread (void)
76 {
77 struct thread_info *tp = find_thread_pid (inferior_ptid);
78 gdb_assert (tp);
79 return tp;
80 }
81
82 void
83 delete_step_resume_breakpoint (struct thread_info *tp)
84 {
85 if (tp && tp->step_resume_breakpoint)
86 {
87 delete_breakpoint (tp->step_resume_breakpoint);
88 tp->step_resume_breakpoint = NULL;
89 }
90 }
91
92 static void
93 clear_thread_inferior_resources (struct thread_info *tp)
94 {
95 /* NOTE: this will take care of any left-over step_resume breakpoints,
96 but not any user-specified thread-specific breakpoints. We can not
97 delete the breakpoint straight-off, because the inferior might not
98 be stopped at the moment. */
99 if (tp->step_resume_breakpoint)
100 {
101 tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
102 tp->step_resume_breakpoint = NULL;
103 }
104
105 bpstat_clear (&tp->stop_bpstat);
106
107 discard_all_intermediate_continuations_thread (tp);
108 discard_all_continuations_thread (tp);
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
131 if (!thread_list)
132 return;
133
134 for (tp = thread_list; tp; tp = tpnext)
135 {
136 tpnext = tp->next;
137 free_thread (tp);
138 }
139
140 thread_list = NULL;
141 }
142
143 struct thread_info *
144 add_thread_silent (ptid_t ptid)
145 {
146 struct thread_info *tp;
147
148 tp = find_thread_pid (ptid);
149 if (tp)
150 /* Found an old thread with the same id. It has to be dead,
151 otherwise we wouldn't be adding a new thread with the same id.
152 The OS is reusing this id --- delete it, and recreate a new
153 one. */
154 {
155 /* In addition to deleting the thread, if this is the current
156 thread, then we need to take care that delete_thread doesn't
157 really delete the thread if it is inferior_ptid. Create a
158 new template thread in the list with an invalid ptid, switch
159 to it, delete the original thread, reset the new thread's
160 ptid, and switch to it. */
161
162 if (ptid_equal (inferior_ptid, ptid))
163 {
164 tp = xmalloc (sizeof (*tp));
165 memset (tp, 0, sizeof (*tp));
166 tp->ptid = minus_one_ptid;
167 tp->num = ++highest_thread_num;
168 tp->next = thread_list;
169 thread_list = tp;
170
171 /* Make switch_to_thread not read from the thread. */
172 tp->state_ = THREAD_EXITED;
173 switch_to_thread (minus_one_ptid);
174
175 /* Now we can delete it. */
176 delete_thread (ptid);
177
178 /* Now reset its ptid, and reswitch inferior_ptid to it. */
179 tp->ptid = ptid;
180 tp->state_ = THREAD_STOPPED;
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 /* Return true if TP is an active thread. */
443 static int
444 thread_alive (struct thread_info *tp)
445 {
446 if (tp->state_ == THREAD_EXITED)
447 return 0;
448 if (!target_thread_alive (tp->ptid))
449 return 0;
450 return 1;
451 }
452
453 static void
454 prune_threads (void)
455 {
456 struct thread_info *tp, *next;
457
458 for (tp = thread_list; tp; tp = next)
459 {
460 next = tp->next;
461 if (!thread_alive (tp))
462 delete_thread (tp->ptid);
463 }
464 }
465
466 void
467 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
468 {
469 struct inferior *inf;
470 struct thread_info *tp;
471
472 /* It can happen that what we knew as the target inferior id
473 changes. E.g, target remote may only discover the remote process
474 pid after adding the inferior to GDB's list. */
475 inf = find_inferior_pid (ptid_get_pid (old_ptid));
476 inf->pid = ptid_get_pid (new_ptid);
477
478 tp = find_thread_pid (old_ptid);
479 tp->ptid = new_ptid;
480
481 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
482 }
483
484 void
485 set_running (ptid_t ptid, int running)
486 {
487 struct thread_info *tp;
488
489 /* We try not to notify the observer if no thread has actually changed
490 the running state -- merely to reduce the number of messages to
491 frontend. Frontend is supposed to handle multiple *running just fine. */
492 if (PIDGET (ptid) == -1)
493 {
494 int any_started = 0;
495 for (tp = thread_list; tp; tp = tp->next)
496 {
497 if (tp->state_ == THREAD_EXITED)
498 continue;
499 if (running && tp->state_ == THREAD_STOPPED)
500 any_started = 1;
501 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
502 }
503 if (any_started && !suppress_resume_observer)
504 observer_notify_target_resumed (ptid);
505 }
506 else
507 {
508 int started = 0;
509 tp = find_thread_pid (ptid);
510 gdb_assert (tp);
511 gdb_assert (tp->state_ != THREAD_EXITED);
512 if (running && tp->state_ == THREAD_STOPPED)
513 started = 1;
514 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
515 if (started && !suppress_resume_observer)
516 observer_notify_target_resumed (ptid);
517 }
518 }
519
520 static int
521 is_thread_state (ptid_t ptid, enum thread_state state)
522 {
523 struct thread_info *tp;
524
525 if (!target_has_execution)
526 return 0;
527
528 tp = find_thread_pid (ptid);
529 gdb_assert (tp);
530 return tp->state_ == state;
531 }
532
533 int
534 is_stopped (ptid_t ptid)
535 {
536 /* Without execution, this property is always true. */
537 if (!target_has_execution)
538 return 1;
539
540 return is_thread_state (ptid, THREAD_STOPPED);
541 }
542
543 int
544 is_exited (ptid_t ptid)
545 {
546 /* Without execution, this property is always false. */
547 if (!target_has_execution)
548 return 0;
549
550 return is_thread_state (ptid, THREAD_EXITED);
551 }
552
553 int
554 is_running (ptid_t ptid)
555 {
556 /* Without execution, this property is always false. */
557 if (!target_has_execution)
558 return 0;
559
560 return is_thread_state (ptid, THREAD_RUNNING);
561 }
562
563 int
564 any_running (void)
565 {
566 struct thread_info *tp;
567
568 if (!target_has_execution)
569 return 0;
570
571 for (tp = thread_list; tp; tp = tp->next)
572 if (tp->state_ == THREAD_RUNNING)
573 return 1;
574
575 return 0;
576 }
577
578 int
579 is_executing (ptid_t ptid)
580 {
581 struct thread_info *tp;
582
583 if (!target_has_execution)
584 return 0;
585
586 tp = find_thread_pid (ptid);
587 gdb_assert (tp);
588 return tp->executing_;
589 }
590
591 void
592 set_executing (ptid_t ptid, int executing)
593 {
594 struct thread_info *tp;
595
596 if (PIDGET (ptid) == -1)
597 {
598 for (tp = thread_list; tp; tp = tp->next)
599 tp->executing_ = executing;
600 }
601 else
602 {
603 tp = find_thread_pid (ptid);
604 gdb_assert (tp);
605 tp->executing_ = executing;
606 }
607 }
608
609 void
610 set_stop_requested (ptid_t ptid, int stop)
611 {
612 struct thread_info *tp;
613 int all = ptid_equal (ptid, minus_one_ptid);
614
615 if (all || ptid_is_pid (ptid))
616 {
617 for (tp = thread_list; tp; tp = tp->next)
618 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
619 tp->stop_requested = stop;
620 }
621 else
622 {
623 tp = find_thread_pid (ptid);
624 gdb_assert (tp);
625 tp->stop_requested = stop;
626 }
627
628 /* Call the stop requested observer so other components of GDB can
629 react to this request. */
630 if (stop)
631 observer_notify_thread_stop_requested (ptid);
632 }
633
634 /* Prints the list of threads and their details on UIOUT.
635 This is a version of 'info_thread_command' suitable for
636 use from MI.
637 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
638 that should be printed. Otherwise, all threads are
639 printed.
640 If PID is not -1, only print threads from the process PID.
641 Otherwise, threads from all attached PIDs are printed.
642 If both REQUESTED_THREAD and PID are not -1, then the thread
643 is printed if it belongs to the specified process. Otherwise,
644 an error is raised. */
645 void
646 print_thread_info (struct ui_out *uiout, int requested_thread, int pid)
647 {
648 struct thread_info *tp;
649 ptid_t current_ptid;
650 struct cleanup *old_chain;
651 char *extra_info;
652 int current_thread = -1;
653
654 prune_threads ();
655 target_find_new_threads ();
656 current_ptid = inferior_ptid;
657
658 /* We'll be switching threads temporarily. */
659 old_chain = make_cleanup_restore_current_thread ();
660
661 make_cleanup_ui_out_list_begin_end (uiout, "threads");
662 for (tp = thread_list; tp; tp = tp->next)
663 {
664 struct cleanup *chain2;
665
666 if (requested_thread != -1 && tp->num != requested_thread)
667 continue;
668
669 if (pid != -1 && PIDGET (tp->ptid) != pid)
670 {
671 if (requested_thread != -1)
672 error (_("Requested thread not found in requested process"));
673 continue;
674 }
675
676 if (ptid_equal (tp->ptid, current_ptid))
677 current_thread = tp->num;
678
679 if (tp->state_ == THREAD_EXITED)
680 continue;
681
682 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
683
684 if (ptid_equal (tp->ptid, current_ptid))
685 ui_out_text (uiout, "* ");
686 else
687 ui_out_text (uiout, " ");
688
689 ui_out_field_int (uiout, "id", tp->num);
690 ui_out_text (uiout, " ");
691 ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid));
692
693 extra_info = target_extra_thread_info (tp);
694 if (extra_info)
695 {
696 ui_out_text (uiout, " (");
697 ui_out_field_string (uiout, "details", extra_info);
698 ui_out_text (uiout, ")");
699 }
700 ui_out_text (uiout, " ");
701
702 if (tp->state_ == THREAD_RUNNING)
703 ui_out_text (uiout, "(running)\n");
704 else
705 {
706 /* The switch below puts us at the top of the stack (leaf
707 frame). */
708 switch_to_thread (tp->ptid);
709 print_stack_frame (get_selected_frame (NULL),
710 /* For MI output, print frame level. */
711 ui_out_is_mi_like_p (uiout),
712 LOCATION);
713 }
714
715 if (ui_out_is_mi_like_p (uiout))
716 {
717 char *state = "stopped";
718 if (tp->state_ == THREAD_RUNNING)
719 state = "running";
720 ui_out_field_string (uiout, "state", state);
721 }
722
723 do_cleanups (chain2);
724 }
725
726 /* Restores the current thread and the frame selected before
727 the "info threads" command. */
728 do_cleanups (old_chain);
729
730 if (pid == -1 && requested_thread == -1 )
731 {
732 gdb_assert (current_thread != -1
733 || !thread_list);
734 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
735 ui_out_field_int (uiout, "current-thread-id", current_thread);
736
737 if (current_thread != -1 && is_exited (current_ptid))
738 ui_out_message (uiout, 0, "\n\
739 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
740 current_thread);
741 }
742 }
743
744
745 /* Print information about currently known threads
746
747 * Note: this has the drawback that it _really_ switches
748 * threads, which frees the frame cache. A no-side
749 * effects info-threads command would be nicer.
750 */
751
752 static void
753 info_threads_command (char *arg, int from_tty)
754 {
755 print_thread_info (uiout, -1, -1);
756 }
757
758 /* Switch from one thread to another. */
759
760 void
761 switch_to_thread (ptid_t ptid)
762 {
763 if (ptid_equal (ptid, inferior_ptid))
764 return;
765
766 inferior_ptid = ptid;
767 reinit_frame_cache ();
768 registers_changed ();
769
770 /* We don't check for is_stopped, because we're called at times
771 while in the TARGET_RUNNING state, e.g., while handling an
772 internal event. */
773 if (!is_exited (ptid) && !is_executing (ptid))
774 stop_pc = read_pc ();
775 else
776 stop_pc = ~(CORE_ADDR) 0;
777 }
778
779 static void
780 restore_current_thread (ptid_t ptid)
781 {
782 switch_to_thread (ptid);
783 }
784
785 static void
786 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
787 {
788 struct frame_info *frame = NULL;
789 int count;
790
791 gdb_assert (frame_level >= 0);
792
793 /* Restore by level first, check if the frame id is the same as
794 expected. If that fails, try restoring by frame id. If that
795 fails, nothing to do, just warn the user. */
796
797 count = frame_level;
798 frame = find_relative_frame (get_current_frame (), &count);
799 if (count == 0
800 && frame != NULL
801 /* Either the frame ids match, of they're both invalid. The
802 latter case is not failsafe, but since it's highly unlikely
803 the search by level finds the wrong frame, it's 99.9(9)% of
804 the time (for all practical purposes) safe. */
805 && (frame_id_eq (get_frame_id (frame), a_frame_id)
806 /* Note: could be better to check every frame_id
807 member for equality here. */
808 || (!frame_id_p (get_frame_id (frame))
809 && !frame_id_p (a_frame_id))))
810 {
811 /* Cool, all is fine. */
812 select_frame (frame);
813 return;
814 }
815
816 frame = frame_find_by_id (a_frame_id);
817 if (frame != NULL)
818 {
819 /* Cool, refound it. */
820 select_frame (frame);
821 return;
822 }
823
824 /* Nothing else to do, the frame layout really changed. Select the
825 innermost stack frame. */
826 select_frame (get_current_frame ());
827
828 /* Warn the user. */
829 if (!ui_out_is_mi_like_p (uiout))
830 {
831 warning (_("\
832 Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
833 frame_level);
834 /* For MI, we should probably have a notification about
835 current frame change. But this error is not very
836 likely, so don't bother for now. */
837 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
838 }
839 }
840
841 struct current_thread_cleanup
842 {
843 ptid_t inferior_ptid;
844 struct frame_id selected_frame_id;
845 int selected_frame_level;
846 int was_stopped;
847 };
848
849 static void
850 do_restore_current_thread_cleanup (void *arg)
851 {
852 struct thread_info *tp;
853 struct current_thread_cleanup *old = arg;
854 restore_current_thread (old->inferior_ptid);
855
856 /* The running state of the originally selected thread may have
857 changed, so we have to recheck it here. */
858 if (old->was_stopped
859 && is_stopped (inferior_ptid)
860 && target_has_registers
861 && target_has_stack
862 && target_has_memory)
863 restore_selected_frame (old->selected_frame_id,
864 old->selected_frame_level);
865 }
866
867 static void
868 restore_current_thread_cleanup_dtor (void *arg)
869 {
870 struct current_thread_cleanup *old = arg;
871 struct thread_info *tp;
872 tp = find_thread_pid (old->inferior_ptid);
873 if (tp)
874 tp->refcount--;
875 xfree (old);
876 }
877
878 struct cleanup *
879 make_cleanup_restore_current_thread (void)
880 {
881 struct thread_info *tp;
882 struct frame_info *frame;
883 struct current_thread_cleanup *old;
884
885 old = xmalloc (sizeof (struct current_thread_cleanup));
886 old->inferior_ptid = inferior_ptid;
887 old->was_stopped = is_stopped (inferior_ptid);
888 if (old->was_stopped
889 && target_has_registers
890 && target_has_stack
891 && target_has_memory)
892 frame = get_selected_frame (NULL);
893 else
894 frame = NULL;
895
896 old->selected_frame_id = get_frame_id (frame);
897 old->selected_frame_level = frame_relative_level (frame);
898
899 tp = find_thread_pid (inferior_ptid);
900 if (tp)
901 tp->refcount++;
902
903 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
904 restore_current_thread_cleanup_dtor);
905 }
906
907 /* Apply a GDB command to a list of threads. List syntax is a whitespace
908 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
909 of two numbers seperated by a hyphen. Examples:
910
911 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
912 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
913 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
914 */
915
916 static void
917 thread_apply_all_command (char *cmd, int from_tty)
918 {
919 struct thread_info *tp;
920 struct cleanup *old_chain;
921 char *saved_cmd;
922
923 if (cmd == NULL || *cmd == '\000')
924 error (_("Please specify a command following the thread ID list"));
925
926 prune_threads ();
927 target_find_new_threads ();
928
929 old_chain = make_cleanup_restore_current_thread ();
930
931 /* Save a copy of the command in case it is clobbered by
932 execute_command */
933 saved_cmd = xstrdup (cmd);
934 make_cleanup (xfree, saved_cmd);
935 for (tp = thread_list; tp; tp = tp->next)
936 if (thread_alive (tp))
937 {
938 switch_to_thread (tp->ptid);
939
940 printf_filtered (_("\nThread %d (%s):\n"),
941 tp->num, target_tid_to_str (inferior_ptid));
942 execute_command (cmd, from_tty);
943 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
944 }
945
946 do_cleanups (old_chain);
947 }
948
949 static void
950 thread_apply_command (char *tidlist, int from_tty)
951 {
952 char *cmd;
953 char *p;
954 struct cleanup *old_chain;
955 char *saved_cmd;
956
957 if (tidlist == NULL || *tidlist == '\000')
958 error (_("Please specify a thread ID list"));
959
960 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
961
962 if (*cmd == '\000')
963 error (_("Please specify a command following the thread ID list"));
964
965 /* Save a copy of the command in case it is clobbered by
966 execute_command */
967 saved_cmd = xstrdup (cmd);
968 old_chain = make_cleanup (xfree, saved_cmd);
969 while (tidlist < cmd)
970 {
971 struct thread_info *tp;
972 int start, end;
973
974 start = strtol (tidlist, &p, 10);
975 if (p == tidlist)
976 error (_("Error parsing %s"), tidlist);
977 tidlist = p;
978
979 while (*tidlist == ' ' || *tidlist == '\t')
980 tidlist++;
981
982 if (*tidlist == '-') /* Got a range of IDs? */
983 {
984 tidlist++; /* Skip the - */
985 end = strtol (tidlist, &p, 10);
986 if (p == tidlist)
987 error (_("Error parsing %s"), tidlist);
988 tidlist = p;
989
990 while (*tidlist == ' ' || *tidlist == '\t')
991 tidlist++;
992 }
993 else
994 end = start;
995
996 make_cleanup_restore_current_thread ();
997
998 for (; start <= end; start++)
999 {
1000 tp = find_thread_id (start);
1001
1002 if (!tp)
1003 warning (_("Unknown thread %d."), start);
1004 else if (!thread_alive (tp))
1005 warning (_("Thread %d has terminated."), start);
1006 else
1007 {
1008 switch_to_thread (tp->ptid);
1009
1010 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1011 target_tid_to_str (inferior_ptid));
1012 execute_command (cmd, from_tty);
1013
1014 /* Restore exact command used previously. */
1015 strcpy (cmd, saved_cmd);
1016 }
1017 }
1018 }
1019
1020 do_cleanups (old_chain);
1021 }
1022
1023 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1024 if prefix of arg is `apply'. */
1025
1026 static void
1027 thread_command (char *tidstr, int from_tty)
1028 {
1029 if (!tidstr)
1030 {
1031 if (target_has_stack)
1032 {
1033 if (is_exited (inferior_ptid))
1034 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1035 pid_to_thread_id (inferior_ptid),
1036 target_tid_to_str (inferior_ptid));
1037 else
1038 printf_filtered (_("[Current thread is %d (%s)]\n"),
1039 pid_to_thread_id (inferior_ptid),
1040 target_tid_to_str (inferior_ptid));
1041 }
1042 else
1043 error (_("No stack."));
1044 return;
1045 }
1046
1047 annotate_thread_changed ();
1048 gdb_thread_select (uiout, tidstr, NULL);
1049 }
1050
1051 /* Print notices when new threads are attached and detached. */
1052 int print_thread_events = 1;
1053 static void
1054 show_print_thread_events (struct ui_file *file, int from_tty,
1055 struct cmd_list_element *c, const char *value)
1056 {
1057 fprintf_filtered (file, _("\
1058 Printing of thread events is %s.\n"),
1059 value);
1060 }
1061
1062 static int
1063 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1064 {
1065 int num;
1066 struct thread_info *tp;
1067
1068 num = value_as_long (parse_and_eval (tidstr));
1069
1070 tp = find_thread_id (num);
1071
1072 if (!tp)
1073 error (_("Thread ID %d not known."), num);
1074
1075 if (!thread_alive (tp))
1076 error (_("Thread ID %d has terminated."), num);
1077
1078 switch_to_thread (tp->ptid);
1079
1080 ui_out_text (uiout, "[Switching to thread ");
1081 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1082 ui_out_text (uiout, " (");
1083 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
1084 ui_out_text (uiout, ")]");
1085
1086 /* Note that we can't reach this with an exited thread, due to the
1087 thread_alive check above. */
1088 if (tp->state_ == THREAD_RUNNING)
1089 ui_out_text (uiout, "(running)\n");
1090 else
1091 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1092
1093 /* Since the current thread may have changed, see if there is any
1094 exited thread we can now delete. */
1095 prune_threads ();
1096
1097 return GDB_RC_OK;
1098 }
1099
1100 enum gdb_rc
1101 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1102 {
1103 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1104 error_message, RETURN_MASK_ALL) < 0)
1105 return GDB_RC_FAIL;
1106 return GDB_RC_OK;
1107 }
1108
1109 /* Commands with a prefix of `thread'. */
1110 struct cmd_list_element *thread_cmd_list = NULL;
1111
1112 void
1113 _initialize_thread (void)
1114 {
1115 static struct cmd_list_element *thread_apply_list = NULL;
1116 struct cmd_list_element *c;
1117
1118 c = add_info ("threads", info_threads_command,
1119 _("IDs of currently known threads."));
1120 set_cmd_no_selected_thread_ok (c);
1121
1122 c = add_prefix_cmd ("thread", class_run, thread_command, _("\
1123 Use this command to switch between threads.\n\
1124 The new thread ID must be currently known."),
1125 &thread_cmd_list, "thread ", 1, &cmdlist);
1126 set_cmd_no_selected_thread_ok (c);
1127
1128 c = add_prefix_cmd ("apply", class_run, thread_apply_command,
1129 _("Apply a command to a list of threads."),
1130 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1131 set_cmd_no_selected_thread_ok (c);
1132
1133 c = add_cmd ("all", class_run, thread_apply_all_command,
1134 _("Apply a command to all threads."), &thread_apply_list);
1135 set_cmd_no_selected_thread_ok (c);
1136
1137 if (!xdb_commands)
1138 add_com_alias ("t", "thread", class_run, 1);
1139
1140 add_setshow_boolean_cmd ("thread-events", no_class,
1141 &print_thread_events, _("\
1142 Set printing of thread events (such as thread start and exit)."), _("\
1143 Show printing of thread events (such as thread start and exit)."), NULL,
1144 NULL,
1145 show_print_thread_events,
1146 &setprintlist, &showprintlist);
1147 }
This page took 0.056097 seconds and 5 git commands to generate.