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