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