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