]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/thread.c
* linux-low.c (handle_extended_wait): Use linux_resume_one_lwp
[thirdparty/binutils-gdb.git] / gdb / thread.c
CommitLineData
c906108c 1/* Multi-process/thread control for GDB, the GNU debugger.
8926118c 2
6aba47ca 3 Copyright (C) 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
0fb0cc75
JB
4 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009
5 Free Software Foundation, Inc.
8926118c 6
b6ba6518 7 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
c906108c 8
c5aa993b 9 This file is part of GDB.
c906108c 10
c5aa993b
JM
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
a9762ec7 13 the Free Software Foundation; either version 3 of the License, or
c5aa993b 14 (at your option) any later version.
c906108c 15
c5aa993b
JM
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
c906108c 20
c5aa993b 21 You should have received a copy of the GNU General Public License
a9762ec7 22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
23
24#include "defs.h"
25#include "symtab.h"
26#include "frame.h"
27#include "inferior.h"
28#include "environ.h"
29#include "value.h"
30#include "target.h"
31#include "gdbthread.h"
60250e8b 32#include "exceptions.h"
c906108c
SS
33#include "command.h"
34#include "gdbcmd.h"
4e052eda 35#include "regcache.h"
5b7f31a4 36#include "gdb.h"
b66d6d2e 37#include "gdb_string.h"
c906108c
SS
38
39#include <ctype.h>
40#include <sys/types.h>
41#include <signal.h>
8b93c638 42#include "ui-out.h"
683f2885 43#include "observer.h"
d4fc5b1e 44#include "annotate.h"
94cc34af
PA
45#include "cli/cli-decode.h"
46
0d06e24b 47/* Definition of struct thread_info exported to gdbthread.h */
c906108c
SS
48
49/* Prototypes for exported functions. */
50
a14ed312 51void _initialize_thread (void);
c906108c
SS
52
53/* Prototypes for local functions. */
54
c906108c
SS
55static struct thread_info *thread_list = NULL;
56static int highest_thread_num;
57
a14ed312
KB
58static void thread_command (char *tidstr, int from_tty);
59static void thread_apply_all_command (char *, int);
60static int thread_alive (struct thread_info *);
61static void info_threads_command (char *, int);
62static void thread_apply_command (char *, int);
39f77062 63static void restore_current_thread (ptid_t);
a14ed312 64static void prune_threads (void);
c906108c 65
4f8d22e3
PA
66/* Frontend view of the thread state. Possible extensions: stepping,
67 finishing, until(ling),... */
68enum thread_state
69{
70 THREAD_STOPPED,
71 THREAD_RUNNING,
72 THREAD_EXITED,
73};
74
a5321aa4 75struct thread_info*
4e1c45ea 76inferior_thread (void)
8601f500 77{
4e1c45ea
PA
78 struct thread_info *tp = find_thread_pid (inferior_ptid);
79 gdb_assert (tp);
80 return tp;
81}
8601f500 82
4e1c45ea
PA
83void
84delete_step_resume_breakpoint (struct thread_info *tp)
85{
86 if (tp && tp->step_resume_breakpoint)
8601f500 87 {
4e1c45ea
PA
88 delete_breakpoint (tp->step_resume_breakpoint);
89 tp->step_resume_breakpoint = NULL;
8601f500
MS
90 }
91}
92
7c952b6d 93static void
4f8d22e3 94clear_thread_inferior_resources (struct thread_info *tp)
7c952b6d
ND
95{
96 /* NOTE: this will take care of any left-over step_resume breakpoints,
4d8453a5
DJ
97 but not any user-specified thread-specific breakpoints. We can not
98 delete the breakpoint straight-off, because the inferior might not
99 be stopped at the moment. */
7c952b6d 100 if (tp->step_resume_breakpoint)
4f8d22e3
PA
101 {
102 tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
103 tp->step_resume_breakpoint = NULL;
104 }
7c952b6d 105
a474d7c2 106 bpstat_clear (&tp->stop_bpstat);
95e54da7
PA
107
108 discard_all_intermediate_continuations_thread (tp);
109 discard_all_continuations_thread (tp);
4f8d22e3
PA
110}
111
112static void
113free_thread (struct thread_info *tp)
114{
115 clear_thread_inferior_resources (tp);
a474d7c2 116
7c952b6d
ND
117 /* FIXME: do I ever need to call the back-end to give it a
118 chance at this private data before deleting the thread? */
119 if (tp->private)
b8c9b27d 120 xfree (tp->private);
7c952b6d 121
b8c9b27d 122 xfree (tp);
7c952b6d
ND
123}
124
c906108c 125void
fba45db2 126init_thread_list (void)
c906108c
SS
127{
128 struct thread_info *tp, *tpnext;
129
7c952b6d 130 highest_thread_num = 0;
8ea051c5 131
c906108c
SS
132 if (!thread_list)
133 return;
134
135 for (tp = thread_list; tp; tp = tpnext)
136 {
137 tpnext = tp->next;
7c952b6d 138 free_thread (tp);
c906108c
SS
139 }
140
141 thread_list = NULL;
c906108c
SS
142}
143
0d06e24b 144struct thread_info *
93815fbf 145add_thread_silent (ptid_t ptid)
c906108c
SS
146{
147 struct thread_info *tp;
148
4f8d22e3
PA
149 tp = find_thread_pid (ptid);
150 if (tp)
151 /* Found an old thread with the same id. It has to be dead,
152 otherwise we wouldn't be adding a new thread with the same id.
153 The OS is reusing this id --- delete it, and recreate a new
154 one. */
155 {
156 /* In addition to deleting the thread, if this is the current
dcf4fbde
PA
157 thread, then we need to take care that delete_thread doesn't
158 really delete the thread if it is inferior_ptid. Create a
159 new template thread in the list with an invalid ptid, switch
160 to it, delete the original thread, reset the new thread's
161 ptid, and switch to it. */
4f8d22e3
PA
162
163 if (ptid_equal (inferior_ptid, ptid))
164 {
165 tp = xmalloc (sizeof (*tp));
166 memset (tp, 0, sizeof (*tp));
167 tp->ptid = minus_one_ptid;
168 tp->num = ++highest_thread_num;
169 tp->next = thread_list;
170 thread_list = tp;
dcf4fbde
PA
171
172 /* Make switch_to_thread not read from the thread. */
173 tp->state_ = THREAD_EXITED;
174 switch_to_thread (minus_one_ptid);
4f8d22e3
PA
175
176 /* Now we can delete it. */
177 delete_thread (ptid);
178
dcf4fbde 179 /* Now reset its ptid, and reswitch inferior_ptid to it. */
4f8d22e3 180 tp->ptid = ptid;
dcf4fbde 181 tp->state_ = THREAD_STOPPED;
4f8d22e3
PA
182 switch_to_thread (ptid);
183
184 observer_notify_new_thread (tp);
185
186 /* All done. */
187 return tp;
188 }
189 else
190 /* Just go ahead and delete it. */
191 delete_thread (ptid);
192 }
193
6c0d3f6a
MS
194 tp = (struct thread_info *) xmalloc (sizeof (*tp));
195 memset (tp, 0, sizeof (*tp));
39f77062 196 tp->ptid = ptid;
c906108c 197 tp->num = ++highest_thread_num;
c906108c
SS
198 tp->next = thread_list;
199 thread_list = tp;
cfc01461
VP
200
201 observer_notify_new_thread (tp);
202
0d06e24b 203 return tp;
c906108c
SS
204}
205
93815fbf 206struct thread_info *
17faa917 207add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
93815fbf
VP
208{
209 struct thread_info *result = add_thread_silent (ptid);
210
17faa917
DJ
211 result->private = private;
212
93815fbf 213 if (print_thread_events)
fd532e2e 214 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
d4fc5b1e
NR
215
216 annotate_new_thread ();
93815fbf
VP
217 return result;
218}
219
17faa917
DJ
220struct thread_info *
221add_thread (ptid_t ptid)
222{
223 return add_thread_with_info (ptid, NULL);
224}
225
5e0b29c1
PA
226/* Delete thread PTID. If SILENT, don't notify the observer of this
227 exit. */
228static void
229delete_thread_1 (ptid_t ptid, int silent)
c906108c
SS
230{
231 struct thread_info *tp, *tpprev;
232
233 tpprev = NULL;
234
235 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
39f77062 236 if (ptid_equal (tp->ptid, ptid))
c906108c
SS
237 break;
238
239 if (!tp)
240 return;
241
4f8d22e3
PA
242 /* If this is the current thread, or there's code out there that
243 relies on it existing (refcount > 0) we can't delete yet. Mark
244 it as exited, and notify it. */
245 if (tp->refcount > 0
246 || ptid_equal (tp->ptid, inferior_ptid))
247 {
248 if (tp->state_ != THREAD_EXITED)
249 {
a07daef3 250 observer_notify_thread_exit (tp, silent);
4f8d22e3
PA
251
252 /* Tag it as exited. */
253 tp->state_ = THREAD_EXITED;
254
255 /* Clear breakpoints, etc. associated with this thread. */
256 clear_thread_inferior_resources (tp);
257 }
258
259 /* Will be really deleted some other time. */
260 return;
261 }
262
c906108c
SS
263 if (tpprev)
264 tpprev->next = tp->next;
265 else
266 thread_list = tp->next;
267
4f8d22e3 268 /* Notify thread exit, but only if we haven't already. */
a07daef3
PA
269 if (tp->state_ != THREAD_EXITED)
270 observer_notify_thread_exit (tp, silent);
063bfe2e 271
7c952b6d 272 free_thread (tp);
c906108c
SS
273}
274
4f8d22e3
PA
275/* Delete thread PTID and notify of thread exit. If this is
276 inferior_ptid, don't actually delete it, but tag it as exited and
277 do the notification. If PTID is the user selected thread, clear
278 it. */
5e0b29c1
PA
279void
280delete_thread (ptid_t ptid)
281{
282 delete_thread_1 (ptid, 0 /* not silent */);
283}
284
285void
286delete_thread_silent (ptid_t ptid)
287{
288 delete_thread_1 (ptid, 1 /* silent */);
289}
290
1e92afda 291struct thread_info *
fba45db2 292find_thread_id (int num)
c906108c
SS
293{
294 struct thread_info *tp;
295
296 for (tp = thread_list; tp; tp = tp->next)
297 if (tp->num == num)
298 return tp;
299
300 return NULL;
301}
302
39f77062 303/* Find a thread_info by matching PTID. */
0d06e24b 304struct thread_info *
39f77062 305find_thread_pid (ptid_t ptid)
0d06e24b
JM
306{
307 struct thread_info *tp;
308
309 for (tp = thread_list; tp; tp = tp->next)
39f77062 310 if (ptid_equal (tp->ptid, ptid))
0d06e24b
JM
311 return tp;
312
313 return NULL;
314}
315
316/*
317 * Thread iterator function.
318 *
319 * Calls a callback function once for each thread, so long as
320 * the callback function returns false. If the callback function
321 * returns true, the iteration will end and the current thread
322 * will be returned. This can be useful for implementing a
323 * search for a thread with arbitrary attributes, or for applying
324 * some operation to every thread.
325 *
326 * FIXME: some of the existing functionality, such as
327 * "Thread apply all", might be rewritten using this functionality.
328 */
329
330struct thread_info *
fd118b61
KB
331iterate_over_threads (int (*callback) (struct thread_info *, void *),
332 void *data)
0d06e24b 333{
4f8d22e3 334 struct thread_info *tp, *next;
0d06e24b 335
4f8d22e3
PA
336 for (tp = thread_list; tp; tp = next)
337 {
338 next = tp->next;
339 if ((*callback) (tp, data))
340 return tp;
341 }
0d06e24b
JM
342
343 return NULL;
344}
345
20874c92
VP
346int
347thread_count (void)
348{
349 int result = 0;
350 struct thread_info *tp;
351
352 for (tp = thread_list; tp; tp = tp->next)
353 ++result;
354
355 return result;
356}
357
c906108c 358int
fba45db2 359valid_thread_id (int num)
c906108c
SS
360{
361 struct thread_info *tp;
362
363 for (tp = thread_list; tp; tp = tp->next)
364 if (tp->num == num)
365 return 1;
366
367 return 0;
368}
369
370int
39f77062 371pid_to_thread_id (ptid_t ptid)
c906108c
SS
372{
373 struct thread_info *tp;
374
375 for (tp = thread_list; tp; tp = tp->next)
39f77062 376 if (ptid_equal (tp->ptid, ptid))
c906108c
SS
377 return tp->num;
378
379 return 0;
380}
381
39f77062 382ptid_t
fba45db2 383thread_id_to_pid (int num)
c906108c
SS
384{
385 struct thread_info *thread = find_thread_id (num);
386 if (thread)
39f77062 387 return thread->ptid;
c906108c 388 else
39f77062 389 return pid_to_ptid (-1);
c906108c
SS
390}
391
392int
39f77062 393in_thread_list (ptid_t ptid)
c906108c
SS
394{
395 struct thread_info *tp;
396
397 for (tp = thread_list; tp; tp = tp->next)
39f77062 398 if (ptid_equal (tp->ptid, ptid))
c906108c
SS
399 return 1;
400
401 return 0; /* Never heard of 'im */
402}
8926118c 403
bad34192
PA
404/* Finds the first thread of the inferior given by PID. If PID is -1,
405 return the first thread in the list. */
406
407struct thread_info *
408first_thread_of_process (int pid)
409{
410 struct thread_info *tp, *ret = NULL;
411
412 for (tp = thread_list; tp; tp = tp->next)
413 if (pid == -1 || ptid_get_pid (tp->ptid) == pid)
414 if (ret == NULL || tp->num < ret->num)
415 ret = tp;
416
417 return ret;
418}
419
8b93c638
JM
420/* Print a list of thread ids currently known, and the total number of
421 threads. To be used from within catch_errors. */
6949171e
JJ
422static int
423do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
8b93c638
JM
424{
425 struct thread_info *tp;
426 int num = 0;
3b31d625 427 struct cleanup *cleanup_chain;
592375cd 428 int current_thread = -1;
8b93c638 429
7990a578
EZ
430 prune_threads ();
431 target_find_new_threads ();
432
3b31d625 433 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
8b93c638
JM
434
435 for (tp = thread_list; tp; tp = tp->next)
436 {
4f8d22e3
PA
437 if (tp->state_ == THREAD_EXITED)
438 continue;
592375cd
VP
439
440 if (ptid_equal (tp->ptid, inferior_ptid))
441 current_thread = tp->num;
442
8b93c638
JM
443 num++;
444 ui_out_field_int (uiout, "thread-id", tp->num);
445 }
446
3b31d625 447 do_cleanups (cleanup_chain);
592375cd
VP
448
449 if (current_thread != -1)
450 ui_out_field_int (uiout, "current-thread-id", current_thread);
8b93c638
JM
451 ui_out_field_int (uiout, "number-of-threads", num);
452 return GDB_RC_OK;
453}
454
455/* Official gdblib interface function to get a list of thread ids and
456 the total number. */
457enum gdb_rc
ce43223b 458gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
8b93c638 459{
b0b13bb4
DJ
460 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
461 error_message, RETURN_MASK_ALL) < 0)
462 return GDB_RC_FAIL;
463 return GDB_RC_OK;
8b93c638 464}
c906108c 465
c906108c
SS
466/* Return true if TP is an active thread. */
467static int
fba45db2 468thread_alive (struct thread_info *tp)
c906108c 469{
4f8d22e3 470 if (tp->state_ == THREAD_EXITED)
c906108c 471 return 0;
39f77062 472 if (!target_thread_alive (tp->ptid))
4f8d22e3 473 return 0;
c906108c
SS
474 return 1;
475}
476
477static void
fba45db2 478prune_threads (void)
c906108c 479{
d4f3574e 480 struct thread_info *tp, *next;
c906108c 481
c906108c
SS
482 for (tp = thread_list; tp; tp = next)
483 {
484 next = tp->next;
485 if (!thread_alive (tp))
39f77062 486 delete_thread (tp->ptid);
c906108c
SS
487 }
488}
489
5231c1fd
PA
490void
491thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
492{
82f73884
PA
493 struct inferior *inf;
494 struct thread_info *tp;
495
496 /* It can happen that what we knew as the target inferior id
497 changes. E.g, target remote may only discover the remote process
498 pid after adding the inferior to GDB's list. */
499 inf = find_inferior_pid (ptid_get_pid (old_ptid));
500 inf->pid = ptid_get_pid (new_ptid);
501
502 tp = find_thread_pid (old_ptid);
5231c1fd
PA
503 tp->ptid = new_ptid;
504
505 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
506}
507
e1ac3328
VP
508void
509set_running (ptid_t ptid, int running)
510{
511 struct thread_info *tp;
d90e17a7 512 int all = ptid_equal (ptid, minus_one_ptid);
e1ac3328 513
e1ac3328
VP
514 /* We try not to notify the observer if no thread has actually changed
515 the running state -- merely to reduce the number of messages to
516 frontend. Frontend is supposed to handle multiple *running just fine. */
d90e17a7 517 if (all || ptid_is_pid (ptid))
e1ac3328
VP
518 {
519 int any_started = 0;
520 for (tp = thread_list; tp; tp = tp->next)
d90e17a7
PA
521 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
522 {
523 if (tp->state_ == THREAD_EXITED)
524 continue;
525 if (running && tp->state_ == THREAD_STOPPED)
526 any_started = 1;
527 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
528 }
c5a4d20b
PA
529 if (any_started)
530 observer_notify_target_resumed (ptid);
e1ac3328
VP
531 }
532 else
533 {
4f8d22e3 534 int started = 0;
e1ac3328
VP
535 tp = find_thread_pid (ptid);
536 gdb_assert (tp);
4f8d22e3
PA
537 gdb_assert (tp->state_ != THREAD_EXITED);
538 if (running && tp->state_ == THREAD_STOPPED)
539 started = 1;
540 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
c5a4d20b 541 if (started)
4f8d22e3
PA
542 observer_notify_target_resumed (ptid);
543 }
e1ac3328
VP
544}
545
4f8d22e3
PA
546static int
547is_thread_state (ptid_t ptid, enum thread_state state)
e1ac3328
VP
548{
549 struct thread_info *tp;
550
8ea051c5
PA
551 if (!target_has_execution)
552 return 0;
553
e1ac3328
VP
554 tp = find_thread_pid (ptid);
555 gdb_assert (tp);
4f8d22e3
PA
556 return tp->state_ == state;
557}
558
559int
560is_stopped (ptid_t ptid)
561{
562 /* Without execution, this property is always true. */
563 if (!target_has_execution)
564 return 1;
565
566 return is_thread_state (ptid, THREAD_STOPPED);
567}
568
569int
570is_exited (ptid_t ptid)
571{
572 /* Without execution, this property is always false. */
573 if (!target_has_execution)
574 return 0;
575
576 return is_thread_state (ptid, THREAD_EXITED);
577}
578
579int
580is_running (ptid_t ptid)
581{
582 /* Without execution, this property is always false. */
583 if (!target_has_execution)
584 return 0;
585
586 return is_thread_state (ptid, THREAD_RUNNING);
e1ac3328
VP
587}
588
8ea051c5
PA
589int
590any_running (void)
591{
592 struct thread_info *tp;
593
594 if (!target_has_execution)
595 return 0;
596
8ea051c5 597 for (tp = thread_list; tp; tp = tp->next)
4f8d22e3 598 if (tp->state_ == THREAD_RUNNING)
8ea051c5
PA
599 return 1;
600
601 return 0;
602}
603
604int
605is_executing (ptid_t ptid)
606{
607 struct thread_info *tp;
608
609 if (!target_has_execution)
610 return 0;
611
8ea051c5
PA
612 tp = find_thread_pid (ptid);
613 gdb_assert (tp);
614 return tp->executing_;
615}
616
617void
618set_executing (ptid_t ptid, int executing)
619{
620 struct thread_info *tp;
d90e17a7 621 int all = ptid_equal (ptid, minus_one_ptid);
8ea051c5 622
d90e17a7 623 if (all || ptid_is_pid (ptid))
8ea051c5
PA
624 {
625 for (tp = thread_list; tp; tp = tp->next)
d90e17a7
PA
626 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
627 tp->executing_ = executing;
8ea051c5
PA
628 }
629 else
630 {
631 tp = find_thread_pid (ptid);
632 gdb_assert (tp);
633 tp->executing_ = executing;
634 }
635}
636
252fbfc8
PA
637void
638set_stop_requested (ptid_t ptid, int stop)
639{
640 struct thread_info *tp;
641 int all = ptid_equal (ptid, minus_one_ptid);
642
643 if (all || ptid_is_pid (ptid))
644 {
645 for (tp = thread_list; tp; tp = tp->next)
646 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
647 tp->stop_requested = stop;
648 }
649 else
650 {
651 tp = find_thread_pid (ptid);
652 gdb_assert (tp);
653 tp->stop_requested = stop;
654 }
655
656 /* Call the stop requested observer so other components of GDB can
657 react to this request. */
658 if (stop)
659 observer_notify_thread_stop_requested (ptid);
660}
661
29f49a6a
PA
662void
663finish_thread_state (ptid_t ptid)
664{
665 struct thread_info *tp;
666 int all;
667 int any_started = 0;
668
669 all = ptid_equal (ptid, minus_one_ptid);
670
671 if (all || ptid_is_pid (ptid))
672 {
673 for (tp = thread_list; tp; tp = tp->next)
674 {
675 if (tp->state_ == THREAD_EXITED)
676 continue;
677 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
678 {
679 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
680 any_started = 1;
681 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
682 }
683 }
684 }
685 else
686 {
687 tp = find_thread_pid (ptid);
688 gdb_assert (tp);
689 if (tp->state_ != THREAD_EXITED)
690 {
691 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
692 any_started = 1;
693 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
694 }
695 }
696
697 if (any_started)
698 observer_notify_target_resumed (ptid);
699}
700
701void
702finish_thread_state_cleanup (void *arg)
703{
704 ptid_t *ptid_p = arg;
705
706 gdb_assert (arg);
707
708 finish_thread_state (*ptid_p);
709}
710
8e8901c5
VP
711/* Prints the list of threads and their details on UIOUT.
712 This is a version of 'info_thread_command' suitable for
713 use from MI.
4f8d22e3 714 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
8e8901c5 715 that should be printed. Otherwise, all threads are
3ee1c036
VP
716 printed.
717 If PID is not -1, only print threads from the process PID.
718 Otherwise, threads from all attached PIDs are printed.
719 If both REQUESTED_THREAD and PID are not -1, then the thread
720 is printed if it belongs to the specified process. Otherwise,
721 an error is raised. */
8e8901c5 722void
3ee1c036 723print_thread_info (struct ui_out *uiout, int requested_thread, int pid)
c906108c
SS
724{
725 struct thread_info *tp;
39f77062 726 ptid_t current_ptid;
99b3d574 727 struct cleanup *old_chain;
0d06e24b 728 char *extra_info;
8e8901c5 729 int current_thread = -1;
c906108c 730
c906108c 731 prune_threads ();
b83266a0 732 target_find_new_threads ();
39f77062 733 current_ptid = inferior_ptid;
4f8d22e3
PA
734
735 /* We'll be switching threads temporarily. */
736 old_chain = make_cleanup_restore_current_thread ();
737
738 make_cleanup_ui_out_list_begin_end (uiout, "threads");
c906108c
SS
739 for (tp = thread_list; tp; tp = tp->next)
740 {
8e8901c5
VP
741 struct cleanup *chain2;
742
743 if (requested_thread != -1 && tp->num != requested_thread)
744 continue;
745
3ee1c036
VP
746 if (pid != -1 && PIDGET (tp->ptid) != pid)
747 {
748 if (requested_thread != -1)
749 error (_("Requested thread not found in requested process"));
750 continue;
751 }
752
4f8d22e3
PA
753 if (ptid_equal (tp->ptid, current_ptid))
754 current_thread = tp->num;
755
756 if (tp->state_ == THREAD_EXITED)
757 continue;
758
8e8901c5
VP
759 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
760
39f77062 761 if (ptid_equal (tp->ptid, current_ptid))
4f8d22e3 762 ui_out_text (uiout, "* ");
c906108c 763 else
8e8901c5 764 ui_out_text (uiout, " ");
c906108c 765
8e8901c5
VP
766 ui_out_field_int (uiout, "id", tp->num);
767 ui_out_text (uiout, " ");
54ba13f7 768 ui_out_field_string (uiout, "target-id", target_pid_to_str (tp->ptid));
0d06e24b 769
ed406532
VP
770 extra_info = target_extra_thread_info (tp);
771 if (extra_info)
8e8901c5 772 {
ed406532
VP
773 ui_out_text (uiout, " (");
774 ui_out_field_string (uiout, "details", extra_info);
775 ui_out_text (uiout, ")");
8e8901c5 776 }
ed406532 777 ui_out_text (uiout, " ");
4f8d22e3
PA
778
779 if (tp->state_ == THREAD_RUNNING)
94cc34af
PA
780 ui_out_text (uiout, "(running)\n");
781 else
782 {
783 /* The switch below puts us at the top of the stack (leaf
784 frame). */
785 switch_to_thread (tp->ptid);
786 print_stack_frame (get_selected_frame (NULL),
787 /* For MI output, print frame level. */
788 ui_out_is_mi_like_p (uiout),
789 LOCATION);
790 }
8e8901c5 791
90139f7d
VP
792 if (ui_out_is_mi_like_p (uiout))
793 {
794 char *state = "stopped";
ed406532 795 if (tp->state_ == THREAD_RUNNING)
90139f7d
VP
796 state = "running";
797 ui_out_field_string (uiout, "state", state);
798 }
799
8e8901c5 800 do_cleanups (chain2);
c906108c
SS
801 }
802
99b3d574
DP
803 /* Restores the current thread and the frame selected before
804 the "info threads" command. */
805 do_cleanups (old_chain);
c906108c 806
d729566a 807 if (pid == -1 && requested_thread == -1)
8e8901c5 808 {
4f8d22e3 809 gdb_assert (current_thread != -1
d729566a
PA
810 || !thread_list
811 || ptid_equal (inferior_ptid, null_ptid));
0bcd3e20 812 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
8e8901c5 813 ui_out_field_int (uiout, "current-thread-id", current_thread);
94cc34af 814
4f8d22e3
PA
815 if (current_thread != -1 && is_exited (current_ptid))
816 ui_out_message (uiout, 0, "\n\
817The current thread <Thread ID %d> has terminated. See `help thread'.\n",
818 current_thread);
d729566a
PA
819 else if (thread_list
820 && current_thread == -1
821 && ptid_equal (current_ptid, null_ptid))
822 ui_out_message (uiout, 0, "\n\
823No selected thread. See `help thread'.\n");
c906108c 824 }
c906108c
SS
825}
826
8e8901c5
VP
827
828/* Print information about currently known threads
829
830 * Note: this has the drawback that it _really_ switches
831 * threads, which frees the frame cache. A no-side
832 * effects info-threads command would be nicer.
833 */
834
835static void
836info_threads_command (char *arg, int from_tty)
837{
3ee1c036 838 print_thread_info (uiout, -1, -1);
8e8901c5
VP
839}
840
c906108c
SS
841/* Switch from one thread to another. */
842
6a6b96b9 843void
39f77062 844switch_to_thread (ptid_t ptid)
c906108c 845{
39f77062 846 if (ptid_equal (ptid, inferior_ptid))
c906108c
SS
847 return;
848
39f77062 849 inferior_ptid = ptid;
35f196d9 850 reinit_frame_cache ();
c906108c 851 registers_changed ();
94cc34af 852
4f8d22e3
PA
853 /* We don't check for is_stopped, because we're called at times
854 while in the TARGET_RUNNING state, e.g., while handling an
855 internal event. */
d729566a
PA
856 if (!ptid_equal (inferior_ptid, null_ptid)
857 && !is_exited (ptid)
858 && !is_executing (ptid))
fb14de7b 859 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
94cc34af
PA
860 else
861 stop_pc = ~(CORE_ADDR) 0;
c906108c
SS
862}
863
864static void
39f77062 865restore_current_thread (ptid_t ptid)
c906108c 866{
dcf4fbde 867 switch_to_thread (ptid);
99b3d574
DP
868}
869
870static void
4f8d22e3 871restore_selected_frame (struct frame_id a_frame_id, int frame_level)
99b3d574 872{
4f8d22e3
PA
873 struct frame_info *frame = NULL;
874 int count;
875
876 gdb_assert (frame_level >= 0);
877
878 /* Restore by level first, check if the frame id is the same as
879 expected. If that fails, try restoring by frame id. If that
880 fails, nothing to do, just warn the user. */
881
882 count = frame_level;
883 frame = find_relative_frame (get_current_frame (), &count);
884 if (count == 0
885 && frame != NULL
886 /* Either the frame ids match, of they're both invalid. The
887 latter case is not failsafe, but since it's highly unlikely
888 the search by level finds the wrong frame, it's 99.9(9)% of
889 the time (for all practical purposes) safe. */
890 && (frame_id_eq (get_frame_id (frame), a_frame_id)
891 /* Note: could be better to check every frame_id
892 member for equality here. */
893 || (!frame_id_p (get_frame_id (frame))
894 && !frame_id_p (a_frame_id))))
895 {
896 /* Cool, all is fine. */
897 select_frame (frame);
898 return;
899 }
99b3d574 900
4f8d22e3
PA
901 frame = frame_find_by_id (a_frame_id);
902 if (frame != NULL)
903 {
904 /* Cool, refound it. */
905 select_frame (frame);
906 return;
907 }
99b3d574 908
0c501536
PA
909 /* Nothing else to do, the frame layout really changed. Select the
910 innermost stack frame. */
911 select_frame (get_current_frame ());
912
913 /* Warn the user. */
4f8d22e3 914 if (!ui_out_is_mi_like_p (uiout))
99b3d574 915 {
4f8d22e3
PA
916 warning (_("\
917Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
918 frame_level);
919 /* For MI, we should probably have a notification about
920 current frame change. But this error is not very
921 likely, so don't bother for now. */
0c501536 922 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
c906108c
SS
923 }
924}
925
6ecce94d
AC
926struct current_thread_cleanup
927{
39f77062 928 ptid_t inferior_ptid;
99b3d574 929 struct frame_id selected_frame_id;
4f8d22e3
PA
930 int selected_frame_level;
931 int was_stopped;
6ecce94d
AC
932};
933
934static void
935do_restore_current_thread_cleanup (void *arg)
936{
4f8d22e3 937 struct thread_info *tp;
6ecce94d 938 struct current_thread_cleanup *old = arg;
d729566a
PA
939
940 tp = find_thread_pid (old->inferior_ptid);
941
942 /* If the previously selected thread belonged to a process that has
943 in the mean time been deleted (due to normal exit, detach, etc.),
944 then don't revert back to it, but instead simply drop back to no
945 thread selected. */
946 if (tp
88fc996f 947 && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL)
d729566a 948 restore_current_thread (old->inferior_ptid);
88fc996f
DE
949 else
950 restore_current_thread (null_ptid);
94cc34af 951
4f8d22e3
PA
952 /* The running state of the originally selected thread may have
953 changed, so we have to recheck it here. */
d729566a
PA
954 if (!ptid_equal (inferior_ptid, null_ptid)
955 && old->was_stopped
4f8d22e3
PA
956 && is_stopped (inferior_ptid)
957 && target_has_registers
958 && target_has_stack
959 && target_has_memory)
960 restore_selected_frame (old->selected_frame_id,
961 old->selected_frame_level);
962}
963
964static void
965restore_current_thread_cleanup_dtor (void *arg)
966{
967 struct current_thread_cleanup *old = arg;
968 struct thread_info *tp;
969 tp = find_thread_pid (old->inferior_ptid);
970 if (tp)
971 tp->refcount--;
b8c9b27d 972 xfree (old);
6ecce94d
AC
973}
974
6208b47d 975struct cleanup *
4f8d22e3 976make_cleanup_restore_current_thread (void)
6ecce94d 977{
4f8d22e3
PA
978 struct thread_info *tp;
979 struct frame_info *frame;
980 struct current_thread_cleanup *old;
981
982 old = xmalloc (sizeof (struct current_thread_cleanup));
39f77062 983 old->inferior_ptid = inferior_ptid;
4f8d22e3 984
d729566a
PA
985 if (!ptid_equal (inferior_ptid, null_ptid))
986 {
987 old->was_stopped = is_stopped (inferior_ptid);
988 if (old->was_stopped
989 && target_has_registers
990 && target_has_stack
991 && target_has_memory)
992 frame = get_selected_frame (NULL);
993 else
994 frame = NULL;
4f8d22e3 995
d729566a
PA
996 old->selected_frame_id = get_frame_id (frame);
997 old->selected_frame_level = frame_relative_level (frame);
998
999 tp = find_thread_pid (inferior_ptid);
1000 if (tp)
1001 tp->refcount++;
1002 }
4f8d22e3
PA
1003
1004 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1005 restore_current_thread_cleanup_dtor);
6ecce94d
AC
1006}
1007
c906108c
SS
1008/* Apply a GDB command to a list of threads. List syntax is a whitespace
1009 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1010 of two numbers seperated by a hyphen. Examples:
1011
c5aa993b
JM
1012 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1013 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
1014 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
1015 */
c906108c
SS
1016
1017static void
fba45db2 1018thread_apply_all_command (char *cmd, int from_tty)
c906108c
SS
1019{
1020 struct thread_info *tp;
4f8d22e3 1021 struct cleanup *old_chain;
e35ce267 1022 char *saved_cmd;
c906108c
SS
1023
1024 if (cmd == NULL || *cmd == '\000')
8a3fe4f8 1025 error (_("Please specify a command following the thread ID list"));
94cc34af 1026
4f8d22e3 1027 prune_threads ();
e9d196c5
MS
1028 target_find_new_threads ();
1029
4f8d22e3
PA
1030 old_chain = make_cleanup_restore_current_thread ();
1031
e35ce267
CF
1032 /* Save a copy of the command in case it is clobbered by
1033 execute_command */
5b616ba1 1034 saved_cmd = xstrdup (cmd);
94cc34af 1035 make_cleanup (xfree, saved_cmd);
c906108c
SS
1036 for (tp = thread_list; tp; tp = tp->next)
1037 if (thread_alive (tp))
1038 {
dcf4fbde 1039 switch_to_thread (tp->ptid);
94cc34af 1040
a3f17187 1041 printf_filtered (_("\nThread %d (%s):\n"),
54ba13f7 1042 tp->num, target_pid_to_str (inferior_ptid));
c906108c 1043 execute_command (cmd, from_tty);
6949171e 1044 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
c906108c 1045 }
6ecce94d
AC
1046
1047 do_cleanups (old_chain);
c906108c
SS
1048}
1049
1050static void
fba45db2 1051thread_apply_command (char *tidlist, int from_tty)
c906108c
SS
1052{
1053 char *cmd;
1054 char *p;
1055 struct cleanup *old_chain;
e35ce267 1056 char *saved_cmd;
c906108c
SS
1057
1058 if (tidlist == NULL || *tidlist == '\000')
8a3fe4f8 1059 error (_("Please specify a thread ID list"));
c906108c 1060
c5aa993b 1061 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
c906108c
SS
1062
1063 if (*cmd == '\000')
8a3fe4f8 1064 error (_("Please specify a command following the thread ID list"));
c906108c 1065
e35ce267
CF
1066 /* Save a copy of the command in case it is clobbered by
1067 execute_command */
5b616ba1 1068 saved_cmd = xstrdup (cmd);
4f8d22e3 1069 old_chain = make_cleanup (xfree, saved_cmd);
c906108c
SS
1070 while (tidlist < cmd)
1071 {
1072 struct thread_info *tp;
1073 int start, end;
1074
1075 start = strtol (tidlist, &p, 10);
1076 if (p == tidlist)
8a3fe4f8 1077 error (_("Error parsing %s"), tidlist);
c906108c
SS
1078 tidlist = p;
1079
1080 while (*tidlist == ' ' || *tidlist == '\t')
1081 tidlist++;
1082
1083 if (*tidlist == '-') /* Got a range of IDs? */
1084 {
c5aa993b 1085 tidlist++; /* Skip the - */
c906108c
SS
1086 end = strtol (tidlist, &p, 10);
1087 if (p == tidlist)
8a3fe4f8 1088 error (_("Error parsing %s"), tidlist);
c906108c
SS
1089 tidlist = p;
1090
1091 while (*tidlist == ' ' || *tidlist == '\t')
1092 tidlist++;
1093 }
1094 else
1095 end = start;
1096
65fc9b77
PA
1097 make_cleanup_restore_current_thread ();
1098
c906108c
SS
1099 for (; start <= end; start++)
1100 {
1101 tp = find_thread_id (start);
1102
1103 if (!tp)
8a3fe4f8 1104 warning (_("Unknown thread %d."), start);
c906108c 1105 else if (!thread_alive (tp))
8a3fe4f8 1106 warning (_("Thread %d has terminated."), start);
c906108c
SS
1107 else
1108 {
dcf4fbde 1109 switch_to_thread (tp->ptid);
4f8d22e3 1110
a3f17187 1111 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
54ba13f7 1112 target_pid_to_str (inferior_ptid));
c906108c 1113 execute_command (cmd, from_tty);
4f8d22e3
PA
1114
1115 /* Restore exact command used previously. */
1116 strcpy (cmd, saved_cmd);
c906108c
SS
1117 }
1118 }
1119 }
6ecce94d
AC
1120
1121 do_cleanups (old_chain);
c906108c
SS
1122}
1123
1124/* Switch to the specified thread. Will dispatch off to thread_apply_command
1125 if prefix of arg is `apply'. */
1126
1127static void
fba45db2 1128thread_command (char *tidstr, int from_tty)
c906108c 1129{
c906108c
SS
1130 if (!tidstr)
1131 {
d729566a
PA
1132 if (ptid_equal (inferior_ptid, null_ptid))
1133 error (_("No thread selected"));
1134
c906108c 1135 if (target_has_stack)
4f8d22e3
PA
1136 {
1137 if (is_exited (inferior_ptid))
1138 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1139 pid_to_thread_id (inferior_ptid),
54ba13f7 1140 target_pid_to_str (inferior_ptid));
4f8d22e3
PA
1141 else
1142 printf_filtered (_("[Current thread is %d (%s)]\n"),
1143 pid_to_thread_id (inferior_ptid),
54ba13f7 1144 target_pid_to_str (inferior_ptid));
4f8d22e3 1145 }
c906108c 1146 else
8a3fe4f8 1147 error (_("No stack."));
c906108c
SS
1148 return;
1149 }
c5394b80 1150
ce43223b 1151 gdb_thread_select (uiout, tidstr, NULL);
c5394b80
JM
1152}
1153
93815fbf
VP
1154/* Print notices when new threads are attached and detached. */
1155int print_thread_events = 1;
1156static void
1157show_print_thread_events (struct ui_file *file, int from_tty,
1158 struct cmd_list_element *c, const char *value)
1159{
1160 fprintf_filtered (file, _("\
1161Printing of thread events is %s.\n"),
1162 value);
1163}
1164
c5394b80 1165static int
6949171e 1166do_captured_thread_select (struct ui_out *uiout, void *tidstr)
c5394b80
JM
1167{
1168 int num;
1169 struct thread_info *tp;
1170
81490ea1 1171 num = value_as_long (parse_and_eval (tidstr));
c906108c
SS
1172
1173 tp = find_thread_id (num);
1174
8b93c638 1175 if (!tp)
8a3fe4f8 1176 error (_("Thread ID %d not known."), num);
c906108c
SS
1177
1178 if (!thread_alive (tp))
8a3fe4f8 1179 error (_("Thread ID %d has terminated."), num);
c906108c 1180
dcf4fbde 1181 switch_to_thread (tp->ptid);
c906108c 1182
db5a7484
NR
1183 annotate_thread_changed ();
1184
8b93c638 1185 ui_out_text (uiout, "[Switching to thread ");
39f77062 1186 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
8b93c638 1187 ui_out_text (uiout, " (");
54ba13f7 1188 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
8b93c638 1189 ui_out_text (uiout, ")]");
c5394b80 1190
4f8d22e3
PA
1191 /* Note that we can't reach this with an exited thread, due to the
1192 thread_alive check above. */
1193 if (tp->state_ == THREAD_RUNNING)
94cc34af 1194 ui_out_text (uiout, "(running)\n");
4f8d22e3
PA
1195 else
1196 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1197
1198 /* Since the current thread may have changed, see if there is any
1199 exited thread we can now delete. */
1200 prune_threads ();
94cc34af 1201
c5394b80
JM
1202 return GDB_RC_OK;
1203}
1204
1205enum gdb_rc
ce43223b 1206gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
c5394b80 1207{
b0b13bb4
DJ
1208 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1209 error_message, RETURN_MASK_ALL) < 0)
1210 return GDB_RC_FAIL;
1211 return GDB_RC_OK;
c906108c
SS
1212}
1213
1214/* Commands with a prefix of `thread'. */
1215struct cmd_list_element *thread_cmd_list = NULL;
1216
1217void
fba45db2 1218_initialize_thread (void)
c906108c
SS
1219{
1220 static struct cmd_list_element *thread_apply_list = NULL;
c906108c 1221
d729566a
PA
1222 add_info ("threads", info_threads_command,
1223 _("IDs of currently known threads."));
c906108c 1224
d729566a 1225 add_prefix_cmd ("thread", class_run, thread_command, _("\
1bedd215
AC
1226Use this command to switch between threads.\n\
1227The new thread ID must be currently known."),
d729566a 1228 &thread_cmd_list, "thread ", 1, &cmdlist);
c906108c 1229
d729566a
PA
1230 add_prefix_cmd ("apply", class_run, thread_apply_command,
1231 _("Apply a command to a list of threads."),
1232 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
c906108c 1233
d729566a
PA
1234 add_cmd ("all", class_run, thread_apply_all_command,
1235 _("Apply a command to all threads."), &thread_apply_list);
c906108c
SS
1236
1237 if (!xdb_commands)
1238 add_com_alias ("t", "thread", class_run, 1);
93815fbf
VP
1239
1240 add_setshow_boolean_cmd ("thread-events", no_class,
1241 &print_thread_events, _("\
11c68c47
EZ
1242Set printing of thread events (such as thread start and exit)."), _("\
1243Show printing of thread events (such as thread start and exit)."), NULL,
93815fbf
VP
1244 NULL,
1245 show_print_thread_events,
1246 &setprintlist, &showprintlist);
c906108c 1247}