PERFORCE change 56201 for review

David Xu davidxu at FreeBSD.org
Thu Jul 1 02:13:52 PDT 2004


http://perforce.freebsd.org/chv.cgi?CH=56201

Change 56201 by davidxu at davidxu_celeron on 2004/07/01 09:13:11

	add proc service functions. if libthread_db can not map a lwp,
	gdb should deal with it.

Affected files ...

.. //depot/projects/davidxu_ksedbg/src/gnu/usr.bin/gdb/libgdb/fbsd-threads.c#2 edit

Differences ...

==== //depot/projects/davidxu_ksedbg/src/gnu/usr.bin/gdb/libgdb/fbsd-threads.c#2 (text+ko) ====

@@ -22,7 +22,7 @@
 
 #include "gdb_assert.h"
 #include <dlfcn.h>
-#include "gdb_proc_service.h"
+#include "proc_service.h"
 #include "thread_db.h"
 
 #include "bfd.h"
@@ -81,16 +81,15 @@
 				      td_thr_events_t *event);
 static td_err_e (*td_ta_event_getmsg_p) (const td_thragent_t *ta,
 					 td_event_msg_t *msg);
-
 static td_err_e (*td_thr_validate_p) (const td_thrhandle_t *th);
 static td_err_e (*td_thr_get_info_p) (const td_thrhandle_t *th,
 				      td_thrinfo_t *infop);
 static td_err_e (*td_thr_getfpregs_p) (const td_thrhandle_t *th,
-				       gdb_prfpregset_t *regset);
+				       prfpregset_t *regset);
 static td_err_e (*td_thr_getgregs_p) (const td_thrhandle_t *th,
 				      prgregset_t gregs);
 static td_err_e (*td_thr_setfpregs_p) (const td_thrhandle_t *th,
-				       const gdb_prfpregset_t *fpregs);
+				       const prfpregset_t *fpregs);
 static td_err_e (*td_thr_setgregs_p) (const td_thrhandle_t *th,
 				      prgregset_t gregs);
 static td_err_e (*td_thr_event_enable_p) (const td_thrhandle_t *th, int event);
@@ -111,8 +110,8 @@
 #define GET_LWP(ptid)		ptid_get_lwp (ptid)
 #define GET_THREAD(ptid)	ptid_get_tid (ptid)
 
-#define is_lwp(ptid)		(GET_LWP (ptid) != 0)
-#define is_thread(ptid)		(GET_THREAD (ptid) != 0)
+#define IS_LWP(ptid)		(GET_LWP (ptid) != 0)
+#define IS_THREAD(ptid)		(GET_THREAD (ptid) != 0)
 
 #define BUILD_LWP(lwp, pid)	ptid_build (pid, lwp, 0)
 #define BUILD_THREAD(tid, pid)	ptid_build (pid, 0, tid)
@@ -197,29 +196,7 @@
     }
 }
 
-static int
-thread_activated ()
-{
-  int activated;
-
-  if (td_ta_activated_p(thread_agent, &activated))
-    return 0;
-  return (activated);
-}
-
-static long
-get_current_lwp (int pid)
-{
-  lwpid_t lwp;
-
-  if (ptrace (PT_GETXTHREAD, GET_PID(inferior_ptid), (caddr_t)&lwp, 0)) {
-    perror_with_name("PT_GETXTHREAD");
-  }
-  return (long)lwp;
-}
-
-/* Convert between user-level thread ids and LWP ids.  */
-
+/* Convert LWP to user-level thread id. */
 static ptid_t
 thread_from_lwp (ptid_t ptid)
 {
@@ -227,139 +204,46 @@
   td_thrhandle_t th;
   td_err_e err;
  
-  if (GET_LWP (ptid) == 0) {
-      ptid = BUILD_LWP (get_current_lwp (GET_PID (ptid)), GET_PID (ptid));
-  }
-  gdb_assert (is_lwp (ptid));
+  gdb_assert (IS_LWP (ptid));
 
   err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
-  if (err != TD_OK)
-    error ("Cannot find user-level thread for LWP %d: %s",
-	   GET_LWP (ptid), thread_db_err_str (err));
+  if (err == TD_OK)
+    {
+      err = td_thr_get_info_p (&th, &ti);
+      if (err != TD_OK)
+        error ("Cannot get thread info: %s", thread_db_err_str (err));
+      return BUILD_THREAD (ti.ti_tid, GET_PID (ptid));
+    }
 
-  err = td_thr_get_info_p (&th, &ti);
-  if (err != TD_OK)
-    error ("Cannot get thread info: %s", thread_db_err_str (err));
-
-  return BUILD_THREAD (ti.ti_tid, GET_PID (ptid));
+  /* the LWP is not mapped to user thread */  
+  return BUILD_LWP (GET_LWP (ptid), GET_PID (ptid));
 }
 
