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