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
;
118 struct thread_info
*tp
, *tpprev
;
122 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
130 tpprev
->next
= tp
->next
;
132 thread_list
= tp
->next
;
134 /* NOTE: this will take care of any left-over step_resume breakpoints,
135 but not any user-specified thread-specific breakpoints. */
136 if (tp
->step_resume_breakpoint
)
137 delete_breakpoint (tp
->step_resume_breakpoint
);
139 /* FIXME: do I ever need to call the back-end to give it a
140 chance at this private data before deleting the thread? */
149 static struct thread_info
*
153 struct thread_info
*tp
;
155 for (tp
= thread_list
; tp
; tp
= tp
->next
)
162 /* Find a thread_info by matching 'pid'. */
164 find_thread_pid (pid
)
167 struct thread_info
*tp
;
169 for (tp
= thread_list
; tp
; tp
= tp
->next
)
177 * Thread iterator function.
179 * Calls a callback function once for each thread, so long as
180 * the callback function returns false. If the callback function
181 * returns true, the iteration will end and the current thread
182 * will be returned. This can be useful for implementing a
183 * search for a thread with arbitrary attributes, or for applying
184 * some operation to every thread.
186 * FIXME: some of the existing functionality, such as
187 * "Thread apply all", might be rewritten using this functionality.
191 iterate_over_threads (callback
, data
)
195 struct thread_info
*tp
;
197 for (tp
= thread_list
; tp
; tp
= tp
->next
)
198 if ((*callback
) (tp
, data
))
205 valid_thread_id (num
)
208 struct thread_info
*tp
;
210 for (tp
= thread_list
; tp
; tp
= tp
->next
)
218 pid_to_thread_id (pid
)
221 struct thread_info
*tp
;
223 for (tp
= thread_list
; tp
; tp
= tp
->next
)
231 thread_id_to_pid (num
)
234 struct thread_info
*thread
= find_thread_id (num
);
245 struct thread_info
*tp
;
247 for (tp
= thread_list
; tp
; tp
= tp
->next
)
251 return 0; /* Never heard of 'im */
254 /* Load infrun state for the thread PID. */
257 load_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
258 trap_expected
, step_resume_breakpoint
,
259 through_sigtramp_breakpoint
, step_range_start
,
260 step_range_end
, step_frame_address
,
261 handling_longjmp
, another_trap
,
262 stepping_through_solib_after_catch
,
263 stepping_through_solib_catchpoints
,
264 stepping_through_sigtramp
)
267 CORE_ADDR
*prev_func_start
;
268 char **prev_func_name
;
270 struct breakpoint
**step_resume_breakpoint
;
271 struct breakpoint
**through_sigtramp_breakpoint
;
272 CORE_ADDR
*step_range_start
;
273 CORE_ADDR
*step_range_end
;
274 CORE_ADDR
*step_frame_address
;
275 int *handling_longjmp
;
277 int *stepping_through_solib_after_catch
;
278 bpstat
*stepping_through_solib_catchpoints
;
279 int *stepping_through_sigtramp
;
281 struct thread_info
*tp
;
283 /* If we can't find the thread, then we're debugging a single threaded
284 process. No need to do anything in that case. */
285 tp
= find_thread_id (pid_to_thread_id (pid
));
289 *prev_pc
= tp
->prev_pc
;
290 *prev_func_start
= tp
->prev_func_start
;
291 *prev_func_name
= tp
->prev_func_name
;
292 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
293 *step_range_start
= tp
->step_range_start
;
294 *step_range_end
= tp
->step_range_end
;
295 *step_frame_address
= tp
->step_frame_address
;
296 *through_sigtramp_breakpoint
= tp
->through_sigtramp_breakpoint
;
297 *handling_longjmp
= tp
->handling_longjmp
;
298 *trap_expected
= tp
->trap_expected
;
299 *another_trap
= tp
->another_trap
;
300 *stepping_through_solib_after_catch
= tp
->stepping_through_solib_after_catch
;
301 *stepping_through_solib_catchpoints
= tp
->stepping_through_solib_catchpoints
;
302 *stepping_through_sigtramp
= tp
->stepping_through_sigtramp
;
305 /* Save infrun state for the thread PID. */
308 save_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
309 trap_expected
, step_resume_breakpoint
,
310 through_sigtramp_breakpoint
, step_range_start
,
311 step_range_end
, step_frame_address
,
312 handling_longjmp
, another_trap
,
313 stepping_through_solib_after_catch
,
314 stepping_through_solib_catchpoints
,
315 stepping_through_sigtramp
)
318 CORE_ADDR prev_func_start
;
319 char *prev_func_name
;
321 struct breakpoint
*step_resume_breakpoint
;
322 struct breakpoint
*through_sigtramp_breakpoint
;
323 CORE_ADDR step_range_start
;
324 CORE_ADDR step_range_end
;
325 CORE_ADDR step_frame_address
;
326 int handling_longjmp
;
328 int stepping_through_solib_after_catch
;
329 bpstat stepping_through_solib_catchpoints
;
330 int stepping_through_sigtramp
;
332 struct thread_info
*tp
;
334 /* If we can't find the thread, then we're debugging a single-threaded
335 process. Nothing to do in that case. */
336 tp
= find_thread_id (pid_to_thread_id (pid
));
340 tp
->prev_pc
= prev_pc
;
341 tp
->prev_func_start
= prev_func_start
;
342 tp
->prev_func_name
= prev_func_name
;
343 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
344 tp
->step_range_start
= step_range_start
;
345 tp
->step_range_end
= step_range_end
;
346 tp
->step_frame_address
= step_frame_address
;
347 tp
->through_sigtramp_breakpoint
= through_sigtramp_breakpoint
;
348 tp
->handling_longjmp
= handling_longjmp
;
349 tp
->trap_expected
= trap_expected
;
350 tp
->another_trap
= another_trap
;
351 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
352 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
353 tp
->stepping_through_sigtramp
= stepping_through_sigtramp
;
356 /* Return true if TP is an active thread. */
359 struct thread_info
*tp
;
363 if (!target_thread_alive (tp
->pid
))
365 tp
->pid
= -1; /* Mark it as dead */
374 struct thread_info
*tp
, *next
;
376 for (tp
= thread_list
; tp
; tp
= next
)
379 if (!thread_alive (tp
))
380 delete_thread (tp
->pid
);
384 /* Print information about currently known threads
386 * Note: this has the drawback that it _really_ switches
387 * threads, which frees the frame cache. A no-side
388 * effects info-threads command would be nicer.
392 info_threads_command (arg
, from_tty
)
396 struct thread_info
*tp
;
398 struct frame_info
*cur_frame
;
399 int saved_frame_level
= selected_frame_level
;
403 /* Avoid coredumps which would happen if we tried to access a NULL
405 if (!target_has_stack
)
409 target_find_new_threads ();
410 current_pid
= inferior_pid
;
411 for (tp
= thread_list
; tp
; tp
= tp
->next
)
413 if (tp
->pid
== current_pid
)
414 printf_filtered ("* ");
416 printf_filtered (" ");
419 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->pid
));
421 printf_filtered ("%d %s", tp
->num
, target_pid_to_str (tp
->pid
));
424 extra_info
= target_extra_thread_info (tp
);
426 printf_filtered (" (%s)", extra_info
);
429 switch_to_thread (tp
->pid
);
431 print_only_stack_frame (selected_frame
, -1, 0);
433 printf_filtered ("[No stack.]\n");
436 switch_to_thread (current_pid
);
438 /* Code below copied from "up_silently_base" in "stack.c".
439 * It restores the frame set by the user before the "info threads"
440 * command. We have finished the info-threads display by switching
441 * back to the current thread. That switch has put us at the top
442 * of the stack (leaf frame).
444 counter
= saved_frame_level
;
445 cur_frame
= find_relative_frame (selected_frame
, &counter
);
448 /* Ooops, can't restore, tell user where we are. */
449 warning ("Couldn't restore frame in current thread, at frame 0");
450 print_stack_frame (selected_frame
, -1, 0);
454 select_frame (cur_frame
, saved_frame_level
);
457 /* re-show current frame. */
458 show_stack_frame (cur_frame
);
461 /* Switch from one thread to another. */
464 switch_to_thread (pid
)
467 if (pid
== inferior_pid
)
471 flush_cached_frames ();
472 registers_changed ();
473 stop_pc
= read_pc ();
474 select_frame (get_current_frame (), 0);
478 restore_current_thread (pid
)
481 if (pid
!= inferior_pid
)
483 switch_to_thread (pid
);
484 print_stack_frame (get_current_frame (), 0, -1);
488 /* Apply a GDB command to a list of threads. List syntax is a whitespace
489 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
490 of two numbers seperated by a hyphen. Examples:
492 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
493 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
494 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
498 thread_apply_all_command (cmd
, from_tty
)
502 struct thread_info
*tp
;
503 struct cleanup
*old_chain
;
505 if (cmd
== NULL
|| *cmd
== '\000')
506 error ("Please specify a command following the thread ID list");
508 old_chain
= make_cleanup ((make_cleanup_func
) restore_current_thread
,
509 (void *) inferior_pid
);
511 for (tp
= thread_list
; tp
; tp
= tp
->next
)
512 if (thread_alive (tp
))
514 switch_to_thread (tp
->pid
);
516 printf_filtered ("\nThread %d (%s):\n",
518 target_tid_to_str (inferior_pid
));
520 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
521 target_pid_to_str (inferior_pid
));
523 execute_command (cmd
, from_tty
);
528 thread_apply_command (tidlist
, from_tty
)
534 struct cleanup
*old_chain
;
536 if (tidlist
== NULL
|| *tidlist
== '\000')
537 error ("Please specify a thread ID list");
539 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
542 error ("Please specify a command following the thread ID list");
544 old_chain
= make_cleanup ((make_cleanup_func
) restore_current_thread
,
545 (void *) inferior_pid
);
547 while (tidlist
< cmd
)
549 struct thread_info
*tp
;
552 start
= strtol (tidlist
, &p
, 10);
554 error ("Error parsing %s", tidlist
);
557 while (*tidlist
== ' ' || *tidlist
== '\t')
560 if (*tidlist
== '-') /* Got a range of IDs? */
562 tidlist
++; /* Skip the - */
563 end
= strtol (tidlist
, &p
, 10);
565 error ("Error parsing %s", tidlist
);
568 while (*tidlist
== ' ' || *tidlist
== '\t')
574 for (; start
<= end
; start
++)
576 tp
= find_thread_id (start
);
579 warning ("Unknown thread %d.", start
);
580 else if (!thread_alive (tp
))
581 warning ("Thread %d has terminated.", start
);
584 switch_to_thread (tp
->pid
);
586 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
587 target_tid_to_str (inferior_pid
));
589 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
590 target_pid_to_str (inferior_pid
));
592 execute_command (cmd
, from_tty
);
598 /* Switch to the specified thread. Will dispatch off to thread_apply_command
599 if prefix of arg is `apply'. */
602 thread_command (tidstr
, from_tty
)
608 /* Don't generate an error, just say which thread is current. */
609 if (target_has_stack
)
610 printf_filtered ("[Current thread is %d (%s)]\n",
611 pid_to_thread_id (inferior_pid
),
612 #if defined(HPUXHPPA)
613 target_tid_to_str (inferior_pid
)
615 target_pid_to_str (inferior_pid
)
623 gdb_thread_select (tidstr
);
627 do_captured_thread_select (void *tidstr
)
630 struct thread_info
*tp
;
632 num
= atoi ((char *)tidstr
);
634 tp
= find_thread_id (num
);
637 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
638 see the IDs of currently known threads.", num
);
640 if (!thread_alive (tp
))
641 error ("Thread ID %d has terminated.\n", num
);
643 switch_to_thread (tp
->pid
);
645 printf_filtered ("[Switching to thread %d (%s)]\n",
646 pid_to_thread_id (inferior_pid
),
647 #if defined(HPUXHPPA)
648 target_tid_to_str (inferior_pid
)
650 target_pid_to_str (inferior_pid
)
654 print_stack_frame (selected_frame
, selected_frame_level
, 1);
659 gdb_thread_select (char *tidstr
)
661 return catch_errors (do_captured_thread_select
, tidstr
,
662 NULL
, RETURN_MASK_ALL
);
665 /* Commands with a prefix of `thread'. */
666 struct cmd_list_element
*thread_cmd_list
= NULL
;
669 _initialize_thread ()
671 static struct cmd_list_element
*thread_apply_list
= NULL
;
673 add_info ("threads", info_threads_command
,
674 "IDs of currently known threads.");
676 add_prefix_cmd ("thread", class_run
, thread_command
,
677 "Use this command to switch between threads.\n\
678 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
681 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
682 "Apply a command to a list of threads.",
683 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
685 add_cmd ("all", class_run
, thread_apply_all_command
,
686 "Apply a command to all threads.",
690 add_com_alias ("t", "thread", class_run
, 1);