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