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