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 thread_switch
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
)
171 struct thread_info
*tp
;
173 for (tp
= thread_list
; tp
; tp
= tp
->next
)
177 return 0; /* Never heard of 'im */
180 /* Load infrun state for the thread PID. */
182 void load_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
183 trap_expected
, step_resume_breakpoint
,
184 through_sigtramp_breakpoint
, step_range_start
,
185 step_range_end
, step_frame_address
,
186 handling_longjmp
, another_trap
)
189 CORE_ADDR
*prev_func_start
;
190 char **prev_func_name
;
192 struct breakpoint
**step_resume_breakpoint
;
193 struct breakpoint
**through_sigtramp_breakpoint
;
194 CORE_ADDR
*step_range_start
;
195 CORE_ADDR
*step_range_end
;
196 CORE_ADDR
*step_frame_address
;
197 int *handling_longjmp
;
200 struct thread_info
*tp
;
202 /* If we can't find the thread, then we're debugging a single threaded
203 process. No need to do anything in that case. */
204 tp
= find_thread_id (pid_to_thread_id (pid
));
208 *prev_pc
= tp
->prev_pc
;
209 *prev_func_start
= tp
->prev_func_start
;
210 *prev_func_name
= tp
->prev_func_name
;
211 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
212 *step_range_start
= tp
->step_range_start
;
213 *step_range_end
= tp
->step_range_end
;
214 *step_frame_address
= tp
->step_frame_address
;
215 *through_sigtramp_breakpoint
= tp
->through_sigtramp_breakpoint
;
216 *handling_longjmp
= tp
->handling_longjmp
;
217 *trap_expected
= tp
->trap_expected
;
218 *another_trap
= tp
->another_trap
;
221 /* Save infrun state for the thread PID. */
223 void save_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
224 trap_expected
, step_resume_breakpoint
,
225 through_sigtramp_breakpoint
, step_range_start
,
226 step_range_end
, step_frame_address
,
227 handling_longjmp
, another_trap
)
230 CORE_ADDR prev_func_start
;
231 char *prev_func_name
;
233 struct breakpoint
*step_resume_breakpoint
;
234 struct breakpoint
*through_sigtramp_breakpoint
;
235 CORE_ADDR step_range_start
;
236 CORE_ADDR step_range_end
;
237 CORE_ADDR step_frame_address
;
238 int handling_longjmp
;
241 struct thread_info
*tp
;
243 /* If we can't find the thread, then we're debugging a single-threaded
244 process. Nothing to do in that case. */
245 tp
= find_thread_id (pid_to_thread_id (pid
));
249 tp
->prev_pc
= prev_pc
;
250 tp
->prev_func_start
= prev_func_start
;
251 tp
->prev_func_name
= prev_func_name
;
252 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
253 tp
->step_range_start
= step_range_start
;
254 tp
->step_range_end
= step_range_end
;
255 tp
->step_frame_address
= step_frame_address
;
256 tp
->through_sigtramp_breakpoint
= through_sigtramp_breakpoint
;
257 tp
->handling_longjmp
= handling_longjmp
;
258 tp
->trap_expected
= trap_expected
;
259 tp
->another_trap
= another_trap
;
265 struct thread_info
*tp
, *tpprev
;
269 for (tp
= thread_list
; tp
; tp
= tp
->next
)
273 tpprev
->next
= tp
->next
;
283 /* Print information about currently known threads */
286 info_threads_command (arg
, from_tty
)
290 struct thread_info
*tp
;
291 int current_pid
= inferior_pid
;
293 /* Avoid coredumps which would happen if we tried to access a NULL
295 if (!target_has_stack
) error ("No stack.");
297 for (tp
= thread_list
; tp
; tp
= tp
->next
)
299 if (! target_thread_alive (tp
->pid
))
301 tp
->pid
= -1; /* Mark it as dead */
305 if (tp
->pid
== current_pid
)
306 printf_filtered ("* ");
308 printf_filtered (" ");
310 printf_filtered ("%d %s ", tp
->num
, target_pid_to_str (tp
->pid
));
312 thread_switch (tp
->pid
);
313 print_stack_frame (selected_frame
, -1, 0);
316 thread_switch (current_pid
);
320 /* Switch from one thread to another. */
326 if (pid
== inferior_pid
)
330 flush_cached_frames ();
331 registers_changed ();
333 select_frame (get_current_frame (), 0);
337 restore_current_thread (pid
)
340 if (pid
!= inferior_pid
)
344 /* Apply a GDB command to a list of threads. List syntax is a whitespace
345 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
346 of two numbers seperated by a hyphen. Examples:
348 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
349 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
350 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
354 thread_apply_all_command (cmd
, from_tty
)
358 struct thread_info
*tp
;
359 struct cleanup
*old_chain
;
361 if (cmd
== NULL
|| *cmd
== '\000')
362 error ("Please specify a command following the thread ID list");
364 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
366 for (tp
= thread_list
; tp
; tp
= tp
->next
)
368 thread_switch (tp
->pid
);
369 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
370 target_pid_to_str (inferior_pid
));
371 execute_command (cmd
, from_tty
);
376 thread_apply_command (tidlist
, from_tty
)
382 struct cleanup
*old_chain
;
384 if (tidlist
== NULL
|| *tidlist
== '\000')
385 error ("Please specify a thread ID list");
387 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha(*cmd
); cmd
++);
390 error ("Please specify a command following the thread ID list");
392 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
394 while (tidlist
< cmd
)
396 struct thread_info
*tp
;
399 start
= strtol (tidlist
, &p
, 10);
401 error ("Error parsing %s", tidlist
);
404 while (*tidlist
== ' ' || *tidlist
== '\t')
407 if (*tidlist
== '-') /* Got a range of IDs? */
409 tidlist
++; /* Skip the - */
410 end
= strtol (tidlist
, &p
, 10);
412 error ("Error parsing %s", tidlist
);
415 while (*tidlist
== ' ' || *tidlist
== '\t')
421 for (; start
<= end
; start
++)
423 tp
= find_thread_id (start
);
427 warning ("Unknown thread %d.", start
);
431 thread_switch (tp
->pid
);
432 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
433 target_pid_to_str (inferior_pid
));
434 execute_command (cmd
, from_tty
);
439 /* Switch to the specified thread. Will dispatch off to thread_apply_command
440 if prefix of arg is `apply'. */
443 thread_command (tidstr
, from_tty
)
448 struct thread_info
*tp
;
451 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
452 see the IDs of currently known threads.");
456 tp
= find_thread_id (num
);
459 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
460 see the IDs of currently known threads.", num
);
462 thread_switch (tp
->pid
);
464 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid
));
465 print_stack_frame (selected_frame
, selected_frame_level
, 1);
469 _initialize_thread ()
471 static struct cmd_list_element
*thread_cmd_list
= NULL
;
472 static struct cmd_list_element
*thread_apply_list
= NULL
;
473 extern struct cmd_list_element
*cmdlist
;
475 add_info ("threads", info_threads_command
,
476 "IDs of currently known threads.");
478 add_prefix_cmd ("thread", class_run
, thread_command
,
479 "Use this command to switch between threads.\n\
480 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
483 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
484 "Apply a command to a list of threads.",
485 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
487 add_cmd ("all", class_run
, thread_apply_all_command
,
488 "Apply a command to all threads.",
491 add_com_alias ("t", "thread", class_run
, 1);