constify error_no_arg
[deliverable/binutils-gdb.git] / gdb / thread.c
CommitLineData
c906108c 1/* Multi-process/thread control for GDB, the GNU debugger.
8926118c 2
ecd75fc8 3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
8926118c 4
b6ba6518 5 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
c906108c 6
c5aa993b 7 This file is part of GDB.
c906108c 8
c5aa993b
JM
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
c5aa993b 12 (at your option) any later version.
c906108c 13
c5aa993b
JM
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
c906108c 18
c5aa993b 19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
23#include "symtab.h"
24#include "frame.h"
25#include "inferior.h"
26#include "environ.h"
27#include "value.h"
28#include "target.h"
29#include "gdbthread.h"
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"
0e9f083f 35#include <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
651is_executing (ptid_t ptid)
652{
653 struct thread_info *tp;
654
e09875d4 655 tp = find_thread_ptid (ptid);
8ea051c5 656 gdb_assert (tp);
30596231 657 return tp->executing;
8ea051c5
PA
658}
659
660void
661set_executing (ptid_t ptid, int executing)
662{
663 struct thread_info *tp;
d90e17a7 664 int all = ptid_equal (ptid, minus_one_ptid);
8ea051c5 665
d90e17a7 666 if (all || ptid_is_pid (ptid))
8ea051c5
PA
667 {
668 for (tp = thread_list; tp; tp = tp->next)
d90e17a7 669 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
30596231 670 tp->executing = executing;
8ea051c5
PA
671 }
672 else
673 {
e09875d4 674 tp = find_thread_ptid (ptid);
8ea051c5 675 gdb_assert (tp);
30596231 676 tp->executing = executing;
8ea051c5
PA
677 }
678}
679
252fbfc8
PA
680void
681set_stop_requested (ptid_t ptid, int stop)
682{
683 struct thread_info *tp;
684 int all = ptid_equal (ptid, minus_one_ptid);
685
686 if (all || ptid_is_pid (ptid))
687 {
688 for (tp = thread_list; tp; tp = tp->next)
689 if (all || ptid_get_pid (tp->ptid) == ptid_get_pid (ptid))
690 tp->stop_requested = stop;
691 }
692 else
693 {
e09875d4 694 tp = find_thread_ptid (ptid);
252fbfc8
PA
695 gdb_assert (tp);
696 tp->stop_requested = stop;
697 }
698
699 /* Call the stop requested observer so other components of GDB can
700 react to this request. */
701 if (stop)
702 observer_notify_thread_stop_requested (ptid);
703}
704
29f49a6a
PA
705void
706finish_thread_state (ptid_t ptid)
707{
708 struct thread_info *tp;
709 int all;
710 int any_started = 0;
711
712 all = ptid_equal (ptid, minus_one_ptid);
713
714 if (all || ptid_is_pid (ptid))
715 {
716 for (tp = thread_list; tp; tp = tp->next)
717 {
30596231 718 if (tp->state == THREAD_EXITED)
29f49a6a
PA
719 continue;
720 if (all || ptid_get_pid (ptid) == ptid_get_pid (tp->ptid))
721 {
30596231 722 if (tp->executing && tp->state == THREAD_STOPPED)
29f49a6a 723 any_started = 1;
30596231 724 tp->state = tp->executing ? THREAD_RUNNING : THREAD_STOPPED;
29f49a6a
PA
725 }
726 }
727 }
728 else
729 {
e09875d4 730 tp = find_thread_ptid (ptid);
29f49a6a 731 gdb_assert (tp);
30596231 732 if (tp->state != THREAD_EXITED)
29f49a6a 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 if (any_started)
741 observer_notify_target_resumed (ptid);
742}
743
744void
745finish_thread_state_cleanup (void *arg)
746{
747 ptid_t *ptid_p = arg;
748
749 gdb_assert (arg);
750
751 finish_thread_state (*ptid_p);
752}
753
ce4c476a
PA
754int
755pc_in_thread_step_range (CORE_ADDR pc, struct thread_info *thread)
756{
757 return (pc >= thread->control.step_range_start
758 && pc < thread->control.step_range_end);
759}
760
8e8901c5 761/* Prints the list of threads and their details on UIOUT.
aea5b279 762 This is a version of 'info_threads_command' suitable for
c378eb4e 763 use from MI.
4f8d22e3 764 If REQUESTED_THREAD is not -1, it's the GDB id of the thread
8e8901c5 765 that should be printed. Otherwise, all threads are
c378eb4e 766 printed.
3ee1c036 767 If PID is not -1, only print threads from the process PID.
c378eb4e 768 Otherwise, threads from all attached PIDs are printed.
3ee1c036
VP
769 If both REQUESTED_THREAD and PID are not -1, then the thread
770 is printed if it belongs to the specified process. Otherwise,
771 an error is raised. */
8e8901c5 772void
aea5b279 773print_thread_info (struct ui_out *uiout, char *requested_threads, int pid)
c906108c
SS
774{
775 struct thread_info *tp;
39f77062 776 ptid_t current_ptid;
99b3d574 777 struct cleanup *old_chain;
4694da01 778 char *extra_info, *name, *target_id;
8e8901c5 779 int current_thread = -1;
c906108c 780
dc146f7c 781 update_thread_list ();
39f77062 782 current_ptid = inferior_ptid;
4f8d22e3
PA
783
784 /* We'll be switching threads temporarily. */
785 old_chain = make_cleanup_restore_current_thread ();
786
a7658b96
TT
787 /* For backward compatibility, we make a list for MI. A table is
788 preferable for the CLI, though, because it shows table
789 headers. */
790 if (ui_out_is_mi_like_p (uiout))
791 make_cleanup_ui_out_list_begin_end (uiout, "threads");
792 else
793 {
794 int n_threads = 0;
795
796 for (tp = thread_list; tp; tp = tp->next)
797 {
aea5b279 798 if (!number_is_in_list (requested_threads, tp->num))
a7658b96
TT
799 continue;
800
dfd4cc63 801 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
a7658b96
TT
802 continue;
803
30596231 804 if (tp->state == THREAD_EXITED)
a7658b96
TT
805 continue;
806
807 ++n_threads;
808 }
809
810 if (n_threads == 0)
811 {
aea5b279 812 if (requested_threads == NULL || *requested_threads == '\0')
a7658b96
TT
813 ui_out_message (uiout, 0, _("No threads.\n"));
814 else
aea5b279
MS
815 ui_out_message (uiout, 0, _("No threads match '%s'.\n"),
816 requested_threads);
a7658b96
TT
817 do_cleanups (old_chain);
818 return;
819 }
820
4694da01 821 make_cleanup_ui_out_table_begin_end (uiout, 4, n_threads, "threads");
a7658b96
TT
822
823 ui_out_table_header (uiout, 1, ui_left, "current", "");
824 ui_out_table_header (uiout, 4, ui_left, "id", "Id");
825 ui_out_table_header (uiout, 17, ui_left, "target-id", "Target Id");
a7658b96
TT
826 ui_out_table_header (uiout, 1, ui_left, "frame", "Frame");
827 ui_out_table_body (uiout);
828 }
829
c906108c
SS
830 for (tp = thread_list; tp; tp = tp->next)
831 {
8e8901c5 832 struct cleanup *chain2;
dc146f7c 833 int core;
8e8901c5 834
aea5b279 835 if (!number_is_in_list (requested_threads, tp->num))
8e8901c5
VP
836 continue;
837
dfd4cc63 838 if (pid != -1 && ptid_get_pid (tp->ptid) != pid)
3ee1c036 839 {
aea5b279 840 if (requested_threads != NULL && *requested_threads != '\0')
3ee1c036
VP
841 error (_("Requested thread not found in requested process"));
842 continue;
843 }
844
4f8d22e3
PA
845 if (ptid_equal (tp->ptid, current_ptid))
846 current_thread = tp->num;
847
30596231 848 if (tp->state == THREAD_EXITED)
4f8d22e3
PA
849 continue;
850
8e8901c5
VP
851 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
852
a7658b96
TT
853 if (ui_out_is_mi_like_p (uiout))
854 {
855 /* Compatibility. */
856 if (ptid_equal (tp->ptid, current_ptid))
857 ui_out_text (uiout, "* ");
858 else
859 ui_out_text (uiout, " ");
860 }
c906108c 861 else
a7658b96
TT
862 {
863 if (ptid_equal (tp->ptid, current_ptid))
864 ui_out_field_string (uiout, "current", "*");
865 else
866 ui_out_field_skip (uiout, "current");
867 }
c906108c 868
8e8901c5 869 ui_out_field_int (uiout, "id", tp->num);
0d06e24b 870
4694da01
TT
871 /* For the CLI, we stuff everything into the target-id field.
872 This is a gross hack to make the output come out looking
873 correct. The underlying problem here is that ui-out has no
874 way to specify that a field's space allocation should be
875 shared by several fields. For MI, we do the right thing
876 instead. */
877
878 target_id = target_pid_to_str (tp->ptid);
ed406532 879 extra_info = target_extra_thread_info (tp);
4694da01
TT
880 name = tp->name ? tp->name : target_thread_name (tp);
881
882 if (ui_out_is_mi_like_p (uiout))
8e8901c5 883 {
4694da01
TT
884 ui_out_field_string (uiout, "target-id", target_id);
885 if (extra_info)
886 ui_out_field_string (uiout, "details", extra_info);
887 if (name)
888 ui_out_field_string (uiout, "name", name);
889 }
890 else
891 {
892 struct cleanup *str_cleanup;
893 char *contents;
894
895 if (extra_info && name)
896 contents = xstrprintf ("%s \"%s\" (%s)", target_id,
897 name, extra_info);
898 else if (extra_info)
899 contents = xstrprintf ("%s (%s)", target_id, extra_info);
900 else if (name)
901 contents = xstrprintf ("%s \"%s\"", target_id, name);
902 else
903 contents = xstrdup (target_id);
904 str_cleanup = make_cleanup (xfree, contents);
905
906 ui_out_field_string (uiout, "target-id", contents);
907 do_cleanups (str_cleanup);
8e8901c5 908 }
4f8d22e3 909
30596231 910 if (tp->state == THREAD_RUNNING)
94cc34af
PA
911 ui_out_text (uiout, "(running)\n");
912 else
913 {
914 /* The switch below puts us at the top of the stack (leaf
915 frame). */
916 switch_to_thread (tp->ptid);
917 print_stack_frame (get_selected_frame (NULL),
918 /* For MI output, print frame level. */
919 ui_out_is_mi_like_p (uiout),
08d72866 920 LOCATION, 0);
94cc34af 921 }
8e8901c5 922
90139f7d
VP
923 if (ui_out_is_mi_like_p (uiout))
924 {
925 char *state = "stopped";
5d502164 926
30596231 927 if (tp->state == THREAD_RUNNING)
90139f7d
VP
928 state = "running";
929 ui_out_field_string (uiout, "state", state);
930 }
931
dc146f7c
VP
932 core = target_core_of_thread (tp->ptid);
933 if (ui_out_is_mi_like_p (uiout) && core != -1)
934 ui_out_field_int (uiout, "core", core);
935
8e8901c5 936 do_cleanups (chain2);
c906108c
SS
937 }
938
99b3d574
DP
939 /* Restores the current thread and the frame selected before
940 the "info threads" command. */
941 do_cleanups (old_chain);
c906108c 942
aea5b279 943 if (pid == -1 && requested_threads == NULL)
8e8901c5 944 {
4f8d22e3 945 gdb_assert (current_thread != -1
d729566a
PA
946 || !thread_list
947 || ptid_equal (inferior_ptid, null_ptid));
0bcd3e20 948 if (current_thread != -1 && ui_out_is_mi_like_p (uiout))
8e8901c5 949 ui_out_field_int (uiout, "current-thread-id", current_thread);
94cc34af 950
4f8d22e3
PA
951 if (current_thread != -1 && is_exited (current_ptid))
952 ui_out_message (uiout, 0, "\n\
953The current thread <Thread ID %d> has terminated. See `help thread'.\n",
954 current_thread);
d729566a
PA
955 else if (thread_list
956 && current_thread == -1
957 && ptid_equal (current_ptid, null_ptid))
958 ui_out_message (uiout, 0, "\n\
959No selected thread. See `help thread'.\n");
c906108c 960 }
c906108c
SS
961}
962
8e8901c5
VP
963/* Print information about currently known threads
964
60f98dde
MS
965 Optional ARG is a thread id, or list of thread ids.
966
967 Note: this has the drawback that it _really_ switches
968 threads, which frees the frame cache. A no-side
969 effects info-threads command would be nicer. */
8e8901c5
VP
970
971static void
972info_threads_command (char *arg, int from_tty)
973{
79a45e25 974 print_thread_info (current_uiout, arg, -1);
8e8901c5
VP
975}
976
c378eb4e 977/* Switch from one thread to another. */
c906108c 978
6a6b96b9 979void
39f77062 980switch_to_thread (ptid_t ptid)
c906108c 981{
6c95b8df
PA
982 /* Switch the program space as well, if we can infer it from the now
983 current thread. Otherwise, it's up to the caller to select the
984 space it wants. */
985 if (!ptid_equal (ptid, null_ptid))
986 {
987 struct inferior *inf;
988
989 inf = find_inferior_pid (ptid_get_pid (ptid));
990 gdb_assert (inf != NULL);
991 set_current_program_space (inf->pspace);
992 set_current_inferior (inf);
993 }
994
39f77062 995 if (ptid_equal (ptid, inferior_ptid))
c906108c
SS
996 return;
997
39f77062 998 inferior_ptid = ptid;
35f196d9 999 reinit_frame_cache ();
94cc34af 1000
4f8d22e3
PA
1001 /* We don't check for is_stopped, because we're called at times
1002 while in the TARGET_RUNNING state, e.g., while handling an
1003 internal event. */
d729566a
PA
1004 if (!ptid_equal (inferior_ptid, null_ptid)
1005 && !is_exited (ptid)
1006 && !is_executing (ptid))
fb14de7b 1007 stop_pc = regcache_read_pc (get_thread_regcache (ptid));
94cc34af
PA
1008 else
1009 stop_pc = ~(CORE_ADDR) 0;
c906108c
SS
1010}
1011
1012static void
39f77062 1013restore_current_thread (ptid_t ptid)
c906108c 1014{
dcf4fbde 1015 switch_to_thread (ptid);
99b3d574
DP
1016}
1017
1018static void
4f8d22e3 1019restore_selected_frame (struct frame_id a_frame_id, int frame_level)
99b3d574 1020{
4f8d22e3
PA
1021 struct frame_info *frame = NULL;
1022 int count;
1023
eb8c0621
TT
1024 /* This means there was no selected frame. */
1025 if (frame_level == -1)
1026 {
1027 select_frame (NULL);
1028 return;
1029 }
1030
4f8d22e3
PA
1031 gdb_assert (frame_level >= 0);
1032
1033 /* Restore by level first, check if the frame id is the same as
1034 expected. If that fails, try restoring by frame id. If that
1035 fails, nothing to do, just warn the user. */
1036
1037 count = frame_level;
1038 frame = find_relative_frame (get_current_frame (), &count);
1039 if (count == 0
1040 && frame != NULL
005ca36a
JB
1041 /* The frame ids must match - either both valid or both outer_frame_id.
1042 The latter case is not failsafe, but since it's highly unlikely
4f8d22e3
PA
1043 the search by level finds the wrong frame, it's 99.9(9)% of
1044 the time (for all practical purposes) safe. */
005ca36a 1045 && frame_id_eq (get_frame_id (frame), a_frame_id))
4f8d22e3
PA
1046 {
1047 /* Cool, all is fine. */
1048 select_frame (frame);
1049 return;
1050 }
99b3d574 1051
4f8d22e3
PA
1052 frame = frame_find_by_id (a_frame_id);
1053 if (frame != NULL)
1054 {
1055 /* Cool, refound it. */
1056 select_frame (frame);
1057 return;
1058 }
99b3d574 1059
0c501536
PA
1060 /* Nothing else to do, the frame layout really changed. Select the
1061 innermost stack frame. */
1062 select_frame (get_current_frame ());
1063
1064 /* Warn the user. */
79a45e25 1065 if (frame_level > 0 && !ui_out_is_mi_like_p (current_uiout))
99b3d574 1066 {
3e43a32a 1067 warning (_("Couldn't restore frame #%d in "
e0162910 1068 "current thread. Bottom (innermost) frame selected:"),
4f8d22e3
PA
1069 frame_level);
1070 /* For MI, we should probably have a notification about
1071 current frame change. But this error is not very
1072 likely, so don't bother for now. */
08d72866 1073 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
c906108c
SS
1074 }
1075}
1076
6ecce94d
AC
1077struct current_thread_cleanup
1078{
39f77062 1079 ptid_t inferior_ptid;
99b3d574 1080 struct frame_id selected_frame_id;
4f8d22e3
PA
1081 int selected_frame_level;
1082 int was_stopped;
6c95b8df 1083 int inf_id;
9addecb9 1084 int was_removable;
6ecce94d
AC
1085};
1086
1087static void
1088do_restore_current_thread_cleanup (void *arg)
1089{
4f8d22e3 1090 struct thread_info *tp;
6ecce94d 1091 struct current_thread_cleanup *old = arg;
d729566a 1092
e09875d4 1093 tp = find_thread_ptid (old->inferior_ptid);
d729566a
PA
1094
1095 /* If the previously selected thread belonged to a process that has
1096 in the mean time been deleted (due to normal exit, detach, etc.),
1097 then don't revert back to it, but instead simply drop back to no
1098 thread selected. */
1099 if (tp
88fc996f 1100 && find_inferior_pid (ptid_get_pid (tp->ptid)) != NULL)
d729566a 1101 restore_current_thread (old->inferior_ptid);
88fc996f 1102 else
6c95b8df
PA
1103 {
1104 restore_current_thread (null_ptid);
1105 set_current_inferior (find_inferior_id (old->inf_id));
1106 }
94cc34af 1107
4f8d22e3
PA
1108 /* The running state of the originally selected thread may have
1109 changed, so we have to recheck it here. */
d729566a
PA
1110 if (!ptid_equal (inferior_ptid, null_ptid)
1111 && old->was_stopped
4f8d22e3
PA
1112 && is_stopped (inferior_ptid)
1113 && target_has_registers
1114 && target_has_stack
1115 && target_has_memory)
1116 restore_selected_frame (old->selected_frame_id,
1117 old->selected_frame_level);
1118}
1119
1120static void
1121restore_current_thread_cleanup_dtor (void *arg)
1122{
1123 struct current_thread_cleanup *old = arg;
1124 struct thread_info *tp;
9addecb9 1125 struct inferior *inf;
5d502164 1126
e09875d4 1127 tp = find_thread_ptid (old->inferior_ptid);
4f8d22e3
PA
1128 if (tp)
1129 tp->refcount--;
9addecb9
TT
1130 inf = find_inferior_id (old->inf_id);
1131 if (inf != NULL)
1132 inf->removable = old->was_removable;
b8c9b27d 1133 xfree (old);
6ecce94d
AC
1134}
1135
054e8d9e
AA
1136/* Set the thread reference count. */
1137
1138static void
1139set_thread_refcount (void *data)
1140{
1141 int k;
1142 struct thread_array_cleanup *ta_cleanup = data;
1143
1144 for (k = 0; k != ta_cleanup->count; k++)
1145 ta_cleanup->tp_array[k]->refcount--;
1146}
1147
6208b47d 1148struct cleanup *
4f8d22e3 1149make_cleanup_restore_current_thread (void)
6ecce94d 1150{
4f8d22e3
PA
1151 struct thread_info *tp;
1152 struct frame_info *frame;
1153 struct current_thread_cleanup *old;
1154
1155 old = xmalloc (sizeof (struct current_thread_cleanup));
39f77062 1156 old->inferior_ptid = inferior_ptid;
6c95b8df 1157 old->inf_id = current_inferior ()->num;
9addecb9 1158 old->was_removable = current_inferior ()->removable;
4f8d22e3 1159
d729566a
PA
1160 if (!ptid_equal (inferior_ptid, null_ptid))
1161 {
1162 old->was_stopped = is_stopped (inferior_ptid);
1163 if (old->was_stopped
1164 && target_has_registers
1165 && target_has_stack
1166 && target_has_memory)
eb8c0621
TT
1167 {
1168 /* When processing internal events, there might not be a
1169 selected frame. If we naively call get_selected_frame
1170 here, then we can end up reading debuginfo for the
1171 current frame, but we don't generally need the debuginfo
1172 at this point. */
1173 frame = get_selected_frame_if_set ();
1174 }
d729566a
PA
1175 else
1176 frame = NULL;
4f8d22e3 1177
d729566a
PA
1178 old->selected_frame_id = get_frame_id (frame);
1179 old->selected_frame_level = frame_relative_level (frame);
1180
e09875d4 1181 tp = find_thread_ptid (inferior_ptid);
d729566a
PA
1182 if (tp)
1183 tp->refcount++;
1184 }
4f8d22e3 1185
9addecb9
TT
1186 current_inferior ()->removable = 0;
1187
4f8d22e3
PA
1188 return make_cleanup_dtor (do_restore_current_thread_cleanup, old,
1189 restore_current_thread_cleanup_dtor);
6ecce94d
AC
1190}
1191
c906108c
SS
1192/* Apply a GDB command to a list of threads. List syntax is a whitespace
1193 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
1194 of two numbers seperated by a hyphen. Examples:
1195
c5aa993b
JM
1196 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
1197 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
c378eb4e 1198 thread apply all p x/i $pc Apply x/i $pc cmd to all threads. */
c906108c
SS
1199
1200static void
fba45db2 1201thread_apply_all_command (char *cmd, int from_tty)
c906108c 1202{
4f8d22e3 1203 struct cleanup *old_chain;
e35ce267 1204 char *saved_cmd;
054e8d9e
AA
1205 int tc;
1206 struct thread_array_cleanup ta_cleanup;
c906108c
SS
1207
1208 if (cmd == NULL || *cmd == '\000')
8a3fe4f8 1209 error (_("Please specify a command following the thread ID list"));
94cc34af 1210
dc146f7c 1211 update_thread_list ();
e9d196c5 1212
4f8d22e3
PA
1213 old_chain = make_cleanup_restore_current_thread ();
1214
e35ce267 1215 /* Save a copy of the command in case it is clobbered by
c378eb4e 1216 execute_command. */
5b616ba1 1217 saved_cmd = xstrdup (cmd);
94cc34af 1218 make_cleanup (xfree, saved_cmd);
054e8d9e 1219 tc = thread_count ();
94cc34af 1220
054e8d9e
AA
1221 if (tc)
1222 {
1223 struct thread_info **tp_array;
1224 struct thread_info *tp;
1225 int i = 0, k;
1226
1227 /* Save a copy of the thread_list in case we execute detach
1228 command. */
1229 tp_array = xmalloc (sizeof (struct thread_info *) * tc);
1230 make_cleanup (xfree, tp_array);
1231 ta_cleanup.tp_array = tp_array;
1232 ta_cleanup.count = tc;
1233
034f788c 1234 ALL_NON_EXITED_THREADS (tp)
054e8d9e
AA
1235 {
1236 tp_array[i] = tp;
1237 tp->refcount++;
1238 i++;
1239 }
1240
1241 make_cleanup (set_thread_refcount, &ta_cleanup);
1242
1243 for (k = 0; k != i; k++)
1244 if (thread_alive (tp_array[k]))
1245 {
1246 switch_to_thread (tp_array[k]->ptid);
1247 printf_filtered (_("\nThread %d (%s):\n"),
1248 tp_array[k]->num,
1249 target_pid_to_str (inferior_ptid));
1250 execute_command (cmd, from_tty);
1251
1252 /* Restore exact command used previously. */
1253 strcpy (cmd, saved_cmd);
1254 }
1255 }
6ecce94d
AC
1256
1257 do_cleanups (old_chain);
c906108c
SS
1258}
1259
1260static void
fba45db2 1261thread_apply_command (char *tidlist, int from_tty)
c906108c
SS
1262{
1263 char *cmd;
c906108c 1264 struct cleanup *old_chain;
e35ce267 1265 char *saved_cmd;
197f0a60 1266 struct get_number_or_range_state state;
c906108c
SS
1267
1268 if (tidlist == NULL || *tidlist == '\000')
8a3fe4f8 1269 error (_("Please specify a thread ID list"));
c906108c 1270
c5aa993b 1271 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
c906108c
SS
1272
1273 if (*cmd == '\000')
8a3fe4f8 1274 error (_("Please specify a command following the thread ID list"));
c906108c 1275
e35ce267 1276 /* Save a copy of the command in case it is clobbered by
c378eb4e 1277 execute_command. */
5b616ba1 1278 saved_cmd = xstrdup (cmd);
4f8d22e3 1279 old_chain = make_cleanup (xfree, saved_cmd);
197f0a60
TT
1280
1281 init_number_or_range (&state, tidlist);
1282 while (!state.finished && state.string < cmd)
c906108c
SS
1283 {
1284 struct thread_info *tp;
65ebfb52 1285 int start;
c906108c 1286
197f0a60 1287 start = get_number_or_range (&state);
c906108c 1288
65fc9b77
PA
1289 make_cleanup_restore_current_thread ();
1290
65ebfb52 1291 tp = find_thread_id (start);
c906108c 1292
65ebfb52
MS
1293 if (!tp)
1294 warning (_("Unknown thread %d."), start);
1295 else if (!thread_alive (tp))
1296 warning (_("Thread %d has terminated."), start);
1297 else
1298 {
1299 switch_to_thread (tp->ptid);
4f8d22e3 1300
65ebfb52
MS
1301 printf_filtered (_("\nThread %d (%s):\n"), tp->num,
1302 target_pid_to_str (inferior_ptid));
1303 execute_command (cmd, from_tty);
4f8d22e3 1304
65ebfb52
MS
1305 /* Restore exact command used previously. */
1306 strcpy (cmd, saved_cmd);
c906108c
SS
1307 }
1308 }
6ecce94d
AC
1309
1310 do_cleanups (old_chain);
c906108c
SS
1311}
1312
1313/* Switch to the specified thread. Will dispatch off to thread_apply_command
1314 if prefix of arg is `apply'. */
1315
1316static void
fba45db2 1317thread_command (char *tidstr, int from_tty)
c906108c 1318{
c906108c
SS
1319 if (!tidstr)
1320 {
d729566a
PA
1321 if (ptid_equal (inferior_ptid, null_ptid))
1322 error (_("No thread selected"));
1323
c906108c 1324 if (target_has_stack)
4f8d22e3
PA
1325 {
1326 if (is_exited (inferior_ptid))
1327 printf_filtered (_("[Current thread is %d (%s) (exited)]\n"),
1328 pid_to_thread_id (inferior_ptid),
54ba13f7 1329 target_pid_to_str (inferior_ptid));
4f8d22e3
PA
1330 else
1331 printf_filtered (_("[Current thread is %d (%s)]\n"),
1332 pid_to_thread_id (inferior_ptid),
54ba13f7 1333 target_pid_to_str (inferior_ptid));
4f8d22e3 1334 }
c906108c 1335 else
8a3fe4f8 1336 error (_("No stack."));
c906108c
SS
1337 return;
1338 }
c5394b80 1339
79a45e25 1340 gdb_thread_select (current_uiout, tidstr, NULL);
c5394b80
JM
1341}
1342
4694da01
TT
1343/* Implementation of `thread name'. */
1344
1345static void
1346thread_name_command (char *arg, int from_tty)
1347{
1348 struct thread_info *info;
1349
1350 if (ptid_equal (inferior_ptid, null_ptid))
1351 error (_("No thread selected"));
1352
529480d0 1353 arg = skip_spaces (arg);
4694da01
TT
1354
1355 info = inferior_thread ();
1356 xfree (info->name);
1357 info->name = arg ? xstrdup (arg) : NULL;
1358}
1359
60f98dde
MS
1360/* Find thread ids with a name, target pid, or extra info matching ARG. */
1361
1362static void
1363thread_find_command (char *arg, int from_tty)
1364{
1365 struct thread_info *tp;
1366 char *tmp;
1367 unsigned long match = 0;
1368
1369 if (arg == NULL || *arg == '\0')
1370 error (_("Command requires an argument."));
1371
1372 tmp = re_comp (arg);
1373 if (tmp != 0)
1374 error (_("Invalid regexp (%s): %s"), tmp, arg);
1375
1376 update_thread_list ();
1377 for (tp = thread_list; tp; tp = tp->next)
1378 {
1379 if (tp->name != NULL && re_exec (tp->name))
1380 {
1381 printf_filtered (_("Thread %d has name '%s'\n"),
1382 tp->num, tp->name);
1383 match++;
1384 }
1385
1386 tmp = target_thread_name (tp);
1387 if (tmp != NULL && re_exec (tmp))
1388 {
1389 printf_filtered (_("Thread %d has target name '%s'\n"),
1390 tp->num, tmp);
1391 match++;
1392 }
1393
1394 tmp = target_pid_to_str (tp->ptid);
1395 if (tmp != NULL && re_exec (tmp))
1396 {
1397 printf_filtered (_("Thread %d has target id '%s'\n"),
1398 tp->num, tmp);
1399 match++;
1400 }
1401
1402 tmp = target_extra_thread_info (tp);
1403 if (tmp != NULL && re_exec (tmp))
1404 {
1405 printf_filtered (_("Thread %d has extra info '%s'\n"),
1406 tp->num, tmp);
1407 match++;
1408 }
1409 }
1410 if (!match)
1411 printf_filtered (_("No threads match '%s'\n"), arg);
1412}
1413
93815fbf
VP
1414/* Print notices when new threads are attached and detached. */
1415int print_thread_events = 1;
1416static void
1417show_print_thread_events (struct ui_file *file, int from_tty,
1418 struct cmd_list_element *c, const char *value)
1419{
3e43a32a
MS
1420 fprintf_filtered (file,
1421 _("Printing of thread events is %s.\n"),
93815fbf
VP
1422 value);
1423}
1424
c5394b80 1425static int
6949171e 1426do_captured_thread_select (struct ui_out *uiout, void *tidstr)
c5394b80
JM
1427{
1428 int num;
1429 struct thread_info *tp;
1430
81490ea1 1431 num = value_as_long (parse_and_eval (tidstr));
c906108c
SS
1432
1433 tp = find_thread_id (num);
1434
8b93c638 1435 if (!tp)
8a3fe4f8 1436 error (_("Thread ID %d not known."), num);
c906108c
SS
1437
1438 if (!thread_alive (tp))
8a3fe4f8 1439 error (_("Thread ID %d has terminated."), num);
c906108c 1440
dcf4fbde 1441 switch_to_thread (tp->ptid);
c906108c 1442
db5a7484
NR
1443 annotate_thread_changed ();
1444
8b93c638 1445 ui_out_text (uiout, "[Switching to thread ");
39f77062 1446 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
8b93c638 1447 ui_out_text (uiout, " (");
54ba13f7 1448 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
8b93c638 1449 ui_out_text (uiout, ")]");
c5394b80 1450
4f8d22e3
PA
1451 /* Note that we can't reach this with an exited thread, due to the
1452 thread_alive check above. */
30596231 1453 if (tp->state == THREAD_RUNNING)
94cc34af 1454 ui_out_text (uiout, "(running)\n");
4f8d22e3 1455 else
98871305
TT
1456 {
1457 ui_out_text (uiout, "\n");
08d72866 1458 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
98871305 1459 }
4f8d22e3
PA
1460
1461 /* Since the current thread may have changed, see if there is any
1462 exited thread we can now delete. */
1463 prune_threads ();
94cc34af 1464
c5394b80
JM
1465 return GDB_RC_OK;
1466}
1467
1468enum gdb_rc
ce43223b 1469gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
c5394b80 1470{
b0b13bb4
DJ
1471 if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
1472 error_message, RETURN_MASK_ALL) < 0)
1473 return GDB_RC_FAIL;
1474 return GDB_RC_OK;
c906108c
SS
1475}
1476
dc146f7c
VP
1477void
1478update_thread_list (void)
1479{
1480 prune_threads ();
1481 target_find_new_threads ();
1482}
1483
6aed2dbc
SS
1484/* Return a new value for the selected thread's id. Return a value of 0 if
1485 no thread is selected, or no threads exist. */
1486
1487static struct value *
22d2b532
SDJ
1488thread_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1489 void *ignore)
6aed2dbc
SS
1490{
1491 struct thread_info *tp = find_thread_ptid (inferior_ptid);
1492
1493 return value_from_longest (builtin_type (gdbarch)->builtin_int,
1494 (tp ? tp->num : 0));
1495}
1496
c906108c
SS
1497/* Commands with a prefix of `thread'. */
1498struct cmd_list_element *thread_cmd_list = NULL;
1499
22d2b532
SDJ
1500/* Implementation of `thread' variable. */
1501
1502static const struct internalvar_funcs thread_funcs =
1503{
1504 thread_id_make_value,
1505 NULL,
1506 NULL
1507};
1508
c906108c 1509void
fba45db2 1510_initialize_thread (void)
c906108c
SS
1511{
1512 static struct cmd_list_element *thread_apply_list = NULL;
c906108c 1513
60f98dde
MS
1514 add_info ("threads", info_threads_command,
1515 _("Display currently known threads.\n\
1516Usage: info threads [ID]...\n\
1517Optional arguments are thread IDs with spaces between.\n\
1518If no arguments, all threads are displayed."));
c906108c 1519
d729566a 1520 add_prefix_cmd ("thread", class_run, thread_command, _("\
1bedd215
AC
1521Use this command to switch between threads.\n\
1522The new thread ID must be currently known."),
d729566a 1523 &thread_cmd_list, "thread ", 1, &cmdlist);
c906108c 1524
d729566a
PA
1525 add_prefix_cmd ("apply", class_run, thread_apply_command,
1526 _("Apply a command to a list of threads."),
1527 &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
c906108c 1528
d729566a
PA
1529 add_cmd ("all", class_run, thread_apply_all_command,
1530 _("Apply a command to all threads."), &thread_apply_list);
c906108c 1531
4694da01
TT
1532 add_cmd ("name", class_run, thread_name_command,
1533 _("Set the current thread's name.\n\
1534Usage: thread name [NAME]\n\
1535If NAME is not given, then any existing name is removed."), &thread_cmd_list);
1536
60f98dde
MS
1537 add_cmd ("find", class_run, thread_find_command, _("\
1538Find threads that match a regular expression.\n\
1539Usage: thread find REGEXP\n\
1540Will display thread ids whose name, target ID, or extra info matches REGEXP."),
1541 &thread_cmd_list);
1542
c906108c
SS
1543 if (!xdb_commands)
1544 add_com_alias ("t", "thread", class_run, 1);
93815fbf
VP
1545
1546 add_setshow_boolean_cmd ("thread-events", no_class,
1547 &print_thread_events, _("\
11c68c47
EZ
1548Set printing of thread events (such as thread start and exit)."), _("\
1549Show printing of thread events (such as thread start and exit)."), NULL,
93815fbf
VP
1550 NULL,
1551 show_print_thread_events,
1552 &setprintlist, &showprintlist);
6aed2dbc 1553
22d2b532 1554 create_internalvar_type_lazy ("_thread", &thread_funcs, NULL);
c906108c 1555}
This page took 1.769963 seconds and 4 git commands to generate.