]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/inferiors.c
Copyright updates for 2007.
[thirdparty/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
DJ
35struct inferior_list all_threads;
36
37struct thread_info *current_inferior;
38
39#define get_thread(inf) ((struct thread_info *)(inf))
40
41void
42add_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
53void
54for_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}
ce3a066d
DJ
66
67void
0d62e5e8 68change_inferior_id (struct inferior_list *list,
a1928bad 69 unsigned long new_id)
ce3a066d 70{
0d62e5e8
DJ
71 if (list->head != list->tail)
72 error ("tried to change thread ID after multiple threads are created");
ce3a066d 73
0d62e5e8
DJ
74 list->head->id = new_id;
75}
ce3a066d 76
0d62e5e8
DJ
77void
78remove_inferior (struct inferior_list *list,
79 struct inferior_list_entry *entry)
80{
81 struct inferior_list_entry **cur;
ce3a066d 82
0d62e5e8
DJ
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;
ce3a066d 97
0d62e5e8
DJ
98 (*cur)->next = entry->next;
99
100 if (list->tail == entry)
101 list->tail = *cur;
102}
103
104void
a06660f7 105add_thread (unsigned long thread_id, void *target_data, unsigned int gdb_id)
0d62e5e8
DJ
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
ce3a066d 116 if (current_inferior == NULL)
0d62e5e8 117 current_inferior = new_thread;
ce3a066d 118
0d62e5e8
DJ
119 new_thread->target_data = target_data;
120 set_inferior_regcache_data (new_thread, new_register_cache ());
a06660f7
DJ
121 new_thread->gdb_id = gdb_id;
122}
123
124unsigned int
125thread_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
140unsigned int
141thread_to_gdb_id (struct thread_info *thread)
142{
143 return thread->gdb_id;
144}
145
dae5f5cf
DJ
146struct thread_info *
147gdb_id_to_thread (unsigned int gdb_id)
a06660f7
DJ
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)
dae5f5cf 155 return thread;
a06660f7
DJ
156 inf = inf->next;
157 }
158
dae5f5cf
DJ
159 return NULL;
160}
161
162unsigned long
163gdb_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;
0d62e5e8 168}
c04a1aa8 169
0d62e5e8
DJ
170static void
171free_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
178void
179remove_thread (struct thread_info *thread)
180{
181 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
182 free_one_thread (&thread->entry);
ce3a066d
DJ
183}
184
185void
186clear_inferiors (void)
187{
0d62e5e8
DJ
188 for_each_inferior (&all_threads, free_one_thread);
189
190 all_threads.head = all_threads.tail = NULL;
191}
192
193struct inferior_list_entry *
194find_inferior (struct inferior_list *list,
195 int (*func) (struct inferior_list_entry *, void *), void *arg)
196{
197 struct inferior_list_entry *inf = list->head;
ce3a066d 198
0d62e5e8 199 while (inf != NULL)
ce3a066d 200 {
0d62e5e8
DJ
201 if ((*func) (inf, arg))
202 return inf;
203 inf = inf->next;
204 }
611cb4a5 205
0d62e5e8
DJ
206 return NULL;
207}
611cb4a5 208
0d62e5e8 209struct inferior_list_entry *
a1928bad 210find_inferior_id (struct inferior_list *list, unsigned long id)
0d62e5e8
DJ
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;
ce3a066d
DJ
219 }
220
0d62e5e8 221 return NULL;
ce3a066d 222}
611cb4a5
DJ
223
224void *
0d62e5e8 225inferior_target_data (struct thread_info *inferior)
611cb4a5
DJ
226{
227 return inferior->target_data;
228}
229
230void
0d62e5e8 231set_inferior_target_data (struct thread_info *inferior, void *data)
611cb4a5
DJ
232{
233 inferior->target_data = data;
234}
c04a1aa8
DJ
235
236void *
0d62e5e8 237inferior_regcache_data (struct thread_info *inferior)
c04a1aa8
DJ
238{
239 return inferior->regcache_data;
240}
241
242void
0d62e5e8 243set_inferior_regcache_data (struct thread_info *inferior, void *data)
c04a1aa8
DJ
244{
245 inferior->regcache_data = data;
246}