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
;
273 /* Return true if TP is an active thread. */
276 struct thread_info
*tp
;
280 if (! target_thread_alive (tp
->pid
))
282 tp
->pid
= -1; /* Mark it as dead */
291 struct thread_info
*tp
, *tpprev
, *next
;
294 for (tp
= thread_list
; tp
; tp
= next
)
297 if (!thread_alive (tp
))
310 /* Print information about currently known threads */
313 info_threads_command (arg
, from_tty
)
317 struct thread_info
*tp
;
318 int current_pid
= inferior_pid
;
320 /* Avoid coredumps which would happen if we tried to access a NULL
322 if (!target_has_stack
) error ("No stack.");
325 for (tp
= thread_list
; tp
; tp
= tp
->next
)
327 if (tp
->pid
== current_pid
)
328 printf_filtered ("* ");
330 printf_filtered (" ");
332 printf_filtered ("%d %s ", tp
->num
, target_pid_to_str (tp
->pid
));
334 switch_to_thread (tp
->pid
);
335 print_stack_frame (selected_frame
, -1, 0);
338 switch_to_thread (current_pid
);
341 /* Switch from one thread to another. */
344 switch_to_thread (pid
)
347 if (pid
== inferior_pid
)
351 flush_cached_frames ();
352 registers_changed ();
354 select_frame (get_current_frame (), 0);
358 restore_current_thread (pid
)
361 if (pid
!= inferior_pid
)
362 switch_to_thread (pid
);
365 /* Apply a GDB command to a list of threads. List syntax is a whitespace
366 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
367 of two numbers seperated by a hyphen. Examples:
369 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
370 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
371 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
375 thread_apply_all_command (cmd
, from_tty
)
379 struct thread_info
*tp
;
380 struct cleanup
*old_chain
;
382 if (cmd
== NULL
|| *cmd
== '\000')
383 error ("Please specify a command following the thread ID list");
385 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
387 for (tp
= thread_list
; tp
; tp
= tp
->next
)
388 if (thread_alive (tp
))
390 switch_to_thread (tp
->pid
);
391 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
392 target_pid_to_str (inferior_pid
));
393 execute_command (cmd
, from_tty
);
398 thread_apply_command (tidlist
, from_tty
)
404 struct cleanup
*old_chain
;
406 if (tidlist
== NULL
|| *tidlist
== '\000')
407 error ("Please specify a thread ID list");
409 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha(*cmd
); cmd
++);
412 error ("Please specify a command following the thread ID list");
414 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
416 while (tidlist
< cmd
)
418 struct thread_info
*tp
;
421 start
= strtol (tidlist
, &p
, 10);
423 error ("Error parsing %s", tidlist
);
426 while (*tidlist
== ' ' || *tidlist
== '\t')
429 if (*tidlist
== '-') /* Got a range of IDs? */
431 tidlist
++; /* Skip the - */
432 end
= strtol (tidlist
, &p
, 10);
434 error ("Error parsing %s", tidlist
);
437 while (*tidlist
== ' ' || *tidlist
== '\t')
443 for (; start
<= end
; start
++)
445 tp
= find_thread_id (start
);
448 warning ("Unknown thread %d.", start
);
449 else if (!thread_alive (tp
))
450 warning ("Thread %d has terminated.", start
);
453 switch_to_thread (tp
->pid
);
454 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
455 target_pid_to_str (inferior_pid
));
456 execute_command (cmd
, from_tty
);
462 /* Switch to the specified thread. Will dispatch off to thread_apply_command
463 if prefix of arg is `apply'. */
466 thread_command (tidstr
, from_tty
)
471 struct thread_info
*tp
;
474 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
475 see the IDs of currently known threads.");
479 tp
= find_thread_id (num
);
482 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
483 see the IDs of currently known threads.", num
);
485 if (!thread_alive (tp
))
486 error ("Thread ID %d has terminated.\n", num
);
488 switch_to_thread (tp
->pid
);
490 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid
));
491 print_stack_frame (selected_frame
, selected_frame_level
, 1);
494 /* Commands with a prefix of `thread'. */
495 struct cmd_list_element
*thread_cmd_list
= NULL
;
498 _initialize_thread ()
500 static struct cmd_list_element
*thread_apply_list
= NULL
;
501 extern struct cmd_list_element
*cmdlist
;
503 add_info ("threads", info_threads_command
,
504 "IDs of currently known threads.");
506 add_prefix_cmd ("thread", class_run
, thread_command
,
507 "Use this command to switch between threads.\n\
508 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
511 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
512 "Apply a command to a list of threads.",
513 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
515 add_cmd ("all", class_run
, thread_apply_all_command
,
516 "Apply a command to all threads.",
519 add_com_alias ("t", "thread", class_run
, 1);