Centralize thread ID printing
[binutils-gdb.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
4
5 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "symtab.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "environ.h"
27 #include "value.h"
28 #include "target.h"
29 #include "gdbthread.h"
30 #include "command.h"
31 #include "gdbcmd.h"
32 #include "regcache.h"
33 #include "gdb.h"
34 #include "btrace.h"
35
36 #include <ctype.h>
37 #include <sys/types.h>
38 #include <signal.h>
39 #include "ui-out.h"
40 #include "observer.h"
41 #include "annotate.h"
42 #include "cli/cli-decode.h"
43 #include "gdb_regex.h"
44 #include "cli/cli-utils.h"
45 #include "thread-fsm.h"
46
47 /* Definition of struct thread_info exported to gdbthread.h. */
48
49 /* Prototypes for exported functions. */
50
51 void _initialize_thread (void);
52
53 /* Prototypes for local functions. */
54
55 struct thread_info *thread_list = NULL;
56 static int highest_thread_num;
57
58 /* True if any thread is, or may be executing. We need to track this
59 separately because until we fully sync the thread list, we won't
60 know whether the target is fully stopped, even if we see stop
61 events for all known threads, because any of those threads may have
62 spawned new threads we haven't heard of yet. */
63 static int threads_executing;
64
65 static void thread_apply_all_command (char *, int);
66 static int thread_alive (struct thread_info *);
67 static void info_threads_command (char *, int);
68 static void thread_apply_command (char *, int);
69 static void restore_current_thread (ptid_t);
70
71 /* Data to cleanup thread array. */
72
73 struct thread_array_cleanup
74 {
75 /* Array of thread pointers used to set
76 reference count. */
77 struct thread_info **tp_array;
78
79 /* Thread count in the array. */
80 int count;
81 };
82
83
84 struct thread_info*
85 inferior_thread (void)
86 {
87 struct thread_info *tp = find_thread_ptid (inferior_ptid);
88 gdb_assert (tp);
89 return tp;
90 }
91
92 /* Delete the breakpoint pointed at by BP_P, if there's one. */
93
94 static void
95 delete_thread_breakpoint (struct breakpoint **bp_p)
96 {
97 if (*bp_p != NULL)
98 {
99 delete_breakpoint (*bp_p);
100 *bp_p = NULL;
101 }
102 }
103
104 void
105 delete_step_resume_breakpoint (struct thread_info *tp)
106 {
107 if (tp != NULL)
108 delete_thread_breakpoint (&tp->control.step_resume_breakpoint);
109 }
110
111 void
112 delete_exception_resume_breakpoint (struct thread_info *tp)
113 {
114 if (tp != NULL)
115 delete_thread_breakpoint (&tp->control.exception_resume_breakpoint);
116 }
117
118 /* See gdbthread.h. */
119
120 void
121 delete_single_step_breakpoints (struct thread_info *tp)
122 {
123 if (tp != NULL)
124 delete_thread_breakpoint (&tp->control.single_step_breakpoints);
125 }
126
127 /* Delete the breakpoint pointed at by BP_P at the next stop, if
128 there's one. */
129
130 static void
131 delete_at_next_stop (struct breakpoint **bp)
132 {
133 if (*bp != NULL)
134 {
135 (*bp)->disposition = disp_del_at_next_stop;
136 *bp = NULL;
137 }
138 }
139
140 /* See gdbthread.h. */
141
142 int
143 thread_has_single_step_breakpoints_set (struct thread_info *tp)
144 {
145 return tp->control.single_step_breakpoints != NULL;
146 }
147
148 /* See gdbthread.h. */
149
150 int
151 thread_has_single_step_breakpoint_here (struct thread_info *tp,
152 struct address_space *aspace,
153 CORE_ADDR addr)
154 {
155 struct breakpoint *ss_bps = tp->control.single_step_breakpoints;
156
157 return (ss_bps != NULL
158 && breakpoint_has_location_inserted_here (ss_bps, aspace, addr));
159 }
160
161 /* See gdbthread.h. */
162
163 void
164 thread_cancel_execution_command (struct thread_info *thr)
165 {
166 if (thr->thread_fsm != NULL)
167 {
168 thread_fsm_clean_up (thr->thread_fsm);
169 thread_fsm_delete (thr->thread_fsm);
170 thr->thread_fsm = NULL;
171 }
172 }
173
174 static void
175 clear_thread_inferior_resources (struct thread_info *tp)
176 {
177 /* NOTE: this will take care of any left-over step_resume breakpoints,
178 but not any user-specified thread-specific breakpoints. We can not
179 delete the breakpoint straight-off, because the inferior might not
180 be stopped at the moment. */
181 delete_at_next_stop (&tp->control.step_resume_breakpoint);
182 delete_at_next_stop (&tp->control.exception_resume_breakpoint);
183 delete_at_next_stop (&tp->control.single_step_breakpoints);
184
185 delete_longjmp_breakpoint_at_next_stop (tp->num);
186
187 bpstat_clear (&tp->control.stop_bpstat);
188
189 btrace_teardown (tp);
190
191 thread_cancel_execution_command (tp);
192 }
193
194 static void
195 free_thread (struct thread_info *tp)
196 {
197 if (tp->priv)
198 {
199 if (tp->private_dtor)
200 tp->private_dtor (tp->priv);
201 else
202 xfree (tp->priv);
203 }
204
205 xfree (tp->name);
206 xfree (tp);
207 }
208
209 void
210 init_thread_list (void)
211 {
212 struct thread_info *tp, *tpnext;
213
214 highest_thread_num = 0;
215
216 if (!thread_list)
217 return;
218
219 for (tp = thread_list; tp; tp = tpnext)
220 {
221 tpnext = tp->next;
222 free_thread (tp);
223 }
224
225 thread_list = NULL;
226 threads_executing = 0;
227 }
228
229 /* Allocate a new thread with target id PTID and add it to the thread
230 list. */
231
232 static struct thread_info *
233 new_thread (ptid_t ptid)
234 {
235 struct thread_info *tp = XCNEW (struct thread_info);
236
237 tp->ptid = ptid;
238 tp->num = ++highest_thread_num;
239
240 if (thread_list == NULL)
241 thread_list = tp;
242 else
243 {
244 struct thread_info *last;
245
246 for (last = thread_list; last->next != NULL; last = last->next)
247 ;
248 last->next = tp;
249 }
250
251 /* Nothing to follow yet. */
252 tp->pending_follow.kind = TARGET_WAITKIND_SPURIOUS;
253 tp->state = THREAD_STOPPED;
254 tp->suspend.waitstatus.kind = TARGET_WAITKIND_IGNORE;
255
256 return tp;
257 }
258
259 struct thread_info *
260 add_thread_silent (ptid_t ptid)
261 {
262 struct thread_info *tp;
263
264 tp = find_thread_ptid (ptid);
265 if (tp)
266 /* Found an old thread with the same id. It has to be dead,
267 otherwise we wouldn't be adding a new thread with the same id.
268 The OS is reusing this id --- delete it, and recreate a new
269 one. */
270 {
271 /* In addition to deleting the thread, if this is the current
272 thread, then we need to take care that delete_thread doesn't
273 really delete the thread if it is inferior_ptid. Create a
274 new template thread in the list with an invalid ptid, switch
275 to it, delete the original thread, reset the new thread's
276 ptid, and switch to it. */
277
278 if (ptid_equal (inferior_ptid, ptid))
279 {
280 tp = new_thread (null_ptid);
281
282 /* Make switch_to_thread not read from the thread. */
283 tp->state = THREAD_EXITED;
284 switch_to_thread (null_ptid);
285
286 /* Now we can delete it. */
287 delete_thread (ptid);
288
289 /* Now reset its ptid, and reswitch inferior_ptid to it. */
290 tp->ptid = ptid;
291 tp->state = THREAD_STOPPED;
292 switch_to_thread (ptid);
293
294 observer_notify_new_thread (tp);
295
296 /* All done. */
297 return tp;
298 }
299 else
300 /* Just go ahead and delete it. */
301 delete_thread (ptid);
302 }
303
304 tp = new_thread (ptid);
305 observer_notify_new_thread (tp);
306
307 return tp;
308 }
309
310 struct thread_info *
311 add_thread_with_info (ptid_t ptid, struct private_thread_info *priv)
312 {
313 struct thread_info *result = add_thread_silent (ptid);
314
315 result->priv = priv;
316
317 if (print_thread_events)
318 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
319
320 annotate_new_thread ();
321 return result;
322 }
323
324 struct thread_info *
325 add_thread (ptid_t ptid)
326 {
327 return add_thread_with_info (ptid, NULL);
328 }
329
330 /* Add TP to the end of the step-over chain LIST_P. */
331
332 static void
333 step_over_chain_enqueue (struct thread_info **list_p, struct thread_info *tp)
334 {
335 gdb_assert (tp->step_over_next == NULL);
336 gdb_assert (tp->step_over_prev == NULL);
337
338 if (*list_p == NULL)
339 {
340 *list_p = tp;
341 tp->step_over_prev = tp->step_over_next = tp;
342 }
343 else
344 {
345 struct thread_info *head = *list_p;
346 struct thread_info *tail = head->step_over_prev;
347
348 tp->step_over_prev = tail;
349 tp->step_over_next = head;
350 head->step_over_prev = tp;
351 tail->step_over_next = tp;
352 }
353 }
354
355 /* Remove TP from step-over chain LIST_P. */
356
357 static void
358 step_over_chain_remove (struct thread_info **list_p, struct thread_info *tp)
359 {
360 gdb_assert (tp->step_over_next != NULL);
361 gdb_assert (tp->step_over_prev != NULL);
362
363 if (*list_p == tp)
364 {
365 if (tp == tp->step_over_next)
366 *list_p = NULL;
367 else
368 *list_p = tp->step_over_next;
369 }
370
371 tp->step_over_prev->step_over_next = tp->step_over_next;
372 tp->step_over_next->step_over_prev = tp->step_over_prev;
373 tp->step_over_prev = tp->step_over_next = NULL;
374 }
375
376 /* See gdbthread.h. */
377
378 struct thread_info *
379 thread_step_over_chain_next (struct thread_info *tp)
380 {
381 struct thread_info *next = tp->step_over_next;
382
383 return (next == step_over_queue_head ? NULL : next);
384 }
385
386 /* See gdbthread.h. */
387
388 int
389 thread_is_in_step_over_chain (struct thread_info *tp)
390 {
391 return (tp->step_over_next != NULL);
392 }
393
394 /* See gdbthread.h. */
395
396 void
397 thread_step_over_chain_enqueue (struct thread_info *tp)
398 {
399 step_over_chain_enqueue (&step_over_queue_head, tp);
400 }
401
402 /* See gdbthread.h. */
403
404 void
405 thread_step_over_chain_remove (struct thread_info *tp)
406 {
407 step_over_chain_remove (&step_over_queue_head, tp);
408 }
409
410 /* Delete thread PTID. If SILENT, don't notify the observer of this
411 exit. */
412 static void
413 delete_thread_1 (ptid_t ptid, int silent)
414 {
415 struct thread_info *tp, *tpprev;
416
417 tpprev = NULL;
418
419 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
420 if (ptid_equal (tp->ptid, ptid))
421 break;
422
423 if (!tp)
424 return;
425
426 /* Dead threads don't need to step-over. Remove from queue. */
427 if (tp->step_over_next != NULL)
428 thread_step_over_chain_remove (tp);
429
430 /* If this is the current thread, or there's code out there that
431 relies on it existing (refcount > 0) we can't delete yet. Mark
432 it as exited, and notify it. */
433 if (tp->refcount > 0
434 || ptid_equal (tp->ptid, inferior_ptid))
435 {
436 if (tp->state != THREAD_EXITED)
437 {
438 observer_notify_thread_exit (tp, silent);
439
440 /* Tag it as exited. */
441 tp->state = THREAD_EXITED;
442
443 /* Clear breakpoints, etc. associated with this thread. */
444 clear_thread_inferior_resources (tp);
445 }
446
447 /* Will be really deleted some other time. */
448 return;
449 }
450
451 /* Notify thread exit, but only if we haven't already. */
452 if (tp->state != THREAD_EXITED)
453 observer_notify_thread_exit (tp, silent);
454
455 /* Tag it as exited. */
456 tp->state = THREAD_EXITED;
457 clear_thread_inferior_resources (tp);
458
459 if (tpprev)
460 tpprev->next = tp->next;
461 else
462 thread_list = tp->next;
463
464 free_thread (tp);
465 }
466
467 /* Delete thread PTID and notify of thread exit. If this is
468 inferior_ptid, don't actually delete it, but tag it as exited and
469 do the notification. If PTID is the user selected thread, clear
470 it. */
471 void
472 delete_thread (ptid_t ptid)
473 {
474 delete_thread_1 (ptid, 0 /* not silent */);
475 }
476
477 void
478 delete_thread_silent (ptid_t ptid)
479 {
480 delete_thread_1 (ptid, 1 /* silent */);
481 }
482
483 struct thread_info *
484 find_thread_id (int num)
485 {
486 struct thread_info *tp;
487
488 for (tp = thread_list; tp; tp = tp->next)
489 if (tp->num == num)
490 return tp;
491
492 return NULL;
493 }
494
495 /* Find a thread_info by matching PTID. */
496 struct thread_info *
497 find_thread_ptid (ptid_t ptid)
498 {
499 struct thread_info *tp;
500
501 for (tp = thread_list; tp; tp = tp->next)
502 if (ptid_equal (tp->ptid, ptid))
503 return tp;
504
505 return NULL;
506 }
507
508 /*
509 * Thread iterator function.
510 *
511 * Calls a callback function once for each thread, so long as
512 * the callback function returns false. If the callback function
513 * returns true, the iteration will end and the current thread
514 * will be returned. This can be useful for implementing a
515 * search for a thread with arbitrary attributes, or for applying
516 * some operation to every thread.
517 *
518 * FIXME: some of the existing functionality, such as
519 * "Thread apply all", might be rewritten using this functionality.
520 */
521
522 struct thread_info *
523 iterate_over_threads (int (*callback) (struct thread_info *, void *),
524 void *data)
525 {
526 struct thread_info *tp, *next;
527
528 for (tp = thread_list; tp; tp = next)
529 {
530 next = tp->next;
531 if ((*callback) (tp, data))
532 return tp;
533 }
534
535 return NULL;
536 }
537
538 int
539 thread_count (void)
540 {
541 int result = 0;
542 struct thread_info *tp;
543
544 for (tp = thread_list; tp; tp = tp->next)
545 ++result;
546
547 return result;
548 }
549
550 int
551 valid_thread_id (int num)
552 {
553 struct thread_info *tp;
554
555 for (tp = thread_list; tp; tp = tp->next)
556 if (tp->num == num)
557 return 1;
558
559 return 0;
560 }
561
562 int
563 pid_to_thread_id (ptid_t ptid)
564 {
565 struct thread_info *tp;
566
567 for (tp = thread_list; tp; tp = tp->next)
568 if (ptid_equal (tp->ptid, ptid))
569 return tp->num;
570
571 return 0;
572 }
573
574 ptid_t
575 thread_id_to_pid (int num)
576 {
577 struct thread_info *thread = find_thread_id (num);
578
579 if (thread)
580 return thread->ptid;
581 else
582 return pid_to_ptid (-1);
583 }
584
585 int
586 in_thread_list (ptid_t ptid)
587 {
588 struct thread_info *tp;
589
590 for (tp = thread_list; tp; tp = tp->next)
591 if (ptid_equal (tp->ptid, ptid))
592 return 1;
593
594 return 0; /* Never heard of 'im. */
595 }
596
597 /* Finds the first thread of the inferior given by PID. If PID is -1,
598 return the first thread in the list. */
599
600 struct thread_info *
601 first_thread_of_process (int pid)
602 {
603 struct thread_info *tp, *ret = NULL;
604
605 for (tp = thread_list; tp; tp = tp->next)
606 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
607 if (ret == NULL || tp->num < ret->num)
608 ret = tp;
609
610 return ret;
611 }
612
613 struct thread_info *
614 any_thread_of_process (int pid)
615 {
616 struct thread_info *tp;
617
618 gdb_assert (pid != 0);
619
620 /* Prefer the current thread. */
621 if (ptid_get_pid (inferior_ptid) == pid)
622 return inferior_thread ();
623
624 ALL_NON_EXITED_THREADS (tp)
625 if (ptid_get_pid (tp->ptid) == pid)
626 return tp;
627
628 return NULL;
629 }
630
631 struct thread_info *
632 any_live_thread_of_process (int pid)
633 {
634 struct thread_info *curr_tp = NULL;
635 struct thread_info *tp;
636 struct thread_info *tp_executing = NULL;
637
638 gdb_assert (pid != 0);
639
640 /* Prefer the current thread if it's not executing. */
641 if (ptid_get_pid (inferior_ptid) == pid)
642 {
643 /* If the current thread is dead, forget it. If it's not
644 executing, use it. Otherwise, still choose it (below), but
645 only if no other non-executing thread is found. */
646 curr_tp = inferior_thread ();
647 if (curr_tp->state == THREAD_EXITED)
648 curr_tp = NULL;
649 else if (!curr_tp->executing)
650 return curr_tp;
651 }
652
653 ALL_NON_EXITED_THREADS (tp)
654 if (ptid_get_pid (tp->ptid) == pid)
655 {
656 if (!tp->executing)
657 return tp;
658
659 tp_executing = tp;
660 }
661
662 /* If both the current thread and all live threads are executing,
663 prefer the current thread. */
664 if (curr_tp != NULL)
665 return curr_tp;
666
667 /* Otherwise, just return an executing thread, if any. */
668 return tp_executing;
669 }
670
671 /* Print a list of thread ids currently known, and the total number of
672 threads. To be used from within catch_errors. */
673 static int
674 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
675 {
676 struct thread_info *tp;
677 int num = 0;
678 struct cleanup *cleanup_chain;
679 int current_thread = -1;
680
681 update_thread_list ();
682
683 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
684
685 for (tp = thread_list; tp; tp = tp->next)
686 {
687 if (tp->state == THREAD_EXITED)
688 continue;
689
690 if (ptid_equal (tp->ptid, inferior_ptid))
691 current_thread = tp->num;
692
693 num++;
694 ui_out_field_int (uiout, "thread-id", tp->num);
695 }
696
697 do_cleanups (cleanup_chain);
698
699 if (current_thread != -1)
700 ui_out_field_int (uiout, "current-thread-id", current_thread);
701 ui_out_field_int (uiout, "number-of-threads", num);
702 return GDB_RC_OK;
703 }
704
705 /* Official gdblib interface function to get a list of thread ids and
706 the total number. */
707 enum gdb_rc
708 gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
709 {
710 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
711 error_message, RETURN_MASK_ALL) < 0)
712 return GDB_RC_FAIL;
713 return GDB_RC_OK;
714 }
715
716 /* Return true if TP is an active thread. */
717 static int
718 thread_alive (struct thread_info *tp)
719 {
720 if (tp->state == THREAD_EXITED)
721 return 0;
722 if (!target_thread_alive (tp->ptid))
723 return 0;
724 return 1;
725 }
726
727 /* See gdbthreads.h. */
728
729 void
730 prune_threads (void)
731 {
732 struct thread_info *tp, *tmp;
733
734 ALL_THREADS_SAFE (tp, tmp)
735 {
736 if (!thread_alive (tp))
737 delete_thread (tp->ptid);
738 }
739 }
740
741 /* See gdbthreads.h. */
742
743 void
744 delete_exited_threads (void)
745 {
746 struct thread_info *tp, *tmp;
747
748 ALL_THREADS_SAFE (tp, tmp)
749 {
750 if (tp->state == THREAD_EXITED)
751 delete_thread (tp->ptid);
752 }
753 }
754
755 /* Disable storing stack temporaries for the thread whose id is
756 stored in DATA. */
757
758 static void
759 disable_thread_stack_temporaries (void *data)
760 {
761 ptid_t *pd = (ptid_t *) data;
762 struct thread_info *tp = find_thread_ptid (*pd);
763
764 if (tp != NULL)
765 {
766 tp->stack_temporaries_enabled = 0;
767 VEC_free (value_ptr, tp->stack_temporaries);
768 }
769
770 xfree (pd);
771 }
772
773 /* Enable storing stack temporaries for thread with id PTID and return a
774 cleanup which can disable and clear the stack temporaries. */
775
776 struct cleanup *
777 enable_thread_stack_temporaries (ptid_t ptid)
778 {
779 struct thread_info *tp = find_thread_ptid (ptid);
780 ptid_t *data;
781 struct cleanup *c;
782
783 gdb_assert (tp != NULL);
784
785 tp->stack_temporaries_enabled = 1;
786 tp->stack_temporaries = NULL;
787 data = XNEW (ptid_t);
788 *data = ptid;
789 c = make_cleanup (disable_thread_stack_temporaries, data);
790
791 return c;
792 }
793
794 /* Return non-zero value if stack temporaies are enabled for the thread
795 with id PTID. */
796
797 int
798 thread_stack_temporaries_enabled_p (ptid_t ptid)
799 {
800 struct thread_info *tp = find_thread_ptid (ptid);
801
802 if (tp == NULL)
803 return 0;
804 else
805 return tp->stack_temporaries_enabled;
806 }
807
808 /* Push V on to the stack temporaries of the thread with id PTID. */
809
810 void
811 push_thread_stack_temporary (ptid_t ptid, struct value *v)
812 {
813 struct thread_info *tp = find_thread_ptid (ptid);
814
815 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
816 VEC_safe_push (value_ptr, tp->stack_temporaries, v);
817 }
818
819 /* Return 1 if VAL is among the stack temporaries of the thread
820 with id PTID. Return 0 otherwise. */
821
822 int
823 value_in_thread_stack_temporaries (struct value *val, ptid_t ptid)
824 {
825 struct thread_info *tp = find_thread_ptid (ptid);
826
827 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
828 if (!VEC_empty (value_ptr, tp->stack_temporaries))
829 {
830 struct value *v;
831 int i;
832
833 for (i = 0; VEC_iterate (value_ptr, tp->stack_temporaries, i, v); i++)
834 if (v == val)
835 return 1;
836 }
837
838 return 0;
839 }
840
841 /* Return the last of the stack temporaries for thread with id PTID.
842 Return NULL if there are no stack temporaries for the thread. */
843
844 struct value *
845 get_last_thread_stack_temporary (ptid_t ptid)
846 {
847 struct value *lastval = NULL;
848 struct thread_info *tp = find_thread_ptid (ptid);
849
850 gdb_assert (tp != NULL);
851 if (!VEC_empty (value_ptr, tp->stack_temporaries))
852 lastval = VEC_last (value_ptr, tp->stack_temporaries);
853
854 return lastval;
855 }
856
857 void
858 thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
859 {
860 struct inferior *inf;
861 struct thread_info *tp;
862
863 /* It can happen that what we knew as the target inferior id
864 changes. E.g, target remote may only discover the remote process
865 pid after adding the inferior to GDB's list. */
866 inf = find_inferior_ptid (old_ptid);
867 inf->pid = ptid_get_pid (new_ptid);
868
869 tp = find_thread_ptid (old_ptid);
870 tp->ptid = new_ptid;
871
872 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
873 }
874
875 /* See gdbthread.h. */
876
877 void
878 set_resumed (ptid_t ptid, int resumed)
879 {
880 struct thread_info *tp;
881 int all = ptid_equal (ptid, minus_one_ptid);
882
883 if (all || ptid_is_pid (ptid))
884 {
885 for (tp = thread_list; tp; tp = tp->next)
886 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
887 tp->resumed = resumed;
888 }
889 else
890 {
891 tp = find_thread_ptid (ptid);
892 gdb_assert (tp != NULL);
893 tp->resumed = resumed;
894 }
895 }
896
897 /* Helper for set_running, that marks one thread either running or
898 stopped. */
899
900 static int
901 set_running_thread (struct thread_info *tp, int running)
902 {
903 int started = 0;
904
905 if (running && tp->state == THREAD_STOPPED)
906 started = 1;
907 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
908
909 if (!running)
910 {
911 /* If the thread is now marked stopped, remove it from
912 the step-over queue, so that we don't try to resume
913 it until the user wants it to. */
914 if (tp->step_over_next != NULL)
915 thread_step_over_chain_remove (tp);
916 }
917
918 return started;
919 }
920
921 void
922 set_running (ptid_t ptid, int running)
923 {
924 struct thread_info *tp;
925 int all = ptid_equal (ptid, minus_one_ptid);
926 int any_started = 0;
927
928 /* We try not to notify the observer if no thread has actually changed
929 the running state -- merely to reduce the number of messages to
930 frontend. Frontend is supposed to handle multiple *running just fine. */
931 if (all || ptid_is_pid (ptid))
932 {
933 for (tp = thread_list; tp; tp = tp->next)
934 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
935 {
936 if (tp->state == THREAD_EXITED)
937 continue;
938
939 if (set_running_thread (tp, running))
940 any_started = 1;
941 }
942 }
943 else
944 {
945 tp = find_thread_ptid (ptid);
946 gdb_assert (tp != NULL);
947 gdb_assert (tp->state != THREAD_EXITED);
948 if (set_running_thread (tp, running))
949 any_started = 1;
950 }
951 if (any_started)
952 observer_notify_target_resumed (ptid);
953 }
954
955 static int
956 is_thread_state (ptid_t ptid, enum thread_state state)
957 {
958 struct thread_info *tp;
959
960 tp = find_thread_ptid (ptid);
961 gdb_assert (tp);
962 return tp->state == state;
963 }
964
965 int
966 is_stopped (ptid_t ptid)
967 {
968 return is_thread_state (ptid, THREAD_STOPPED);
969 }
970
971 int
972 is_exited (ptid_t ptid)
973 {
974 return is_thread_state (ptid, THREAD_EXITED);
975 }
976
977 int
978 is_running (ptid_t ptid)
979 {
980 return is_thread_state (ptid, THREAD_RUNNING);
981 }
982
983 int
984 is_executing (ptid_t ptid)
985 {
986 struct thread_info *tp;
987
988 tp = find_thread_ptid (ptid);
989 gdb_assert (tp);
990 return tp->executing;
991 }
992
993 void
994 set_executing (ptid_t ptid, int executing)
995 {
996 struct thread_info *tp;
997 int all = ptid_equal (ptid, minus_one_ptid);
998
999 if (all || ptid_is_pid (ptid))
1000 {
1001 for (tp = thread_list; tp; tp = tp->next)
1002 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1003 tp->executing = executing;
1004 }
1005 else
1006 {
1007 tp = find_thread_ptid (ptid);
1008 gdb_assert (tp);
1009 tp->executing = executing;
1010 }
1011
1012 /* It only takes one running thread to spawn more threads.*/
1013 if (executing)
1014 threads_executing = 1;
1015 /* Only clear the flag if the caller is telling us everything is
1016 stopped. */
1017 else if (ptid_equal (minus_one_ptid, ptid))
1018 threads_executing = 0;
1019 }
1020
1021 /* See gdbthread.h. */
1022
1023 int
1024 threads_are_executing (void)
1025 {
1026 return threads_executing;
1027 }
1028
1029 void
1030 set_stop_requested (ptid_t ptid, int stop)
1031 {
1032 struct thread_info *tp;
1033 int all = ptid_equal (ptid, minus_one_ptid);
1034
1035 if (all || ptid_is_pid (ptid))
1036 {
1037 for (tp = thread_list; tp; tp = tp->next)
1038 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
1039 tp->stop_requested = stop;
1040 }
1041 else
1042 {
1043 tp = find_thread_ptid (ptid);
1044 gdb_assert (tp);
1045 tp->stop_requested = stop;
1046 }
1047
1048 /* Call the stop requested observer so other components of GDB can
1049 react to this request. */
1050 if (stop)
1051 observer_notify_thread_stop_requested (ptid);
1052 }
1053
1054 void
1055 finish_thread_state (ptid_t ptid)
1056 {
1057 struct thread_info *tp;
1058 int all;
1059 int any_started = 0;
1060
1061 all = ptid_equal (ptid, minus_one_ptid);
1062
1063 if (all || ptid_is_pid (ptid))
1064 {
1065 for (tp = thread_list; tp; tp = tp->next)
1066 {
1067 if (tp->state == THREAD_EXITED)
1068 continue;
1069 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
1070 {
1071 if (set_running_thread (tp, tp->executing))
1072 any_started = 1;
1073 }
1074 }
1075 }
1076 else
1077 {
1078 tp = find_thread_ptid (ptid);
1079 gdb_assert (tp);
1080 if (tp->state != THREAD_EXITED)
1081 {
1082 if (set_running_thread (tp, tp->executing))
1083 any_started = 1;
1084 }
1085 }
1086
1087 if (any_started)
1088 observer_notify_target_resumed (ptid);
1089 }
1090
1091 void
1092 finish_thread_state_cleanup (void *arg)
1093 {
1094 ptid_t *ptid_p = (ptid_t *) arg;
1095
1096 gdb_assert (arg);
1097
1098 finish_thread_state (*ptid_p);
1099 }
1100
1101 /* See gdbthread.h. */
1102
1103 void
1104 validate_registers_access (void)
1105 {
1106 /* No selected thread, no registers. */
1107 if (ptid_equal (inferior_ptid, null_ptid))
1108 error (_("No thread selected."));
1109
1110 /* Don't try to read from a dead thread. */
1111 if (is_exited (inferior_ptid))
1112 error (_("The current thread has terminated"));
1113
1114 /* ... or from a spinning thread. FIXME: This isn't actually fully
1115 correct. It'll allow an user-requested access (e.g., "print $pc"
1116 at the prompt) when a thread is not executing for some internal
1117 reason, but is marked running from the user's perspective. E.g.,
1118 the thread is waiting for its turn in the step-over queue. */
1119 if (is_executing (inferior_ptid))
1120 error (_("Selected thread is running."));
1121 }
1122
1123 int
1124 pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
1125 {
1126 return (pc >= thread->control.step_range_start
1127 && pc < thread->control.step_range_end);
1128 }
1129
1130 /* Prints the list of threads and their details on UIOUT.
1131 This is a version of 'info_threads_command' suitable for
1132 use from MI.
1133 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
1134 that should be printed. Otherwise, all threads are
1135 printed.
1136 If PID is not -1, only print threads from the process PID.
1137 Otherwise, threads from all attached PIDs are printed.
1138 If both REQUESTED_THREAD and PID are not -1, then the thread
1139 is printed if it belongs to the specified process. Otherwise,
1140 an error is raised. */
1141 void
1142 print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
1143 {
1144 struct thread_info *tp;
1145 ptid_t current_ptid;
1146 struct cleanup *old_chain;
1147 const char *extra_info, *name, *target_id;
1148 int current_thread = -1;
1149
1150 update_thread_list ();
1151 current_ptid = inferior_ptid;
1152
1153 /* We'll be switching threads temporarily. */
1154 old_chain = make_cleanup_restore_current_thread ();
1155
1156 /* For backward compatibility, we make a list for MI. A table is
1157 preferable for the CLI, though, because it shows table
1158 headers. */
1159 if (ui_out_is_mi_like_p (uiout))
1160 make_cleanup_ui_out_list_begin_end (uiout, "threads");
1161 else
1162 {
1163 int n_threads = 0;
1164
1165 for (tp = thread_list; tp; tp = tp->next)
1166 {
1167 if (!number_is_in_list (requested_threads, tp->num))
1168 continue;
1169
1170 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1171 continue;
1172
1173 if (tp->state == THREAD_EXITED)
1174 continue;
1175
1176 ++n_threads;
1177 }
1178
1179 if (n_threads == 0)
1180 {
1181 if (requested_threads == NULL || *requested_threads == '\0')
1182 ui_out_message (uiout, 0, _("No threads.\n"));
1183 else
1184 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
1185 requested_threads);
1186 do_cleanups (old_chain);
1187 return;
1188 }
1189
1190 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
1191
1192 ui_out_table_header (uiout, 1, ui_left, "current", "");
1193 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
1194 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
1195 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
1196 ui_out_table_body (uiout);
1197 }
1198
1199 for (tp = thread_list; tp; tp = tp->next)
1200 {
1201 struct cleanup *chain2;
1202 int core;
1203
1204 if (!number_is_in_list (requested_threads, tp->num))
1205 continue;
1206
1207 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
1208 {
1209 if (requested_threads != NULL && *requested_threads != '\0')
1210 error (_("Requested thread not found in requested process"));
1211 continue;
1212 }
1213
1214 if (ptid_equal (tp->ptid, current_ptid))
1215 current_thread = tp->num;
1216
1217 if (tp->state == THREAD_EXITED)
1218 continue;
1219
1220 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
1221
1222 if (ui_out_is_mi_like_p (uiout))
1223 {
1224 /* Compatibility. */
1225 if (ptid_equal (tp->ptid, current_ptid))
1226 ui_out_text (uiout, "* ");
1227 else
1228 ui_out_text (uiout, " ");
1229 }
1230 else
1231 {
1232 if (ptid_equal (tp->ptid, current_ptid))
1233 ui_out_field_string (uiout, "current", "*");
1234 else
1235 ui_out_field_skip (uiout, "current");
1236 }
1237
1238 ui_out_field_int (uiout, "id", tp->num);
1239
1240 /* For the CLI, we stuff everything into the target-id field.
1241 This is a gross hack to make the output come out looking
1242 correct. The underlying problem here is that ui-out has no
1243 way to specify that a field's space allocation should be
1244 shared by several fields. For MI, we do the right thing
1245 instead. */
1246
1247 target_id = target_pid_to_str (tp->ptid);
1248 extra_info = target_extra_thread_info (tp);
1249 name = tp->name ? tp->name : target_thread_name (tp);
1250
1251 if (ui_out_is_mi_like_p (uiout))
1252 {
1253 ui_out_field_string (uiout, "target-id", target_id);
1254 if (extra_info)
1255 ui_out_field_string (uiout, "details", extra_info);
1256 if (name)
1257 ui_out_field_string (uiout, "name", name);
1258 }
1259 else
1260 {
1261 struct cleanup *str_cleanup;
1262 char *contents;
1263
1264 if (extra_info && name)
1265 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
1266 name, extra_info);
1267 else if (extra_info)
1268 contents = xstrprintf ("%s (%s)", target_id, extra_info);
1269 else if (name)
1270 contents = xstrprintf ("%s \"%s\"", target_id, name);
1271 else
1272 contents = xstrdup (target_id);
1273 str_cleanup = make_cleanup (xfree, contents);
1274
1275 ui_out_field_string (uiout, "target-id", contents);
1276 do_cleanups (str_cleanup);
1277 }
1278
1279 if (tp->state == THREAD_RUNNING)
1280 ui_out_text (uiout, "(running)\n");
1281 else
1282 {
1283 /* The switch below puts us at the top of the stack (leaf
1284 frame). */
1285 switch_to_thread (tp->ptid);
1286 print_stack_frame (get_selected_frame (NULL),
1287 /* For MI output, print frame level. */
1288 ui_out_is_mi_like_p (uiout),
1289 LOCATION, 0);
1290 }
1291
1292 if (ui_out_is_mi_like_p (uiout))
1293 {
1294 char *state = "stopped";
1295
1296 if (tp->state == THREAD_RUNNING)
1297 state = "running";
1298 ui_out_field_string (uiout, "state", state);
1299 }
1300
1301 core = target_core_of_thread (tp->ptid);
1302 if (ui_out_is_mi_like_p (uiout) && core != -1)
1303 ui_out_field_int (uiout, "core", core);
1304
1305 do_cleanups (chain2);
1306 }
1307
1308 /* Restores the current thread and the frame selected before
1309 the "info threads" command. */
1310 do_cleanups (old_chain);
1311
1312 if (pid == -1 && requested_threads == NULL)
1313 {
1314 gdb_assert (current_thread != -1
1315 || !thread_list
1316 || ptid_equal (inferior_ptid, null_ptid));
1317 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
1318 ui_out_field_int (uiout, "current-thread-id", current_thread);
1319
1320 if (current_thread != -1 && is_exited (current_ptid))
1321 ui_out_message (uiout, 0, "\n\
1322 The current thread <Thread ID %d> has terminated. See `help thread'.\n",
1323 current_thread);
1324 else if (thread_list
1325 && current_thread == -1
1326 && ptid_equal (current_ptid, null_ptid))
1327 ui_out_message (uiout, 0, "\n\
1328 No selected thread. See `help thread'.\n");
1329 }
1330 }
1331
1332 /* Print information about currently known threads
1333
1334 Optional ARG is a thread id, or list of thread ids.
1335
1336 Note: this has the drawback that it _really_ switches
1337 threads, which frees the frame cache. A no-side
1338 effects info-threads command would be nicer. */
1339
1340 static void
1341 info_threads_command (char *arg, int from_tty)
1342 {
1343 print_thread_info (current_uiout, arg, -1);
1344 }
1345
1346 /* See gdbthread.h. */
1347
1348 void
1349 switch_to_thread_no_regs (struct thread_info *thread)
1350 {
1351 struct inferior *inf;
1352
1353 inf = find_inferior_ptid (thread->ptid);
1354 gdb_assert (inf != NULL);
1355 set_current_program_space (inf->pspace);
1356 set_current_inferior (inf);
1357
1358 inferior_ptid = thread->ptid;
1359 stop_pc = ~(CORE_ADDR) 0;
1360 }
1361
1362 /* Switch from one thread to another. */
1363
1364 void
1365 switch_to_thread (ptid_t ptid)
1366 {
1367 /* Switch the program space as well, if we can infer it from the now
1368 current thread. Otherwise, it's up to the caller to select the
1369 space it wants. */
1370 if (!ptid_equal (ptid, null_ptid))
1371 {
1372 struct inferior *inf;
1373
1374 inf = find_inferior_ptid (ptid);
1375 gdb_assert (inf != NULL);
1376 set_current_program_space (inf->pspace);
1377 set_current_inferior (inf);
1378 }
1379
1380 if (ptid_equal (ptid, inferior_ptid))
1381 return;
1382
1383 inferior_ptid = ptid;
1384 reinit_frame_cache ();
1385
1386 /* We don't check for is_stopped, because we're called at times
1387 while in the TARGET_RUNNING state, e.g., while handling an
1388 internal event. */
1389 if (!ptid_equal (inferior_ptid, null_ptid)
1390 && !is_exited (ptid)
1391 && !is_executing (ptid))
1392 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
1393 else
1394 stop_pc = ~(CORE_ADDR) 0;
1395 }
1396
1397 static void
1398 restore_current_thread (ptid_t ptid)
1399 {
1400 switch_to_thread (ptid);
1401 }
1402
1403 static void
1404 restore_selected_frame (struct frame_id a_frame_id, int frame_level)
1405 {
1406 struct frame_info *frame = NULL;
1407 int count;
1408
1409 /* This means there was no selected frame. */
1410 if (frame_level == -1)
1411 {
1412 select_frame (NULL);
1413 return;
1414 }
1415
1416 gdb_assert (frame_level >= 0);
1417
1418 /* Restore by level first, check if the frame id is the same as
1419 expected. If that fails, try restoring by frame id. If that
1420 fails, nothing to do, just warn the user. */
1421
1422 count = frame_level;
1423 frame = find_relative_frame (get_current_frame (), &count);
1424 if (count == 0
1425 && frame != NULL
1426 /* The frame ids must match - either both valid or both outer_frame_id.
1427 The latter case is not failsafe, but since it's highly unlikely
1428 the search by level finds the wrong frame, it's 99.9(9)% of
1429 the time (for all practical purposes) safe. */
1430 && frame_id_eq (get_frame_id (frame), a_frame_id))
1431 {
1432 /* Cool, all is fine. */
1433 select_frame (frame);
1434 return;
1435 }
1436
1437 frame = frame_find_by_id (a_frame_id);
1438 if (frame != NULL)
1439 {
1440 /* Cool, refound it. */
1441 select_frame (frame);
1442 return;
1443 }
1444
1445 /* Nothing else to do, the frame layout really changed. Select the
1446 innermost stack frame. */
1447 select_frame (get_current_frame ());
1448
1449 /* Warn the user. */
1450 if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
1451 {
1452 warning (_("Couldn't restore frame #%d in "
1453 "current thread. Bottom (innermost) frame selected:"),
1454 frame_level);
1455 /* For MI, we should probably have a notification about
1456 current frame change. But this error is not very
1457 likely, so don't bother for now. */
1458 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1459 }
1460 }
1461
1462 /* Data used by the cleanup installed by
1463 'make_cleanup_restore_current_thread'. */
1464
1465 struct current_thread_cleanup
1466 {
1467 /* Next in list of currently installed 'struct
1468 current_thread_cleanup' cleanups. See
1469 'current_thread_cleanup_chain' below. */
1470 struct current_thread_cleanup *next;
1471
1472 ptid_t inferior_ptid;
1473 struct frame_id selected_frame_id;
1474 int selected_frame_level;
1475 int was_stopped;
1476 int inf_id;
1477 int was_removable;
1478 };
1479
1480 /* A chain of currently installed 'struct current_thread_cleanup'
1481 cleanups. Restoring the previously selected thread looks up the
1482 old thread in the thread list by ptid. If the thread changes ptid,
1483 we need to update the cleanup's thread structure so the look up
1484 succeeds. */
1485 static struct current_thread_cleanup *current_thread_cleanup_chain;
1486
1487 /* A thread_ptid_changed observer. Update all currently installed
1488 current_thread_cleanup cleanups that want to switch back to
1489 OLD_PTID to switch back to NEW_PTID instead. */
1490
1491 static void
1492 restore_current_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
1493 {
1494 struct current_thread_cleanup *it;
1495
1496 for (it = current_thread_cleanup_chain; it != NULL; it = it->next)
1497 {
1498 if (ptid_equal (it->inferior_ptid, old_ptid))
1499 it->inferior_ptid = new_ptid;
1500 }
1501 }
1502
1503 static void
1504 do_restore_current_thread_cleanup (void *arg)
1505 {
1506 struct thread_info *tp;
1507 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1508
1509 tp = find_thread_ptid (old->inferior_ptid);
1510
1511 /* If the previously selected thread belonged to a process that has
1512 in the mean time been deleted (due to normal exit, detach, etc.),
1513 then don't revert back to it, but instead simply drop back to no
1514 thread selected. */
1515 if (tp
1516 && find_inferior_ptid (tp->ptid) != NULL)
1517 restore_current_thread (old->inferior_ptid);
1518 else
1519 {
1520 restore_current_thread (null_ptid);
1521 set_current_inferior (find_inferior_id (old->inf_id));
1522 }
1523
1524 /* The running state of the originally selected thread may have
1525 changed, so we have to recheck it here. */
1526 if (!ptid_equal (inferior_ptid, null_ptid)
1527 && old->was_stopped
1528 && is_stopped (inferior_ptid)
1529 && target_has_registers
1530 && target_has_stack
1531 && target_has_memory)
1532 restore_selected_frame (old->selected_frame_id,
1533 old->selected_frame_level);
1534 }
1535
1536 static void
1537 restore_current_thread_cleanup_dtor (void *arg)
1538 {
1539 struct current_thread_cleanup *old = (struct current_thread_cleanup *) arg;
1540 struct thread_info *tp;
1541 struct inferior *inf;
1542
1543 current_thread_cleanup_chain = current_thread_cleanup_chain->next;
1544
1545 tp = find_thread_ptid (old->inferior_ptid);
1546 if (tp)
1547 tp->refcount--;
1548 inf = find_inferior_id (old->inf_id);
1549 if (inf != NULL)
1550 inf->removable = old->was_removable;
1551 xfree (old);
1552 }
1553
1554 /* Set the thread reference count. */
1555
1556 static void
1557 set_thread_refcount (void *data)
1558 {
1559 int k;
1560 struct thread_array_cleanup *ta_cleanup
1561 = (struct thread_array_cleanup *) data;
1562
1563 for (k = 0; k != ta_cleanup->count; k++)
1564 ta_cleanup->tp_array[k]->refcount--;
1565 }
1566
1567 struct cleanup *
1568 make_cleanup_restore_current_thread (void)
1569 {
1570 struct thread_info *tp;
1571 struct frame_info *frame;
1572 struct current_thread_cleanup *old = XNEW (struct current_thread_cleanup);
1573
1574 old->inferior_ptid = inferior_ptid;
1575 old->inf_id = current_inferior ()->num;
1576 old->was_removable = current_inferior ()->removable;
1577
1578 old->next = current_thread_cleanup_chain;
1579 current_thread_cleanup_chain = old;
1580
1581 if (!ptid_equal (inferior_ptid, null_ptid))
1582 {
1583 old->was_stopped = is_stopped (inferior_ptid);
1584 if (old->was_stopped
1585 && target_has_registers
1586 && target_has_stack
1587 && target_has_memory)
1588 {
1589 /* When processing internal events, there might not be a
1590 selected frame. If we naively call get_selected_frame
1591 here, then we can end up reading debuginfo for the
1592 current frame, but we don't generally need the debuginfo
1593 at this point. */
1594 frame = get_selected_frame_if_set ();
1595 }
1596 else
1597 frame = NULL;
1598
1599 old->selected_frame_id = get_frame_id (frame);
1600 old->selected_frame_level = frame_relative_level (frame);
1601
1602 tp = find_thread_ptid (inferior_ptid);
1603 if (tp)
1604 tp->refcount++;
1605 }
1606
1607 current_inferior ()->removable = 0;
1608
1609 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1610 restore_current_thread_cleanup_dtor);
1611 }
1612
1613 /* See gdbthread.h. */
1614
1615 const char *
1616 print_thread_id (struct thread_info *thr)
1617 {
1618 char *s = get_print_cell ();
1619
1620 xsnprintf (s, PRINT_CELL_SIZE, "%d", thr->num);
1621 return s;
1622 }
1623
1624 /* If non-zero tp_array_compar should sort in ascending order, otherwise in
1625 descending order. */
1626
1627 static int tp_array_compar_ascending;
1628
1629 /* Sort an array for struct thread_info pointers by their NUM, order is
1630 determined by TP_ARRAY_COMPAR_ASCENDING. */
1631
1632 static int
1633 tp_array_compar (const void *ap_voidp, const void *bp_voidp)
1634 {
1635 const struct thread_info *const *ap
1636 = (const struct thread_info * const*) ap_voidp;
1637 const struct thread_info *const *bp
1638 = (const struct thread_info * const*) bp_voidp;
1639
1640 return ((((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num))
1641 * (tp_array_compar_ascending ? +1 : -1));
1642 }
1643
1644 /* Apply a GDB command to a list of threads. List syntax is a whitespace
1645 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1646 of two numbers seperated by a hyphen. Examples:
1647
1648 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1649 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1650 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
1651
1652 static void
1653 thread_apply_all_command (char *cmd, int from_tty)
1654 {
1655 struct cleanup *old_chain;
1656 char *saved_cmd;
1657 int tc;
1658 struct thread_array_cleanup ta_cleanup;
1659
1660 tp_array_compar_ascending = 0;
1661 if (cmd != NULL
1662 && check_for_argument (&cmd, "-ascending", strlen ("-ascending")))
1663 {
1664 cmd = skip_spaces (cmd);
1665 tp_array_compar_ascending = 1;
1666 }
1667
1668 if (cmd == NULL || *cmd == '\000')
1669 error (_("Please specify a command following the thread ID list"));
1670
1671 update_thread_list ();
1672
1673 old_chain = make_cleanup_restore_current_thread ();
1674
1675 /* Save a copy of the command in case it is clobbered by
1676 execute_command. */
1677 saved_cmd = xstrdup (cmd);
1678 make_cleanup (xfree, saved_cmd);
1679
1680 /* Note this includes exited threads. */
1681 tc = thread_count ();
1682 if (tc != 0)
1683 {
1684 struct thread_info **tp_array;
1685 struct thread_info *tp;
1686 int i = 0, k;
1687
1688 /* Save a copy of the thread_list in case we execute detach
1689 command. */
1690 tp_array = XNEWVEC (struct thread_info *, tc);
1691 make_cleanup (xfree, tp_array);
1692
1693 ALL_NON_EXITED_THREADS (tp)
1694 {
1695 tp_array[i] = tp;
1696 tp->refcount++;
1697 i++;
1698 }
1699 /* Because we skipped exited threads, we may end up with fewer
1700 threads in the array than the total count of threads. */
1701 gdb_assert (i <= tc);
1702
1703 if (i != 0)
1704 qsort (tp_array, i, sizeof (*tp_array), tp_array_compar);
1705
1706 ta_cleanup.tp_array = tp_array;
1707 ta_cleanup.count = i;
1708 make_cleanup (set_thread_refcount, &ta_cleanup);
1709
1710 for (k = 0; k != i; k++)
1711 if (thread_alive (tp_array[k]))
1712 {
1713 switch_to_thread (tp_array[k]->ptid);
1714 printf_filtered (_("\nThread %s (%s):\n"),
1715 print_thread_id (tp_array[k]),
1716 target_pid_to_str (inferior_ptid));
1717 execute_command (cmd, from_tty);
1718
1719 /* Restore exact command used previously. */
1720 strcpy (cmd, saved_cmd);
1721 }
1722 }
1723
1724 do_cleanups (old_chain);
1725 }
1726
1727 /* Implementation of the "thread apply" command. */
1728
1729 static void
1730 thread_apply_command (char *tidlist, int from_tty)
1731 {
1732 char *cmd;
1733 struct cleanup *old_chain;
1734 char *saved_cmd;
1735 struct get_number_or_range_state state;
1736
1737 if (tidlist == NULL || *tidlist == '\000')
1738 error (_("Please specify a thread ID list"));
1739
1740 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
1741
1742 if (*cmd == '\000')
1743 error (_("Please specify a command following the thread ID list"));
1744
1745 /* Save a copy of the command in case it is clobbered by
1746 execute_command. */
1747 saved_cmd = xstrdup (cmd);
1748 old_chain = make_cleanup (xfree, saved_cmd);
1749
1750 init_number_or_range (&state, tidlist);
1751 while (!state.finished && state.string < cmd)
1752 {
1753 struct thread_info *tp;
1754 int start;
1755
1756 start = get_number_or_range (&state);
1757
1758 make_cleanup_restore_current_thread ();
1759
1760 tp = find_thread_id (start);
1761
1762 if (!tp)
1763 warning (_("Unknown thread %d."), start);
1764 else if (!thread_alive (tp))
1765 warning (_("Thread %d has terminated."), start);
1766 else
1767 {
1768 switch_to_thread (tp->ptid);
1769
1770 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1771 target_pid_to_str (inferior_ptid));
1772 execute_command (cmd, from_tty);
1773
1774 /* Restore exact command used previously. */
1775 strcpy (cmd, saved_cmd);
1776 }
1777 }
1778
1779 do_cleanups (old_chain);
1780 }
1781
1782 /* Switch to the specified thread. Will dispatch off to thread_apply_command
1783 if prefix of arg is `apply'. */
1784
1785 void
1786 thread_command (char *tidstr, int from_tty)
1787 {
1788 if (!tidstr)
1789 {
1790 if (ptid_equal (inferior_ptid, null_ptid))
1791 error (_("No thread selected"));
1792
1793 if (target_has_stack)
1794 {
1795 struct thread_info *tp = inferior_thread ();
1796
1797 if (is_exited (inferior_ptid))
1798 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1799 print_thread_id (tp),
1800 target_pid_to_str (inferior_ptid));
1801 else
1802 printf_filtered (_("[Current thread is %s (%s)]\n"),
1803 print_thread_id (tp),
1804 target_pid_to_str (inferior_ptid));
1805 }
1806 else
1807 error (_("No stack."));
1808 return;
1809 }
1810
1811 gdb_thread_select (current_uiout, tidstr, NULL);
1812 }
1813
1814 /* Implementation of `thread name'. */
1815
1816 static void
1817 thread_name_command (char *arg, int from_tty)
1818 {
1819 struct thread_info *info;
1820
1821 if (ptid_equal (inferior_ptid, null_ptid))
1822 error (_("No thread selected"));
1823
1824 arg = skip_spaces (arg);
1825
1826 info = inferior_thread ();
1827 xfree (info->name);
1828 info->name = arg ? xstrdup (arg) : NULL;
1829 }
1830
1831 /* Find thread ids with a name, target pid, or extra info matching ARG. */
1832
1833 static void
1834 thread_find_command (char *arg, int from_tty)
1835 {
1836 struct thread_info *tp;
1837 const char *tmp;
1838 unsigned long match = 0;
1839
1840 if (arg == NULL || *arg == '\0')
1841 error (_("Command requires an argument."));
1842
1843 tmp = re_comp (arg);
1844 if (tmp != 0)
1845 error (_("Invalid regexp (%s): %s"), tmp, arg);
1846
1847 update_thread_list ();
1848 for (tp = thread_list; tp; tp = tp->next)
1849 {
1850 if (tp->name != NULL && re_exec (tp->name))
1851 {
1852 printf_filtered (_("Thread %s has name '%s'\n"),
1853 print_thread_id (tp), tp->name);
1854 match++;
1855 }
1856
1857 tmp = target_thread_name (tp);
1858 if (tmp != NULL && re_exec (tmp))
1859 {
1860 printf_filtered (_("Thread %s has target name '%s'\n"),
1861 print_thread_id (tp), tmp);
1862 match++;
1863 }
1864
1865 tmp = target_pid_to_str (tp->ptid);
1866 if (tmp != NULL && re_exec (tmp))
1867 {
1868 printf_filtered (_("Thread %s has target id '%s'\n"),
1869 print_thread_id (tp), tmp);
1870 match++;
1871 }
1872
1873 tmp = target_extra_thread_info (tp);
1874 if (tmp != NULL && re_exec (tmp))
1875 {
1876 printf_filtered (_("Thread %s has extra info '%s'\n"),
1877 print_thread_id (tp), tmp);
1878 match++;
1879 }
1880 }
1881 if (!match)
1882 printf_filtered (_("No threads match '%s'\n"), arg);
1883 }
1884
1885 /* Print notices when new threads are attached and detached. */
1886 int print_thread_events = 1;
1887 static void
1888 show_print_thread_events (struct ui_file *file, int from_tty,
1889 struct cmd_list_element *c, const char *value)
1890 {
1891 fprintf_filtered (file,
1892 _("Printing of thread events is %s.\n"),
1893 value);
1894 }
1895
1896 static int
1897 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
1898 {
1899 int num;
1900 struct thread_info *tp;
1901
1902 num = value_as_long (parse_and_eval ((const char *) tidstr));
1903
1904 tp = find_thread_id (num);
1905
1906 if (!tp)
1907 error (_("Thread ID %d not known."), num);
1908
1909 if (!thread_alive (tp))
1910 error (_("Thread ID %d has terminated."), num);
1911
1912 switch_to_thread (tp->ptid);
1913
1914 annotate_thread_changed ();
1915
1916 ui_out_text (uiout, "[Switching to thread ");
1917 ui_out_field_string (uiout, "new-thread-id", print_thread_id (tp));
1918 ui_out_text (uiout, " (");
1919 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
1920 ui_out_text (uiout, ")]");
1921
1922 /* Note that we can't reach this with an exited thread, due to the
1923 thread_alive check above. */
1924 if (tp->state == THREAD_RUNNING)
1925 ui_out_text (uiout, "(running)\n");
1926 else
1927 {
1928 ui_out_text (uiout, "\n");
1929 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1930 }
1931
1932 /* Since the current thread may have changed, see if there is any
1933 exited thread we can now delete. */
1934 prune_threads ();
1935
1936 return GDB_RC_OK;
1937 }
1938
1939 enum gdb_rc
1940 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
1941 {
1942 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1943 error_message, RETURN_MASK_ALL) < 0)
1944 return GDB_RC_FAIL;
1945 return GDB_RC_OK;
1946 }
1947
1948 /* Update the 'threads_executing' global based on the threads we know
1949 about right now. */
1950
1951 static void
1952 update_threads_executing (void)
1953 {
1954 struct thread_info *tp;
1955
1956 threads_executing = 0;
1957 ALL_NON_EXITED_THREADS (tp)
1958 {
1959 if (tp->executing)
1960 {
1961 threads_executing = 1;
1962 break;
1963 }
1964 }
1965 }
1966
1967 void
1968 update_thread_list (void)
1969 {
1970 target_update_thread_list ();
1971 update_threads_executing ();
1972 }
1973
1974 /* Return a new value for the selected thread's id. Return a value of 0 if
1975 no thread is selected, or no threads exist. */
1976
1977 static struct value *
1978 thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1979 void *ignore)
1980 {
1981 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1982
1983 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1984 (tp ? tp->num : 0));
1985 }
1986
1987 /* Commands with a prefix of `thread'. */
1988 struct cmd_list_element *thread_cmd_list = NULL;
1989
1990 /* Implementation of `thread' variable. */
1991
1992 static const struct internalvar_funcs thread_funcs =
1993 {
1994 thread_id_make_value,
1995 NULL,
1996 NULL
1997 };
1998
1999 void
2000 _initialize_thread (void)
2001 {
2002 static struct cmd_list_element *thread_apply_list = NULL;
2003
2004 add_info ("threads", info_threads_command,
2005 _("Display currently known threads.\n\
2006 Usage: info threads [ID]...\n\
2007 Optional arguments are thread IDs with spaces between.\n\
2008 If no arguments, all threads are displayed."));
2009
2010 add_prefix_cmd ("thread", class_run, thread_command, _("\
2011 Use this command to switch between threads.\n\
2012 The new thread ID must be currently known."),
2013 &thread_cmd_list, "thread ", 1, &cmdlist);
2014
2015 add_prefix_cmd ("apply", class_run, thread_apply_command,
2016 _("Apply a command to a list of threads."),
2017 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
2018
2019 add_cmd ("all", class_run, thread_apply_all_command,
2020 _("\
2021 Apply a command to all threads.\n\
2022 \n\
2023 Usage: thread apply all [-ascending] <command>\n\
2024 -ascending: Call <command> for all threads in ascending order.\n\
2025 The default is descending order.\
2026 "),
2027 &thread_apply_list);
2028
2029 add_cmd ("name", class_run, thread_name_command,
2030 _("Set the current thread's name.\n\
2031 Usage: thread name [NAME]\n\
2032 If NAME is not given, then any existing name is removed."), &thread_cmd_list);
2033
2034 add_cmd ("find", class_run, thread_find_command, _("\
2035 Find threads that match a regular expression.\n\
2036 Usage: thread find REGEXP\n\
2037 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2038 &thread_cmd_list);
2039
2040 add_com_alias ("t", "thread", class_run, 1);
2041
2042 add_setshow_boolean_cmd ("thread-events", no_class,
2043 &print_thread_events, _("\
2044 Set printing of thread events (such as thread start and exit)."), _("\
2045 Show printing of thread events (such as thread start and exit)."), NULL,
2046 NULL,
2047 show_print_thread_events,
2048 &setprintlist, &showprintlist);
2049
2050 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
2051
2052 observer_attach_thread_ptid_changed (restore_current_thread_ptid_changed);
2053 }