1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002, 2005, 2007 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
29 struct inferior_list_entry entry
;
35 struct inferior_list all_threads
;
36 struct inferior_list all_dlls
;
39 struct thread_info
*current_inferior
;
41 #define get_thread(inf) ((struct thread_info *)(inf))
42 #define get_dll(inf) ((struct dll_info *)(inf))
45 add_inferior_to_list (struct inferior_list
*list
,
46 struct inferior_list_entry
*new_inferior
)
48 new_inferior
->next
= NULL
;
49 if (list
->tail
!= NULL
)
50 list
->tail
->next
= new_inferior
;
52 list
->head
= new_inferior
;
53 list
->tail
= new_inferior
;
57 for_each_inferior (struct inferior_list
*list
,
58 void (*action
) (struct inferior_list_entry
*))
60 struct inferior_list_entry
*cur
= list
->head
, *next
;
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. */
76 change_inferior_id (struct inferior_list
*list
,
79 if (list
->head
!= list
->tail
)
80 error ("tried to change thread ID after multiple threads are created");
82 list
->head
->id
= new_id
;
86 remove_inferior (struct inferior_list
*list
,
87 struct inferior_list_entry
*entry
)
89 struct inferior_list_entry
**cur
;
91 if (list
->head
== entry
)
93 list
->head
= entry
->next
;
94 if (list
->tail
== entry
)
95 list
->tail
= list
->head
;
100 while (*cur
&& (*cur
)->next
!= entry
)
106 (*cur
)->next
= entry
->next
;
108 if (list
->tail
== entry
)
113 add_thread (unsigned long thread_id
, void *target_data
, unsigned int gdb_id
)
115 struct thread_info
*new_thread
= malloc (sizeof (*new_thread
));
117 memset (new_thread
, 0, sizeof (*new_thread
));
119 new_thread
->entry
.id
= thread_id
;
121 add_inferior_to_list (&all_threads
, & new_thread
->entry
);
123 if (current_inferior
== NULL
)
124 current_inferior
= new_thread
;
126 new_thread
->target_data
= target_data
;
127 set_inferior_regcache_data (new_thread
, new_register_cache ());
128 new_thread
->gdb_id
= gdb_id
;
132 thread_id_to_gdb_id (unsigned long thread_id
)
134 struct inferior_list_entry
*inf
= all_threads
.head
;
138 struct thread_info
*thread
= get_thread (inf
);
139 if (inf
->id
== thread_id
)
140 return thread
->gdb_id
;
148 thread_to_gdb_id (struct thread_info
*thread
)
150 return thread
->gdb_id
;
154 gdb_id_to_thread (unsigned int gdb_id
)
156 struct inferior_list_entry
*inf
= all_threads
.head
;
160 struct thread_info
*thread
= get_thread (inf
);
161 if (thread
->gdb_id
== gdb_id
)
170 gdb_id_to_thread_id (unsigned int gdb_id
)
172 struct thread_info
*thread
= gdb_id_to_thread (gdb_id
);
174 return thread
? thread
->entry
.id
: 0;
178 free_one_thread (struct inferior_list_entry
*inf
)
180 struct thread_info
*thread
= get_thread (inf
);
181 free_register_cache (inferior_regcache_data (thread
));
186 remove_thread (struct thread_info
*thread
)
188 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
189 free_one_thread (&thread
->entry
);
192 struct inferior_list_entry
*
193 find_inferior (struct inferior_list
*list
,
194 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
196 struct inferior_list_entry
*inf
= list
->head
;
200 if ((*func
) (inf
, arg
))
208 struct inferior_list_entry
*
209 find_inferior_id (struct inferior_list
*list
, unsigned long id
)
211 struct inferior_list_entry
*inf
= list
->head
;
224 inferior_target_data (struct thread_info
*inferior
)
226 return inferior
->target_data
;
230 set_inferior_target_data (struct thread_info
*inferior
, void *data
)
232 inferior
->target_data
= data
;
236 inferior_regcache_data (struct thread_info
*inferior
)
238 return inferior
->regcache_data
;
242 set_inferior_regcache_data (struct thread_info
*inferior
, void *data
)
244 inferior
->regcache_data
= data
;
248 free_one_dll (struct inferior_list_entry
*inf
)
250 struct dll_info
*dll
= get_dll (inf
);
251 if (dll
->name
!= NULL
)
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. */
260 match_dll (struct inferior_list_entry
*inf
, void *arg
)
262 struct dll_info
*iter
= (void *) inf
;
263 struct dll_info
*key
= arg
;
265 if (key
->base_addr
!= ~(CORE_ADDR
) 0
266 && iter
->base_addr
== key
->base_addr
)
268 else if (key
->name
!= NULL
269 && iter
->name
!= NULL
270 && strcmp (key
->name
, iter
->name
) == 0)
276 /* Record a newly loaded DLL at BASE_ADDR. */
279 loaded_dll (const char *name
, CORE_ADDR base_addr
)
281 struct dll_info
*new_dll
= malloc (sizeof (*new_dll
));
282 memset (new_dll
, 0, sizeof (*new_dll
));
284 new_dll
->entry
.id
= -1;
286 new_dll
->name
= strdup (name
);
287 new_dll
->base_addr
= base_addr
;
289 add_inferior_to_list (&all_dlls
, &new_dll
->entry
);
293 /* Record that the DLL with NAME and BASE_ADDR has been unloaded. */
296 unloaded_dll (const char *name
, CORE_ADDR base_addr
)
298 struct dll_info
*dll
;
299 struct dll_info key_dll
;
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
;
305 dll
= (void *) find_inferior (&all_dlls
, match_dll
, &key_dll
);
306 remove_inferior (&all_dlls
, &dll
->entry
);
307 free_one_dll (&dll
->entry
);
311 #define clear_list(LIST) \
312 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
315 clear_inferiors (void)
317 for_each_inferior (&all_threads
, free_one_thread
);
318 for_each_inferior (&all_dlls
, free_one_dll
);
320 clear_list (&all_threads
);
321 clear_list (&all_dlls
);