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
;
132 struct thread_info
*tp
, *tpprev
;
136 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
144 tpprev
->next
= tp
->next
;
146 thread_list
= tp
->next
;
153 static struct thread_info
*
157 struct thread_info
*tp
;
159 for (tp
= thread_list
; tp
; tp
= tp
->next
)
167 valid_thread_id (num
)
170 struct thread_info
*tp
;
172 for (tp
= thread_list
; tp
; tp
= tp
->next
)
180 pid_to_thread_id (pid
)
183 struct thread_info
*tp
;
185 for (tp
= thread_list
; tp
; tp
= tp
->next
)
193 thread_id_to_pid (num
)
196 struct thread_info
*thread
= find_thread_id (num
);
207 struct thread_info
*tp
;
209 for (tp
= thread_list
; tp
; tp
= tp
->next
)
213 return 0; /* Never heard of 'im */
216 /* Load infrun state for the thread PID. */
218 void load_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
219 trap_expected
, step_resume_breakpoint
,
220 through_sigtramp_breakpoint
, step_range_start
,
221 step_range_end
, step_frame_address
,
222 handling_longjmp
, another_trap
)
225 CORE_ADDR
*prev_func_start
;
226 char **prev_func_name
;
228 struct breakpoint
**step_resume_breakpoint
;
229 struct breakpoint
**through_sigtramp_breakpoint
;
230 CORE_ADDR
*step_range_start
;
231 CORE_ADDR
*step_range_end
;
232 CORE_ADDR
*step_frame_address
;
233 int *handling_longjmp
;
236 struct thread_info
*tp
;
238 /* If we can't find the thread, then we're debugging a single threaded
239 process. No need to do anything in that case. */
240 tp
= find_thread_id (pid_to_thread_id (pid
));
244 *prev_pc
= tp
->prev_pc
;
245 *prev_func_start
= tp
->prev_func_start
;
246 *prev_func_name
= tp
->prev_func_name
;
247 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
248 *step_range_start
= tp
->step_range_start
;
249 *step_range_end
= tp
->step_range_end
;
250 *step_frame_address
= tp
->step_frame_address
;
251 *through_sigtramp_breakpoint
= tp
->through_sigtramp_breakpoint
;
252 *handling_longjmp
= tp
->handling_longjmp
;
253 *trap_expected
= tp
->trap_expected
;
254 *another_trap
= tp
->another_trap
;
257 /* Save infrun state for the thread PID. */
259 void save_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
260 trap_expected
, step_resume_breakpoint
,
261 through_sigtramp_breakpoint
, step_range_start
,
262 step_range_end
, step_frame_address
,
263 handling_longjmp
, another_trap
)
266 CORE_ADDR prev_func_start
;
267 char *prev_func_name
;
269 struct breakpoint
*step_resume_breakpoint
;
270 struct breakpoint
*through_sigtramp_breakpoint
;
271 CORE_ADDR step_range_start
;
272 CORE_ADDR step_range_end
;
273 CORE_ADDR step_frame_address
;
274 int handling_longjmp
;
277 struct thread_info
*tp
;
279 /* If we can't find the thread, then we're debugging a single-threaded
280 process. Nothing to do in that case. */
281 tp
= find_thread_id (pid_to_thread_id (pid
));
285 tp
->prev_pc
= prev_pc
;
286 tp
->prev_func_start
= prev_func_start
;
287 tp
->prev_func_name
= prev_func_name
;
288 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
289 tp
->step_range_start
= step_range_start
;
290 tp
->step_range_end
= step_range_end
;
291 tp
->step_frame_address
= step_frame_address
;
292 tp
->through_sigtramp_breakpoint
= through_sigtramp_breakpoint
;
293 tp
->handling_longjmp
= handling_longjmp
;
294 tp
->trap_expected
= trap_expected
;
295 tp
->another_trap
= another_trap
;
298 /* Return true if TP is an active thread. */
301 struct thread_info
*tp
;
305 if (! target_thread_alive (tp
->pid
))
307 tp
->pid
= -1; /* Mark it as dead */
316 struct thread_info
*tp
, *tpprev
, *next
;
319 for (tp
= thread_list
; tp
; tp
= next
)
322 if (!thread_alive (tp
))
335 /* Print information about currently known threads */
338 info_threads_command (arg
, from_tty
)
342 struct thread_info
*tp
;
343 int current_pid
= inferior_pid
;
345 /* Avoid coredumps which would happen if we tried to access a NULL
347 if (!target_has_stack
) error ("No stack.");
350 #if defined(FIND_NEW_THREADS)
354 for (tp
= thread_list
; tp
; tp
= tp
->next
)
356 if (tp
->pid
== current_pid
)
357 printf_filtered ("* ");
359 printf_filtered (" ");
361 printf_filtered ("%d %s ", tp
->num
, target_pid_to_str (tp
->pid
));
363 switch_to_thread (tp
->pid
);
365 print_stack_frame (selected_frame
, -1, 0);
367 printf_filtered ("[No stack.]\n");
370 switch_to_thread (current_pid
);
373 /* Switch from one thread to another. */
376 switch_to_thread (pid
)
379 if (pid
== inferior_pid
)
383 flush_cached_frames ();
384 registers_changed ();
386 select_frame (get_current_frame (), 0);
390 restore_current_thread (pid
)
393 if (pid
!= inferior_pid
)
394 switch_to_thread (pid
);
397 /* Apply a GDB command to a list of threads. List syntax is a whitespace
398 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
399 of two numbers seperated by a hyphen. Examples:
401 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
402 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
403 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
407 thread_apply_all_command (cmd
, from_tty
)
411 struct thread_info
*tp
;
412 struct cleanup
*old_chain
;
414 if (cmd
== NULL
|| *cmd
== '\000')
415 error ("Please specify a command following the thread ID list");
417 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
419 for (tp
= thread_list
; tp
; tp
= tp
->next
)
420 if (thread_alive (tp
))
422 switch_to_thread (tp
->pid
);
423 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
424 target_pid_to_str (inferior_pid
));
425 execute_command (cmd
, from_tty
);
430 thread_apply_command (tidlist
, from_tty
)
436 struct cleanup
*old_chain
;
438 if (tidlist
== NULL
|| *tidlist
== '\000')
439 error ("Please specify a thread ID list");
441 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha(*cmd
); cmd
++);
444 error ("Please specify a command following the thread ID list");
446 old_chain
= make_cleanup (restore_current_thread
, inferior_pid
);
448 while (tidlist
< cmd
)
450 struct thread_info
*tp
;
453 start
= strtol (tidlist
, &p
, 10);
455 error ("Error parsing %s", tidlist
);
458 while (*tidlist
== ' ' || *tidlist
== '\t')
461 if (*tidlist
== '-') /* Got a range of IDs? */
463 tidlist
++; /* Skip the - */
464 end
= strtol (tidlist
, &p
, 10);
466 error ("Error parsing %s", tidlist
);
469 while (*tidlist
== ' ' || *tidlist
== '\t')
475 for (; start
<= end
; start
++)
477 tp
= find_thread_id (start
);
480 warning ("Unknown thread %d.", start
);
481 else if (!thread_alive (tp
))
482 warning ("Thread %d has terminated.", start
);
485 switch_to_thread (tp
->pid
);
486 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
487 target_pid_to_str (inferior_pid
));
488 execute_command (cmd
, from_tty
);
494 /* Switch to the specified thread. Will dispatch off to thread_apply_command
495 if prefix of arg is `apply'. */
498 thread_command (tidstr
, from_tty
)
503 struct thread_info
*tp
;
506 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
507 see the IDs of currently known threads.");
511 tp
= find_thread_id (num
);
514 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
515 see the IDs of currently known threads.", num
);
517 if (!thread_alive (tp
))
518 error ("Thread ID %d has terminated.\n", num
);
520 switch_to_thread (tp
->pid
);
522 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid
));
523 print_stack_frame (selected_frame
, selected_frame_level
, 1);
526 /* Commands with a prefix of `thread'. */
527 struct cmd_list_element
*thread_cmd_list
= NULL
;
530 _initialize_thread ()
532 static struct cmd_list_element
*thread_apply_list
= NULL
;
533 extern struct cmd_list_element
*cmdlist
;
535 add_info ("threads", info_threads_command
,
536 "IDs of currently known threads.");
538 add_prefix_cmd ("thread", class_run
, thread_command
,
539 "Use this command to switch between threads.\n\
540 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
543 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
544 "Apply a command to a list of threads.",
545 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
547 add_cmd ("all", class_run
, thread_apply_all_command
,
548 "Apply a command to all threads.",
551 add_com_alias ("t", "thread", class_run
, 1);