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