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