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