target.h: #include <sys/types.h>.
[deliverable/binutils-gdb.git] / gdb / gdbserver / inferiors.c
index 48d7700b57f89e5cfc0a8b1a562734ce2929274a..72a3ef1b1b61e09d27b48ec38acea2a4ab7bc1d1 100644 (file)
@@ -1,5 +1,5 @@
 /* Inferior process information for the remote server for GDB.
-   Copyright (C) 2002-2014 Free Software Foundation, Inc.
+   Copyright (C) 2002-2015 Free Software Foundation, Inc.
 
    Contributed by MontaVista Software.
 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "server.h"
-#include <stdlib.h>
 #include "gdbthread.h"
 #include "dll.h"
 
 struct inferior_list all_processes;
 struct inferior_list all_threads;
 
-struct thread_info *current_inferior;
+struct thread_info *current_thread;
 
 #define get_thread(inf) ((struct thread_info *)(inf))
 
@@ -106,9 +105,7 @@ remove_inferior (struct inferior_list *list,
 struct thread_info *
 add_thread (ptid_t thread_id, void *target_data)
 {
-  struct thread_info *new_thread = xmalloc (sizeof (*new_thread));
-
-  memset (new_thread, 0, sizeof (*new_thread));
+  struct thread_info *new_thread = XCNEW (struct thread_info);
 
   new_thread->entry.id = thread_id;
   new_thread->last_resume_kind = resume_continue;
@@ -116,8 +113,8 @@ add_thread (ptid_t thread_id, void *target_data)
 
   add_inferior_to_list (&all_threads, &new_thread->entry);
 
-  if (current_inferior == NULL)
-    current_inferior = new_thread;
+  if (current_thread == NULL)
+    current_thread = new_thread;
 
   new_thread->target_data = target_data;
 
@@ -144,6 +141,27 @@ find_thread_ptid (ptid_t ptid)
   return (struct thread_info *) find_inferior_id (&all_threads, ptid);
 }
 
+/* Predicate function for matching thread entry's pid to the given
+   pid value passed by address in ARGS.  */
+
+static int
+thread_pid_matches_callback (struct inferior_list_entry *entry, void *args)
+{
+  return (ptid_get_pid (entry->id) == *(pid_t *)args);
+}
+
+/* Find a thread associated with the given PROCESS, or NULL if no
+   such thread exists.  */
+
+static struct thread_info *
+find_thread_process (const struct process_info *const process)
+{
+  pid_t pid = ptid_get_pid (ptid_of (process));
+
+  return (struct thread_info *)
+    find_inferior (&all_threads, thread_pid_matches_callback, &pid);
+}
+
 ptid_t
 gdb_id_to_thread_id (ptid_t gdb_id)
 {
@@ -166,8 +184,11 @@ remove_thread (struct thread_info *thread)
   if (thread->btrace != NULL)
     target_disable_btrace (thread->btrace);
 
+  discard_queued_stop_replies (ptid_of (thread));
   remove_inferior (&all_threads, (struct inferior_list_entry *) thread);
   free_one_thread (&thread->entry);
+  if (current_thread == thread)
+    current_thread = NULL;
 }
 
 /* Return a pointer to the first inferior in LIST, or NULL if there isn't one.
@@ -231,14 +252,14 @@ set_inferior_target_data (struct thread_info *inferior, void *data)
   inferior->target_data = data;
 }
 
-void *
+struct regcache *
 inferior_regcache_data (struct thread_info *inferior)
 {
   return inferior->regcache_data;
 }
 
 void
-set_inferior_regcache_data (struct thread_info *inferior, void *data)
+set_inferior_regcache_data (struct thread_info *inferior, struct regcache *data)
 {
   inferior->regcache_data = data;
 }
@@ -268,15 +289,13 @@ clear_inferiors (void)
 
   clear_dlls ();
 
-  current_inferior = NULL;
+  current_thread = NULL;
 }
 
 struct process_info *
 add_process (int pid, int attached)
 {
-  struct process_info *process;
-
-  process = xcalloc (1, sizeof (*process));
+  struct process_info *process = XCNEW (struct process_info);
 
   process->entry.id = pid_to_ptid (pid);
   process->attached = attached;
@@ -295,6 +314,7 @@ remove_process (struct process_info *process)
 {
   clear_symbol_cache (&process->symbol_cache);
   free_all_breakpoints (process);
+  gdb_assert (find_thread_process (process) == NULL);
   remove_inferior (&all_processes, &process->entry);
   free (process);
 }
@@ -306,6 +326,14 @@ find_process_pid (int pid)
     find_inferior_id (&all_processes, pid_to_ptid (pid));
 }
 
+/* Wrapper around get_first_inferior to return a struct process_info *.  */
+
+struct process_info *
+get_first_process (void)
+{
+  return (struct process_info *) get_first_inferior (&all_processes);
+}
+
 /* Return non-zero if INF, a struct process_info, was started by us,
    i.e. not attached to.  */
 
@@ -356,8 +384,6 @@ get_thread_process (struct thread_info *thread)
 struct process_info *
 current_process (void)
 {
-  if (current_inferior == NULL)
-    fatal ("Current inferior requested, but current_inferior is NULL\n");
-
-  return get_thread_process (current_inferior);
+  gdb_assert (current_thread != NULL);
+  return get_thread_process (current_thread);
 }
This page took 0.025163 seconds and 4 git commands to generate.