1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993, 1998, 1999, 2000
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,
22 Boston, MA 02111-1307, USA. */
31 #include "gdbthread.h"
36 #include <sys/types.h>
39 /*#include "lynxos-core.h" */
41 /* Definition of struct thread_info exported to gdbthread.h */
43 /* Prototypes for exported functions. */
45 void _initialize_thread
PARAMS ((void));
47 /* Prototypes for local functions. */
49 static struct thread_info
*thread_list
= NULL
;
50 static int highest_thread_num
;
52 static struct thread_info
*find_thread_id
PARAMS ((int num
));
54 static void thread_command
PARAMS ((char *tidstr
, int from_tty
));
55 static void thread_apply_all_command
PARAMS ((char *, int));
56 static int thread_alive
PARAMS ((struct thread_info
*));
57 static void info_threads_command
PARAMS ((char *, int));
58 static void thread_apply_command
PARAMS ((char *, int));
59 static void restore_current_thread
PARAMS ((int));
60 static void switch_to_thread
PARAMS ((int pid
));
61 static void prune_threads
PARAMS ((void));
66 struct thread_info
*tp
, *tpnext
;
71 for (tp
= thread_list
; tp
; tp
= tpnext
)
78 highest_thread_num
= 0;
81 /* add_thread now returns a pointer to the new thread_info,
82 so that back_ends can initialize their private data. */
88 struct thread_info
*tp
;
90 tp
= (struct thread_info
*) xmalloc (sizeof (struct thread_info
));
93 tp
->num
= ++highest_thread_num
;
95 tp
->prev_func_start
= 0;
96 tp
->prev_func_name
= NULL
;
97 tp
->step_range_start
= 0;
98 tp
->step_range_end
= 0;
99 tp
->step_frame_address
= 0;
100 tp
->step_resume_breakpoint
= 0;
101 tp
->through_sigtramp_breakpoint
= 0;
102 tp
->handling_longjmp
= 0;
103 tp
->trap_expected
= 0;
104 tp
->another_trap
= 0;
105 tp
->stepping_through_solib_after_catch
= 0;
106 tp
->stepping_through_solib_catchpoints
= NULL
;
107 tp
->stepping_through_sigtramp
= 0;
108 tp
->next
= thread_list
;
117 struct thread_info
*tp
, *tpprev
;
121 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
129 tpprev
->next
= tp
->next
;
131 thread_list
= tp
->next
;
133 /* NOTE: this will take care of any left-over step_resume breakpoints,
134 but not any user-specified thread-specific breakpoints. */
135 if (tp
->step_resume_breakpoint
)
136 delete_breakpoint (tp
->step_resume_breakpoint
);
138 /* FIXME: do I ever need to call the back-end to give it a
139 chance at this private data before deleting the thread? */
148 static struct thread_info
*
152 struct thread_info
*tp
;
154 for (tp
= thread_list
; tp
; tp
= tp
->next
)
161 /* Find a thread_info by matching 'pid'. */
163 find_thread_pid (pid
)
166 struct thread_info
*tp
;
168 for (tp
= thread_list
; tp
; tp
= tp
->next
)
176 * Thread iterator function.
178 * Calls a callback function once for each thread, so long as
179 * the callback function returns false. If the callback function
180 * returns true, the iteration will end and the current thread
181 * will be returned. This can be useful for implementing a
182 * search for a thread with arbitrary attributes, or for applying
183 * some operation to every thread.
185 * FIXME: some of the existing functionality, such as
186 * "Thread apply all", might be rewritten using this functionality.
190 iterate_over_threads (callback
, data
)
194 struct thread_info
*tp
;
196 for (tp
= thread_list
; tp
; tp
= tp
->next
)
197 if ((*callback
) (tp
, data
))
204 valid_thread_id (num
)
207 struct thread_info
*tp
;
209 for (tp
= thread_list
; tp
; tp
= tp
->next
)
217 pid_to_thread_id (pid
)
220 struct thread_info
*tp
;
222 for (tp
= thread_list
; tp
; tp
= tp
->next
)
230 thread_id_to_pid (num
)
233 struct thread_info
*thread
= find_thread_id (num
);
244 struct thread_info
*tp
;
246 for (tp
= thread_list
; tp
; tp
= tp
->next
)
250 return 0; /* Never heard of 'im */
253 /* Load infrun state for the thread PID. */
256 load_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
257 trap_expected
, step_resume_breakpoint
,
258 through_sigtramp_breakpoint
, step_range_start
,
259 step_range_end
, step_frame_address
,
260 handling_longjmp
, another_trap
,
261 stepping_through_solib_after_catch
,
262 stepping_through_solib_catchpoints
,
263 stepping_through_sigtramp
)
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
;
276 int *stepping_through_solib_after_catch
;
277 bpstat
*stepping_through_solib_catchpoints
;
278 int *stepping_through_sigtramp
;
280 struct thread_info
*tp
;
282 /* If we can't find the thread, then we're debugging a single threaded
283 process. No need to do anything in that case. */
284 tp
= find_thread_id (pid_to_thread_id (pid
));
288 *prev_pc
= tp
->prev_pc
;
289 *prev_func_start
= tp
->prev_func_start
;
290 *prev_func_name
= tp
->prev_func_name
;
291 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
292 *step_range_start
= tp
->step_range_start
;
293 *step_range_end
= tp
->step_range_end
;
294 *step_frame_address
= tp
->step_frame_address
;
295 *through_sigtramp_breakpoint
= tp
->through_sigtramp_breakpoint
;
296 *handling_longjmp
= tp
->handling_longjmp
;
297 *trap_expected
= tp
->trap_expected
;
298 *another_trap
= tp
->another_trap
;
299 *stepping_through_solib_after_catch
= tp
->stepping_through_solib_after_catch
;
300 *stepping_through_solib_catchpoints
= tp
->stepping_through_solib_catchpoints
;
301 *stepping_through_sigtramp
= tp
->stepping_through_sigtramp
;
304 /* Save infrun state for the thread PID. */
307 save_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
308 trap_expected
, step_resume_breakpoint
,
309 through_sigtramp_breakpoint
, step_range_start
,
310 step_range_end
, step_frame_address
,
311 handling_longjmp
, another_trap
,
312 stepping_through_solib_after_catch
,
313 stepping_through_solib_catchpoints
,
314 stepping_through_sigtramp
)
317 CORE_ADDR prev_func_start
;
318 char *prev_func_name
;
320 struct breakpoint
*step_resume_breakpoint
;
321 struct breakpoint
*through_sigtramp_breakpoint
;
322 CORE_ADDR step_range_start
;
323 CORE_ADDR step_range_end
;
324 CORE_ADDR step_frame_address
;
325 int handling_longjmp
;
327 int stepping_through_solib_after_catch
;
328 bpstat stepping_through_solib_catchpoints
;
329 int stepping_through_sigtramp
;
331 struct thread_info
*tp
;
333 /* If we can't find the thread, then we're debugging a single-threaded
334 process. Nothing to do in that case. */
335 tp
= find_thread_id (pid_to_thread_id (pid
));
339 tp
->prev_pc
= prev_pc
;
340 tp
->prev_func_start
= prev_func_start
;
341 tp
->prev_func_name
= prev_func_name
;
342 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
343 tp
->step_range_start
= step_range_start
;
344 tp
->step_range_end
= step_range_end
;
345 tp
->step_frame_address
= step_frame_address
;
346 tp
->through_sigtramp_breakpoint
= through_sigtramp_breakpoint
;
347 tp
->handling_longjmp
= handling_longjmp
;
348 tp
->trap_expected
= trap_expected
;
349 tp
->another_trap
= another_trap
;
350 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
351 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
352 tp
->stepping_through_sigtramp
= stepping_through_sigtramp
;
355 /* Return true if TP is an active thread. */
358 struct thread_info
*tp
;
362 if (!target_thread_alive (tp
->pid
))
364 tp
->pid
= -1; /* Mark it as dead */
373 struct thread_info
*tp
, *next
;
375 for (tp
= thread_list
; tp
; tp
= next
)
378 if (!thread_alive (tp
))
379 delete_thread (tp
->pid
);
383 /* Print information about currently known threads
385 * Note: this has the drawback that it _really_ switches
386 * threads, which frees the frame cache. A no-side
387 * effects info-threads command would be nicer.
391 info_threads_command (arg
, from_tty
)
395 struct thread_info
*tp
;
397 struct frame_info
*cur_frame
;
398 int saved_frame_level
= selected_frame_level
;
402 /* Avoid coredumps which would happen if we tried to access a NULL
404 if (!target_has_stack
)
408 target_find_new_threads ();
409 current_pid
= inferior_pid
;
410 for (tp
= thread_list
; tp
; tp
= tp
->next
)
412 if (tp
->pid
== current_pid
)
413 printf_filtered ("* ");
415 printf_filtered (" ");
418 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->pid
));
420 printf_filtered ("%d %s", tp
->num
, target_pid_to_str (tp
->pid
));
423 extra_info
= target_extra_thread_info (tp
);
425 printf_filtered (" (%s)", extra_info
);
428 switch_to_thread (tp
->pid
);
430 print_only_stack_frame (selected_frame
, -1, 0);
432 printf_filtered ("[No stack.]\n");
435 switch_to_thread (current_pid
);
437 /* Code below copied from "up_silently_base" in "stack.c".
438 * It restores the frame set by the user before the "info threads"
439 * command. We have finished the info-threads display by switching
440 * back to the current thread. That switch has put us at the top
441 * of the stack (leaf frame).
443 counter
= saved_frame_level
;
444 cur_frame
= find_relative_frame (selected_frame
, &counter
);
447 /* Ooops, can't restore, tell user where we are. */
448 warning ("Couldn't restore frame in current thread, at frame 0");
449 print_stack_frame (selected_frame
, -1, 0);
453 select_frame (cur_frame
, saved_frame_level
);
456 /* re-show current frame. */
457 show_stack_frame (cur_frame
);
460 /* Switch from one thread to another. */
463 switch_to_thread (pid
)
466 if (pid
== inferior_pid
)
470 flush_cached_frames ();
471 registers_changed ();
472 stop_pc
= read_pc ();
473 select_frame (get_current_frame (), 0);
477 restore_current_thread (pid
)
480 if (pid
!= inferior_pid
)
482 switch_to_thread (pid
);
483 print_stack_frame (get_current_frame (), 0, -1);
487 /* Apply a GDB command to a list of threads. List syntax is a whitespace
488 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
489 of two numbers seperated by a hyphen. Examples:
491 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
492 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
493 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
497 thread_apply_all_command (cmd
, from_tty
)
501 struct thread_info
*tp
;
502 struct cleanup
*old_chain
;
504 if (cmd
== NULL
|| *cmd
== '\000')
505 error ("Please specify a command following the thread ID list");
507 old_chain
= make_cleanup ((make_cleanup_func
) restore_current_thread
,
508 (void *) inferior_pid
);
510 for (tp
= thread_list
; tp
; tp
= tp
->next
)
511 if (thread_alive (tp
))
513 switch_to_thread (tp
->pid
);
515 printf_filtered ("\nThread %d (%s):\n",
517 target_tid_to_str (inferior_pid
));
519 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
520 target_pid_to_str (inferior_pid
));
522 execute_command (cmd
, from_tty
);
527 thread_apply_command (tidlist
, from_tty
)
533 struct cleanup
*old_chain
;
535 if (tidlist
== NULL
|| *tidlist
== '\000')
536 error ("Please specify a thread ID list");
538 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
541 error ("Please specify a command following the thread ID list");
543 old_chain
= make_cleanup ((make_cleanup_func
) restore_current_thread
,
544 (void *) inferior_pid
);
546 while (tidlist
< cmd
)
548 struct thread_info
*tp
;
551 start
= strtol (tidlist
, &p
, 10);
553 error ("Error parsing %s", tidlist
);
556 while (*tidlist
== ' ' || *tidlist
== '\t')
559 if (*tidlist
== '-') /* Got a range of IDs? */
561 tidlist
++; /* Skip the - */
562 end
= strtol (tidlist
, &p
, 10);
564 error ("Error parsing %s", tidlist
);
567 while (*tidlist
== ' ' || *tidlist
== '\t')
573 for (; start
<= end
; start
++)
575 tp
= find_thread_id (start
);
578 warning ("Unknown thread %d.", start
);
579 else if (!thread_alive (tp
))
580 warning ("Thread %d has terminated.", start
);
583 switch_to_thread (tp
->pid
);
585 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
586 target_tid_to_str (inferior_pid
));
588 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
589 target_pid_to_str (inferior_pid
));
591 execute_command (cmd
, from_tty
);
597 /* Switch to the specified thread. Will dispatch off to thread_apply_command
598 if prefix of arg is `apply'. */
601 thread_command (tidstr
, from_tty
)
606 struct thread_info
*tp
;
610 /* Don't generate an error, just say which thread is current. */
611 if (target_has_stack
)
612 printf_filtered ("[Current thread is %d (%s)]\n",
613 pid_to_thread_id (inferior_pid
),
614 #if defined(HPUXHPPA)
615 target_tid_to_str (inferior_pid
)
617 target_pid_to_str (inferior_pid
)
626 tp
= find_thread_id (num
);
629 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
630 see the IDs of currently known threads.", num
);
632 if (!thread_alive (tp
))
633 error ("Thread ID %d has terminated.\n", num
);
635 switch_to_thread (tp
->pid
);
640 printf_filtered ("[Switching to thread %d (%s)]\n",
641 pid_to_thread_id (inferior_pid
),
642 #if defined(HPUXHPPA)
643 target_tid_to_str (inferior_pid
)
645 target_pid_to_str (inferior_pid
)
648 print_stack_frame (selected_frame
, selected_frame_level
, 1);
651 /* Commands with a prefix of `thread'. */
652 struct cmd_list_element
*thread_cmd_list
= NULL
;
655 _initialize_thread ()
657 static struct cmd_list_element
*thread_apply_list
= NULL
;
659 add_info ("threads", info_threads_command
,
660 "IDs of currently known threads.");
662 add_prefix_cmd ("thread", class_run
, thread_command
,
663 "Use this command to switch between threads.\n\
664 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
667 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
668 "Apply a command to a list of threads.",
669 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
671 add_cmd ("all", class_run
, thread_apply_all_command
,
672 "Apply a command to all threads.",
676 add_com_alias ("t", "thread", class_run
, 1);