2002-12-13 Jeff Johnston <jjohnstn@redhat.com>
[deliverable/binutils-gdb.git] / gdb / infttrace.c
index 55164594b3e553abdec4a11ca15bbced808bfd08..589a0e237bcab562ac252c6b5e9d6a8456f3e529 100644 (file)
 #include "gdb_wait.h"
 #include "command.h"
 
+/* We need pstat functionality so that we can get the exec file
+   for a process we attach to.
+
+   According to HP, we should use the 64bit interfaces, so we
+   define _PSTAT64 to achieve this.  */
+#define _PSTAT64
+#include <sys/pstat.h>
+
 /* Some hackery to work around a use of the #define name NO_FLAGS
  * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
  */
@@ -2281,7 +2289,7 @@ call_ttrace_wait (int pid, ttwopt_t option, ttstate_t *tsp, size_t tsp_size)
      thread descriptor.
 
      This caches the state.  The implementation of queries like
-     target_has_execd can then use this cached state, rather than
+     hpux_has_execd can then use this cached state, rather than
      be forced to make an explicit ttrace call to get it.
 
      (Guard against the condition that this is the first time we've
@@ -2596,7 +2604,7 @@ count_unhandled_events (int real_pid, lwpid_t real_tid)
  *
  * Note: used by core gdb and so uses the pseudo-pid (really tid).
  */
-ptid_t
+int
 ptrace_wait (ptid_t ptid, int *status)
 {
   ttstate_t tsp;
@@ -2621,13 +2629,13 @@ ptrace_wait (ptid_t ptid, int *status)
       if (errno == ESRCH)
        {
          *status = 0;          /* WIFEXITED */
-         return inferior_ptid;
+         return PIDGET (inferior_ptid);
        }
 
       warning ("Call of ttrace_wait returned with errno %d.",
               errno);
       *status = ttwait_return;
-      return inferior_ptid;
+      return PIDGET (inferior_ptid);
     }
 
   real_pid = tsp.tts_pid;
@@ -2888,7 +2896,7 @@ ptrace_wait (ptid_t ptid, int *status)
       *status = _SIGTRAP;
     }
 
-  target_post_wait (tsp.tts_pid, *status);
+  target_post_wait (pid_to_ptid (tsp.tts_pid), *status);
 
 
 #ifdef THREAD_DEBUG
@@ -2914,7 +2922,7 @@ ptrace_wait (ptid_t ptid, int *status)
       warning ("Internal error: process-wait failed.");
     }
 
-  return pid_to_ptid (return_pid);
+  return return_pid;
 }
 \f
 
@@ -3123,7 +3131,7 @@ child_acknowledge_created_inferior (int pid)
    *    the process safely to ask what it is.  Anyway, we'll
    *    add it when it gets the EXEC event.
    */
-  add_thread (pid);            /* in thread.c */
+  add_thread (pid_to_ptid (pid));              /* in thread.c */
 
   /* We can now set the child's ttrace event mask.
    */
@@ -3349,8 +3357,6 @@ child_remove_vfork_catchpoint (int tid)
 }
 #endif
 
-#if defined(CHILD_HAS_FORKED)
-
 /* Q: Do we need to map the returned process ID to a thread ID?
 
  * A: I don't think so--here we want a _real_ pid.  Any later
@@ -3358,7 +3364,7 @@ child_remove_vfork_catchpoint (int tid)
  *    start the mapping.
  */
 int
-child_has_forked (int tid, int *childpid)
+hpux_has_forked (int tid, int *childpid)
 {
   int tt_status;
   ttstate_t ttrace_state;
@@ -3395,15 +3401,11 @@ child_has_forked (int tid, int *childpid)
 
   return 0;
 }
-#endif
-
-
-#if defined(CHILD_HAS_VFORKED)
 
-/* See child_has_forked for pid discussion.
+/* See hpux_has_forked for pid discussion.
  */
 int
-child_has_vforked (int tid, int *childpid)
+hpux_has_vforked (int tid, int *childpid)
 {
   int tt_status;
   ttstate_t ttrace_state;
@@ -3438,22 +3440,6 @@ child_has_vforked (int tid, int *childpid)
 
   return 0;
 }
-#endif
-
-
-#if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
-int
-child_can_follow_vfork_prior_to_exec (void)
-{
-  /* ttrace does allow this.
-
-     ??rehrauer: However, I had major-league problems trying to
-     convince wait_for_inferior to handle that case.  Perhaps when
-     it is rewritten to grok multiple processes in an explicit way...
-   */
-  return 0;
-}
-#endif
 
 
 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
@@ -3482,9 +3468,8 @@ child_remove_exec_catchpoint (int tid)
 #endif
 
 
-#if defined(CHILD_HAS_EXECD)
 int
-child_has_execd (int tid, char **execd_pathname)
+hpux_has_execd (int tid, char **execd_pathname)
 {
   int tt_status;
   ttstate_t ttrace_state;
@@ -3523,12 +3508,10 @@ child_has_execd (int tid, char **execd_pathname)
 
   return 0;
 }
