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