2007-07-17 Pedro Alves <pedro_alves@portugalmail.pt>
[deliverable/binutils-gdb.git] / gdb / gdbserver / inferiors.c
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002, 2005, 2007 Free Software Foundation, Inc.
3
4 Contributed by MontaVista Software.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include <stdlib.h>
24
25 #include "server.h"
26
27 struct thread_info
28 {
29 struct inferior_list_entry entry;
30 void *target_data;
31 void *regcache_data;
32 unsigned int gdb_id;
33 };
34
35 struct inferior_list all_threads;
36 struct inferior_list all_dlls;
37 int dlls_changed;
38
39 struct thread_info *current_inferior;
40
41 #define get_thread(inf) ((struct thread_info *)(inf))
42 #define get_dll(inf) ((struct dll_info *)(inf))
43
44 void
45 add_inferior_to_list (struct inferior_list *list,
46 struct inferior_list_entry *new_inferior)
47 {
48 new_inferior->next = NULL;
49 if (list->tail != NULL)
50 list->tail->next = new_inferior;
51 else
52 list->head = new_inferior;
53 list->tail = new_inferior;
54 }
55
56 void
57 for_each_inferior (struct inferior_list *list,
58 void (*action) (struct inferior_list_entry *))
59 {
60 struct inferior_list_entry *cur = list->head, *next;
61
62 while (cur != NULL)
63 {
64 next = cur->next;
65 (*action) (cur);
66 cur = next;
67 }
68 }
69
70 /* When debugging a single-threaded program, the threads list (such as
71 it is) is indexed by PID. When debugging a multi-threaded program,
72 we index by TID. This ugly routine replaces the
73 first-debugged-thread's PID with its TID. */
74
75 void
76 change_inferior_id (struct inferior_list *list,
77 unsigned long new_id)
78 {
79 if (list->head != list->tail)
80 error ("tried to change thread ID after multiple threads are created");
81
82 list->head->id = new_id;
83 }
84
85 void
86 remove_inferior (struct inferior_list *list,
87 struct inferior_list_entry *entry)
88 {
89 struct inferior_list_entry **cur;
90
91 if (list->head == entry)
92 {
93 list->head = entry->next;
94 if (list->tail == entry)
95 list->tail = list->head;
96 return;
97 }
98
99 cur = &list->head;
100 while (*cur && (*cur)->next != entry)
101 cur = &(*cur)->next;
102
103 if (*cur == NULL)
104 return;
105
106 (*cur)->next = entry->next;
107
108 if (list->tail == entry)
109 list->tail = *cur;
110 }
111
112 void
113 add_thread (unsigned long thread_id, void *target_data, unsigned int gdb_id)
114 {
115 struct thread_info *new_thread = malloc (sizeof (*new_thread));
116
117 memset (new_thread, 0, sizeof (*new_thread));
118
119 new_thread->entry.id = thread_id;
120
121 add_inferior_to_list (&all_threads, & new_thread->entry);
122
123 if (current_inferior == NULL)
124 current_inferior = new_thread;
125
126 new_thread->target_data = target_data;
127 set_inferior_regcache_data (new_thread, new_register_cache ());
128 new_thread->gdb_id = gdb_id;
129 }
130
131 unsigned int
132 thread_id_to_gdb_id (unsigned long thread_id)
133 {
134 struct inferior_list_entry *inf = all_threads.head;
135
136 while (inf != NULL)
137 {
138 struct thread_info *thread = get_thread (inf);
139 if (inf->id == thread_id)
140 return thread->gdb_id;
141 inf = inf->next;
142 }
143
144 return 0;
145 }
146
147 unsigned int
148 thread_to_gdb_id (struct thread_info *thread)
149 {
150 return thread->gdb_id;
151 }
152
153 struct thread_info *
154 gdb_id_to_thread (unsigned int gdb_id)
155 {
156 struct inferior_list_entry *inf = all_threads.head;
157
158 while (inf != NULL)
159 {
160 struct thread_info *thread = get_thread (inf);
161 if (thread->gdb_id == gdb_id)
162 return thread;
163 inf = inf->next;
164 }
165
166 return NULL;
167 }
168
169 unsigned long
170 gdb_id_to_thread_id (unsigned int gdb_id)
171 {
172 struct thread_info *thread = gdb_id_to_thread (gdb_id);
173
174 return thread ? thread->entry.id : 0;
175 }
176
177 static void
178 free_one_thread (struct inferior_list_entry *inf)
179 {
180 struct thread_info *thread = get_thread (inf);
181 free_register_cache (inferior_regcache_data (thread));
182 free (thread);
183 }
184
185 void
186 remove_thread (struct thread_info *thread)
187 {
188 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
189 free_one_thread (&thread->entry);
190 }
191
192 struct inferior_list_entry *
193 find_inferior (struct inferior_list *list,
194 int (*func) (struct inferior_list_entry *, void *), void *arg)
195 {
196 struct inferior_list_entry *inf = list->head;
197
198 while (inf != NULL)
199 {
200 if ((*func) (inf, arg))
201 return inf;
202 inf = inf->next;
203 }
204
205 return NULL;
206 }
207
208 struct inferior_list_entry *
209 find_inferior_id (struct inferior_list *list, unsigned long id)
210 {
211 struct inferior_list_entry *inf = list->head;
212
213 while (inf != NULL)
214 {
215 if (inf->id == id)
216 return inf;
217 inf = inf->next;
218 }
219
220 return NULL;
221 }
222
223 void *
224 inferior_target_data (struct thread_info *inferior)
225 {
226 return inferior->target_data;
227 }
228
229 void
230 set_inferior_target_data (struct thread_info *inferior, void *data)
231 {
232 inferior->target_data = data;
233 }
234
235 void *
236 inferior_regcache_data (struct thread_info *inferior)
237 {
238 return inferior->regcache_data;
239 }
240
241 void
242 set_inferior_regcache_data (struct thread_info *inferior, void *data)
243 {
244 inferior->regcache_data = data;
245 }
246
247 static void
248 free_one_dll (struct inferior_list_entry *inf)
249 {
250 struct dll_info *dll = get_dll (inf);
251 if (dll->name != NULL)
252 free (dll->name);
253 free (dll);
254 }
255
256 /* Find a DLL with the same name and/or base address. A NULL name in
257 the key is ignored; so is an all-ones base address. */
258
259 static int
260 match_dll (struct inferior_list_entry *inf, void *arg)
261 {
262 struct dll_info *iter = (void *) inf;
263 struct dll_info *key = arg;
264
265 if (key->base_addr != ~(CORE_ADDR) 0
266 && iter->base_addr == key->base_addr)
267 return 1;
268 else if (key->name != NULL
269 && iter->name != NULL
270 && strcmp (key->name, iter->name) == 0)
271 return 1;
272
273 return 0;
274 }
275
276 /* Record a newly loaded DLL at BASE_ADDR. */
277
278 void
279 loaded_dll (const char *name, CORE_ADDR base_addr)
280 {
281 struct dll_info *new_dll = malloc (sizeof (*new_dll));
282 memset (new_dll, 0, sizeof (*new_dll));
283
284 new_dll->entry.id = -1;
285
286 new_dll->name = strdup (name);
287 new_dll->base_addr = base_addr;
288
289 add_inferior_to_list (&all_dlls, &new_dll->entry);
290 dlls_changed = 1;
291 }
292
293 /* Record that the DLL with NAME and BASE_ADDR has been unloaded. */
294
295 void
296 unloaded_dll (const char *name, CORE_ADDR base_addr)
297 {
298 struct dll_info *dll;
299 struct dll_info key_dll;
300
301 /* Be careful not to put the key DLL in any list. */
302 key_dll.name = (char *) name;
303 key_dll.base_addr = base_addr;
304
305 dll = (void *) find_inferior (&all_dlls, match_dll, &key_dll);
306 remove_inferior (&all_dlls, &dll->entry);
307 free_one_dll (&dll->entry);
308 dlls_changed = 1;
309 }
310
311 #define clear_list(LIST) \
312 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
313
314 void
315 clear_inferiors (void)
316 {
317 for_each_inferior (&all_threads, free_one_thread);
318 for_each_inferior (&all_dlls, free_one_dll);
319
320 clear_list (&all_threads);
321 clear_list (&all_dlls);
322 }
This page took 0.036866 seconds and 5 git commands to generate.