1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993
4 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
5 Free Software Foundation, Inc.
7 This file is part of GDB.
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
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #include "gdbthread.h"
35 #include <sys/types.h>
38 /*#include "lynxos-core.h"*/
42 struct thread_info
*next
;
43 int pid
; /* Actual process id */
44 int num
; /* Convenient handle */
45 CORE_ADDR prev_pc
; /* State from wait_for_inferior */
46 CORE_ADDR prev_func_start
;
48 struct breakpoint
*step_resume_breakpoint
;
49 struct breakpoint
*through_sigtramp_breakpoint
;
50 CORE_ADDR step_range_start
;
51 CORE_ADDR step_range_end
;
52 CORE_ADDR step_frame_address
;
58 static struct thread_info
*thread_list
= NULL
;
59 static int highest_thread_num
;
61 static void thread_command
PARAMS ((char * tidstr
, int from_tty
));
63 static void prune_threads
PARAMS ((void));
65 static void thread_switch
PARAMS ((int pid
));
67 static struct thread_info
* find_thread_id
PARAMS ((int num
));
72 struct thread_info
*tp
, *tpnext
;
77 for (tp
= thread_list
; tp
; tp
= tpnext
)
84 highest_thread_num
= 0;
91 struct thread_info
*tp
;
93 tp
= (struct thread_info
*) xmalloc (sizeof (struct thread_info
));
96 tp
->num
= ++highest_thread_num
;
98 tp
->prev_func_start
= 0;
99 tp
->prev_func_name
= NULL
;
100 tp
->step_range_start
= 0;
101 tp
->step_range_end
= 0;
102 tp
->step_frame_address
=0;
103 tp
->step_resume_breakpoint
= 0;
104 tp
->through_sigtramp_breakpoint
= 0;
105 tp
->handling_longjmp
= 0;
106 tp
->trap_expected
= 0;
107 tp
->another_trap
= 0;
108 tp
->next
= thread_list
;
112 static struct thread_info
*
116 struct thread_info
*tp
;
118 for (tp
= thread_list
; tp
; tp
= tp
->next
)
126 valid_thread_id (num
)
129 struct thread_info
*tp
;
131 for (tp
= thread_list
; tp
; tp
= tp
->next
)
139 pid_to_thread_id (pid
)
142 struct thread_info
*tp
;
144 for (tp
= thread_list
; tp
; tp
= tp
->next
)
155 struct thread_info
*tp
;
157 for (tp
= thread_list
; tp
; tp
= tp
->next
)
161 return 0; /* Never heard of 'im */
164 /* Load infrun state for the thread PID. */
166 void load_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
167 trap_expected
, step_resume_breakpoint
,
168 through_sigtramp_breakpoint
, step_range_start
,
169 step_range_end
, step_frame_address
,
170 handling_longjmp
, another_trap
)
173 CORE_ADDR
*prev_func_start
;
174 char **prev_func_name
;
176 struct breakpoint
**step_resume_breakpoint
;
177 struct breakpoint
**through_sigtramp_breakpoint
;
178 CORE_ADDR
*step_range_start
;
179 CORE_ADDR
*step_range_end
;
180 CORE_ADDR
*step_frame_address
;
181 int *handling_longjmp
;
184 struct thread_info
*tp
;
186 /* If we can't find the thread, then we're debugging a single threaded
187 process. No need to do anything in that case. */
188 tp
= find_thread_id (pid_to_thread_id (pid
));
192 *prev_pc
= tp
->prev_pc
;
193 *prev_func_start
= tp
->prev_func_start
;
194 *prev_func_name
= tp
->prev_func_name
;
195 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
196 *step_range_start
= tp
->step_range_start
;
197 *step_range_end
= tp
->step_range_end
;
198 *step_frame_address
= tp
->step_frame_address
;
199 *through_sigtramp_breakpoint
= tp
->through_sigtramp_breakpoint
;
200 *handling_longjmp
= tp
->handling_longjmp
;
201 *trap_expected
= tp
->trap_expected
;
202 *another_trap
= tp
->another_trap
;
205 /* Save infrun state for the thread PID. */
207 void save_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
208 trap_expected
, step_resume_breakpoint
,
209 through_sigtramp_breakpoint
, step_range_start
,
210 step_range_end
, step_frame_address
,
211 handling_longjmp
, another_trap
)
214 CORE_ADDR prev_func_start
;
215 char *prev_func_name
;
217 struct breakpoint
*step_resume_breakpoint
;
218 struct breakpoint
*through_sigtramp_breakpoint
;
219 CORE_ADDR step_range_start
;
220 CORE_ADDR step_range_end
;
221 CORE_ADDR step_frame_address
;
222 int handling_longjmp
;
225 struct thread_info
*tp
;
227 /* If we can't find the thread, then we're debugging a single-threaded
228 process. Nothing to do in that case. */
229 tp
= find_thread_id (pid_to_thread_id (pid
));
233 tp
->prev_pc
= prev_pc
;
234 tp
->prev_func_start
= prev_func_start
;
235 tp
->prev_func_name
= prev_func_name
;
236 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
237 tp
->step_range_start
= step_range_start
;
238 tp
->step_range_end
= step_range_end
;
239 tp
->step_frame_address
= step_frame_address
;
240 tp
->through_sigtramp_breakpoint
= through_sigtramp_breakpoint
;
241 tp
->handling_longjmp
= handling_longjmp
;
242 tp
->trap_expected
= trap_expected
;
243 tp
->another_trap
= another_trap
;
249 struct thread_info
*tp
, *tpprev
;
253 for (tp
= thread_list
; tp
; tp
= tp
->next
)
257 tpprev
->next
= tp
->next
;
267 /* Print information about currently known threads */
270 info_threads_command (arg
, from_tty
)
274 struct thread_info
*tp
;
275 int current_pid
= inferior_pid
;
277 /* Avoid coredumps which would happen if we tried to access a NULL
279 if (!target_has_stack
) error ("No stack.");
281 for (tp
= thread_list
; tp
; tp
= tp
->next
)
283 if (! target_thread_alive (tp
->pid
))
285 tp
->pid
= -1; /* Mark it as dead */
289 if (tp
->pid
== current_pid
)
290 printf_filtered ("* ");
292 printf_filtered (" ");
294 printf_filtered ("%d %s ", tp
->num
, target_pid_to_str (tp
->pid
));
296 thread_switch (tp
->pid
);
297 print_stack_frame (selected_frame
, -1, 0);
300 thread_switch (current_pid
);
304 /* Switch from one thread to another. */
310 if (pid
== inferior_pid
)
314 flush_cached_frames ();
315 registers_changed ();
317 select_frame (get_current_frame (), 0);
321 restore_current_thread (pid
)
324 if (pid
!= inferior_pid
)
328 /* Apply a GDB command to a list of threads. List syntax is a whitespace
329 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
330 of two numbers seperated by a hyphen. Examples:
332 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
333 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
334 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
338 thread_apply_all_command (cmd
, from_tty
)
342 struct thread_info
*tp
;
343 struct cleanup
*old_chain
;
345 if (cmd
== NULL
|| *cmd
== '\000')
346 error ("Please specify a command following the thread ID list");
348 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
350 for (tp
= thread_list
; tp
; tp
= tp
->next
)
352 thread_switch (tp
->pid
);
353 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
354 target_pid_to_str (inferior_pid
));
355 execute_command (cmd
, from_tty
);
360 thread_apply_command (tidlist
, from_tty
)
366 struct cleanup
*old_chain
;
368 if (tidlist
== NULL
|| *tidlist
== '\000')
369 error ("Please specify a thread ID list");
371 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha(*cmd
); cmd
++);
374 error ("Please specify a command following the thread ID list");
376 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
378 while (tidlist
< cmd
)
380 struct thread_info
*tp
;
383 start
= strtol (tidlist
, &p
, 10);
385 error ("Error parsing %s", tidlist
);
388 while (*tidlist
== ' ' || *tidlist
== '\t')
391 if (*tidlist
== '-') /* Got a range of IDs? */
393 tidlist
++; /* Skip the - */
394 end
= strtol (tidlist
, &p
, 10);
396 error ("Error parsing %s", tidlist
);
399 while (*tidlist
== ' ' || *tidlist
== '\t')
405 for (; start
<= end
; start
++)
407 tp
= find_thread_id (start
);
411 warning ("Unknown thread %d.", start
);
415 thread_switch (tp
->pid
);
416 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
417 target_pid_to_str (inferior_pid
));
418 execute_command (cmd
, from_tty
);
423 /* Switch to the specified thread. Will dispatch off to thread_apply_command
424 if prefix of arg is `apply'. */
427 thread_command (tidstr
, from_tty
)
432 struct thread_info
*tp
;
435 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
436 see the IDs of currently known threads.");
440 tp
= find_thread_id (num
);
443 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
444 see the IDs of currently known threads.", num
);
446 thread_switch (tp
->pid
);
448 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid
));
449 print_stack_frame (selected_frame
, selected_frame_level
, 1);
453 _initialize_thread ()
455 static struct cmd_list_element
*thread_cmd_list
= NULL
;
456 static struct cmd_list_element
*thread_apply_list
= NULL
;
457 extern struct cmd_list_element
*cmdlist
;
459 add_info ("threads", info_threads_command
,
460 "IDs of currently known threads.");
462 add_prefix_cmd ("thread", class_run
, thread_command
,
463 "Use this command to switch between threads.\n\
464 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
467 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
468 "Apply a command to a list of threads.",
469 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
471 add_cmd ("all", class_run
, thread_apply_all_command
,
472 "Apply a command to all threads.",
475 add_com_alias ("t", "thread", class_run
, 1);