Teach linux gdbserver to step-over-breakpoints.
[deliverable/binutils-gdb.git] / gdb / gdbserver / inferiors.c
1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002, 2005, 2007, 2008, 2009, 2010
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 3 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, see <http://www.gnu.org/licenses/>. */
21
22 #include <stdlib.h>
23
24 #include "server.h"
25
26 struct inferior_list all_processes;
27 struct inferior_list all_threads;
28 struct inferior_list all_dlls;
29 int dlls_changed;
30
31 struct thread_info *current_inferior;
32
33
34 /* Oft used ptids */
35 ptid_t null_ptid;
36 ptid_t minus_one_ptid;
37
38 /* Create a ptid given the necessary PID, LWP, and TID components. */
39
40 ptid_t
41 ptid_build (int pid, long lwp, long tid)
42 {
43 ptid_t ptid;
44
45 ptid.pid = pid;
46 ptid.lwp = lwp;
47 ptid.tid = tid;
48 return ptid;
49 }
50
51 /* Create a ptid from just a pid. */
52
53 ptid_t
54 pid_to_ptid (int pid)
55 {
56 return ptid_build (pid, 0, 0);
57 }
58
59 /* Fetch the pid (process id) component from a ptid. */
60
61 int
62 ptid_get_pid (ptid_t ptid)
63 {
64 return ptid.pid;
65 }
66
67 /* Fetch the lwp (lightweight process) component from a ptid. */
68
69 long
70 ptid_get_lwp (ptid_t ptid)
71 {
72 return ptid.lwp;
73 }
74
75 /* Fetch the tid (thread id) component from a ptid. */
76
77 long
78 ptid_get_tid (ptid_t ptid)
79 {
80 return ptid.tid;
81 }
82
83 /* ptid_equal() is used to test equality of two ptids. */
84
85 int
86 ptid_equal (ptid_t ptid1, ptid_t ptid2)
87 {
88 return (ptid1.pid == ptid2.pid
89 && ptid1.lwp == ptid2.lwp
90 && ptid1.tid == ptid2.tid);
91 }
92
93 /* Return true if this ptid represents a process. */
94
95 int
96 ptid_is_pid (ptid_t ptid)
97 {
98 if (ptid_equal (minus_one_ptid, ptid))
99 return 0;
100 if (ptid_equal (null_ptid, ptid))
101 return 0;
102
103 return (ptid_get_pid (ptid) != 0
104 && ptid_get_lwp (ptid) == 0
105 && ptid_get_tid (ptid) == 0);
106 }
107
108 #define get_thread(inf) ((struct thread_info *)(inf))
109 #define get_dll(inf) ((struct dll_info *)(inf))
110
111 void
112 add_inferior_to_list (struct inferior_list *list,
113 struct inferior_list_entry *new_inferior)
114 {
115 new_inferior->next = NULL;
116 if (list->tail != NULL)
117 list->tail->next = new_inferior;
118 else
119 list->head = new_inferior;
120 list->tail = new_inferior;
121 }
122
123 /* Invoke ACTION for each inferior in LIST. */
124
125 void
126 for_each_inferior (struct inferior_list *list,
127 void (*action) (struct inferior_list_entry *))
128 {
129 struct inferior_list_entry *cur = list->head, *next;
130
131 while (cur != NULL)
132 {
133 next = cur->next;
134 (*action) (cur);
135 cur = next;
136 }
137 }
138
139 void
140 remove_inferior (struct inferior_list *list,
141 struct inferior_list_entry *entry)
142 {
143 struct inferior_list_entry **cur;
144
145 if (list->head == entry)
146 {
147 list->head = entry->next;
148 if (list->tail == entry)
149 list->tail = list->head;
150 return;
151 }
152
153 cur = &list->head;
154 while (*cur && (*cur)->next != entry)
155 cur = &(*cur)->next;
156
157 if (*cur == NULL)
158 return;
159
160 (*cur)->next = entry->next;
161
162 if (list->tail == entry)
163 list->tail = *cur;
164 }
165
166 void
167 add_thread (ptid_t thread_id, void *target_data)
168 {
169 struct thread_info *new_thread = xmalloc (sizeof (*new_thread));
170
171 memset (new_thread, 0, sizeof (*new_thread));
172
173 new_thread->entry.id = thread_id;
174
175 add_inferior_to_list (&all_threads, & new_thread->entry);
176
177 if (current_inferior == NULL)
178 current_inferior = new_thread;
179
180 new_thread->target_data = target_data;
181 set_inferior_regcache_data (new_thread, new_register_cache ());
182 }
183
184 ptid_t
185 thread_id_to_gdb_id (ptid_t thread_id)
186 {
187 struct inferior_list_entry *inf = all_threads.head;
188
189 while (inf != NULL)
190 {
191 if (ptid_equal (inf->id, thread_id))
192 return thread_id;
193 inf = inf->next;
194 }
195
196 return null_ptid;
197 }
198
199 ptid_t
200 thread_to_gdb_id (struct thread_info *thread)
201 {
202 return thread->entry.id;
203 }
204
205 struct thread_info *
206 find_thread_ptid (ptid_t ptid)
207 {
208 struct inferior_list_entry *inf = all_threads.head;
209
210 while (inf != NULL)
211 {
212 struct thread_info *thread = get_thread (inf);
213 if (ptid_equal (thread->entry.id, ptid))
214 return thread;
215 inf = inf->next;
216 }
217
218 return NULL;
219 }
220
221 ptid_t
222 gdb_id_to_thread_id (ptid_t gdb_id)
223 {
224 struct thread_info *thread = find_thread_ptid (gdb_id);
225
226 return thread ? thread->entry.id : null_ptid;
227 }
228
229 static void
230 free_one_thread (struct inferior_list_entry *inf)
231 {
232 struct thread_info *thread = get_thread (inf);
233 free_register_cache (inferior_regcache_data (thread));
234 free (thread);
235 }
236
237 void
238 remove_thread (struct thread_info *thread)
239 {
240 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
241 free_one_thread (&thread->entry);
242 }
243
244 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
245 returns non-zero. If no entry is found then return NULL. */
246
247 struct inferior_list_entry *
248 find_inferior (struct inferior_list *list,
249 int (*func) (struct inferior_list_entry *, void *), void *arg)
250 {
251 struct inferior_list_entry *inf = list->head;
252
253 while (inf != NULL)
254 {
255 struct inferior_list_entry *next;
256
257 next = inf->next;
258 if ((*func) (inf, arg))
259 return inf;
260 inf = next;
261 }
262
263 return NULL;
264 }
265
266 struct inferior_list_entry *
267 find_inferior_id (struct inferior_list *list, ptid_t id)
268 {
269 struct inferior_list_entry *inf = list->head;
270
271 while (inf != NULL)
272 {
273 if (ptid_equal (inf->id, id))
274 return inf;
275 inf = inf->next;
276 }
277
278 return NULL;
279 }
280
281 void *
282 inferior_target_data (struct thread_info *inferior)
283 {
284 return inferior->target_data;
285 }
286
287 void
288 set_inferior_target_data (struct thread_info *inferior, void *data)
289 {
290 inferior->target_data = data;
291 }
292
293 void *
294 inferior_regcache_data (struct thread_info *inferior)
295 {
296 return inferior->regcache_data;
297 }
298
299 void
300 set_inferior_regcache_data (struct thread_info *inferior, void *data)
301 {
302 inferior->regcache_data = data;
303 }
304
305 static void
306 free_one_dll (struct inferior_list_entry *inf)
307 {
308 struct dll_info *dll = get_dll (inf);
309 if (dll->name != NULL)
310 free (dll->name);
311 free (dll);
312 }
313
314 /* Find a DLL with the same name and/or base address. A NULL name in
315 the key is ignored; so is an all-ones base address. */
316
317 static int
318 match_dll (struct inferior_list_entry *inf, void *arg)
319 {
320 struct dll_info *iter = (void *) inf;
321 struct dll_info *key = arg;
322
323 if (key->base_addr != ~(CORE_ADDR) 0
324 && iter->base_addr == key->base_addr)
325 return 1;
326 else if (key->name != NULL
327 && iter->name != NULL
328 && strcmp (key->name, iter->name) == 0)
329 return 1;
330
331 return 0;
332 }
333
334 /* Record a newly loaded DLL at BASE_ADDR. */
335
336 void
337 loaded_dll (const char *name, CORE_ADDR base_addr)
338 {
339 struct dll_info *new_dll = xmalloc (sizeof (*new_dll));
340 memset (new_dll, 0, sizeof (*new_dll));
341
342 new_dll->entry.id = minus_one_ptid;
343
344 new_dll->name = xstrdup (name);
345 new_dll->base_addr = base_addr;
346
347 add_inferior_to_list (&all_dlls, &new_dll->entry);
348 dlls_changed = 1;
349 }
350
351 /* Record that the DLL with NAME and BASE_ADDR has been unloaded. */
352
353 void
354 unloaded_dll (const char *name, CORE_ADDR base_addr)
355 {
356 struct dll_info *dll;
357 struct dll_info key_dll;
358
359 /* Be careful not to put the key DLL in any list. */
360 key_dll.name = (char *) name;
361 key_dll.base_addr = base_addr;
362
363 dll = (void *) find_inferior (&all_dlls, match_dll, &key_dll);
364
365 if (dll == NULL)
366 /* For some inferiors we might get unloaded_dll events without having
367 a corresponding loaded_dll. In that case, the dll cannot be found
368 in ALL_DLL, and there is nothing further for us to do.
369
370 This has been observed when running 32bit executables on Windows64
371 (i.e. through WOW64, the interface between the 32bits and 64bits
372 worlds). In that case, the inferior always does some strange
373 unloading of unnamed dll. */
374 return;
375 else
376 {
377 /* DLL has been found so remove the entry and free associated
378 resources. */
379 remove_inferior (&all_dlls, &dll->entry);
380 free_one_dll (&dll->entry);
381 dlls_changed = 1;
382 }
383 }
384
385 #define clear_list(LIST) \
386 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
387
388 void
389 clear_inferiors (void)
390 {
391 for_each_inferior (&all_threads, free_one_thread);
392 for_each_inferior (&all_dlls, free_one_dll);
393
394 clear_list (&all_threads);
395 clear_list (&all_dlls);
396
397 current_inferior = NULL;
398 }
399
400 /* Two utility functions for a truly degenerate inferior_list: a simple
401 PID listing. */
402
403 void
404 add_pid_to_list (struct inferior_list *list, unsigned long pid)
405 {
406 struct inferior_list_entry *new_entry;
407
408 new_entry = xmalloc (sizeof (struct inferior_list_entry));
409 new_entry->id = pid_to_ptid (pid);
410 add_inferior_to_list (list, new_entry);
411 }
412
413 int
414 pull_pid_from_list (struct inferior_list *list, unsigned long pid)
415 {
416 struct inferior_list_entry *new_entry;
417
418 new_entry = find_inferior_id (list, pid_to_ptid (pid));
419 if (new_entry == NULL)
420 return 0;
421 else
422 {
423 remove_inferior (list, new_entry);
424 free (new_entry);
425 return 1;
426 }
427 }
428
429 struct process_info *
430 add_process (int pid, int attached)
431 {
432 struct process_info *process;
433
434 process = xcalloc (1, sizeof (*process));
435
436 process->head.id = pid_to_ptid (pid);
437 process->attached = attached;
438
439 add_inferior_to_list (&all_processes, &process->head);
440
441 return process;
442 }
443
444 /* Remove a process from the common process list and free the memory
445 allocated for it.
446 The caller is responsible for freeing private data first. */
447
448 void
449 remove_process (struct process_info *process)
450 {
451 clear_symbol_cache (&process->symbol_cache);
452 free_all_breakpoints (process);
453 remove_inferior (&all_processes, &process->head);
454 free (process);
455 }
456
457 struct process_info *
458 find_process_pid (int pid)
459 {
460 return (struct process_info *)
461 find_inferior_id (&all_processes, pid_to_ptid (pid));
462 }
463
464 /* Return non-zero if INF, a struct process_info, was started by us,
465 i.e. not attached to. */
466
467 static int
468 started_inferior_callback (struct inferior_list_entry *entry, void *args)
469 {
470 struct process_info *process = (struct process_info *) entry;
471
472 return ! process->attached;
473 }
474
475 /* Return non-zero if there are any inferiors that we have created
476 (as opposed to attached-to). */
477
478 int
479 have_started_inferiors_p (void)
480 {
481 return (find_inferior (&all_processes, started_inferior_callback, NULL)
482 != NULL);
483 }
484
485 /* Return non-zero if INF, a struct process_info, was attached to. */
486
487 static int
488 attached_inferior_callback (struct inferior_list_entry *entry, void *args)
489 {
490 struct process_info *process = (struct process_info *) entry;
491
492 return process->attached;
493 }
494
495 /* Return non-zero if there are any inferiors that we have attached to. */
496
497 int
498 have_attached_inferiors_p (void)
499 {
500 return (find_inferior (&all_processes, attached_inferior_callback, NULL)
501 != NULL);
502 }
503
504 struct process_info *
505 get_thread_process (struct thread_info *thread)
506 {
507 int pid = ptid_get_pid (thread->entry.id);
508 return find_process_pid (pid);
509 }
510
511 struct process_info *
512 current_process (void)
513 {
514 if (current_inferior == NULL)
515 fatal ("Current inferior requested, but current_inferior is NULL\n");
516
517 return get_thread_process (current_inferior);
518 }
519
520 void
521 initialize_inferiors (void)
522 {
523 null_ptid = ptid_build (0, 0, 0);
524 minus_one_ptid = ptid_build (-1, 0, 0);
525 }
This page took 0.039743 seconds and 4 git commands to generate.