No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / gdbserver / inferiors.c
blob6fa665f951571bb0bb13f672fd2235b38674b27b
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002, 2005
3 Free Software Foundation, Inc.
5 Contributed by MontaVista Software.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 #include <stdlib.h>
26 #include "server.h"
28 struct thread_info
30 struct inferior_list_entry entry;
31 void *target_data;
32 void *regcache_data;
33 unsigned int gdb_id;
36 struct inferior_list all_threads;
38 struct thread_info *current_inferior;
40 #define get_thread(inf) ((struct thread_info *)(inf))
42 void
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;
49 else
50 list->head = new_inferior;
51 list->tail = new_inferior;
54 void
55 for_each_inferior (struct inferior_list *list,
56 void (*action) (struct inferior_list_entry *))
58 struct inferior_list_entry *cur = list->head, *next;
60 while (cur != NULL)
62 next = cur->next;
63 (*action) (cur);
64 cur = next;
68 void
69 change_inferior_id (struct inferior_list *list,
70 unsigned long new_id)
72 if (list->head != list->tail)
73 error ("tried to change thread ID after multiple threads are created");
75 list->head->id = new_id;
78 void
79 remove_inferior (struct inferior_list *list,
80 struct inferior_list_entry *entry)
82 struct inferior_list_entry **cur;
84 if (list->head == entry)
86 list->head = entry->next;
87 if (list->tail == entry)
88 list->tail = list->head;
89 return;
92 cur = &list->head;
93 while (*cur && (*cur)->next != entry)
94 cur = &(*cur)->next;
96 if (*cur == NULL)
97 return;
99 (*cur)->next = entry->next;
101 if (list->tail == entry)
102 list->tail = *cur;
105 void
106 add_thread (unsigned long thread_id, void *target_data, unsigned int gdb_id)
108 struct thread_info *new_thread
109 = (struct thread_info *) malloc (sizeof (*new_thread));
111 memset (new_thread, 0, sizeof (*new_thread));
113 new_thread->entry.id = thread_id;
115 add_inferior_to_list (&all_threads, & new_thread->entry);
117 if (current_inferior == NULL)
118 current_inferior = new_thread;
120 new_thread->target_data = target_data;
121 set_inferior_regcache_data (new_thread, new_register_cache ());
122 new_thread->gdb_id = gdb_id;
125 unsigned int
126 thread_id_to_gdb_id (unsigned long thread_id)
128 struct inferior_list_entry *inf = all_threads.head;
130 while (inf != NULL)
132 struct thread_info *thread = get_thread (inf);
133 if (inf->id == thread_id)
134 return thread->gdb_id;
135 inf = inf->next;
138 return 0;
141 unsigned int
142 thread_to_gdb_id (struct thread_info *thread)
144 return thread->gdb_id;
147 unsigned long
148 gdb_id_to_thread_id (unsigned int gdb_id)
150 struct inferior_list_entry *inf = all_threads.head;
152 while (inf != NULL)
154 struct thread_info *thread = get_thread (inf);
155 if (thread->gdb_id == gdb_id)
156 return inf->id;
157 inf = inf->next;
160 return 0;
163 static void
164 free_one_thread (struct inferior_list_entry *inf)
166 struct thread_info *thread = get_thread (inf);
167 free_register_cache (inferior_regcache_data (thread));
168 free (thread);
171 void
172 remove_thread (struct thread_info *thread)
174 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
175 free_one_thread (&thread->entry);
178 void
179 clear_inferiors (void)
181 for_each_inferior (&all_threads, free_one_thread);
183 all_threads.head = all_threads.tail = NULL;
186 struct inferior_list_entry *
187 find_inferior (struct inferior_list *list,
188 int (*func) (struct inferior_list_entry *, void *), void *arg)
190 struct inferior_list_entry *inf = list->head;
192 while (inf != NULL)
194 if ((*func) (inf, arg))
195 return inf;
196 inf = inf->next;
199 return NULL;
202 struct inferior_list_entry *
203 find_inferior_id (struct inferior_list *list, unsigned long id)
205 struct inferior_list_entry *inf = list->head;
207 while (inf != NULL)
209 if (inf->id == id)
210 return inf;
211 inf = inf->next;
214 return NULL;
217 void *
218 inferior_target_data (struct thread_info *inferior)
220 return inferior->target_data;
223 void
224 set_inferior_target_data (struct thread_info *inferior, void *data)
226 inferior->target_data = data;
229 void *
230 inferior_regcache_data (struct thread_info *inferior)
232 return inferior->regcache_data;
235 void
236 set_inferior_regcache_data (struct thread_info *inferior, void *data)
238 inferior->regcache_data = data;