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