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