2004-05-18 Randolph Chung <tausq@debian.org>
[deliverable/binutils-gdb.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2
3 Copyright 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5
6 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA. */
24
25 #include "defs.h"
26 #include "symtab.h"
27 #include "frame.h"
28 #include "inferior.h"
29 #include "environ.h"
30 #include "value.h"
31 #include "target.h"
32 #include "gdbthread.h"
33 #include "command.h"
34 #include "gdbcmd.h"
35 #include "regcache.h"
36 #include "gdb.h"
37 #include "gdb_string.h"
38
39 #include <ctype.h>
40 #include <sys/types.h>
41 #include <signal.h>
42 #include "ui-out.h"
43
44 /*#include "lynxos-core.h" */
45
46 /* Definition of struct thread_info exported to gdbthread.h */
47
48 /* Prototypes for exported functions. */
49
50 void _initialize_thread (void);
51
52 /* Prototypes for local functions. */
53
54 static struct thread_info *thread_list = NULL;
55 static int highest_thread_num;
56
57 static struct thread_info *find_thread_id (int num);
58
59 static void thread_command (char *tidstr, int from_tty);
60 static void thread_apply_all_command (char *, int);
61 static int thread_alive (struct thread_info *);
62 static void info_threads_command (char *, int);
63 static void thread_apply_command (char *, int);
64 static void restore_current_thread (ptid_t);
65 static void switch_to_thread (ptid_t ptid);
66 static void prune_threads (void);
67
68 void
69 delete_step_resume_breakpoint (void *arg)
70 {
71 struct breakpoint **breakpointp = (struct breakpoint **) arg;
72 struct thread_info *tp;
73
74 if (*breakpointp != NULL)
75 {
76 delete_breakpoint (*breakpointp);
77 for (tp = thread_list; tp; tp = tp->next)
78 if (tp->step_resume_breakpoint == *breakpointp)
79 tp->step_resume_breakpoint = NULL;
80
81 *breakpointp = NULL;
82 }
83 }
84
85 static void
86 free_thread (struct thread_info *tp)
87 {
88 /* NOTE: this will take care of any left-over step_resume breakpoints,
89 but not any user-specified thread-specific breakpoints. */
90 if (tp->step_resume_breakpoint)
91 delete_breakpoint (tp->step_resume_breakpoint);
92
93 /* FIXME: do I ever need to call the back-end to give it a
94 chance at this private data before deleting the thread? */
95 if (tp->private)
96 xfree (tp->private);
97
98 xfree (tp);
99 }
100
101 void
102 init_thread_list (void)
103 {
104 struct thread_info *tp, *tpnext;
105
106 highest_thread_num = 0;
107 if (!thread_list)
108 return;
109
110 for (tp = thread_list; tp; tp = tpnext)
111 {
112 tpnext = tp->next;
113 free_thread (tp);
114 }
115
116 thread_list = NULL;
117 }
118
119 /* add_thread now returns a pointer to the new thread_info,
120 so that back_ends can initialize their private data. */
121
122 struct thread_info *
123 add_thread (ptid_t ptid)
124 {
125 struct thread_info *tp;
126
127 tp = (struct thread_info *) xmalloc (sizeof (*tp));
128 memset (tp, 0, sizeof (*tp));
129 tp->ptid = ptid;
130 tp->num = ++highest_thread_num;
131 tp->next = thread_list;
132 thread_list = tp;
133 return tp;
134 }
135
136 void
137 delete_thread (ptid_t ptid)
138 {
139 struct thread_info *tp, *tpprev;
140
141 tpprev = NULL;
142
143 for (tp = thread_list; tp; tpprev = tp, tp = tp->next)
144 if (ptid_equal (tp->ptid, ptid))
145 break;
146
147 if (!tp)
148 return;
149
150 if (tpprev)
151 tpprev->next = tp->next;
152 else
153 thread_list = tp->next;
154
155 free_thread (tp);
156 }
157
158 static struct thread_info *
159 find_thread_id (int num)
160 {
161 struct thread_info *tp;
162
163 for (tp = thread_list; tp; tp = tp->next)
164 if (tp->num == num)
165 return tp;
166
167 return NULL;
168 }
169
170 /* Find a thread_info by matching PTID. */
171 struct thread_info *
172 find_thread_pid (ptid_t ptid)
173 {
174 struct thread_info *tp;
175
176 for (tp = thread_list; tp; tp = tp->next)
177 if (ptid_equal (tp->ptid, ptid))
178 return tp;
179
180 return NULL;
181 }
182
183 /*
184 * Thread iterator function.
185 *
186 * Calls a callback function once for each thread, so long as
187 * the callback function returns false. If the callback function
188 * returns true, the iteration will end and the current thread
189 * will be returned. This can be useful for implementing a
190 * search for a thread with arbitrary attributes, or for applying
191 * some operation to every thread.
192 *
193 * FIXME: some of the existing functionality, such as
194 * "Thread apply all", might be rewritten using this functionality.
195 */
196
197 struct thread_info *
198 iterate_over_threads (int (*callback) (struct thread_info *, void *),
199 void *data)
200 {
201 struct thread_info *tp;
202
203 for (tp = thread_list; tp; tp = tp->next)
204 if ((*callback) (tp, data))
205 return tp;
206
207 return NULL;
208 }
209
210 int
211 valid_thread_id (int num)
212 {
213 struct thread_info *tp;
214
215 for (tp = thread_list; tp; tp = tp->next)
216 if (tp->num == num)
217 return 1;
218
219 return 0;
220 }
221
222 int
223 pid_to_thread_id (ptid_t ptid)
224 {
225 struct thread_info *tp;
226
227 for (tp = thread_list; tp; tp = tp->next)
228 if (ptid_equal (tp->ptid, ptid))
229 return tp->num;
230
231 return 0;
232 }
233
234 ptid_t
235 thread_id_to_pid (int num)
236 {
237 struct thread_info *thread = find_thread_id (num);
238 if (thread)
239 return thread->ptid;
240 else
241 return pid_to_ptid (-1);
242 }
243
244 int
245 in_thread_list (ptid_t ptid)
246 {
247 struct thread_info *tp;
248
249 for (tp = thread_list; tp; tp = tp->next)
250 if (ptid_equal (tp->ptid, ptid))
251 return 1;
252
253 return 0; /* Never heard of 'im */
254 }
255
256 /* Print a list of thread ids currently known, and the total number of
257 threads. To be used from within catch_errors. */
258 static int
259 do_captured_list_thread_ids (struct ui_out *uiout, void *arg)
260 {
261 struct thread_info *tp;
262 int num = 0;
263 struct cleanup *cleanup_chain;
264
265 prune_threads ();
266 target_find_new_threads ();
267
268 cleanup_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "thread-ids");
269
270 for (tp = thread_list; tp; tp = tp->next)
271 {
272 num++;
273 ui_out_field_int (uiout, "thread-id", tp->num);
274 }
275
276 do_cleanups (cleanup_chain);
277 ui_out_field_int (uiout, "number-of-threads", num);
278 return GDB_RC_OK;
279 }
280
281 /* Official gdblib interface function to get a list of thread ids and
282 the total number. */
283 enum gdb_rc
284 gdb_list_thread_ids (struct ui_out *uiout)
285 {
286 return catch_exceptions (uiout, do_captured_list_thread_ids, NULL,
287 NULL, RETURN_MASK_ALL);
288 }
289
290 /* Load infrun state for the thread PID. */
291
292 void
293 load_infrun_state (ptid_t ptid,
294 CORE_ADDR *prev_pc,
295 int *trap_expected,
296 struct breakpoint **step_resume_breakpoint,
297 CORE_ADDR *step_range_start,
298 CORE_ADDR *step_range_end,
299 struct frame_id *step_frame_id,
300 int *handling_longjmp,
301 int *another_trap,
302 int *stepping_through_solib_after_catch,
303 bpstat *stepping_through_solib_catchpoints,
304 int *stepping_through_sigtramp,
305 int *current_line,
306 struct symtab **current_symtab)
307 {
308 struct thread_info *tp;
309
310 /* If we can't find the thread, then we're debugging a single threaded
311 process. No need to do anything in that case. */
312 tp = find_thread_id (pid_to_thread_id (ptid));
313 if (tp == NULL)
314 return;
315
316 *prev_pc = tp->prev_pc;
317 *trap_expected = tp->trap_expected;
318 *step_resume_breakpoint = tp->step_resume_breakpoint;
319 *step_range_start = tp->step_range_start;
320 *step_range_end = tp->step_range_end;
321 *step_frame_id = tp->step_frame_id;
322 *handling_longjmp = tp->handling_longjmp;
323 *another_trap = tp->another_trap;
324 *stepping_through_solib_after_catch =
325 tp->stepping_through_solib_after_catch;
326 *stepping_through_solib_catchpoints =
327 tp->stepping_through_solib_catchpoints;
328 *stepping_through_sigtramp = tp->stepping_through_sigtramp;
329 *current_line = tp->current_line;
330 *current_symtab = tp->current_symtab;
331 }
332
333 /* Save infrun state for the thread PID. */
334
335 void
336 save_infrun_state (ptid_t ptid,
337 CORE_ADDR prev_pc,
338 int trap_expected,
339 struct breakpoint *step_resume_breakpoint,
340 CORE_ADDR step_range_start,
341 CORE_ADDR step_range_end,
342 const struct frame_id *step_frame_id,
343 int handling_longjmp,
344 int another_trap,
345 int stepping_through_solib_after_catch,
346 bpstat stepping_through_solib_catchpoints,
347 int stepping_through_sigtramp,
348 int current_line,
349 struct symtab *current_symtab)
350 {
351 struct thread_info *tp;
352
353 /* If we can't find the thread, then we're debugging a single-threaded
354 process. Nothing to do in that case. */
355 tp = find_thread_id (pid_to_thread_id (ptid));
356 if (tp == NULL)
357 return;
358
359 tp->prev_pc = prev_pc;
360 tp->trap_expected = trap_expected;
361 tp->step_resume_breakpoint = step_resume_breakpoint;
362 tp->step_range_start = step_range_start;
363 tp->step_range_end = step_range_end;
364 tp->step_frame_id = (*step_frame_id);
365 tp->handling_longjmp = handling_longjmp;
366 tp->another_trap = another_trap;
367 tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
368 tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
369 tp->stepping_through_sigtramp = stepping_through_sigtramp;
370 tp->current_line = current_line;
371 tp->current_symtab = current_symtab;
372 }
373
374 /* Return true if TP is an active thread. */
375 static int
376 thread_alive (struct thread_info *tp)
377 {
378 if (PIDGET (tp->ptid) == -1)
379 return 0;
380 if (!target_thread_alive (tp->ptid))
381 {
382 tp->ptid = pid_to_ptid (-1); /* Mark it as dead */
383 return 0;
384 }
385 return 1;
386 }
387
388 static void
389 prune_threads (void)
390 {
391 struct thread_info *tp, *next;
392
393 for (tp = thread_list; tp; tp = next)
394 {
395 next = tp->next;
396 if (!thread_alive (tp))
397 delete_thread (tp->ptid);
398 }
399 }
400
401 /* Print information about currently known threads
402
403 * Note: this has the drawback that it _really_ switches
404 * threads, which frees the frame cache. A no-side
405 * effects info-threads command would be nicer.
406 */
407
408 static void
409 info_threads_command (char *arg, int from_tty)
410 {
411 struct thread_info *tp;
412 ptid_t current_ptid;
413 struct frame_info *cur_frame;
414 struct frame_id saved_frame_id = get_frame_id (get_selected_frame ());
415 char *extra_info;
416
417 prune_threads ();
418 target_find_new_threads ();
419 current_ptid = inferior_ptid;
420 for (tp = thread_list; tp; tp = tp->next)
421 {
422 if (ptid_equal (tp->ptid, current_ptid))
423 printf_filtered ("* ");
424 else
425 printf_filtered (" ");
426
427 #ifdef HPUXHPPA
428 printf_filtered ("%d %s", tp->num, target_tid_to_str (tp->ptid));
429 #else
430 printf_filtered ("%d %s", tp->num, target_pid_to_str (tp->ptid));
431 #endif
432
433 extra_info = target_extra_thread_info (tp);
434 if (extra_info)
435 printf_filtered (" (%s)", extra_info);
436 puts_filtered (" ");
437
438 switch_to_thread (tp->ptid);
439 print_stack_frame (get_selected_frame (), 0, LOCATION);
440 }
441
442 switch_to_thread (current_ptid);
443
444 /* Restores the frame set by the user before the "info threads"
445 command. We have finished the info-threads display by switching
446 back to the current thread. That switch has put us at the top of
447 the stack (leaf frame). */
448 cur_frame = frame_find_by_id (saved_frame_id);
449 if (cur_frame == NULL)
450 {
451 /* Ooops, can't restore, tell user where we are. */
452 warning ("Couldn't restore frame in current thread, at frame 0");
453 print_stack_frame (get_selected_frame (), 0, LOCATION);
454 }
455 else
456 {
457 select_frame (cur_frame);
458 /* re-show current frame. */
459 show_stack_frame (cur_frame);
460 }
461 }
462
463 /* Switch from one thread to another. */
464
465 static void
466 switch_to_thread (ptid_t ptid)
467 {
468 if (ptid_equal (ptid, inferior_ptid))
469 return;
470
471 inferior_ptid = ptid;
472 flush_cached_frames ();
473 registers_changed ();
474 stop_pc = read_pc ();
475 select_frame (get_current_frame ());
476 }
477
478 static void
479 restore_current_thread (ptid_t ptid)
480 {
481 if (!ptid_equal (ptid, inferior_ptid))
482 {
483 switch_to_thread (ptid);
484 print_stack_frame (get_current_frame (), 1, SRC_LINE);
485 }
486 }
487
488 struct current_thread_cleanup
489 {
490 ptid_t inferior_ptid;
491 };
492
493 static void
494 do_restore_current_thread_cleanup (void *arg)
495 {
496 struct current_thread_cleanup *old = arg;
497 restore_current_thread (old->inferior_ptid);
498 xfree (old);
499 }
500
501 static struct cleanup *
502 make_cleanup_restore_current_thread (ptid_t inferior_ptid)
503 {
504 struct current_thread_cleanup *old
505 = xmalloc (sizeof (struct current_thread_cleanup));
506 old->inferior_ptid = inferior_ptid;
507 return make_cleanup (do_restore_current_thread_cleanup, old);
508 }
509
510 /* Apply a GDB command to a list of threads. List syntax is a whitespace
511 seperated list of numbers, or ranges, or the keyword `all'. Ranges consist
512 of two numbers seperated by a hyphen. Examples:
513
514 thread apply 1 2 7 4 backtrace Apply backtrace cmd to threads 1,2,7,4
515 thread apply 2-7 9 p foo(1) Apply p foo(1) cmd to threads 2->7 & 9
516 thread apply all p x/i $pc Apply x/i $pc cmd to all threads
517 */
518
519 static void
520 thread_apply_all_command (char *cmd, int from_tty)
521 {
522 struct thread_info *tp;
523 struct cleanup *old_chain;
524 struct cleanup *saved_cmd_cleanup_chain;
525 char *saved_cmd;
526
527 if (cmd == NULL || *cmd == '\000')
528 error ("Please specify a command following the thread ID list");
529
530 old_chain = make_cleanup_restore_current_thread (inferior_ptid);
531
532 /* It is safe to update the thread list now, before
533 traversing it for "thread apply all". MVS */
534 target_find_new_threads ();
535
536 /* Save a copy of the command in case it is clobbered by
537 execute_command */
538 saved_cmd = xstrdup (cmd);
539 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
540 for (tp = thread_list; tp; tp = tp->next)
541 if (thread_alive (tp))
542 {
543 switch_to_thread (tp->ptid);
544 #ifdef HPUXHPPA
545 printf_filtered ("\nThread %d (%s):\n",
546 tp->num, target_tid_to_str (inferior_ptid));
547 #else
548 printf_filtered ("\nThread %d (%s):\n", tp->num,
549 target_pid_to_str (inferior_ptid));
550 #endif
551 execute_command (cmd, from_tty);
552 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
553 }
554
555 do_cleanups (saved_cmd_cleanup_chain);
556 do_cleanups (old_chain);
557 }
558
559 static void
560 thread_apply_command (char *tidlist, int from_tty)
561 {
562 char *cmd;
563 char *p;
564 struct cleanup *old_chain;
565 struct cleanup *saved_cmd_cleanup_chain;
566 char *saved_cmd;
567
568 if (tidlist == NULL || *tidlist == '\000')
569 error ("Please specify a thread ID list");
570
571 for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
572
573 if (*cmd == '\000')
574 error ("Please specify a command following the thread ID list");
575
576 old_chain = make_cleanup_restore_current_thread (inferior_ptid);
577
578 /* Save a copy of the command in case it is clobbered by
579 execute_command */
580 saved_cmd = xstrdup (cmd);
581 saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
582 while (tidlist < cmd)
583 {
584 struct thread_info *tp;
585 int start, end;
586
587 start = strtol (tidlist, &p, 10);
588 if (p == tidlist)
589 error ("Error parsing %s", tidlist);
590 tidlist = p;
591
592 while (*tidlist == ' ' || *tidlist == '\t')
593 tidlist++;
594
595 if (*tidlist == '-') /* Got a range of IDs? */
596 {
597 tidlist++; /* Skip the - */
598 end = strtol (tidlist, &p, 10);
599 if (p == tidlist)
600 error ("Error parsing %s", tidlist);
601 tidlist = p;
602
603 while (*tidlist == ' ' || *tidlist == '\t')
604 tidlist++;
605 }
606 else
607 end = start;
608
609 for (; start <= end; start++)
610 {
611 tp = find_thread_id (start);
612
613 if (!tp)
614 warning ("Unknown thread %d.", start);
615 else if (!thread_alive (tp))
616 warning ("Thread %d has terminated.", start);
617 else
618 {
619 switch_to_thread (tp->ptid);
620 #ifdef HPUXHPPA
621 printf_filtered ("\nThread %d (%s):\n", tp->num,
622 target_tid_to_str (inferior_ptid));
623 #else
624 printf_filtered ("\nThread %d (%s):\n", tp->num,
625 target_pid_to_str (inferior_ptid));
626 #endif
627 execute_command (cmd, from_tty);
628 strcpy (cmd, saved_cmd); /* Restore exact command used previously */
629 }
630 }
631 }
632
633 do_cleanups (saved_cmd_cleanup_chain);
634 do_cleanups (old_chain);
635 }
636
637 /* Switch to the specified thread. Will dispatch off to thread_apply_command
638 if prefix of arg is `apply'. */
639
640 static void
641 thread_command (char *tidstr, int from_tty)
642 {
643 if (!tidstr)
644 {
645 /* Don't generate an error, just say which thread is current. */
646 if (target_has_stack)
647 printf_filtered ("[Current thread is %d (%s)]\n",
648 pid_to_thread_id (inferior_ptid),
649 #if defined(HPUXHPPA)
650 target_tid_to_str (inferior_ptid)
651 #else
652 target_pid_to_str (inferior_ptid)
653 #endif
654 );
655 else
656 error ("No stack.");
657 return;
658 }
659
660 gdb_thread_select (uiout, tidstr);
661 }
662
663 static int
664 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
665 {
666 int num;
667 struct thread_info *tp;
668
669 num = value_as_long (parse_and_eval (tidstr));
670
671 tp = find_thread_id (num);
672
673 if (!tp)
674 error ("Thread ID %d not known.", num);
675
676 if (!thread_alive (tp))
677 error ("Thread ID %d has terminated.\n", num);
678
679 switch_to_thread (tp->ptid);
680
681 ui_out_text (uiout, "[Switching to thread ");
682 ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
683 ui_out_text (uiout, " (");
684 #if defined(HPUXHPPA)
685 ui_out_text (uiout, target_tid_to_str (inferior_ptid));
686 #else
687 ui_out_text (uiout, target_pid_to_str (inferior_ptid));
688 #endif
689 ui_out_text (uiout, ")]");
690
691 print_stack_frame (get_selected_frame (), 1, SRC_AND_LOC);
692 return GDB_RC_OK;
693 }
694
695 enum gdb_rc
696 gdb_thread_select (struct ui_out *uiout, char *tidstr)
697 {
698 return catch_exceptions (uiout, do_captured_thread_select, tidstr,
699 NULL, RETURN_MASK_ALL);
700 }
701
702 /* Commands with a prefix of `thread'. */
703 struct cmd_list_element *thread_cmd_list = NULL;
704
705 void
706 _initialize_thread (void)
707 {
708 static struct cmd_list_element *thread_apply_list = NULL;
709
710 add_info ("threads", info_threads_command,
711 "IDs of currently known threads.");
712
713 add_prefix_cmd ("thread", class_run, thread_command,
714 "Use this command to switch between threads.\n\
715 The new thread ID must be currently known.", &thread_cmd_list, "thread ", 1, &cmdlist);
716
717 add_prefix_cmd ("apply", class_run, thread_apply_command,
718 "Apply a command to a list of threads.",
719 &thread_apply_list, "apply ", 1, &thread_cmd_list);
720
721 add_cmd ("all", class_run, thread_apply_all_command,
722 "Apply a command to all threads.", &thread_apply_list);
723
724 if (!xdb_commands)
725 add_com_alias ("t", "thread", class_run, 1);
726 }
This page took 0.04506 seconds and 4 git commands to generate.