* inferior.h (stop_bpstat): Delete.
[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);
4f8d22e3
PA
109}
110
111static void
112free_thread (struct thread_info *tp)
113{
114 clear_thread_inferior_resources (tp);
a474d7c2 115
7c952b6d
ND
116 /* FIXME: do I ever need to call the back-end to give it a
117 chance at this private data before deleting the thread? */
118 if (tp->private)
b8c9b27d 119 xfree (tp->private);
7c952b6d 120
b8c9b27d 121 xfree (tp);
7c952b6d
ND
122}
123
c906108c 124void
fba45db2 125init_thread_list (void)
c906108c
SS
126{
127 struct thread_info *tp, *tpnext;
128
7c952b6d 129 highest_thread_num = 0;
4f8d22e3 130 main_thread_state = THREAD_STOPPED;
8ea051c5
PA
131 main_thread_executing = 0;
132
c906108c
SS
133 if (!thread_list)
134 return;
135
136 for (tp = thread_list; tp; tp = tpnext)
137 {
138 tpnext = tp->next;
7c952b6d 139 free_thread (tp);
c906108c
SS
140 }
141
142 thread_list = NULL;
c906108c
SS
143}
144
0d06e24b 145struct thread_info *
93815fbf 146add_thread_silent (ptid_t ptid)
c906108c
SS
147{
148 struct thread_info *tp;
149
4f8d22e3
PA
150 tp = find_thread_pid (ptid);
151 if (tp)
152 /* Found an old thread with the same id. It has to be dead,
153 otherwise we wouldn't be adding a new thread with the same id.
154 The OS is reusing this id --- delete it, and recreate a new
155 one. */
156 {
157 /* In addition to deleting the thread, if this is the current
158 thread, then we need to also get rid of the current infrun
159 context, and take care that delete_thread doesn't really
160 delete the thread if it is inferior_ptid. Create a new
161 template thread in the list with an invalid ptid, context
162 switch to it, delete the original thread, reset the new
163 thread's ptid, and switch to it. */
164
165 if (ptid_equal (inferior_ptid, ptid))
166 {
167 tp = xmalloc (sizeof (*tp));
168 memset (tp, 0, sizeof (*tp));
169 tp->ptid = minus_one_ptid;
170 tp->num = ++highest_thread_num;
171 tp->next = thread_list;
172 thread_list = tp;
173 context_switch_to (minus_one_ptid);
174
175 /* Now we can delete it. */
176 delete_thread (ptid);
177
178 /* Since the context is already set to this new thread,
179 reset its ptid, and reswitch inferior_ptid to it. */
180 tp->ptid = ptid;
181 switch_to_thread (ptid);
182
183 observer_notify_new_thread (tp);
184
185 /* All done. */
186 return tp;
187 }
188 else
189 /* Just go ahead and delete it. */
190 delete_thread (ptid);
191 }
192
6c0d3f6a
MS
193 tp = (struct thread_info *) xmalloc (sizeof (*tp));
194 memset (tp, 0, sizeof (*tp));
39f77062 195 tp->ptid = ptid;
c906108c 196 tp->num = ++highest_thread_num;
c906108c
SS
197 tp->next = thread_list;
198 thread_list = tp;
cfc01461
VP
199
200 observer_notify_new_thread (tp);
201
0d06e24b 202 return tp;
c906108c
SS
203}
204
93815fbf 205struct thread_info *
17faa917 206add_thread_with_info (ptid_t ptid, struct private_thread_info *private)
93815fbf
VP
207{
208 struct thread_info *result = add_thread_silent (ptid);
209
17faa917
DJ
210 result->private = private;
211
93815fbf 212 if (print_thread_events)
fd532e2e 213 printf_unfiltered (_("[New %s]\n"), target_pid_to_str (ptid));
d4fc5b1e
NR
214
215 annotate_new_thread ();
93815fbf
VP
216 return result;
217}
218
17faa917
DJ
219struct thread_info *
220add_thread (ptid_t ptid)
221{
222 return add_thread_with_info (ptid, NULL);
223}
224
5e0b29c1
PA
225/* Delete thread PTID. If SILENT, don't notify the observer of this
226 exit. */
227static void
228delete_thread_1 (ptid_t ptid, int silent)
c906108c
SS
229{
230 struct thread_info *tp, *tpprev;
231
232 tpprev = NULL;
233
234 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
39f77062 235 if (ptid_equal (tp->ptid, ptid))
c906108c
SS
236 break;
237
238 if (!tp)
239 return;
240
4f8d22e3
PA
241 /* If this is the current thread, or there's code out there that
242 relies on it existing (refcount > 0) we can't delete yet. Mark
243 it as exited, and notify it. */
244 if (tp->refcount > 0
245 || ptid_equal (tp->ptid, inferior_ptid))
246 {
247 if (tp->state_ != THREAD_EXITED)
248 {
249 if (!silent)
250 observer_notify_thread_exit (tp);
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
PA
268 /* Notify thread exit, but only if we haven't already. */
269 if (!silent && tp->state_ != THREAD_EXITED)
5e0b29c1 270 observer_notify_thread_exit (tp);
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
8b93c638
JM
404/* Print a list of thread ids currently known, and the total number of
405 threads. To be used from within catch_errors. */
6949171e
JJ
406static int
407do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
8b93c638
JM
408{
409 struct thread_info *tp;
410 int num = 0;
3b31d625 411 struct cleanup *cleanup_chain;
8b93c638 412
7990a578
EZ
413 prune_threads ();
414 target_find_new_threads ();
415
3b31d625 416 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
8b93c638
JM
417
418 for (tp = thread_list; tp; tp = tp->next)
419 {
4f8d22e3
PA
420 if (tp->state_ == THREAD_EXITED)
421 continue;
8b93c638
JM
422 num++;
423 ui_out_field_int (uiout, "thread-id", tp->num);
424 }
425
3b31d625 426 do_cleanups (cleanup_chain);
8b93c638
JM
427 ui_out_field_int (uiout, "number-of-threads", num);
428 return GDB_RC_OK;
429}
430
431/* Official gdblib interface function to get a list of thread ids and
432 the total number. */
433enum gdb_rc
ce43223b 434gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
8b93c638 435{
b0b13bb4
DJ
436 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
437 error_message, RETURN_MASK_ALL) < 0)
438 return GDB_RC_FAIL;
439 return GDB_RC_OK;
8b93c638 440}
c906108c
SS
441
442/* Load infrun state for the thread PID. */
443
c5aa993b 444void
6949171e 445load_infrun_state (ptid_t ptid,
a474d7c2
PA
446 struct continuation **continuations,
447 struct continuation **intermediate_continuations,
448 int *proceed_to_finish,
a474d7c2
PA
449 int *stop_step,
450 int *step_multi,
347bddb7 451 enum target_signal *stop_signal)
c906108c
SS
452{
453 struct thread_info *tp;
454
455 /* If we can't find the thread, then we're debugging a single threaded
456 process. No need to do anything in that case. */
39f77062 457 tp = find_thread_id (pid_to_thread_id (ptid));
c906108c
SS
458 if (tp == NULL)
459 return;
460
a474d7c2
PA
461 /* In all-stop mode, these are global state, while in non-stop mode,
462 they are per thread. */
463 if (non_stop)
464 {
465 *continuations = tp->continuations;
466 tp->continuations = NULL;
467 *intermediate_continuations = tp->intermediate_continuations;
468 tp->intermediate_continuations = NULL;
469 *proceed_to_finish = tp->proceed_to_finish;
a474d7c2
PA
470 *stop_step = tp->stop_step;
471 *step_multi = tp->step_multi;
472 *stop_signal = tp->stop_signal;
a474d7c2 473 }
c906108c
SS
474}
475
476/* Save infrun state for the thread PID. */
477
c5aa993b 478void
6949171e 479save_infrun_state (ptid_t ptid,
a474d7c2
PA
480 struct continuation *continuations,
481 struct continuation *intermediate_continuations,
482 int proceed_to_finish,
a474d7c2
PA
483 int stop_step,
484 int step_multi,
347bddb7 485 enum target_signal stop_signal)
c906108c
SS
486{
487 struct thread_info *tp;
488
489 /* If we can't find the thread, then we're debugging a single-threaded
490 process. Nothing to do in that case. */
39f77062 491 tp = find_thread_id (pid_to_thread_id (ptid));
c906108c
SS
492 if (tp == NULL)
493 return;
494
a474d7c2
PA
495 /* In all-stop mode, these are global state, while in non-stop mode,
496 they are per thread. */
497 if (non_stop)
498 {
499 tp->continuations = continuations;
500 tp->intermediate_continuations = intermediate_continuations;
501 tp->proceed_to_finish = proceed_to_finish;
a474d7c2
PA
502 tp->stop_step = stop_step;
503 tp->step_multi = step_multi;
504 tp->stop_signal = stop_signal;
a474d7c2 505 }
c906108c
SS
506}
507
508/* Return true if TP is an active thread. */
509static int
fba45db2 510thread_alive (struct thread_info *tp)
c906108c 511{
4f8d22e3 512 if (tp->state_ == THREAD_EXITED)
c906108c 513 return 0;
39f77062 514 if (!target_thread_alive (tp->ptid))
4f8d22e3 515 return 0;
c906108c
SS
516 return 1;
517}
518
519static void
fba45db2 520prune_threads (void)
c906108c 521{
d4f3574e 522 struct thread_info *tp, *next;
c906108c 523
c906108c
SS
524 for (tp = thread_list; tp; tp = next)
525 {
526 next = tp->next;
527 if (!thread_alive (tp))
39f77062 528 delete_thread (tp->ptid);
c906108c
SS
529 }
530}
531
5231c1fd
PA
532void
533thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
534{
535 struct thread_info * tp = find_thread_pid (old_ptid);
536 tp->ptid = new_ptid;
537
538 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
539}
540
e1ac3328
VP
541void
542set_running (ptid_t ptid, int running)
543{
544 struct thread_info *tp;
545
546 if (!thread_list)
547 {
548 /* This is one of the targets that does not add main
549 thread to the thread list. Just use a single
550 global flag to indicate that a thread is running.
551
552 This problem is unique to ST programs. For MT programs,
553 the main thread is always present in the thread list. If it's
554 not, the first call to context_switch will mess up GDB internal
555 state. */
4f8d22e3
PA
556 if (running
557 && main_thread_state != THREAD_RUNNING
558 && !suppress_resume_observer)
e1ac3328 559 observer_notify_target_resumed (ptid);
4f8d22e3 560 main_thread_state = running ? THREAD_RUNNING : THREAD_STOPPED;
e1ac3328
VP
561 return;
562 }
563
564 /* We try not to notify the observer if no thread has actually changed
565 the running state -- merely to reduce the number of messages to
566 frontend. Frontend is supposed to handle multiple *running just fine. */
567 if (PIDGET (ptid) == -1)
568 {
569 int any_started = 0;
570 for (tp = thread_list; tp; tp = tp->next)
571 {
4f8d22e3
PA
572 if (tp->state_ == THREAD_EXITED)
573 continue;
574 if (running && tp->state_ == THREAD_STOPPED)
575 any_started = 1;
576 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
e1ac3328 577 }
8f6a8e84 578 if (any_started && !suppress_resume_observer)
e1ac3328
VP
579 observer_notify_target_resumed (ptid);
580 }
581 else
582 {
4f8d22e3 583 int started = 0;
e1ac3328
VP
584 tp = find_thread_pid (ptid);
585 gdb_assert (tp);
4f8d22e3
PA
586 gdb_assert (tp->state_ != THREAD_EXITED);
587 if (running && tp->state_ == THREAD_STOPPED)
588 started = 1;
589 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
590 if (started && !suppress_resume_observer)
591 observer_notify_target_resumed (ptid);
592 }
e1ac3328
VP
593}
594
4f8d22e3
PA
595static int
596is_thread_state (ptid_t ptid, enum thread_state state)
e1ac3328
VP
597{
598 struct thread_info *tp;
599
8ea051c5
PA
600 if (!target_has_execution)
601 return 0;
602
e1ac3328 603 if (!thread_list)
4f8d22e3 604 return main_thread_state == state;
e1ac3328
VP
605
606 tp = find_thread_pid (ptid);
607 gdb_assert (tp);
4f8d22e3
PA
608 return tp->state_ == state;
609}
610
611int
612is_stopped (ptid_t ptid)
613{
614 /* Without execution, this property is always true. */
615 if (!target_has_execution)
616 return 1;
617
618 return is_thread_state (ptid, THREAD_STOPPED);
619}
620
621int
622is_exited (ptid_t ptid)
623{
624 /* Without execution, this property is always false. */
625 if (!target_has_execution)
626 return 0;
627
628 return is_thread_state (ptid, THREAD_EXITED);
629}
630
631int
632is_running (ptid_t ptid)
633{
634 /* Without execution, this property is always false. */
635 if (!target_has_execution)
636 return 0;
637
638 return is_thread_state (ptid, THREAD_RUNNING);
e1ac3328
VP
639}
640
8ea051c5
PA
641int
642any_running (void)
643{
644 struct thread_info *tp;
645
646 if (!target_has_execution)
647 return 0;
648
649 if (!thread_list)
4f8d22e3 650 return main_thread_state == THREAD_RUNNING;
8ea051c5
PA
651
652 for (tp = thread_list; tp; tp = tp->next)
4f8d22e3 653 if (tp->state_ == THREAD_RUNNING)
8ea051c5
PA
654 return 1;
655
656 return 0;
657}
658
659int
660is_executing (ptid_t ptid)
661{
662 struct thread_info *tp;
663
664 if (!target_has_execution)
665 return 0;
666
667 if (!thread_list)
668 return main_thread_executing;
669
670 tp = find_thread_pid (ptid);
671 gdb_assert (tp);
672 return tp->executing_;
673}
674
675void
676set_executing (ptid_t ptid, int executing)
677{
678 struct thread_info *tp;
679
680 if (!thread_list)
681 {
682 /* This target does not add the main thread to the thread list.
683 Use a global flag to indicate that the thread is
684 executing. */
685 main_thread_executing = executing;
686 return;
687 }
688
689 if (PIDGET (ptid) == -1)
690 {
691 for (tp = thread_list; tp; tp = tp->next)
692 tp->executing_ = executing;
693 }
694 else
695 {
696 tp = find_thread_pid (ptid);
697 gdb_assert (tp);
698 tp->executing_ = executing;
699 }
700}
701
8e8901c5
VP
702/* Prints the list of threads and their details on UIOUT.
703 This is a version of 'info_thread_command' suitable for
704 use from MI.
4f8d22e3 705 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
8e8901c5
VP
706 that should be printed. Otherwise, all threads are
707 printed. */
708void
709print_thread_info (struct ui_out *uiout, int requested_thread)
c906108c
SS
710{
711 struct thread_info *tp;
39f77062 712 ptid_t current_ptid;
99b3d574 713 struct cleanup *old_chain;
0d06e24b 714 char *extra_info;
8e8901c5 715 int current_thread = -1;
c906108c 716
c906108c 717 prune_threads ();
b83266a0 718 target_find_new_threads ();
39f77062 719 current_ptid = inferior_ptid;
4f8d22e3
PA
720
721 /* We'll be switching threads temporarily. */
722 old_chain = make_cleanup_restore_current_thread ();
723
724 make_cleanup_ui_out_list_begin_end (uiout, "threads");
c906108c
SS
725 for (tp = thread_list; tp; tp = tp->next)
726 {
8e8901c5
VP
727 struct cleanup *chain2;
728
729 if (requested_thread != -1 && tp->num != requested_thread)
730 continue;
731
4f8d22e3
PA
732 if (ptid_equal (tp->ptid, current_ptid))
733 current_thread = tp->num;
734
735 if (tp->state_ == THREAD_EXITED)
736 continue;
737
8e8901c5
VP
738 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
739
39f77062 740 if (ptid_equal (tp->ptid, current_ptid))
4f8d22e3 741 ui_out_text (uiout, "* ");
c906108c 742 else
8e8901c5 743 ui_out_text (uiout, " ");
c906108c 744
8e8901c5
VP
745 ui_out_field_int (uiout, "id", tp->num);
746 ui_out_text (uiout, " ");
747 ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid));
0d06e24b 748
4f8d22e3 749 if (tp->state_ != THREAD_EXITED)
8e8901c5 750 {
4f8d22e3
PA
751 extra_info = target_extra_thread_info (tp);
752 if (extra_info)
753 {
754 ui_out_text (uiout, " (");
755 ui_out_field_string (uiout, "details", extra_info);
756 ui_out_text (uiout, ")");
757 }
758 ui_out_text (uiout, " ");
8e8901c5 759 }
4f8d22e3
PA
760
761 if (tp->state_ == THREAD_RUNNING)
94cc34af
PA
762 ui_out_text (uiout, "(running)\n");
763 else
764 {
765 /* The switch below puts us at the top of the stack (leaf
766 frame). */
767 switch_to_thread (tp->ptid);
768 print_stack_frame (get_selected_frame (NULL),
769 /* For MI output, print frame level. */
770 ui_out_is_mi_like_p (uiout),
771 LOCATION);
772 }
8e8901c5 773
90139f7d
VP
774 if (ui_out_is_mi_like_p (uiout))
775 {
776 char *state = "stopped";
777 if (tp->state_ == THREAD_EXITED)
778 state = "exited";
779 else if (tp->state_ == THREAD_RUNNING)
780 state = "running";
781 ui_out_field_string (uiout, "state", state);
782 }
783
8e8901c5 784 do_cleanups (chain2);
c906108c
SS
785 }
786
99b3d574
DP
787 /* Restores the current thread and the frame selected before
788 the "info threads" command. */
789 do_cleanups (old_chain);
c906108c 790
8e8901c5
VP
791 if (requested_thread == -1)
792 {
4f8d22e3
PA
793 gdb_assert (current_thread != -1
794 || !thread_list);
0bcd3e20 795 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
8e8901c5 796 ui_out_field_int (uiout, "current-thread-id", current_thread);
94cc34af 797
4f8d22e3
PA
798 if (current_thread != -1 && is_exited (current_ptid))
799 ui_out_message (uiout, 0, "\n\
800The current thread <Thread ID %d> has terminated. See `help thread'.\n",
801 current_thread);
c906108c 802 }
c906108c
SS
803}
804
8e8901c5
VP
805
806/* Print information about currently known threads
807
808 * Note: this has the drawback that it _really_ switches
809 * threads, which frees the frame cache. A no-side
810 * effects info-threads command would be nicer.
811 */
812
813static void
814info_threads_command (char *arg, int from_tty)
815{
816 print_thread_info (uiout, -1);
817}
818
c906108c
SS
819/* Switch from one thread to another. */
820
6a6b96b9 821void
39f77062 822switch_to_thread (ptid_t ptid)
c906108c 823{
39f77062 824 if (ptid_equal (ptid, inferior_ptid))
c906108c
SS
825 return;
826
39f77062 827 inferior_ptid = ptid;
35f196d9 828 reinit_frame_cache ();
c906108c 829 registers_changed ();
94cc34af 830
4f8d22e3
PA
831 /* We don't check for is_stopped, because we're called at times
832 while in the TARGET_RUNNING state, e.g., while handling an
833 internal event. */
834 if (!is_exited (ptid) && !is_executing (ptid))
94cc34af
PA
835 stop_pc = read_pc ();
836 else
837 stop_pc = ~(CORE_ADDR) 0;
c906108c
SS
838}
839
840static void
39f77062 841restore_current_thread (ptid_t ptid)
c906108c 842{
6949171e 843 if (!ptid_equal (ptid, inferior_ptid))
c906108c 844 {
4f8d22e3
PA
845 if (non_stop)
846 context_switch_to (ptid);
847 else
848 switch_to_thread (ptid);
99b3d574
DP
849 }
850}
851
852static void
4f8d22e3 853restore_selected_frame (struct frame_id a_frame_id, int frame_level)
99b3d574 854{
4f8d22e3
PA
855 struct frame_info *frame = NULL;
856 int count;
857
858 gdb_assert (frame_level >= 0);
859
860 /* Restore by level first, check if the frame id is the same as
861 expected. If that fails, try restoring by frame id. If that
862 fails, nothing to do, just warn the user. */
863
864 count = frame_level;
865 frame = find_relative_frame (get_current_frame (), &count);
866 if (count == 0
867 && frame != NULL
868 /* Either the frame ids match, of they're both invalid. The
869 latter case is not failsafe, but since it's highly unlikely
870 the search by level finds the wrong frame, it's 99.9(9)% of
871 the time (for all practical purposes) safe. */
872 && (frame_id_eq (get_frame_id (frame), a_frame_id)
873 /* Note: could be better to check every frame_id
874 member for equality here. */
875 || (!frame_id_p (get_frame_id (frame))
876 && !frame_id_p (a_frame_id))))
877 {
878 /* Cool, all is fine. */
879 select_frame (frame);
880 return;
881 }
99b3d574 882
4f8d22e3
PA
883 frame = frame_find_by_id (a_frame_id);
884 if (frame != NULL)
885 {
886 /* Cool, refound it. */
887 select_frame (frame);
888 return;
889 }
99b3d574 890
0c501536
PA
891 /* Nothing else to do, the frame layout really changed. Select the
892 innermost stack frame. */
893 select_frame (get_current_frame ());
894
895 /* Warn the user. */
4f8d22e3 896 if (!ui_out_is_mi_like_p (uiout))
99b3d574 897 {
4f8d22e3
PA
898 warning (_("\
899Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
900 frame_level);
901 /* For MI, we should probably have a notification about
902 current frame change. But this error is not very
903 likely, so don't bother for now. */
0c501536 904 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
c906108c
SS
905 }
906}
907
6ecce94d
AC
908struct current_thread_cleanup
909{
39f77062 910 ptid_t inferior_ptid;
99b3d574 911 struct frame_id selected_frame_id;
4f8d22e3
PA
912 int selected_frame_level;
913 int was_stopped;
6ecce94d
AC
914};
915
916static void
917do_restore_current_thread_cleanup (void *arg)
918{
4f8d22e3 919 struct thread_info *tp;
6ecce94d 920 struct current_thread_cleanup *old = arg;
39f77062 921 restore_current_thread (old->inferior_ptid);
94cc34af 922
4f8d22e3
PA
923 /* The running state of the originally selected thread may have
924 changed, so we have to recheck it here. */
925 if (old->was_stopped
926 && is_stopped (inferior_ptid)
927 && target_has_registers
928 && target_has_stack
929 && target_has_memory)
930 restore_selected_frame (old->selected_frame_id,
931 old->selected_frame_level);
932}
933
934static void
935restore_current_thread_cleanup_dtor (void *arg)
936{
937 struct current_thread_cleanup *old = arg;
938 struct thread_info *tp;
939 tp = find_thread_pid (old->inferior_ptid);
940 if (tp)
941 tp->refcount--;
b8c9b27d 942 xfree (old);
6ecce94d
AC
943}
944
6208b47d 945struct cleanup *
4f8d22e3 946make_cleanup_restore_current_thread (void)
6ecce94d 947{
4f8d22e3
PA
948 struct thread_info *tp;
949 struct frame_info *frame;
950 struct current_thread_cleanup *old;
951
952 old = xmalloc (sizeof (struct current_thread_cleanup));
39f77062 953 old->inferior_ptid = inferior_ptid;
4f8d22e3
PA
954 old->was_stopped = is_stopped (inferior_ptid);
955 if (old->was_stopped
956 && target_has_registers
957 && target_has_stack
958 && target_has_memory)
959 frame = get_selected_frame (NULL);
960 else
961 frame = NULL;
962
963 old->selected_frame_id = get_frame_id (frame);
964 old->selected_frame_level = frame_relative_level (frame);
965
966 tp = find_thread_pid (inferior_ptid);
967 if (tp)
968 tp->refcount++;
969
970 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
971 restore_current_thread_cleanup_dtor);
6ecce94d
AC
972}
973
c906108c
SS
974/* Apply a GDB command to a list of threads. List syntax is a whitespace
975 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
976 of two numbers seperated by a hyphen. Examples:
977
c5aa993b
JM
978 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
979 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
980 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
981 */
c906108c
SS
982
983static void
fba45db2 984thread_apply_all_command (char *cmd, int from_tty)
c906108c
SS
985{
986 struct thread_info *tp;
4f8d22e3 987 struct cleanup *old_chain;
e35ce267 988 char *saved_cmd;
c906108c
SS
989
990 if (cmd == NULL || *cmd == '\000')
8a3fe4f8 991 error (_("Please specify a command following the thread ID list"));
94cc34af 992
4f8d22e3 993 prune_threads ();
e9d196c5
MS
994 target_find_new_threads ();
995
4f8d22e3
PA
996 old_chain = make_cleanup_restore_current_thread ();
997
e35ce267
CF
998 /* Save a copy of the command in case it is clobbered by
999 execute_command */
5b616ba1 1000 saved_cmd = xstrdup (cmd);
94cc34af 1001 make_cleanup (xfree, saved_cmd);
c906108c
SS
1002 for (tp = thread_list; tp; tp = tp->next)
1003 if (thread_alive (tp))
1004 {
94cc34af
PA
1005 if (non_stop)
1006 context_switch_to (tp->ptid);
1007 else
1008 switch_to_thread (tp->ptid);
1009
a3f17187 1010 printf_filtered (_("\nThread %d (%s):\n"),
6949171e 1011 tp->num, target_tid_to_str (inferior_ptid));
c906108c 1012 execute_command (cmd, from_tty);
6949171e 1013 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
c906108c 1014 }
6ecce94d
AC
1015
1016 do_cleanups (old_chain);
c906108c
SS
1017}
1018
1019static void
fba45db2 1020thread_apply_command (char *tidlist, int from_tty)
c906108c
SS
1021{
1022 char *cmd;
1023 char *p;
1024 struct cleanup *old_chain;
e35ce267 1025 char *saved_cmd;
c906108c
SS
1026
1027 if (tidlist == NULL || *tidlist == '\000')
8a3fe4f8 1028 error (_("Please specify a thread ID list"));
c906108c 1029
c5aa993b 1030 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
c906108c
SS
1031
1032 if (*cmd == '\000')
8a3fe4f8 1033 error (_("Please specify a command following the thread ID list"));
c906108c 1034
e35ce267
CF
1035 /* Save a copy of the command in case it is clobbered by
1036 execute_command */
5b616ba1 1037 saved_cmd = xstrdup (cmd);
4f8d22e3 1038 old_chain = make_cleanup (xfree, saved_cmd);
c906108c
SS
1039 while (tidlist < cmd)
1040 {
1041 struct thread_info *tp;
1042 int start, end;
1043
1044 start = strtol (tidlist, &p, 10);
1045 if (p == tidlist)
8a3fe4f8 1046 error (_("Error parsing %s"), tidlist);
c906108c
SS
1047 tidlist = p;
1048
1049 while (*tidlist == ' ' || *tidlist == '\t')
1050 tidlist++;
1051
1052 if (*tidlist == '-') /* Got a range of IDs? */
1053 {
c5aa993b 1054 tidlist++; /* Skip the - */
c906108c
SS
1055 end = strtol (tidlist, &p, 10);
1056 if (p == tidlist)
8a3fe4f8 1057 error (_("Error parsing %s"), tidlist);
c906108c
SS
1058 tidlist = p;
1059
1060 while (*tidlist == ' ' || *tidlist == '\t')
1061 tidlist++;
1062 }
1063 else
1064 end = start;
1065
65fc9b77
PA
1066 make_cleanup_restore_current_thread ();
1067
c906108c
SS
1068 for (; start <= end; start++)
1069 {
1070 tp = find_thread_id (start);
1071
1072 if (!tp)
8a3fe4f8 1073 warning (_("Unknown thread %d."), start);
c906108c 1074 else if (!thread_alive (tp))
8a3fe4f8 1075 warning (_("Thread %d has terminated."), start);
c906108c
SS
1076 else
1077 {
94cc34af
PA
1078 if (non_stop)
1079 context_switch_to (tp->ptid);
1080 else
1081 switch_to_thread (tp->ptid);
4f8d22e3 1082
a3f17187 1083 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
39f77062 1084 target_tid_to_str (inferior_ptid));
c906108c 1085 execute_command (cmd, from_tty);
4f8d22e3
PA
1086
1087 /* Restore exact command used previously. */
1088 strcpy (cmd, saved_cmd);
c906108c
SS
1089 }
1090 }
1091 }
6ecce94d
AC
1092
1093 do_cleanups (old_chain);
c906108c
SS
1094}
1095
1096/* Switch to the specified thread. Will dispatch off to thread_apply_command
1097 if prefix of arg is `apply'. */
1098
1099static void
fba45db2 1100thread_command (char *tidstr, int from_tty)
c906108c 1101{
c906108c
SS
1102 if (!tidstr)
1103 {
c906108c 1104 if (target_has_stack)
4f8d22e3
PA
1105 {
1106 if (is_exited (inferior_ptid))
1107 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1108 pid_to_thread_id (inferior_ptid),
1109 target_tid_to_str (inferior_ptid));
1110 else
1111 printf_filtered (_("[Current thread is %d (%s)]\n"),
1112 pid_to_thread_id (inferior_ptid),
1113 target_tid_to_str (inferior_ptid));
1114 }
c906108c 1115 else
8a3fe4f8 1116 error (_("No stack."));
c906108c
SS
1117 return;
1118 }
c5394b80 1119
b8fa951a 1120 annotate_thread_changed ();
ce43223b 1121 gdb_thread_select (uiout, tidstr, NULL);
c5394b80
JM
1122}
1123
93815fbf
VP
1124/* Print notices when new threads are attached and detached. */
1125int print_thread_events = 1;
1126static void
1127show_print_thread_events (struct ui_file *file, int from_tty,
1128 struct cmd_list_element *c, const char *value)
1129{
1130 fprintf_filtered (file, _("\
1131Printing of thread events is %s.\n"),
1132 value);
1133}
1134
c5394b80 1135static int
6949171e 1136do_captured_thread_select (struct ui_out *uiout, void *tidstr)
c5394b80
JM
1137{
1138 int num;
1139 struct thread_info *tp;
1140
81490ea1 1141 num = value_as_long (parse_and_eval (tidstr));
c906108c
SS
1142
1143 tp = find_thread_id (num);
1144
8b93c638 1145 if (!tp)
8a3fe4f8 1146 error (_("Thread ID %d not known."), num);
c906108c
SS
1147
1148 if (!thread_alive (tp))
8a3fe4f8 1149 error (_("Thread ID %d has terminated."), num);
c906108c 1150
94cc34af
PA
1151 if (non_stop)
1152 context_switch_to (tp->ptid);
1153 else
1154 switch_to_thread (tp->ptid);
c906108c 1155
8b93c638 1156 ui_out_text (uiout, "[Switching to thread ");
39f77062 1157 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
8b93c638 1158 ui_out_text (uiout, " (");
39f77062 1159 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
8b93c638 1160 ui_out_text (uiout, ")]");
c5394b80 1161
4f8d22e3
PA
1162 /* Note that we can't reach this with an exited thread, due to the
1163 thread_alive check above. */
1164 if (tp->state_ == THREAD_RUNNING)
94cc34af 1165 ui_out_text (uiout, "(running)\n");
4f8d22e3
PA
1166 else
1167 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1168
1169 /* Since the current thread may have changed, see if there is any
1170 exited thread we can now delete. */
1171 prune_threads ();
94cc34af 1172
c5394b80
JM
1173 return GDB_RC_OK;
1174}
1175
1176enum gdb_rc
ce43223b 1177gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
c5394b80 1178{
b0b13bb4
DJ
1179 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1180 error_message, RETURN_MASK_ALL) < 0)
1181 return GDB_RC_FAIL;
1182 return GDB_RC_OK;
c906108c
SS
1183}
1184
1185/* Commands with a prefix of `thread'. */
1186struct cmd_list_element *thread_cmd_list = NULL;
1187
1188void
fba45db2 1189_initialize_thread (void)
c906108c
SS
1190{
1191 static struct cmd_list_element *thread_apply_list = NULL;
94cc34af 1192 struct cmd_list_element *c;
c906108c 1193
94cc34af
PA
1194 c = add_info ("threads", info_threads_command,
1195 _("IDs of currently known threads."));
4f8d22e3 1196 set_cmd_no_selected_thread_ok (c);
c906108c 1197
94cc34af 1198 c = add_prefix_cmd ("thread", class_run, thread_command, _("\
1bedd215
AC
1199Use this command to switch between threads.\n\
1200The new thread ID must be currently known."),
94cc34af 1201 &thread_cmd_list, "thread ", 1, &cmdlist);
4f8d22e3 1202 set_cmd_no_selected_thread_ok (c);
c906108c 1203
4f8d22e3
PA
1204 c = add_prefix_cmd ("apply", class_run, thread_apply_command,
1205 _("Apply a command to a list of threads."),
1206 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
4f8d22e3 1207 set_cmd_no_selected_thread_ok (c);
c906108c 1208
4f8d22e3
PA
1209 c = add_cmd ("all", class_run, thread_apply_all_command,
1210 _("Apply a command to all threads."), &thread_apply_list);
4f8d22e3 1211 set_cmd_no_selected_thread_ok (c);
c906108c
SS
1212
1213 if (!xdb_commands)
1214 add_com_alias ("t", "thread", class_run, 1);
93815fbf
VP
1215
1216 add_setshow_boolean_cmd ("thread-events", no_class,
1217 &print_thread_events, _("\
11c68c47
EZ
1218Set printing of thread events (such as thread start and exit)."), _("\
1219Show printing of thread events (such as thread start and exit)."), NULL,
93815fbf
VP
1220 NULL,
1221 show_print_thread_events,
1222 &setprintlist, &showprintlist);
c906108c 1223}
This page took 0.886252 seconds and 4 git commands to generate.