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