2008-10-23 Paul Pluzhnikov <ppluzhnikov@google.com>
[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
4e1c45ea
PA
74extern struct thread_info*
75inferior_thread (void)
8601f500 76{
4e1c45ea
PA
77 struct thread_info *tp = find_thread_pid (inferior_ptid);
78 gdb_assert (tp);
79 return tp;
80}
8601f500 81
4e1c45ea
PA
82void
83delete_step_resume_breakpoint (struct thread_info *tp)
84{
85 if (tp && tp->step_resume_breakpoint)
8601f500 86 {
4e1c45ea
PA
87 delete_breakpoint (tp->step_resume_breakpoint);
88 tp->step_resume_breakpoint = NULL;
8601f500
MS
89 }
90}
91
7c952b6d 92static void
4f8d22e3 93clear_thread_inferior_resources (struct thread_info *tp)
7c952b6d
ND
94{
95 /* NOTE: this will take care of any left-over step_resume breakpoints,
4d8453a5
DJ
96 but not any user-specified thread-specific breakpoints. We can not
97 delete the breakpoint straight-off, because the inferior might not
98 be stopped at the moment. */
7c952b6d 99 if (tp->step_resume_breakpoint)
4f8d22e3
PA
100 {
101 tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
102 tp->step_resume_breakpoint = NULL;
103 }
7c952b6d 104
a474d7c2 105 bpstat_clear (&tp->stop_bpstat);
95e54da7
PA
106
107 discard_all_intermediate_continuations_thread (tp);
108 discard_all_continuations_thread (tp);
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;
8ea051c5 130
c906108c
SS
131 if (!thread_list)
132 return;
133
134 for (tp = thread_list; tp; tp = tpnext)
135 {
136 tpnext = tp->next;
7c952b6d 137 free_thread (tp);
c906108c
SS
138 }
139
140 thread_list = NULL;
c906108c
SS
141}
142
0d06e24b 143struct thread_info *
93815fbf 144add_thread_silent (ptid_t ptid)
c906108c
SS
145{
146 struct thread_info *tp;
147
4f8d22e3
PA
148 tp = find_thread_pid (ptid);
149 if (tp)
150 /* Found an old thread with the same id. It has to be dead,
151 otherwise we wouldn't be adding a new thread with the same id.
152 The OS is reusing this id --- delete it, and recreate a new
153 one. */
154 {
155 /* In addition to deleting the thread, if this is the current
dcf4fbde
PA
156 thread, then we need to take care that delete_thread doesn't
157 really delete the thread if it is inferior_ptid. Create a
158 new template thread in the list with an invalid ptid, switch
159 to it, delete the original thread, reset the new thread's
160 ptid, and switch to it. */
4f8d22e3
PA
161
162 if (ptid_equal (inferior_ptid, ptid))
163 {
164 tp = xmalloc (sizeof (*tp));
165 memset (tp, 0, sizeof (*tp));
166 tp->ptid = minus_one_ptid;
167 tp->num = ++highest_thread_num;
168 tp->next = thread_list;
169 thread_list = tp;
dcf4fbde
PA
170
171 /* Make switch_to_thread not read from the thread. */
172 tp->state_ = THREAD_EXITED;
173 switch_to_thread (minus_one_ptid);
4f8d22e3
PA
174
175 /* Now we can delete it. */
176 delete_thread (ptid);
177
dcf4fbde 178 /* Now reset its ptid, and reswitch inferior_ptid to it. */
4f8d22e3 179 tp->ptid = ptid;
dcf4fbde 180 tp->state_ = THREAD_STOPPED;
4f8d22e3
PA
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 441
c906108c
SS
442/* Return true if TP is an active thread. */
443static int
fba45db2 444thread_alive (struct thread_info *tp)
c906108c 445{
4f8d22e3 446 if (tp->state_ == THREAD_EXITED)
c906108c 447 return 0;
39f77062 448 if (!target_thread_alive (tp->ptid))
4f8d22e3 449 return 0;
c906108c
SS
450 return 1;
451}
452
453static void
fba45db2 454prune_threads (void)
c906108c 455{
d4f3574e 456 struct thread_info *tp, *next;
c906108c 457
c906108c
SS
458 for (tp = thread_list; tp; tp = next)
459 {
460 next = tp->next;
461 if (!thread_alive (tp))
39f77062 462 delete_thread (tp->ptid);
c906108c
SS
463 }
464}
465
5231c1fd
PA
466void
467thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
468{
82f73884
PA
469 struct inferior *inf;
470 struct thread_info *tp;
471
472 /* It can happen that what we knew as the target inferior id
473 changes. E.g, target remote may only discover the remote process
474 pid after adding the inferior to GDB's list. */
475 inf = find_inferior_pid (ptid_get_pid (old_ptid));
476 inf->pid = ptid_get_pid (new_ptid);
477
478 tp = find_thread_pid (old_ptid);
5231c1fd
PA
479 tp->ptid = new_ptid;
480
481 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
482}
483
e1ac3328
VP
484void
485set_running (ptid_t ptid, int running)
486{
487 struct thread_info *tp;
488
e1ac3328
VP
489 /* We try not to notify the observer if no thread has actually changed
490 the running state -- merely to reduce the number of messages to
491 frontend. Frontend is supposed to handle multiple *running just fine. */
492 if (PIDGET (ptid) == -1)
493 {
494 int any_started = 0;
495 for (tp = thread_list; tp; tp = tp->next)
496 {
4f8d22e3
PA
497 if (tp->state_ == THREAD_EXITED)
498 continue;
499 if (running && tp->state_ == THREAD_STOPPED)
500 any_started = 1;
501 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
e1ac3328 502 }
8f6a8e84 503 if (any_started && !suppress_resume_observer)
e1ac3328
VP
504 observer_notify_target_resumed (ptid);
505 }
506 else
507 {
4f8d22e3 508 int started = 0;
e1ac3328
VP
509 tp = find_thread_pid (ptid);
510 gdb_assert (tp);
4f8d22e3
PA
511 gdb_assert (tp->state_ != THREAD_EXITED);
512 if (running && tp->state_ == THREAD_STOPPED)
513 started = 1;
514 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
515 if (started && !suppress_resume_observer)
516 observer_notify_target_resumed (ptid);
517 }
e1ac3328
VP
518}
519
4f8d22e3
PA
520static int
521is_thread_state (ptid_t ptid, enum thread_state state)
e1ac3328
VP
522{
523 struct thread_info *tp;
524
8ea051c5
PA
525 if (!target_has_execution)
526 return 0;
527
e1ac3328
VP
528 tp = find_thread_pid (ptid);
529 gdb_assert (tp);
4f8d22e3
PA
530 return tp->state_ == state;
531}
532
533int
534is_stopped (ptid_t ptid)
535{
536 /* Without execution, this property is always true. */
537 if (!target_has_execution)
538 return 1;
539
540 return is_thread_state (ptid, THREAD_STOPPED);
541}
542
543int
544is_exited (ptid_t ptid)
545{
546 /* Without execution, this property is always false. */
547 if (!target_has_execution)
548 return 0;
549
550 return is_thread_state (ptid, THREAD_EXITED);
551}
552
553int
554is_running (ptid_t ptid)
555{
556 /* Without execution, this property is always false. */
557 if (!target_has_execution)
558 return 0;
559
560 return is_thread_state (ptid, THREAD_RUNNING);
e1ac3328
VP
561}
562
8ea051c5
PA
563int
564any_running (void)
565{
566 struct thread_info *tp;
567
568 if (!target_has_execution)
569 return 0;
570
8ea051c5 571 for (tp = thread_list; tp; tp = tp->next)
4f8d22e3 572 if (tp->state_ == THREAD_RUNNING)
8ea051c5
PA
573 return 1;
574
575 return 0;
576}
577
578int
579is_executing (ptid_t ptid)
580{
581 struct thread_info *tp;
582
583 if (!target_has_execution)
584 return 0;
585
8ea051c5
PA
586 tp = find_thread_pid (ptid);
587 gdb_assert (tp);
588 return tp->executing_;
589}
590
591void
592set_executing (ptid_t ptid, int executing)
593{
594 struct thread_info *tp;
595
8ea051c5
PA
596 if (PIDGET (ptid) == -1)
597 {
598 for (tp = thread_list; tp; tp = tp->next)
599 tp->executing_ = executing;
600 }
601 else
602 {
603 tp = find_thread_pid (ptid);
604 gdb_assert (tp);
605 tp->executing_ = executing;
606 }
607}
608
8e8901c5
VP
609/* Prints the list of threads and their details on UIOUT.
610 This is a version of 'info_thread_command' suitable for
611 use from MI.
4f8d22e3 612 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
8e8901c5
VP
613 that should be printed. Otherwise, all threads are
614 printed. */
615void
616print_thread_info (struct ui_out *uiout, int requested_thread)
c906108c
SS
617{
618 struct thread_info *tp;
39f77062 619 ptid_t current_ptid;
99b3d574 620 struct cleanup *old_chain;
0d06e24b 621 char *extra_info;
8e8901c5 622 int current_thread = -1;
c906108c 623
c906108c 624 prune_threads ();
b83266a0 625 target_find_new_threads ();
39f77062 626 current_ptid = inferior_ptid;
4f8d22e3
PA
627
628 /* We'll be switching threads temporarily. */
629 old_chain = make_cleanup_restore_current_thread ();
630
631 make_cleanup_ui_out_list_begin_end (uiout, "threads");
c906108c
SS
632 for (tp = thread_list; tp; tp = tp->next)
633 {
8e8901c5
VP
634 struct cleanup *chain2;
635
636 if (requested_thread != -1 && tp->num != requested_thread)
637 continue;
638
4f8d22e3
PA
639 if (ptid_equal (tp->ptid, current_ptid))
640 current_thread = tp->num;
641
642 if (tp->state_ == THREAD_EXITED)
643 continue;
644
8e8901c5
VP
645 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
646
39f77062 647 if (ptid_equal (tp->ptid, current_ptid))
4f8d22e3 648 ui_out_text (uiout, "* ");
c906108c 649 else
8e8901c5 650 ui_out_text (uiout, " ");
c906108c 651
8e8901c5
VP
652 ui_out_field_int (uiout, "id", tp->num);
653 ui_out_text (uiout, " ");
654 ui_out_field_string (uiout, "target-id", target_tid_to_str (tp->ptid));
0d06e24b 655
4f8d22e3 656 if (tp->state_ != THREAD_EXITED)
8e8901c5 657 {
4f8d22e3
PA
658 extra_info = target_extra_thread_info (tp);
659 if (extra_info)
660 {
661 ui_out_text (uiout, " (");
662 ui_out_field_string (uiout, "details", extra_info);
663 ui_out_text (uiout, ")");
664 }
665 ui_out_text (uiout, " ");
8e8901c5 666 }
4f8d22e3
PA
667
668 if (tp->state_ == THREAD_RUNNING)
94cc34af
PA
669 ui_out_text (uiout, "(running)\n");
670 else
671 {
672 /* The switch below puts us at the top of the stack (leaf
673 frame). */
674 switch_to_thread (tp->ptid);
675 print_stack_frame (get_selected_frame (NULL),
676 /* For MI output, print frame level. */
677 ui_out_is_mi_like_p (uiout),
678 LOCATION);
679 }
8e8901c5 680
90139f7d
VP
681 if (ui_out_is_mi_like_p (uiout))
682 {
683 char *state = "stopped";
684 if (tp->state_ == THREAD_EXITED)
685 state = "exited";
686 else if (tp->state_ == THREAD_RUNNING)
687 state = "running";
688 ui_out_field_string (uiout, "state", state);
689 }
690
8e8901c5 691 do_cleanups (chain2);
c906108c
SS
692 }
693
99b3d574
DP
694 /* Restores the current thread and the frame selected before
695 the "info threads" command. */
696 do_cleanups (old_chain);
c906108c 697
8e8901c5
VP
698 if (requested_thread == -1)
699 {
4f8d22e3
PA
700 gdb_assert (current_thread != -1
701 || !thread_list);
0bcd3e20 702 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
8e8901c5 703 ui_out_field_int (uiout, "current-thread-id", current_thread);
94cc34af 704
4f8d22e3
PA
705 if (current_thread != -1 && is_exited (current_ptid))
706 ui_out_message (uiout, 0, "\n\
707The current thread <Thread ID %d> has terminated. See `help thread'.\n",
708 current_thread);
c906108c 709 }
c906108c
SS
710}
711
8e8901c5
VP
712
713/* Print information about currently known threads
714
715 * Note: this has the drawback that it _really_ switches
716 * threads, which frees the frame cache. A no-side
717 * effects info-threads command would be nicer.
718 */
719
720static void
721info_threads_command (char *arg, int from_tty)
722{
723 print_thread_info (uiout, -1);
724}
725
c906108c
SS
726/* Switch from one thread to another. */
727
6a6b96b9 728void
39f77062 729switch_to_thread (ptid_t ptid)
c906108c 730{
39f77062 731 if (ptid_equal (ptid, inferior_ptid))
c906108c
SS
732 return;
733
39f77062 734 inferior_ptid = ptid;
35f196d9 735 reinit_frame_cache ();
c906108c 736 registers_changed ();
94cc34af 737
4f8d22e3
PA
738 /* We don't check for is_stopped, because we're called at times
739 while in the TARGET_RUNNING state, e.g., while handling an
740 internal event. */
741 if (!is_exited (ptid) && !is_executing (ptid))
94cc34af
PA
742 stop_pc = read_pc ();
743 else
744 stop_pc = ~(CORE_ADDR) 0;
c906108c
SS
745}
746
747static void
39f77062 748restore_current_thread (ptid_t ptid)
c906108c 749{
dcf4fbde 750 switch_to_thread (ptid);
99b3d574
DP
751}
752
753static void
4f8d22e3 754restore_selected_frame (struct frame_id a_frame_id, int frame_level)
99b3d574 755{
4f8d22e3
PA
756 struct frame_info *frame = NULL;
757 int count;
758
759 gdb_assert (frame_level >= 0);
760
761 /* Restore by level first, check if the frame id is the same as
762 expected. If that fails, try restoring by frame id. If that
763 fails, nothing to do, just warn the user. */
764
765 count = frame_level;
766 frame = find_relative_frame (get_current_frame (), &count);
767 if (count == 0
768 && frame != NULL
769 /* Either the frame ids match, of they're both invalid. The
770 latter case is not failsafe, but since it's highly unlikely
771 the search by level finds the wrong frame, it's 99.9(9)% of
772 the time (for all practical purposes) safe. */
773 && (frame_id_eq (get_frame_id (frame), a_frame_id)
774 /* Note: could be better to check every frame_id
775 member for equality here. */
776 || (!frame_id_p (get_frame_id (frame))
777 && !frame_id_p (a_frame_id))))
778 {
779 /* Cool, all is fine. */
780 select_frame (frame);
781 return;
782 }
99b3d574 783
4f8d22e3
PA
784 frame = frame_find_by_id (a_frame_id);
785 if (frame != NULL)
786 {
787 /* Cool, refound it. */
788 select_frame (frame);
789 return;
790 }
99b3d574 791
0c501536
PA
792 /* Nothing else to do, the frame layout really changed. Select the
793 innermost stack frame. */
794 select_frame (get_current_frame ());
795
796 /* Warn the user. */
4f8d22e3 797 if (!ui_out_is_mi_like_p (uiout))
99b3d574 798 {
4f8d22e3
PA
799 warning (_("\
800Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
801 frame_level);
802 /* For MI, we should probably have a notification about
803 current frame change. But this error is not very
804 likely, so don't bother for now. */
0c501536 805 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
c906108c
SS
806 }
807}
808
6ecce94d
AC
809struct current_thread_cleanup
810{
39f77062 811 ptid_t inferior_ptid;
99b3d574 812 struct frame_id selected_frame_id;
4f8d22e3
PA
813 int selected_frame_level;
814 int was_stopped;
6ecce94d
AC
815};
816
817static void
818do_restore_current_thread_cleanup (void *arg)
819{
4f8d22e3 820 struct thread_info *tp;
6ecce94d 821 struct current_thread_cleanup *old = arg;
39f77062 822 restore_current_thread (old->inferior_ptid);
94cc34af 823
4f8d22e3
PA
824 /* The running state of the originally selected thread may have
825 changed, so we have to recheck it here. */
826 if (old->was_stopped
827 && is_stopped (inferior_ptid)
828 && target_has_registers
829 && target_has_stack
830 && target_has_memory)
831 restore_selected_frame (old->selected_frame_id,
832 old->selected_frame_level);
833}
834
835static void
836restore_current_thread_cleanup_dtor (void *arg)
837{
838 struct current_thread_cleanup *old = arg;
839 struct thread_info *tp;
840 tp = find_thread_pid (old->inferior_ptid);
841 if (tp)
842 tp->refcount--;
b8c9b27d 843 xfree (old);
6ecce94d
AC
844}
845
6208b47d 846struct cleanup *
4f8d22e3 847make_cleanup_restore_current_thread (void)
6ecce94d 848{
4f8d22e3
PA
849 struct thread_info *tp;
850 struct frame_info *frame;
851 struct current_thread_cleanup *old;
852
853 old = xmalloc (sizeof (struct current_thread_cleanup));
39f77062 854 old->inferior_ptid = inferior_ptid;
4f8d22e3
PA
855 old->was_stopped = is_stopped (inferior_ptid);
856 if (old->was_stopped
857 && target_has_registers
858 && target_has_stack
859 && target_has_memory)
860 frame = get_selected_frame (NULL);
861 else
862 frame = NULL;
863
864 old->selected_frame_id = get_frame_id (frame);
865 old->selected_frame_level = frame_relative_level (frame);
866
867 tp = find_thread_pid (inferior_ptid);
868 if (tp)
869 tp->refcount++;
870
871 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
872 restore_current_thread_cleanup_dtor);
6ecce94d
AC
873}
874
c906108c
SS
875/* Apply a GDB command to a list of threads. List syntax is a whitespace
876 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
877 of two numbers seperated by a hyphen. Examples:
878
c5aa993b
JM
879 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
880 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
881 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
882 */
c906108c
SS
883
884static void
fba45db2 885thread_apply_all_command (char *cmd, int from_tty)
c906108c
SS
886{
887 struct thread_info *tp;
4f8d22e3 888 struct cleanup *old_chain;
e35ce267 889 char *saved_cmd;
c906108c
SS
890
891 if (cmd == NULL || *cmd == '\000')
8a3fe4f8 892 error (_("Please specify a command following the thread ID list"));
94cc34af 893
4f8d22e3 894 prune_threads ();
e9d196c5
MS
895 target_find_new_threads ();
896
4f8d22e3
PA
897 old_chain = make_cleanup_restore_current_thread ();
898
e35ce267
CF
899 /* Save a copy of the command in case it is clobbered by
900 execute_command */
5b616ba1 901 saved_cmd = xstrdup (cmd);
94cc34af 902 make_cleanup (xfree, saved_cmd);
c906108c
SS
903 for (tp = thread_list; tp; tp = tp->next)
904 if (thread_alive (tp))
905 {
dcf4fbde 906 switch_to_thread (tp->ptid);
94cc34af 907
a3f17187 908 printf_filtered (_("\nThread %d (%s):\n"),
6949171e 909 tp->num, target_tid_to_str (inferior_ptid));
c906108c 910 execute_command (cmd, from_tty);
6949171e 911 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
c906108c 912 }
6ecce94d
AC
913
914 do_cleanups (old_chain);
c906108c
SS
915}
916
917static void
fba45db2 918thread_apply_command (char *tidlist, int from_tty)
c906108c
SS
919{
920 char *cmd;
921 char *p;
922 struct cleanup *old_chain;
e35ce267 923 char *saved_cmd;
c906108c
SS
924
925 if (tidlist == NULL || *tidlist == '\000')
8a3fe4f8 926 error (_("Please specify a thread ID list"));
c906108c 927
c5aa993b 928 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
c906108c
SS
929
930 if (*cmd == '\000')
8a3fe4f8 931 error (_("Please specify a command following the thread ID list"));
c906108c 932
e35ce267
CF
933 /* Save a copy of the command in case it is clobbered by
934 execute_command */
5b616ba1 935 saved_cmd = xstrdup (cmd);
4f8d22e3 936 old_chain = make_cleanup (xfree, saved_cmd);
c906108c
SS
937 while (tidlist < cmd)
938 {
939 struct thread_info *tp;
940 int start, end;
941
942 start = strtol (tidlist, &p, 10);
943 if (p == tidlist)
8a3fe4f8 944 error (_("Error parsing %s"), tidlist);
c906108c
SS
945 tidlist = p;
946
947 while (*tidlist == ' ' || *tidlist == '\t')
948 tidlist++;
949
950 if (*tidlist == '-') /* Got a range of IDs? */
951 {
c5aa993b 952 tidlist++; /* Skip the - */
c906108c
SS
953 end = strtol (tidlist, &p, 10);
954 if (p == tidlist)
8a3fe4f8 955 error (_("Error parsing %s"), tidlist);
c906108c
SS
956 tidlist = p;
957
958 while (*tidlist == ' ' || *tidlist == '\t')
959 tidlist++;
960 }
961 else
962 end = start;
963
65fc9b77
PA
964 make_cleanup_restore_current_thread ();
965
c906108c
SS
966 for (; start <= end; start++)
967 {
968 tp = find_thread_id (start);
969
970 if (!tp)
8a3fe4f8 971 warning (_("Unknown thread %d."), start);
c906108c 972 else if (!thread_alive (tp))
8a3fe4f8 973 warning (_("Thread %d has terminated."), start);
c906108c
SS
974 else
975 {
dcf4fbde 976 switch_to_thread (tp->ptid);
4f8d22e3 977
a3f17187 978 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
39f77062 979 target_tid_to_str (inferior_ptid));
c906108c 980 execute_command (cmd, from_tty);
4f8d22e3
PA
981
982 /* Restore exact command used previously. */
983 strcpy (cmd, saved_cmd);
c906108c
SS
984 }
985 }
986 }
6ecce94d
AC
987
988 do_cleanups (old_chain);
c906108c
SS
989}
990
991/* Switch to the specified thread. Will dispatch off to thread_apply_command
992 if prefix of arg is `apply'. */
993
994static void
fba45db2 995thread_command (char *tidstr, int from_tty)
c906108c 996{
c906108c
SS
997 if (!tidstr)
998 {
c906108c 999 if (target_has_stack)
4f8d22e3
PA
1000 {
1001 if (is_exited (inferior_ptid))
1002 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1003 pid_to_thread_id (inferior_ptid),
1004 target_tid_to_str (inferior_ptid));
1005 else
1006 printf_filtered (_("[Current thread is %d (%s)]\n"),
1007 pid_to_thread_id (inferior_ptid),
1008 target_tid_to_str (inferior_ptid));
1009 }
c906108c 1010 else
8a3fe4f8 1011 error (_("No stack."));
c906108c
SS
1012 return;
1013 }
c5394b80 1014
b8fa951a 1015 annotate_thread_changed ();
ce43223b 1016 gdb_thread_select (uiout, tidstr, NULL);
c5394b80
JM
1017}
1018
93815fbf
VP
1019/* Print notices when new threads are attached and detached. */
1020int print_thread_events = 1;
1021static void
1022show_print_thread_events (struct ui_file *file, int from_tty,
1023 struct cmd_list_element *c, const char *value)
1024{
1025 fprintf_filtered (file, _("\
1026Printing of thread events is %s.\n"),
1027 value);
1028}
1029
c5394b80 1030static int
6949171e 1031do_captured_thread_select (struct ui_out *uiout, void *tidstr)
c5394b80
JM
1032{
1033 int num;
1034 struct thread_info *tp;
1035
81490ea1 1036 num = value_as_long (parse_and_eval (tidstr));
c906108c
SS
1037
1038 tp = find_thread_id (num);
1039
8b93c638 1040 if (!tp)
8a3fe4f8 1041 error (_("Thread ID %d not known."), num);
c906108c
SS
1042
1043 if (!thread_alive (tp))
8a3fe4f8 1044 error (_("Thread ID %d has terminated."), num);
c906108c 1045
dcf4fbde 1046 switch_to_thread (tp->ptid);
c906108c 1047
8b93c638 1048 ui_out_text (uiout, "[Switching to thread ");
39f77062 1049 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
8b93c638 1050 ui_out_text (uiout, " (");
39f77062 1051 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
8b93c638 1052 ui_out_text (uiout, ")]");
c5394b80 1053
4f8d22e3
PA
1054 /* Note that we can't reach this with an exited thread, due to the
1055 thread_alive check above. */
1056 if (tp->state_ == THREAD_RUNNING)
94cc34af 1057 ui_out_text (uiout, "(running)\n");
4f8d22e3
PA
1058 else
1059 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1060
1061 /* Since the current thread may have changed, see if there is any
1062 exited thread we can now delete. */
1063 prune_threads ();
94cc34af 1064
c5394b80
JM
1065 return GDB_RC_OK;
1066}
1067
1068enum gdb_rc
ce43223b 1069gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
c5394b80 1070{
b0b13bb4
DJ
1071 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1072 error_message, RETURN_MASK_ALL) < 0)
1073 return GDB_RC_FAIL;
1074 return GDB_RC_OK;
c906108c
SS
1075}
1076
1077/* Commands with a prefix of `thread'. */
1078struct cmd_list_element *thread_cmd_list = NULL;
1079
1080void
fba45db2 1081_initialize_thread (void)
c906108c
SS
1082{
1083 static struct cmd_list_element *thread_apply_list = NULL;
94cc34af 1084 struct cmd_list_element *c;
c906108c 1085
94cc34af
PA
1086 c = add_info ("threads", info_threads_command,
1087 _("IDs of currently known threads."));
4f8d22e3 1088 set_cmd_no_selected_thread_ok (c);
c906108c 1089
94cc34af 1090 c = add_prefix_cmd ("thread", class_run, thread_command, _("\
1bedd215
AC
1091Use this command to switch between threads.\n\
1092The new thread ID must be currently known."),
94cc34af 1093 &thread_cmd_list, "thread ", 1, &cmdlist);
4f8d22e3 1094 set_cmd_no_selected_thread_ok (c);
c906108c 1095
4f8d22e3
PA
1096 c = add_prefix_cmd ("apply", class_run, thread_apply_command,
1097 _("Apply a command to a list of threads."),
1098 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
4f8d22e3 1099 set_cmd_no_selected_thread_ok (c);
c906108c 1100
4f8d22e3
PA
1101 c = add_cmd ("all", class_run, thread_apply_all_command,
1102 _("Apply a command to all threads."), &thread_apply_list);
4f8d22e3 1103 set_cmd_no_selected_thread_ok (c);
c906108c
SS
1104
1105 if (!xdb_commands)
1106 add_com_alias ("t", "thread", class_run, 1);
93815fbf
VP
1107
1108 add_setshow_boolean_cmd ("thread-events", no_class,
1109 &print_thread_events, _("\
11c68c47
EZ
1110Set printing of thread events (such as thread start and exit)."), _("\
1111Show printing of thread events (such as thread start and exit)."), NULL,
93815fbf
VP
1112 NULL,
1113 show_print_thread_events,
1114 &setprintlist, &showprintlist);
c906108c 1115}
This page took 0.916129 seconds and 4 git commands to generate.