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