-#endif
 
 
-#if defined(CHILD_HAS_SYSCALL_EVENT)
 int
-child_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
+hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
 {
   int tt_status;
   ttstate_t ttrace_state;
@@ -3568,7 +3551,6 @@ child_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
   *syscall_id = ttrace_state.tts_scno;
   return 1;
 }
-#endif
 \f
 
 
@@ -3853,27 +3835,18 @@ kill_inferior (void)
 
       if (t->am_pseudo && (t->pid != PIDGET (inferior_ptid)))
        {
-         /* TT_PROC_STOP doesn't require a subsequent ttrace_wait, as it
-          * generates no event.
-          */
-         call_ttrace (TT_PROC_STOP,
+         call_ttrace (TT_PROC_EXIT,
                       t->pid,
                       TT_NIL,
                       TT_NIL,
                       TT_NIL);
-
-         call_ttrace (TT_PROC_DETACH,
-                      t->pid,
-                      TT_NIL,
-                      (TTRACE_ARG_TYPE) TARGET_SIGNAL_0,
-                      TT_NIL);
        }
       t = t->next;
     }
 
   xfree (paranoia);
 
-  call_ttrace (TT_PROC_STOP,
+  call_ttrace (TT_PROC_EXIT,
               PIDGET (inferior_ptid),
               TT_NIL,
               TT_NIL,
@@ -4540,98 +4513,49 @@ child_resume (ptid_t ptid, int step, enum target_signal signal)
 
   else
     {
-      /* TT_LWP_CONTINUE can pass signals to threads,
-       * TT_PROC_CONTINUE can't.  So if there are any
-       * signals to pass, we have to use the (slower)
-       * loop over the stopped threads.
-       *
-       * Equally, if we have to not continue some threads,
-       * due to saved events, we have to use the loop.
-       */
-      if ((signal != 0) || saved_signals_exist ())
+      /* TT_LWP_CONTINUE can pass signals to threads, TT_PROC_CONTINUE can't.
+        Therefore, we really can't use TT_PROC_CONTINUE here.
+
+        Consider a process which stopped due to signal which gdb decides
+        to handle and not pass on to the inferior.  In that case we must
+        clear the pending signal by restarting the inferior using
+        TT_LWP_CONTINUE and pass zero as the signal number.  Else the
+        pending signal will be passed to the inferior.  interrupt.exp
+        in the testsuite does this precise thing and fails due to the
+        unwanted signal delivery to the inferior.  */
+      /* drow/2002-12-05: However, note that we must use TT_PROC_CONTINUE
+        if we are tracing a vfork.  */
+      if (vfork_in_flight)
+       {
+         call_ttrace (TT_PROC_CONTINUE, tid, TT_NIL, TT_NIL, TT_NIL);
+         clear_all_handled ();
+         clear_all_stepping_mode ();
+       }
+      else if (resume_all_threads)
        {
-         if (resume_all_threads)
-           {
-
-#ifdef THREAD_DEBUG
-             if (debug_on)
-               printf ("Doing a continue by loop of all threads\n");
-#endif
-
-             threads_continue_all_with_signals (tid, signal);
-
-             clear_all_handled ();
-             clear_all_stepping_mode ();
-           }
-
-         else
-           {
 #ifdef THREAD_DEBUG
-             printf ("Doing a continue w/signal of just thread %d\n", tid);
+         if (debug_on)
+           printf ("Doing a continue by loop of all threads\n");
 #endif
 
-             threads_continue_one_with_signal (tid, signal);
+         threads_continue_all_with_signals (tid, signal);
 
-             /* Clear the "handled" state of this thread, because
-              * we'll soon get a new event for it.  Other events
-              * can stay as they were.
-              */
-             clear_handled (tid);
-             clear_stepping_mode (tid);
-           }
+         clear_all_handled ();
+         clear_all_stepping_mode ();
        }
-
       else
        {
-         /* No signals to send.
-          */
-         if (resume_all_threads)
-           {
-#ifdef THREAD_DEBUG
-             if (debug_on)
-               printf ("Doing a continue by process of process %d\n", tid);
-#endif
-
-             if (more_events_left > 0)
-               {
-                 warning ("Losing buffered events on continue.");
-                 more_events_left = 0;
-               }
-
-             call_ttrace (TT_PROC_CONTINUE,
-                          tid,
-                          TT_NIL,
-                          TT_NIL,
-                          TT_NIL);
-
-             clear_all_handled ();
-             clear_all_stepping_mode ();
-           }
-
-         else
-           {
 #ifdef THREAD_DEBUG
-             if (debug_on)
-               {
-                 printf ("Doing a continue of just thread %d\n", tid);
-                 if (is_terminated (tid))
-                   printf ("Why are we continuing a dead thread? (5)\n");
-               }
+         printf ("Doing a continue w/signal of just thread %d\n", tid);
 #endif
 
-             call_ttrace (TT_LWP_CONTINUE,
-                          tid,
-                          TT_NIL,
-                          TT_NIL,
-                          TT_NIL);
+         threads_continue_one_with_signal (tid, signal);
 
-             /* Clear the "handled" state of this thread, because
-              * we'll soon get a new event for it.  Other events
-              * can stay as they were.
-              */
-             clear_handled (tid);
-             clear_stepping_mode (tid);
-           }
+         /* Clear the "handled" state of this thread, because we
+            will soon get a new event for it.  Other events can
+            stay as they were.  */
+         clear_handled (tid);
+         clear_stepping_mode (tid);
        }
     }
 
@@ -4773,7 +4697,7 @@ update_thread_state_after_attach (int pid, attach_continue_t kind_of_go)
            }
        }
 
