OSDN Git Service

c4ce6088baed596c0794f2dee6339822389165b4
[pf3gnuchains/pf3gnuchains3x.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4    2000, 2001, 2002, 2003, 2004, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>.  */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "inferior.h"
27 #include "environ.h"
28 #include "value.h"
29 #include "target.h"
30 #include "gdbthread.h"
31 #include "exceptions.h"
32 #include "command.h"
33 #include "gdbcmd.h"
34 #include "regcache.h"
35 #include "gdb.h"
36 #include "gdb_string.h"
37
38 #include <ctype.h>
39 #include <sys/types.h>
40 #include <signal.h>
41 #include "ui-out.h"
42
43 /* Definition of struct thread_info exported to gdbthread.h */
44
45 /* Prototypes for exported functions. */
46
47 void _initialize_thread (void);
48
49 /* Prototypes for local functions. */
50
51 static struct thread_info *thread_list = NULL;
52 static int highest_thread_num;
53
54 static struct thread_info *find_thread_id (int num);
55
56 static void thread_command (char *tidstr, int from_tty);
57 static void thread_apply_all_command (char *, int);
58 static int thread_alive (struct thread_info *);
59 static void info_threads_command (char *, int);
60 static void thread_apply_command (char *, int);
61 static void restore_current_thread (ptid_t);
62 static void prune_threads (void);
63 static struct cleanup *make_cleanup_restore_current_thread (ptid_t,
64                                                             struct frame_id);
65
66 void
67 delete_step_resume_breakpoint (void *arg)
68 {
69   struct breakpoint **breakpointp = (struct breakpoint **) arg;
70   struct thread_info *tp;
71
72   if (*breakpointp != NULL)
73     {
74       delete_breakpoint (*breakpointp);
75       for (tp = thread_list; tp; tp = tp->next)
76         if (tp->step_resume_breakpoint == *breakpointp)
77           tp->step_resume_breakpoint = NULL;
78
79       *breakpointp = NULL;
80     }
81 }
82
83 static void
84 free_thread (struct thread_info *tp)
85 {
86   /* NOTE: this will take care of any left-over step_resume breakpoints,
87      but not any user-specified thread-specific breakpoints.  We can not
88      delete the breakpoint straight-off, because the inferior might not
89      be stopped at the moment.  */
90   if (tp->step_resume_breakpoint)
91     tp->step_resume_breakpoint->disposition = disp_del_at_next_stop;
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, char **error_message)
285 {
286   if (catch_exceptions_with_msg (uiout, do_captured_list_thread_ids, NULL,
287                                  error_message, RETURN_MASK_ALL) < 0)
288     return GDB_RC_FAIL;
289   return GDB_RC_OK;
290 }
291
292 /* Load infrun state for the thread PID.  */
293
294 void
295 load_infrun_state (ptid_t ptid,
296                    CORE_ADDR *prev_pc,
297                    int *trap_expected,
298                    struct breakpoint **step_resume_breakpoint,
299                    CORE_ADDR *step_range_start,
300                    CORE_ADDR *step_range_end,
301                    struct frame_id *step_frame_id,
302                    int *handling_longjmp,
303                    int *stepping_over_breakpoint,
304                    int *stepping_through_solib_after_catch,
305                    bpstat *stepping_through_solib_catchpoints,
306                    int *current_line,
307                    struct symtab **current_symtab)
308 {
309   struct thread_info *tp;
310
311   /* If we can't find the thread, then we're debugging a single threaded
312      process.  No need to do anything in that case.  */
313   tp = find_thread_id (pid_to_thread_id (ptid));
314   if (tp == NULL)
315     return;
316
317   *prev_pc = tp->prev_pc;
318   *trap_expected = tp->trap_expected;
319   *step_resume_breakpoint = tp->step_resume_breakpoint;
320   *step_range_start = tp->step_range_start;
321   *step_range_end = tp->step_range_end;
322   *step_frame_id = tp->step_frame_id;
323   *handling_longjmp = tp->handling_longjmp;
324   *stepping_over_breakpoint = tp->stepping_over_breakpoint;
325   *stepping_through_solib_after_catch =
326     tp->stepping_through_solib_after_catch;
327   *stepping_through_solib_catchpoints =
328     tp->stepping_through_solib_catchpoints;
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 stepping_over_breakpoint,
345                    int stepping_through_solib_after_catch,
346                    bpstat stepping_through_solib_catchpoints,
347                    int current_line,
348                    struct symtab *current_symtab)
349 {
350   struct thread_info *tp;
351
352   /* If we can't find the thread, then we're debugging a single-threaded
353      process.  Nothing to do in that case.  */
354   tp = find_thread_id (pid_to_thread_id (ptid));
355   if (tp == NULL)
356     return;
357
358   tp->prev_pc = prev_pc;
359   tp->trap_expected = trap_expected;
360   tp->step_resume_breakpoint = step_resume_breakpoint;
361   tp->step_range_start = step_range_start;
362   tp->step_range_end = step_range_end;
363   tp->step_frame_id = (*step_frame_id);
364   tp->handling_longjmp = handling_longjmp;
365   tp->stepping_over_breakpoint = stepping_over_breakpoint;
366   tp->stepping_through_solib_after_catch = stepping_through_solib_after_catch;
367   tp->stepping_through_solib_catchpoints = stepping_through_solib_catchpoints;
368   tp->current_line = current_line;
369   tp->current_symtab = current_symtab;
370 }
371
372 /* Return true if TP is an active thread. */
373 static int
374 thread_alive (struct thread_info *tp)
375 {
376   if (PIDGET (tp->ptid) == -1)
377     return 0;
378   if (!target_thread_alive (tp->ptid))
379     {
380       tp->ptid = pid_to_ptid (-1);      /* Mark it as dead */
381       return 0;
382     }
383   return 1;
384 }
385
386 static void
387 prune_threads (void)
388 {
389   struct thread_info *tp, *next;
390
391   for (tp = thread_list; tp; tp = next)
392     {
393       next = tp->next;
394       if (!thread_alive (tp))
395         delete_thread (tp->ptid);
396     }
397 }
398
399 /* Print information about currently known threads 
400
401  * Note: this has the drawback that it _really_ switches
402  *       threads, which frees the frame cache.  A no-side
403  *       effects info-threads command would be nicer.
404  */
405
406 static void
407 info_threads_command (char *arg, int from_tty)
408 {
409   struct thread_info *tp;
410   ptid_t current_ptid;
411   struct frame_info *cur_frame;
412   struct cleanup *old_chain;
413   struct frame_id saved_frame_id;
414   char *extra_info;
415
416   /* Backup current thread and selected frame.  */
417   saved_frame_id = get_frame_id (get_selected_frame (NULL));
418   old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
419
420   prune_threads ();
421   target_find_new_threads ();
422   current_ptid = inferior_ptid;
423   for (tp = thread_list; tp; tp = tp->next)
424     {
425       if (ptid_equal (tp->ptid, current_ptid))
426         printf_filtered ("* ");
427       else
428         printf_filtered ("  ");
429
430       printf_filtered ("%d %s", tp->num, target_tid_to_str (tp->ptid));
431
432       extra_info = target_extra_thread_info (tp);
433       if (extra_info)
434         printf_filtered (" (%s)", extra_info);
435       puts_filtered ("  ");
436       /* That switch put us at the top of the stack (leaf frame).  */
437       switch_to_thread (tp->ptid);
438       print_stack_frame (get_selected_frame (NULL), 0, LOCATION);
439     }
440
441   /* Restores the current thread and the frame selected before
442      the "info threads" command.  */
443   do_cleanups (old_chain);
444
445   /*  If case we were not able to find the original frame, print the
446       new selected frame.  */
447   if (frame_find_by_id (saved_frame_id) == NULL)
448     {
449       warning (_("Couldn't restore frame in current thread, at frame 0"));
450       print_stack_frame (get_selected_frame (NULL), 0, LOCATION);
451     }
452 }
453
454 /* Switch from one thread to another. */
455
456 void
457 switch_to_thread (ptid_t ptid)
458 {
459   if (ptid_equal (ptid, inferior_ptid))
460     return;
461
462   inferior_ptid = ptid;
463   reinit_frame_cache ();
464   registers_changed ();
465   stop_pc = read_pc ();
466 }
467
468 static void
469 restore_current_thread (ptid_t ptid)
470 {
471   if (!ptid_equal (ptid, inferior_ptid))
472     {
473       switch_to_thread (ptid);
474     }
475 }
476
477 static void
478 restore_selected_frame (struct frame_id a_frame_id)
479 {
480   struct frame_info *selected_frame_info = NULL;
481
482   if (frame_id_eq (a_frame_id, null_frame_id))
483     return;        
484
485   if ((selected_frame_info = frame_find_by_id (a_frame_id)) != NULL)
486     {
487       select_frame (selected_frame_info);
488     }
489 }
490
491 struct current_thread_cleanup
492 {
493   ptid_t inferior_ptid;
494   struct frame_id selected_frame_id;
495 };
496
497 static void
498 do_restore_current_thread_cleanup (void *arg)
499 {
500   struct current_thread_cleanup *old = arg;
501   restore_current_thread (old->inferior_ptid);
502   restore_selected_frame (old->selected_frame_id);
503   xfree (old);
504 }
505
506 static struct cleanup *
507 make_cleanup_restore_current_thread (ptid_t inferior_ptid, 
508                                      struct frame_id a_frame_id)
509 {
510   struct current_thread_cleanup *old
511     = xmalloc (sizeof (struct current_thread_cleanup));
512   old->inferior_ptid = inferior_ptid;
513   old->selected_frame_id = a_frame_id;
514   return make_cleanup (do_restore_current_thread_cleanup, old);
515 }
516
517 /* Apply a GDB command to a list of threads.  List syntax is a whitespace
518    seperated list of numbers, or ranges, or the keyword `all'.  Ranges consist
519    of two numbers seperated by a hyphen.  Examples:
520
521    thread apply 1 2 7 4 backtrace       Apply backtrace cmd to threads 1,2,7,4
522    thread apply 2-7 9 p foo(1)  Apply p foo(1) cmd to threads 2->7 & 9
523    thread apply all p x/i $pc   Apply x/i $pc cmd to all threads
524  */
525
526 static void
527 thread_apply_all_command (char *cmd, int from_tty)
528 {
529   struct thread_info *tp;
530   struct cleanup *old_chain;
531   struct cleanup *saved_cmd_cleanup_chain;
532   char *saved_cmd;
533   struct frame_id saved_frame_id;
534   ptid_t current_ptid;
535   int thread_has_changed = 0;
536
537   if (cmd == NULL || *cmd == '\000')
538     error (_("Please specify a command following the thread ID list"));
539   
540   current_ptid = inferior_ptid;
541   saved_frame_id = get_frame_id (get_selected_frame (NULL));
542   old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
543
544   /* It is safe to update the thread list now, before
545      traversing it for "thread apply all".  MVS */
546   target_find_new_threads ();
547
548   /* Save a copy of the command in case it is clobbered by
549      execute_command */
550   saved_cmd = xstrdup (cmd);
551   saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
552   for (tp = thread_list; tp; tp = tp->next)
553     if (thread_alive (tp))
554       {
555         switch_to_thread (tp->ptid);
556         printf_filtered (_("\nThread %d (%s):\n"),
557                          tp->num, target_tid_to_str (inferior_ptid));
558         execute_command (cmd, from_tty);
559         strcpy (cmd, saved_cmd);        /* Restore exact command used previously */
560       }
561
562   if (!ptid_equal (current_ptid, inferior_ptid))
563     thread_has_changed = 1;
564
565   do_cleanups (saved_cmd_cleanup_chain);
566   do_cleanups (old_chain);
567   /* Print stack frame only if we changed thread.  */
568   if (thread_has_changed)
569     print_stack_frame (get_current_frame (), 1, SRC_LINE);
570
571 }
572
573 static void
574 thread_apply_command (char *tidlist, int from_tty)
575 {
576   char *cmd;
577   char *p;
578   struct cleanup *old_chain;
579   struct cleanup *saved_cmd_cleanup_chain;
580   char *saved_cmd;
581   struct frame_id saved_frame_id;
582   ptid_t current_ptid;
583   int thread_has_changed = 0;
584
585   if (tidlist == NULL || *tidlist == '\000')
586     error (_("Please specify a thread ID list"));
587
588   for (cmd = tidlist; *cmd != '\000' && !isalpha (*cmd); cmd++);
589
590   if (*cmd == '\000')
591     error (_("Please specify a command following the thread ID list"));
592
593   current_ptid = inferior_ptid;
594   saved_frame_id = get_frame_id (get_selected_frame (NULL));
595   old_chain = make_cleanup_restore_current_thread (inferior_ptid, saved_frame_id);
596
597   /* Save a copy of the command in case it is clobbered by
598      execute_command */
599   saved_cmd = xstrdup (cmd);
600   saved_cmd_cleanup_chain = make_cleanup (xfree, (void *) saved_cmd);
601   while (tidlist < cmd)
602     {
603       struct thread_info *tp;
604       int start, end;
605
606       start = strtol (tidlist, &p, 10);
607       if (p == tidlist)
608         error (_("Error parsing %s"), tidlist);
609       tidlist = p;
610
611       while (*tidlist == ' ' || *tidlist == '\t')
612         tidlist++;
613
614       if (*tidlist == '-')      /* Got a range of IDs? */
615         {
616           tidlist++;            /* Skip the - */
617           end = strtol (tidlist, &p, 10);
618           if (p == tidlist)
619             error (_("Error parsing %s"), tidlist);
620           tidlist = p;
621
622           while (*tidlist == ' ' || *tidlist == '\t')
623             tidlist++;
624         }
625       else
626         end = start;
627
628       for (; start <= end; start++)
629         {
630           tp = find_thread_id (start);
631
632           if (!tp)
633             warning (_("Unknown thread %d."), start);
634           else if (!thread_alive (tp))
635             warning (_("Thread %d has terminated."), start);
636           else
637             {
638               switch_to_thread (tp->ptid);
639               printf_filtered (_("\nThread %d (%s):\n"), tp->num,
640                                target_tid_to_str (inferior_ptid));
641               execute_command (cmd, from_tty);
642               strcpy (cmd, saved_cmd);  /* Restore exact command used previously */
643             }
644         }
645     }
646
647   if (!ptid_equal (current_ptid, inferior_ptid))
648     thread_has_changed = 1;
649
650   do_cleanups (saved_cmd_cleanup_chain);
651   do_cleanups (old_chain);
652   /* Print stack frame only if we changed thread.  */
653   if (thread_has_changed)
654     print_stack_frame (get_current_frame (), 1, SRC_LINE);
655 }
656
657 /* Switch to the specified thread.  Will dispatch off to thread_apply_command
658    if prefix of arg is `apply'.  */
659
660 static void
661 thread_command (char *tidstr, int from_tty)
662 {
663   if (!tidstr)
664     {
665       /* Don't generate an error, just say which thread is current. */
666       if (target_has_stack)
667         printf_filtered (_("[Current thread is %d (%s)]\n"),
668                          pid_to_thread_id (inferior_ptid),
669                          target_tid_to_str (inferior_ptid));
670       else
671         error (_("No stack."));
672       return;
673     }
674
675   gdb_thread_select (uiout, tidstr, NULL);
676 }
677
678 static int
679 do_captured_thread_select (struct ui_out *uiout, void *tidstr)
680 {
681   int num;
682   struct thread_info *tp;
683
684   num = value_as_long (parse_and_eval (tidstr));
685
686   tp = find_thread_id (num);
687
688   if (!tp)
689     error (_("Thread ID %d not known."), num);
690
691   if (!thread_alive (tp))
692     error (_("Thread ID %d has terminated."), num);
693
694   switch_to_thread (tp->ptid);
695
696   ui_out_text (uiout, "[Switching to thread ");
697   ui_out_field_int (uiout, "new-thread-id", pid_to_thread_id (inferior_ptid));
698   ui_out_text (uiout, " (");
699   ui_out_text (uiout, target_tid_to_str (inferior_ptid));
700   ui_out_text (uiout, ")]");
701
702   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
703   return GDB_RC_OK;
704 }
705
706 enum gdb_rc
707 gdb_thread_select (struct ui_out *uiout, char *tidstr, char **error_message)
708 {
709   if (catch_exceptions_with_msg (uiout, do_captured_thread_select, tidstr,
710                                  error_message, RETURN_MASK_ALL) < 0)
711     return GDB_RC_FAIL;
712   return GDB_RC_OK;
713 }
714
715 /* Commands with a prefix of `thread'.  */
716 struct cmd_list_element *thread_cmd_list = NULL;
717
718 void
719 _initialize_thread (void)
720 {
721   static struct cmd_list_element *thread_apply_list = NULL;
722
723   add_info ("threads", info_threads_command,
724             _("IDs of currently known threads."));
725
726   add_prefix_cmd ("thread", class_run, thread_command, _("\
727 Use this command to switch between threads.\n\
728 The new thread ID must be currently known."),
729                   &thread_cmd_list, "thread ", 1, &cmdlist);
730
731   add_prefix_cmd ("apply", class_run, thread_apply_command,
732                   _("Apply a command to a list of threads."),
733                   &thread_apply_list, "thread apply ", 1, &thread_cmd_list);
734
735   add_cmd ("all", class_run, thread_apply_all_command,
736            _("Apply a command to all threads."), &thread_apply_list);
737
738   if (!xdb_commands)
739     add_com_alias ("t", "thread", class_run, 1);
740 }