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