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