OSDN Git Service

*** empty log message ***
[pf3gnuchains/sourceware.git] / gdb / dec-thread.c
index 8398e8d..650b86c 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+/* Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -40,9 +40,6 @@ pthreadDebugContext_t debug_context;
 /* The dec-thread target_ops structure.  */
 static struct target_ops dec_thread_ops;
 
-/* A copy of the target_ops over which our dec_thread_ops is pushed.  */
-static struct target_ops base_target;
-
 /* Print a debug trace if DEBUG_DEC_THREAD is set (its value is adjusted
    by the user using "set debug dec-thread ...").  */
 
@@ -111,7 +108,7 @@ write_clbk (void *caller_context, void *address, void *buffer,
   return ESUCCESS;
 }
 
-/* Get integer regs */
+/* Get integer regs */
 
 static int
 get_reg_clbk(void *caller_context, pthreadDebugGetRegRtn_t regs,
@@ -123,7 +120,7 @@ get_reg_clbk(void *caller_context, pthreadDebugGetRegRtn_t regs,
   return ESUCCESS;
 }
 
-/* Set integer regs */
+/* Set integer regs */
 
 static int
 set_reg_clbk(void *caller_context, const pthreadDebugRegs_t *regs,
@@ -253,14 +250,13 @@ enable_dec_thread (void)
       return;
     }
 
-  base_target = current_target;
   push_target (&dec_thread_ops);
   dec_thread_active = 1;
 
   debug ("enable_dec_thread: Thread support enabled.");
 }
 
-/* Deactivate thread support.  Do nothing is thread support is
+/* Deactivate thread support.  Do nothing if thread support is
    already inactive.  */
 
 static void
@@ -315,6 +311,30 @@ ptid_build_from_info (struct dec_thread_info info)
   return ptid_build (pid, 0, (long) info.thread);
 }
 
+/* Return non-zero if PTID is still alive.
+
+   Assumes that DEC_THREAD_LIST is up to date.  */
+static int
+dec_thread_ptid_is_alive (ptid_t ptid)
+{
+  pthreadDebugId_t tid = ptid_get_tid (ptid);
+  int i;
+  struct dec_thread_info *info;
+
+  if (tid == 0)
+    /* This is the thread corresponding to the process.  This ptid
+       is always alive until the program exits.  */
+    return 1;
+
+  /* Search whether an entry with the same tid exists in the dec-thread
+     list of threads.  If it does, then the thread is still alive.
+     No match found means that the thread must be dead, now.  */
+  for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info); i++)
+    if (info->thread == tid)
+      return 1;
+  return 0;
+}
+
 /* Recompute the list of user threads and store the result in
    DEC_THREAD_LIST.  */
 
@@ -356,7 +376,7 @@ dec_thread_count_gdb_threads (struct thread_info *ignored, void *context)
 {
   int *count = (int *) context;
 
-  *count++;
+  *count = *count + 1;
   return 0;
 }
 
@@ -370,34 +390,41 @@ dec_thread_add_gdb_thread (struct thread_info *info, void *context)
   struct thread_info ***listp = (struct thread_info ***) context;
   
   **listp = info;
-  *listp++;
+  *listp = *listp + 1;
   return 0;
 }
 
-/* Resynchronize the list of threads known by GDB with the actual
-   list of threads reported by libpthread_debug.  */
+/* Implement the find_new_thread target_ops method.  */
 
 static void
-resync_thread_list (void)
+dec_thread_find_new_threads (struct target_ops *ops)
 {
   int i;
   struct dec_thread_info *info;
-  int num_gdb_threads = 0;
-  struct thread_info **gdb_thread_list;
-  struct thread_info **next_thread_info;
 
   update_dec_thread_list ();
-
-  /* Add new threads.  */
-
-  for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info);
-       i++)
+  for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info); i++)
     {
       ptid_t ptid = ptid_build_from_info (*info);
 
       if (!in_thread_list (ptid))
         add_thread (ptid);
     }
