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
;
37 struct thread_info
*current_inferior
;
39 #define get_thread(inf) ((struct thread_info *)(inf))
42 add_inferior_to_list (struct inferior_list
*list
,
43 struct inferior_list_entry
*new_inferior
)
45 new_inferior
->next
= NULL
;
46 if (list
->tail
!= NULL
)
47 list
->tail
->next
= new_inferior
;
49 list
->head
= new_inferior
;
50 list
->tail
= new_inferior
;
54 for_each_inferior (struct inferior_list
*list
,
55 void (*action
) (struct inferior_list_entry
*))
57 struct inferior_list_entry
*cur
= list
->head
, *next
;
67 /* When debugging a single-threaded program, the threads list (such as
68 it is) is indexed by PID. When debugging a multi-threaded program,
69 we index by TID. This ugly routine replaces the
70 first-debugged-thread's PID with its TID. */
73 change_inferior_id (struct inferior_list
*list
,
76 if (list
->head
!= list
->tail
)
77 error ("tried to change thread ID after multiple threads are created");
79 list
->head
->id
= new_id
;
83 remove_inferior (struct inferior_list
*list
,
84 struct inferior_list_entry
*entry
)
86 struct inferior_list_entry
**cur
;
88 if (list
->head
== entry
)
90 list
->head
= entry
->next
;
91 if (list
->tail
== entry
)
92 list
->tail
= list
->head
;
97 while (*cur
&& (*cur
)->next
!= entry
)
103 (*cur
)->next
= entry
->next
;
105 if (list
->tail
== entry
)
110 add_thread (unsigned long thread_id
, void *target_data
, unsigned int gdb_id
)
112 struct thread_info
*new_thread
113 = (struct thread_info
*) malloc (sizeof (*new_thread
));
115 memset (new_thread
, 0, sizeof (*new_thread
));
117 new_thread
->entry
.id
= thread_id
;
119 add_inferior_to_list (&all_threads
, & new_thread
->entry
);
121 if (current_inferior
== NULL
)
122 current_inferior
= new_thread
;
124 new_thread
->target_data
= target_data
;
125 set_inferior_regcache_data (new_thread
, new_register_cache ());
126 new_thread
->gdb_id
= gdb_id
;
130 thread_id_to_gdb_id (unsigned long thread_id
)
132 struct inferior_list_entry
*inf
= all_threads
.head
;
136 struct thread_info
*thread
= get_thread (inf
);
137 if (inf
->id
== thread_id
)
138 return thread
->gdb_id
;
146 thread_to_gdb_id (struct thread_info
*thread
)
148 return thread
->gdb_id
;
152 gdb_id_to_thread (unsigned int gdb_id
)
154 struct inferior_list_entry
*inf
= all_threads
.head
;
158 struct thread_info
*thread
= get_thread (inf
);
159 if (thread
->gdb_id
== gdb_id
)
168 gdb_id_to_thread_id (unsigned int gdb_id
)
170 struct thread_info
*thread
= gdb_id_to_thread (gdb_id
);
172 return thread
? thread
->entry
.id
: 0;
176 free_one_thread (struct inferior_list_entry
*inf
)
178 struct thread_info
*thread
= get_thread (inf
);
179 free_register_cache (inferior_regcache_data (thread
));
184 remove_thread (struct thread_info
*thread
)
186 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
187 free_one_thread (&thread
->entry
);
191 clear_inferiors (void)
193 for_each_inferior (&all_threads
, free_one_thread
);
195 all_threads
.head
= all_threads
.tail
= NULL
;
198 struct inferior_list_entry
*
199 find_inferior (struct inferior_list
*list
,
200 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
202 struct inferior_list_entry
*inf
= list
->head
;
206 if ((*func
) (inf
, arg
))
214 struct inferior_list_entry
*
215 find_inferior_id (struct inferior_list
*list
, unsigned long id
)
217 struct inferior_list_entry
*inf
= list
->head
;
230 inferior_target_data (struct thread_info
*inferior
)
232 return inferior
->target_data
;
236 set_inferior_target_data (struct thread_info
*inferior
, void *data
)
238 inferior
->target_data
= data
;
242 inferior_regcache_data (struct thread_info
*inferior
)
244 return inferior
->regcache_data
;
248 set_inferior_regcache_data (struct thread_info
*inferior
, void *data
)
250 inferior
->regcache_data
= data
;