1e93f7393Sniklas /* Multi-process/thread control for GDB, the GNU debugger.
2b725ae77Skettenis
3b725ae77Skettenis Copyright 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
4*63addd46Skettenis 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5e93f7393Sniklas
6e93f7393Sniklas Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
7e93f7393Sniklas
8e93f7393Sniklas This file is part of GDB.
9e93f7393Sniklas
10e93f7393Sniklas This program is free software; you can redistribute it and/or modify
11e93f7393Sniklas it under the terms of the GNU General Public License as published by
12e93f7393Sniklas the Free Software Foundation; either version 2 of the License, or
13e93f7393Sniklas (at your option) any later version.
14e93f7393Sniklas
15e93f7393Sniklas This program is distributed in the hope that it will be useful,
16e93f7393Sniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
17e93f7393Sniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18e93f7393Sniklas GNU General Public License for more details.
19e93f7393Sniklas
20e93f7393Sniklas You should have received a copy of the GNU General Public License
21e93f7393Sniklas along with this program; if not, write to the Free Software
22b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330,
23b725ae77Skettenis Boston, MA 02111-1307, USA. */
24e93f7393Sniklas
25e93f7393Sniklas #include "defs.h"
26e93f7393Sniklas #include "symtab.h"
27e93f7393Sniklas #include "frame.h"
28e93f7393Sniklas #include "inferior.h"
29e93f7393Sniklas #include "environ.h"
30e93f7393Sniklas #include "value.h"
31e93f7393Sniklas #include "target.h"
32e93f7393Sniklas #include "gdbthread.h"
33e93f7393Sniklas #include "command.h"
34e93f7393Sniklas #include "gdbcmd.h"
35b725ae77Skettenis #include "regcache.h"
36b725ae77Skettenis #include "gdb.h"
37b725ae77Skettenis #include "gdb_string.h"
38e93f7393Sniklas
39e93f7393Sniklas #include <ctype.h>
40e93f7393Sniklas #include <sys/types.h>
41e93f7393Sniklas #include <signal.h>
42b725ae77Skettenis #include "ui-out.h"
43e93f7393Sniklas
44e93f7393Sniklas /*#include "lynxos-core.h" */
45e93f7393Sniklas
46b725ae77Skettenis /* Definition of struct thread_info exported to gdbthread.h */
47b725ae77Skettenis
48b725ae77Skettenis /* Prototypes for exported functions. */
49b725ae77Skettenis
50b725ae77Skettenis void _initialize_thread (void);
51b725ae77Skettenis
52b725ae77Skettenis /* Prototypes for local functions. */
53e93f7393Sniklas
54e93f7393Sniklas static struct thread_info *thread_list = NULL;
55e93f7393Sniklas static int highest_thread_num;
56e93f7393Sniklas
57b725ae77Skettenis static struct thread_info *find_thread_id (int num);
58e93f7393Sniklas
59b725ae77Skettenis static void thread_command (char *tidstr, int from_tty);
60b725ae77Skettenis static void thread_apply_all_command (char *, int);
61b725ae77Skettenis static int thread_alive (struct thread_info *);
62b725ae77Skettenis static void info_threads_command (char *, int);
63b725ae77Skettenis static void thread_apply_command (char *, int);
64b725ae77Skettenis static void restore_current_thread (ptid_t);
65b725ae77Skettenis static void switch_to_thread (ptid_t ptid);
66b725ae77Skettenis static void prune_threads (void);
67e93f7393Sniklas
68e93f7393Sniklas void
delete_step_resume_breakpoint(void * arg)69b725ae77Skettenis delete_step_resume_breakpoint (void *arg)
70b725ae77Skettenis {
71b725ae77Skettenis struct breakpoint **breakpointp = (struct breakpoint **) arg;
72b725ae77Skettenis struct thread_info *tp;
73b725ae77Skettenis
74b725ae77Skettenis if (*breakpointp != NULL)
75b725ae77Skettenis {
76b725ae77Skettenis delete_breakpoint (*breakpointp);
77b725ae77Skettenis for (tp = thread_list; tp; tp = tp->next)
78b725ae77Skettenis if (tp->step_resume_breakpoint == *breakpointp)
79b725ae77Skettenis tp->step_resume_breakpoint = NULL;
80b725ae77Skettenis
81b725ae77Skettenis *breakpointp = NULL;
82b725ae77Skettenis }
83b725ae77Skettenis }
84b725ae77Skettenis
85b725ae77Skettenis static void
free_thread(struct thread_info * tp)86b725ae77Skettenis free_thread (struct thread_info *tp)
87b725ae77Skettenis {
88b725ae77Skettenis /* NOTE: this will take care of any left-over step_resume breakpoints,
89b725ae77Skettenis but not any user-specified thread-specific breakpoints. */
90b725ae77Skettenis if (tp->step_resume_breakpoint)
91b725ae77Skettenis delete_breakpoint (tp->step_resume_breakpoint);
92b725ae77Skettenis
93b725ae77Skettenis /* FIXME: do I ever need to call the back-end to give it a
94b725ae77Skettenis chance at this private data before deleting the thread? */
95b725ae77Skettenis if (tp->private)
96b725ae77Skettenis xfree (tp->private);
97b725ae77Skettenis
98b725ae77Skettenis xfree (tp);
99b725ae77Skettenis }
100b725ae77Skettenis
101b725ae77Skettenis void
init_thread_list(void)102b725ae77Skettenis init_thread_list (void)
103e93f7393Sniklas {
104e93f7393Sniklas struct thread_info *tp, *tpnext;
105e93f7393Sniklas
106b725ae77Skettenis highest_thread_num = 0;
107e93f7393Sniklas if (!thread_list)
108e93f7393Sniklas return;
109e93f7393Sniklas
110e93f7393Sniklas for (tp = thread_list; tp; tp = tpnext)
111e93f7393Sniklas {
112e93f7393Sniklas tpnext = tp->next;
113b725ae77Skettenis free_thread (tp);
114e93f7393Sniklas }
115e93f7393Sniklas
116e93f7393Sniklas thread_list = NULL;
117e93f7393Sniklas }
118e93f7393Sniklas
119b725ae77Skettenis /* add_thread now returns a pointer to the new thread_info,
120b725ae77Skettenis so that back_ends can initialize their private data. */
121b725ae77Skettenis
122b725ae77Skettenis struct thread_info *
add_thread(ptid_t ptid)123b725ae77Skettenis add_thread (ptid_t ptid)
124e93f7393Sniklas {
125e93f7393Sniklas struct thread_info *tp;
126e93f7393Sniklas
127b725ae77Skettenis tp = (struct thread_info *) xmalloc (sizeof (*tp));
128b725ae77Skettenis memset (tp, 0, sizeof (*tp));
129b725ae77Skettenis tp->ptid = ptid;
130e93f7393Sniklas tp->num = ++highest_thread_num;
131e93f7393Sniklas tp->next = thread_list;
132e93f7393Sniklas thread_list = tp;
133b725ae77Skettenis return tp;
134b725ae77Skettenis }
135b725ae77Skettenis
136b725ae77Skettenis void
delete_thread(ptid_t ptid)137b725ae77Skettenis delete_thread (ptid_t ptid)
138b725ae77Skettenis {
139b725ae77Skettenis struct thread_info *tp, *tpprev;
140b725ae77Skettenis
141b725ae77Skettenis tpprev = NULL;
142b725ae77Skettenis
143b725ae77Skettenis for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
144b725ae77Skettenis if (ptid_equal (tp->ptid, ptid))
145b725ae77Skettenis break;
146b725ae77Skettenis
147b725ae77Skettenis if (!tp)
148b725ae77Skettenis return;
149b725ae77Skettenis
150b725ae77Skettenis if (tpprev)
151b725ae77Skettenis tpprev->next = tp->next;
152b725ae77Skettenis else
153b725ae77Skettenis thread_list = tp->next;
154b725ae77Skettenis
155b725ae77Skettenis free_thread (tp);
156e93f7393Sniklas }
157e93f7393Sniklas
158e93f7393Sniklas static struct thread_info *
find_thread_id(int num)159b725ae77Skettenis find_thread_id (int num)
160e93f7393Sniklas {
161e93f7393Sniklas struct thread_info *tp;
162e93f7393Sniklas
163e93f7393Sniklas for (tp = thread_list; tp; tp = tp->next)
164e93f7393Sniklas if (tp->num == num)
165e93f7393Sniklas return tp;
166e93f7393Sniklas
167e93f7393Sniklas return NULL;
168e93f7393Sniklas }
169e93f7393Sniklas
170b725ae77Skettenis /* Find a thread_info by matching PTID. */
171b725ae77Skettenis struct thread_info *
find_thread_pid(ptid_t ptid)172b725ae77Skettenis find_thread_pid (ptid_t ptid)
173b725ae77Skettenis {
174b725ae77Skettenis struct thread_info *tp;
175b725ae77Skettenis
176b725ae77Skettenis for (tp = thread_list; tp; tp = tp->next)
177b725ae77Skettenis if (ptid_equal (tp->ptid, ptid))
178b725ae77Skettenis return tp;
179b725ae77Skettenis
180b725ae77Skettenis return NULL;
181b725ae77Skettenis }
182b725ae77Skettenis
183b725ae77Skettenis /*
184b725ae77Skettenis * Thread iterator function.
185b725ae77Skettenis *
186b725ae77Skettenis * Calls a callback function once for each thread, so long as
187b725ae77Skettenis * the callback function returns false. If the callback function
188b725ae77Skettenis * returns true, the iteration will end and the current thread
189b725ae77Skettenis * will be returned. This can be useful for implementing a
190b725ae77Skettenis * search for a thread with arbitrary attributes, or for applying
191b725ae77Skettenis * some operation to every thread.
192b725ae77Skettenis *
193b725ae77Skettenis * FIXME: some of the existing functionality, such as
194b725ae77Skettenis * "Thread apply all", might be rewritten using this functionality.
195b725ae77Skettenis */
196b725ae77Skettenis
197b725ae77Skettenis struct thread_info *
iterate_over_threads(int (* callback)(struct thread_info *,void *),void * data)198b725ae77Skettenis iterate_over_threads (int (*callback) (struct thread_info *, void *),
199b725ae77Skettenis void *data)
200b725ae77Skettenis {
201b725ae77Skettenis struct thread_info *tp;
202b725ae77Skettenis
203b725ae77Skettenis for (tp = thread_list; tp; tp = tp->next)
204b725ae77Skettenis if ((*callback) (tp, data))
205b725ae77Skettenis return tp;
206b725ae77Skettenis
207b725ae77Skettenis return NULL;
208b725ae77Skettenis }
209b725ae77Skettenis
210e93f7393Sniklas int
valid_thread_id(int num)211b725ae77Skettenis valid_thread_id (int num)
212e93f7393Sniklas {
213e93f7393Sniklas struct thread_info *tp;
214e93f7393Sniklas
215e93f7393Sniklas for (tp = thread_list; tp; tp = tp->next)
216e93f7393Sniklas if (tp->num == num)
217e93f7393Sniklas return 1;
218e93f7393Sniklas
219e93f7393Sniklas return 0;
220e93f7393Sniklas }
221e93f7393Sniklas
222e93f7393Sniklas int
pid_to_thread_id(ptid_t ptid)223b725ae77Skettenis pid_to_thread_id (ptid_t ptid)
224e93f7393Sniklas {
225e93f7393Sniklas struct thread_info *tp;
226e93f7393Sniklas
227e93f7393Sniklas for (tp = thread_list; tp; tp = tp->next)
228b725ae77Skettenis if (ptid_equal (tp->ptid, ptid))
229e93f7393Sniklas return tp->num;
230e93f7393Sniklas
231e93f7393Sniklas return 0;
232e93f7393Sniklas }
233e93f7393Sniklas
234b725ae77Skettenis ptid_t
thread_id_to_pid(int num)235b725ae77Skettenis thread_id_to_pid (int num)
236b725ae77Skettenis {
237b725ae77Skettenis struct thread_info *thread = find_thread_id (num);
238b725ae77Skettenis if (thread)
239b725ae77Skettenis return thread->ptid;
240b725ae77Skettenis else
241b725ae77Skettenis return pid_to_ptid (-1);
242b725ae77Skettenis }
243b725ae77Skettenis
244e93f7393Sniklas int
in_thread_list(ptid_t ptid)245b725ae77Skettenis in_thread_list (ptid_t ptid)
246e93f7393Sniklas {
247e93f7393Sniklas struct thread_info *tp;
248e93f7393Sniklas
249e93f7393Sniklas for (tp = thread_list; tp; tp = tp->next)
250b725ae77Skettenis if (ptid_equal (tp->ptid, ptid))
251e93f7393Sniklas return 1;
252e93f7393Sniklas
253e93f7393Sniklas return 0; /* Never heard of 'im */
254e93f7393Sniklas }
255e93f7393Sniklas
256b725ae77Skettenis /* Print a list of thread ids currently known, and the total number of
257b725ae77Skettenis threads. To be used from within catch_errors. */
258b725ae77Skettenis static int
do_captured_list_thread_ids(struct ui_out * uiout,void * arg)259b725ae77Skettenis do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
260b725ae77Skettenis {
261b725ae77Skettenis struct thread_info *tp;
262b725ae77Skettenis int num = 0;
263b725ae77Skettenis struct cleanup *cleanup_chain;
264b725ae77Skettenis
265b725ae77Skettenis prune_threads ();
266b725ae77Skettenis target_find_new_threads ();
267b725ae77Skettenis
268b725ae77Skettenis cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
269b725ae77Skettenis
270b725ae77Skettenis for (tp = thread_list; tp; tp = tp->next)
271b725ae77Skettenis {
272b725ae77Skettenis num++;
273b725ae77Skettenis ui_out_field_int (uiout, "thread-id", tp->num);
274b725ae77Skettenis }
275b725ae77Skettenis
276b725ae77Skettenis do_cleanups (cleanup_chain);
277b725ae77Skettenis ui_out_field_int (uiout, "number-of-threads", num);
278b725ae77Skettenis return GDB_RC_OK;
279b725ae77Skettenis }
280b725ae77Skettenis
281b725ae77Skettenis /* Official gdblib interface function to get a list of thread ids and
282b725ae77Skettenis the total number. */
283b725ae77Skettenis enum gdb_rc
gdb_list_thread_ids(struct ui_out * uiout)284b725ae77Skettenis gdb_list_thread_ids (struct ui_out *uiout)
285b725ae77Skettenis {
286b725ae77Skettenis return catch_exceptions (uiout, do_captured_list_thread_ids, NULL,
287b725ae77Skettenis NULL, RETURN_MASK_ALL);
288b725ae77Skettenis }
289b725ae77Skettenis
290e93f7393Sniklas /* Load infrun state for the thread PID. */
291e93f7393Sniklas
292b725ae77Skettenis void
load_infrun_state(ptid_t ptid,CORE_ADDR * prev_pc,int * trap_expected,struct breakpoint ** step_resume_breakpoint,CORE_ADDR * step_range_start,CORE_ADDR * step_range_end,struct frame_id * step_frame_id,int * handling_longjmp,int * another_trap,int * stepping_through_solib_after_catch,bpstat * stepping_through_solib_catchpoints,int * current_line,struct symtab ** current_symtab)293b725ae77Skettenis load_infrun_state (ptid_t ptid,
294b725ae77Skettenis CORE_ADDR *prev_pc,
295b725ae77Skettenis int *trap_expected,
296b725ae77Skettenis struct breakpoint **step_resume_breakpoint,
297b725ae77Skettenis CORE_ADDR *step_range_start,
298b725ae77Skettenis CORE_ADDR *step_range_end,
299b725ae77Skettenis struct frame_id *step_frame_id,
300b725ae77Skettenis int *handling_longjmp,
301b725ae77Skettenis int *another_trap,
302b725ae77Skettenis int *stepping_through_solib_after_catch,
303b725ae77Skettenis bpstat *stepping_through_solib_catchpoints,
304b725ae77Skettenis int *current_line,
305*63addd46Skettenis struct symtab **current_symtab)
306e93f7393Sniklas {
307e93f7393Sniklas struct thread_info *tp;
308e93f7393Sniklas
309e93f7393Sniklas /* If we can't find the thread, then we're debugging a single threaded
310e93f7393Sniklas process. No need to do anything in that case. */
311b725ae77Skettenis tp = find_thread_id (pid_to_thread_id (ptid));
312e93f7393Sniklas if (tp == NULL)
313e93f7393Sniklas return;
314e93f7393Sniklas
315e93f7393Sniklas *prev_pc = tp->prev_pc;
316b725ae77Skettenis *trap_expected = tp->trap_expected;
317e93f7393Sniklas *step_resume_breakpoint = tp->step_resume_breakpoint;
318e93f7393Sniklas *step_range_start = tp->step_range_start;
319e93f7393Sniklas *step_range_end = tp->step_range_end;
320b725ae77Skettenis *step_frame_id = tp->step_frame_id;
321e93f7393Sniklas *handling_longjmp = tp->handling_longjmp;
322e93f7393Sniklas *another_trap = tp->another_trap;
323b725ae77Skettenis *stepping_through_solib_after_catch =
324b725ae77Skettenis tp->stepping_through_solib_after_catch;
325b725ae77Skettenis *stepping_through_solib_catchpoints =
326b725ae77Skettenis tp->stepping_through_solib_catchpoints;
327b725ae77Skettenis *current_line = tp->current_line;
328b725ae77Skettenis *current_symtab = tp->current_symtab;
329e93f7393Sniklas }
330e93f7393Sniklas
331e93f7393Sniklas /* Save infrun state for the thread PID. */
332e93f7393Sniklas
333b725ae77Skettenis void
save_infrun_state(ptid_t ptid,CORE_ADDR prev_pc,int trap_expected,struct breakpoint * step_resume_breakpoint,CORE_ADDR step_range_start,CORE_ADDR step_range_end,const struct frame_id * step_frame_id,int handling_longjmp,int another_trap,int stepping_through_solib_after_catch,bpstat stepping_through_solib_catchpoints,int current_line,struct symtab * current_symtab)334b725ae77Skettenis save_infrun_state (ptid_t ptid,
335b725ae77Skettenis CORE_ADDR prev_pc,
336b725ae77Skettenis int trap_expected,
337b725ae77Skettenis struct breakpoint *step_resume_breakpoint,
338b725ae77Skettenis CORE_ADDR step_range_start,
339b725ae77Skettenis CORE_ADDR step_range_end,
340b725ae77Skettenis const struct frame_id *step_frame_id,
341b725ae77Skettenis int handling_longjmp,
342b725ae77Skettenis int another_trap,
343b725ae77Skettenis int stepping_through_solib_after_catch,
344b725ae77Skettenis bpstat stepping_through_solib_catchpoints,
345b725ae77Skettenis int current_line,
346*63addd46Skettenis struct symtab *current_symtab)
347e93f7393Sniklas {
348e93f7393Sniklas struct thread_info *tp;
349e93f7393Sniklas
350e93f7393Sniklas /* If we can't find the thread, then we're debugging a single-threaded
351e93f7393Sniklas process. Nothing to do in that case. */
352b725ae77Skettenis tp = find_thread_id (pid_to_thread_id (ptid));
353e93f7393Sniklas if (tp == NULL)
354e93f7393Sniklas return;
355e93f7393Sniklas
356e93f7393Sniklas tp->prev_pc = prev_pc;
357b725ae77Skettenis tp->trap_expected = trap_expected;
358e93f7393Sniklas tp->step_resume_breakpoint = step_resume_breakpoint;
359e93f7393Sniklas tp->step_range_start = step_range_start;
360e93f7393Sniklas tp->step_range_end = step_range_end;
361b725ae77Skettenis tp->step_frame_id = (*step_frame_id);
362e93f7393Sniklas tp->handling_longjmp = handling_longjmp;
363e93f7393Sniklas tp->another_trap = another_trap;
364b725ae77Skettenis tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
365b725ae77Skettenis tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
366b725ae77Skettenis tp->current_line = current_line;
367b725ae77Skettenis tp->current_symtab = current_symtab;
368b725ae77Skettenis }
369b725ae77Skettenis
370b725ae77Skettenis /* Return true if TP is an active thread. */
371b725ae77Skettenis static int
thread_alive(struct thread_info * tp)372b725ae77Skettenis thread_alive (struct thread_info *tp)
373b725ae77Skettenis {
374b725ae77Skettenis if (PIDGET (tp->ptid) == -1)
375b725ae77Skettenis return 0;
376b725ae77Skettenis if (!target_thread_alive (tp->ptid))
377b725ae77Skettenis {
378b725ae77Skettenis tp->ptid = pid_to_ptid (-1); /* Mark it as dead */
379b725ae77Skettenis return 0;
380b725ae77Skettenis }
381b725ae77Skettenis return 1;
382e93f7393Sniklas }
383e93f7393Sniklas
384e93f7393Sniklas static void
prune_threads(void)385b725ae77Skettenis prune_threads (void)
386e93f7393Sniklas {
387b725ae77Skettenis struct thread_info *tp, *next;
388e93f7393Sniklas
389b725ae77Skettenis for (tp = thread_list; tp; tp = next)
390e93f7393Sniklas {
391b725ae77Skettenis next = tp->next;
392b725ae77Skettenis if (!thread_alive (tp))
393b725ae77Skettenis delete_thread (tp->ptid);
394e93f7393Sniklas }
395e93f7393Sniklas }
396e93f7393Sniklas
397b725ae77Skettenis /* Print information about currently known threads
398b725ae77Skettenis
399b725ae77Skettenis * Note: this has the drawback that it _really_ switches
400b725ae77Skettenis * threads, which frees the frame cache. A no-side
401b725ae77Skettenis * effects info-threads command would be nicer.
402b725ae77Skettenis */
403e93f7393Sniklas
404e93f7393Sniklas static void
info_threads_command(char * arg,int from_tty)405b725ae77Skettenis info_threads_command (char *arg, int from_tty)
406e93f7393Sniklas {
407e93f7393Sniklas struct thread_info *tp;
408b725ae77Skettenis ptid_t current_ptid;
409b725ae77Skettenis struct frame_info *cur_frame;
410*63addd46Skettenis struct frame_id saved_frame_id = get_frame_id (get_selected_frame ());
411b725ae77Skettenis char *extra_info;
412e93f7393Sniklas
413b725ae77Skettenis prune_threads ();
414b725ae77Skettenis target_find_new_threads ();
415b725ae77Skettenis current_ptid = inferior_ptid;
416e93f7393Sniklas for (tp = thread_list; tp; tp = tp->next)
417e93f7393Sniklas {
418b725ae77Skettenis if (ptid_equal (tp->ptid, current_ptid))
419e93f7393Sniklas printf_filtered ("* ");
420e93f7393Sniklas else
421e93f7393Sniklas printf_filtered (" ");
422e93f7393Sniklas
423b725ae77Skettenis printf_filtered ("%d %s", tp->num, target_tid_to_str (tp->ptid));
424e93f7393Sniklas
425b725ae77Skettenis extra_info = target_extra_thread_info (tp);
426b725ae77Skettenis if (extra_info)
427b725ae77Skettenis printf_filtered (" (%s)", extra_info);
428b725ae77Skettenis puts_filtered (" ");
429b725ae77Skettenis
430b725ae77Skettenis switch_to_thread (tp->ptid);
431*63addd46Skettenis print_stack_frame (get_selected_frame (), 0, LOCATION);
432e93f7393Sniklas }
433e93f7393Sniklas
434b725ae77Skettenis switch_to_thread (current_ptid);
435b725ae77Skettenis
436*63addd46Skettenis /* Restores the frame set by the user before the "info threads"
437*63addd46Skettenis command. We have finished the info-threads display by switching
438*63addd46Skettenis back to the current thread. That switch has put us at the top of
439*63addd46Skettenis the stack (leaf frame). */
440*63addd46Skettenis cur_frame = frame_find_by_id (saved_frame_id);
441*63addd46Skettenis if (cur_frame == NULL)
442b725ae77Skettenis {
443b725ae77Skettenis /* Ooops, can't restore, tell user where we are. */
444b725ae77Skettenis warning ("Couldn't restore frame in current thread, at frame 0");
445*63addd46Skettenis print_stack_frame (get_selected_frame (), 0, LOCATION);
446b725ae77Skettenis }
447b725ae77Skettenis else
448b725ae77Skettenis {
449b725ae77Skettenis select_frame (cur_frame);
450b725ae77Skettenis /* re-show current frame. */
451b725ae77Skettenis show_stack_frame (cur_frame);
452e93f7393Sniklas }
453*63addd46Skettenis }
454e93f7393Sniklas
455e93f7393Sniklas /* Switch from one thread to another. */
456e93f7393Sniklas
457e93f7393Sniklas static void
switch_to_thread(ptid_t ptid)458b725ae77Skettenis switch_to_thread (ptid_t ptid)
459e93f7393Sniklas {
460b725ae77Skettenis if (ptid_equal (ptid, inferior_ptid))
461e93f7393Sniklas return;
462e93f7393Sniklas
463b725ae77Skettenis inferior_ptid = ptid;
464e93f7393Sniklas flush_cached_frames ();
465e93f7393Sniklas registers_changed ();
466e93f7393Sniklas stop_pc = read_pc ();
467b725ae77Skettenis select_frame (get_current_frame ());
468e93f7393Sniklas }
469e93f7393Sniklas
470e93f7393Sniklas static void
restore_current_thread(ptid_t ptid)471b725ae77Skettenis restore_current_thread (ptid_t ptid)
472e93f7393Sniklas {
473b725ae77Skettenis if (!ptid_equal (ptid, inferior_ptid))
474b725ae77Skettenis {
475b725ae77Skettenis switch_to_thread (ptid);
476*63addd46Skettenis print_stack_frame (get_current_frame (), 1, SRC_LINE);
477b725ae77Skettenis }
478b725ae77Skettenis }
479b725ae77Skettenis
480b725ae77Skettenis struct current_thread_cleanup
481b725ae77Skettenis {
482b725ae77Skettenis ptid_t inferior_ptid;
483b725ae77Skettenis };
484b725ae77Skettenis
485b725ae77Skettenis static void
do_restore_current_thread_cleanup(void * arg)486b725ae77Skettenis do_restore_current_thread_cleanup (void *arg)
487b725ae77Skettenis {
488b725ae77Skettenis struct current_thread_cleanup *old = arg;
489b725ae77Skettenis restore_current_thread (old->inferior_ptid);
490b725ae77Skettenis xfree (old);
491b725ae77Skettenis }
492b725ae77Skettenis
493b725ae77Skettenis static struct cleanup *
make_cleanup_restore_current_thread(ptid_t inferior_ptid)494b725ae77Skettenis make_cleanup_restore_current_thread (ptid_t inferior_ptid)
495b725ae77Skettenis {
496b725ae77Skettenis struct current_thread_cleanup *old
497b725ae77Skettenis = xmalloc (sizeof (struct current_thread_cleanup));
498b725ae77Skettenis old->inferior_ptid = inferior_ptid;
499b725ae77Skettenis return make_cleanup (do_restore_current_thread_cleanup, old);
500e93f7393Sniklas }
501e93f7393Sniklas
502e93f7393Sniklas /* Apply a GDB command to a list of threads. List syntax is a whitespace
503e93f7393Sniklas seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
504e93f7393Sniklas of two numbers seperated by a hyphen. Examples:
505e93f7393Sniklas
506e93f7393Sniklas thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
507e93f7393Sniklas thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
508e93f7393Sniklas thread apply all p x/i $pc Apply x/i $pc cmd to all threads
509e93f7393Sniklas */
510e93f7393Sniklas
511e93f7393Sniklas static void
thread_apply_all_command(char * cmd,int from_tty)512b725ae77Skettenis thread_apply_all_command (char *cmd, int from_tty)
513e93f7393Sniklas {
514e93f7393Sniklas struct thread_info *tp;
515e93f7393Sniklas struct cleanup *old_chain;
516b725ae77Skettenis struct cleanup *saved_cmd_cleanup_chain;
517b725ae77Skettenis char *saved_cmd;
518e93f7393Sniklas
519e93f7393Sniklas if (cmd == NULL || *cmd == '\000')
520e93f7393Sniklas error ("Please specify a command following the thread ID list");
521e93f7393Sniklas
522b725ae77Skettenis old_chain = make_cleanup_restore_current_thread (inferior_ptid);
523e93f7393Sniklas
524b725ae77Skettenis /* It is safe to update the thread list now, before
525b725ae77Skettenis traversing it for "thread apply all". MVS */
526b725ae77Skettenis target_find_new_threads ();
527b725ae77Skettenis
528b725ae77Skettenis /* Save a copy of the command in case it is clobbered by
529b725ae77Skettenis execute_command */
530b725ae77Skettenis saved_cmd = xstrdup (cmd);
531b725ae77Skettenis saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
532e93f7393Sniklas for (tp = thread_list; tp; tp = tp->next)
533b725ae77Skettenis if (thread_alive (tp))
534e93f7393Sniklas {
535b725ae77Skettenis switch_to_thread (tp->ptid);
536b725ae77Skettenis printf_filtered ("\nThread %d (%s):\n",
537b725ae77Skettenis tp->num, target_tid_to_str (inferior_ptid));
538e93f7393Sniklas execute_command (cmd, from_tty);
539b725ae77Skettenis strcpy (cmd, saved_cmd); /* Restore exact command used previously */
540e93f7393Sniklas }
541b725ae77Skettenis
542b725ae77Skettenis do_cleanups (saved_cmd_cleanup_chain);
543b725ae77Skettenis do_cleanups (old_chain);
544e93f7393Sniklas }
545e93f7393Sniklas
546e93f7393Sniklas static void
thread_apply_command(char * tidlist,int from_tty)547b725ae77Skettenis thread_apply_command (char *tidlist, int from_tty)
548e93f7393Sniklas {
549e93f7393Sniklas char *cmd;
550e93f7393Sniklas char *p;
551e93f7393Sniklas struct cleanup *old_chain;
552b725ae77Skettenis struct cleanup *saved_cmd_cleanup_chain;
553b725ae77Skettenis char *saved_cmd;
554e93f7393Sniklas
555e93f7393Sniklas if (tidlist == NULL || *tidlist == '\000')
556e93f7393Sniklas error ("Please specify a thread ID list");
557e93f7393Sniklas
558e93f7393Sniklas for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
559e93f7393Sniklas
560e93f7393Sniklas if (*cmd == '\000')
561e93f7393Sniklas error ("Please specify a command following the thread ID list");
562e93f7393Sniklas
563b725ae77Skettenis old_chain = make_cleanup_restore_current_thread (inferior_ptid);
564e93f7393Sniklas
565b725ae77Skettenis /* Save a copy of the command in case it is clobbered by
566b725ae77Skettenis execute_command */
567b725ae77Skettenis saved_cmd = xstrdup (cmd);
568b725ae77Skettenis saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
569e93f7393Sniklas while (tidlist < cmd)
570e93f7393Sniklas {
571e93f7393Sniklas struct thread_info *tp;
572e93f7393Sniklas int start, end;
573e93f7393Sniklas
574e93f7393Sniklas start = strtol (tidlist, &p, 10);
575e93f7393Sniklas if (p == tidlist)
576e93f7393Sniklas error ("Error parsing %s", tidlist);
577e93f7393Sniklas tidlist = p;
578e93f7393Sniklas
579e93f7393Sniklas while (*tidlist == ' ' || *tidlist == '\t')
580e93f7393Sniklas tidlist++;
581e93f7393Sniklas
582e93f7393Sniklas if (*tidlist == '-') /* Got a range of IDs? */
583e93f7393Sniklas {
584e93f7393Sniklas tidlist++; /* Skip the - */
585e93f7393Sniklas end = strtol (tidlist, &p, 10);
586e93f7393Sniklas if (p == tidlist)
587e93f7393Sniklas error ("Error parsing %s", tidlist);
588e93f7393Sniklas tidlist = p;
589e93f7393Sniklas
590e93f7393Sniklas while (*tidlist == ' ' || *tidlist == '\t')
591e93f7393Sniklas tidlist++;
592e93f7393Sniklas }
593e93f7393Sniklas else
594e93f7393Sniklas end = start;
595e93f7393Sniklas
596e93f7393Sniklas for (; start <= end; start++)
597e93f7393Sniklas {
598e93f7393Sniklas tp = find_thread_id (start);
599e93f7393Sniklas
600e93f7393Sniklas if (!tp)
601e93f7393Sniklas warning ("Unknown thread %d.", start);
602b725ae77Skettenis else if (!thread_alive (tp))
603b725ae77Skettenis warning ("Thread %d has terminated.", start);
604b725ae77Skettenis else
605b725ae77Skettenis {
606b725ae77Skettenis switch_to_thread (tp->ptid);
607b725ae77Skettenis printf_filtered ("\nThread %d (%s):\n", tp->num,
608b725ae77Skettenis target_tid_to_str (inferior_ptid));
609b725ae77Skettenis execute_command (cmd, from_tty);
610b725ae77Skettenis strcpy (cmd, saved_cmd); /* Restore exact command used previously */
611b725ae77Skettenis }
612b725ae77Skettenis }
613e93f7393Sniklas }
614e93f7393Sniklas
615b725ae77Skettenis do_cleanups (saved_cmd_cleanup_chain);
616b725ae77Skettenis do_cleanups (old_chain);
617e93f7393Sniklas }
618e93f7393Sniklas
619e93f7393Sniklas /* Switch to the specified thread. Will dispatch off to thread_apply_command
620e93f7393Sniklas if prefix of arg is `apply'. */
621e93f7393Sniklas
622e93f7393Sniklas static void
thread_command(char * tidstr,int from_tty)623b725ae77Skettenis thread_command (char *tidstr, int from_tty)
624b725ae77Skettenis {
625b725ae77Skettenis if (!tidstr)
626b725ae77Skettenis {
627b725ae77Skettenis /* Don't generate an error, just say which thread is current. */
628b725ae77Skettenis if (target_has_stack)
629b725ae77Skettenis printf_filtered ("[Current thread is %d (%s)]\n",
630b725ae77Skettenis pid_to_thread_id (inferior_ptid),
631*63addd46Skettenis target_tid_to_str (inferior_ptid));
632b725ae77Skettenis else
633b725ae77Skettenis error ("No stack.");
634b725ae77Skettenis return;
635b725ae77Skettenis }
636b725ae77Skettenis
637b725ae77Skettenis gdb_thread_select (uiout, tidstr);
638b725ae77Skettenis }
639b725ae77Skettenis
640b725ae77Skettenis static int
do_captured_thread_select(struct ui_out * uiout,void * tidstr)641b725ae77Skettenis do_captured_thread_select (struct ui_out *uiout, void *tidstr)
642e93f7393Sniklas {
643e93f7393Sniklas int num;
644e93f7393Sniklas struct thread_info *tp;
645e93f7393Sniklas
646b725ae77Skettenis num = value_as_long (parse_and_eval (tidstr));
647e93f7393Sniklas
648e93f7393Sniklas tp = find_thread_id (num);
649e93f7393Sniklas
650e93f7393Sniklas if (!tp)
651b725ae77Skettenis error ("Thread ID %d not known.", num);
652e93f7393Sniklas
653b725ae77Skettenis if (!thread_alive (tp))
654b725ae77Skettenis error ("Thread ID %d has terminated.\n", num);
655e93f7393Sniklas
656b725ae77Skettenis switch_to_thread (tp->ptid);
657b725ae77Skettenis
658b725ae77Skettenis ui_out_text (uiout, "[Switching to thread ");
659b725ae77Skettenis ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
660b725ae77Skettenis ui_out_text (uiout, " (");
661b725ae77Skettenis ui_out_text (uiout, target_tid_to_str (inferior_ptid));
662b725ae77Skettenis ui_out_text (uiout, ")]");
663b725ae77Skettenis
664*63addd46Skettenis print_stack_frame (get_selected_frame (), 1, SRC_AND_LOC);
665b725ae77Skettenis return GDB_RC_OK;
666e93f7393Sniklas }
667e93f7393Sniklas
668b725ae77Skettenis enum gdb_rc
gdb_thread_select(struct ui_out * uiout,char * tidstr)669b725ae77Skettenis gdb_thread_select (struct ui_out *uiout, char *tidstr)
670e93f7393Sniklas {
671b725ae77Skettenis return catch_exceptions (uiout, do_captured_thread_select, tidstr,
672b725ae77Skettenis NULL, RETURN_MASK_ALL);
673b725ae77Skettenis }
674b725ae77Skettenis
675b725ae77Skettenis /* Commands with a prefix of `thread'. */
676b725ae77Skettenis struct cmd_list_element *thread_cmd_list = NULL;
677b725ae77Skettenis
678b725ae77Skettenis void
_initialize_thread(void)679b725ae77Skettenis _initialize_thread (void)
680b725ae77Skettenis {
681e93f7393Sniklas static struct cmd_list_element *thread_apply_list = NULL;
682e93f7393Sniklas
683e93f7393Sniklas add_info ("threads", info_threads_command,
684e93f7393Sniklas "IDs of currently known threads.");
685e93f7393Sniklas
686e93f7393Sniklas add_prefix_cmd ("thread", class_run, thread_command,
687e93f7393Sniklas "Use this command to switch between threads.\n\
688b725ae77Skettenis The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1, &cmdlist);
689e93f7393Sniklas
690e93f7393Sniklas add_prefix_cmd ("apply", class_run, thread_apply_command,
691e93f7393Sniklas "Apply a command to a list of threads.",
692e93f7393Sniklas &thread_apply_list, "apply ", 1, &thread_cmd_list);
693e93f7393Sniklas
694e93f7393Sniklas add_cmd ("all", class_run, thread_apply_all_command,
695b725ae77Skettenis "Apply a command to all threads.", &thread_apply_list);
696e93f7393Sniklas
697b725ae77Skettenis if (!xdb_commands)
698e93f7393Sniklas add_com_alias ("t", "thread", class_run, 1);
699e93f7393Sniklas }
700