+}
+
+/* Resynchronize the list of threads known by GDB with the actual
+   list of threads reported by libpthread_debug.  */
+
+static void
+resync_thread_list (struct target_ops *ops)
+{
+  int i;
+  int num_gdb_threads = 0;
+  struct thread_info **gdb_thread_list;
+  struct thread_info **next_thread_info;
+
+  /* Add new threads.  */
+  dec_thread_find_new_threads (ops);
 
   /* Remove threads that no longer exist.  To help with the search,
      we build an array of GDB threads, and then iterate over this
@@ -408,28 +435,23 @@ resync_thread_list (void)
   gdb_thread_list = alloca (num_gdb_threads * sizeof (struct thread_info *));
   next_thread_info = gdb_thread_list;
   iterate_over_threads (dec_thread_add_gdb_thread, (void *) &next_thread_info);
-  for (i = 0; i < num_gdb_threads; i++)
-    {
-      int j;
 
-      for (j = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, j, info);
-           j++)
-        if (ptid_equal (gdb_thread_list[i]->ptid,
-                         ptid_build_from_info (*info)))
-          break;
+  for (i = 0; i < num_gdb_threads; i++)
+    if (!dec_thread_ptid_is_alive (gdb_thread_list[i]->ptid))
       delete_thread (gdb_thread_list[i]->ptid);
-    }
 }
 
 /* The "to_detach" method of the dec_thread_ops.  */
 
 static void
-dec_thread_detach (char *args, int from_tty)
+dec_thread_detach (struct target_ops *ops, char *args, int from_tty)
 {   
+  struct target_ops *beneath = find_target_beneath (ops);
+
   debug ("dec_thread_detach");
 
   disable_dec_thread ();
-  base_target.to_detach (&base_target, args, from_tty);
+  beneath->to_detach (beneath, args, from_tty);
 }
 
 /* Return the ptid of the thread that is currently active.  */
@@ -454,18 +476,18 @@ get_active_ptid (void)
 
 static ptid_t
 dec_thread_wait (struct target_ops *ops,
-                ptid_t ptid, struct target_waitstatus *status)
+                ptid_t ptid, struct target_waitstatus *status, int options)
 {
   ptid_t active_ptid;
+  struct target_ops *beneath = find_target_beneath (ops);
 
   debug ("dec_thread_wait");
 
-  ptid = base_target.to_wait (&base_target, ptid, status);
+  ptid = beneath->to_wait (beneath, ptid, status, options);
 
-  /* The ptid returned by the base_target is the ptid of the process.
-     We need to find which thread is currently active and return its
-     ptid.  */
-  resync_thread_list ();
+  /* The ptid returned by the target beneath us is the ptid of the process.
+     We need to find which thread is currently active and return its ptid.  */
+  resync_thread_list (ops);
   active_ptid = get_active_ptid ();
   if (ptid_equal (active_ptid, null_ptid))
     return ptid;
@@ -487,7 +509,7 @@ dec_thread_get_regsets (pthreadDebugId_t tid, gdb_gregset_t *gregset,
   res = pthreadDebugThdGetReg (debug_context, tid, &regs);
   if (res != ESUCCESS)
     {
-      debug ("dec_thread_fetch_registers: pthreadDebugThdGetReg -> %d", res);
+      debug ("dec_thread_get_regsets: pthreadDebugThdGetReg -> %d", res);
       return -1;
     }
   memcpy (gregset->regs, &regs, sizeof (regs));
@@ -495,7 +517,7 @@ dec_thread_get_regsets (pthreadDebugId_t tid, gdb_gregset_t *gregset,
   res = pthreadDebugThdGetFreg (debug_context, tid, &fregs);
   if (res != ESUCCESS)
     {
-      debug ("dec_thread_fetch_registers: pthreadDebugThdGetFreg -> %d", res);
+      debug ("dec_thread_get_regsets: pthreadDebugThdGetFreg -> %d", res);
       return -1;
     }
   memcpy (fpregset->regs, &fregs, sizeof (fregs));
@@ -510,7 +532,8 @@ dec_thread_get_regsets (pthreadDebugId_t tid, gdb_gregset_t *gregset,
    registers.  */
 
 static void
-dec_thread_fetch_registers (struct regcache *regcache, int regno)
+dec_thread_fetch_registers (struct target_ops *ops,
+                            struct regcache *regcache, int regno)
 {
   pthreadDebugId_t tid = ptid_get_tid (inferior_ptid);
   gregset_t gregset;
@@ -522,7 +545,9 @@ dec_thread_fetch_registers (struct regcache *regcache, int regno)
 
   if (tid == 0 || ptid_equal (inferior_ptid, get_active_ptid ()))
     {
-      base_target.to_fetch_registers (regcache, regno);
+      struct target_ops *beneath = find_target_beneath (ops);
+
+      beneath->to_fetch_registers (beneath, regcache, regno);
       return;
     }
 
@@ -550,7 +575,7 @@ dec_thread_set_regsets (pthreadDebugId_t tid, gdb_gregset_t gregset,
   res = pthreadDebugThdSetReg (debug_context, tid, &regs);
   if (res != ESUCCESS)
     {
-      debug ("dec_thread_fetch_registers: pthreadDebugThdSetReg -> %d", res);
+      debug ("dec_thread_set_regsets: pthreadDebugThdSetReg -> %d", res);
       return -1;
     }
 
@@ -558,7 +583,7 @@ dec_thread_set_regsets (pthreadDebugId_t tid, gdb_gregset_t gregset,
   res = pthreadDebugThdSetFreg (debug_context, tid, &fregs);
   if (res != ESUCCESS)
     {
-      debug ("dec_thread_fetch_registers: pthreadDebugThdSetFreg -> %d", res);
+      debug ("dec_thread_set_regsets: pthreadDebugThdSetFreg -> %d", res);
       return -1;
     }
 
@@ -571,7 +596,8 @@ dec_thread_set_regsets (pthreadDebugId_t tid, gdb_gregset_t gregset,
    just one register, we store all the registers.  */
 
 static void
-dec_thread_store_registers (struct regcache *regcache, int regno)
+dec_thread_store_registers (struct target_ops *ops,
+                            struct regcache *regcache, int regno)
 {
   pthreadDebugId_t tid = ptid_get_tid (inferior_ptid);
   gregset_t gregset;
@@ -582,7 +608,9 @@ dec_thread_store_registers (struct regcache *regcache, int regno)
 
   if (tid == 0 || ptid_equal (inferior_ptid, get_active_ptid ()))
     {
-      base_target.to_store_registers (regcache, regno);
+      struct target_ops *beneath = find_target_beneath (ops);
+
+      beneath->to_store_registers (beneath, regcache, regno);
       return;
     }
 
@@ -601,17 +629,19 @@ dec_thread_store_registers (struct regcache *regcache, int regno)
 /* The "to_mourn_inferior" method of the dec_thread_ops.  */
 
 static void
-dec_thread_mourn_inferior (void)
+dec_thread_mourn_inferior (struct target_ops *ops)
 {
+  struct target_ops *beneath = find_target_beneath (ops);
+
   debug ("dec_thread_mourn_inferior");
 
   disable_dec_thread ();
-  base_target.to_mourn_inferior (&base_target);
+  beneath->to_mourn_inferior (beneath);
 }
 
 /* The "to_thread_alive" method of the dec_thread_ops.  */
 static int
-dec_thread_thread_alive (ptid_t ptid)
+dec_thread_thread_alive (struct target_ops *ops, ptid_t ptid)
 {
   debug ("dec_thread_thread_alive (tid=%ld)", ptid_get_tid (ptid));
 
@@ -628,7 +658,11 @@ dec_thread_pid_to_str (struct target_ops *ops, ptid_t ptid)
   static char *ret = NULL;
 
   if (ptid_get_tid (ptid) == 0)
-    return base_target.to_pid_to_str (&base_target, ptid);
+    {
+      struct target_ops *beneath = find_target_beneath (ops);
+
+      return beneath->to_pid_to_str (beneath, ptid);
+    }
 
   /* Free previous return value; a new one will be allocated by
      xstrprintf().  */
@@ -650,6 +684,26 @@ dec_thread_new_objfile_observer (struct objfile *objfile)
      disable_dec_thread ();
 }
 
+/* The "to_get_ada_task_ptid" method of the dec_thread_ops.  */
+
+static ptid_t
+dec_thread_get_ada_task_ptid (long lwp, long thread)
+{
+  int i;
+  struct dec_thread_info *info;
+
+  debug ("dec_thread_get_ada_task_ptid (lwp=0x%lx, thread=0x%lx)",
+         lwp, thread);
+
+  for (i = 0; VEC_iterate (dec_thread_info_s, dec_thread_list, i, info);
+       i++)
+    if (info->info.teb == (pthread_t) thread)
+      return ptid_build_from_info (*info);
+  
+  warning (_("Could not find thread id from THREAD = 0x%lx\n"), thread);
+  return inferior_ptid;
+}
+
 static void
 init_dec_thread_ops (void)
 {
@@ -662,8 +716,10 @@ init_dec_thread_ops (void)
   dec_thread_ops.to_store_registers    = dec_thread_store_registers;
   dec_thread_ops.to_mourn_inferior     = dec_thread_mourn_inferior;
   dec_thread_ops.to_thread_alive       = dec_thread_thread_alive;
+  dec_thread_ops.to_find_new_threads   = dec_thread_find_new_threads;
   dec_thread_ops.to_pid_to_str         = dec_thread_pid_to_str;
   dec_thread_ops.to_stratum            = thread_stratum;
+  dec_thread_ops.to_get_ada_task_ptid  = dec_thread_get_ada_task_ptid;
   dec_thread_ops.to_magic              = OPS_MAGIC;
 }