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