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