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