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>
42 /*#include "lynxos-core.h" */
44 /* Definition of struct thread_info exported to gdbthread.h */
46 /* Prototypes for exported functions. */
48 void _initialize_thread
PARAMS ((void));
50 /* Prototypes for local functions. */
52 static struct thread_info
*thread_list
= NULL
;
53 static int highest_thread_num
;
55 static struct thread_info
*find_thread_id
PARAMS ((int num
));
57 static void thread_command
PARAMS ((char *tidstr
, int from_tty
));
58 static void thread_apply_all_command
PARAMS ((char *, int));
59 static int thread_alive
PARAMS ((struct thread_info
*));
60 static void info_threads_command
PARAMS ((char *, int));
61 static void thread_apply_command
PARAMS ((char *, int));
62 static void restore_current_thread
PARAMS ((int));
63 static void switch_to_thread
PARAMS ((int pid
));
64 static void prune_threads
PARAMS ((void));
69 struct thread_info
*tp
, *tpnext
;
74 for (tp
= thread_list
; tp
; tp
= tpnext
)
81 highest_thread_num
= 0;
84 /* add_thread now returns a pointer to the new thread_info,
85 so that back_ends can initialize their private data. */
91 struct thread_info
*tp
;
93 tp
= (struct thread_info
*) xmalloc (sizeof (struct thread_info
));
96 tp
->num
= ++highest_thread_num
;
98 tp
->prev_func_start
= 0;
99 tp
->prev_func_name
= NULL
;
100 tp
->step_range_start
= 0;
101 tp
->step_range_end
= 0;
102 tp
->step_frame_address
= 0;
103 tp
->step_resume_breakpoint
= 0;
104 tp
->through_sigtramp_breakpoint
= 0;
105 tp
->handling_longjmp
= 0;
106 tp
->trap_expected
= 0;
107 tp
->another_trap
= 0;
108 tp
->stepping_through_solib_after_catch
= 0;
109 tp
->stepping_through_solib_catchpoints
= NULL
;
110 tp
->stepping_through_sigtramp
= 0;
111 tp
->next
= thread_list
;
121 struct thread_info
*tp
, *tpprev
;
125 for (tp
= thread_list
; tp
; tpprev
= tp
, tp
= tp
->next
)
133 tpprev
->next
= tp
->next
;
135 thread_list
= tp
->next
;
137 /* NOTE: this will take care of any left-over step_resume breakpoints,
138 but not any user-specified thread-specific breakpoints. */
139 if (tp
->step_resume_breakpoint
)
140 delete_breakpoint (tp
->step_resume_breakpoint
);
142 /* FIXME: do I ever need to call the back-end to give it a
143 chance at this private data before deleting the thread? */
152 static struct thread_info
*
156 struct thread_info
*tp
;
158 for (tp
= thread_list
; tp
; tp
= tp
->next
)
165 /* Find a thread_info by matching 'pid'. */
167 find_thread_pid (pid
)
170 struct thread_info
*tp
;
172 for (tp
= thread_list
; tp
; tp
= tp
->next
)
180 * Thread iterator function.
182 * Calls a callback function once for each thread, so long as
183 * the callback function returns false. If the callback function
184 * returns true, the iteration will end and the current thread
185 * will be returned. This can be useful for implementing a
186 * search for a thread with arbitrary attributes, or for applying
187 * some operation to every thread.
189 * FIXME: some of the existing functionality, such as
190 * "Thread apply all", might be rewritten using this functionality.
194 iterate_over_threads (callback
, data
)
198 struct thread_info
*tp
;
200 for (tp
= thread_list
; tp
; tp
= tp
->next
)
201 if ((*callback
) (tp
, data
))
208 valid_thread_id (num
)
211 struct thread_info
*tp
;
213 for (tp
= thread_list
; tp
; tp
= tp
->next
)
221 pid_to_thread_id (pid
)
224 struct thread_info
*tp
;
226 for (tp
= thread_list
; tp
; tp
= tp
->next
)
234 thread_id_to_pid (num
)
237 struct thread_info
*thread
= find_thread_id (num
);
248 struct thread_info
*tp
;
250 for (tp
= thread_list
; tp
; tp
= tp
->next
)
254 return 0; /* Never heard of 'im */
257 /* Print a list of thread ids currently known, and the total number of
258 threads. To be used from within catch_errors. */
260 do_captured_list_thread_ids (void *arg
)
262 struct thread_info
*tp
;
265 ui_out_list_begin (uiout
, "thread-ids");
267 for (tp
= thread_list
; tp
; tp
= tp
->next
)
270 ui_out_field_int (uiout
, "thread-id", tp
->num
);
273 ui_out_list_end (uiout
);
274 ui_out_field_int (uiout
, "number-of-threads", num
);
278 /* Official gdblib interface function to get a list of thread ids and
281 gdb_list_thread_ids (/* output object */)
283 return catch_errors (do_captured_list_thread_ids
, NULL
,
284 NULL
, RETURN_MASK_ALL
);
288 /* Load infrun state for the thread PID. */
291 load_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
292 trap_expected
, step_resume_breakpoint
,
293 through_sigtramp_breakpoint
, step_range_start
,
294 step_range_end
, step_frame_address
,
295 handling_longjmp
, another_trap
,
296 stepping_through_solib_after_catch
,
297 stepping_through_solib_catchpoints
,
298 stepping_through_sigtramp
)
301 CORE_ADDR
*prev_func_start
;
302 char **prev_func_name
;
304 struct breakpoint
**step_resume_breakpoint
;
305 struct breakpoint
**through_sigtramp_breakpoint
;
306 CORE_ADDR
*step_range_start
;
307 CORE_ADDR
*step_range_end
;
308 CORE_ADDR
*step_frame_address
;
309 int *handling_longjmp
;
311 int *stepping_through_solib_after_catch
;
312 bpstat
*stepping_through_solib_catchpoints
;
313 int *stepping_through_sigtramp
;
315 struct thread_info
*tp
;
317 /* If we can't find the thread, then we're debugging a single threaded
318 process. No need to do anything in that case. */
319 tp
= find_thread_id (pid_to_thread_id (pid
));
323 *prev_pc
= tp
->prev_pc
;
324 *prev_func_start
= tp
->prev_func_start
;
325 *prev_func_name
= tp
->prev_func_name
;
326 *step_resume_breakpoint
= tp
->step_resume_breakpoint
;
327 *step_range_start
= tp
->step_range_start
;
328 *step_range_end
= tp
->step_range_end
;
329 *step_frame_address
= tp
->step_frame_address
;
330 *through_sigtramp_breakpoint
= tp
->through_sigtramp_breakpoint
;
331 *handling_longjmp
= tp
->handling_longjmp
;
332 *trap_expected
= tp
->trap_expected
;
333 *another_trap
= tp
->another_trap
;
334 *stepping_through_solib_after_catch
= tp
->stepping_through_solib_after_catch
;
335 *stepping_through_solib_catchpoints
= tp
->stepping_through_solib_catchpoints
;
336 *stepping_through_sigtramp
= tp
->stepping_through_sigtramp
;
339 /* Save infrun state for the thread PID. */
342 save_infrun_state (pid
, prev_pc
, prev_func_start
, prev_func_name
,
343 trap_expected
, step_resume_breakpoint
,
344 through_sigtramp_breakpoint
, step_range_start
,
345 step_range_end
, step_frame_address
,
346 handling_longjmp
, another_trap
,
347 stepping_through_solib_after_catch
,
348 stepping_through_solib_catchpoints
,
349 stepping_through_sigtramp
)
352 CORE_ADDR prev_func_start
;
353 char *prev_func_name
;
355 struct breakpoint
*step_resume_breakpoint
;
356 struct breakpoint
*through_sigtramp_breakpoint
;
357 CORE_ADDR step_range_start
;
358 CORE_ADDR step_range_end
;
359 CORE_ADDR step_frame_address
;
360 int handling_longjmp
;
362 int stepping_through_solib_after_catch
;
363 bpstat stepping_through_solib_catchpoints
;
364 int stepping_through_sigtramp
;
366 struct thread_info
*tp
;
368 /* If we can't find the thread, then we're debugging a single-threaded
369 process. Nothing to do in that case. */
370 tp
= find_thread_id (pid_to_thread_id (pid
));
374 tp
->prev_pc
= prev_pc
;
375 tp
->prev_func_start
= prev_func_start
;
376 tp
->prev_func_name
= prev_func_name
;
377 tp
->step_resume_breakpoint
= step_resume_breakpoint
;
378 tp
->step_range_start
= step_range_start
;
379 tp
->step_range_end
= step_range_end
;
380 tp
->step_frame_address
= step_frame_address
;
381 tp
->through_sigtramp_breakpoint
= through_sigtramp_breakpoint
;
382 tp
->handling_longjmp
= handling_longjmp
;
383 tp
->trap_expected
= trap_expected
;
384 tp
->another_trap
= another_trap
;
385 tp
->stepping_through_solib_after_catch
= stepping_through_solib_after_catch
;
386 tp
->stepping_through_solib_catchpoints
= stepping_through_solib_catchpoints
;
387 tp
->stepping_through_sigtramp
= stepping_through_sigtramp
;
390 /* Return true if TP is an active thread. */
393 struct thread_info
*tp
;
397 if (!target_thread_alive (tp
->pid
))
399 tp
->pid
= -1; /* Mark it as dead */
408 struct thread_info
*tp
, *next
;
410 for (tp
= thread_list
; tp
; tp
= next
)
413 if (!thread_alive (tp
))
414 delete_thread (tp
->pid
);
418 /* Print information about currently known threads
420 * Note: this has the drawback that it _really_ switches
421 * threads, which frees the frame cache. A no-side
422 * effects info-threads command would be nicer.
426 info_threads_command (arg
, from_tty
)
430 struct thread_info
*tp
;
432 struct frame_info
*cur_frame
;
433 int saved_frame_level
= selected_frame_level
;
437 /* Avoid coredumps which would happen if we tried to access a NULL
439 if (!target_has_stack
)
443 target_find_new_threads ();
444 current_pid
= inferior_pid
;
445 for (tp
= thread_list
; tp
; tp
= tp
->next
)
447 if (tp
->pid
== current_pid
)
448 printf_filtered ("* ");
450 printf_filtered (" ");
453 printf_filtered ("%d %s", tp
->num
, target_tid_to_str (tp
->pid
));
455 printf_filtered ("%d %s", tp
->num
, target_pid_to_str (tp
->pid
));
458 extra_info
= target_extra_thread_info (tp
);
460 printf_filtered (" (%s)", extra_info
);
463 switch_to_thread (tp
->pid
);
465 print_only_stack_frame (selected_frame
, -1, 0);
467 printf_filtered ("[No stack.]\n");
470 switch_to_thread (current_pid
);
472 /* Code below copied from "up_silently_base" in "stack.c".
473 * It restores the frame set by the user before the "info threads"
474 * command. We have finished the info-threads display by switching
475 * back to the current thread. That switch has put us at the top
476 * of the stack (leaf frame).
478 counter
= saved_frame_level
;
479 cur_frame
= find_relative_frame (selected_frame
, &counter
);
482 /* Ooops, can't restore, tell user where we are. */
483 warning ("Couldn't restore frame in current thread, at frame 0");
484 print_stack_frame (selected_frame
, -1, 0);
488 select_frame (cur_frame
, saved_frame_level
);
491 /* re-show current frame. */
492 show_stack_frame (cur_frame
);
495 /* Switch from one thread to another. */
498 switch_to_thread (pid
)
501 if (pid
== inferior_pid
)
505 flush_cached_frames ();
506 registers_changed ();
507 stop_pc
= read_pc ();
508 select_frame (get_current_frame (), 0);
512 restore_current_thread (pid
)
515 if (pid
!= inferior_pid
)
517 switch_to_thread (pid
);
518 print_stack_frame (get_current_frame (), 0, -1);
522 /* Apply a GDB command to a list of threads. List syntax is a whitespace
523 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
524 of two numbers seperated by a hyphen. Examples:
526 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
527 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
528 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
532 thread_apply_all_command (cmd
, from_tty
)
536 struct thread_info
*tp
;
537 struct cleanup
*old_chain
;
539 if (cmd
== NULL
|| *cmd
== '\000')
540 error ("Please specify a command following the thread ID list");
542 old_chain
= make_cleanup ((make_cleanup_func
) restore_current_thread
,
543 (void *) inferior_pid
);
545 for (tp
= thread_list
; tp
; tp
= tp
->next
)
546 if (thread_alive (tp
))
548 switch_to_thread (tp
->pid
);
550 printf_filtered ("\nThread %d (%s):\n",
552 target_tid_to_str (inferior_pid
));
554 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
555 target_pid_to_str (inferior_pid
));
557 execute_command (cmd
, from_tty
);
562 thread_apply_command (tidlist
, from_tty
)
568 struct cleanup
*old_chain
;
570 if (tidlist
== NULL
|| *tidlist
== '\000')
571 error ("Please specify a thread ID list");
573 for (cmd
= tidlist
; *cmd
!= '\000' && !isalpha (*cmd
); cmd
++);
576 error ("Please specify a command following the thread ID list");
578 old_chain
= make_cleanup ((make_cleanup_func
) restore_current_thread
,
579 (void *) inferior_pid
);
581 while (tidlist
< cmd
)
583 struct thread_info
*tp
;
586 start
= strtol (tidlist
, &p
, 10);
588 error ("Error parsing %s", tidlist
);
591 while (*tidlist
== ' ' || *tidlist
== '\t')
594 if (*tidlist
== '-') /* Got a range of IDs? */
596 tidlist
++; /* Skip the - */
597 end
= strtol (tidlist
, &p
, 10);
599 error ("Error parsing %s", tidlist
);
602 while (*tidlist
== ' ' || *tidlist
== '\t')
608 for (; start
<= end
; start
++)
610 tp
= find_thread_id (start
);
613 warning ("Unknown thread %d.", start
);
614 else if (!thread_alive (tp
))
615 warning ("Thread %d has terminated.", start
);
618 switch_to_thread (tp
->pid
);
620 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
621 target_tid_to_str (inferior_pid
));
623 printf_filtered ("\nThread %d (%s):\n", tp
->num
,
624 target_pid_to_str (inferior_pid
));
626 execute_command (cmd
, from_tty
);
632 /* Switch to the specified thread. Will dispatch off to thread_apply_command
633 if prefix of arg is `apply'. */
636 thread_command (tidstr
, from_tty
)
642 /* Don't generate an error, just say which thread is current. */
643 if (target_has_stack
)
644 printf_filtered ("[Current thread is %d (%s)]\n",
645 pid_to_thread_id (inferior_pid
),
646 #if defined(HPUXHPPA)
647 target_tid_to_str (inferior_pid
)
649 target_pid_to_str (inferior_pid
)
657 gdb_thread_select (tidstr
);
661 do_captured_thread_select (void *tidstr
)
664 struct thread_info
*tp
;
666 num
= atoi ((char *)tidstr
);
668 tp
= find_thread_id (num
);
672 error ("Thread ID %d not known.", num
);
675 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
676 see the IDs of currently known threads.", num
);
679 if (!thread_alive (tp
))
680 error ("Thread ID %d has terminated.\n", num
);
682 switch_to_thread (tp
->pid
);
685 ui_out_text (uiout
, "[Switching to thread ");
686 ui_out_field_int (uiout
, "new-thread-id", pid_to_thread_id (inferior_pid
));
687 ui_out_text (uiout
, " (");
688 #if defined(HPUXHPPA)
689 ui_out_text (uiout
, target_tid_to_str (inferior_pid
));
691 ui_out_text (uiout
, target_pid_to_str (inferior_pid
));
693 ui_out_text (uiout
, ")]");
695 printf_filtered ("[Switching to thread %d (%s)]\n",
696 pid_to_thread_id (inferior_pid
),
697 #if defined(HPUXHPPA)
698 target_tid_to_str (inferior_pid
)
700 target_pid_to_str (inferior_pid
)
705 print_stack_frame (selected_frame
, selected_frame_level
, 1);
710 gdb_thread_select (char *tidstr
)
712 return catch_errors (do_captured_thread_select
, tidstr
,
713 NULL
, RETURN_MASK_ALL
);
716 /* Commands with a prefix of `thread'. */
717 struct cmd_list_element
*thread_cmd_list
= NULL
;
720 _initialize_thread ()
722 static struct cmd_list_element
*thread_apply_list
= NULL
;
724 add_info ("threads", info_threads_command
,
725 "IDs of currently known threads.");
727 add_prefix_cmd ("thread", class_run
, thread_command
,
728 "Use this command to switch between threads.\n\
729 The new thread ID must be currently known.", &thread_cmd_list
, "thread ", 1,
732 add_prefix_cmd ("apply", class_run
, thread_apply_command
,
733 "Apply a command to a list of threads.",
734 &thread_apply_list
, "apply ", 1, &thread_cmd_list
);
736 add_cmd ("all", class_run
, thread_apply_all_command
,
737 "Apply a command to all threads.",
741 add_com_alias ("t", "thread", class_run
, 1);