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