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