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