-      add_thread (tid);                /* in thread.c */
+      add_thread (pid_to_ptid (pid));          /* in thread.c */
     }
 
 #ifdef PARANOIA
@@ -4931,8 +4855,11 @@ child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
   = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1)
   / sizeof (TTRACE_XFER_TYPE);
   /* Allocate buffer of that many longwords.  */
+  /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe
+     because it uses alloca to allocate a buffer of arbitrary size.
+     For very large xfers, this could crash GDB's stack.  */
   register TTRACE_XFER_TYPE *buffer
-  = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));
+    = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));
 
   if (write)
     {
@@ -5065,80 +4992,44 @@ udot_info (void)
 }
 #endif /* !defined (CHILD_XFER_MEMORY).  */
 
+
 /* TTrace version of "target_pid_to_exec_file"
  */
 char *
 child_pid_to_exec_file (int tid)
 {
-  static char exec_file_buffer[1024];
   int tt_status;
-  CORE_ADDR top_of_stack;
-  char four_chars[4];
-  int name_index;
-  int i;
-  int done;
-  ptid_t saved_inferior_ptid;
+  static char exec_file_buffer[1024];
+  pid_t pid;
+  static struct pst_status buf;
 
-  /* As of 10.x HP-UX, there's an explicit request to get the
-   *pathname.
-   */
+  /* On various versions of hpux11, this may fail due to a supposed
+     kernel bug.  We have alternate methods to get this information
+     (ie pstat).  */
   tt_status = call_ttrace (TT_PROC_GET_PATHNAME,
                           tid,
-                          (TTRACE_ARG_TYPE) exec_file_buffer,
-                          (TTRACE_ARG_TYPE) sizeof (exec_file_buffer) - 1,
-                          TT_NIL);
+                          (uint64_t) exec_file_buffer,
+                          sizeof (exec_file_buffer) - 1,
+                          0);
   if (tt_status >= 0)
     return exec_file_buffer;
 
-  /* ??rehrauer: The above request may or may not be broken.  It
-     doesn't seem to work when I use it.  But, it may be designed
-     to only work immediately after an exec event occurs.  (I'm
-     waiting for COSL to explain.)
-
-     In any case, if it fails, try a really, truly amazingly gross
-     hack that DDE uses, of pawing through the process' data
-     segment to find the pathname.
-   */
-  top_of_stack = (TARGET_PTR_BIT == 64 ? 0x800003ffff7f0000 : 0x7b03a000);
-  name_index = 0;
-  done = 0;
-
-  /* On the chance that pid != inferior_ptid, set inferior_ptid
-     to pid, so that (grrrr!) implicit uses of inferior_ptid get
-     the right id.
-   */
-  saved_inferior_ptid = inferior_ptid;
-  inferior_ptid = pid_to_ptid (tid);
-
-  /* Try to grab a null-terminated string. */
-  while (!done)
+  /* Try to get process information via pstat and extract the filename
+     from the pst_cmd field within the pst_status structure.  */
+  if (pstat_getproc (&buf, sizeof (struct pst_status), 0, tid) != -1)
     {
-      if (target_read_memory (top_of_stack, four_chars, 4) != 0)
-       {
-         inferior_ptid = saved_inferior_ptid;
-         return NULL;
-       }
-      for (i = 0; i < 4; i++)
-       {
-         exec_file_buffer[name_index++] = four_chars[i];
-         done = (four_chars[i] == '\0');
-         if (done)
-           break;
-       }
-      top_of_stack += 4;
-    }
+      char *p = buf.pst_cmd;
 
-  if (exec_file_buffer[0] == '\0')
-    {
-      inferior_ptid = saved_inferior_ptid;
-      return NULL;
+      while (*p && *p != ' ')
+       p++;
+      *p = 0;
+
+      return (buf.pst_cmd);
     }
 
-  inferior_ptid = saved_inferior_ptid;
-  return exec_file_buffer;
+  return (NULL);
 }
 
-
 void
 pre_fork_inferior (void)
 {
@@ -5159,9 +5050,7 @@ pre_fork_inferior (void)
     }
 }
 
-/* Called via #define REQUIRE_ATTACH from inftarg.c,
- * ultimately from "follow_inferior_fork" in infrun.c,
- * itself called from "resume".
+/* Called from child_follow_fork in hppah-nat.c.
  *
  * This seems to be intended to attach after a fork or
  * vfork, while "attach" is used to attach to a pid
This page took 0.029892 seconds and 4 git commands to generate.