-static ptid_t
-lwp_from_thread (ptid_t ptid)
+static long
+get_current_lwp (int pid)
 {
-  td_thrinfo_t ti;
-  td_thrhandle_t th;
-  td_err_e err;
- 
-  if (!is_thread (ptid))
-    return ptid;
+  lwpid_t lwp;
 
-  err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (ptid), &th);
-  if (err != TD_OK)
-    error ("Cannot find thread %ld: %s",
-	   (long) GET_THREAD (ptid), thread_db_err_str (err));
+  if (ptrace (PT_GETXTHREAD, GET_PID(inferior_ptid), (caddr_t)&lwp, 0))
+    perror_with_name("PT_GETXTHREAD");
 
-  err = td_thr_get_info_p (&th, &ti);
-  if (err != TD_OK)
-    error ("Cannot get thread info: %s", thread_db_err_str (err));
-
-  return BUILD_LWP (ti.ti_lid, GET_PID (ptid));
+  return (long)lwp;
 }
 
 static void
-find_current_thread ()
+get_current_thread ()
 {
-   long lwp;
-   ptid_t tmp, ptid;
+  long lwp;
+  ptid_t tmp, ptid;
 
-#if 0
-   if (!thread_activated(thread_agent))
-	return;
-#endif
-   lwp = get_current_lwp (proc_handle.pid);
-   tmp = BUILD_LWP (lwp, proc_handle.pid);
-   ptid = thread_from_lwp (tmp);
-   if (!in_thread_list(ptid)) {
-     add_thread (ptid);
-     inferior_ptid = ptid;
-   }
-}
-
-static int
-thread_db_load (void)
-{
-  void *handle;
-  td_err_e err;
-
-  handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
-  if (handle == NULL)
-    return 0;
-
-  td_init_p = dlsym (handle, "td_init");
-  if (td_init_p == NULL)
-    return 0;
-
-  td_ta_new_p = dlsym (handle, "td_ta_new");
-  if (td_ta_new_p == NULL)
-    return 0;
-
-  td_ta_map_id2thr_p = dlsym (handle, "td_ta_map_id2thr");
-  if (td_ta_map_id2thr_p == NULL)
-    return 0;
-
-  td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr");
-  if (td_ta_map_lwp2thr_p == NULL)
-    return 0;
-
-  td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter");
-  if (td_ta_thr_iter_p == NULL)
-    return 0;
-
-  td_thr_validate_p = dlsym (handle, "td_thr_validate");
-  if (td_thr_validate_p == NULL)
-    return 0;
-
-  td_thr_get_info_p = dlsym (handle, "td_thr_get_info");
-  if (td_thr_get_info_p == NULL)
-    return 0;
-
-  td_thr_getfpregs_p = dlsym (handle, "td_thr_getfpregs");
-  if (td_thr_getfpregs_p == NULL)
-    return 0;
-
-  td_thr_getgregs_p = dlsym (handle, "td_thr_getgregs");
-  if (td_thr_getgregs_p == NULL)
-    return 0;
-
-  td_thr_setfpregs_p = dlsym (handle, "td_thr_setfpregs");
-  if (td_thr_setfpregs_p == NULL)
-    return 0;
-
-  td_thr_setgregs_p = dlsym (handle, "td_thr_setgregs");
-  if (td_thr_setgregs_p == NULL)
-    return 0;
-
-  td_thr_sstep_p = dlsym(handle, "td_thr_sstep");
-  if (td_thr_sstep_p == NULL)
-    return 0;
-
-  td_ta_activated_p = dlsym (handle, "td_ta_activated");
-  if (td_ta_activated_p == NULL)
-    return 0;
-
-  td_ta_tsd_iter_p = dlsym (handle, "td_ta_tsd_iter");
-  if (td_ta_tsd_iter_p == NULL)
-    return 0;
-
-  /* Initialize the library.  */
-  err = td_init_p ();
-  if (err != TD_OK)
+  lwp = get_current_lwp (proc_handle.pid);
+  tmp = BUILD_LWP (lwp, proc_handle.pid);
+  ptid = thread_from_lwp (tmp);
+  if (!in_thread_list (ptid))
     {
-      warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err));
-      return 0;
+      add_thread (ptid);
+      inferior_ptid = ptid;
     }
