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