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