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