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