1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 Contributed by MontaVista Software.
7 This file is part of GDB.
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.
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.
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/>. */
28 struct inferior_list_entry entry
;
34 struct inferior_list all_processes
;
35 struct inferior_list all_threads
;
36 struct inferior_list all_dlls
;
39 struct thread_info
*current_inferior
;
44 ptid_t minus_one_ptid
;
46 /* Create a ptid given the necessary PID, LWP, and TID components. */
49 ptid_build (int pid
, long lwp
, long tid
)
59 /* Create a ptid from just a pid. */
64 return ptid_build (pid
, 0, 0);
67 /* Fetch the pid (process id) component from a ptid. */
70 ptid_get_pid (ptid_t ptid
)
75 /* Fetch the lwp (lightweight process) component from a ptid. */
78 ptid_get_lwp (ptid_t ptid
)
83 /* Fetch the tid (thread id) component from a ptid. */
86 ptid_get_tid (ptid_t ptid
)
91 /* ptid_equal() is used to test equality of two ptids. */
94 ptid_equal (ptid_t ptid1
, ptid_t ptid2
)
96 return (ptid1
.pid
== ptid2
.pid
97 && ptid1
.lwp
== ptid2
.lwp
98 && ptid1
.tid
== ptid2
.tid
);
101 /* Return true if this ptid represents a process. */
104 ptid_is_pid (ptid_t ptid
)
106 if (ptid_equal (minus_one_ptid
, ptid
))
108 if (ptid_equal (null_ptid
, ptid
))
111 return (ptid_get_pid (ptid
) != 0
112 && ptid_get_lwp (ptid
) == 0
113 && ptid_get_tid (ptid
) == 0);
116 #define get_thread(inf) ((struct thread_info *)(inf))
117 #define get_dll(inf) ((struct dll_info *)(inf))
120 add_inferior_to_list (struct inferior_list
*list
,
121 struct inferior_list_entry
*new_inferior
)
123 new_inferior
->next
= NULL
;
124 if (list
->tail
!= NULL
)
125 list
->tail
->next
= new_inferior
;
127 list
->head
= new_inferior
;
128 list
->tail
= new_inferior
;
131 /* Invoke ACTION for each inferior in LIST. */
134 for_each_inferior (struct inferior_list
*list
,
135 void (*action
) (struct inferior_list_entry
*))
137 struct inferior_list_entry
*cur
= list
->head
, *next
;
148 remove_inferior (struct inferior_list
*list
,
149 struct inferior_list_entry
*entry
)
151 struct inferior_list_entry
**cur
;
153 if (list
->head
== entry
)
155 list
->head
= entry
->next
;
156 if (list
->tail
== entry
)
157 list
->tail
= list
->head
;
162 while (*cur
&& (*cur
)->next
!= entry
)
168 (*cur
)->next
= entry
->next
;
170 if (list
->tail
== entry
)
175 add_thread (ptid_t thread_id
, void *target_data
)
177 struct thread_info
*new_thread
= xmalloc (sizeof (*new_thread
));
179 memset (new_thread
, 0, sizeof (*new_thread
));
181 new_thread
->entry
.id
= thread_id
;
183 add_inferior_to_list (&all_threads
, & new_thread
->entry
);
185 if (current_inferior
== NULL
)
186 current_inferior
= new_thread
;
188 new_thread
->target_data
= target_data
;
189 set_inferior_regcache_data (new_thread
, new_register_cache ());
193 thread_id_to_gdb_id (ptid_t thread_id
)
195 struct inferior_list_entry
*inf
= all_threads
.head
;
199 if (ptid_equal (inf
->id
, thread_id
))
208 thread_to_gdb_id (struct thread_info
*thread
)
210 return thread
->entry
.id
;
214 find_thread_ptid (ptid_t ptid
)
216 struct inferior_list_entry
*inf
= all_threads
.head
;
220 struct thread_info
*thread
= get_thread (inf
);
221 if (ptid_equal (thread
->entry
.id
, ptid
))
230 gdb_id_to_thread_id (ptid_t gdb_id
)
232 struct thread_info
*thread
= find_thread_ptid (gdb_id
);
234 return thread
? thread
->entry
.id
: null_ptid
;
238 free_one_thread (struct inferior_list_entry
*inf
)
240 struct thread_info
*thread
= get_thread (inf
);
241 free_register_cache (inferior_regcache_data (thread
));
246 remove_thread (struct thread_info
*thread
)
248 remove_inferior (&all_threads
, (struct inferior_list_entry
*) thread
);
249 free_one_thread (&thread
->entry
);
252 struct inferior_list_entry
*
253 find_inferior (struct inferior_list
*list
,
254 int (*func
) (struct inferior_list_entry
*, void *), void *arg
)
256 struct inferior_list_entry
*inf
= list
->head
;
260 struct inferior_list_entry
*next
;
263 if ((*func
) (inf
, arg
))
271 struct inferior_list_entry
*
272 find_inferior_id (struct inferior_list
*list
, ptid_t id
)
274 struct inferior_list_entry
*inf
= list
->head
;
278 if (ptid_equal (inf
->id
, id
))
287 inferior_target_data (struct thread_info
*inferior
)
289 return inferior
->target_data
;
293 set_inferior_target_data (struct thread_info
*inferior
, void *data
)
295 inferior
->target_data
= data
;
299 inferior_regcache_data (struct thread_info
*inferior
)
301 return inferior
->regcache_data
;
305 set_inferior_regcache_data (struct thread_info
*inferior
, void *data
)
307 inferior
->regcache_data
= data
;
311 free_one_dll (struct inferior_list_entry
*inf
)
313 struct dll_info
*dll
= get_dll (inf
);
314 if (dll
->name
!= NULL
)
319 /* Find a DLL with the same name and/or base address. A NULL name in
320 the key is ignored; so is an all-ones base address. */
323 match_dll (struct inferior_list_entry
*inf
, void *arg
)
325 struct dll_info
*iter
= (void *) inf
;
326 struct dll_info
*key
= arg
;
328 if (key
->base_addr
!= ~(CORE_ADDR
) 0
329 && iter
->base_addr
== key
->base_addr
)
331 else if (key
->name
!= NULL
332 && iter
->name
!= NULL
333 && strcmp (key
->name
, iter
->name
) == 0)
339 /* Record a newly loaded DLL at BASE_ADDR. */
342 loaded_dll (const char *name
, CORE_ADDR base_addr
)
344 struct dll_info
*new_dll
= xmalloc (sizeof (*new_dll
));
345 memset (new_dll
, 0, sizeof (*new_dll
));
347 new_dll
->entry
.id
= minus_one_ptid
;
349 new_dll
->name
= xstrdup (name
);
350 new_dll
->base_addr
= base_addr
;
352 add_inferior_to_list (&all_dlls
, &new_dll
->entry
);
356 /* Record that the DLL with NAME and BASE_ADDR has been unloaded. */
359 unloaded_dll (const char *name
, CORE_ADDR base_addr
)
361 struct dll_info
*dll
;
362 struct dll_info key_dll
;
364 /* Be careful not to put the key DLL in any list. */
365 key_dll
.name
= (char *) name
;
366 key_dll
.base_addr
= base_addr
;
368 dll
= (void *) find_inferior (&all_dlls
, match_dll
, &key_dll
);
369 remove_inferior (&all_dlls
, &dll
->entry
);
370 free_one_dll (&dll
->entry
);
374 #define clear_list(LIST) \
375 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
378 clear_inferiors (void)
380 for_each_inferior (&all_threads
, free_one_thread
);
381 for_each_inferior (&all_dlls
, free_one_dll
);
383 clear_list (&all_threads
);
384 clear_list (&all_dlls
);
386 current_inferior
= NULL
;
389 /* Two utility functions for a truly degenerate inferior_list: a simple
393 add_pid_to_list (struct inferior_list
*list
, unsigned long pid
)
395 struct inferior_list_entry
*new_entry
;
397 new_entry
= xmalloc (sizeof (struct inferior_list_entry
));
398 new_entry
->id
= pid_to_ptid (pid
);
399 add_inferior_to_list (list
, new_entry
);
403 pull_pid_from_list (struct inferior_list
*list
, unsigned long pid
)
405 struct inferior_list_entry
*new_entry
;
407 new_entry
= find_inferior_id (list
, pid_to_ptid (pid
));
408 if (new_entry
== NULL
)
412 remove_inferior (list
, new_entry
);
418 struct process_info
*
419 add_process (int pid
, int attached
)
421 struct process_info
*process
;
423 process
= xcalloc (1, sizeof (*process
));
425 process
->head
.id
= pid_to_ptid (pid
);
426 process
->attached
= attached
;
428 add_inferior_to_list (&all_processes
, &process
->head
);
433 /* Remove a process from the common process list and free the memory
435 The caller is responsible for freeing private data first. */
438 remove_process (struct process_info
*process
)
440 clear_symbol_cache (&process
->symbol_cache
);
441 free_all_breakpoints (process
);
442 remove_inferior (&all_processes
, &process
->head
);
446 struct process_info
*
447 find_process_pid (int pid
)
449 return (struct process_info
*)
450 find_inferior_id (&all_processes
, pid_to_ptid (pid
));
453 /* Return non-zero if INF, a struct process_info, was started by us,
454 i.e. not attached to. */
457 started_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
459 struct process_info
*process
= (struct process_info
*) entry
;
461 return ! process
->attached
;
464 /* Return non-zero if there are any inferiors that we have created
465 (as opposed to attached-to). */
468 have_started_inferiors_p (void)
470 return (find_inferior (&all_processes
, started_inferior_callback
, NULL
)
474 /* Return non-zero if INF, a struct process_info, was attached to. */
477 attached_inferior_callback (struct inferior_list_entry
*entry
, void *args
)
479 struct process_info
*process
= (struct process_info
*) entry
;
481 return process
->attached
;
484 /* Return non-zero if there are any inferiors that we have attached to. */
487 have_attached_inferiors_p (void)
489 return (find_inferior (&all_processes
, attached_inferior_callback
, NULL
)
493 struct process_info
*
494 get_thread_process (struct thread_info
*thread
)
496 int pid
= ptid_get_pid (thread
->entry
.id
);
497 return find_process_pid (pid
);
500 struct process_info
*
501 current_process (void)
503 if (current_inferior
== NULL
)
504 fatal ("Current inferior requested, but current_inferior is NULL\n");
506 return get_thread_process (current_inferior
);
510 initialize_inferiors (void)
512 null_ptid
= ptid_build (0, 0, 0);
513 minus_one_ptid
= ptid_build (-1, 0, 0);