-
-  return 1;
 }
 
 static void
@@ -374,11 +258,11 @@
       /* All symbols have been discarded.  If the thread_db target is
          active, deactivate it now.  */
       if (using_thread_db)
-	{
-	  gdb_assert (proc_handle.pid == 0);
-	  unpush_target (&thread_db_ops);
-	  using_thread_db = 0;
-	}
+        {
+          gdb_assert (proc_handle.pid == 0);
+          unpush_target (&thread_db_ops);
+          using_thread_db = 0;
+        }
 
       keep_thread_db = 0;
 
@@ -394,8 +278,7 @@
      that at this point there is no guarantee that we actually have a
      child process.  */
   proc_handle.pid = GET_PID (inferior_ptid);
-  /* XXX bring in proc service module */
-  ps_getpid(&proc_handle);
+  
   /* Now attempt to open a connection to the thread library.  */
   err = td_ta_new_p (&proc_handle, &thread_agent);
   switch (err)
@@ -416,24 +299,26 @@
          least until all symbols have been discarded anyway (see
          above).  */
       if (objfile == symfile_objfile)
-	{
-	  gdb_assert (proc_handle.pid == 0);
-	  keep_thread_db = 1;
-	}
+        {
+          gdb_assert (proc_handle.pid == 0);
+          keep_thread_db = 1;
+        }
 
       /* We can only poke around if there actually is a child process.
          If there is no child process alive, postpone the steps below
          until one has been created.  */
       if (proc_handle.pid != 0)
-	{
-	  fbsd_thread_find_new_threads ();
-	  find_current_thread ();
-	}
+        {
+          fbsd_thread_find_new_threads ();
+          get_current_thread ();
+        }
+      else
+        printf_filtered("%s postpone processing\n", __func__);
       break;
 
     default:
       warning ("Cannot initialize thread debugging library: %s",
-	       thread_db_err_str (err));
+               thread_db_err_str (err));
       break;
     }
 
@@ -456,7 +341,7 @@
 
   /* ...and perform the remaining initialization steps.  */
   fbsd_thread_find_new_threads();
-  find_current_thread ();
+  get_current_thread ();
 }
 
 static void
@@ -467,12 +352,27 @@
 }
 
 static int
+single_step (ptid_t ptid, int step)
+{
+  if (IS_LWP(ptid))
+    {
+      int req = step ? PT_SETSTEP : PT_CLEARSTEP; 
+      return ptrace (req, GET_LWP (ptid), 0, 0) == 0;
+    }
+
+  td_thrhandle_t th;
+  if (td_ta_map_id2thr_p (thread_agent, GET_THREAD(ptid), &th) == 0)
+    {
+      td_thr_sstep_p (&th, step);
+      return 1;
+    }
+  return 0; 
+}
+
+static int
 resume_callback (struct thread_info *info, void *ta)
 {
-  td_thrhandle_t th;
-                                                                                                                             
-  if (td_ta_map_id2thr_p(ta, GET_THREAD(info->ptid), &th) == 0)
-    td_thr_sstep_p (&th, 0);
+  single_step (info->ptid, 0);
   return 0;
 }
 
@@ -488,7 +388,7 @@
 	GET_PID(ptid), GET_LWP(ptid), GET_THREAD(ptid), step);
 #endif
 
