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