Mon Aug 2 11:30:57 1993 Stu Grossman (grossman at cygnus.com)
[binutils-gdb.git] / gdb / thread.c
1 /* Multi-process/thread control for GDB, the GNU debugger.
2 Copyright 1986, 1987, 1988, 1993
3
4 Contributed by Lynx Real-Time Systems, Inc. Los Gatos, CA.
5 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
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.
13
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.
18
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
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 "thread.h"
31
32 #include <sys/types.h>
33 #include <signal.h>
34
35 /*#include "lynxos-core.h"*/
36
37 struct thread_info
38 {
39 struct thread_info *next;
40 int pid; /* Actual process id */
41 int num; /* Convenient handle */
42 };
43
44 static struct thread_info *thread_list = NULL;
45 static int highest_thread_num;
46
47 static void thread_info PARAMS ((void));
48
49 static void thread_command PARAMS ((char * tidstr, int from_tty));
50
51 static void prune_threads PARAMS ((void));
52
53 static void thread_switch PARAMS ((int pid));
54
55 void
56 init_thread_list ()
57 {
58 struct thread_info *tp, *tpnext;
59
60 if (!thread_list)
61 return;
62
63 for (tp = thread_list; tp; tp = tpnext)
64 {
65 tpnext = tp->next;
66 free (tp);
67 }
68
69 thread_list = NULL;
70 highest_thread_num = 0;
71 }
72
73 void
74 add_thread (pid)
75 int pid;
76 {
77 struct thread_info *tp;
78
79 tp = xmalloc (sizeof (struct thread_info));
80
81 tp->pid = pid;
82 tp->num = ++highest_thread_num;
83 tp->next = thread_list;
84 thread_list = tp;
85 }
86
87 static struct thread_info *
88 find_thread_id (num)
89 int num;
90 {
91 struct thread_info *tp;
92
93 for (tp = thread_list; tp; tp = tp->next)
94 if (tp->num == num)
95 return tp;
96
97 return NULL;
98 }
99
100 int
101 in_thread_list (pid)
102 int pid;
103 {
104 struct thread_info *tp;
105
106 for (tp = thread_list; tp; tp = tp->next)
107 if (tp->pid == pid)
108 return 1;
109
110 return 0; /* Never heard of 'im */
111 }
112
113 #if 0
114 void
115 bfd_get_core_threads (abfd)
116 bfd *abfd;
117 {
118 int i;
119
120 inferior_pid = BUILDPID (inferior_pid, core_thread (abfd)->pid);
121 for (i = 0; i < core_pss (abfd).threadcnt; i++)
122 add_thread (core_thread (abfd)[i].pid);
123 }
124 #endif
125
126 static void
127 prune_threads ()
128 {
129 struct thread_info *tp, *tpprev;
130
131 tpprev = 0;
132
133 for (tp = thread_list; tp; tp = tp->next)
134 if (tp->pid == -1)
135 {
136 if (tpprev)
137 tpprev->next = tp->next;
138 else
139 thread_list = NULL;
140
141 free (tp);
142 }
143 else
144 tpprev = tp;
145 }
146
147 /* Print information about currently known threads */
148
149 static void
150 info_threads_command (arg, from_tty)
151 char *arg;
152 int from_tty;
153 {
154 struct thread_info *tp;
155 int current_pid = inferior_pid;
156
157 for (tp = thread_list; tp; tp = tp->next)
158 {
159 if (target_has_execution
160 && kill (tp->pid, 0) == -1)
161 {
162 tp->pid == -1; /* Mark it as dead */
163 continue;
164 }
165
166 if (tp->pid == current_pid)
167 printf_filtered ("* ");
168 else
169 printf_filtered (" ");
170
171 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
172
173 thread_switch (tp->pid);
174 print_stack_frame (selected_frame, -1, 0);
175 }
176
177 thread_switch (current_pid);
178 prune_threads ();
179 }
180
181 /* Switch from one thread to another. */
182
183 void
184 thread_switch (pid)
185 int pid;
186 {
187 if (pid == inferior_pid)
188 return;
189
190 inferior_pid = pid;
191 pc_changed = 0;
192 flush_cached_frames ();
193 registers_changed ();
194 stop_pc = read_pc();
195 set_current_frame (create_new_frame (read_fp (), stop_pc));
196 stop_frame_address = FRAME_FP (get_current_frame ());
197 select_frame (get_current_frame (), 0);
198 }
199
200 static void
201 thread_command (tidstr, from_tty)
202 char *tidstr;
203 int from_tty;
204 {
205 int num;
206 struct thread_info *tp;
207
208 if (!tidstr)
209 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
210 see the IDs of currently known threads.");
211
212
213 num = atoi (tidstr);
214
215 tp = find_thread_id (num);
216
217 if (!tp)
218 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
219 see the IDs of currently known threads.", num);
220
221 thread_switch (tp->pid);
222
223 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
224 print_stack_frame (selected_frame, selected_frame_level, 1);
225 }
226
227 void
228 _initialize_thread ()
229 {
230 add_info ("threads", info_threads_command,
231 "IDs of currently known threads.");
232 add_com ("thread", class_info, thread_command,
233 "Use this command to switch between threads.\n\
234 The new thread ID must be currently known.");
235 }