Commit | Line | Data |
---|---|---|
ce3a066d | 1 | /* Inferior process information for the remote server for GDB. |
6aba47ca | 2 | Copyright (C) 2002, 2005, 2007 Free Software Foundation, Inc. |
ce3a066d DJ |
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 | |
6f0f660e EZ |
20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
ce3a066d DJ |
22 | |
23 | #include <stdlib.h> | |
24 | ||
25 | #include "server.h" | |
26 | ||
0d62e5e8 | 27 | struct thread_info |
ce3a066d | 28 | { |
0d62e5e8 | 29 | struct inferior_list_entry entry; |
611cb4a5 | 30 | void *target_data; |
c04a1aa8 | 31 | void *regcache_data; |
a06660f7 | 32 | unsigned int gdb_id; |
ce3a066d DJ |
33 | }; |
34 | ||
0d62e5e8 | 35 | struct inferior_list all_threads; |
255e7678 DJ |
36 | struct inferior_list all_dlls; |
37 | int dlls_changed; | |
0d62e5e8 DJ |
38 | |
39 | struct thread_info *current_inferior; | |
40 | ||
41 | #define get_thread(inf) ((struct thread_info *)(inf)) | |
255e7678 | 42 | #define get_dll(inf) ((struct dll_info *)(inf)) |
0d62e5e8 DJ |
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 | } | |
ce3a066d | 69 | |
ae13219e DJ |
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 | ||
ce3a066d | 75 | void |
0d62e5e8 | 76 | change_inferior_id (struct inferior_list *list, |
a1928bad | 77 | unsigned long new_id) |
ce3a066d | 78 | { |
0d62e5e8 DJ |
79 | if (list->head != list->tail) |
80 | error ("tried to change thread ID after multiple threads are created"); | |
ce3a066d | 81 | |
0d62e5e8 DJ |
82 | list->head->id = new_id; |
83 | } | |
ce3a066d | 84 | |
0d62e5e8 DJ |
85 | void |
86 | remove_inferior (struct inferior_list *list, | |
87 | struct inferior_list_entry *entry) | |
88 | { | |
89 | struct inferior_list_entry **cur; | |
ce3a066d | 90 | |
0d62e5e8 DJ |
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; | |
ce3a066d | 105 | |
0d62e5e8 DJ |
106 | (*cur)->next = entry->next; |
107 | ||
108 | if (list->tail == entry) | |
109 | list->tail = *cur; | |
110 | } | |
111 | ||
112 | void | |
a06660f7 | 113 | add_thread (unsigned long thread_id, void *target_data, unsigned int gdb_id) |
0d62e5e8 | 114 | { |
255e7678 | 115 | struct thread_info *new_thread = malloc (sizeof (*new_thread)); |
0d62e5e8 DJ |
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); | |
255e7678 | 122 | |
ce3a066d | 123 | if (current_inferior == NULL) |
0d62e5e8 | 124 | current_inferior = new_thread; |
ce3a066d | 125 | |
0d62e5e8 DJ |
126 | new_thread->target_data = target_data; |
127 | set_inferior_regcache_data (new_thread, new_register_cache ()); | |
a06660f7 DJ |
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 | ||
dae5f5cf DJ |
153 | struct thread_info * |
154 | gdb_id_to_thread (unsigned int gdb_id) | |
a06660f7 DJ |
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) | |
dae5f5cf | 162 | return thread; |
a06660f7 DJ |
163 | inf = inf->next; |
164 | } | |
165 | ||
dae5f5cf DJ |
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; | |
0d62e5e8 | 175 | } |
c04a1aa8 | 176 | |
0d62e5e8 DJ |
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); | |
ce3a066d DJ |
190 | } |
191 | ||
0d62e5e8 DJ |
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; | |
ce3a066d | 197 | |
0d62e5e8 | 198 | while (inf != NULL) |
ce3a066d | 199 | { |
0d62e5e8 DJ |
200 | if ((*func) (inf, arg)) |
201 | return inf; | |
202 | inf = inf->next; | |
203 | } | |
611cb4a5 | 204 | |
0d62e5e8 DJ |
205 | return NULL; |
206 | } | |
611cb4a5 | 207 | |
0d62e5e8 | 208 | struct inferior_list_entry * |
a1928bad | 209 | find_inferior_id (struct inferior_list *list, unsigned long id) |
0d62e5e8 DJ |
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; | |
ce3a066d DJ |
218 | } |
219 | ||
0d62e5e8 | 220 | return NULL; |
ce3a066d | 221 | } |
611cb4a5 DJ |
222 | |
223 | void * | |
0d62e5e8 | 224 | inferior_target_data (struct thread_info *inferior) |
611cb4a5 DJ |
225 | { |
226 | return inferior->target_data; | |
227 | } | |
228 | ||
229 | void | |
0d62e5e8 | 230 | set_inferior_target_data (struct thread_info *inferior, void *data) |
611cb4a5 DJ |
231 | { |
232 | inferior->target_data = data; | |
233 | } | |
c04a1aa8 DJ |
234 | |
235 | void * | |
0d62e5e8 | 236 | inferior_regcache_data (struct thread_info *inferior) |
c04a1aa8 DJ |
237 | { |
238 | return inferior->regcache_data; | |
239 | } | |
240 | ||
241 | void | |
0d62e5e8 | 242 | set_inferior_regcache_data (struct thread_info *inferior, void *data) |
c04a1aa8 DJ |
243 | { |
244 | inferior->regcache_data = data; | |
245 | } | |
255e7678 DJ |
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 | } |