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