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