some gcc lint
[deliverable/binutils-gdb.git] / gdb / thread.c
CommitLineData
da0baf42
SG
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
7This file is part of GDB.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25286543
SG
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"
46c28185 31#include "command.h"
25286543
SG
32
33#include <sys/types.h>
34#include <signal.h>
35
36/*#include "lynxos-core.h"*/
37
38struct thread_info
39{
40 struct thread_info *next;
41 int pid; /* Actual process id */
42 int num; /* Convenient handle */
43};
44
45static struct thread_info *thread_list = NULL;
46static int highest_thread_num;
47
25286543
SG
48static void thread_command PARAMS ((char * tidstr, int from_tty));
49
50static void prune_threads PARAMS ((void));
51
52static void thread_switch PARAMS ((int pid));
53
54void
55init_thread_list ()
56{
57 struct thread_info *tp, *tpnext;
58
59 if (!thread_list)
60 return;
61
62 for (tp = thread_list; tp; tp = tpnext)
63 {
64 tpnext = tp->next;
65 free (tp);
66 }
67
68 thread_list = NULL;
69 highest_thread_num = 0;
70}
71
72void
73add_thread (pid)
74 int pid;
75{
76 struct thread_info *tp;
77
3082244d 78 tp = (struct thread_info *) xmalloc (sizeof (struct thread_info));
25286543
SG
79
80 tp->pid = pid;
81 tp->num = ++highest_thread_num;
82 tp->next = thread_list;
83 thread_list = tp;
84}
85
86static struct thread_info *
87find_thread_id (num)
88 int num;
89{
90 struct thread_info *tp;
91
92 for (tp = thread_list; tp; tp = tp->next)
93 if (tp->num == num)
94 return tp;
95
96 return NULL;
97}
98
99int
100in_thread_list (pid)
101 int pid;
102{
103 struct thread_info *tp;
104
105 for (tp = thread_list; tp; tp = tp->next)
106 if (tp->pid == pid)
107 return 1;
108
109 return 0; /* Never heard of 'im */
110}
111
112#if 0
113void
114bfd_get_core_threads (abfd)
115 bfd *abfd;
116{
117 int i;
118
119 inferior_pid = BUILDPID (inferior_pid, core_thread (abfd)->pid);
120 for (i = 0; i < core_pss (abfd).threadcnt; i++)
121 add_thread (core_thread (abfd)[i].pid);
122}
123#endif
124
125static void
126prune_threads ()
127{
128 struct thread_info *tp, *tpprev;
129
130 tpprev = 0;
131
132 for (tp = thread_list; tp; tp = tp->next)
133 if (tp->pid == -1)
134 {
135 if (tpprev)
136 tpprev->next = tp->next;
137 else
138 thread_list = NULL;
139
140 free (tp);
141 }
142 else
143 tpprev = tp;
144}
145
146/* Print information about currently known threads */
147
148static void
149info_threads_command (arg, from_tty)
150 char *arg;
151 int from_tty;
152{
153 struct thread_info *tp;
154 int current_pid = inferior_pid;
155
156 for (tp = thread_list; tp; tp = tp->next)
157 {
158 if (target_has_execution
159 && kill (tp->pid, 0) == -1)
160 {
46c28185 161 tp->pid = -1; /* Mark it as dead */
25286543
SG
162 continue;
163 }
164
165 if (tp->pid == current_pid)
166 printf_filtered ("* ");
167 else
168 printf_filtered (" ");
169
170 printf_filtered ("%d %s ", tp->num, target_pid_to_str (tp->pid));
171
172 thread_switch (tp->pid);
173 print_stack_frame (selected_frame, -1, 0);
174 }
175
176 thread_switch (current_pid);
177 prune_threads ();
178}
179
180/* Switch from one thread to another. */
181
3082244d 182static void
25286543
SG
183thread_switch (pid)
184 int pid;
185{
186 if (pid == inferior_pid)
187 return;
188
189 inferior_pid = pid;
190 pc_changed = 0;
191 flush_cached_frames ();
192 registers_changed ();
193 stop_pc = read_pc();
194 set_current_frame (create_new_frame (read_fp (), stop_pc));
195 stop_frame_address = FRAME_FP (get_current_frame ());
196 select_frame (get_current_frame (), 0);
197}
198
199static void
200thread_command (tidstr, from_tty)
201 char *tidstr;
202 int from_tty;
203{
204 int num;
205 struct thread_info *tp;
206
207 if (!tidstr)
208 error ("Please specify a thread ID. Use the \"info threads\" command to\n\
209see the IDs of currently known threads.");
210
211
212 num = atoi (tidstr);
213
214 tp = find_thread_id (num);
215
216 if (!tp)
217 error ("Thread ID %d not known. Use the \"info threads\" command to\n\
218see the IDs of currently known threads.", num);
219
220 thread_switch (tp->pid);
221
222 printf_filtered ("[Switching to %s]\n", target_pid_to_str (inferior_pid));
223 print_stack_frame (selected_frame, selected_frame_level, 1);
224}
225
226void
227_initialize_thread ()
228{
229 add_info ("threads", info_threads_command,
230 "IDs of currently known threads.");
231 add_com ("thread", class_info, thread_command,
232 "Use this command to switch between threads.\n\
233The new thread ID must be currently known.");
234}
This page took 0.05507 seconds and 4 git commands to generate.