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