]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/inferiors.c
Switch the license of all .c files to GPLv3.
[thirdparty/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 3 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, see <http://www.gnu.org/licenses/>. */
20
21 #include <stdlib.h>
22
23 #include "server.h"
24
25 struct thread_info
26 {
27 struct inferior_list_entry entry;
28 void *target_data;
29 void *regcache_data;
30 unsigned int gdb_id;
31 };
32
33 struct inferior_list all_threads;
34 struct inferior_list all_dlls;
35 int dlls_changed;
36
37 struct thread_info *current_inferior;
38
39 #define get_thread(inf) ((struct thread_info *)(inf))
40 #define get_dll(inf) ((struct dll_info *)(inf))
41
42 void
43 add_inferior_to_list (struct inferior_list *list,
44 struct inferior_list_entry *new_inferior)
45 {
46 new_inferior->next = NULL;
47 if (list->tail != NULL)
48 list->tail->next = new_inferior;
49 else
50 list->head = new_inferior;
51 list->tail = new_inferior;
52 }
53
54 void
55 for_each_inferior (struct inferior_list *list,
56 void (*action) (struct inferior_list_entry *))
57 {
58 struct inferior_list_entry *cur = list->head, *next;
59
60 while (cur != NULL)
61 {
62 next = cur->next;
63 (*action) (cur);
64 cur = next;
65 }
66 }
67
68 /* When debugging a single-threaded program, the threads list (such as
69 it is) is indexed by PID. When debugging a multi-threaded program,
70 we index by TID. This ugly routine replaces the
71 first-debugged-thread's PID with its TID. */
72
73 void
74 change_inferior_id (struct inferior_list *list,
75 unsigned long new_id)
76 {
77 if (list->head != list->tail)
78 error ("tried to change thread ID after multiple threads are created");
79
80 list->head->id = new_id;
81 }
82
83 void
84 remove_inferior (struct inferior_list *list,
85 struct inferior_list_entry *entry)
86 {
87 struct inferior_list_entry **cur;
88
89 if (list->head == entry)
90 {
91 list->head = entry->next;
92 if (list->tail == entry)
93 list->tail = list->head;
94 return;
95 }
96
97 cur = &list->head;
98 while (*cur && (*cur)->next != entry)
99 cur = &(*cur)->next;
100
101 if (*cur == NULL)
102 return;
103
104 (*cur)->next = entry->next;
105
106 if (list->tail == entry)
107 list->tail = *cur;
108 }
109
110 void
111 add_thread (unsigned long thread_id, void *target_data, unsigned int gdb_id)
112 {
113 struct thread_info *new_thread = malloc (sizeof (*new_thread));
114
115 memset (new_thread, 0, sizeof (*new_thread));
116
117 new_thread->entry.id = thread_id;
118
119 add_inferior_to_list (&all_threads, & new_thread->entry);
120
121 if (current_inferior == NULL)
122 current_inferior = new_thread;
123
124 new_thread->target_data = target_data;
125 set_inferior_regcache_data (new_thread, new_register_cache ());
126 new_thread->gdb_id = gdb_id;
127 }
128
129 unsigned int
130 thread_id_to_gdb_id (unsigned long thread_id)
131 {
132 struct inferior_list_entry *inf = all_threads.head;
133
134 while (inf != NULL)
135 {
136 struct thread_info *thread = get_thread (inf);
137 if (inf->id == thread_id)
138 return thread->gdb_id;
139 inf = inf->next;
140 }
141
142 return 0;
143 }
144
145 unsigned int
146 thread_to_gdb_id (struct thread_info *thread)
147 {
148 return thread->gdb_id;
149 }
150
151 struct thread_info *
152 gdb_id_to_thread (unsigned int gdb_id)
153 {
154 struct inferior_list_entry *inf = all_threads.head;
155
156 while (inf != NULL)
157 {
158 struct thread_info *thread = get_thread (inf);
159 if (thread->gdb_id == gdb_id)
160 return thread;
161 inf = inf->next;
162 }
163
164 return NULL;
165 }
166
167 unsigned long
168 gdb_id_to_thread_id (unsigned int gdb_id)
169 {
170 struct thread_info *thread = gdb_id_to_thread (gdb_id);
171
172 return thread ? thread->entry.id : 0;
173 }
174
175 static void
176 free_one_thread (struct inferior_list_entry *inf)
177 {
178 struct thread_info *thread = get_thread (inf);
179 free_register_cache (inferior_regcache_data (thread));
180 free (thread);
181 }
182
183 void
184 remove_thread (struct thread_info *thread)
185 {
186 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
187 free_one_thread (&thread->entry);
188 }
189
190 struct inferior_list_entry *
191 find_inferior (struct inferior_list *list,
192 int (*func) (struct inferior_list_entry *, void *), void *arg)
193 {
194 struct inferior_list_entry *inf = list->head;
195
196 while (inf != NULL)
197 {
198 if ((*func) (inf, arg))
199 return inf;
200 inf = inf->next;
201 }
202
203 return NULL;
204 }
205
206 struct inferior_list_entry *
207 find_inferior_id (struct inferior_list *list, unsigned long id)
208 {
209 struct inferior_list_entry *inf = list->head;
210
211 while (inf != NULL)
212 {
213 if (inf->id == id)
214 return inf;
215 inf = inf->next;
216 }
217
218 return NULL;
219 }
220
221 void *
222 inferior_target_data (struct thread_info *inferior)
223 {
224 return inferior->target_data;
225 }
226
227 void
228 set_inferior_target_data (struct thread_info *inferior, void *data)
229 {
230 inferior->target_data = data;
231 }
232
233 void *
234 inferior_regcache_data (struct thread_info *inferior)
235 {
236 return inferior->regcache_data;
237 }
238
239 void
240 set_inferior_regcache_data (struct thread_info *inferior, void *data)
241 {
242 inferior->regcache_data = data;
243 }
244
245 static void
246 free_one_dll (struct inferior_list_entry *inf)
247 {
248 struct dll_info *dll = get_dll (inf);
249 if (dll->name != NULL)
250 free (dll->name);
251 free (dll);
252 }
253
254 /* Find a DLL with the same name and/or base address. A NULL name in
255 the key is ignored; so is an all-ones base address. */
256
257 static int
258 match_dll (struct inferior_list_entry *inf, void *arg)
259 {
260 struct dll_info *iter = (void *) inf;
261 struct dll_info *key = arg;
262
263 if (key->base_addr != ~(CORE_ADDR) 0
264 && iter->base_addr == key->base_addr)
265 return 1;
266 else if (key->name != NULL
267 && iter->name != NULL
268 && strcmp (key->name, iter->name) == 0)
269 return 1;
270
271 return 0;
272 }
273
274 /* Record a newly loaded DLL at BASE_ADDR. */
275
276 void
277 loaded_dll (const char *name, CORE_ADDR base_addr)
278 {
279 struct dll_info *new_dll = malloc (sizeof (*new_dll));
280 memset (new_dll, 0, sizeof (*new_dll));
281
282 new_dll->entry.id = -1;
283
284 new_dll->name = strdup (name);
285 new_dll->base_addr = base_addr;
286
287 add_inferior_to_list (&all_dlls, &new_dll->entry);
288 dlls_changed = 1;
289 }
290
291 /* Record that the DLL with NAME and BASE_ADDR has been unloaded. */
292
293 void
294 unloaded_dll (const char *name, CORE_ADDR base_addr)
295 {
296 struct dll_info *dll;
297 struct dll_info key_dll;
298
299 /* Be careful not to put the key DLL in any list. */
300 key_dll.name = (char *) name;
301 key_dll.base_addr = base_addr;
302
303 dll = (void *) find_inferior (&all_dlls, match_dll, &key_dll);
304 remove_inferior (&all_dlls, &dll->entry);
305 free_one_dll (&dll->entry);
306 dlls_changed = 1;
307 }
308
309 #define clear_list(LIST) \
310 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
311
312 void
313 clear_inferiors (void)
314 {
315 for_each_inferior (&all_threads, free_one_thread);
316 for_each_inferior (&all_dlls, free_one_dll);
317
318 clear_list (&all_threads);
319 clear_list (&all_dlls);
320 }