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