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