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