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