1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by MontaVista Software.
6 This file is part of GDB.
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.
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.
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/>. */
27 struct inferior_list_entry entry
;
33 struct inferior_list all_processes
;
34 struct inferior_list all_threads
;
35 struct inferior_list all_dlls
;
38 struct thread_info
*current_inferior
;
43 ptid_t minus_one_ptid
;
45 /* Create a ptid given the necessary PID, LWP, and TID components. */
48 ptid_build (int pid
, long lwp
, long tid
)
58 /* Create a ptid from just a pid. */
63 return ptid_build (pid
, 0, 0);
66 /* Fetch the pid (process id) component from a ptid. */
69 ptid_get_pid (ptid_t ptid
)
74 /* Fetch the lwp (lightweight process) component from a ptid. */
77 ptid_get_lwp (ptid_t ptid
)
82 /* Fetch the tid (thread id) component from a ptid. */
85 ptid_get_tid (ptid_t ptid
)
90 /* ptid_equal() is used to test equality of two ptids. */
93 ptid_equal (ptid_t ptid1
, ptid_t ptid2
)
95 return (ptid1
.pid
== ptid2
.pid
96 && ptid1
.lwp
== ptid2
.lwp
97 && ptid1
.tid
== ptid2
.tid
);
100 /* Return true if this ptid represents a process. */
103 ptid_is_pid (ptid_t ptid
)
105 if (ptid_equal (minus_one_ptid
, ptid
))
107 if (ptid_equal (null_ptid
, ptid
))
110 return (ptid_get_pid (ptid
) != 0
111 && ptid_get_lwp (ptid
) == 0
112 && ptid_get_tid (ptid
) == 0);
115 #define get_thread(inf) ((struct thread_info *)(inf))
116 #define get_dll(inf) ((struct dll_info *)(inf))
119 add_inferior_to_list (struct inferior_list
*list
,
120 struct inferior_list_entry
*new_inferior
)
122 new_inferior
->next
= NULL
;
123 if (list
->tail
!= NULL
)
124 list
->tail
->next
= new_inferior
;
126 list
->head
= new_inferior
;
127 list
->tail
= new_inferior
;
130 /* Invoke ACTION for each inferior in LIST. */
133 for_each_inferior (struct inferior_list
*list
,
134 void (*action
) (struct inferior_list_entry
*))
136 struct inferior_list_entry
*cur
= list
->head
, *next
;
147 remove_inferior (struct inferior_list
*list
,
148 struct inferior_list_entry
*entry
)
150 struct inferior_list_entry
**cur
;
152 if (list
->head
== entry
)
154 list
->head
= entry
->next
;
155 if (list
->tail
== entry
)
156 list
->tail
= list
->head
;
161 while (*cur
&& (*cur
)->next
!= entry
)
167 (*cur
)->next
= entry
->next
;
169 if (list
->tail
== entry
)
174 add_thread (ptid_t thread_id
, void *target_data
)
176 struct thread_info
*new_thread
= xmalloc (sizeof (*new_thread
));
178 memset (new_thread
, 0, sizeof (*new_thread
));
180 new_thread
->entry
.id
= thread_id
;
182 add_inferior_to_list (&all_threads
, & new_thread
->entry
);
184 if (current_inferior
== NULL
)
185 current_inferior
= new_thread
;
187 new_thread
->target_data
= target_data
;
188 set_inferior_regcache_data (new_thread
, new_register_cache ());
192 thread_id_to_gdb_id (ptid_t thread_id
)
194 struct inferior_list_entry
*inf
= all_threads
.head
;
198 if (ptid_equal (inf
->id
, thread_id
))
207 thread_to_gdb_id (struct thread_info
*thread
)
209 return thread
->entry
.id
;
213 find_thread_ptid (ptid_t ptid
)
215 struct inferior_list_entry
*inf
= all_threads
.head
;
219 struct thread_info
*thread
= get_thread (inf
);
220 if (ptid_equal (thread
->entry
.id
, ptid
))
229 gdb_id_to_thread_id (ptid_t gdb_id
)
231 struct thread_info
*thread
= find_thread_ptid (gdb_id
);
233 return thread
? thread
->entry
.id
: null_ptid
;
237 free_one_thread (struct inferior_list_entry
*inf
)
239 struct thread_info
*thread
= get_thread (inf
);
240 free_register_cache (inferior_regcache_data (thread
));
245 remove_thread (struct thread_info
*thread
)
247 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
248 free_one_thread (&thread
->entry
);
251 struct inferior_list_entry
*
252 find_inferior (struct inferior_list
*list
,
253 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
255 struct inferior_list_entry
*inf
= list
->head
;
259 struct inferior_list_entry
*next
;
262 if ((*func
) (inf
, arg
))
270 struct inferior_list_entry
*
271 find_inferior_id (struct inferior_list
*list
, ptid_t id
)
273 struct inferior_list_entry
*inf
= list
->head
;
277 if (ptid_equal (inf
->id
, id
))
286 inferior_target_data (struct thread_info
*inferior
)
288 return inferior
->target_data
;
292 set_inferior_target_data (struct thread_info
*inferior
, void *data
)
294 inferior
->target_data
= data
;
298 inferior_regcache_data (struct thread_info
*inferior
)
300 return inferior
->regcache_data
;
304 set_inferior_regcache_data (struct thread_info
*inferior
, void *data
)
306 inferior
->regcache_data
= data
;
310 free_one_dll (struct inferior_list_entry
*inf
)
312 struct dll_info
*dll
= get_dll (inf
);
313 if (dll
->name
!= NULL
)
318 /* Find a DLL with the same name and/or base address. A NULL name in
319 the key is ignored; so is an all-ones base address. */
322 match_dll (struct inferior_list_entry
*inf
, void *arg
)
324 struct dll_info
*iter
= (void *) inf
;
325 struct dll_info
*key
= arg
;
327 if (key
->base_addr
!= ~(CORE_ADDR
) 0
328 && iter
->base_addr
== key
->base_addr
)
330 else if (key
->name
!= NULL
331 && iter
->name
!= NULL
332 && strcmp (key
->name
, iter
->name
) == 0)
338 /* Record a newly loaded DLL at BASE_ADDR. */
341 loaded_dll (const char *name
, CORE_ADDR base_addr
)
343 struct dll_info
*new_dll
= xmalloc (sizeof (*new_dll
));
344 memset (new_dll
, 0, sizeof (*new_dll
));
346 new_dll
->entry
.id
= minus_one_ptid
;
348 new_dll
->name
= xstrdup (name
);
349 new_dll
->base_addr
= base_addr
;
351 add_inferior_to_list (&all_dlls
, &new_dll
->entry
);
355 /* Record that the DLL with NAME and BASE_ADDR has been unloaded. */
358 unloaded_dll (const char *name
, CORE_ADDR base_addr
)
360 struct dll_info
*dll
;
361 struct dll_info key_dll
;
363 /* Be careful not to put the key DLL in any list. */
364 key_dll
.name
= (char *) name
;
365 key_dll
.base_addr
= base_addr
;
367 dll
= (void *) find_inferior (&all_dlls
, match_dll
, &key_dll
);
368 remove_inferior (&all_dlls
, &dll
->entry
);
369 free_one_dll (&dll
->entry
);
373 #define clear_list(LIST) \
374 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
377 clear_inferiors (void)
379 for_each_inferior (&all_threads
, free_one_thread
);
380 for_each_inferior (&all_dlls
, free_one_dll
);
382 clear_list (&all_threads
);
383 clear_list (&all_dlls
);
385 current_inferior
= NULL
;
388 /* Two utility functions for a truly degenerate inferior_list: a simple
392 add_pid_to_list (struct inferior_list
*list
, unsigned long pid
)
394 struct inferior_list_entry
*new_entry
;
396 new_entry
= xmalloc (sizeof (struct inferior_list_entry
));
397 new_entry
->id
= pid_to_ptid (pid
);
398 add_inferior_to_list (list
, new_entry
);
402 pull_pid_from_list (struct inferior_list
*list
, unsigned long pid
)
404 struct inferior_list_entry
*new_entry
;
406 new_entry
= find_inferior_id (list
, pid_to_ptid (pid
));
407 if (new_entry
== NULL
)
411 remove_inferior (list
, new_entry
);
417 struct process_info
*
418 add_process (int pid
, int attached
)
420 struct process_info
*process
;
422 process
= xcalloc (1, sizeof (*process
));
424 process
->head
.id
= pid_to_ptid (pid
);
425 process
->attached
= attached
;
427 add_inferior_to_list (&all_processes
, &process
->head
);
432 /* Remove a process from the common process list and free the memory
434 The caller is responsible for freeing private data first. */
437 remove_process (struct process_info
*process
)
439 clear_symbol_cache (&process
->symbol_cache
);
440 free_all_breakpoints (process
);
441 remove_inferior (&all_processes
, &process
->head
);
445 struct process_info
*
446 find_process_pid (int pid
)
448 return (struct process_info
*)
449 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
452 /* Return non-zero if INF, a struct process_info, was started by us,
453 i.e. not attached to. */
456 started_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
458 struct process_info
*process
= (struct process_info
*) entry
;
460 return ! process
->attached
;
463 /* Return non-zero if there are any inferiors that we have created
464 (as opposed to attached-to). */
467 have_started_inferiors_p (void)
469 return (find_inferior (&all_processes
, started_inferior_callback
, NULL
)
473 /* Return non-zero if INF, a struct process_info, was attached to. */
476 attached_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
478 struct process_info
*process
= (struct process_info
*) entry
;
480 return process
->attached
;
483 /* Return non-zero if there are any inferiors that we have attached to. */
486 have_attached_inferiors_p (void)
488 return (find_inferior (&all_processes
, attached_inferior_callback
, NULL
)
492 struct process_info
*
493 get_thread_process (struct thread_info
*thread
)
495 int pid
= ptid_get_pid (thread
->entry
.id
);
496 return find_process_pid (pid
);
499 struct process_info
*
500 current_process (void)
502 if (current_inferior
== NULL
)
503 fatal ("Current inferior requested, but current_inferior is NULL\n");
505 return get_thread_process (current_inferior
);
509 initialize_inferiors (void)
511 null_ptid
= ptid_build (0, 0, 0);
512 minus_one_ptid
= ptid_build (-1, 0, 0);