delete thread_id_to_gdb_id, unused
[deliverable/binutils-gdb.git] / gdb / gdbserver / inferiors.c
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
20
21 #include <stdlib.h>
22
23 #include "server.h"
24 #include "gdbthread.h"
25 #include "dll.h"
26
27 struct inferior_list all_processes;
28 struct inferior_list all_threads;
29
30 struct thread_info *current_inferior;
31
32 #define get_thread(inf) ((struct thread_info *)(inf))
33
34 void
35 add_inferior_to_list (struct inferior_list *list,
36 struct inferior_list_entry *new_inferior)
37 {
38 new_inferior->next = NULL;
39 if (list->tail != NULL)
40 list->tail->next = new_inferior;
41 else
42 list->head = new_inferior;
43 list->tail = new_inferior;
44 }
45
46 /* Invoke ACTION for each inferior in LIST. */
47
48 void
49 for_each_inferior (struct inferior_list *list,
50 void (*action) (struct inferior_list_entry *))
51 {
52 struct inferior_list_entry *cur = list->head, *next;
53
54 while (cur != NULL)
55 {
56 next = cur->next;
57 (*action) (cur);
58 cur = next;
59 }
60 }
61
62 void
63 remove_inferior (struct inferior_list *list,
64 struct inferior_list_entry *entry)
65 {
66 struct inferior_list_entry **cur;
67
68 if (list->head == entry)
69 {
70 list->head = entry->next;
71 if (list->tail == entry)
72 list->tail = list->head;
73 return;
74 }
75
76 cur = &list->head;
77 while (*cur && (*cur)->next != entry)
78 cur = &(*cur)->next;
79
80 if (*cur == NULL)
81 return;
82
83 (*cur)->next = entry->next;
84
85 if (list->tail == entry)
86 list->tail = *cur;
87 }
88
89 void
90 add_thread (ptid_t thread_id, void *target_data)
91 {
92 struct thread_info *new_thread = xmalloc (sizeof (*new_thread));
93
94 memset (new_thread, 0, sizeof (*new_thread));
95
96 new_thread->entry.id = thread_id;
97 new_thread->last_resume_kind = resume_continue;
98 new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
99
100 add_inferior_to_list (&all_threads, & new_thread->entry);
101
102 if (current_inferior == NULL)
103 current_inferior = new_thread;
104
105 new_thread->target_data = target_data;
106 }
107
108 ptid_t
109 thread_to_gdb_id (struct thread_info *thread)
110 {
111 return thread->entry.id;
112 }
113
114 struct thread_info *
115 find_thread_ptid (ptid_t ptid)
116 {
117 struct inferior_list_entry *inf = all_threads.head;
118
119 while (inf != NULL)
120 {
121 struct thread_info *thread = get_thread (inf);
122 if (ptid_equal (thread->entry.id, ptid))
123 return thread;
124 inf = inf->next;
125 }
126
127 return NULL;
128 }
129
130 ptid_t
131 gdb_id_to_thread_id (ptid_t gdb_id)
132 {
133 struct thread_info *thread = find_thread_ptid (gdb_id);
134
135 return thread ? thread->entry.id : null_ptid;
136 }
137
138 static void
139 free_one_thread (struct inferior_list_entry *inf)
140 {
141 struct thread_info *thread = get_thread (inf);
142 free_register_cache (inferior_regcache_data (thread));
143 free (thread);
144 }
145
146 void
147 remove_thread (struct thread_info *thread)
148 {
149 if (thread->btrace != NULL)
150 target_disable_btrace (thread->btrace);
151
152 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
153 free_one_thread (&thread->entry);
154 }
155
156 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
157 returns non-zero. If no entry is found then return NULL. */
158
159 struct inferior_list_entry *
160 find_inferior (struct inferior_list *list,
161 int (*func) (struct inferior_list_entry *, void *), void *arg)
162 {
163 struct inferior_list_entry *inf = list->head;
164
165 while (inf != NULL)
166 {
167 struct inferior_list_entry *next;
168
169 next = inf->next;
170 if ((*func) (inf, arg))
171 return inf;
172 inf = next;
173 }
174
175 return NULL;
176 }
177
178 struct inferior_list_entry *
179 find_inferior_id (struct inferior_list *list, ptid_t id)
180 {
181 struct inferior_list_entry *inf = list->head;
182
183 while (inf != NULL)
184 {
185 if (ptid_equal (inf->id, id))
186 return inf;
187 inf = inf->next;
188 }
189
190 return NULL;
191 }
192
193 void *
194 inferior_target_data (struct thread_info *inferior)
195 {
196 return inferior->target_data;
197 }
198
199 void
200 set_inferior_target_data (struct thread_info *inferior, void *data)
201 {
202 inferior->target_data = data;
203 }
204
205 void *
206 inferior_regcache_data (struct thread_info *inferior)
207 {
208 return inferior->regcache_data;
209 }
210
211 void
212 set_inferior_regcache_data (struct thread_info *inferior, void *data)
213 {
214 inferior->regcache_data = data;
215 }
216
217 #define clear_list(LIST) \
218 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
219
220 void
221 clear_inferiors (void)
222 {
223 for_each_inferior (&all_threads, free_one_thread);
224 clear_list (&all_threads);
225
226 clear_dlls ();
227
228 current_inferior = NULL;
229 }
230
231 struct process_info *
232 add_process (int pid, int attached)
233 {
234 struct process_info *process;
235
236 process = xcalloc (1, sizeof (*process));
237
238 process->head.id = pid_to_ptid (pid);
239 process->attached = attached;
240
241 add_inferior_to_list (&all_processes, &process->head);
242
243 return process;
244 }
245
246 /* Remove a process from the common process list and free the memory
247 allocated for it.
248 The caller is responsible for freeing private data first. */
249
250 void
251 remove_process (struct process_info *process)
252 {
253 clear_symbol_cache (&process->symbol_cache);
254 free_all_breakpoints (process);
255 remove_inferior (&all_processes, &process->head);
256 free (process);
257 }
258
259 struct process_info *
260 find_process_pid (int pid)
261 {
262 return (struct process_info *)
263 find_inferior_id (&all_processes, pid_to_ptid (pid));
264 }
265
266 /* Return non-zero if INF, a struct process_info, was started by us,
267 i.e. not attached to. */
268
269 static int
270 started_inferior_callback (struct inferior_list_entry *entry, void *args)
271 {
272 struct process_info *process = (struct process_info *) entry;
273
274 return ! process->attached;
275 }
276
277 /* Return non-zero if there are any inferiors that we have created
278 (as opposed to attached-to). */
279
280 int
281 have_started_inferiors_p (void)
282 {
283 return (find_inferior (&all_processes, started_inferior_callback, NULL)
284 != NULL);
285 }
286
287 /* Return non-zero if INF, a struct process_info, was attached to. */
288
289 static int
290 attached_inferior_callback (struct inferior_list_entry *entry, void *args)
291 {
292 struct process_info *process = (struct process_info *) entry;
293
294 return process->attached;
295 }
296
297 /* Return non-zero if there are any inferiors that we have attached to. */
298
299 int
300 have_attached_inferiors_p (void)
301 {
302 return (find_inferior (&all_processes, attached_inferior_callback, NULL)
303 != NULL);
304 }
305
306 struct process_info *
307 get_thread_process (struct thread_info *thread)
308 {
309 int pid = ptid_get_pid (thread->entry.id);
310 return find_process_pid (pid);
311 }
312
313 struct process_info *
314 current_process (void)
315 {
316 if (current_inferior == NULL)
317 fatal ("Current inferior requested, but current_inferior is NULL\n");
318
319 return get_thread_process (current_inferior);
320 }
This page took 0.038424 seconds and 5 git commands to generate.