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
;
62 thread_command
PARAMS ((char * tidstr
, int from_tty
));
65 prune_threads
PARAMS ((void));
68 switch_to_thread
PARAMS ((int pid
));
70 static struct thread_info
*
71 find_thread_id
PARAMS ((int num
));
74 info_threads_command
PARAMS ((char *, int));
77 restore_current_thread
PARAMS ((int));
80 thread_apply_all_command
PARAMS ((char *, int));
83 thread_apply_command
PARAMS ((char *, int));
88 struct thread_info
*tp
, *tpnext
;
93 for (tp
= thread_list
; tp
; tp
= tpnext
)
100 highest_thread_num
= 0;
107 struct thread_info
*tp
;
109 tp
= (struct thread_info
*) xmalloc (sizeof (struct thread_info
));
112 tp
->num
= ++highest_thread_num
;
114 tp
->prev_func_start
= 0;
115 tp
->prev_func_name
= NULL
;
116 tp
->step_range_start
= 0;
117 tp
->step_range_end
= 0;
118 tp
->step_frame_address
=0;
119 tp
->step_resume_breakpoint
= 0;
120 tp
->through_sigtramp_breakpoint
= 0;
121 tp
->handling_longjmp
= 0;
122 tp
->trap_expected
= 0;
123 tp
->another_trap
= 0;
124 tp
->next
= thread_list
;
128 static struct thread_info
*
132 struct thread_info
*tp
;
134 for (tp
= thread_list
; tp
; tp
= tp
->next
)
142 valid_thread_id (num
)
145 struct thread_info
*tp
;
147 for (tp
= thread_list
; tp
; tp
= tp
->next
)
155 pid_to_thread_id (pid
)
158 struct thread_info
*tp
;
160 for (tp
= thread_list
; tp
; tp
= tp
->next
)
168 thread_id_to_pid (num
)
171 struct thread_info
*thread
= find_thread_id (num
);
182 struct thread_info
*tp
;
184 for (tp
= thread_list
; tp
; tp
= tp
->next
)
188 return 0; /* Never heard of 'im */
191 /* Load infrun state for the thread PID. */
193 void load_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
194 trap_expected
, step_resume_breakpoint
,
195 through_sigtramp_breakpoint
, step_range_start
,
196 step_range_end
, step_frame_address
,
197 handling_longjmp
, another_trap
)
200 CORE_ADDR
*prev_func_start
;
201 char **prev_func_name
;
203 struct breakpoint
**step_resume_breakpoint
;
204 struct breakpoint
**through_sigtramp_breakpoint
;
205 CORE_ADDR
*step_range_start
;
206 CORE_ADDR
*step_range_end
;
207 CORE_ADDR
*step_frame_address
;
208 int *handling_longjmp
;
211 struct thread_info
*tp
;
213 /* If we can't find the thread, then we're debugging a single threaded
214 process. No need to do anything in that case. */
215 tp
= find_thread_id (pid_to_thread_id (pid
));
219 *prev_pc
= tp
->prev_pc
;
220 *prev_func_start
= tp
->prev_func_start
;
221 *prev_func_name
= tp
->prev_func_name
;
222 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
223 *step_range_start
= tp
->step_range_start
;
224 *step_range_end
= tp
->step_range_end
;
225 *step_frame_address
= tp
->step_frame_address
;
226 *through_sigtramp_breakpoint
= tp
->through_sigtramp_breakpoint
;
227 *handling_longjmp
= tp
->handling_longjmp
;
228 *trap_expected
= tp
->trap_expected
;
229 *another_trap
= tp
->another_trap
;
232 /* Save infrun state for the thread PID. */
234 void save_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
235 trap_expected
, step_resume_breakpoint
,
236 through_sigtramp_breakpoint
, step_range_start
,
237 step_range_end
, step_frame_address
,
238 handling_longjmp
, another_trap
)
241 CORE_ADDR prev_func_start
;
242 char *prev_func_name
;
244 struct breakpoint
*step_resume_breakpoint
;
245 struct breakpoint
*through_sigtramp_breakpoint
;
246 CORE_ADDR step_range_start
;
247 CORE_ADDR step_range_end
;
248 CORE_ADDR step_frame_address
;
249 int handling_longjmp
;
252 struct thread_info
*tp
;
254 /* If we can't find the thread, then we're debugging a single-threaded
255 process. Nothing to do in that case. */
256 tp
= find_thread_id (pid_to_thread_id (pid
));
260 tp
->prev_pc
= prev_pc
;
261 tp
->prev_func_start
= prev_func_start
;
262 tp
->prev_func_name
= prev_func_name
;
263 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
264 tp
->step_range_start
= step_range_start
;
265 tp
->step_range_end
= step_range_end
;
266 tp
->step_frame_address
= step_frame_address
;
267 tp
->through_sigtramp_breakpoint
= through_sigtramp_breakpoint
;
268 tp
->handling_longjmp
= handling_longjmp
;
269 tp
->trap_expected
= trap_expected
;
270 tp
->another_trap
= another_trap
;
276 struct thread_info
*tp
, *tpprev
;
280 for (tp
= thread_list
; tp
; tp
= tp
->next
)
284 tpprev
->next
= tp
->next
;
294 /* Print information about currently known threads */
297 info_threads_command (arg
, from_tty
)
301 struct thread_info
*tp
;
302 int current_pid
= inferior_pid
;
304 /* Avoid coredumps which would happen if we tried to access a NULL
306 if (!target_has_stack
) error ("No stack.");
308 for (tp
= thread_list
; tp
; tp
= tp
->next
)
310 if (! target_thread_alive (tp
->pid
))
312 tp
->pid
= -1; /* Mark it as dead */
316 if (tp
->pid
== current_pid
)
317 printf_filtered ("* ");
319 printf_filtered (" ");
321 printf_filtered ("%d %s ", tp
->num
, target_pid_to_str (tp
->pid
));
323 switch_to_thread (tp
->pid
);
324 print_stack_frame (selected_frame
, -1, 0);
327 switch_to_thread (current_pid
);
331 /* Switch from one thread to another. */
334 switch_to_thread (pid
)
337 if (pid
== inferior_pid
)
341 flush_cached_frames ();
342 registers_changed ();
344 select_frame (get_current_frame (), 0);
348 restore_current_thread (pid
)
351 if (pid
!= inferior_pid
)
352 switch_to_thread (pid
);
355 /* Apply a GDB command to a list of threads. List syntax is a whitespace
356 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
357 of two numbers seperated by a hyphen. Examples:
359 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
360 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
361 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
365 thread_apply_all_command (cmd
, from_tty
)
369 struct thread_info
*tp
;
370 struct cleanup
*old_chain
;
372 if (cmd
== NULL
|| *cmd
== '\000')
373 error ("Please specify a command following the thread ID list");
375 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
377 for (tp
= thread_list
; tp
; tp
= tp
->next
)
379 switch_to_thread (tp
->pid
);
380 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
381 target_pid_to_str (inferior_pid
));
382 execute_command (cmd
, from_tty
);
387 thread_apply_command (tidlist
, from_tty
)
393 struct cleanup
*old_chain
;
395 if (tidlist
== NULL
|| *tidlist
== '\000')
396 error ("Please specify a thread ID list");
398 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha(*cmd
); cmd
++);
401 error ("Please specify a command following the thread ID list");
403 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
405 while (tidlist
< cmd
)
407 struct thread_info
*tp
;
410 start
= strtol (tidlist
, &p
, 10);
412 error ("Error parsing %s", tidlist
);
415 while (*tidlist
== ' ' || *tidlist
== '\t')
418 if (*tidlist
== '-') /* Got a range of IDs? */
420 tidlist
++; /* Skip the - */
421 end
= strtol (tidlist
, &p
, 10);
423 error ("Error parsing %s", tidlist
);
426 while (*tidlist
== ' ' || *tidlist
== '\t')
432 for (; start
<= end
; start
++)
434 tp
= find_thread_id (start
);
438 warning ("Unknown thread %d.", start
);
442 switch_to_thread (tp
->pid
);
443 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
444 target_pid_to_str (inferior_pid
));
445 execute_command (cmd
, from_tty
);
450 /* Switch to the specified thread. Will dispatch off to thread_apply_command
451 if prefix of arg is `apply'. */
454 thread_command (tidstr
, from_tty
)
459 struct thread_info
*tp
;
462 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
463 see the IDs of currently known threads.");
467 tp
= find_thread_id (num
);
470 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
471 see the IDs of currently known threads.", num
);
473 switch_to_thread (tp
->pid
);
475 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid
));
476 print_stack_frame (selected_frame
, selected_frame_level
, 1);
479 /* Commands with a prefix of `thread'. */
480 struct cmd_list_element
*thread_cmd_list
= NULL
;
483 _initialize_thread ()
485 static struct cmd_list_element
*thread_apply_list
= NULL
;
486 extern struct cmd_list_element
*cmdlist
;
488 add_info ("threads", info_threads_command
,
489 "IDs of currently known threads.");
491 add_prefix_cmd ("thread", class_run
, thread_command
,
492 "Use this command to switch between threads.\n\
493 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
496 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
497 "Apply a command to a list of threads.",
498 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
500 add_cmd ("all", class_run
, thread_apply_all_command
,
501 "Apply a command to all threads.",
504 add_com_alias ("t", "thread", class_run
, 1);