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