-  if (proc_handle.pid == 0 || !thread_activated())
+  if (proc_handle.pid == 0)
     {
       child_resume (ptid, step, signo);
       return;
@@ -509,10 +409,10 @@
     ptid = inferior_ptid;
 
   if (resume_all)
-      iterate_over_threads(resume_callback, thread_agent);
-  if (td_ta_map_id2thr_p(thread_agent, GET_THREAD(ptid), &th))
-     error ("%s: can not map user level thread", __func__);
-  td_thr_sstep_p(&th, step);
+      iterate_over_threads(resume_callback, 0);
+
+  if (single_step (ptid, step))
+     error ("single step failed");
 
   if (ptrace (PT_CONTINUE, pid, (caddr_t)1,
               target_signal_to_host(signo)) == -1)
@@ -528,37 +428,32 @@
   ret = child_ops.to_wait (ptid, ourstatus);
   if (GET_PID(ret) >= 0 && ourstatus->kind == TARGET_WAITKIND_STOPPED)
     {
- //     if (!thread_activated(thread_agent))
- //       goto out;
       if (ptrace (PT_GETXTHREAD, GET_PID(ret), (caddr_t)&lwp, 0))
-        perror_with_name ("ptrace PT_GETXTHREAD");
-      ret = BUILD_LWP(lwp, GET_PID(ret));
-      ret = thread_from_lwp(ret);
-      if (!in_thread_list (ret)) {
+        perror_with_name ("ptrace cannot get current lwp");
+      ret = thread_from_lwp (BUILD_LWP (lwp, GET_PID (ret)));
+      if (!in_thread_list (ret))
         add_thread (ret);
-      }
     }
 
-out:
   return (ret);
 }
 
 static int
 fbsd_thread_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
-		       struct mem_attrib *attrib,
-		       struct target_ops *target)
+                        struct mem_attrib *attrib, struct target_ops *target)
 {
   return child_ops.to_xfer_memory (memaddr, myaddr, len, write,
            attrib, target);
 }
 
 static void
