2009-03-26 H.J. Lu <hongjiu.lu@intel.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,
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
a5321aa4 75struct thread_info*
4e1c45ea 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 {
a07daef3 250 observer_notify_thread_exit (tp, silent);
4f8d22e3
PA
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 268 /* Notify thread exit, but only if we haven't already. */
a07daef3
PA
269 if (tp->state_ != THREAD_EXITED)
270 observer_notify_thread_exit (tp, silent);
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;
592375cd 412 int current_thread = -1;
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;
592375cd
VP
423
424 if (ptid_equal (tp->ptid, inferior_ptid))
425 current_thread = tp->num;
426
8b93c638
JM
427 num++;
428 ui_out_field_int (uiout, "thread-id", tp->num);
429 }
430
3b31d625 431 do_cleanups (cleanup_chain);
592375cd
VP
432
433 if (current_thread != -1)
434 ui_out_field_int (uiout, "current-thread-id", current_thread);
8b93c638
JM
435 ui_out_field_int (uiout, "number-of-threads", num);
436 return GDB_RC_OK;
437}
438
439/* Official gdblib interface function to get a list of thread ids and
440 the total number. */
441enum gdb_rc
ce43223b 442gdb_list_thread_ids (struct ui_out *uiout, char **error_message)
8b93c638 443{
b0b13bb4
DJ
444 if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
445 error_message, RETURN_MASK_ALL) < 0)
446 return GDB_RC_FAIL;
447 return GDB_RC_OK;
8b93c638 448}
c906108c 449
c906108c
SS
450/* Return true if TP is an active thread. */
451static int
fba45db2 452thread_alive (struct thread_info *tp)
c906108c 453{
4f8d22e3 454 if (tp->state_ == THREAD_EXITED)
c906108c 455 return 0;
39f77062 456 if (!target_thread_alive (tp->ptid))
4f8d22e3 457 return 0;
c906108c
SS
458 return 1;
459}
460
461static void
fba45db2 462prune_threads (void)
c906108c 463{
d4f3574e 464 struct thread_info *tp, *next;
c906108c 465
c906108c
SS
466 for (tp = thread_list; tp; tp = next)
467 {
468 next = tp->next;
469 if (!thread_alive (tp))
39f77062 470 delete_thread (tp->ptid);
c906108c
SS
471 }
472}
473
5231c1fd
PA
474void
475thread_change_ptid (ptid_t old_ptid, ptid_t new_ptid)
476{
82f73884
PA
477 struct inferior *inf;
478 struct thread_info *tp;
479
480 /* It can happen that what we knew as the target inferior id
481 changes. E.g, target remote may only discover the remote process
482 pid after adding the inferior to GDB's list. */
483 inf = find_inferior_pid (ptid_get_pid (old_ptid));
484 inf->pid = ptid_get_pid (new_ptid);
485
486 tp = find_thread_pid (old_ptid);
5231c1fd
PA
487 tp->ptid = new_ptid;
488
489 observer_notify_thread_ptid_changed (old_ptid, new_ptid);
490}
491
e1ac3328
VP
492void
493set_running (ptid_t ptid, int running)
494{
495 struct thread_info *tp;
496
e1ac3328
VP
497 /* We try not to notify the observer if no thread has actually changed
498 the running state -- merely to reduce the number of messages to
499 frontend. Frontend is supposed to handle multiple *running just fine. */
500 if (PIDGET (ptid) == -1)
501 {
502 int any_started = 0;
503 for (tp = thread_list; tp; tp = tp->next)
504 {
4f8d22e3
PA
505 if (tp->state_ == THREAD_EXITED)
506 continue;
507 if (running && tp->state_ == THREAD_STOPPED)
508 any_started = 1;
509 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
e1ac3328 510 }
c5a4d20b
PA
511 if (any_started)
512 observer_notify_target_resumed (ptid);
e1ac3328
VP
513 }
514 else
515 {
4f8d22e3 516 int started = 0;
e1ac3328
VP
517 tp = find_thread_pid (ptid);
518 gdb_assert (tp);
4f8d22e3
PA
519 gdb_assert (tp->state_ != THREAD_EXITED);
520 if (running && tp->state_ == THREAD_STOPPED)
521 started = 1;
522 tp->state_ = running ? THREAD_RUNNING : THREAD_STOPPED;
c5a4d20b 523 if (started)
4f8d22e3
PA
524 observer_notify_target_resumed (ptid);
525 }
e1ac3328
VP
526}
527
4f8d22e3
PA
528static int
529is_thread_state (ptid_t ptid, enum thread_state state)
e1ac3328
VP
530{
531 struct thread_info *tp;
532
8ea051c5
PA
533 if (!target_has_execution)
534 return 0;
535
e1ac3328
VP
536 tp = find_thread_pid (ptid);
537 gdb_assert (tp);
4f8d22e3
PA
538 return tp->state_ == state;
539}
540
541int
542is_stopped (ptid_t ptid)
543{
544 /* Without execution, this property is always true. */
545 if (!target_has_execution)
546 return 1;
547
548 return is_thread_state (ptid, THREAD_STOPPED);
549}
550
551int
552is_exited (ptid_t ptid)
553{
554 /* Without execution, this property is always false. */
555 if (!target_has_execution)
556 return 0;
557
558 return is_thread_state (ptid, THREAD_EXITED);
559}
560
561int
562is_running (ptid_t ptid)
563{
564 /* Without execution, this property is always false. */
565 if (!target_has_execution)
566 return 0;
567
568 return is_thread_state (ptid, THREAD_RUNNING);
e1ac3328
VP
569}
570
8ea051c5
PA
571int
572any_running (void)
573{
574 struct thread_info *tp;
575
576 if (!target_has_execution)
577 return 0;
578
8ea051c5 579 for (tp = thread_list; tp; tp = tp->next)
4f8d22e3 580 if (tp->state_ == THREAD_RUNNING)
8ea051c5
PA
581 return 1;
582
583 return 0;
584}
585
586int
587is_executing (ptid_t ptid)
588{
589 struct thread_info *tp;
590
591 if (!target_has_execution)
592 return 0;
593
8ea051c5
PA
594 tp = find_thread_pid (ptid);
595 gdb_assert (tp);
596 return tp->executing_;
597}
598
599void
600set_executing (ptid_t ptid, int executing)
601{
602 struct thread_info *tp;
603
8ea051c5
PA
604 if (PIDGET (ptid) == -1)
605 {
606 for (tp = thread_list; tp; tp = tp->next)
607 tp->executing_ = executing;
608 }
609 else
610 {
611 tp = find_thread_pid (ptid);
612 gdb_assert (tp);
613 tp->executing_ = executing;
614 }
615}
616
252fbfc8
PA
617void
618set_stop_requested (ptid_t ptid, int stop)
619{
620 struct thread_info *tp;
621 int all = ptid_equal (ptid, minus_one_ptid);
622
623 if (all || ptid_is_pid (ptid))
624 {
625 for (tp = thread_list; tp; tp = tp->next)
626 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
627 tp->stop_requested = stop;
628 }
629 else
630 {
631 tp = find_thread_pid (ptid);
632 gdb_assert (tp);
633 tp->stop_requested = stop;
634 }
635
636 /* Call the stop requested observer so other components of GDB can
637 react to this request. */
638 if (stop)
639 observer_notify_thread_stop_requested (ptid);
640}
641
29f49a6a
PA
642void
643finish_thread_state (ptid_t ptid)
644{
645 struct thread_info *tp;
646 int all;
647 int any_started = 0;
648
649 all = ptid_equal (ptid, minus_one_ptid);
650
651 if (all || ptid_is_pid (ptid))
652 {
653 for (tp = thread_list; tp; tp = tp->next)
654 {
655 if (tp->state_ == THREAD_EXITED)
656 continue;
657 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
658 {
659 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
660 any_started = 1;
661 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
662 }
663 }
664 }
665 else
666 {
667 tp = find_thread_pid (ptid);
668 gdb_assert (tp);
669 if (tp->state_ != THREAD_EXITED)
670 {
671 if (tp->executing_ && tp->state_ == THREAD_STOPPED)
672 any_started = 1;
673 tp->state_ = tp->executing_ ? THREAD_RUNNING : THREAD_STOPPED;
674 }
675 }
676
677 if (any_started)
678 observer_notify_target_resumed (ptid);
679}
680
681void
682finish_thread_state_cleanup (void *arg)
683{
684 ptid_t *ptid_p = arg;
685
686 gdb_assert (arg);
687
688 finish_thread_state (*ptid_p);
689}
690
8e8901c5
VP
691/* Prints the list of threads and their details on UIOUT.
692 This is a version of 'info_thread_command' suitable for
693 use from MI.
4f8d22e3 694 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
8e8901c5 695 that should be printed. Otherwise, all threads are
3ee1c036
VP
696 printed.
697 If PID is not -1, only print threads from the process PID.
698 Otherwise, threads from all attached PIDs are printed.
699 If both REQUESTED_THREAD and PID are not -1, then the thread
700 is printed if it belongs to the specified process. Otherwise,
701 an error is raised. */
8e8901c5 702void
3ee1c036 703print_thread_info (struct ui_out *uiout, int requested_thread, int pid)
c906108c
SS
704{
705 struct thread_info *tp;
39f77062 706 ptid_t current_ptid;
99b3d574 707 struct cleanup *old_chain;
0d06e24b 708 char *extra_info;
8e8901c5 709 int current_thread = -1;
c906108c 710
c906108c 711 prune_threads ();
b83266a0 712 target_find_new_threads ();
39f77062 713 current_ptid = inferior_ptid;
4f8d22e3
PA
714
715 /* We'll be switching threads temporarily. */
716 old_chain = make_cleanup_restore_current_thread ();
717
718 make_cleanup_ui_out_list_begin_end (uiout, "threads");
c906108c
SS
719 for (tp = thread_list; tp; tp = tp->next)
720 {
8e8901c5
VP
721 struct cleanup *chain2;
722
723 if (requested_thread != -1 && tp->num != requested_thread)
724 continue;
725
3ee1c036
VP
726 if (pid != -1 && PIDGET (tp->ptid) != pid)
727 {
728 if (requested_thread != -1)
729 error (_("Requested thread not found in requested process"));
730 continue;
731 }
732
4f8d22e3
PA
733 if (ptid_equal (tp->ptid, current_ptid))
734 current_thread = tp->num;
735
736 if (tp->state_ == THREAD_EXITED)
737 continue;
738
8e8901c5
VP
739 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
740
39f77062 741 if (ptid_equal (tp->ptid, current_ptid))
4f8d22e3 742 ui_out_text (uiout, "* ");
c906108c 743 else
8e8901c5 744 ui_out_text (uiout, " ");
c906108c 745
8e8901c5
VP
746 ui_out_field_int (uiout, "id", tp->num);
747 ui_out_text (uiout, " ");
54ba13f7 748 ui_out_field_string (uiout, "target-id", target_pid_to_str (tp->ptid));
0d06e24b 749
ed406532
VP
750 extra_info = target_extra_thread_info (tp);
751 if (extra_info)
8e8901c5 752 {
ed406532
VP
753 ui_out_text (uiout, " (");
754 ui_out_field_string (uiout, "details", extra_info);
755 ui_out_text (uiout, ")");
8e8901c5 756 }
ed406532 757 ui_out_text (uiout, " ");
4f8d22e3
PA
758
759 if (tp->state_ == THREAD_RUNNING)
94cc34af
PA
760 ui_out_text (uiout, "(running)\n");
761 else
762 {
763 /* The switch below puts us at the top of the stack (leaf
764 frame). */
765 switch_to_thread (tp->ptid);
766 print_stack_frame (get_selected_frame (NULL),
767 /* For MI output, print frame level. */
768 ui_out_is_mi_like_p (uiout),
769 LOCATION);
770 }
8e8901c5 771
90139f7d
VP
772 if (ui_out_is_mi_like_p (uiout))
773 {
774 char *state = "stopped";
ed406532 775 if (tp->state_ == THREAD_RUNNING)
90139f7d
VP
776 state = "running";
777 ui_out_field_string (uiout, "state", state);
778 }
779
8e8901c5 780 do_cleanups (chain2);
c906108c
SS
781 }
782
99b3d574
DP
783 /* Restores the current thread and the frame selected before
784 the "info threads" command. */
785 do_cleanups (old_chain);
c906108c 786
d729566a 787 if (pid == -1 && requested_thread == -1)
8e8901c5 788 {
4f8d22e3 789 gdb_assert (current_thread != -1
d729566a
PA
790 || !thread_list
791 || ptid_equal (inferior_ptid, null_ptid));
0bcd3e20 792 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
8e8901c5 793 ui_out_field_int (uiout, "current-thread-id", current_thread);
94cc34af 794
4f8d22e3
PA
795 if (current_thread != -1 && is_exited (current_ptid))
796 ui_out_message (uiout, 0, "\n\
797The current thread <Thread ID %d> has terminated. See `help thread'.\n",
798 current_thread);
d729566a
PA
799 else if (thread_list
800 && current_thread == -1
801 && ptid_equal (current_ptid, null_ptid))
802 ui_out_message (uiout, 0, "\n\
803No selected thread. See `help thread'.\n");
c906108c 804 }
c906108c
SS
805}
806
8e8901c5
VP
807
808/* Print information about currently known threads
809
810 * Note: this has the drawback that it _really_ switches
811 * threads, which frees the frame cache. A no-side
812 * effects info-threads command would be nicer.
813 */
814
815static void
816info_threads_command (char *arg, int from_tty)
817{
3ee1c036 818 print_thread_info (uiout, -1, -1);
8e8901c5
VP
819}
820
c906108c
SS
821/* Switch from one thread to another. */
822
6a6b96b9 823void
39f77062 824switch_to_thread (ptid_t ptid)
c906108c 825{
39f77062 826 if (ptid_equal (ptid, inferior_ptid))
c906108c
SS
827 return;
828
39f77062 829 inferior_ptid = ptid;
35f196d9 830 reinit_frame_cache ();
c906108c 831 registers_changed ();
94cc34af 832
4f8d22e3
PA
833 /* We don't check for is_stopped, because we're called at times
834 while in the TARGET_RUNNING state, e.g., while handling an
835 internal event. */
d729566a
PA
836 if (!ptid_equal (inferior_ptid, null_ptid)
837 && !is_exited (ptid)
838 && !is_executing (ptid))
94cc34af
PA
839 stop_pc = read_pc ();
840 else
841 stop_pc = ~(CORE_ADDR) 0;
c906108c
SS
842}
843
844static void
39f77062 845restore_current_thread (ptid_t ptid)
c906108c 846{
dcf4fbde 847 switch_to_thread (ptid);
99b3d574
DP
848}
849
850static void
4f8d22e3 851restore_selected_frame (struct frame_id a_frame_id, int frame_level)
99b3d574 852{
4f8d22e3
PA
853 struct frame_info *frame = NULL;
854 int count;
855
856 gdb_assert (frame_level >= 0);
857
858 /* Restore by level first, check if the frame id is the same as
859 expected. If that fails, try restoring by frame id. If that
860 fails, nothing to do, just warn the user. */
861
862 count = frame_level;
863 frame = find_relative_frame (get_current_frame (), &count);
864 if (count == 0
865 && frame != NULL
866 /* Either the frame ids match, of they're both invalid. The
867 latter case is not failsafe, but since it's highly unlikely
868 the search by level finds the wrong frame, it's 99.9(9)% of
869 the time (for all practical purposes) safe. */
870 && (frame_id_eq (get_frame_id (frame), a_frame_id)
871 /* Note: could be better to check every frame_id
872 member for equality here. */
873 || (!frame_id_p (get_frame_id (frame))
874 && !frame_id_p (a_frame_id))))
875 {
876 /* Cool, all is fine. */
877 select_frame (frame);
878 return;
879 }
99b3d574 880
4f8d22e3
PA
881 frame = frame_find_by_id (a_frame_id);
882 if (frame != NULL)
883 {
884 /* Cool, refound it. */
885 select_frame (frame);
886 return;
887 }
99b3d574 888
0c501536
PA
889 /* Nothing else to do, the frame layout really changed. Select the
890 innermost stack frame. */
891 select_frame (get_current_frame ());
892
893 /* Warn the user. */
4f8d22e3 894 if (!ui_out_is_mi_like_p (uiout))
99b3d574 895 {
4f8d22e3
PA
896 warning (_("\
897Couldn't restore frame #%d in current thread, at reparsed frame #0\n"),
898 frame_level);
899 /* For MI, we should probably have a notification about
900 current frame change. But this error is not very
901 likely, so don't bother for now. */
0c501536 902 print_stack_frame (get_selected_frame (NULL), 1, SRC_LINE);
c906108c
SS
903 }
904}
905
6ecce94d
AC
906struct current_thread_cleanup
907{
39f77062 908 ptid_t inferior_ptid;
99b3d574 909 struct frame_id selected_frame_id;
4f8d22e3
PA
910 int selected_frame_level;
911 int was_stopped;
6ecce94d
AC
912};
913
914static void
915do_restore_current_thread_cleanup (void *arg)
916{
4f8d22e3 917 struct thread_info *tp;
6ecce94d 918 struct current_thread_cleanup *old = arg;
d729566a
PA
919
920 tp = find_thread_pid (old->inferior_ptid);
921
922 /* If the previously selected thread belonged to a process that has
923 in the mean time been deleted (due to normal exit, detach, etc.),
924 then don't revert back to it, but instead simply drop back to no
925 thread selected. */
926 if (tp
927 && is_exited (tp->ptid)
928 && find_inferior_pid (ptid_get_pid (tp->ptid)) == NULL)
929 restore_current_thread (null_ptid);
930 else
931 restore_current_thread (old->inferior_ptid);
94cc34af 932
4f8d22e3
PA
933 /* The running state of the originally selected thread may have
934 changed, so we have to recheck it here. */
d729566a
PA
935 if (!ptid_equal (inferior_ptid, null_ptid)
936 && old->was_stopped
4f8d22e3
PA
937 && is_stopped (inferior_ptid)
938 && target_has_registers
939 && target_has_stack
940 && target_has_memory)
941 restore_selected_frame (old->selected_frame_id,
942 old->selected_frame_level);
943}
944
945static void
946restore_current_thread_cleanup_dtor (void *arg)
947{
948 struct current_thread_cleanup *old = arg;
949 struct thread_info *tp;
950 tp = find_thread_pid (old->inferior_ptid);
951 if (tp)
952 tp->refcount--;
b8c9b27d 953 xfree (old);
6ecce94d
AC
954}
955
6208b47d 956struct cleanup *
4f8d22e3 957make_cleanup_restore_current_thread (void)
6ecce94d 958{
4f8d22e3
PA
959 struct thread_info *tp;
960 struct frame_info *frame;
961 struct current_thread_cleanup *old;
962
963 old = xmalloc (sizeof (struct current_thread_cleanup));
39f77062 964 old->inferior_ptid = inferior_ptid;
4f8d22e3 965
d729566a
PA
966 if (!ptid_equal (inferior_ptid, null_ptid))
967 {
968 old->was_stopped = is_stopped (inferior_ptid);
969 if (old->was_stopped
970 && target_has_registers
971 && target_has_stack
972 && target_has_memory)
973 frame = get_selected_frame (NULL);
974 else
975 frame = NULL;
4f8d22e3 976
d729566a
PA
977 old->selected_frame_id = get_frame_id (frame);
978 old->selected_frame_level = frame_relative_level (frame);
979
980 tp = find_thread_pid (inferior_ptid);
981 if (tp)
982 tp->refcount++;
983 }
4f8d22e3
PA
984
985 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
986 restore_current_thread_cleanup_dtor);
6ecce94d
AC
987}
988
c906108c
SS
989/* Apply a GDB command to a list of threads. List syntax is a whitespace
990 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
991 of two numbers seperated by a hyphen. Examples:
992
c5aa993b
JM
993 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
994 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
995 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
996 */
c906108c
SS
997
998static void
fba45db2 999thread_apply_all_command (char *cmd, int from_tty)
c906108c
SS
1000{
1001 struct thread_info *tp;
4f8d22e3 1002 struct cleanup *old_chain;
e35ce267 1003 char *saved_cmd;
c906108c
SS
1004
1005 if (cmd == NULL || *cmd == '\000')
8a3fe4f8 1006 error (_("Please specify a command following the thread ID list"));
94cc34af 1007
4f8d22e3 1008 prune_threads ();
e9d196c5
MS
1009 target_find_new_threads ();
1010
4f8d22e3
PA
1011 old_chain = make_cleanup_restore_current_thread ();
1012
e35ce267
CF
1013 /* Save a copy of the command in case it is clobbered by
1014 execute_command */
5b616ba1 1015 saved_cmd = xstrdup (cmd);
94cc34af 1016 make_cleanup (xfree, saved_cmd);
c906108c
SS
1017 for (tp = thread_list; tp; tp = tp->next)
1018 if (thread_alive (tp))
1019 {
dcf4fbde 1020 switch_to_thread (tp->ptid);
94cc34af 1021
a3f17187 1022 printf_filtered (_("\nThread %d (%s):\n"),
54ba13f7 1023 tp->num, target_pid_to_str (inferior_ptid));
c906108c 1024 execute_command (cmd, from_tty);
6949171e 1025 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
c906108c 1026 }
6ecce94d
AC
1027
1028 do_cleanups (old_chain);
c906108c
SS
1029}
1030
1031static void
fba45db2 1032thread_apply_command (char *tidlist, int from_tty)
c906108c
SS
1033{
1034 char *cmd;
1035 char *p;
1036 struct cleanup *old_chain;
e35ce267 1037 char *saved_cmd;
c906108c
SS
1038
1039 if (tidlist == NULL || *tidlist == '\000')
8a3fe4f8 1040 error (_("Please specify a thread ID list"));
c906108c 1041
c5aa993b 1042 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
c906108c
SS
1043
1044 if (*cmd == '\000')
8a3fe4f8 1045 error (_("Please specify a command following the thread ID list"));
c906108c 1046
e35ce267
CF
1047 /* Save a copy of the command in case it is clobbered by
1048 execute_command */
5b616ba1 1049 saved_cmd = xstrdup (cmd);
4f8d22e3 1050 old_chain = make_cleanup (xfree, saved_cmd);
c906108c
SS
1051 while (tidlist < cmd)
1052 {
1053 struct thread_info *tp;
1054 int start, end;
1055
1056 start = strtol (tidlist, &p, 10);
1057 if (p == tidlist)
8a3fe4f8 1058 error (_("Error parsing %s"), tidlist);
c906108c
SS
1059 tidlist = p;
1060
1061 while (*tidlist == ' ' || *tidlist == '\t')
1062 tidlist++;
1063
1064 if (*tidlist == '-') /* Got a range of IDs? */
1065 {
c5aa993b 1066 tidlist++; /* Skip the - */
c906108c
SS
1067 end = strtol (tidlist, &p, 10);
1068 if (p == tidlist)
8a3fe4f8 1069 error (_("Error parsing %s"), tidlist);
c906108c
SS
1070 tidlist = p;
1071
1072 while (*tidlist == ' ' || *tidlist == '\t')
1073 tidlist++;
1074 }
1075 else
1076 end = start;
1077
65fc9b77
PA
1078 make_cleanup_restore_current_thread ();
1079
c906108c
SS
1080 for (; start <= end; start++)
1081 {
1082 tp = find_thread_id (start);
1083
1084 if (!tp)
8a3fe4f8 1085 warning (_("Unknown thread %d."), start);
c906108c 1086 else if (!thread_alive (tp))
8a3fe4f8 1087 warning (_("Thread %d has terminated."), start);
c906108c
SS
1088 else
1089 {
dcf4fbde 1090 switch_to_thread (tp->ptid);
4f8d22e3 1091
a3f17187 1092 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
54ba13f7 1093 target_pid_to_str (inferior_ptid));
c906108c 1094 execute_command (cmd, from_tty);
4f8d22e3
PA
1095
1096 /* Restore exact command used previously. */
1097 strcpy (cmd, saved_cmd);
c906108c
SS
1098 }
1099 }
1100 }
6ecce94d
AC
1101
1102 do_cleanups (old_chain);
c906108c
SS
1103}
1104
1105/* Switch to the specified thread. Will dispatch off to thread_apply_command
1106 if prefix of arg is `apply'. */
1107
1108static void
fba45db2 1109thread_command (char *tidstr, int from_tty)
c906108c 1110{
c906108c
SS
1111 if (!tidstr)
1112 {
d729566a
PA
1113 if (ptid_equal (inferior_ptid, null_ptid))
1114 error (_("No thread selected"));
1115
c906108c 1116 if (target_has_stack)
4f8d22e3
PA
1117 {
1118 if (is_exited (inferior_ptid))
1119 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1120 pid_to_thread_id (inferior_ptid),
54ba13f7 1121 target_pid_to_str (inferior_ptid));
4f8d22e3
PA
1122 else
1123 printf_filtered (_("[Current thread is %d (%s)]\n"),
1124 pid_to_thread_id (inferior_ptid),
54ba13f7 1125 target_pid_to_str (inferior_ptid));
4f8d22e3 1126 }
c906108c 1127 else
8a3fe4f8 1128 error (_("No stack."));
c906108c
SS
1129 return;
1130 }
c5394b80 1131
ce43223b 1132 gdb_thread_select (uiout, tidstr, NULL);
c5394b80
JM
1133}
1134
93815fbf
VP
1135/* Print notices when new threads are attached and detached. */
1136int print_thread_events = 1;
1137static void
1138show_print_thread_events (struct ui_file *file, int from_tty,
1139 struct cmd_list_element *c, const char *value)
1140{
1141 fprintf_filtered (file, _("\
1142Printing of thread events is %s.\n"),
1143 value);
1144}
1145
c5394b80 1146static int
6949171e 1147do_captured_thread_select (struct ui_out *uiout, void *tidstr)
c5394b80
JM
1148{
1149 int num;
1150 struct thread_info *tp;
1151
81490ea1 1152 num = value_as_long (parse_and_eval (tidstr));
c906108c
SS
1153
1154 tp = find_thread_id (num);
1155
8b93c638 1156 if (!tp)
8a3fe4f8 1157 error (_("Thread ID %d not known."), num);
c906108c
SS
1158
1159 if (!thread_alive (tp))
8a3fe4f8 1160 error (_("Thread ID %d has terminated."), num);
c906108c 1161
dcf4fbde 1162 switch_to_thread (tp->ptid);
c906108c 1163
db5a7484
NR
1164 annotate_thread_changed ();
1165
8b93c638 1166 ui_out_text (uiout, "[Switching to thread ");
39f77062 1167 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
8b93c638 1168 ui_out_text (uiout, " (");
54ba13f7 1169 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
8b93c638 1170 ui_out_text (uiout, ")]");
c5394b80 1171
4f8d22e3
PA
1172 /* Note that we can't reach this with an exited thread, due to the
1173 thread_alive check above. */
1174 if (tp->state_ == THREAD_RUNNING)
94cc34af 1175 ui_out_text (uiout, "(running)\n");
4f8d22e3
PA
1176 else
1177 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
1178
1179 /* Since the current thread may have changed, see if there is any
1180 exited thread we can now delete. */
1181 prune_threads ();
94cc34af 1182
c5394b80
JM
1183 return GDB_RC_OK;
1184}
1185
1186enum gdb_rc
ce43223b 1187gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
c5394b80 1188{
b0b13bb4
DJ
1189 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1190 error_message, RETURN_MASK_ALL) < 0)
1191 return GDB_RC_FAIL;
1192 return GDB_RC_OK;
c906108c
SS
1193}
1194
1195/* Commands with a prefix of `thread'. */
1196struct cmd_list_element *thread_cmd_list = NULL;
1197
1198void
fba45db2 1199_initialize_thread (void)
c906108c
SS
1200{
1201 static struct cmd_list_element *thread_apply_list = NULL;
c906108c 1202
d729566a
PA
1203 add_info ("threads", info_threads_command,
1204 _("IDs of currently known threads."));
c906108c 1205
d729566a 1206 add_prefix_cmd ("thread", class_run, thread_command, _("\
1bedd215
AC
1207Use this command to switch between threads.\n\
1208The new thread ID must be currently known."),
d729566a 1209 &thread_cmd_list, "thread ", 1, &cmdlist);
c906108c 1210
d729566a
PA
1211 add_prefix_cmd ("apply", class_run, thread_apply_command,
1212 _("Apply a command to a list of threads."),
1213 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
c906108c 1214
d729566a
PA
1215 add_cmd ("all", class_run, thread_apply_all_command,
1216 _("Apply a command to all threads."), &thread_apply_list);
c906108c
SS
1217
1218 if (!xdb_commands)
1219 add_com_alias ("t", "thread", class_run, 1);
93815fbf
VP
1220
1221 add_setshow_boolean_cmd ("thread-events", no_class,
1222 &print_thread_events, _("\
11c68c47
EZ
1223Set printing of thread events (such as thread start and exit)."), _("\
1224Show printing of thread events (such as thread start and exit)."), NULL,
93815fbf
VP
1225 NULL,
1226 show_print_thread_events,
1227 &setprintlist, &showprintlist);
c906108c 1228}
This page took 1.042891 seconds and 4 git commands to generate.