]>
Commit | Line | Data |
---|---|---|
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 | 27 | struct 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 |
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 | } | |
ce3a066d DJ |
66 | |
67 | void | |
0d62e5e8 | 68 | change_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 |
77 | void |
78 | remove_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 | ||
104 | void | |
a06660f7 | 105 | add_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 | ||
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 | ||
dae5f5cf DJ |
146 | struct thread_info * |
147 | gdb_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 | ||
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; | |
0d62e5e8 | 168 | } |
c04a1aa8 | 169 | |
0d62e5e8 DJ |
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); | |
ce3a066d DJ |
183 | } |
184 | ||
185 | void | |
186 | clear_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 | ||
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; | |
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 | 209 | struct inferior_list_entry * |
a1928bad | 210 | find_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 | |
224 | void * | |
0d62e5e8 | 225 | inferior_target_data (struct thread_info *inferior) |
611cb4a5 DJ |
226 | { |
227 | return inferior->target_data; | |
228 | } | |
229 | ||
230 | void | |
0d62e5e8 | 231 | set_inferior_target_data (struct thread_info *inferior, void *data) |
611cb4a5 DJ |
232 | { |
233 | inferior->target_data = data; | |
234 | } | |
c04a1aa8 DJ |
235 | |
236 | void * | |
0d62e5e8 | 237 | inferior_regcache_data (struct thread_info *inferior) |
c04a1aa8 DJ |
238 | { |
239 | return inferior->regcache_data; | |
240 | } | |
241 | ||
242 | void | |
0d62e5e8 | 243 | set_inferior_regcache_data (struct thread_info *inferior, void *data) |
c04a1aa8 DJ |
244 | { |
245 | inferior->regcache_data = data; | |
246 | } |