1 /* Inferior process information for the remote server for GDB.
2 Copyright (C) 2002, 2005, 2007, 2008, 2009, 2010, 2011
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/>. */
26 struct inferior_list all_processes;
27 struct inferior_list all_threads;
28 struct inferior_list all_dlls;
31 struct thread_info *current_inferior;
36 ptid_t minus_one_ptid;
38 /* Create a ptid given the necessary PID, LWP, and TID components. */
41 ptid_build (int pid, long lwp, long tid)
51 /* Create a ptid from just a pid. */
56 return ptid_build (pid, 0, 0);
59 /* Fetch the pid (process id) component from a ptid. */
62 ptid_get_pid (ptid_t ptid)
67 /* Fetch the lwp (lightweight process) component from a ptid. */
70 ptid_get_lwp (ptid_t ptid)
75 /* Fetch the tid (thread id) component from a ptid. */
78 ptid_get_tid (ptid_t ptid)
83 /* ptid_equal() is used to test equality of two ptids. */
86 ptid_equal (ptid_t ptid1, ptid_t ptid2)
88 return (ptid1.pid == ptid2.pid
89 && ptid1.lwp == ptid2.lwp
90 && ptid1.tid == ptid2.tid);
93 /* Return true if this ptid represents a process. */
96 ptid_is_pid (ptid_t ptid)
98 if (ptid_equal (minus_one_ptid, ptid))
100 if (ptid_equal (null_ptid, ptid))
103 return (ptid_get_pid (ptid) != 0
104 && ptid_get_lwp (ptid) == 0
105 && ptid_get_tid (ptid) == 0);
108 #define get_thread(inf) ((struct thread_info *)(inf))
109 #define get_dll(inf) ((struct dll_info *)(inf))
112 add_inferior_to_list (struct inferior_list *list,
113 struct inferior_list_entry *new_inferior)
115 new_inferior->next = NULL;
116 if (list->tail != NULL)
117 list->tail->next = new_inferior;
119 list->head = new_inferior;
120 list->tail = new_inferior;
123 /* Invoke ACTION for each inferior in LIST. */
126 for_each_inferior (struct inferior_list *list,
127 void (*action) (struct inferior_list_entry *))
129 struct inferior_list_entry *cur = list->head, *next;
140 remove_inferior (struct inferior_list *list,
141 struct inferior_list_entry *entry)
143 struct inferior_list_entry **cur;
145 if (list->head == entry)
147 list->head = entry->next;
148 if (list->tail == entry)
149 list->tail = list->head;
154 while (*cur && (*cur)->next != entry)
160 (*cur)->next = entry->next;
162 if (list->tail == entry)
167 add_thread (ptid_t thread_id, void *target_data)
169 struct thread_info *new_thread = xmalloc (sizeof (*new_thread));
171 memset (new_thread, 0, sizeof (*new_thread));
173 new_thread->entry.id = thread_id;
174 new_thread->last_resume_kind = resume_continue;
175 new_thread->last_status.kind = TARGET_WAITKIND_IGNORE;
177 add_inferior_to_list (&all_threads, & new_thread->entry);
179 if (current_inferior == NULL)
180 current_inferior = new_thread;
182 new_thread->target_data = target_data;
183 set_inferior_regcache_data (new_thread, new_register_cache ());
187 thread_id_to_gdb_id (ptid_t thread_id)
189 struct inferior_list_entry *inf = all_threads.head;
193 if (ptid_equal (inf->id, thread_id))
202 thread_to_gdb_id (struct thread_info *thread)
204 return thread->entry.id;
208 find_thread_ptid (ptid_t ptid)
210 struct inferior_list_entry *inf = all_threads.head;
214 struct thread_info *thread = get_thread (inf);
215 if (ptid_equal (thread->entry.id, ptid))
224 gdb_id_to_thread_id (ptid_t gdb_id)
226 struct thread_info *thread = find_thread_ptid (gdb_id);
228 return thread ? thread->entry.id : null_ptid;
232 free_one_thread (struct inferior_list_entry *inf)
234 struct thread_info *thread = get_thread (inf);
235 free_register_cache (inferior_regcache_data (thread));
240 remove_thread (struct thread_info *thread)
242 remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
243 free_one_thread (&thread->entry);
246 /* Find the first inferior_list_entry E in LIST for which FUNC (E, ARG)
247 returns non-zero. If no entry is found then return NULL. */
249 struct inferior_list_entry *
250 find_inferior (struct inferior_list *list,
251 int (*func) (struct inferior_list_entry *, void *), void *arg)
253 struct inferior_list_entry *inf = list->head;
257 struct inferior_list_entry *next;
260 if ((*func) (inf, arg))
268 struct inferior_list_entry *
269 find_inferior_id (struct inferior_list *list, ptid_t id)
271 struct inferior_list_entry *inf = list->head;
275 if (ptid_equal (inf->id, id))
284 inferior_target_data (struct thread_info *inferior)
286 return inferior->target_data;
290 set_inferior_target_data (struct thread_info *inferior, void *data)
292 inferior->target_data = data;
296 inferior_regcache_data (struct thread_info *inferior)
298 return inferior->regcache_data;
302 set_inferior_regcache_data (struct thread_info *inferior, void *data)
304 inferior->regcache_data = data;
308 free_one_dll (struct inferior_list_entry *inf)
310 struct dll_info *dll = get_dll (inf);
311 if (dll->name != NULL)
316 /* Find a DLL with the same name and/or base address. A NULL name in
317 the key is ignored; so is an all-ones base address. */
320 match_dll (struct inferior_list_entry *inf, void *arg)
322 struct dll_info *iter = (void *) inf;
323 struct dll_info *key = arg;
325 if (key->base_addr != ~(CORE_ADDR) 0
326 && iter->base_addr == key->base_addr)
328 else if (key->name != NULL
329 && iter->name != NULL
330 && strcmp (key->name, iter->name) == 0)
336 /* Record a newly loaded DLL at BASE_ADDR. */
339 loaded_dll (const char *name, CORE_ADDR base_addr)
341 struct dll_info *new_dll = xmalloc (sizeof (*new_dll));
342 memset (new_dll, 0, sizeof (*new_dll));
344 new_dll->entry.id = minus_one_ptid;
346 new_dll->name = xstrdup (name);
347 new_dll->base_addr = base_addr;
349 add_inferior_to_list (&all_dlls, &new_dll->entry);
353 /* Record that the DLL with NAME and BASE_ADDR has been unloaded. */
356 unloaded_dll (const char *name, CORE_ADDR base_addr)
358 struct dll_info *dll;
359 struct dll_info key_dll;
361 /* Be careful not to put the key DLL in any list. */
362 key_dll.name = (char *) name;
363 key_dll.base_addr = base_addr;
365 dll = (void *) find_inferior (&all_dlls, match_dll, &key_dll);
368 /* For some inferiors we might get unloaded_dll events without having
369 a corresponding loaded_dll. In that case, the dll cannot be found
370 in ALL_DLL, and there is nothing further for us to do.
372 This has been observed when running 32bit executables on Windows64
373 (i.e. through WOW64, the interface between the 32bits and 64bits
374 worlds). In that case, the inferior always does some strange
375 unloading of unnamed dll. */
379 /* DLL has been found so remove the entry and free associated
381 remove_inferior (&all_dlls, &dll->entry);
382 free_one_dll (&dll->entry);
387 #define clear_list(LIST) \
388 do { (LIST)->head = (LIST)->tail = NULL; } while (0)
391 clear_inferiors (void)
393 for_each_inferior (&all_threads, free_one_thread);
394 for_each_inferior (&all_dlls, free_one_dll);
396 clear_list (&all_threads);
397 clear_list (&all_dlls);
399 current_inferior = NULL;
402 /* Two utility functions for a truly degenerate inferior_list: a simple
406 add_pid_to_list (struct inferior_list *list, unsigned long pid)
408 struct inferior_list_entry *new_entry;
410 new_entry = xmalloc (sizeof (struct inferior_list_entry));
411 new_entry->id = pid_to_ptid (pid);
412 add_inferior_to_list (list, new_entry);
416 pull_pid_from_list (struct inferior_list *list, unsigned long pid)
418 struct inferior_list_entry *new_entry;
420 new_entry = find_inferior_id (list, pid_to_ptid (pid));
421 if (new_entry == NULL)
425 remove_inferior (list, new_entry);
431 struct process_info *
432 add_process (int pid, int attached)
434 struct process_info *process;
436 process = xcalloc (1, sizeof (*process));
438 process->head.id = pid_to_ptid (pid);
439 process->attached = attached;
441 add_inferior_to_list (&all_processes, &process->head);
446 /* Remove a process from the common process list and free the memory
448 The caller is responsible for freeing private data first. */
451 remove_process (struct process_info *process)
453 clear_symbol_cache (&process->symbol_cache);
454 free_all_breakpoints (process);
455 remove_inferior (&all_processes, &process->head);
459 struct process_info *
460 find_process_pid (int pid)
462 return (struct process_info *)
463 find_inferior_id (&all_processes, pid_to_ptid (pid));
466 /* Return non-zero if INF, a struct process_info, was started by us,
467 i.e. not attached to. */
470 started_inferior_callback (struct inferior_list_entry *entry, void *args)
472 struct process_info *process = (struct process_info *) entry;
474 return ! process->attached;
477 /* Return non-zero if there are any inferiors that we have created
478 (as opposed to attached-to). */
481 have_started_inferiors_p (void)
483 return (find_inferior (&all_processes, started_inferior_callback, NULL)
487 /* Return non-zero if INF, a struct process_info, was attached to. */
490 attached_inferior_callback (struct inferior_list_entry *entry, void *args)
492 struct process_info *process = (struct process_info *) entry;
494 return process->attached;
497 /* Return non-zero if there are any inferiors that we have attached to. */
500 have_attached_inferiors_p (void)
502 return (find_inferior (&all_processes, attached_inferior_callback, NULL)
506 struct process_info *
507 get_thread_process (struct thread_info *thread)
509 int pid = ptid_get_pid (thread->entry.id);
510 return find_process_pid (pid);
513 struct process_info *
514 current_process (void)
516 if (current_inferior == NULL)
517 fatal ("Current inferior requested, but current_inferior is NULL\n");
519 return get_thread_process (current_inferior);
523 initialize_inferiors (void)
525 null_ptid = ptid_build (0, 0, 0);
526 minus_one_ptid = ptid_build (-1, 0, 0);