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