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