-fbsd_fetch_lwp_registers (int regno)
+fbsd_lwp_fetch_registers (int regno)
 {
   gregset_t gregs;
   lwpid_t lwp;
 
-  if (!is_lwp (inferior_ptid))
+  /* FIXME, is it possible ? */
+  if (!IS_LWP (inferior_ptid))
     {
       child_ops.to_fetch_registers (regno);
       return;
@@ -566,15 +461,13 @@
 
   lwp = GET_LWP (inferior_ptid);
 
-  if (ttrace (PT_GETREGS, GET_PID (inferior_ptid), lwp,
-              (caddr_t)&gregs, 0) == -1)
-     perror_with_name ("Couldn't get registers");
+  if (ptrace (PT_GETREGS, lwp, (caddr_t) &gregs, 0) == -1)
+    error ("Cannot get lwp %d registers: %s\n", lwp, safe_strerror (errno));
   supply_gregset (&gregs);
   
   fpregset_t fpregs;
-  if (ttrace (PT_GETFPREGS, GET_PID (inferior_ptid), lwp,
-              (caddr_t)&fpregs, 0) == -1)
-    perror_with_name ("Couldn't get floating point status");
+  if (ptrace (PT_GETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1)
+    error ("Cannot get lwp %d registers: %s\n ", lwp, safe_strerror (errno));
 
   supply_fpregset (&fpregs);
 }
@@ -583,96 +476,88 @@
 fbsd_thread_fetch_registers (int regno)
 {
   prgregset_t gregset;
-  gdb_prfpregset_t fpregset;
+  prfpregset_t fpregset;
   td_thrhandle_t th;
   td_err_e err;
-  td_thrinfo_t ti;
 
-  if (!is_thread (inferior_ptid))
+  if (!IS_THREAD (inferior_ptid))
     {
-      fbsd_fetch_lwp_registers (regno);
+      fbsd_lwp_fetch_registers (regno);
       return;
     }
 
   err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th);
   if (err != TD_OK)
     error ("Cannot find thread %ld: %s",
-	   (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
+           (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
 
   err = td_thr_getgregs_p (&th, gregset);
   if (err != TD_OK)
     error ("Cannot fetch general-purpose registers for thread %ld: %s",
-	   (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
+           (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
 
   err = td_thr_getfpregs_p (&th, &fpregset);
   if (err != TD_OK)
     error ("Cannot get floating-point registers for thread %ld: %s",
-	   (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
+           (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
 
-  /* Note that we must call supply_gregset after calling the thread_db
-     routines because the thread_db routines call ps_lgetgregs and
-     friends which clobber GDB's register cache.  */
-  supply_gregset ((gdb_gregset_t *) gregset);
+  supply_gregset (gregset);
   supply_fpregset (&fpregset);
-
-  err = td_thr_get_info_p (&th, &ti);
 }
 
 static void
-fbsd_store_lwp_registers (int regno)
+fbsd_lwp_store_registers (int regno)
 {
   lwpid_t lwp;
 
-  if (!is_lwp (inferior_ptid))
+  /* FIXME, is it possible ? */
+  if (!IS_LWP (inferior_ptid))
     {
       child_ops.to_store_registers (regno);
-      return;
+      return ;
     }
 
   lwp = GET_LWP (inferior_ptid);
 
   gregset_t gregs;
 
-  if (ttrace (PT_GETREGS, GET_PID (inferior_ptid), lwp,
-              (caddr_t)&gregs, 0) == -1)
-    perror_with_name ("Couldn't get registers");
+  if (ptrace (PT_GETREGS, lwp, (caddr_t) &gregs, 0) == -1)
+    error ("Cannot get lwp %d registers: %s\n", lwp, safe_strerror (errno));
 
   fill_gregset (&gregs, regno);
 
-  if (ttrace (PT_SETREGS, GET_PID (inferior_ptid), lwp,
-              (caddr_t)&gregs, 0) == -1)
-    perror_with_name ("Couldn't write registers");
+  if (ptrace (PT_SETREGS, lwp, (caddr_t) &gregs, 0) == -1)
+    error ("Cannot set lwp %d registers: %s\n", lwp, safe_strerror (errno));
 
   fpregset_t fpregs;
-  if (ttrace (PT_GETFPREGS, GET_PID (inferior_ptid), lwp,
-              (caddr_t) &fpregs, 0) == -1)
-     perror_with_name ("Couldn't get floating point status");
+  if (ptrace (PT_GETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1)
+    error ("Cannot get lwp %d float registers: %s\n", lwp,
+           safe_strerror (errno));
 
   fill_fpregset (&fpregs, regno);
-
-  if (ttrace (PT_SETFPREGS, GET_PID (inferior_ptid), lwp,
-              (caddr_t) &fpregs, 0) == -1)
-     perror_with_name ("Couldn't write floating point status");
+  if (ptrace (PT_SETFPREGS, lwp, (caddr_t) &fpregs, 0) == -1)
+     error ("Cannot set lwp %d float registers: %s\n", lwp,
+            safe_strerr (errno));
 }
 
 static void
 fbsd_thread_store_registers (int regno)
 {
   prgregset_t gregset;
-  gdb_prfpregset_t fpregset;
+  prfpregset_t fpregset;
   td_thrhandle_t th;
   td_err_e err;
 
-  if (!is_thread (inferior_ptid))
+  if (!IS_THREAD (inferior_ptid))
     {
-      fbsd_store_lwp_registers (regno);
+      fbsd_lwp_store_registers (regno);
       return;
     }
 
   err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (inferior_ptid), &th);
   if (err != TD_OK)
-    error ("Cannot find thread %ld: %s",
-	   (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
+    error ("Cannot find thread %lx: %s",
+           (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
 
   if (regno != -1)
     {
@@ -683,17 +568,17 @@
       supply_register (regno, raw);
     }
 
-  fill_gregset ((gdb_gregset_t *) gregset, -1);
+  fill_gregset (gregset, -1);
   fill_fpregset (&fpregset, -1);
 
   err = td_thr_setgregs_p (&th, gregset);
   if (err != TD_OK)
-    error ("Cannot store general-purpose registers for thread %ld: %s",
-	   (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
+    error ("Cannot store general-purpose registers for thread %lx: %s",
+           (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
   err = td_thr_setfpregs_p (&th, &fpregset);
   if (err != TD_OK)
-    error ("Cannot store floating-point registers  for thread %ld: %s",
-	   (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
+    error ("Cannot store floating-point registers for thread %lx: %s",
+           (long) GET_THREAD (inferior_ptid), thread_db_err_str (err));
 }
 
 static void
@@ -724,7 +609,7 @@
       proc_handle.pid = GET_PID (ptid);
 
       fbsd_thread_find_new_threads ();
-      find_current_thread ();
+      get_current_thread ();
     }
 }
 
@@ -746,32 +631,40 @@
   td_err_e err;
   gregset_t gregs;
 
-  if (is_thread (ptid))
+  if (IS_THREAD (ptid))
     {
       err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (ptid), &th);
       if (err != TD_OK)
-	return 0;
-
-      err = td_thr_validate_p (&th);
-      if (err != TD_OK)
-	return 0;
+        return 0;
 
       err = td_thr_get_info_p (&th, &ti);
       if (err != TD_OK)
-	return 0;
+        return 0;
 
+      /* A zombie thread.  */
       if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
-	return 0;		/* A zombie thread.  */
+        return 0;
 
       return 1;
     }
   else if (GET_LWP (ptid) == 0)
     {
+      /* we sometimes are called with lwp == 0 */
       return 1;
     }
-
-  return ttrace (PT_GETREGS, GET_PID(ptid), GET_LWP (ptid),
-          (caddr_t)&gregs, 0) == 0;
+  else
+   {
+      err = td_ta_map_lwp2thr_p (thread_agent, GET_LWP (ptid), &th);
+      /*
+       * if the lwp was already mapped to user thread, don't use it
+       * directly, please use user thread id instead.
+       */
+      if (err == TD_OK)
+        return 0;
+   }
+   
+  /* check lwp in kernel */
+  return ptrace (PT_GETREGS, GET_LWP (ptid), (caddr_t)&gregs, 0) == 0;
 }
 
 static int
@@ -785,15 +678,15 @@
   if (err != TD_OK)
     error ("Cannot get thread info: %s", thread_db_err_str (err));
 
+  /* Ignore zombie */
   if (ti.ti_state == TD_THR_UNKNOWN || ti.ti_state == TD_THR_ZOMBIE)
-    return 0;			/* A zombie -- ignore.  */
+    return 0;
 
   ptid = BUILD_THREAD (ti.ti_tid, GET_PID (inferior_ptid));
 
   if (!in_thread_list (ptid))
-    {
       add_thread (ptid);
-    }
+ 
   return 0;
 }
 
@@ -802,10 +695,10 @@
 {
   td_err_e err;
 
-  /* Iterate over all user-space threads to discover new threads.  */
+  /* Iterate over all user-space threads to discover new threads. */
   err = td_ta_thr_iter_p (thread_agent, find_new_threads_callback, NULL,
-			  TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
-			  TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
+          TD_THR_ANY_STATE, TD_THR_LOWEST_PRIORITY,
+          TD_SIGNO_MASK, TD_THR_ANY_USER_FLAGS);
   if (err != TD_OK)
     error ("Cannot find new threads: %s", thread_db_err_str (err));
 }
@@ -815,7 +708,7 @@
 {
   static char buf[64];
 
-  if (is_thread (ptid))
+  if (IS_THREAD (ptid))
     {
       td_thrhandle_t th;
       td_thrinfo_t ti;
@@ -823,54 +716,41 @@
 
       err = td_ta_map_id2thr_p (thread_agent, GET_THREAD (ptid), &th);
       if (err != TD_OK)
-	error ("Cannot find thread %lx: %s",
-	       (long) GET_THREAD (ptid), thread_db_err_str (err));
+        error ("Cannot find thread %lx: %s",
+                GET_THREAD (ptid), thread_db_err_str (err));
 
       err = td_thr_get_info_p (&th, &ti);
       if (err != TD_OK)
-	error ("Cannot get thread info for thread %lx: %s",
-	       (long) GET_THREAD (ptid), thread_db_err_str (err));
+        error ("Cannot get thread info for thread %lx: %s",
+               GET_THREAD (ptid), thread_db_err_str (err));
 
-      if (ti.ti_state == TD_THR_ACTIVE && ti.ti_lid != 0)
-	{
-	  snprintf (buf, sizeof (buf), "Thread %lx (LWP %x)",
-		    (long) ti.ti_tid, ti.ti_lid);
-	}
+      if (ti.ti_lid != 0)
+        {
+          snprintf (buf, sizeof (buf), "Thread %lx (LWP %d)",
+                    (long) ti.ti_tid, ti.ti_lid);
+        }
       else
-	{
-	  snprintf (buf, sizeof (buf), "Thread %lx (%s)",
-		    (long) ti.ti_tid, thread_db_state_str (ti.ti_state));
-	}
+       {
+         snprintf (buf, sizeof (buf), "Thread %lx (%s)",
+                   (long) ti.ti_tid, thread_db_state_str (ti.ti_state));
+       }
 
       return buf;
     }
-  else if (is_lwp (ptid))
+  else if (IS_LWP (ptid))
     {
-      snprintf (buf, sizeof (buf), "kernel thread %lx", GET_LWP (ptid));
+      snprintf (buf, sizeof (buf), "LWP %d", (int) GET_LWP (ptid));
       return buf;
     }
   return normal_pid_to_str (ptid);
 }
 
-ps_err_e
-ps_lsetstep (struct ps_prochandle *ph, lwpid_t lwp, int step)
-{
-  int req = step ? PT_SETSTEP : PT_CLEARSTEP;
-
-  if (ttrace(req, ph->pid, lwp, (caddr_t)1, 0))
-    {
-      perror_with_name ("PT_SETSTEP/PT_CLEARSTEP");
-      return PS_ERR;
-    }
-  return (0);
-}
-
 static int
 tsd_cb (thread_key_t key, void (*destructor)(void *), void *ignore)
 {
   struct minimal_symbol *ms;
   char *name;
-                                                                                                                             
+
   ms = lookup_minimal_symbol_by_pc ((CORE_ADDR)destructor);
   if (!ms)
     name = "???";
@@ -915,6 +795,83 @@
   thread_db_ops.to_magic = OPS_MAGIC;
 }
 
+static int
+thread_db_load (void)
+{
+  void *handle;
+  td_err_e err;
+
+  handle = dlopen (LIBTHREAD_DB_SO, RTLD_NOW);
+  if (handle == NULL)
+    return 0;
+
+  td_init_p = dlsym (handle, "td_init");
+  if (td_init_p == NULL)
+    return 0;
+
+  td_ta_new_p = dlsym (handle, "td_ta_new");
+  if (td_ta_new_p == NULL)
+    return 0;
+
+  td_ta_map_id2thr_p = dlsym (handle, "td_ta_map_id2thr");
+  if (td_ta_map_id2thr_p == NULL)
+    return 0;
+
+  td_ta_map_lwp2thr_p = dlsym (handle, "td_ta_map_lwp2thr");
+  if (td_ta_map_lwp2thr_p == NULL)
+    return 0;
+
+  td_ta_thr_iter_p = dlsym (handle, "td_ta_thr_iter");
+  if (td_ta_thr_iter_p == NULL)
+    return 0;
+
+  td_thr_validate_p = dlsym (handle, "td_thr_validate");
+  if (td_thr_validate_p == NULL)
+    return 0;
+
+  td_thr_get_info_p = dlsym (handle, "td_thr_get_info");
+  if (td_thr_get_info_p == NULL)
+    return 0;
+
+  td_thr_getfpregs_p = dlsym (handle, "td_thr_getfpregs");
+  if (td_thr_getfpregs_p == NULL)
+    return 0;
+
+  td_thr_getgregs_p = dlsym (handle, "td_thr_getgregs");
+  if (td_thr_getgregs_p == NULL)
+    return 0;
+
+  td_thr_setfpregs_p = dlsym (handle, "td_thr_setfpregs");
+  if (td_thr_setfpregs_p == NULL)
+    return 0;
+
+  td_thr_setgregs_p = dlsym (handle, "td_thr_setgregs");
+  if (td_thr_setgregs_p == NULL)
+    return 0;
+
+  td_thr_sstep_p = dlsym(handle, "td_thr_sstep");
+  if (td_thr_sstep_p == NULL)
+    return 0;
+
+  td_ta_activated_p = dlsym (handle, "td_ta_activated");
+  if (td_ta_activated_p == NULL)
+    return 0;
+
+  td_ta_tsd_iter_p = dlsym (handle, "td_ta_tsd_iter");
+  if (td_ta_tsd_iter_p == NULL)
+    return 0;
+
+  /* Initialize the library.  */
+  err = td_init_p ();
+  if (err != TD_OK)
+    {
+      warning ("Cannot initialize libthread_db: %s", thread_db_err_str (err));
+      return 0;
+    }
+
+  return 1;
+}
+
 void
 _initialize_thread_db (void)
 {
@@ -933,7 +890,108 @@
       /* Add ourselves to objfile event chain. */
       target_new_objfile_chain = target_new_objfile_hook;
       target_new_objfile_hook = fbsd_thread_new_objfile;
-    } else {
+    }
+  else
+   {
       printf_filtered("%s: can not load %s.\n", __func__, LIBTHREAD_DB_SO);
-    }
+   }
+}
+
+/* proc service functions */
+void
+ps_plog (const char *fmt, ...)
+{
+  va_list args;
+
+  va_start (args, fmt);
+  vfprintf_filtered (gdb_stderr, fmt, args);
+}
+
+ps_err_e
+ps_pglobal_lookup (struct ps_prochandle *ph, const char *obj,
+   const char *name, psaddr_t *sym_addr)
+{
+  struct minimal_symbol *ms;
+
+  ms = lookup_minimal_symbol (name, NULL, NULL);
+  if (ms == NULL)
+    return PS_NOSYM;
+
+  *sym_addr = (psaddr_t) SYMBOL_VALUE_ADDRESS (ms);
+  return PS_OK;
+}
+
+ps_err_e
+ps_pdread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t len)
+{
+  return target_read_memory ((CORE_ADDR) addr, buf, len);
+}
+
+ps_err_e
+ps_pdwrite (struct ps_prochandle *ph, psaddr_t addr, const void *buf,
+            size_t len)
+{
+  return target_write_memory ((CORE_ADDR) addr, (void *)buf, len);
+}
+
+ps_err_e
+ps_ptread (struct ps_prochandle *ph, psaddr_t addr, void *buf, size_t len)
+{
+  return target_read_memory ((CORE_ADDR) addr, buf, len);
+}
+
+ps_err_e
+ps_ptwrite (struct ps_prochandle *ph, psaddr_t addr, const void *buf,
+            size_t len)
+{
+  return target_write_memory ((CORE_ADDR) addr, (void *)buf, len);
+}
+
+ps_err_e
+ps_lgetregs (struct ps_prochandle *ph, lwpid_t lwpid, prgregset_t gregset)
+{
+  /* should check data modal, .core or process ? */
+  if (ptrace (PT_GETREGS, lwpid, (caddr_t)gregset, 0) == -1)
+    return PS_ERR;
+  return PS_OK;
+}
+
+ps_err_e
+ps_lsetregs (struct ps_prochandle *ph, lwpid_t lwpid, const prgregset_t gregset)
+{
+  if (ptrace (PT_SETREGS, lwpid, (caddr_t) gregset, 0) == -1)
+    return PS_ERR;
+  return PS_OK;
+}
+
+ps_err_e
+ps_lgetfpregs (struct ps_prochandle *ph, lwpid_t lwpid, prfpregset_t *fpregset)
+{
+  if (ptrace (PT_GETFPREGS, lwpid, (caddr_t) fpregset, 0) == -1)
+    return PS_ERR;
+  return PS_OK;
+}
+
+ps_err_e
+ps_lsetfpregs (struct ps_prochandle *ph, lwpid_t lwpid,
+               const prfpregset_t *fpregset)
+{
+  if (ptrace (PT_SETFPREGS, lwpid, (caddr_t) fpregset, 0) == -1)
+    return PS_ERR;
+  return PS_OK;
+}
+
+pid_t
+ps_getpid (struct ps_prochandle *ph)
+{
+  return ph->pid;
+}
+
+ps_err_e
+ps_lsetstep (struct ps_prochandle *ph, lwpid_t lwp, int step)
+{
+  if (ptrace ((step ? PT_SETSTEP : PT_CLEARSTEP), lwp, 0, 0))
+      return PS_ERR;
+  return PS_OK;
 }
+


More information about the p4-projects mailing list