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