switch inferior/thread before calling target methods
[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
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
85102364 445/* Delete the thread referenced by THR. If SILENT, don't notify
2eab46b1
JB
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. */
f3f8ece4
PA
680static bool
681thread_alive (thread_info *tp)
c906108c 682{
30596231 683 if (tp->state == THREAD_EXITED)
f3f8ece4
PA
684 return false;
685
686 /* Ensure we're looking at the right target stack. */
687 gdb_assert (tp->inf == current_inferior ());
688
689 return target_thread_alive (tp->ptid);
690}
691
692/* Switch to thread TP if it is alive. Returns true if successfully
693 switched, false otherwise. */
694
695static bool
696switch_to_thread_if_alive (thread_info *thr)
697{
698 scoped_restore_current_thread restore_thread;
699
700 /* Switch inferior first, so that we're looking at the right target
701 stack. */
702 switch_to_inferior_no_thread (thr->inf);
703
704 if (thread_alive (thr))
705 {
706 switch_to_thread (thr);
707 restore_thread.dont_restore ();
708 return true;
709 }
710
711 return false;
c906108c
SS
712}
713
e8032dde
PA
714/* See gdbthreads.h. */
715
716void
fba45db2 717prune_threads (void)
c906108c 718{
f3f8ece4
PA
719 scoped_restore_current_thread restore_thread;
720
08036331 721 for (thread_info *tp : all_threads_safe ())
f3f8ece4
PA
722 {
723 switch_to_inferior_no_thread (tp->inf);
724
725 if (!thread_alive (tp))
726 delete_thread (tp);
727 }
c906108c
SS
728}
729
8a06aea7
PA
730/* See gdbthreads.h. */
731
732void
733delete_exited_threads (void)
734{
08036331
PA
735 for (thread_info *tp : all_threads_safe ())
736 if (tp->state == THREAD_EXITED)
737 delete_thread (tp);
8a06aea7
PA
738}
739
85102364 740/* Return true value if stack temporaries are enabled for the thread
00431a78 741 TP. */
6c659fc2 742
fdf07f3a 743bool
00431a78 744thread_stack_temporaries_enabled_p (thread_info *tp)
6c659fc2 745{
6c659fc2 746 if (tp == NULL)
fdf07f3a 747 return false;
6c659fc2
SC
748 else
749 return tp->stack_temporaries_enabled;
750}
751
752/* Push V on to the stack temporaries of the thread with id PTID. */
753
754void
00431a78 755push_thread_stack_temporary (thread_info *tp, struct value *v)
6c659fc2 756{
6c659fc2 757 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
fdf07f3a 758 tp->stack_temporaries.push_back (v);
6c659fc2
SC
759}
760
fdf07f3a 761/* Return true if VAL is among the stack temporaries of the thread
00431a78 762 TP. Return false otherwise. */
6c659fc2 763
fdf07f3a 764bool
00431a78 765value_in_thread_stack_temporaries (struct value *val, thread_info *tp)
6c659fc2 766{
6c659fc2 767 gdb_assert (tp != NULL && tp->stack_temporaries_enabled);
52941706 768 for (value *v : tp->stack_temporaries)
fdf07f3a
TT
769 if (v == val)
770 return true;
6c659fc2 771
fdf07f3a 772 return false;
6c659fc2
SC
773}
774
775/* Return the last of the stack temporaries for thread with id PTID.
776 Return NULL if there are no stack temporaries for the thread. */
777
00431a78
PA
778value *
779get_last_thread_stack_temporary (thread_info *tp)
6c659fc2
SC
780{
781 struct value *lastval = NULL;
6c659fc2
SC
782
783 gdb_assert (tp != NULL);
fdf07f3a
TT
784 if (!tp->stack_temporaries.empty ())
785 lastval = tp->stack_temporaries.back ();
6c659fc2
SC
786
787 return lastval;
788}
789
5231c1fd
PA
790void
791thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
792{
82f73884
PA
793 struct inferior *inf;
794 struct thread_info *tp;
795
796 /* It can happen that what we knew as the target inferior id
797 changes. E.g, target remote may only discover the remote process
798 pid after adding the inferior to GDB's list. */
c9657e70 799 inf = find_inferior_ptid (old_ptid);
e99b03dc 800 inf->pid = new_ptid.pid ();
82f73884 801
08036331 802 tp = find_thread_ptid (inf, old_ptid);
5231c1fd
PA
803 tp->ptid = new_ptid;
804
76727919 805 gdb::observers::thread_ptid_changed.notify (old_ptid, new_ptid);
5231c1fd
PA
806}
807
372316f1
PA
808/* See gdbthread.h. */
809
810void
811set_resumed (ptid_t ptid, int resumed)
812{
08036331
PA
813 for (thread_info *tp : all_non_exited_threads (ptid))
814 tp->resumed = resumed;
372316f1
PA
815}
816
4d9d9d04
PA
817/* Helper for set_running, that marks one thread either running or
818 stopped. */
819
820static int
821set_running_thread (struct thread_info *tp, int running)
822{
823 int started = 0;
824
825 if (running && tp->state == THREAD_STOPPED)
826 started = 1;
827 tp->state = running ? THREAD_RUNNING : THREAD_STOPPED;
828
829 if (!running)
830 {
831 /* If the thread is now marked stopped, remove it from
832 the step-over queue, so that we don't try to resume
833 it until the user wants it to. */
834 if (tp->step_over_next != NULL)
835 thread_step_over_chain_remove (tp);
836 }
837
838 return started;
839}
840
00431a78
PA
841/* See gdbthread.h. */
842
843void
844thread_info::set_running (bool running)
845{
846 if (set_running_thread (this, running))
847 gdb::observers::target_resumed.notify (this->ptid);
848}
849
e1ac3328
VP
850void
851set_running (ptid_t ptid, int running)
852{
08036331
PA
853 /* We try not to notify the observer if no thread has actually
854 changed the running state -- merely to reduce the number of
855 messages to the MI frontend. A frontend is supposed to handle
856 multiple *running notifications just fine. */
857 bool any_started = false;
e1ac3328 858
08036331
PA
859 for (thread_info *tp : all_non_exited_threads (ptid))
860 if (set_running_thread (tp, running))
861 any_started = true;
4d9d9d04 862
4d9d9d04 863 if (any_started)
76727919 864 gdb::observers::target_resumed.notify (ptid);
e1ac3328
VP
865}
866
8ea051c5 867
f2ffa92b
PA
868/* Helper for set_executing. Set's the thread's 'executing' field
869 from EXECUTING, and if EXECUTING is true also clears the thread's
870 stop_pc. */
871
872static void
873set_executing_thread (thread_info *thr, bool executing)
874{
875 thr->executing = executing;
876 if (executing)
877 thr->suspend.stop_pc = ~(CORE_ADDR) 0;
878}
879
8ea051c5
PA
880void
881set_executing (ptid_t ptid, int executing)
882{
08036331
PA
883 for (thread_info *tp : all_non_exited_threads (ptid))
884 set_executing_thread (tp, executing);
8ea051c5 885
08036331 886 /* It only takes one running thread to spawn more threads. */
b57bacec
PA
887 if (executing)
888 threads_executing = 1;
889 /* Only clear the flag if the caller is telling us everything is
890 stopped. */
9295a5a9 891 else if (minus_one_ptid == ptid)
b57bacec
PA
892 threads_executing = 0;
893}
894
895/* See gdbthread.h. */
896
897int
898threads_are_executing (void)
899{
900 return threads_executing;
8ea051c5
PA
901}
902
252fbfc8
PA
903void
904set_stop_requested (ptid_t ptid, int stop)
905{
08036331
PA
906 for (thread_info *tp : all_non_exited_threads (ptid))
907 tp->stop_requested = stop;
252fbfc8
PA
908
909 /* Call the stop requested observer so other components of GDB can
910 react to this request. */
911 if (stop)
76727919 912 gdb::observers::thread_stop_requested.notify (ptid);
252fbfc8
PA
913}
914
29f49a6a
PA
915void
916finish_thread_state (ptid_t ptid)
917{
08036331 918 bool any_started = false;
29f49a6a 919
08036331
PA
920 for (thread_info *tp : all_non_exited_threads (ptid))
921 if (set_running_thread (tp, tp->executing))
922 any_started = true;
29f49a6a
PA
923
924 if (any_started)
76727919 925 gdb::observers::target_resumed.notify (ptid);
29f49a6a
PA
926}
927
a911d87a
PA
928/* See gdbthread.h. */
929
930void
931validate_registers_access (void)
932{
933 /* No selected thread, no registers. */
9295a5a9 934 if (inferior_ptid == null_ptid)
a911d87a
PA
935 error (_("No thread selected."));
936
00431a78
PA
937 thread_info *tp = inferior_thread ();
938
a911d87a 939 /* Don't try to read from a dead thread. */
00431a78 940 if (tp->state == THREAD_EXITED)
a911d87a
PA
941 error (_("The current thread has terminated"));
942
943 /* ... or from a spinning thread. FIXME: This isn't actually fully
944 correct. It'll allow an user-requested access (e.g., "print $pc"
945 at the prompt) when a thread is not executing for some internal
946 reason, but is marked running from the user's perspective. E.g.,
947 the thread is waiting for its turn in the step-over queue. */
00431a78 948 if (tp->executing)
a911d87a
PA
949 error (_("Selected thread is running."));
950}
951
cf77c34e
MM
952/* See gdbthread.h. */
953
954bool
00431a78 955can_access_registers_thread (thread_info *thread)
cf77c34e
MM
956{
957 /* No thread, no registers. */
00431a78 958 if (thread == NULL)
cf77c34e
MM
959 return false;
960
961 /* Don't try to read from a dead thread. */
00431a78 962 if (thread->state == THREAD_EXITED)
cf77c34e
MM
963 return false;
964
965 /* ... or from a spinning thread. FIXME: see validate_registers_access. */
00431a78 966 if (thread->executing)
cf77c34e
MM
967 return false;
968
969 return true;
970}
971
ce4c476a
PA
972int
973pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
974{
975 return (pc >= thread->control.step_range_start
976 && pc < thread->control.step_range_end);
977}
978
5d5658a1
PA
979/* Helper for print_thread_info. Returns true if THR should be
980 printed. If REQUESTED_THREADS, a list of GDB ids/ranges, is not
981 NULL, only print THR if its ID is included in the list. GLOBAL_IDS
982 is true if REQUESTED_THREADS is list of global IDs, false if a list
983 of per-inferior thread ids. If PID is not -1, only print THR if it
984 is a thread from the process PID. Otherwise, threads from all
985 attached PIDs are printed. If both REQUESTED_THREADS is not NULL
986 and PID is not -1, then the thread is printed if it belongs to the
987 specified process. Otherwise, an error is raised. */
988
989static int
990should_print_thread (const char *requested_threads, int default_inf_num,
991 int global_ids, int pid, struct thread_info *thr)
992{
993 if (requested_threads != NULL && *requested_threads != '\0')
994 {
995 int in_list;
996
997 if (global_ids)
998 in_list = number_is_in_list (requested_threads, thr->global_num);
999 else
1000 in_list = tid_is_in_list (requested_threads, default_inf_num,
1001 thr->inf->num, thr->per_inf_num);
1002 if (!in_list)
1003 return 0;
1004 }
1005
e99b03dc 1006 if (pid != -1 && thr->ptid.pid () != pid)
5d5658a1
PA
1007 {
1008 if (requested_threads != NULL && *requested_threads != '\0')
1009 error (_("Requested thread not found in requested process"));
1010 return 0;
1011 }
1012
1013 if (thr->state == THREAD_EXITED)
1014 return 0;
1015
1016 return 1;
1017}
1018
75acb486
PA
1019/* Return the string to display in "info threads"'s "Target Id"
1020 column, for TP. */
1021
1022static std::string
1023thread_target_id_str (thread_info *tp)
1024{
a068643d 1025 std::string target_id = target_pid_to_str (tp->ptid);
75acb486
PA
1026 const char *extra_info = target_extra_thread_info (tp);
1027 const char *name = tp->name != nullptr ? tp->name : target_thread_name (tp);
1028
1029 if (extra_info != nullptr && name != nullptr)
a068643d
TT
1030 return string_printf ("%s \"%s\" (%s)", target_id.c_str (), name,
1031 extra_info);
75acb486 1032 else if (extra_info != nullptr)
a068643d 1033 return string_printf ("%s (%s)", target_id.c_str (), extra_info);
75acb486 1034 else if (name != nullptr)
a068643d 1035 return string_printf ("%s \"%s\"", target_id.c_str (), name);
75acb486
PA
1036 else
1037 return target_id;
1038}
1039
5d5658a1
PA
1040/* Like print_thread_info, but in addition, GLOBAL_IDS indicates
1041 whether REQUESTED_THREADS is a list of global or per-inferior
1042 thread ids. */
1043
1044static void
1d12d88f 1045print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
5d5658a1
PA
1046 int global_ids, int pid,
1047 int show_global_ids)
c906108c 1048{
5d5658a1 1049 int default_inf_num = current_inferior ()->num;
c906108c 1050
dc146f7c 1051 update_thread_list ();
00431a78
PA
1052
1053 /* Whether we saw any thread. */
1054 bool any_thread = false;
1055 /* Whether the current thread is exited. */
1056 bool current_exited = false;
1057
1058 thread_info *current_thread = (inferior_ptid != null_ptid
1059 ? inferior_thread () : NULL);
4f8d22e3 1060
f8cc3da6
TT
1061 {
1062 /* For backward compatibility, we make a list for MI. A table is
1063 preferable for the CLI, though, because it shows table
1064 headers. */
1065 gdb::optional<ui_out_emit_list> list_emitter;
1066 gdb::optional<ui_out_emit_table> table_emitter;
1067
f3f8ece4
PA
1068 /* We'll be switching threads temporarily below. */
1069 scoped_restore_current_thread restore_thread;
1070
f8cc3da6
TT
1071 if (uiout->is_mi_like_p ())
1072 list_emitter.emplace (uiout, "threads");
1073 else
1074 {
1075 int n_threads = 0;
75acb486
PA
1076 /* The width of the "Target Id" column. Grown below to
1077 accommodate the largest entry. */
1078 size_t target_id_col_width = 17;
a7658b96 1079
08036331 1080 for (thread_info *tp : all_threads ())
f8cc3da6
TT
1081 {
1082 if (!should_print_thread (requested_threads, default_inf_num,
1083 global_ids, pid, tp))
1084 continue;
a7658b96 1085
75acb486
PA
1086 if (!uiout->is_mi_like_p ())
1087 {
f3f8ece4
PA
1088 /* Switch inferiors so we're looking at the right
1089 target stack. */
1090 switch_to_inferior_no_thread (tp->inf);
1091
75acb486
PA
1092 target_id_col_width
1093 = std::max (target_id_col_width,
1094 thread_target_id_str (tp).size ());
1095 }
1096
f8cc3da6
TT
1097 ++n_threads;
1098 }
a7658b96 1099
f8cc3da6
TT
1100 if (n_threads == 0)
1101 {
1102 if (requested_threads == NULL || *requested_threads == '\0')
1103 uiout->message (_("No threads.\n"));
1104 else
1105 uiout->message (_("No threads match '%s'.\n"),
1106 requested_threads);
1107 return;
1108 }
a7658b96 1109
0d64823e 1110 table_emitter.emplace (uiout, show_global_ids ? 5 : 4,
f8cc3da6 1111 n_threads, "threads");
a7658b96 1112
f8cc3da6 1113 uiout->table_header (1, ui_left, "current", "");
0d64823e
SM
1114 uiout->table_header (4, ui_left, "id-in-tg", "Id");
1115 if (show_global_ids)
f8cc3da6 1116 uiout->table_header (4, ui_left, "id", "GId");
75acb486
PA
1117 uiout->table_header (target_id_col_width, ui_left,
1118 "target-id", "Target Id");
f8cc3da6
TT
1119 uiout->table_header (1, ui_left, "frame", "Frame");
1120 uiout->table_body ();
1121 }
a7658b96 1122
08036331
PA
1123 for (inferior *inf : all_inferiors ())
1124 for (thread_info *tp : inf->threads ())
3061113b
SM
1125 {
1126 int core;
1127
1128 any_thread = true;
1129 if (tp == current_thread && tp->state == THREAD_EXITED)
1130 current_exited = true;
1131
1132 if (!should_print_thread (requested_threads, default_inf_num,
1133 global_ids, pid, tp))
1134 continue;
1135
1136 ui_out_emit_tuple tuple_emitter (uiout, NULL);
1137
1138 if (!uiout->is_mi_like_p ())
1139 {
1140 if (tp == current_thread)
1141 uiout->field_string ("current", "*");
1142 else
1143 uiout->field_skip ("current");
1144
1145 uiout->field_string ("id-in-tg", print_thread_id (tp));
1146 }
1147
1148 if (show_global_ids || uiout->is_mi_like_p ())
1149 uiout->field_signed ("id", tp->global_num);
1150
f3f8ece4
PA
1151 /* Switch to the thread (and inferior / target). */
1152 switch_to_thread (tp);
1153
3061113b
SM
1154 /* For the CLI, we stuff everything into the target-id field.
1155 This is a gross hack to make the output come out looking
1156 correct. The underlying problem here is that ui-out has no
1157 way to specify that a field's space allocation should be
1158 shared by several fields. For MI, we do the right thing
1159 instead. */
1160
1161 if (uiout->is_mi_like_p ())
1162 {
1163 uiout->field_string ("target-id", target_pid_to_str (tp->ptid));
1164
1165 const char *extra_info = target_extra_thread_info (tp);
1166 if (extra_info != nullptr)
1167 uiout->field_string ("details", extra_info);
1168
1169 const char *name = (tp->name != nullptr
1170 ? tp->name
1171 : target_thread_name (tp));
1172 if (name != NULL)
1173 uiout->field_string ("name", name);
1174 }
1175 else
1176 {
1177 uiout->field_string ("target-id",
1178 thread_target_id_str (tp).c_str ());
1179 }
1180
1181 if (tp->state == THREAD_RUNNING)
1182 uiout->text ("(running)\n");
1183 else
1184 {
f3f8ece4 1185 /* The switch above put us at the top of the stack (leaf
3061113b 1186 frame). */
3061113b
SM
1187 print_stack_frame (get_selected_frame (NULL),
1188 /* For MI output, print frame level. */
1189 uiout->is_mi_like_p (),
1190 LOCATION, 0);
1191 }
1192
1193 if (uiout->is_mi_like_p ())
1194 {
1195 const char *state = "stopped";
1196
1197 if (tp->state == THREAD_RUNNING)
1198 state = "running";
1199 uiout->field_string ("state", state);
1200 }
1201
1202 core = target_core_of_thread (tp->ptid);
1203 if (uiout->is_mi_like_p () && core != -1)
1204 uiout->field_signed ("core", core);
1205 }
c906108c 1206
5ed8105e 1207 /* This end scope restores the current thread and the frame
f8cc3da6
TT
1208 selected before the "info threads" command, and it finishes the
1209 ui-out list or table. */
5ed8105e
PA
1210 }
1211
aea5b279 1212 if (pid == -1 && requested_threads == NULL)
8e8901c5 1213 {
00431a78 1214 if (uiout->is_mi_like_p () && inferior_ptid != null_ptid)
381befee 1215 uiout->field_signed ("current-thread-id", current_thread->global_num);
5d5658a1 1216
00431a78 1217 if (inferior_ptid != null_ptid && current_exited)
112e8700 1218 uiout->message ("\n\
5d5658a1
PA
1219The current thread <Thread ID %s> has terminated. See `help thread'.\n",
1220 print_thread_id (inferior_thread ()));
00431a78 1221 else if (any_thread && inferior_ptid == null_ptid)
112e8700 1222 uiout->message ("\n\
d729566a 1223No selected thread. See `help thread'.\n");
c906108c 1224 }
c906108c
SS
1225}
1226
5d5658a1 1227/* See gdbthread.h. */
8e8901c5 1228
5d5658a1 1229void
24c54127
TT
1230print_thread_info (struct ui_out *uiout, const char *requested_threads,
1231 int pid)
5d5658a1
PA
1232{
1233 print_thread_info_1 (uiout, requested_threads, 1, pid, 0);
1234}
1235
54d66006
PA
1236/* The options for the "info threads" command. */
1237
1238struct info_threads_opts
1239{
1240 /* For "-gid". */
491144b5 1241 bool show_global_ids = false;
54d66006
PA
1242};
1243
1244static const gdb::option::option_def info_threads_option_defs[] = {
1245
1246 gdb::option::flag_option_def<info_threads_opts> {
1247 "gid",
1248 [] (info_threads_opts *opts) { return &opts->show_global_ids; },
1249 N_("Show global thread IDs."),
1250 },
1251
1252};
1253
1254/* Create an option_def_group for the "info threads" options, with
1255 IT_OPTS as context. */
1256
1257static inline gdb::option::option_def_group
1258make_info_threads_options_def_group (info_threads_opts *it_opts)
1259{
1260 return {{info_threads_option_defs}, it_opts};
1261}
1262
5d5658a1 1263/* Implementation of the "info threads" command.
60f98dde
MS
1264
1265 Note: this has the drawback that it _really_ switches
ae0eee42
PA
1266 threads, which frees the frame cache. A no-side
1267 effects info-threads command would be nicer. */
8e8901c5
VP
1268
1269static void
1d12d88f 1270info_threads_command (const char *arg, int from_tty)
8e8901c5 1271{
54d66006
PA
1272 info_threads_opts it_opts;
1273
1274 auto grp = make_info_threads_options_def_group (&it_opts);
1275 gdb::option::process_options
1276 (&arg, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp);
1277
1278 print_thread_info_1 (current_uiout, arg, 0, -1, it_opts.show_global_ids);
1279}
1280
1281/* Completer for the "info threads" command. */
c84f6bbf 1282
54d66006
PA
1283static void
1284info_threads_command_completer (struct cmd_list_element *ignore,
1285 completion_tracker &tracker,
1286 const char *text, const char *word_ignored)
1287{
1288 const auto grp = make_info_threads_options_def_group (nullptr);
1289
1290 if (gdb::option::complete_options
1291 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR, grp))
1292 return;
1293
1294 /* Convenience to let the user know what the option can accept. */
1295 if (*text == '\0')
c84f6bbf 1296 {
54d66006
PA
1297 gdb::option::complete_on_all_options (tracker, grp);
1298 /* Keep this "ID" in sync with what "help info threads"
1299 says. */
1300 tracker.add_completion (make_unique_xstrdup ("ID"));
c84f6bbf 1301 }
8e8901c5
VP
1302}
1303
6efcd9a8
PA
1304/* See gdbthread.h. */
1305
1306void
1307switch_to_thread_no_regs (struct thread_info *thread)
1308{
3a3fd0fd 1309 struct inferior *inf = thread->inf;
6efcd9a8 1310
6efcd9a8
PA
1311 set_current_program_space (inf->pspace);
1312 set_current_inferior (inf);
1313
1314 inferior_ptid = thread->ptid;
6efcd9a8
PA
1315}
1316
00431a78 1317/* See gdbthread.h. */
c906108c 1318
00431a78 1319void
3a3fd0fd 1320switch_to_no_thread ()
c906108c 1321{
3a3fd0fd
PA
1322 if (inferior_ptid == null_ptid)
1323 return;
6c95b8df 1324
3a3fd0fd
PA
1325 inferior_ptid = null_ptid;
1326 reinit_frame_cache ();
3a3fd0fd
PA
1327}
1328
00431a78 1329/* See gdbthread.h. */
6c95b8df 1330
00431a78 1331void
3a3fd0fd
PA
1332switch_to_thread (thread_info *thr)
1333{
1334 gdb_assert (thr != NULL);
1335
1336 if (inferior_ptid == thr->ptid)
c906108c
SS
1337 return;
1338
3a3fd0fd
PA
1339 switch_to_thread_no_regs (thr);
1340
35f196d9 1341 reinit_frame_cache ();
c906108c
SS
1342}
1343
268a13a5 1344/* See gdbsupport/common-gdbthread.h. */
3a3fd0fd
PA
1345
1346void
1347switch_to_thread (ptid_t ptid)
c906108c 1348{
00431a78
PA
1349 thread_info *thr = find_thread_ptid (ptid);
1350 switch_to_thread (thr);
99b3d574
DP
1351}
1352
1353static void
4f8d22e3 1354restore_selected_frame (struct frame_id a_frame_id, int frame_level)
99b3d574 1355{
4f8d22e3
PA
1356 struct frame_info *frame = NULL;
1357 int count;
1358
eb8c0621
TT
1359 /* This means there was no selected frame. */
1360 if (frame_level == -1)
1361 {
1362 select_frame (NULL);
1363 return;
1364 }
1365
4f8d22e3
PA
1366 gdb_assert (frame_level >= 0);
1367
1368 /* Restore by level first, check if the frame id is the same as
1369 expected. If that fails, try restoring by frame id. If that
1370 fails, nothing to do, just warn the user. */
1371
1372 count = frame_level;
1373 frame = find_relative_frame (get_current_frame (), &count);
1374 if (count == 0
1375 && frame != NULL
005ca36a
JB
1376 /* The frame ids must match - either both valid or both outer_frame_id.
1377 The latter case is not failsafe, but since it's highly unlikely
4f8d22e3
PA
1378 the search by level finds the wrong frame, it's 99.9(9)% of
1379 the time (for all practical purposes) safe. */
005ca36a 1380 && frame_id_eq (get_frame_id (frame), a_frame_id))
4f8d22e3
PA
1381 {
1382 /* Cool, all is fine. */
1383 select_frame (frame);
1384 return;
1385 }
99b3d574 1386
4f8d22e3
PA
1387 frame = frame_find_by_id (a_frame_id);
1388 if (frame != NULL)
1389 {
1390 /* Cool, refound it. */
1391 select_frame (frame);
1392 return;
1393 }
99b3d574 1394
0c501536
PA
1395 /* Nothing else to do, the frame layout really changed. Select the
1396 innermost stack frame. */
1397 select_frame (get_current_frame ());
1398
1399 /* Warn the user. */
112e8700 1400 if (frame_level > 0 && !current_uiout->is_mi_like_p ())
99b3d574 1401 {
3e43a32a 1402 warning (_("Couldn't restore frame #%d in "
e0162910 1403 "current thread. Bottom (innermost) frame selected:"),
4f8d22e3
PA
1404 frame_level);
1405 /* For MI, we should probably have a notification about
1406 current frame change. But this error is not very
1407 likely, so don't bother for now. */
08d72866 1408 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
c906108c
SS
1409 }
1410}
1411
873657b9
PA
1412void
1413scoped_restore_current_thread::restore ()
6ecce94d 1414{
803bdfe4
YQ
1415 /* If an entry of thread_info was previously selected, it won't be
1416 deleted because we've increased its refcount. The thread represented
1417 by this thread_info entry may have already exited (due to normal exit,
1418 detach, etc), so the thread_info.state is THREAD_EXITED. */
5ed8105e 1419 if (m_thread != NULL
803bdfe4
YQ
1420 /* If the previously selected thread belonged to a process that has
1421 in the mean time exited (or killed, detached, etc.), then don't revert
1422 back to it, but instead simply drop back to no thread selected. */
5ed8105e
PA
1423 && m_inf->pid != 0)
1424 switch_to_thread (m_thread);
88fc996f 1425 else
6c95b8df 1426 {
3a3fd0fd 1427 switch_to_no_thread ();
5ed8105e 1428 set_current_inferior (m_inf);
6c95b8df 1429 }
94cc34af 1430
4f8d22e3
PA
1431 /* The running state of the originally selected thread may have
1432 changed, so we have to recheck it here. */
9295a5a9 1433 if (inferior_ptid != null_ptid
5ed8105e 1434 && m_was_stopped
00431a78 1435 && m_thread->state == THREAD_STOPPED
4f8d22e3
PA
1436 && target_has_registers
1437 && target_has_stack
1438 && target_has_memory)
5ed8105e 1439 restore_selected_frame (m_selected_frame_id, m_selected_frame_level);
873657b9
PA
1440}
1441
1442scoped_restore_current_thread::~scoped_restore_current_thread ()
1443{
1444 if (!m_dont_restore)
1445 {
1446 try
1447 {
1448 restore ();
1449 }
1450 catch (const gdb_exception &ex)
1451 {
1452 /* We're in a dtor, there's really nothing else we can do
1453 but swallow the exception. */
1454 }
1455 }
5d502164 1456
5ed8105e
PA
1457 if (m_thread != NULL)
1458 m_thread->decref ();
1459 m_inf->decref ();
6ecce94d
AC
1460}
1461
5ed8105e 1462scoped_restore_current_thread::scoped_restore_current_thread ()
6ecce94d 1463{
5ed8105e
PA
1464 m_thread = NULL;
1465 m_inf = current_inferior ();
4f8d22e3 1466
9295a5a9 1467 if (inferior_ptid != null_ptid)
d729566a 1468 {
00431a78 1469 thread_info *tp = inferior_thread ();
12316564
YQ
1470 struct frame_info *frame;
1471
5ed8105e
PA
1472 m_was_stopped = tp->state == THREAD_STOPPED;
1473 if (m_was_stopped
d729566a
PA
1474 && target_has_registers
1475 && target_has_stack
1476 && target_has_memory)
eb8c0621
TT
1477 {
1478 /* When processing internal events, there might not be a
1479 selected frame. If we naively call get_selected_frame
1480 here, then we can end up reading debuginfo for the
1481 current frame, but we don't generally need the debuginfo
1482 at this point. */
1483 frame = get_selected_frame_if_set ();
1484 }
d729566a
PA
1485 else
1486 frame = NULL;
4f8d22e3 1487
5ed8105e
PA
1488 m_selected_frame_id = get_frame_id (frame);
1489 m_selected_frame_level = frame_relative_level (frame);
d729566a 1490
f6223dbb 1491 tp->incref ();
5ed8105e 1492 m_thread = tp;
d729566a 1493 }
4f8d22e3 1494
5ed8105e 1495 m_inf->incref ();
6ecce94d
AC
1496}
1497
43792cf0
PA
1498/* See gdbthread.h. */
1499
f303dbd6
PA
1500int
1501show_thread_that_caused_stop (void)
1502{
1503 return highest_thread_num > 1;
1504}
1505
1506/* See gdbthread.h. */
1507
5d5658a1
PA
1508int
1509show_inferior_qualified_tids (void)
1510{
1511 return (inferior_list->next != NULL || inferior_list->num != 1);
1512}
1513
1514/* See gdbthread.h. */
1515
43792cf0
PA
1516const char *
1517print_thread_id (struct thread_info *thr)
1518{
1519 char *s = get_print_cell ();
1520
5d5658a1
PA
1521 if (show_inferior_qualified_tids ())
1522 xsnprintf (s, PRINT_CELL_SIZE, "%d.%d", thr->inf->num, thr->per_inf_num);
1523 else
1524 xsnprintf (s, PRINT_CELL_SIZE, "%d", thr->per_inf_num);
43792cf0
PA
1525 return s;
1526}
1527
6665660a
PA
1528/* Sort an array of struct thread_info pointers by thread ID (first by
1529 inferior number, and then by per-inferior thread number). Sorts in
1530 ascending order. */
253828f1 1531
6665660a
PA
1532static bool
1533tp_array_compar_ascending (const thread_info *a, const thread_info *b)
1534{
1535 if (a->inf->num != b->inf->num)
1536 return a->inf->num < b->inf->num;
1537
1538 return (a->per_inf_num < b->per_inf_num);
1539}
253828f1 1540
6665660a
PA
1541/* Sort an array of struct thread_info pointers by thread ID (first by
1542 inferior number, and then by per-inferior thread number). Sorts in
1543 descending order. */
253828f1 1544
c6609450 1545static bool
6665660a 1546tp_array_compar_descending (const thread_info *a, const thread_info *b)
253828f1 1547{
5d5658a1 1548 if (a->inf->num != b->inf->num)
6665660a 1549 return a->inf->num > b->inf->num;
253828f1 1550
6665660a 1551 return (a->per_inf_num > b->per_inf_num);
253828f1
JK
1552}
1553
1fe75df7
PW
1554/* Switch to thread THR and execute CMD.
1555 FLAGS.QUIET controls the printing of the thread information.
1556 FLAGS.CONT and FLAGS.SILENT control how to handle errors. */
1557
1558static void
1559thr_try_catch_cmd (thread_info *thr, const char *cmd, int from_tty,
1560 const qcs_flags &flags)
1561{
1562 switch_to_thread (thr);
a70b8144 1563 try
1fe75df7 1564 {
8a522c6c
PW
1565 std::string cmd_result = execute_command_to_string
1566 (cmd, from_tty, gdb_stdout->term_out ());
1fe75df7
PW
1567 if (!flags.silent || cmd_result.length () > 0)
1568 {
1569 if (!flags.quiet)
1570 printf_filtered (_("\nThread %s (%s):\n"),
1571 print_thread_id (thr),
a068643d 1572 target_pid_to_str (inferior_ptid).c_str ());
1fe75df7
PW
1573 printf_filtered ("%s", cmd_result.c_str ());
1574 }
1575 }
230d2906 1576 catch (const gdb_exception_error &ex)
1fe75df7
PW
1577 {
1578 if (!flags.silent)
1579 {
1580 if (!flags.quiet)
1581 printf_filtered (_("\nThread %s (%s):\n"),
1582 print_thread_id (thr),
a068643d 1583 target_pid_to_str (inferior_ptid).c_str ());
1fe75df7 1584 if (flags.cont)
3d6e9d23 1585 printf_filtered ("%s\n", ex.what ());
1fe75df7 1586 else
eedc3f4f 1587 throw;
1fe75df7
PW
1588 }
1589 }
1fe75df7
PW
1590}
1591
6665660a
PA
1592/* Option definition of "thread apply"'s "-ascending" option. */
1593
1594static const gdb::option::flag_option_def<> ascending_option_def = {
1595 "ascending",
1596 N_("\
1597Call COMMAND for all threads in ascending order.\n\
1598The default is descending order."),
1599};
1600
1601/* The qcs command line flags for the "thread apply" commands. Keep
1602 this in sync with the "frame apply" commands. */
1603
1604using qcs_flag_option_def
1605 = gdb::option::flag_option_def<qcs_flags>;
1606
1607static const gdb::option::option_def thr_qcs_flags_option_defs[] = {
1608 qcs_flag_option_def {
1609 "q", [] (qcs_flags *opt) { return &opt->quiet; },
1610 N_("Disables printing the thread information."),
1611 },
1612
1613 qcs_flag_option_def {
1614 "c", [] (qcs_flags *opt) { return &opt->cont; },
1615 N_("Print any error raised by COMMAND and continue."),
1616 },
1617
1618 qcs_flag_option_def {
1619 "s", [] (qcs_flags *opt) { return &opt->silent; },
1620 N_("Silently ignore any errors or empty output produced by COMMAND."),
1621 },
1622};
1623
1624/* Create an option_def_group for the "thread apply all" options, with
1625 ASCENDING and FLAGS as context. */
1626
1627static inline std::array<gdb::option::option_def_group, 2>
491144b5 1628make_thread_apply_all_options_def_group (bool *ascending,
6665660a
PA
1629 qcs_flags *flags)
1630{
1631 return {{
66eb1ed3
PA
1632 { {ascending_option_def.def ()}, ascending},
1633 { {thr_qcs_flags_option_defs}, flags },
6665660a
PA
1634 }};
1635}
1636
1637/* Create an option_def_group for the "thread apply" options, with
1638 FLAGS as context. */
1639
1640static inline gdb::option::option_def_group
1641make_thread_apply_options_def_group (qcs_flags *flags)
1642{
66eb1ed3 1643 return {{thr_qcs_flags_option_defs}, flags};
6665660a
PA
1644}
1645
c906108c 1646/* Apply a GDB command to a list of threads. List syntax is a whitespace
224608c3
PW
1647 separated list of numbers, or ranges, or the keyword `all'. Ranges consist
1648 of two numbers separated by a hyphen. Examples:
c906108c 1649
c5aa993b
JM
1650 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1651 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
224608c3 1652 thread apply all x/i $pc Apply x/i $pc cmd to all threads. */
c906108c
SS
1653
1654static void
5fed81ff 1655thread_apply_all_command (const char *cmd, int from_tty)
c906108c 1656{
491144b5 1657 bool ascending = false;
1fe75df7
PW
1658 qcs_flags flags;
1659
6665660a
PA
1660 auto group = make_thread_apply_all_options_def_group (&ascending,
1661 &flags);
1662 gdb::option::process_options
1663 (&cmd, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group);
1fe75df7 1664
6665660a 1665 validate_flags_qcs ("thread apply all", &flags);
253828f1 1666
c906108c 1667 if (cmd == NULL || *cmd == '\000')
1fe75df7 1668 error (_("Please specify a command at the end of 'thread apply all'"));
94cc34af 1669
dc146f7c 1670 update_thread_list ();
e9d196c5 1671
c6609450 1672 int tc = live_threads_count ();
a25d8bf9 1673 if (tc != 0)
054e8d9e 1674 {
c6609450
PA
1675 /* Save a copy of the thread list and increment each thread's
1676 refcount while executing the command in the context of each
1677 thread, in case the command is one that wipes threads. E.g.,
1678 detach, kill, disconnect, etc., or even normally continuing
1679 over an inferior or thread exit. */
1680 std::vector<thread_info *> thr_list_cpy;
1681 thr_list_cpy.reserve (tc);
054e8d9e 1682
08036331
PA
1683 for (thread_info *tp : all_non_exited_threads ())
1684 thr_list_cpy.push_back (tp);
1685 gdb_assert (thr_list_cpy.size () == tc);
054e8d9e 1686
c6609450
PA
1687 /* Increment the refcounts, and restore them back on scope
1688 exit. */
1689 scoped_inc_dec_ref inc_dec_ref (thr_list_cpy);
253828f1 1690
6665660a
PA
1691 auto *sorter = (ascending
1692 ? tp_array_compar_ascending
1693 : tp_array_compar_descending);
1694 std::sort (thr_list_cpy.begin (), thr_list_cpy.end (), sorter);
054e8d9e 1695
5ed8105e
PA
1696 scoped_restore_current_thread restore_thread;
1697
c6609450 1698 for (thread_info *thr : thr_list_cpy)
f3f8ece4 1699 if (switch_to_thread_if_alive (thr))
1fe75df7 1700 thr_try_catch_cmd (thr, cmd, from_tty, flags);
054e8d9e 1701 }
c906108c
SS
1702}
1703
6665660a
PA
1704/* Completer for "thread apply [ID list]". */
1705
1706static void
1707thread_apply_command_completer (cmd_list_element *ignore,
1708 completion_tracker &tracker,
1709 const char *text, const char * /*word*/)
1710{
1711 /* Don't leave this to complete_options because there's an early
1712 return below. */
1713 tracker.set_use_custom_word_point (true);
1714
1715 tid_range_parser parser;
1716 parser.init (text, current_inferior ()->num);
1717
1718 try
1719 {
1720 while (!parser.finished ())
1721 {
1722 int inf_num, thr_start, thr_end;
1723
1724 if (!parser.get_tid_range (&inf_num, &thr_start, &thr_end))
1725 break;
1726
1727 if (parser.in_star_range () || parser.in_thread_range ())
1728 parser.skip_range ();
1729 }
1730 }
1731 catch (const gdb_exception_error &ex)
1732 {
1733 /* get_tid_range throws if it parses a negative number, for
1734 example. But a seemingly negative number may be the start of
1735 an option instead. */
1736 }
1737
1738 const char *cmd = parser.cur_tok ();
1739
1740 if (cmd == text)
1741 {
1742 /* No thread ID list yet. */
1743 return;
1744 }
1745
1746 /* Check if we're past a valid thread ID list already. */
1747 if (parser.finished ()
1748 && cmd > text && !isspace (cmd[-1]))
1749 return;
1750
1751 /* We're past the thread ID list, advance word point. */
1752 tracker.advance_custom_word_point_by (cmd - text);
1753 text = cmd;
1754
1755 const auto group = make_thread_apply_options_def_group (nullptr);
1756 if (gdb::option::complete_options
1757 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
1758 return;
1759
1760 complete_nested_command_line (tracker, text);
1761}
1762
1763/* Completer for "thread apply all". */
1764
1765static void
1766thread_apply_all_command_completer (cmd_list_element *ignore,
1767 completion_tracker &tracker,
1768 const char *text, const char *word)
1769{
1770 const auto group = make_thread_apply_all_options_def_group (nullptr,
1771 nullptr);
1772 if (gdb::option::complete_options
1773 (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group))
1774 return;
1775
1776 complete_nested_command_line (tracker, text);
1777}
1778
43792cf0
PA
1779/* Implementation of the "thread apply" command. */
1780
c906108c 1781static void
981a3fb3 1782thread_apply_command (const char *tidlist, int from_tty)
c906108c 1783{
1fe75df7 1784 qcs_flags flags;
95a6b0a1 1785 const char *cmd = NULL;
bfd28288 1786 tid_range_parser parser;
c906108c
SS
1787
1788 if (tidlist == NULL || *tidlist == '\000')
8a3fe4f8 1789 error (_("Please specify a thread ID list"));
c906108c 1790
bfd28288
PA
1791 parser.init (tidlist, current_inferior ()->num);
1792 while (!parser.finished ())
3f5b7598
PA
1793 {
1794 int inf_num, thr_start, thr_end;
c906108c 1795
bfd28288 1796 if (!parser.get_tid_range (&inf_num, &thr_start, &thr_end))
b9a3f842 1797 break;
3f5b7598
PA
1798 }
1799
b9a3f842
PA
1800 cmd = parser.cur_tok ();
1801
6665660a
PA
1802 auto group = make_thread_apply_options_def_group (&flags);
1803 gdb::option::process_options
1804 (&cmd, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group);
1805
1806 validate_flags_qcs ("thread apply", &flags);
1fe75df7 1807
b9a3f842 1808 if (*cmd == '\0')
8a3fe4f8 1809 error (_("Please specify a command following the thread ID list"));
c906108c 1810
f7e13587 1811 if (tidlist == cmd || isdigit (cmd[0]))
3f5b7598
PA
1812 invalid_thread_id_error (cmd);
1813
5ed8105e 1814 scoped_restore_current_thread restore_thread;
c906108c 1815
bfd28288 1816 parser.init (tidlist, current_inferior ()->num);
b9a3f842 1817 while (!parser.finished ())
5d5658a1
PA
1818 {
1819 struct thread_info *tp = NULL;
1820 struct inferior *inf;
1821 int inf_num, thr_num;
65fc9b77 1822
bfd28288 1823 parser.get_tid (&inf_num, &thr_num);
5d5658a1
PA
1824 inf = find_inferior_id (inf_num);
1825 if (inf != NULL)
1826 tp = find_thread_id (inf, thr_num);
71ef29a8 1827
bfd28288 1828 if (parser.in_star_range ())
71ef29a8
PA
1829 {
1830 if (inf == NULL)
1831 {
1832 warning (_("Unknown inferior %d"), inf_num);
bfd28288 1833 parser.skip_range ();
71ef29a8
PA
1834 continue;
1835 }
1836
1837 /* No use looking for threads past the highest thread number
1838 the inferior ever had. */
1839 if (thr_num >= inf->highest_thread_num)
bfd28288 1840 parser.skip_range ();
71ef29a8
PA
1841
1842 /* Be quiet about unknown threads numbers. */
1843 if (tp == NULL)
1844 continue;
1845 }
1846
5d5658a1
PA
1847 if (tp == NULL)
1848 {
bfd28288 1849 if (show_inferior_qualified_tids () || parser.tid_is_qualified ())
5d5658a1
PA
1850 warning (_("Unknown thread %d.%d"), inf_num, thr_num);
1851 else
1852 warning (_("Unknown thread %d"), thr_num);
1853 continue;
1854 }
c906108c 1855
f3f8ece4 1856 if (!switch_to_thread_if_alive (tp))
65ebfb52 1857 {
5d5658a1
PA
1858 warning (_("Thread %s has terminated."), print_thread_id (tp));
1859 continue;
1860 }
4f8d22e3 1861
1fe75df7 1862 thr_try_catch_cmd (tp, cmd, from_tty, flags);
c906108c
SS
1863 }
1864}
1865
1fe75df7
PW
1866
1867/* Implementation of the "taas" command. */
1868
1869static void
1870taas_command (const char *cmd, int from_tty)
1871{
e0fad1ea
PW
1872 if (cmd == NULL || *cmd == '\0')
1873 error (_("Please specify a command to apply on all threads"));
1fe75df7
PW
1874 std::string expanded = std::string ("thread apply all -s ") + cmd;
1875 execute_command (expanded.c_str (), from_tty);
1876}
1877
1878/* Implementation of the "tfaas" command. */
1879
1880static void
1881tfaas_command (const char *cmd, int from_tty)
1882{
e0fad1ea
PW
1883 if (cmd == NULL || *cmd == '\0')
1884 error (_("Please specify a command to apply on all frames of all threads"));
1fe75df7 1885 std::string expanded
5d707134 1886 = std::string ("thread apply all -s -- frame apply all -s ") + cmd;
1fe75df7
PW
1887 execute_command (expanded.c_str (), from_tty);
1888}
1889
224608c3 1890/* Switch to the specified thread, or print the current thread. */
c906108c 1891
f0e8c4c5 1892void
981a3fb3 1893thread_command (const char *tidstr, int from_tty)
c906108c 1894{
4034d0ff 1895 if (tidstr == NULL)
c906108c 1896 {
9295a5a9 1897 if (inferior_ptid == null_ptid)
d729566a
PA
1898 error (_("No thread selected"));
1899
c906108c 1900 if (target_has_stack)
4f8d22e3 1901 {
43792cf0
PA
1902 struct thread_info *tp = inferior_thread ();
1903
00431a78 1904 if (tp->state == THREAD_EXITED)
43792cf0
PA
1905 printf_filtered (_("[Current thread is %s (%s) (exited)]\n"),
1906 print_thread_id (tp),
a068643d 1907 target_pid_to_str (inferior_ptid).c_str ());
4f8d22e3 1908 else
43792cf0
PA
1909 printf_filtered (_("[Current thread is %s (%s)]\n"),
1910 print_thread_id (tp),
a068643d 1911 target_pid_to_str (inferior_ptid).c_str ());
4f8d22e3 1912 }
c906108c 1913 else
8a3fe4f8 1914 error (_("No stack."));
c906108c 1915 }
4034d0ff
AT
1916 else
1917 {
1918 ptid_t previous_ptid = inferior_ptid;
4034d0ff 1919
65630365 1920 thread_select (tidstr, parse_thread_id (tidstr, NULL));
c5394b80 1921
ae0eee42
PA
1922 /* Print if the thread has not changed, otherwise an event will
1923 be sent. */
9295a5a9 1924 if (inferior_ptid == previous_ptid)
4034d0ff
AT
1925 {
1926 print_selected_thread_frame (current_uiout,
1927 USER_SELECTED_THREAD
1928 | USER_SELECTED_FRAME);
1929 }
1930 else
1931 {
76727919
TT
1932 gdb::observers::user_selected_context_changed.notify
1933 (USER_SELECTED_THREAD | USER_SELECTED_FRAME);
4034d0ff
AT
1934 }
1935 }
c5394b80
JM
1936}
1937
4694da01
TT
1938/* Implementation of `thread name'. */
1939
1940static void
fc41a75b 1941thread_name_command (const char *arg, int from_tty)
4694da01
TT
1942{
1943 struct thread_info *info;
1944
9295a5a9 1945 if (inferior_ptid == null_ptid)
4694da01
TT
1946 error (_("No thread selected"));
1947
529480d0 1948 arg = skip_spaces (arg);
4694da01
TT
1949
1950 info = inferior_thread ();
1951 xfree (info->name);
1952 info->name = arg ? xstrdup (arg) : NULL;
1953}
1954
60f98dde
MS
1955/* Find thread ids with a name, target pid, or extra info matching ARG. */
1956
1957static void
fc41a75b 1958thread_find_command (const char *arg, int from_tty)
60f98dde 1959{
73ede765 1960 const char *tmp;
60f98dde
MS
1961 unsigned long match = 0;
1962
1963 if (arg == NULL || *arg == '\0')
1964 error (_("Command requires an argument."));
1965
1966 tmp = re_comp (arg);
1967 if (tmp != 0)
1968 error (_("Invalid regexp (%s): %s"), tmp, arg);
1969
1970 update_thread_list ();
08036331 1971 for (thread_info *tp : all_threads ())
60f98dde
MS
1972 {
1973 if (tp->name != NULL && re_exec (tp->name))
1974 {
43792cf0
PA
1975 printf_filtered (_("Thread %s has name '%s'\n"),
1976 print_thread_id (tp), tp->name);
60f98dde
MS
1977 match++;
1978 }
1979
1980 tmp = target_thread_name (tp);
1981 if (tmp != NULL && re_exec (tmp))
1982 {
43792cf0
PA
1983 printf_filtered (_("Thread %s has target name '%s'\n"),
1984 print_thread_id (tp), tmp);
60f98dde
MS
1985 match++;
1986 }
1987
a068643d
TT
1988 std::string name = target_pid_to_str (tp->ptid);
1989 if (!name.empty () && re_exec (name.c_str ()))
60f98dde 1990 {
43792cf0 1991 printf_filtered (_("Thread %s has target id '%s'\n"),
a068643d 1992 print_thread_id (tp), name.c_str ());
60f98dde
MS
1993 match++;
1994 }
1995
1996 tmp = target_extra_thread_info (tp);
1997 if (tmp != NULL && re_exec (tmp))
1998 {
43792cf0
PA
1999 printf_filtered (_("Thread %s has extra info '%s'\n"),
2000 print_thread_id (tp), tmp);
60f98dde
MS
2001 match++;
2002 }
2003 }
2004 if (!match)
2005 printf_filtered (_("No threads match '%s'\n"), arg);
2006}
2007
93815fbf 2008/* Print notices when new threads are attached and detached. */
491144b5 2009bool print_thread_events = true;
93815fbf
VP
2010static void
2011show_print_thread_events (struct ui_file *file, int from_tty,
ae0eee42 2012 struct cmd_list_element *c, const char *value)
93815fbf 2013{
3e43a32a
MS
2014 fprintf_filtered (file,
2015 _("Printing of thread events is %s.\n"),
ae0eee42 2016 value);
93815fbf
VP
2017}
2018
65630365 2019/* See gdbthread.h. */
c906108c 2020
65630365
PA
2021void
2022thread_select (const char *tidstr, thread_info *tp)
2023{
f3f8ece4 2024 if (!switch_to_thread_if_alive (tp))
5d5658a1 2025 error (_("Thread ID %s has terminated."), tidstr);
c906108c 2026
db5a7484
NR
2027 annotate_thread_changed ();
2028
4034d0ff
AT
2029 /* Since the current thread may have changed, see if there is any
2030 exited thread we can now delete. */
2031 prune_threads ();
4034d0ff
AT
2032}
2033
2034/* Print thread and frame switch command response. */
2035
2036void
2037print_selected_thread_frame (struct ui_out *uiout,
2038 user_selected_what selection)
2039{
2040 struct thread_info *tp = inferior_thread ();
4034d0ff
AT
2041
2042 if (selection & USER_SELECTED_THREAD)
5d5658a1 2043 {
112e8700 2044 if (uiout->is_mi_like_p ())
4034d0ff 2045 {
381befee
TT
2046 uiout->field_signed ("new-thread-id",
2047 inferior_thread ()->global_num);
4034d0ff
AT
2048 }
2049 else
2050 {
112e8700
SM
2051 uiout->text ("[Switching to thread ");
2052 uiout->field_string ("new-thread-id", print_thread_id (tp));
2053 uiout->text (" (");
a068643d 2054 uiout->text (target_pid_to_str (inferior_ptid).c_str ());
112e8700 2055 uiout->text (")]");
4034d0ff 2056 }
5d5658a1 2057 }
c5394b80 2058
30596231 2059 if (tp->state == THREAD_RUNNING)
98871305 2060 {
4034d0ff 2061 if (selection & USER_SELECTED_THREAD)
112e8700 2062 uiout->text ("(running)\n");
98871305 2063 }
4034d0ff
AT
2064 else if (selection & USER_SELECTED_FRAME)
2065 {
2066 if (selection & USER_SELECTED_THREAD)
112e8700 2067 uiout->text ("\n");
4f8d22e3 2068
4034d0ff
AT
2069 if (has_stack_frames ())
2070 print_stack_frame_to_uiout (uiout, get_selected_frame (NULL),
2071 1, SRC_AND_LOC, 1);
2072 }
c5394b80
JM
2073}
2074
b57bacec
PA
2075/* Update the 'threads_executing' global based on the threads we know
2076 about right now. */
2077
2078static void
2079update_threads_executing (void)
2080{
b57bacec 2081 threads_executing = 0;
08036331 2082 for (thread_info *tp : all_non_exited_threads ())
b57bacec
PA
2083 {
2084 if (tp->executing)
2085 {
2086 threads_executing = 1;
2087 break;
2088 }
2089 }
2090}
2091
dc146f7c
VP
2092void
2093update_thread_list (void)
2094{
e8032dde 2095 target_update_thread_list ();
b57bacec 2096 update_threads_executing ();
dc146f7c
VP
2097}
2098
663f6d42
PA
2099/* Return a new value for the selected thread's id. Return a value of
2100 0 if no thread is selected. If GLOBAL is true, return the thread's
2101 global number. Otherwise return the per-inferior number. */
2102
2103static struct value *
2104thread_num_make_value_helper (struct gdbarch *gdbarch, int global)
2105{
663f6d42
PA
2106 int int_val;
2107
00431a78 2108 if (inferior_ptid == null_ptid)
663f6d42 2109 int_val = 0;
663f6d42 2110 else
00431a78
PA
2111 {
2112 thread_info *tp = inferior_thread ();
2113 if (global)
2114 int_val = tp->global_num;
2115 else
2116 int_val = tp->per_inf_num;
2117 }
663f6d42
PA
2118
2119 return value_from_longest (builtin_type (gdbarch)->builtin_int, int_val);
2120}
2121
5d5658a1
PA
2122/* Return a new value for the selected thread's per-inferior thread
2123 number. Return a value of 0 if no thread is selected, or no
2124 threads exist. */
6aed2dbc
SS
2125
2126static struct value *
ae0eee42
PA
2127thread_id_per_inf_num_make_value (struct gdbarch *gdbarch,
2128 struct internalvar *var,
663f6d42 2129 void *ignore)
6aed2dbc 2130{
663f6d42
PA
2131 return thread_num_make_value_helper (gdbarch, 0);
2132}
2133
2134/* Return a new value for the selected thread's global id. Return a
2135 value of 0 if no thread is selected, or no threads exist. */
6aed2dbc 2136
663f6d42
PA
2137static struct value *
2138global_thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
2139 void *ignore)
2140{
2141 return thread_num_make_value_helper (gdbarch, 1);
6aed2dbc
SS
2142}
2143
c906108c
SS
2144/* Commands with a prefix of `thread'. */
2145struct cmd_list_element *thread_cmd_list = NULL;
2146
22d2b532
SDJ
2147/* Implementation of `thread' variable. */
2148
2149static const struct internalvar_funcs thread_funcs =
2150{
663f6d42
PA
2151 thread_id_per_inf_num_make_value,
2152 NULL,
2153 NULL
2154};
2155
2156/* Implementation of `gthread' variable. */
2157
2158static const struct internalvar_funcs gthread_funcs =
2159{
2160 global_thread_id_make_value,
22d2b532
SDJ
2161 NULL,
2162 NULL
2163};
2164
c906108c 2165void
fba45db2 2166_initialize_thread (void)
c906108c
SS
2167{
2168 static struct cmd_list_element *thread_apply_list = NULL;
5d707134 2169 cmd_list_element *c;
c906108c 2170
54d66006
PA
2171 const auto info_threads_opts = make_info_threads_options_def_group (nullptr);
2172
2173 /* Note: keep this "ID" in sync with what "info threads [TAB]"
2174 suggests. */
2175 static std::string info_threads_help
2176 = gdb::option::build_help (_("\
2177Display currently known threads.\n\
2178Usage: info threads [OPTION]... [ID]...\n\
2179\n\
2180Options:\n\
2181%OPTIONS%\
5d5658a1 2182If ID is given, it is a space-separated list of IDs of threads to display.\n\
54d66006
PA
2183Otherwise, all threads are displayed."),
2184 info_threads_opts);
2185
2186 c = add_info ("threads", info_threads_command, info_threads_help.c_str ());
2187 set_cmd_completer_handle_brkchars (c, info_threads_command_completer);
c906108c 2188
d729566a 2189 add_prefix_cmd ("thread", class_run, thread_command, _("\
1bedd215
AC
2190Use this command to switch between threads.\n\
2191The new thread ID must be currently known."),
d729566a 2192 &thread_cmd_list, "thread ", 1, &cmdlist);
c906108c 2193
6665660a 2194#define THREAD_APPLY_OPTION_HELP "\
1fe75df7
PW
2195Prints per-inferior thread number and target system's thread id\n\
2196followed by COMMAND output.\n\
6665660a
PA
2197\n\
2198By default, an error raised during the execution of COMMAND\n\
2199aborts \"thread apply\".\n\
2200\n\
2201Options:\n\
2202%OPTIONS%"
2203
2204 const auto thread_apply_opts = make_thread_apply_options_def_group (nullptr);
2205
8abfcabc 2206 static std::string thread_apply_help = gdb::option::build_help (_("\
6665660a
PA
2207Apply a command to a list of threads.\n\
2208Usage: thread apply ID... [OPTION]... COMMAND\n\
1fe75df7 2209ID is a space-separated list of IDs of threads to apply COMMAND on.\n"
6665660a
PA
2210THREAD_APPLY_OPTION_HELP),
2211 thread_apply_opts);
2212
2213 c = add_prefix_cmd ("apply", class_run, thread_apply_command,
2214 thread_apply_help.c_str (),
2215 &thread_apply_list, "thread apply ", 1,
2216 &thread_cmd_list);
2217 set_cmd_completer_handle_brkchars (c, thread_apply_command_completer);
c906108c 2218
6665660a
PA
2219 const auto thread_apply_all_opts
2220 = make_thread_apply_all_options_def_group (nullptr, nullptr);
2221
8abfcabc 2222 static std::string thread_apply_all_help = gdb::option::build_help (_("\
253828f1
JK
2223Apply a command to all threads.\n\
2224\n\
6665660a
PA
2225Usage: thread apply all [OPTION]... COMMAND\n"
2226THREAD_APPLY_OPTION_HELP),
2227 thread_apply_all_opts);
2228
2229 c = add_cmd ("all", class_run, thread_apply_all_command,
2230 thread_apply_all_help.c_str (),
2231 &thread_apply_list);
2232 set_cmd_completer_handle_brkchars (c, thread_apply_all_command_completer);
c906108c 2233
6665660a 2234 c = add_com ("taas", class_run, taas_command, _("\
1fe75df7 2235Apply a command to all threads (ignoring errors and empty output).\n\
6665660a
PA
2236Usage: taas [OPTION]... COMMAND\n\
2237shortcut for 'thread apply all -s [OPTION]... COMMAND'\n\
2238See \"help thread apply all\" for available options."));
2239 set_cmd_completer_handle_brkchars (c, thread_apply_all_command_completer);
1fe75df7 2240
5d707134 2241 c = add_com ("tfaas", class_run, tfaas_command, _("\
1fe75df7 2242Apply a command to all frames of all threads (ignoring errors and empty output).\n\
5d707134
PA
2243Usage: tfaas [OPTION]... COMMAND\n\
2244shortcut for 'thread apply all -s -- frame apply all -s [OPTION]... COMMAND'\n\
2245See \"help frame apply all\" for available options."));
2246 set_cmd_completer_handle_brkchars (c, frame_apply_all_cmd_completer);
1fe75df7 2247
4694da01
TT
2248 add_cmd ("name", class_run, thread_name_command,
2249 _("Set the current thread's name.\n\
2250Usage: thread name [NAME]\n\
2251If NAME is not given, then any existing name is removed."), &thread_cmd_list);
2252
60f98dde
MS
2253 add_cmd ("find", class_run, thread_find_command, _("\
2254Find threads that match a regular expression.\n\
2255Usage: thread find REGEXP\n\
2256Will display thread ids whose name, target ID, or extra info matches REGEXP."),
2257 &thread_cmd_list);
2258
4f45d445 2259 add_com_alias ("t", "thread", class_run, 1);
93815fbf
VP
2260
2261 add_setshow_boolean_cmd ("thread-events", no_class,
ae0eee42 2262 &print_thread_events, _("\
11c68c47
EZ
2263Set printing of thread events (such as thread start and exit)."), _("\
2264Show printing of thread events (such as thread start and exit)."), NULL,
ae0eee42
PA
2265 NULL,
2266 show_print_thread_events,
2267 &setprintlist, &showprintlist);
6aed2dbc 2268
22d2b532 2269 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
663f6d42 2270 create_internalvar_type_lazy ("_gthread", &gthread_funcs, NULL);
c906108c 2271}
This page took 2.263026 seconds and 4 git commands to generate.