]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/gdbserver/inferiors.c
Copyright updates for 2007.
[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 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
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include <stdlib.h>
24
25 #include "server.h"
26
27 struct thread_info
28 {
29 struct inferior_list_entry entry;
30 void *target_data;
31 void *regcache_data;
32 unsigned int gdb_id;
33 };
34
35 struct inferior_list all_threads;
36
37 struct thread_info *current_inferior;
38
39 #define get_thread(inf) ((struct thread_info *)(inf))
40
41 void
42 add_inferior_to_list (struct inferior_list *list,
43 struct inferior_list_entry *new_inferior)
44 {
45 new_inferior->next = NULL;
46 if (list->tail != NULL)
47 list->tail->next = new_inferior;
48 else
49 list->head = new_inferior;
50 list->tail = new_inferior;
51 }
52
53 void
54 for_each_inferior (struct inferior_list *list,
55 void (*action) (struct inferior_list_entry *))
56 {
57 struct inferior_list_entry *cur = list->head, *next;
58
59 while (cur != NULL)
60 {
61 next = cur->next;
62 (*action) (cur);
63 cur = next;
64 }
65 }
66
67 void
68 change_inferior_id (struct inferior_list *list,
69 unsigned long new_id)
70 {
71 if (list->head != list->tail)
72 error ("tried to change thread ID after multiple threads are created");
73
74 list->head->id = new_id;
75 }
76
77 void
78 remove_inferior (struct inferior_list *list,
79 struct inferior_list_entry *entry)
80 {
81 struct inferior_list_entry **cur;
82
83 if (list->head == entry)
84 {
85 list->head = entry->next;
86 if (list->tail == entry)
87 list->tail = list->head;
88 return;
89 }
90
91 cur = &list->head;
92 while (*cur && (*cur)->next != entry)
93 cur = &(*cur)->next;
94
95 if (*cur == NULL)
96 return;
97
98 (*cur)->next = entry->next;
99
100 if (list->tail == entry)
101 list->tail = *cur;
102 }
103
104 void
105 add_thread (unsigned long thread_id, void *target_data, unsigned int gdb_id)
106 {
107 struct thread_info *new_thread
108 = (struct thread_info *) malloc (sizeof (*new_thread));
109
110 memset (new_thread, 0, sizeof (*new_thread));
111
112 new_thread->entry.id = thread_id;
113
114 add_inferior_to_list (&all_threads, & new_thread->entry);
115
116 if (current_inferior == NULL)
117 current_inferior = new_thread;
118
119 new_thread->target_data = target_data;
120 set_inferior_regcache_data (new_thread, new_register_cache ());
121 new_thread->gdb_id = gdb_id;
122 }
123
124 unsigned int
125 thread_id_to_gdb_id (unsigned long thread_id)
126 {
127 struct inferior_list_entry *inf = all_threads.head;
128
129 while (inf != NULL)
130 {
131 struct thread_info *thread = get_thread (inf);
132 if (inf->id == thread_id)
133 return thread->gdb_id;
134 inf = inf->next;
135 }
136
137 return 0;
138 }
139
140 unsigned int
141 thread_to_gdb_id (struct thread_info *thread)
142 {
143 return thread->gdb_id;
144 }
145
146 struct thread_info *
147 gdb_id_to_thread (unsigned int gdb_id)
148 {
149 struct inferior_list_entry *inf = all_threads.head;
150
151 while (inf != NULL)
152 {
153 struct thread_info *thread = get_thread (inf);
154 if (thread->gdb_id == gdb_id)
155 return thread;
156 inf = inf->next;
157 }
158
159 return NULL;
160 }
161
162 unsigned long
163 gdb_id_to_thread_id (unsigned int gdb_id)
164 {
165 struct thread_info *thread = gdb_id_to_thread (gdb_id);
166
167 return thread ? thread->entry.id : 0;
168 }
169
170 static void
171 free_one_thread (struct inferior_list_entry *inf)
172 {
173 struct thread_info *thread = get_thread (inf);
174 free_register_cache (inferior_regcache_data (thread));
175 free (thread);
176 }
177
178 void
179 remove_thread (struct thread_info *thread)
180 {
181 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
182 free_one_thread (&thread->entry);
183 }
184
185 void
186 clear_inferiors (void)
187 {
188 for_each_inferior (&all_threads, free_one_thread);
189
190 all_threads.head = all_threads.tail = NULL;
191 }
192
193 struct inferior_list_entry *
194 find_inferior (struct inferior_list *list,
195 int (*func) (struct inferior_list_entry *, void *), void *arg)
196 {
197 struct inferior_list_entry *inf = list->head;
198
199 while (inf != NULL)
200 {
201 if ((*func) (inf, arg))
202 return inf;
203 inf = inf->next;
204 }
205
206 return NULL;
207 }
208
209 struct inferior_list_entry *
210 find_inferior_id (struct inferior_list *list, unsigned long id)
211 {
212 struct inferior_list_entry *inf = list->head;
213
214 while (inf != NULL)
215 {
216 if (inf->id == id)
217 return inf;
218 inf = inf->next;
219 }
220
221 return NULL;
222 }
223
224 void *
225 inferior_target_data (struct thread_info *inferior)
226 {
227 return inferior->target_data;
228 }
229
230 void
231 set_inferior_target_data (struct thread_info *inferior, void *data)
232 {
233 inferior->target_data = data;
234 }
235
236 void *
237 inferior_regcache_data (struct thread_info *inferior)
238 {
239 return inferior->regcache_data;
240 }
241
242 void
243 set_inferior_regcache_data (struct thread_info *inferior, void *data)
244 {
245 inferior->regcache_data = data;
246 }