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