* remote-utils.c (remote_open): Set SO_KEEPALIVE on remote_desc
[deliverable/binutils-gdb.git] / gdb / gdbserver / inferiors.c
CommitLineData
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 27struct 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 35struct inferior_list all_threads;
255e7678
DJ
36struct inferior_list all_dlls;
37int dlls_changed;
0d62e5e8
DJ
38
39struct 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
44void
45add_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
56void
57for_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 75void
0d62e5e8 76change_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
85void
86remove_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
112void
a06660f7 113add_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
131unsigned int
132thread_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
147unsigned int
148thread_to_gdb_id (struct thread_info *thread)
149{
150 return thread->gdb_id;
151}
152
dae5f5cf
DJ
153struct thread_info *
154gdb_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
169unsigned long
170gdb_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
177static void
178free_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
185void
186remove_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
192struct inferior_list_entry *
193find_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 208struct inferior_list_entry *
a1928bad 209find_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
223void *
0d62e5e8 224inferior_target_data (struct thread_info *inferior)
611cb4a5
DJ
225{
226 return inferior->target_data;
227}
228
229void
0d62e5e8 230set_inferior_target_data (struct thread_info *inferior, void *data)
611cb4a5
DJ
231{
232 inferior->target_data = data;
233}
c04a1aa8
DJ
234
235void *
0d62e5e8 236inferior_regcache_data (struct thread_info *inferior)
c04a1aa8
DJ
237{
238 return inferior->regcache_data;
239}
240
241void
0d62e5e8 242set_inferior_regcache_data (struct thread_info *inferior, void *data)
c04a1aa8
DJ
243{
244 inferior->regcache_data = data;
245}
255e7678
DJ
246
247static void
248free_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
259static int
260match_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
278void
279loaded_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
295void
296unloaded_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
314void
315clear_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}
This page took 0.377226 seconds and 4 git commands to generate.