Replace ../include/wait.h with gdb_wait.h.
[deliverable/binutils-gdb.git] / gdb / procfs.c
index 67d1c3ccd168f4ababfea0d59e4447cb70d63665..920019fae17595cff691b724baacf5ed522e2e6a 100644 (file)
@@ -24,6 +24,7 @@ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #include "target.h"
 #include "gdbcore.h"
 #include "gdbcmd.h"
+#include "gdbthread.h"
 
 #if defined (NEW_PROC_API)
 #define _STRUCTURED_PROC 1     /* Should be done by configure script. */
@@ -33,6 +34,11 @@ Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #include <sys/fault.h>
 #include <sys/syscall.h>
 #include <sys/errno.h>
+#include <sys/wait.h>
+#include <signal.h>
+#include <ctype.h>
+
+#include "proc-utils.h"
 
 /* 
  * PROCFS.C
@@ -170,11 +176,7 @@ init_procfs_ops ()
 #ifdef NEW_PROC_API
 extern  int   write_with_trace PARAMS ((int, void *, size_t, char *, int));
 extern  off_t lseek_with_trace PARAMS ((int, off_t,  int,    char *, int));
-
-#if 0
 #define write(X,Y,Z)   write_with_trace (X, Y, Z, __FILE__, __LINE__)
-#endif
-
 #define lseek(X,Y,Z)   lseek_with_trace (X, Y, Z, __FILE__, __LINE__)
 #else
 extern  int ioctl_with_trace PARAMS ((int, long, void *, char *, int));
@@ -183,26 +185,14 @@ extern  int ioctl_with_trace PARAMS ((int, long, void *, char *, int));
 #define open(X,Y)      open_with_trace  (X, Y,    __FILE__, __LINE__)
 #define close(X)       close_with_trace (X,       __FILE__, __LINE__)
 #define wait(X)        wait_with_trace  (X,       __FILE__, __LINE__)
-
-#if 0
 #define PROCFS_NOTE(X) procfs_note      (X,       __FILE__, __LINE__)
 #define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T) \
 proc_prettyfprint_status (X, Y, Z, T)
-#else 
-#define PROCFS_NOTE(X)
-#define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T)
-#endif
-
 #else
 #define PROCFS_NOTE(X)
 #define PROC_PRETTYFPRINT_STATUS(X,Y,Z,T)
 #endif
 
-     /* temp: */
-#undef  PROCFS_NOTE
-#define PROCFS_NOTE(X)
-     /* suppress */
-
 
 /*
  * World Unification:
@@ -297,20 +287,13 @@ typedef prstatus_t gdb_lwpstatus_t;
   typedef fpregset_t gdb_fpregset_t;
 #endif
 
-/* The PIDs that we pass to and from GDB will be composed from
-   the actual PID plus the LWPID.  These macros will be used to
-   compose and decompose them.  */
+/* Provide default composite pid manipulation macros for systems that
+   don't have threads. */
 
 #ifndef PIDGET
-#define PIDGET(PID)            (((PID) & 0xffff))
-#endif
-
-#ifndef TIDGET
-#define TIDGET(PID)            (((PID) & 0x7fffffff) >> 16)
-#endif
-
-#ifndef MERGEPID
-#define MERGEPID(PID, TID)     (((PID) & 0xffff) | ((TID) << 16))
+#define PIDGET(PID)            (PID)
+#define TIDGET(PID)            (PID)
+#define MERGEPID(PID, TID)     (PID)
 #endif
 
 typedef struct procinfo {
@@ -426,11 +409,13 @@ find_procinfo_or_die (pid, tid)
   procinfo *pi = find_procinfo (pid, tid);
 
   if (pi == NULL)
-    if (tid)
-      error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.", 
-            pid, tid);
-    else
-      error ("procfs: couldn't find pid %d in procinfo list.", pid);
+    {
+      if (tid)
+       error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.", 
+              pid, tid);
+      else
+       error ("procfs: couldn't find pid %d in procinfo list.", pid);
+    }
   return pi;
 }
 
