1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
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.
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.
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/>. */
27 struct inferior_list_entry entry
;
33 struct inferior_list all_threads
;
34 struct inferior_list all_dlls
;
37 struct thread_info
*current_inferior
;
39 #define get_thread(inf) ((struct thread_info *)(inf))
40 #define get_dll(inf) ((struct dll_info *)(inf))
43 add_inferior_to_list (struct inferior_list
*list
,
44 struct inferior_list_entry
*new_inferior
)
46 new_inferior
->next
= NULL
;
47 if (list
->tail
!= NULL
)
48 list
->tail
->next
= new_inferior
;
50 list
->head
= new_inferior
;
51 list
->tail
= new_inferior
;
55 for_each_inferior (struct inferior_list
*list
,
56 void (*action
) (struct inferior_list_entry
*))
58 struct inferior_list_entry
*cur
= list
->head
, *next
;
69 remove_inferior (struct inferior_list
*list
,
70 struct inferior_list_entry
*entry
)
72 struct inferior_list_entry
**cur
;
74 if (list
->head
== entry
)
76 list
->head
= entry
->next
;
77 if (list
->tail
== entry
)
78 list
->tail
= list
->head
;
83 while (*cur
&& (*cur
)->next
!= entry
)
89 (*cur
)->next
= entry
->next
;
91 if (list
->tail
== entry
)
96 add_thread (unsigned long thread_id
, void *target_data
, unsigned int gdb_id
)
98 struct thread_info
*new_thread
= xmalloc (sizeof (*new_thread
));
100 memset (new_thread
, 0, sizeof (*new_thread
));
102 new_thread
->entry
.id
= thread_id
;
104 add_inferior_to_list (&all_threads
, & new_thread
->entry
);
106 if (current_inferior
== NULL
)
107 current_inferior
= new_thread
;
109 new_thread
->target_data
= target_data
;
110 set_inferior_regcache_data (new_thread
, new_register_cache ());
111 new_thread
->gdb_id
= gdb_id
;
115 thread_id_to_gdb_id (unsigned long thread_id
)
117 struct inferior_list_entry
*inf
= all_threads
.head
;
121 struct thread_info
*thread
= get_thread (inf
);
122 if (inf
->id
== thread_id
)
123 return thread
->gdb_id
;
131 thread_to_gdb_id (struct thread_info
*thread
)
133 return thread
->gdb_id
;
137 gdb_id_to_thread (unsigned int gdb_id
)
139 struct inferior_list_entry
*inf
= all_threads
.head
;
143 struct thread_info
*thread
= get_thread (inf
);
144 if (thread
->gdb_id
== gdb_id
)
153 gdb_id_to_thread_id (unsigned int gdb_id
)
155 struct thread_info
*thread
= gdb_id_to_thread (gdb_id
);
157 return thread
? thread
->entry
.id
: 0;
161 free_one_thread (struct inferior_list_entry
*inf
)
163 struct thread_info
*thread
= get_thread (inf
);
164 free_register_cache (inferior_regcache_data (thread
));
169 remove_thread (struct thread_info
*thread
)
171 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
172 free_one_thread (&thread
->entry
);
175 struct inferior_list_entry
*
176 find_inferior (struct inferior_list
*list
,
177 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
179 struct inferior_list_entry
*inf
= list
->head
;
183 struct inferior_list_entry
*next
;
186 if ((*func
) (inf
, arg
))
194 struct inferior_list_entry
*
195 find_inferior_id (struct inferior_list
*list
, unsigned long id
)
197 struct inferior_list_entry
*inf
= list
->head
;
210 inferior_target_data (struct thread_info
*inferior
)
212 return inferior
->target_data
;
216 set_inferior_target_data (struct thread_info
*inferior
, void *data
)
218 inferior
->target_data
= data
;
222 inferior_regcache_data (struct thread_info
*inferior
)
224 return inferior
->regcache_data
;
228 set_inferior_regcache_data (struct thread_info
*inferior
, void *data
)
230 inferior
->regcache_data
= data
;
234 free_one_dll (struct inferior_list_entry
*inf
)
236 struct dll_info
*dll
= get_dll (inf
);
237 if (dll
->name
!= NULL
)
242 /* Find a DLL with the same name and/or base address. A NULL name in
243 the key is ignored; so is an all-ones base address. */
246 match_dll (struct inferior_list_entry
*inf
, void *arg
)
248 struct dll_info
*iter
= (void *) inf
;
249 struct dll_info
*key
= arg
;
251 if (key
->base_addr
!= ~(CORE_ADDR
) 0
252 && iter
->base_addr
== key
->base_addr
)
254 else if (key
->name
!= NULL
255 && iter
->name
!= NULL
256 && strcmp (key
->name
, iter
->name
) == 0)
262 /* Record a newly loaded DLL at BASE_ADDR. */
265 loaded_dll (const char *name
, CORE_ADDR base_addr
)
267 struct dll_info
*new_dll
= xmalloc (sizeof (*new_dll
));
268 memset (new_dll
, 0, sizeof (*new_dll
));
270 new_dll
->entry
.id
= -1;
272 new_dll
->name
= xstrdup (name
);
273 new_dll
->base_addr
= base_addr
;
275 add_inferior_to_list (&all_dlls
, &new_dll
->entry
);
279 /* Record that the DLL with NAME and BASE_ADDR has been unloaded. */
282 unloaded_dll (const char *name
, CORE_ADDR base_addr
)
284 struct dll_info
*dll
;
285 struct dll_info key_dll
;
287 /* Be careful not to put the key DLL in any list. */
288 key_dll
.name
= (char *) name
;
289 key_dll
.base_addr
= base_addr
;
291 dll
= (void *) find_inferior (&all_dlls
, match_dll
, &key_dll
);
292 remove_inferior (&all_dlls
, &dll
->entry
);
293 free_one_dll (&dll
->entry
);
297 #define clear_list(LIST) \
298 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
301 clear_inferiors (void)
303 for_each_inferior (&all_threads
, free_one_thread
);
304 for_each_inferior (&all_dlls
, free_one_dll
);
306 clear_list (&all_threads
);
307 clear_list (&all_dlls
);
309 current_inferior
= NULL
;
312 /* Two utility functions for a truly degenerate inferior_list: a simple
316 add_pid_to_list (struct inferior_list
*list
, unsigned long pid
)
318 struct inferior_list_entry
*new_entry
;
320 new_entry
= xmalloc (sizeof (struct inferior_list_entry
));
322 add_inferior_to_list (list
, new_entry
);
326 pull_pid_from_list (struct inferior_list
*list
, unsigned long pid
)
328 struct inferior_list_entry
*new_entry
;
330 new_entry
= find_inferior_id (list
, pid
);
331 if (new_entry
== NULL
)
335 remove_inferior (list
, new_entry
);