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