@@ -454,7 +439,9 @@ open_procinfo_files (pi, which)
      procinfo *pi;
      int       which;
 {
+#ifdef NEW_PROC_API
   char tmp[MAX_PROC_NAME_SIZE];
+#endif
   int  fd;
 
   /* 
@@ -611,7 +598,7 @@ create_procinfo (pid, tid)
 {
   procinfo *pi, *parent;
 
-  if (pi = find_procinfo (pid, tid))
+  if ((pi = find_procinfo (pid, tid)))
     return pi;                 /* Already exists, nothing to do. */
 
   /* find parent before doing malloc, to save having to cleanup */
@@ -959,7 +946,14 @@ proc_flags (pi)
       return 0;        /* FIXME: not a good failure value (but what is?) */
 
 #ifdef NEW_PROC_API
+# ifdef UNIXWARE
+  /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
+     pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
+     The two sets of flags don't overlap. */
+  return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags;
+# else
   return pi->prstatus.pr_lwp.pr_flags;
+# endif
 #else
   return pi->prstatus.pr_flags;
 #endif
@@ -1093,7 +1087,7 @@ proc_cursig (struct procinfo *pi)
 }
 
 /*
- * Function: proc_modify_flags 
+ * Function: proc_modify_flag 
  *
  *  === I appologize for the messiness of this function. 
  *  === This is an area where the different versions of
@@ -1102,6 +1096,7 @@ proc_cursig (struct procinfo *pi)
  * Set or reset any of the following process flags:
  *    PR_FORK  -- forked child will inherit trace flags
  *    PR_RLC   -- traced process runs when last /proc file closed.
+ *    PR_KLC    -- traced process is killed when last /proc file closed.
  *    PR_ASYNC -- LWP's get to run/stop independently.
  *
  * There are three methods for doing this function:
@@ -1113,7 +1108,8 @@ proc_cursig (struct procinfo *pi)
  *    [OSF, Sol5]
  *
  * Note: Irix does not define PR_ASYNC.
- * Note: OSF is the only one that can ONLY use the oldest method.
+ * Note: OSF  does not define PR_KLC.
+ * Note: OSF  is the only one that can ONLY use the oldest method.
  *
  * Arguments: 
  *    pi   -- the procinfo
@@ -1217,6 +1213,9 @@ proc_modify_flag (pi, flag, mode)
             flag == PR_RLC   ? "PR_RLC"   :
 #ifdef PR_ASYNC
             flag == PR_ASYNC ? "PR_ASYNC" :
+#endif
+#ifdef PR_KLC
+            flag == PR_KLC   ? "PR_KLC"   :
 #endif
             "<unknown flag>",
             mode == FLAG_RESET ? "off" : "on");
@@ -1258,6 +1257,42 @@ proc_unset_run_on_last_close (pi)
   return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
 }
 
+#ifdef PR_KLC
+/*
+ * Function: proc_set_kill_on_last_close
+ *
+ * Set the kill_on_last_close flag.
+ * Process with all threads will be killed when debugger
+ * closes all /proc fds (or debugger exits or dies).
+ *
+ * Returns non-zero for success, zero for failure.
+ */
+
+int
+proc_set_kill_on_last_close (pi)
+     procinfo *pi;
+{
+  return proc_modify_flag (pi, PR_KLC, FLAG_SET);
+}
+
+/*
+ * Function: proc_unset_kill_on_last_close
+ *
+ * Reset the kill_on_last_close flag.
+ * Process will NOT be killed when debugger 
+ * closes its file handles (or exits or dies).
+ *
+ * Returns non-zero for success, zero for failure.
+ */
+
+int
+proc_unset_kill_on_last_close (pi)
+     procinfo *pi;
+{
+  return proc_modify_flag (pi, PR_KLC, FLAG_RESET);
+}
+#endif /* PR_KLC */
+
 /*
  * Function: proc_set_inherit_on_fork
  *
@@ -2601,10 +2636,15 @@ proc_iterate_over_mappings (func)
 {
   struct prmap *map;
   procinfo *pi;
-  int nmaps = 0, i;
-  int funcstat = 0;
-  int fd, map_fd;
+#ifndef NEW_PROC_API   /* avoid compiler warning */
+  int nmaps = 0;
+  int i;
+#else
+  int map_fd;
   char pathname[MAX_PROC_NAME_SIZE];
+#endif
+  int funcstat = 0;
+  int fd;
 
   pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);
 
