]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/thread.c
* tracepoint.c (trace_actions_command): Update.
[thirdparty/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, 2010, 2011
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 #include "gdb_regex.h"
47 #include "cli/cli-utils.h"
48
49 /* Definition of struct thread_info exported to gdbthread.h. */
50
51 /* Prototypes for exported functions. */
52
53 void _initialize_thread (void);
54
55 /* Prototypes for local functions. */
56
57 static struct thread_info *thread_list = NULL;
58 static int highest_thread_num;
59
60 static void thread_command (char *tidstr, int from_tty);
61 static void thread_apply_all_command (char *, int);
62 static int thread_alive (struct thread_info *);
63 static void info_threads_command (char *, int);
64 static void thread_apply_command (char *, int);
65 static void restore_current_thread (ptid_t);
66 static void prune_threads (void);
67
68 /* Frontend view of the thread state. Possible extensions: stepping,
69 finishing, until(ling),... */
70 enum thread_state
71 {
72 THREAD_STOPPED,
73 THREAD_RUNNING,
74 THREAD_EXITED,
75 };
76
77 struct thread_info*
78 inferior_thread (void)
79 {
80 struct thread_info *tp = find_thread_ptid (inferior_ptid);
81 gdb_assert (tp);
82 return tp;
83 }
84
85 void
86 delete_step_resume_breakpoint (struct thread_info *tp)
87 {
88 if (tp && tp->control.step_resume_breakpoint)
89 {
90 delete_breakpoint (tp->control.step_resume_breakpoint);
91 tp->control.step_resume_breakpoint = NULL;
92 }
93 }
94
95 void
96 delete_exception_resume_breakpoint (struct thread_info *tp)
97 {
98 if (tp && tp->control.exception_resume_breakpoint)
99 {
100 delete_breakpoint (tp->control.exception_resume_breakpoint);
101 tp->control.exception_resume_breakpoint = NULL;
102 }
103 }
104
105 static void
106 clear_thread_inferior_resources (struct thread_info *tp)
107 {
108 /* NOTE: this will take care of any left-over step_resume breakpoints,
109 but not any user-specified thread-specific breakpoints. We can not
110 delete the breakpoint straight-off, because the inferior might not
111 be stopped at the moment. */
112 if (tp->control.step_resume_breakpoint)
113 {
114 tp->control.step_resume_breakpoint->disposition = disp_del_at_next_stop;
115 tp->control.step_resume_breakpoint = NULL;
116 }
117
118 if (tp->control.exception_resume_breakpoint)
119 {
120 tp->control.exception_resume_breakpoint->disposition
121 = disp_del_at_next_stop;
122 tp->control.exception_resume_breakpoint = NULL;
123 }
124
125 bpstat_clear (&tp->control.stop_bpstat);
126
127 discard_all_intermediate_continuations_thread (tp);
128 discard_all_continuations_thread (tp);
129
130 delete_longjmp_breakpoint (tp->num);
131 }
132
133 static void
134 free_thread (struct thread_info *tp)
135 {
136 clear_thread_inferior_resources (tp);
137
138 if (tp->private)
139 {
140 if (tp->private_dtor)
141 tp->private_dtor (tp->private);
142 else
143 xfree (tp->private);
144 }
145
146 xfree (tp->name);
147 xfree (tp);
148 }
149
150 void
151 init_thread_list (void)
152 {
153 struct thread_info *tp, *tpnext;
154
155 highest_thread_num = 0;
156
157 if (!thread_list)
158 return;
159
160 for (tp = thread_list; tp; tp = tpnext)
161 {
162 tpnext = tp->next;
163 free_thread (tp);
164 }
165
166 thread_list = NULL;
167 }
168
169 /* Allocate a new thread with target id PTID and add it to the thread
170 list. */
171
172 static struct thread_info *
173 new_thread (ptid_t ptid)
174 {
175 struct thread_info *tp;
176
177 tp = xcalloc (1, sizeof (*tp));
178
179 tp->ptid = ptid;
180 tp->num = ++highest_thread_num;
181 tp->next = thread_list;
182 thread_list = tp;
183
184 /* Nothing to follow yet. */
185 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
186 tp->state_ = THREAD_STOPPED;
187
188 return tp;
189 }
190
191 struct thread_info *
192 add_thread_silent (ptid_t ptid)
193 {
194 struct thread_info *tp;
195
196 tp = find_thread_ptid (ptid);
197 if (tp)
198 /* Found an old thread with the same id. It has to be dead,
199 otherwise we wouldn't be adding a new thread with the same id.
200 The OS is reusing this id --- delete it, and recreate a new
201 one. */
202 {
203 /* In addition to deleting the thread, if this is the current
204 thread, then we need to take care that delete_thread doesn't
205 really delete the thread if it is inferior_ptid. Create a
206 new template thread in the list with an invalid ptid, switch
207 to it, delete the original thread, reset the new thread's
208 ptid, and switch to it. */
209
210 if (ptid_equal (inferior_ptid, ptid))
211 {
212 tp = new_thread (null_ptid);
213
214 /* Make switch_to_thread not read from the thread. */
215 tp->state_ = THREAD_EXITED;
216 switch_to_thread (null_ptid);
217
218 /* Now we can delete it. */
219 delete_thread (ptid);
220
221 /* Now reset its ptid, and reswitch inferior_ptid to it. */
222 tp->ptid = ptid;
223 tp->state_ = THREAD_STOPPED;
224 switch_to_thread (ptid);
225
226 observer_notify_new_thread (tp);
227
228 /* All done. */
229 return tp;
230 }
231 else
232 /* Just go ahead and delete it. */
233 delete_thread (ptid);
234 }
235
236 tp = new_thread (ptid);
237 observer_notify_new_thread (tp);
238
239 return tp;
240 }
241
242 struct thread_info *
243 add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
244 {
245 struct thread_info *result = add_thread_silent (ptid);
246
247 result->private = private;
248
249 if (print_thread_events)
250 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
251
252 annotate_new_thread ();
253 return result;
254 }
255
256 struct thread_info *
257 add_thread (ptid_t ptid)
258 {
259 return add_thread_with_info (ptid, NULL);
260 }
261
262 /* Delete thread PTID. If SILENT, don't notify the observer of this
263 exit. */
264 static void
265 delete_thread_1 (ptid_t ptid, int silent)
266 {
267 struct thread_info *tp, *tpprev;
268
269 tpprev = NULL;
270
271 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
272 if (ptid_equal (tp->ptid, ptid))
273 break;
274
275 if (!tp)
276 return;
277
278 /* If this is the current thread, or there's code out there that
279 relies on it existing (refcount > 0) we can't delete yet. Mark
280 it as exited, and notify it. */
281 if (tp->refcount > 0
282 || ptid_equal (tp->ptid, inferior_ptid))
283 {
284 if (tp->state_ != THREAD_EXITED)
285 {
286 observer_notify_thread_exit (tp, silent);
287
288 /* Tag it as exited. */
289 tp->state_ = THREAD_EXITED;
290
291 /* Clear breakpoints, etc. associated with this thread. */
292 clear_thread_inferior_resources (tp);
293 }
294
295 /* Will be really deleted some other time. */
296 return;
297 }
298
299 if (tpprev)
300 tpprev->next = tp->next;
301 else
302 thread_list = tp->next;
303
304 /* Notify thread exit, but only if we haven't already. */
305 if (tp->state_ != THREAD_EXITED)
306 observer_notify_thread_exit (tp, silent);
307
308 free_thread (tp);
309 }
310
311 /* Delete thread PTID and notify of thread exit. If this is
312 inferior_ptid, don't actually delete it, but tag it as exited and
313 do the notification. If PTID is the user selected thread, clear
314 it. */
315 void
316 delete_thread (ptid_t ptid)
317 {
318 delete_thread_1 (ptid, 0 /* not silent */);
319 }
320
321 void
322 delete_thread_silent (ptid_t ptid)
323 {
324 delete_thread_1 (ptid, 1 /* silent */);
325 }
326
327 struct thread_info *
328 find_thread_id (int num)
329 {
330 struct thread_info *tp;
331
332 for (tp = thread_list; tp; tp = tp->next)
333 if (tp->num == num)
334 return tp;
335
336 return NULL;
337 }
338
339 /* Find a thread_info by matching PTID. */
340 struct thread_info *
341 find_thread_ptid (ptid_t ptid)
342 {
343 struct thread_info *tp;
344
345 for (tp = thread_list; tp; tp = tp->next)
346 if (ptid_equal (tp->ptid, ptid))
347 return tp;
348
349 return NULL;
350 }
351
352 /*
353 * Thread iterator function.
354 *
355 * Calls a callback function once for each thread, so long as
356 * the callback function returns false. If the callback function
357 * returns true, the iteration will end and the current thread
358 * will be returned. This can be useful for implementing a
359 * search for a thread with arbitrary attributes, or for applying
360 * some operation to every thread.
361 *
362 * FIXME: some of the existing functionality, such as
363 * "Thread apply all", might be rewritten using this functionality.
364 */
365
366 struct thread_info *
367 iterate_over_threads (int (*callback) (struct thread_info *, void *),
368 void *data)
369 {
370 struct thread_info *tp, *next;
371
372 for (tp = thread_list; tp; tp = next)
373 {
374 next = tp->next;
375 if ((*callback) (tp, data))
376 return tp;
377 }
378
379 return NULL;
380 }
381
382 int
383 thread_count (void)
384 {
385 int result = 0;
386 struct thread_info *tp;
387
388 for (tp = thread_list; tp; tp = tp->next)
389 ++result;
390
391 return result;
392 }
393
394 int
395 valid_thread_id (int num)
396 {
397 struct thread_info *tp;
398
399 for (tp = thread_list; tp; tp = tp->next)
400 if (tp->num == num)
401 return 1;
402
403 return 0;
404 }
405
406 int
407 pid_to_thread_id (ptid_t ptid)
408 {
409 struct thread_info *tp;
410
411 for (tp = thread_list; tp; tp = tp->next)
412 if (ptid_equal (tp->ptid, ptid))
413 return tp->num;
414
415 return 0;
416 }
417
418 ptid_t
419 thread_id_to_pid (int num)
420 {
421 struct thread_info *thread = find_thread_id (num);
422
423 if (thread)
424 return thread->ptid;
425 else
426 return pid_to_ptid (-1);
427 }
428
429 int
430 in_thread_list (ptid_t ptid)
431 {
432 struct thread_info *tp;
433
434 for (tp = thread_list; tp; tp = tp->next)
435 if (ptid_equal (tp->ptid, ptid))
436 return 1;
437
438 return 0; /* Never heard of 'im. */
439 }
440
441 /* Finds the first thread of the inferior given by PID. If PID is -1,
442 return the first thread in the list. */
443
444 struct thread_info *
445 first_thread_of_process (int pid)
446 {
447 struct thread_info *tp, *ret = NULL;
448
449 for (tp = thread_list; tp; tp = tp->next)
450 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
451 if (ret == NULL || tp->num < ret->num)
452 ret = tp;
453
454 return ret;
455 }
456
457 struct thread_info *
458 any_thread_of_process (int pid)
459 {
460 struct thread_info *tp;
461
462 for (tp = thread_list; tp; tp = tp->next)
463 if (ptid_get_pid (tp->ptid) == pid)
464 return tp;
465
466 return NULL;
467 }
468
469 struct thread_info *
470 any_live_thread_of_process (int pid)
471 {
472 struct thread_info *tp;
473 struct thread_info *tp_running = NULL;
474
475 for (tp = thread_list; tp; tp = tp->next)
476 if (ptid_get_pid (tp->ptid) == pid)
477 {
478 if (tp->state_ == THREAD_STOPPED)
479 return tp;
480 else if (tp->state_ == THREAD_RUNNING)
481 tp_running = tp;
482 }
483
484 return tp_running;
485 }
486
487 /* Print a list of thread ids currently known, and the total number of
488 threads. To be used from within catch_errors. */
489 static int
490 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
491 {
492 struct thread_info *tp;
493 int num = 0;
494 struct cleanup *cleanup_chain;
495 int current_thread = -1;
496
497 update_thread_list ();
498
499 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
500
501 for (tp = thread_list; tp; tp = tp->next)
502 {
503 if (tp->state_ == THREAD_EXITED)
504 continue;
505
506 if (ptid_equal (tp->ptid, inferior_ptid))
507 current_thread = tp->num;
508
509 num++;
510 ui_out_field_int (uiout, "thread-id", tp->num);
511 }
512
513 do_cleanups (cleanup_chain);
514
515 if (current_thread != -1)
516 ui_out_field_int (uiout, "current-thread-id", current_thread);
517 ui_out_field_int (uiout, "number-of-threads", num);
518 return GDB_RC_OK;
519 }
520
521 /* Official gdblib interface function to get a list of thread ids and
522 the total number. */
523 enum gdb_rc
524 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
525 {
526 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
527 error_message, RETURN_MASK_ALL) < 0)
528 return GDB_RC_FAIL;
529 return GDB_RC_OK;
530 }
531
532 /* Return true if TP is an active thread. */
533 static int
534 thread_alive (struct thread_info *tp)
535 {
536 if (tp->state_ == THREAD_EXITED)
537 return 0;
538 if (!target_thread_alive (tp->ptid))
539 return 0;
540 return 1;
541 }
542
543 static void
544 prune_threads (void)
545 {
546 struct thread_info *tp, *next;
547
548 for (tp = thread_list; tp; tp = next)
549 {
550 next = tp->next;
551 if (!thread_alive (tp))
552 delete_thread (tp->ptid);
553 }
554 }
555
556 void
557 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
558 {
559 struct inferior *inf;
560 struct thread_info *tp;
561
562 /* It can happen that what we knew as the target inferior id
563 changes. E.g, target remote may only discover the remote process
564 pid after adding the inferior to GDB's list. */
565 inf = find_inferior_pid (ptid_get_pid (old_ptid));
566 inf->pid = ptid_get_pid (new_ptid);
567
568 tp = find_thread_ptid (old_ptid);
569 tp->ptid = new_ptid;
570
571 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
572 }
573
574 void
575 set_running (ptid_t ptid, int running)
576 {
577 struct thread_info *tp;
578 int all = ptid_equal (ptid, minus_one_ptid);
579
580 /* We try not to notify the observer if no thread has actually changed
581 the running state -- merely to reduce the number of messages to
582 frontend. Frontend is supposed to handle multiple *running just fine. */
583 if (all || ptid_is_pid (ptid))
584 {
585 int any_started = 0;
586
587 for (tp = thread_list; tp; tp = tp->next)
588 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
589 {
590 if (tp->state_ == THREAD_EXITED)
591 continue;
592 if (running && tp->state_ == THREAD_STOPPED)
593 any_started = 1;
594 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
595 }
596 if (any_started)
597 observer_notify_target_resumed (ptid);
598 }
599 else
600 {
601 int started = 0;
602
603 tp = find_thread_ptid (ptid);
604 gdb_assert (tp);
605 gdb_assert (tp->state_ != THREAD_EXITED);
606 if (running && tp->state_ == THREAD_STOPPED)
607 started = 1;
608 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
609 if (started)
610 observer_notify_target_resumed (ptid);
611 }
612 }
613
614 static int
615 is_thread_state (ptid_t ptid, enum thread_state state)
616 {
617 struct thread_info *tp;
618
619 tp = find_thread_ptid (ptid);
620 gdb_assert (tp);
621 return tp->state_ == state;
622 }
623
624 int
625 is_stopped (ptid_t ptid)
626 {
627 return is_thread_state (ptid, THREAD_STOPPED);
628 }
629
630 int
631 is_exited (ptid_t ptid)
632 {
633 return is_thread_state (ptid, THREAD_EXITED);
634 }
635
636 int
637 is_running (ptid_t ptid)
638 {
639 return is_thread_state (ptid, THREAD_RUNNING);
640 }
641
642 int
643 any_running (void)
644 {
645 struct thread_info *tp;
646
647 for (tp = thread_list; tp; tp = tp->next)
648 if (tp->state_ == THREAD_RUNNING)
649 return 1;
650
651 return 0;
652 }
653
654 int
655 is_executing (ptid_t ptid)
656 {
657 struct thread_info *tp;
658
659 tp = find_thread_ptid (ptid);
660 gdb_assert (tp);
661 return tp->executing_;
662 }
663
664 void
665 set_executing (ptid_t ptid, int executing)
666 {
667 struct thread_info *tp;
668 int all = ptid_equal (ptid, minus_one_ptid);
669
670 if (all || ptid_is_pid (ptid))
671 {
672 for (tp = thread_list; tp; tp = tp->next)
673 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
674 tp->executing_ = executing;
675 }
676 else
677 {
678 tp = find_thread_ptid (ptid);
679 gdb_assert (tp);
680 tp->executing_ = executing;
681 }
682 }
683
684 void
685 set_stop_requested (ptid_t ptid, int stop)
686 {
687 struct thread_info *tp;
688 int all = ptid_equal (ptid, minus_one_ptid);
689
690 if (all || ptid_is_pid (ptid))
691 {
692 for (tp = thread_list; tp; tp = tp->next)
693 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
694 tp->stop_requested = stop;
695 }
696 else
697 {
698 tp = find_thread_ptid (ptid);
699 gdb_assert (tp);
700 tp->stop_requested = stop;
701 }
702
703 /* Call the stop requested observer so other components of GDB can
704 react to this request. */
705 if (stop)
706 observer_notify_thread_stop_requested (ptid);
707 }
708
709 void
710 finish_thread_state (ptid_t ptid)
711 {
712 struct thread_info *tp;
713 int all;
714 int any_started = 0;
715
716 all = ptid_equal (ptid, minus_one_ptid);
717
718 if (all || ptid_is_pid (ptid))
719 {
720 for (tp = thread_list; tp; tp = tp->next)
721 {
722 if (tp->state_ == THREAD_EXITED)
723 continue;
724 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
725 {
726 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
727 any_started = 1;
728 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
729 }
730 }
731 }
732 else
733 {
734 tp = find_thread_ptid (ptid);
735 gdb_assert (tp);
736 if (tp->state_ != THREAD_EXITED)
737 {
738 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
739 any_started = 1;
740 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
741 }
742 }
743
744 if (any_started)
745 observer_notify_target_resumed (ptid);
746 }
747
748 void
749 finish_thread_state_cleanup (void *arg)
750 {
751 ptid_t *ptid_p = arg;
752
753 gdb_assert (arg);
754
755 finish_thread_state (*ptid_p);
756 }
757
758 /* Prints the list of threads and their details on UIOUT.
759 This is a version of 'info_threads_command' suitable for
760 use from MI.
761 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
762 that should be printed. Otherwise, all threads are
763 printed.
764 If PID is not -1, only print threads from the process PID.
765 Otherwise, threads from all attached PIDs are printed.
766 If both REQUESTED_THREAD and PID are not -1, then the thread
767 is printed if it belongs to the specified process. Otherwise,
768 an error is raised. */
769 void
770 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
771 {
772 struct thread_info *tp;
773 ptid_t current_ptid;
774 struct cleanup *old_chain;
775 char *extra_info, *name, *target_id;
776 int current_thread = -1;
777
778 update_thread_list ();
779 current_ptid = inferior_ptid;
780
781 /* We'll be switching threads temporarily. */
782 old_chain = make_cleanup_restore_current_thread ();
783
784 /* For backward compatibility, we make a list for MI. A table is
785 preferable for the CLI, though, because it shows table
786 headers. */
787 if (ui_out_is_mi_like_p (uiout))
788 make_cleanup_ui_out_list_begin_end (uiout, "threads");
789 else
790 {
791 int n_threads = 0;
792
793 for (tp = thread_list; tp; tp = tp->next)
794 {
795 if (!number_is_in_list (requested_threads, tp->num))
796 continue;
797
798 if (pid != -1 && PIDGET (tp->ptid) != pid)
799 continue;
800
801 if (tp->state_ == THREAD_EXITED)
802 continue;
803
804 ++n_threads;
805 }
806
807 if (n_threads == 0)
808 {
809 if (requested_threads == NULL || *requested_threads == '\0')
810 ui_out_message (uiout, 0, _("No threads.\n"));
811 else
812 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
813 requested_threads);
814 do_cleanups (old_chain);
815 return;
816 }
817
818 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
819
820 ui_out_table_header (uiout, 1, ui_left, "current", "");
821 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
822 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
823 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
824 ui_out_table_body (uiout);
825 }
826
827 for (tp = thread_list; tp; tp = tp->next)
828 {
829 struct cleanup *chain2;
830 int core;
831
832 if (!number_is_in_list (requested_threads, tp->num))
833 continue;
834
835 if (pid != -1 && PIDGET (tp->ptid) != pid)
836 {
837 if (requested_threads != NULL && *requested_threads != '\0')
838 error (_("Requested thread not found in requested process"));
839 continue;
840 }
841
842 if (ptid_equal (tp->ptid, current_ptid))
843 current_thread = tp->num;
844
845 if (tp->state_ == THREAD_EXITED)
846 continue;
847
848 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
849
850 if (ui_out_is_mi_like_p (uiout))
851 {
852 /* Compatibility. */
853 if (ptid_equal (tp->ptid, current_ptid))
854 ui_out_text (uiout, "* ");
855 else
856 ui_out_text (uiout, " ");
857 }
858 else
859 {
860 if (ptid_equal (tp->ptid, current_ptid))
861 ui_out_field_string (uiout, "current", "*");
862 else
863 ui_out_field_skip (uiout, "current");
864 }
865
866 ui_out_field_int (uiout, "id", tp->num);
867
868 /* For the CLI, we stuff everything into the target-id field.
869 This is a gross hack to make the output come out looking
870 correct. The underlying problem here is that ui-out has no
871 way to specify that a field's space allocation should be
872 shared by several fields. For MI, we do the right thing
873 instead. */
874
875 target_id = target_pid_to_str (tp->ptid);
876 extra_info = target_extra_thread_info (tp);
877 name = tp->name ? tp->name : target_thread_name (tp);
878
879 if (ui_out_is_mi_like_p (uiout))
880 {
881 ui_out_field_string (uiout, "target-id", target_id);
882 if (extra_info)
883 ui_out_field_string (uiout, "details", extra_info);
884 if (name)
885 ui_out_field_string (uiout, "name", name);
886 }
887 else
888 {
889 struct cleanup *str_cleanup;
890 char *contents;
891
892 if (extra_info && name)
893 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
894 name, extra_info);
895 else if (extra_info)
896 contents = xstrprintf ("%s (%s)", target_id, extra_info);
897 else if (name)
898 contents = xstrprintf ("%s \"%s\"", target_id, name);
899 else
900 contents = xstrdup (target_id);
901 str_cleanup = make_cleanup (xfree, contents);
902
903 ui_out_field_string (uiout, "target-id", contents);
904 do_cleanups (str_cleanup);
905 }
906
907 if (tp->state_ == THREAD_RUNNING)
908 ui_out_text (uiout, "(running)\n");
909 else
910 {
911 /* The switch below puts us at the top of the stack (leaf
912 frame). */
913 switch_to_thread (tp->ptid);
914 print_stack_frame (get_selected_frame (NULL),
915 /* For MI output, print frame level. */
916 ui_out_is_mi_like_p (uiout),
917 LOCATION);
918 }
919
920 if (ui_out_is_mi_like_p (uiout))
921 {
922 char *state = "stopped";
923
924 if (tp->state_ == THREAD_RUNNING)
925 state = "running";
926 ui_out_field_string (uiout, "state", state);
927 }
928
929 core = target_core_of_thread (tp->ptid);
930 if (ui_out_is_mi_like_p (uiout) && core != -1)
931 ui_out_field_int (uiout, "core", core);
932
933 do_cleanups (chain2);
934 }
935
936 /* Restores the current thread and the frame selected before
937 the "info threads" command. */
938 do_cleanups (old_chain);
939
940 if (pid == -1 && requested_threads == NULL)
941 {
942 gdb_assert (current_thread != -1
943 || !thread_list
944 || ptid_equal (inferior_ptid, null_ptid));
945 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
946 ui_out_field_int (uiout, "current-thread-id", current_thread);
947
948 if (current_thread != -1 && is_exited (current_ptid))
949 ui_out_message (uiout, 0, "\n\
950 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
951 current_thread);
952 else if (thread_list
953 && current_thread == -1
954 && ptid_equal (current_ptid, null_ptid))
955 ui_out_message (uiout, 0, "\n\
956 No selected thread. See `help thread'.\n");
957 }
958 }
959
960 /* Print information about currently known threads
961
962 Optional ARG is a thread id, or list of thread ids.
963
964 Note: this has the drawback that it _really_ switches
965 threads, which frees the frame cache. A no-side
966 effects info-threads command would be nicer. */
967
968 static void
969 info_threads_command (char *arg, int from_tty)
970 {
971 print_thread_info (uiout, arg, -1);
972 }
973
974 /* Switch from one thread to another. */
975
976 void
977 switch_to_thread (ptid_t ptid)
978 {
979 /* Switch the program space as well, if we can infer it from the now
980 current thread. Otherwise, it's up to the caller to select the
981 space it wants. */
982 if (!ptid_equal (ptid, null_ptid))
983 {
984 struct inferior *inf;
985
986 inf = find_inferior_pid (ptid_get_pid (ptid));
987 gdb_assert (inf != NULL);
988 set_current_program_space (inf->pspace);
989 set_current_inferior (inf);
990 }
991
992 if (ptid_equal (ptid, inferior_ptid))
993 return;
994
995 inferior_ptid = ptid;
996 reinit_frame_cache ();
997 registers_changed ();
998
999 /* We don't check for is_stopped, because we're called at times
1000 while in the TARGET_RUNNING state, e.g., while handling an
1001 internal event. */
1002 if (!ptid_equal (inferior_ptid, null_ptid)
1003 && !is_exited (ptid)
1004 && !is_executing (ptid))
1005 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1006 else
1007 stop_pc = ~(CORE_ADDR) 0;
1008 }
1009
1010 static void
1011 restore_current_thread (ptid_t ptid)
1012 {
1013 switch_to_thread (ptid);
1014 }
1015
1016 static void
1017 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1018 {
1019 struct frame_info *frame = NULL;
1020 int count;
1021
1022 /* This means there was no selected frame. */
1023 if (frame_level == -1)
1024 {
1025 select_frame (NULL);
1026 return;
1027 }
1028
1029 gdb_assert (frame_level >= 0);
1030
1031 /* Restore by level first, check if the frame id is the same as
1032 expected. If that fails, try restoring by frame id. If that
1033 fails, nothing to do, just warn the user. */
1034
1035 count = frame_level;
1036 frame = find_relative_frame (get_current_frame (), &count);
1037 if (count == 0
1038 && frame != NULL
1039 /* The frame ids must match - either both valid or both outer_frame_id.
1040 The latter case is not failsafe, but since it's highly unlikely
1041 the search by level finds the wrong frame, it's 99.9(9)% of
1042 the time (for all practical purposes) safe. */
1043 && frame_id_eq (get_frame_id (frame), a_frame_id))
1044 {
1045 /* Cool, all is fine. */
1046 select_frame (frame);
1047 return;
1048 }
1049
1050 frame = frame_find_by_id (a_frame_id);
1051 if (frame != NULL)
1052 {
1053 /* Cool, refound it. */
1054 select_frame (frame);
1055 return;
1056 }
1057
1058 /* Nothing else to do, the frame layout really changed. Select the
1059 innermost stack frame. */
1060 select_frame (get_current_frame ());
1061
1062 /* Warn the user. */
1063 if (frame_level > 0 && !ui_out_is_mi_like_p (uiout))
1064 {
1065 warning (_("Couldn't restore frame #%d in "
1066 "current thread, at reparsed frame #0\n"),
1067 frame_level);
1068 /* For MI, we should probably have a notification about
1069 current frame change. But this error is not very
1070 likely, so don't bother for now. */
1071 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
1072 }
1073 }
1074
1075 struct current_thread_cleanup
1076 {
1077 ptid_t inferior_ptid;
1078 struct frame_id selected_frame_id;
1079 int selected_frame_level;
1080 int was_stopped;
1081 int inf_id;
1082 };
1083
1084 static void
1085 do_restore_current_thread_cleanup (void *arg)
1086 {
1087 struct thread_info *tp;
1088 struct current_thread_cleanup *old = arg;
1089
1090 tp = find_thread_ptid (old->inferior_ptid);
1091
1092 /* If the previously selected thread belonged to a process that has
1093 in the mean time been deleted (due to normal exit, detach, etc.),
1094 then don't revert back to it, but instead simply drop back to no
1095 thread selected. */
1096 if (tp
1097 && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL)
1098 restore_current_thread (old->inferior_ptid);
1099 else
1100 {
1101 restore_current_thread (null_ptid);
1102 set_current_inferior (find_inferior_id (old->inf_id));
1103 }
1104
1105 /* The running state of the originally selected thread may have
1106 changed, so we have to recheck it here. */
1107 if (!ptid_equal (inferior_ptid, null_ptid)
1108 && old->was_stopped
1109 && is_stopped (inferior_ptid)
1110 && target_has_registers
1111 && target_has_stack
1112 && target_has_memory)
1113 restore_selected_frame (old->selected_frame_id,
1114 old->selected_frame_level);
1115 }
1116
1117 static void
1118 restore_current_thread_cleanup_dtor (void *arg)
1119 {
1120 struct current_thread_cleanup *old = arg;
1121 struct thread_info *tp;
1122
1123 tp = find_thread_ptid (old->inferior_ptid);
1124 if (tp)
1125 tp->refcount--;
1126 xfree (old);
1127 }
1128
1129 struct cleanup *
1130 make_cleanup_restore_current_thread (void)
1131 {
1132 struct thread_info *tp;
1133 struct frame_info *frame;
1134 struct current_thread_cleanup *old;
1135
1136 old = xmalloc (sizeof (struct current_thread_cleanup));
1137 old->inferior_ptid = inferior_ptid;
1138 old->inf_id = current_inferior ()->num;
1139
1140 if (!ptid_equal (inferior_ptid, null_ptid))
1141 {
1142 old->was_stopped = is_stopped (inferior_ptid);
1143 if (old->was_stopped
1144 && target_has_registers
1145 && target_has_stack
1146 && target_has_memory)
1147 {
1148 /* When processing internal events, there might not be a
1149 selected frame. If we naively call get_selected_frame
1150 here, then we can end up reading debuginfo for the
1151 current frame, but we don't generally need the debuginfo
1152 at this point. */
1153 frame = get_selected_frame_if_set ();
1154 }
1155 else
1156 frame = NULL;
1157
1158 old->selected_frame_id = get_frame_id (frame);
1159 old->selected_frame_level = frame_relative_level (frame);
1160
1161 tp = find_thread_ptid (inferior_ptid);
1162 if (tp)
1163 tp->refcount++;
1164 }
1165
1166 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1167 restore_current_thread_cleanup_dtor);
1168 }
1169
1170 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1171 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1172 of two numbers seperated by a hyphen. Examples:
1173
1174 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1175 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1176 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1177
1178 static void
1179 thread_apply_all_command (char *cmd, int from_tty)
1180 {
1181 struct thread_info *tp;
1182 struct cleanup *old_chain;
1183 char *saved_cmd;
1184
1185 if (cmd == NULL || *cmd == '\000')
1186 error (_("Please specify a command following the thread ID list"));
1187
1188 update_thread_list ();
1189
1190 old_chain = make_cleanup_restore_current_thread ();
1191
1192 /* Save a copy of the command in case it is clobbered by
1193 execute_command. */
1194 saved_cmd = xstrdup (cmd);
1195 make_cleanup (xfree, saved_cmd);
1196 for (tp = thread_list; tp; tp = tp->next)
1197 if (thread_alive (tp))
1198 {
1199 switch_to_thread (tp->ptid);
1200
1201 printf_filtered (_("\nThread %d (%s):\n"),
1202 tp->num, target_pid_to_str (inferior_ptid));
1203 execute_command (cmd, from_tty);
1204 strcpy (cmd, saved_cmd); /* Restore exact command used
1205 previously. */
1206 }
1207
1208 do_cleanups (old_chain);
1209 }
1210
1211 static void
1212 thread_apply_command (char *tidlist, int from_tty)
1213 {
1214 char *cmd;
1215 struct cleanup *old_chain;
1216 char *saved_cmd;
1217 struct get_number_or_range_state state;
1218
1219 if (tidlist == NULL || *tidlist == '\000')
1220 error (_("Please specify a thread ID list"));
1221
1222 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1223
1224 if (*cmd == '\000')
1225 error (_("Please specify a command following the thread ID list"));
1226
1227 /* Save a copy of the command in case it is clobbered by
1228 execute_command. */
1229 saved_cmd = xstrdup (cmd);
1230 old_chain = make_cleanup (xfree, saved_cmd);
1231
1232 init_number_or_range (&state, tidlist);
1233 while (!state.finished && state.string < cmd)
1234 {
1235 struct thread_info *tp;
1236 int start;
1237 char *p = tidlist;
1238
1239 start = get_number_or_range (&state);
1240
1241 make_cleanup_restore_current_thread ();
1242
1243 tp = find_thread_id (start);
1244
1245 if (!tp)
1246 warning (_("Unknown thread %d."), start);
1247 else if (!thread_alive (tp))
1248 warning (_("Thread %d has terminated."), start);
1249 else
1250 {
1251 switch_to_thread (tp->ptid);
1252
1253 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1254 target_pid_to_str (inferior_ptid));
1255 execute_command (cmd, from_tty);
1256
1257 /* Restore exact command used previously. */
1258 strcpy (cmd, saved_cmd);
1259 }
1260 }
1261
1262 do_cleanups (old_chain);
1263 }
1264
1265 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1266 if prefix of arg is `apply'. */
1267
1268 static void
1269 thread_command (char *tidstr, int from_tty)
1270 {
1271 if (!tidstr)
1272 {
1273 if (ptid_equal (inferior_ptid, null_ptid))
1274 error (_("No thread selected"));
1275
1276 if (target_has_stack)
1277 {
1278 if (is_exited (inferior_ptid))
1279 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1280 pid_to_thread_id (inferior_ptid),
1281 target_pid_to_str (inferior_ptid));
1282 else
1283 printf_filtered (_("[Current thread is %d (%s)]\n"),
1284 pid_to_thread_id (inferior_ptid),
1285 target_pid_to_str (inferior_ptid));
1286 }
1287 else
1288 error (_("No stack."));
1289 return;
1290 }
1291
1292 gdb_thread_select (uiout, tidstr, NULL);
1293 }
1294
1295 /* Implementation of `thread name'. */
1296
1297 static void
1298 thread_name_command (char *arg, int from_tty)
1299 {
1300 struct thread_info *info;
1301
1302 if (ptid_equal (inferior_ptid, null_ptid))
1303 error (_("No thread selected"));
1304
1305 while (arg && isspace (*arg))
1306 ++arg;
1307
1308 info = inferior_thread ();
1309 xfree (info->name);
1310 info->name = arg ? xstrdup (arg) : NULL;
1311 }
1312
1313 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1314
1315 static void
1316 thread_find_command (char *arg, int from_tty)
1317 {
1318 struct thread_info *tp;
1319 char *tmp;
1320 unsigned long match = 0;
1321
1322 if (arg == NULL || *arg == '\0')
1323 error (_("Command requires an argument."));
1324
1325 tmp = re_comp (arg);
1326 if (tmp != 0)
1327 error (_("Invalid regexp (%s): %s"), tmp, arg);
1328
1329 update_thread_list ();
1330 for (tp = thread_list; tp; tp = tp->next)
1331 {
1332 if (tp->name != NULL && re_exec (tp->name))
1333 {
1334 printf_filtered (_("Thread %d has name '%s'\n"),
1335 tp->num, tp->name);
1336 match++;
1337 }
1338
1339 tmp = target_thread_name (tp);
1340 if (tmp != NULL && re_exec (tmp))
1341 {
1342 printf_filtered (_("Thread %d has target name '%s'\n"),
1343 tp->num, tmp);
1344 match++;
1345 }
1346
1347 tmp = target_pid_to_str (tp->ptid);
1348 if (tmp != NULL && re_exec (tmp))
1349 {
1350 printf_filtered (_("Thread %d has target id '%s'\n"),
1351 tp->num, tmp);
1352 match++;
1353 }
1354
1355 tmp = target_extra_thread_info (tp);
1356 if (tmp != NULL && re_exec (tmp))
1357 {
1358 printf_filtered (_("Thread %d has extra info '%s'\n"),
1359 tp->num, tmp);
1360 match++;
1361 }
1362 }
1363 if (!match)
1364 printf_filtered (_("No threads match '%s'\n"), arg);
1365 }
1366
1367 /* Print notices when new threads are attached and detached. */
1368 int print_thread_events = 1;
1369 static void
1370 show_print_thread_events (struct ui_file *file, int from_tty,
1371 struct cmd_list_element *c, const char *value)
1372 {
1373 fprintf_filtered (file,
1374 _("Printing of thread events is %s.\n"),
1375 value);
1376 }
1377
1378 static int
1379 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1380 {
1381 int num;
1382 struct thread_info *tp;
1383
1384 num = value_as_long (parse_and_eval (tidstr));
1385
1386 tp = find_thread_id (num);
1387
1388 if (!tp)
1389 error (_("Thread ID %d not known."), num);
1390
1391 if (!thread_alive (tp))
1392 error (_("Thread ID %d has terminated."), num);
1393
1394 switch_to_thread (tp->ptid);
1395
1396 annotate_thread_changed ();
1397
1398 ui_out_text (uiout, "[Switching to thread ");
1399 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
1400 ui_out_text (uiout, " (");
1401 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1402 ui_out_text (uiout, ")]");
1403
1404 /* Note that we can't reach this with an exited thread, due to the
1405 thread_alive check above. */
1406 if (tp->state_ == THREAD_RUNNING)
1407 ui_out_text (uiout, "(running)\n");
1408 else
1409 {
1410 ui_out_text (uiout, "\n");
1411 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1412 }
1413
1414 /* Since the current thread may have changed, see if there is any
1415 exited thread we can now delete. */
1416 prune_threads ();
1417
1418 return GDB_RC_OK;
1419 }
1420
1421 enum gdb_rc
1422 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1423 {
1424 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1425 error_message, RETURN_MASK_ALL) < 0)
1426 return GDB_RC_FAIL;
1427 return GDB_RC_OK;
1428 }
1429
1430 void
1431 update_thread_list (void)
1432 {
1433 prune_threads ();
1434 target_find_new_threads ();
1435 }
1436
1437 /* Return a new value for the selected thread's id. Return a value of 0 if
1438 no thread is selected, or no threads exist. */
1439
1440 static struct value *
1441 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var)
1442 {
1443 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1444
1445 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1446 (tp ? tp->num : 0));
1447 }
1448
1449 /* Commands with a prefix of `thread'. */
1450 struct cmd_list_element *thread_cmd_list = NULL;
1451
1452 void
1453 _initialize_thread (void)
1454 {
1455 static struct cmd_list_element *thread_apply_list = NULL;
1456
1457 add_info ("threads", info_threads_command,
1458 _("Display currently known threads.\n\
1459 Usage: info threads [ID]...\n\
1460 Optional arguments are thread IDs with spaces between.\n\
1461 If no arguments, all threads are displayed."));
1462
1463 add_prefix_cmd ("thread", class_run, thread_command, _("\
1464 Use this command to switch between threads.\n\
1465 The new thread ID must be currently known."),
1466 &thread_cmd_list, "thread ", 1, &cmdlist);
1467
1468 add_prefix_cmd ("apply", class_run, thread_apply_command,
1469 _("Apply a command to a list of threads."),
1470 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
1471
1472 add_cmd ("all", class_run, thread_apply_all_command,
1473 _("Apply a command to all threads."), &thread_apply_list);
1474
1475 add_cmd ("name", class_run, thread_name_command,
1476 _("Set the current thread's name.\n\
1477 Usage: thread name [NAME]\n\
1478 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
1479
1480 add_cmd ("find", class_run, thread_find_command, _("\
1481 Find threads that match a regular expression.\n\
1482 Usage: thread find REGEXP\n\
1483 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1484 &thread_cmd_list);
1485
1486 if (!xdb_commands)
1487 add_com_alias ("t", "thread", class_run, 1);
1488
1489 add_setshow_boolean_cmd ("thread-events", no_class,
1490 &print_thread_events, _("\
1491 Set printing of thread events (such as thread start and exit)."), _("\
1492 Show printing of thread events (such as thread start and exit)."), NULL,
1493 NULL,
1494 show_print_thread_events,
1495 &setprintlist, &showprintlist);
1496
1497 create_internalvar_type_lazy ("_thread", thread_id_make_value);
1498 }