@@ -3390,7 +3430,6 @@ do_detach (signo)
      int signo;
 {
   procinfo *pi;
-  long      pflags;
 
   /* Find procinfo for the main process */
   pi = find_procinfo_or_die (PIDGET (inferior_pid), 0);        /* FIXME: threads */
@@ -3650,18 +3689,6 @@ wait_again:
          why   = proc_why (pi);
          what  = proc_what (pi);
 
-#if 0
-         {
-           int stopped_pc = read_pc ();
-           if (stopped_pc != 0x10c68 &&
-               stopped_pc != 0x10c6c &&
-               stopped_pc != 0x10c70 &&
-               stopped_pc != 0x22768 &&
-               stopped_pc != 0x10c74)
-             printf ("%x,%d,%d\n", stopped_pc, why, what);
-         }
-#endif
-
          if (flags & (PR_STOPPED | PR_ISTOP))
            {
 #ifdef PR_ASYNC
@@ -3748,9 +3775,9 @@ wait_again:
                    if ((nsysargs = proc_nsysarg (pi)) > 0 &&
                        (sysargs  = proc_sysargs (pi)) != NULL)
                      {
-                       printf_filtered ("%d syscall arguments:\n", nsysargs);
+                       printf_filtered ("%ld syscall arguments:\n", nsysargs);
                        for (i = 0; i < nsysargs; i++)
-                         printf_filtered ("#%d: 0x%08x\n", 
+                         printf_filtered ("#%ld: 0x%08x\n", 
                                           i, sysargs[i]);
                      }
 
@@ -3862,9 +3889,9 @@ wait_again:
                    if ((nsysargs = proc_nsysarg (pi)) > 0 &&
                        (sysargs  = proc_sysargs (pi)) != NULL)
                      {
-                       printf_filtered ("%d syscall arguments:\n", nsysargs);
+                       printf_filtered ("%ld syscall arguments:\n", nsysargs);
                        for (i = 0; i < nsysargs; i++)
-                         printf_filtered ("#%d: 0x%08x\n", 
+                         printf_filtered ("#%ld: 0x%08x\n", 
                                           i, sysargs[i]);
                      }
                  }
@@ -3895,7 +3922,7 @@ wait_again:
                    temp = MERGEPID (pi->pid, temp);
                    if (!in_thread_list (temp))
                      {
-                       printf_filtered ("[*New %s]\n", 
+                       printf_filtered ("[New %s]\n", 
                                         target_pid_to_str (temp));
                        add_thread (temp);
                      }
@@ -4127,6 +4154,7 @@ invalidate_cache (parent, pi, ptr)
   return 0;
 }
 
+#if 0
 /*
  * Function: make_signal_thread_runnable
  *
@@ -4151,6 +4179,7 @@ make_signal_thread_runnable (process, pi, ptr)
 #endif
   return 0;
 }
+#endif
 
 /*
  * Function: target_resume
@@ -4202,7 +4231,7 @@ procfs_resume (pid, step, signo)
 
   /* Convert signal to host numbering.  */
   if (signo == 0 ||
-      signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop)
+      (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
     native_signo = 0;
   else
     native_signo = target_signal_to_host (signo);
@@ -4427,9 +4456,9 @@ unconditionally_kill_inferior (pi)
     /* FIXME: should we use waitpid to make sure we get the right event?  
        Should we check the returned event?  */
     {
+#if 0
       int status, ret;
 
-#if 0
       ret = waitpid (pi->pid, &status, 0);
 #else
       wait (NULL);
@@ -4547,6 +4576,10 @@ procfs_init_inferior (pid)
   if ((fail = procfs_debug_inferior (pi)) != 0)
     proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
 
+  /* FIXME: logically, we should really be turning OFF run-on-last-close,
+     and possibly even turning ON kill-on-last-close at this point.  But
+     I can't make that change without careful testing which I don't have
+     time to do right now...  */
   /* Turn on run-on-last-close flag so that the child
      will die if GDB goes away for some reason.  */
   if (!proc_set_run_on_last_close (pi))
@@ -4561,7 +4594,7 @@ procfs_init_inferior (pid)
 #else
   /* One trap to exec the shell, one to exec the program being debugged.  */
   startup_inferior (2);
-#endif
+#endif /* START_INFERIOR_TRAPS_EXPECTED */
 }
 
 /*
@@ -4585,7 +4618,6 @@ procfs_set_exec_trap ()
 
   procinfo *pi;
   sysset_t exitset;
-  sysset_t entryset;
 
   if ((pi = create_procinfo (getpid (), 0)) == NULL)
     perror_with_name ("procfs: create_procinfo failed in child.");
@@ -5009,11 +5041,12 @@ info_proc_cmd (args, from_tty)
 
   old_chain = make_cleanup (null_cleanup, 0);
   if (args)
-    if ((argv = buildargv (args)) == NULL)
-      nomem (0);
-    else
-      make_cleanup ((make_cleanup_func) freeargv, argv);
-
+    {
+      if ((argv = buildargv (args)) == NULL)
+       nomem (0);
+      else
+       make_cleanup ((make_cleanup_func) freeargv, argv);
+    }
   while (argv != NULL && *argv != NULL)
     {
       if (isdigit (argv[0][0]))
This page took 0.028315 seconds and 4 git commands to generate.