Print this page
3946 ::gcore
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>


   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  *
  26  * Portions Copyright 2007 Chad Mynhier
  27  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.

  28  */
  29 
  30 #include <assert.h>
  31 #include <stdio.h>
  32 #include <stdlib.h>
  33 #include <unistd.h>
  34 #include <ctype.h>
  35 #include <fcntl.h>
  36 #include <string.h>
  37 #include <strings.h>
  38 #include <memory.h>
  39 #include <errno.h>
  40 #include <dirent.h>
  41 #include <limits.h>
  42 #include <signal.h>
  43 #include <atomic.h>

  44 #include <sys/types.h>
  45 #include <sys/uio.h>
  46 #include <sys/stat.h>
  47 #include <sys/resource.h>
  48 #include <sys/param.h>
  49 #include <sys/stack.h>
  50 #include <sys/fault.h>
  51 #include <sys/syscall.h>
  52 #include <sys/sysmacros.h>

  53 
  54 #include "libproc.h"
  55 #include "Pcontrol.h"
  56 #include "Putil.h"
  57 #include "P32ton.h"
  58 
  59 int     _libproc_debug;         /* set non-zero to enable debugging printfs */
  60 int     _libproc_no_qsort;      /* set non-zero to inhibit sorting */
  61                                 /* of symbol tables */
  62 int     _libproc_incore_elf;    /* only use in-core elf data */
  63 
  64 sigset_t blockable_sigs;        /* signals to block when we need to be safe */
  65 static  int     minfd;  /* minimum file descriptor returned by dupfd(fd, 0) */
  66 char    procfs_path[PATH_MAX] = "/proc";
  67 
  68 /*
  69  * Function prototypes for static routines in this module.
  70  */
  71 static  void    deadcheck(struct ps_prochandle *);
  72 static  void    restore_tracing_flags(struct ps_prochandle *);
  73 static  void    Lfree_internal(struct ps_prochandle *, struct ps_lwphandle *);

  74 
  75 /*
  76  * Read/write interface for live processes: just pread/pwrite the
  77  * /proc/<pid>/as file:
  78  */
  79 

  80 static ssize_t
  81 Pread_live(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr)

  82 {
  83         return (pread(P->asfd, buf, n, (off_t)addr));
  84 }
  85 

  86 static ssize_t
  87 Pwrite_live(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr)

  88 {
  89         return (pwrite(P->asfd, buf, n, (off_t)addr));
  90 }
  91 
  92 static const ps_rwops_t P_live_ops = { Pread_live, Pwrite_live };









  93 





































































































































  94 /*


























































































  95  * This is the library's .init handler.
  96  */
  97 #pragma init(_libproc_init)
  98 void
  99 _libproc_init(void)
 100 {
 101         _libproc_debug = getenv("LIBPROC_DEBUG") != NULL;
 102         _libproc_no_qsort = getenv("LIBPROC_NO_QSORT") != NULL;
 103         _libproc_incore_elf = getenv("LIBPROC_INCORE_ELF") != NULL;
 104 
 105         (void) sigfillset(&blockable_sigs);
 106         (void) sigdelset(&blockable_sigs, SIGKILL);
 107         (void) sigdelset(&blockable_sigs, SIGSTOP);
 108 }
 109 
 110 void
 111 Pset_procfs_path(const char *path)
 112 {
 113         (void) snprintf(procfs_path, sizeof (procfs_path), "%s", path);
 114 }


 232                 if (envp)
 233                         environ = (char **)envp;
 234 
 235                 (void) execvp(file, argv);  /* execute the program */
 236                 _exit(127);
 237         }
 238 
 239         /*
 240          * Initialize the process structure.
 241          */
 242         (void) memset(P, 0, sizeof (*P));
 243         (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
 244         P->flags |= CREATED;
 245         P->state = PS_RUN;
 246         P->pid = pid;
 247         P->asfd = -1;
 248         P->ctlfd = -1;
 249         P->statfd = -1;
 250         P->agentctlfd = -1;
 251         P->agentstatfd = -1;
 252         P->ops = &P_live_ops;
 253         Pinitsym(P);
 254 
 255         /*
 256          * Open the /proc/pid files.
 257          */
 258         (void) snprintf(procname, sizeof (procname), "%s/%d/",
 259             procfs_path, (int)pid);
 260         fname = procname + strlen(procname);
 261         (void) set_minfd();
 262 
 263         /*
 264          * Exclusive write open advises others not to interfere.
 265          * There is no reason for any of these open()s to fail.
 266          */
 267         (void) strcpy(fname, "as");
 268         if ((fd = open(procname, (O_RDWR|O_EXCL))) < 0 ||
 269             (fd = dupfd(fd, 0)) < 0) {
 270                 dprintf("Pcreate: failed to open %s: %s\n",
 271                     procname, strerror(errno));
 272                 rc = C_STRANGE;


 535         }
 536 
 537         P->asfd = -1;
 538         P->ctlfd = -1;
 539         P->statfd = -1;
 540 
 541 again:  /* Come back here if we lose it in the Window of Vulnerability */
 542         if (P->ctlfd >= 0)
 543                 (void) close(P->ctlfd);
 544         if (P->asfd >= 0)
 545                 (void) close(P->asfd);
 546         if (P->statfd >= 0)
 547                 (void) close(P->statfd);
 548         (void) memset(P, 0, sizeof (*P));
 549         (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
 550         P->ctlfd = -1;
 551         P->asfd = -1;
 552         P->statfd = -1;
 553         P->agentctlfd = -1;
 554         P->agentstatfd = -1;
 555         P->ops = &P_live_ops;
 556         Pinitsym(P);
 557 
 558         /*
 559          * Open the /proc/pid files
 560          */
 561         (void) snprintf(procname, sizeof (procname), "%s/%d/",
 562             procfs_path, (int)pid);
 563         fname = procname + strlen(procname);
 564         (void) set_minfd();
 565 
 566         /*
 567          * Request exclusive open to avoid grabbing someone else's
 568          * process and to prevent others from interfering afterwards.
 569          * If this fails and the 'PGRAB_FORCE' flag is set, attempt to
 570          * open non-exclusively.
 571          */
 572         (void) strcpy(fname, "as");
 573         omode = (flags & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
 574 
 575         if (((fd = open(procname, omode | O_EXCL)) < 0 &&


 916         case G_NOFD:
 917                 str = "too many open files";
 918                 break;
 919         default:
 920                 str = "unknown error";
 921                 break;
 922         }
 923 
 924         return (str);
 925 }
 926 
 927 /*
 928  * Free a process control structure.
 929  * Close the file descriptors but don't do the Prelease logic.
 930  */
 931 void
 932 Pfree(struct ps_prochandle *P)
 933 {
 934         uint_t i;
 935 
 936         if (P->core != NULL) {
 937                 extern void __priv_free_info(void *);
 938                 lwp_info_t *nlwp, *lwp = list_next(&P->core->core_lwp_head);
 939 
 940                 for (i = 0; i < P->core->core_nlwp; i++, lwp = nlwp) {
 941                         nlwp = list_next(lwp);
 942 #ifdef __sparc
 943                         if (lwp->lwp_gwins != NULL)
 944                                 free(lwp->lwp_gwins);
 945                         if (lwp->lwp_xregs != NULL)
 946                                 free(lwp->lwp_xregs);
 947                         if (lwp->lwp_asrs != NULL)
 948                                 free(lwp->lwp_asrs);
 949 #endif
 950                         free(lwp);
 951                 }
 952 
 953                 if (P->core->core_platform != NULL)
 954                         free(P->core->core_platform);
 955                 if (P->core->core_uts != NULL)
 956                         free(P->core->core_uts);
 957                 if (P->core->core_cred != NULL)
 958                         free(P->core->core_cred);
 959                 if (P->core->core_priv != NULL)
 960                         free(P->core->core_priv);
 961                 if (P->core->core_privinfo != NULL)
 962                         __priv_free_info(P->core->core_privinfo);
 963                 if (P->core->core_ppii != NULL)
 964                         free(P->core->core_ppii);
 965                 if (P->core->core_zonename != NULL)
 966                         free(P->core->core_zonename);
 967 #if defined(__i386) || defined(__amd64)
 968                 if (P->core->core_ldt != NULL)
 969                         free(P->core->core_ldt);
 970 #endif
 971 
 972                 free(P->core);
 973         }
 974 
 975         if (P->ucaddrs != NULL) {
 976                 free(P->ucaddrs);
 977                 P->ucaddrs = NULL;
 978                 P->ucnelems = 0;
 979         }
 980 
 981         (void) mutex_lock(&P->proc_lock);
 982         if (P->hashtab != NULL) {
 983                 struct ps_lwphandle *L;
 984                 for (i = 0; i < HASHSIZE; i++) {
 985                         while ((L = P->hashtab[i]) != NULL)
 986                                 Lfree_internal(P, L);
 987                 }
 988                 free(P->hashtab);
 989         }
 990 
 991         while (P->num_fd > 0) {
 992                 fd_info_t *fip = list_next(&P->fd_head);
 993                 list_unlink(fip);
 994                 free(fip);
 995                 P->num_fd--;
 996         }
 997         (void) mutex_unlock(&P->proc_lock);
 998         (void) mutex_destroy(&P->proc_lock);
 999 
1000         if (P->agentctlfd >= 0)
1001                 (void) close(P->agentctlfd);
1002         if (P->agentstatfd >= 0)
1003                 (void) close(P->agentstatfd);
1004         if (P->ctlfd >= 0)
1005                 (void) close(P->ctlfd);
1006         if (P->asfd >= 0)
1007                 (void) close(P->asfd);
1008         if (P->statfd >= 0)
1009                 (void) close(P->statfd);
1010         Preset_maps(P);

1011 
1012         /* clear out the structure as a precaution against reuse */
1013         (void) memset(P, 0, sizeof (*P));
1014         P->ctlfd = -1;
1015         P->asfd = -1;
1016         P->statfd = -1;
1017         P->agentctlfd = -1;
1018         P->agentstatfd = -1;
1019 
1020         free(P);
1021 }
1022 
1023 /*
1024  * Return the state of the process, one of the PS_* values.
1025  */
1026 int
1027 Pstate(struct ps_prochandle *P)
1028 {
1029         return (P->state);
1030 }


1042 
1043 /*
1044  * Return the open control file descriptor for the process.
1045  * Clients must not close this file descriptor, not use it
1046  * after the process is freed.
1047  */
1048 int
1049 Pctlfd(struct ps_prochandle *P)
1050 {
1051         return (P->ctlfd);
1052 }
1053 
1054 /*
1055  * Return a pointer to the process psinfo structure.
1056  * Clients should not hold on to this pointer indefinitely.
1057  * It will become invalid on Prelease().
1058  */
1059 const psinfo_t *
1060 Ppsinfo(struct ps_prochandle *P)
1061 {
1062         if (P->state == PS_IDLE) {
1063                 errno = ENODATA;
1064                 return (NULL);
1065         }
1066 
1067         if (P->state != PS_DEAD && proc_get_psinfo(P->pid, &P->psinfo) == -1)
1068                 return (NULL);
1069 
1070         return (&P->psinfo);
1071 }
1072 
1073 /*
1074  * Return a pointer to the process status structure.
1075  * Clients should not hold on to this pointer indefinitely.
1076  * It will become invalid on Prelease().
1077  */
1078 const pstatus_t *
1079 Pstatus(struct ps_prochandle *P)
1080 {
1081         return (&P->status);
1082 }
1083 






1084 /*
1085  * Fill in a pointer to a process credentials structure.  The ngroups parameter
1086  * is the number of supplementary group entries allocated in the caller's cred
1087  * structure.  It should equal zero or one unless extra space has been
1088  * allocated for the group list by the caller.
1089  */
1090 int
1091 Pcred(struct ps_prochandle *P, prcred_t *pcrp, int ngroups)
1092 {
1093         if (P->state == PS_IDLE) {
1094                 errno = ENODATA;
1095                 return (-1);
1096         }
1097 
1098         if (P->state != PS_DEAD)
1099                 return (proc_get_cred(P->pid, pcrp, ngroups));



1100 
1101         if (P->core->core_cred != NULL) {
1102                 /*
1103                  * Avoid returning more supplementary group data than the
1104                  * caller has allocated in their buffer.  We expect them to
1105                  * check pr_ngroups afterward and potentially call us again.
1106                  */
1107                 ngroups = MIN(ngroups, P->core->core_cred->pr_ngroups);
1108 
1109                 (void) memcpy(pcrp, P->core->core_cred,
1110                     sizeof (prcred_t) + (ngroups - 1) * sizeof (gid_t));
1111 
1112                 return (0);
1113         }
1114 
1115         errno = ENODATA;
1116         return (-1);
1117 }
1118 

1119 #if defined(__i386) || defined(__amd64)
1120 /*
1121  * Fill in a pointer to a process LDT structure.
1122  * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
1123  * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
1124  * Otherwise we return the actual number of LDT entries fetched (<= nldt).
1125  */
1126 int
1127 Pldt(struct ps_prochandle *P, struct ssd *pldt, int nldt)
1128 {
1129         if (P->state == PS_IDLE) {
1130                 errno = ENODATA;
1131                 return (-1);
1132         }
1133 
1134         if (P->state != PS_DEAD)
1135                 return (proc_get_ldt(P->pid, pldt, nldt));
1136 
1137         if (pldt == NULL || nldt == 0)
1138                 return (P->core->core_nldt);
1139 
1140         if (P->core->core_ldt != NULL) {
1141                 nldt = MIN(nldt, P->core->core_nldt);
1142 
1143                 (void) memcpy(pldt, P->core->core_ldt,
1144                     nldt * sizeof (struct ssd));
1145 
1146                 return (nldt);
1147         }
1148 
1149         errno = ENODATA;
1150         return (-1);
1151 }
1152 #endif  /* __i386 */
1153 
1154 /*
1155  * Fill in a pointer to a process privilege structure.
1156  */
1157 ssize_t
1158 Ppriv(struct ps_prochandle *P, prpriv_t *pprv, size_t size)
1159 {
1160         if (P->state != PS_DEAD) {
1161                 prpriv_t *pp = proc_get_priv(P->pid);
1162                 if (pp != NULL) {
1163                         size = MIN(size, PRIV_PRPRIV_SIZE(pp));
1164                         (void) memcpy(pprv, pp, size);
1165                         free(pp);
1166                         return (size);
1167                 }
1168                 return (-1);
1169         }
1170 
1171         if (P->core->core_priv != NULL) {
1172                 size = MIN(P->core->core_priv_size, size);
1173                 (void) memcpy(pprv, P->core->core_priv, size);
1174                 return (size);
1175         }
1176         errno = ENODATA;
1177         return (-1);
1178 }
1179 
1180 int
1181 Psetpriv(struct ps_prochandle *P, prpriv_t *pprv)
1182 {
1183         int rc;
1184         long *ctl;
1185         size_t sz;
1186 
1187         if (P->state == PS_DEAD) {
1188                 errno = EBADF;
1189                 return (-1);
1190         }
1191 
1192         sz = PRIV_PRPRIV_SIZE(pprv) + sizeof (long);
1193 
1194         sz = ((sz - 1) / sizeof (long) + 1) * sizeof (long);
1195 
1196         ctl = malloc(sz);
1197         if (ctl == NULL)
1198                 return (-1);
1199 
1200         ctl[0] = PCSPRIV;
1201 
1202         (void) memcpy(&ctl[1], pprv, PRIV_PRPRIV_SIZE(pprv));
1203 
1204         if (write(P->ctlfd, ctl, sz) != sz)
1205                 rc = -1;
1206         else
1207                 rc = 0;
1208 
1209         free(ctl);
1210 
1211         return (rc);
1212 }
1213 
1214 void *
1215 Pprivinfo(struct ps_prochandle *P)
1216 {


1217         /* Use default from libc */
1218         if (P->state != PS_DEAD)
1219                 return (NULL);
1220 
1221         return (P->core->core_privinfo);
1222 }
1223 
1224 /*
1225  * Ensure that all cached state is written to the process.
1226  * The cached state is the LWP's signal mask and registers
1227  * and the process's tracing flags.
1228  */
1229 void
1230 Psync(struct ps_prochandle *P)
1231 {
1232         int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
1233         long cmd[6];
1234         iovec_t iov[12];
1235         int n = 0;
1236 
1237         if (P->flags & SETHOLD) {
1238                 cmd[0] = PCSHOLD;
1239                 iov[n].iov_base = (caddr_t)&cmd[0];
1240                 iov[n++].iov_len = sizeof (long);
1241                 iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_lwphold;


1961                 }
1962                 /* If it is not in a jobcontrol stop, issue an error message */
1963                 if (errno != EBUSY ||
1964                     P->status.pr_lwp.pr_why != PR_JOBCONTROL) {
1965                         dprintf("Psetrun: %s\n", strerror(errno));
1966                         return (-1);
1967                 }
1968                 /* Otherwise pretend that the job-stopped process is running */
1969         }
1970 
1971         P->state = PS_RUN;
1972         return (0);
1973 }
1974 
1975 ssize_t
1976 Pread(struct ps_prochandle *P,
1977         void *buf,              /* caller's buffer */
1978         size_t nbyte,           /* number of bytes to read */
1979         uintptr_t address)      /* address in process */
1980 {
1981         return (P->ops->p_pread(P, buf, nbyte, address));
1982 }
1983 
1984 ssize_t
1985 Pread_string(struct ps_prochandle *P,
1986         char *buf,              /* caller's buffer */
1987         size_t size,            /* upper limit on bytes to read */
1988         uintptr_t addr)         /* address in process */
1989 {
1990         enum { STRSZ = 40 };
1991         char string[STRSZ + 1];
1992         ssize_t leng = 0;
1993         int nbyte;
1994 
1995         if (size < 2) {
1996                 errno = EINVAL;
1997                 return (-1);
1998         }
1999 
2000         size--;                 /* ensure trailing null fits in buffer */
2001 
2002         *buf = '\0';
2003         string[STRSZ] = '\0';
2004 
2005         for (nbyte = STRSZ; nbyte == STRSZ && leng < size; addr += STRSZ) {
2006                 if ((nbyte = P->ops->p_pread(P, string, STRSZ, addr)) <= 0) {

2007                         buf[leng] = '\0';
2008                         return (leng ? leng : -1);
2009                 }
2010                 if ((nbyte = strlen(string)) > 0) {
2011                         if (leng + nbyte > size)
2012                                 nbyte = size - leng;
2013                         (void) strncpy(buf + leng, string, nbyte);
2014                         leng += nbyte;
2015                 }
2016         }
2017         buf[leng] = '\0';
2018         return (leng);
2019 }
2020 
2021 ssize_t
2022 Pwrite(struct ps_prochandle *P,
2023         const void *buf,        /* caller's buffer */
2024         size_t nbyte,           /* number of bytes to write */
2025         uintptr_t address)      /* address in process */
2026 {
2027         return (P->ops->p_pwrite(P, buf, nbyte, address));
2028 }
2029 
2030 int
2031 Pclearsig(struct ps_prochandle *P)
2032 {
2033         int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2034         long ctl = PCCSIG;
2035 
2036         if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
2037                 return (-1);
2038         P->status.pr_lwp.pr_cursig = 0;
2039         return (0);
2040 }
2041 
2042 int
2043 Pclearfault(struct ps_prochandle *P)
2044 {
2045         int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2046         long ctl = PCCFAULT;
2047 


2748                 Psync(P);
2749                 break;
2750 
2751         case PS_IDLE:
2752                 errno = ENODATA;
2753                 return (-1);
2754         }
2755 
2756         /*
2757          * For either live processes or cores, the single LWP case is easy:
2758          * the pstatus_t contains the lwpstatus_t for the only LWP.
2759          */
2760         if (P->status.pr_nlwp <= 1)
2761                 return (func(cd, &P->status.pr_lwp));
2762 
2763         /*
2764          * For the core file multi-LWP case, we just iterate through the
2765          * list of LWP structs we read in from the core file.
2766          */
2767         if (P->state == PS_DEAD) {
2768                 lwp_info_t *lwp = list_prev(&P->core->core_lwp_head);

2769                 uint_t i;
2770 
2771                 for (i = 0; i < P->core->core_nlwp; i++, lwp = list_prev(lwp)) {
2772                         if (lwp->lwp_psinfo.pr_sname != 'Z' &&
2773                             (rv = func(cd, &lwp->lwp_status)) != 0)
2774                                 break;
2775                 }
2776 
2777                 return (rv);
2778         }
2779 
2780         /*
2781          * For the live process multi-LWP case, we have to work a little
2782          * harder: the /proc/pid/lstatus file has the array of LWP structs.
2783          */
2784         if ((Lhp = read_lfile(P, "lstatus")) == NULL)
2785                 return (-1);
2786 
2787         for (nlwp = Lhp->pr_nent, Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
2788             nlwp > 0;
2789             nlwp--, Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize)) {
2790                 if ((rv = func(cd, Lsp)) != 0)
2791                         break;
2792         }
2793 
2794         free(Lhp);
2795         return (rv);
2796 }
2797 
2798 /*
2799  * Extended LWP iteration interface.
2800  * Iterate over all LWPs, active and zombie.
2801  */
2802 int
2803 Plwp_iter_all(struct ps_prochandle *P, proc_lwp_all_f *func, void *cd)
2804 {


2819         if (P->state == PS_RUN)
2820                 (void) Pstopstatus(P, PCNULL, 0);
2821         (void) Ppsinfo(P);
2822 
2823         if (P->state == PS_STOP)
2824                 Psync(P);
2825 
2826         /*
2827          * For either live processes or cores, the single LWP case is easy:
2828          * the pstatus_t contains the lwpstatus_t for the only LWP and
2829          * the psinfo_t contains the lwpsinfo_t for the only LWP.
2830          */
2831         if (P->status.pr_nlwp + P->status.pr_nzomb <= 1)
2832                 return (func(cd, &P->status.pr_lwp, &P->psinfo.pr_lwp));
2833 
2834         /*
2835          * For the core file multi-LWP case, we just iterate through the
2836          * list of LWP structs we read in from the core file.
2837          */
2838         if (P->state == PS_DEAD) {
2839                 lwp_info_t *lwp = list_prev(&P->core->core_lwp_head);

2840                 uint_t i;
2841 
2842                 for (i = 0; i < P->core->core_nlwp; i++, lwp = list_prev(lwp)) {
2843                         sp = (lwp->lwp_psinfo.pr_sname == 'Z')? NULL :
2844                             &lwp->lwp_status;
2845                         if ((rv = func(cd, sp, &lwp->lwp_psinfo)) != 0)
2846                                 break;
2847                 }
2848 
2849                 return (rv);
2850         }
2851 
2852         /*
2853          * For the live process multi-LWP case, we have to work a little
2854          * harder: the /proc/pid/lstatus file has the array of lwpstatus_t's
2855          * and the /proc/pid/lpsinfo file has the array of lwpsinfo_t's.
2856          */
2857         if ((Lhp = read_lfile(P, "lstatus")) == NULL)
2858                 return (-1);
2859         if ((Lphp = read_lfile(P, "lpsinfo")) == NULL) {
2860                 free(Lhp);
2861                 return (-1);
2862         }
2863 
2864         /*
2865          * If we are looking at a running process, or one we do not control,
2866          * the active and zombie lwps in the process may have changed since
2867          * we read the process status structure.  If so, just start over.
2868          */
2869         if (Lhp->pr_nent != P->status.pr_nlwp ||
2870             Lphp->pr_nent != P->status.pr_nlwp + P->status.pr_nzomb)
2871                 goto retry;
2872 
2873         /*
2874          * To be perfectly safe, prescan the two arrays, checking consistency.
2875          * We rely on /proc giving us lwpstatus_t's and lwpsinfo_t's in the
2876          * same order (the lwp directory order) in their respective files.
2877          * We also rely on there being (possibly) more lwpsinfo_t's than
2878          * lwpstatus_t's (the extra lwpsinfo_t's are for zombie lwps).
2879          */


2903         for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
2904                 if (Lpsp->pr_sname != 'Z') {
2905                         sp = Lsp;
2906                         Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
2907                 } else {
2908                         sp = NULL;
2909                 }
2910                 if ((rv = func(cd, sp, Lpsp)) != 0)
2911                         break;
2912                 Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
2913         }
2914 
2915         free(Lhp);
2916         free(Lphp);
2917         return (rv);
2918 }
2919 
2920 core_content_t
2921 Pcontent(struct ps_prochandle *P)
2922 {


2923         if (P->state == PS_DEAD)
2924                 return (P->core->core_content);
2925         if (P->state == PS_IDLE)
2926                 return (CC_CONTENT_TEXT | CC_CONTENT_DATA | CC_CONTENT_CTF);
2927 
2928         return (CC_CONTENT_ALL);
2929 }
2930 
2931 /*
2932  * =================================================================
2933  * The remainder of the functions in this file are for the
2934  * control of individual LWPs in the controlled process.
2935  * =================================================================
2936  */
2937 
2938 /*
2939  * Find an entry in the process hash table for the specified lwpid.
2940  * The entry will either point to an existing struct ps_lwphandle
2941  * or it will point to an empty slot for a new struct ps_lwphandle.
2942  */
2943 static struct ps_lwphandle **
2944 Lfind(struct ps_prochandle *P, lwpid_t lwpid)


3710  * Sort the current set of mappings.  Should be called during target
3711  * initialization after all calls to Padd_mapping() have been made.
3712  */
3713 void
3714 Psort_mappings(struct ps_prochandle *P)
3715 {
3716         int i;
3717         map_info_t *mp;
3718 
3719         qsort(P->mappings, P->map_count, sizeof (map_info_t), map_sort);
3720 
3721         /*
3722          * Update all the file_map pointers to refer to the new locations.
3723          */
3724         for (i = 0; i < P->map_count; i++) {
3725                 mp = &P->mappings[i];
3726                 if (mp->map_relocate)
3727                         mp->map_file->file_map = mp;
3728                 mp->map_relocate = 0;
3729         }





























3730 }


   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  *
  26  * Portions Copyright 2007 Chad Mynhier
  27  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
  28  * Copyright (c) 2013 by Delphix. All rights reserved.
  29  */
  30 
  31 #include <assert.h>
  32 #include <stdio.h>
  33 #include <stdlib.h>
  34 #include <unistd.h>
  35 #include <ctype.h>
  36 #include <fcntl.h>
  37 #include <string.h>
  38 #include <strings.h>
  39 #include <memory.h>
  40 #include <errno.h>
  41 #include <dirent.h>
  42 #include <limits.h>
  43 #include <signal.h>
  44 #include <atomic.h>
  45 #include <zone.h>
  46 #include <sys/types.h>
  47 #include <sys/uio.h>
  48 #include <sys/stat.h>
  49 #include <sys/resource.h>
  50 #include <sys/param.h>
  51 #include <sys/stack.h>
  52 #include <sys/fault.h>
  53 #include <sys/syscall.h>
  54 #include <sys/sysmacros.h>
  55 #include <sys/systeminfo.h>
  56 
  57 #include "libproc.h"
  58 #include "Pcontrol.h"
  59 #include "Putil.h"
  60 #include "P32ton.h"
  61 
  62 int     _libproc_debug;         /* set non-zero to enable debugging printfs */
  63 int     _libproc_no_qsort;      /* set non-zero to inhibit sorting */
  64                                 /* of symbol tables */
  65 int     _libproc_incore_elf;    /* only use in-core elf data */
  66 
  67 sigset_t blockable_sigs;        /* signals to block when we need to be safe */
  68 static  int     minfd;  /* minimum file descriptor returned by dupfd(fd, 0) */
  69 char    procfs_path[PATH_MAX] = "/proc";
  70 
  71 /*
  72  * Function prototypes for static routines in this module.
  73  */
  74 static  void    deadcheck(struct ps_prochandle *);
  75 static  void    restore_tracing_flags(struct ps_prochandle *);
  76 static  void    Lfree_internal(struct ps_prochandle *, struct ps_lwphandle *);
  77 static  prheader_t *read_lfile(struct ps_prochandle *, const char *);
  78 
  79 /*
  80  * Ops vector functions for live processes.

  81  */
  82 
  83 /*ARGSUSED*/
  84 static ssize_t
  85 Pread_live(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr,
  86     void *data)
  87 {
  88         return (pread(P->asfd, buf, n, (off_t)addr));
  89 }
  90 
  91 /*ARGSUSED*/
  92 static ssize_t
  93 Pwrite_live(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr,
  94     void *data)
  95 {
  96         return (pwrite(P->asfd, buf, n, (off_t)addr));
  97 }
  98 
  99 /*ARGSUSED*/
 100 static int
 101 Pread_maps_live(struct ps_prochandle *P, prmap_t **Pmapp, ssize_t *nmapp,
 102     void *data)
 103 {
 104         char mapfile[PATH_MAX];
 105         int mapfd;
 106         struct stat statb;
 107         ssize_t nmap;
 108         prmap_t *Pmap = NULL;
 109 
 110         (void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
 111             procfs_path, (int)P->pid);
 112         if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
 113             fstat(mapfd, &statb) != 0 ||
 114             statb.st_size < sizeof (prmap_t) ||
 115             (Pmap = malloc(statb.st_size)) == NULL ||
 116             (nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
 117             (nmap /= sizeof (prmap_t)) == 0) {
 118                 if (Pmap != NULL)
 119                         free(Pmap);
 120                 if (mapfd >= 0)
 121                         (void) close(mapfd);
 122                 Preset_maps(P); /* utter failure; destroy tables */
 123                 return (-1);
 124         }
 125         (void) close(mapfd);
 126 
 127         *Pmapp = Pmap;
 128         *nmapp = nmap;
 129 
 130         return (0);
 131 }
 132 
 133 /*ARGSUSED*/
 134 static void
 135 Pread_aux_live(struct ps_prochandle *P, auxv_t **auxvp, int *nauxp, void *data)
 136 {
 137         char auxfile[64];
 138         int fd;
 139         struct stat statb;
 140         auxv_t *auxv;
 141         ssize_t naux;
 142 
 143         (void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv",
 144             procfs_path, (int)P->pid);
 145         if ((fd = open(auxfile, O_RDONLY)) < 0) {
 146                 dprintf("%s: failed to open %s: %s\n",
 147                     __func__, auxfile, strerror(errno));
 148                 return;
 149         }
 150 
 151         if (fstat(fd, &statb) == 0 &&
 152             statb.st_size >= sizeof (auxv_t) &&
 153             (auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) {
 154                 if ((naux = read(fd, auxv, statb.st_size)) < 0 ||
 155                     (naux /= sizeof (auxv_t)) < 1) {
 156                         dprintf("%s: read failed: %s\n",
 157                             __func__, strerror(errno));
 158                         free(auxv);
 159                 } else {
 160                         auxv[naux].a_type = AT_NULL;
 161                         auxv[naux].a_un.a_val = 0L;
 162 
 163                         *auxvp = auxv;
 164                         *nauxp = (int)naux;
 165                 }
 166         }
 167 
 168         (void) close(fd);
 169 }
 170 
 171 /*ARGSUSED*/
 172 static int
 173 Pcred_live(struct ps_prochandle *P, prcred_t *pcrp, int ngroups, void *data)
 174 {
 175         return (proc_get_cred(P->pid, pcrp, ngroups));
 176 }
 177 
 178 /*ARGSUSED*/
 179 static int
 180 Ppriv_live(struct ps_prochandle *P, prpriv_t **pprv, void *data)
 181 {
 182         prpriv_t *pp;
 183 
 184         pp = proc_get_priv(P->pid);
 185         if (pp == NULL) {
 186                 return (-1);
 187         }
 188 
 189         *pprv = pp;
 190         return (0);
 191 }
 192 
 193 /*ARGSUSED*/
 194 static const psinfo_t *
 195 Ppsinfo_live(struct ps_prochandle *P, psinfo_t *psinfo, void *data)
 196 {
 197         if (proc_get_psinfo(P->pid, psinfo) == -1)
 198                 return (NULL);
 199 
 200         return (psinfo);
 201 }
 202 
 203 /*ARGSUSED*/
 204 static prheader_t *
 205 Plstatus_live(struct ps_prochandle *P, void *data)
 206 {
 207         return (read_lfile(P, "lstatus"));
 208 }
 209 
 210 /*ARGSUSED*/
 211 static prheader_t *
 212 Plpsinfo_live(struct ps_prochandle *P, void *data)
 213 {
 214         return (read_lfile(P, "lpsinfo"));
 215 }
 216 
 217 /*ARGSUSED*/
 218 static char *
 219 Pplatform_live(struct ps_prochandle *P, char *s, size_t n, void *data)
 220 {
 221         if (sysinfo(SI_PLATFORM, s, n) == -1)
 222                 return (NULL);
 223         return (s);
 224 }
 225 
 226 /*ARGSUSED*/
 227 static int
 228 Puname_live(struct ps_prochandle *P, struct utsname *u, void *data)
 229 {
 230         return (uname(u));
 231 }
 232 
 233 /*ARGSUSED*/
 234 static char *
 235 Pzonename_live(struct ps_prochandle *P, char *s, size_t n, void *data)
 236 {
 237         if (getzonenamebyid(P->status.pr_zoneid, s, n) < 0)
 238                 return (NULL);
 239         s[n - 1] = '\0';
 240         return (s);
 241 }
 242 
 243 /*
 244  * Callback function for Pfindexec().  We return a match if we can stat the
 245  * suggested pathname and confirm its device and inode number match our
 246  * previous information about the /proc/<pid>/object/a.out file.
 247  */
 248 static int
 249 stat_exec(const char *path, void *arg)
 250 {
 251         struct stat64 *stp = arg;
 252         struct stat64 st;
 253 
 254         return (stat64(path, &st) == 0 && S_ISREG(st.st_mode) &&
 255             stp->st_dev == st.st_dev && stp->st_ino == st.st_ino);
 256 }
 257 
 258 /*ARGSUSED*/
 259 static char *
 260 Pexecname_live(struct ps_prochandle *P, char *buf, size_t buflen, void *data)
 261 {
 262         char exec_name[PATH_MAX];
 263         char cwd[PATH_MAX];
 264         char proc_cwd[64];
 265         struct stat64 st;
 266         int ret;
 267 
 268         /*
 269          * Try to get the path information first.
 270          */
 271         (void) snprintf(exec_name, sizeof (exec_name),
 272             "%s/%d/path/a.out", procfs_path, (int)P->pid);
 273         if ((ret = readlink(exec_name, buf, buflen - 1)) > 0) {
 274                 buf[ret] = '\0';
 275                 (void) Pfindobj(P, buf, buf, buflen);
 276                 return (buf);
 277         }
 278 
 279         /*
 280          * Stat the executable file so we can compare Pfindexec's
 281          * suggestions to the actual device and inode number.
 282          */
 283         (void) snprintf(exec_name, sizeof (exec_name),
 284             "%s/%d/object/a.out", procfs_path, (int)P->pid);
 285 
 286         if (stat64(exec_name, &st) != 0 || !S_ISREG(st.st_mode))
 287                 return (NULL);
 288 
 289         /*
 290          * Attempt to figure out the current working directory of the
 291          * target process.  This only works if the target process has
 292          * not changed its current directory since it was exec'd.
 293          */
 294         (void) snprintf(proc_cwd, sizeof (proc_cwd),
 295             "%s/%d/path/cwd", procfs_path, (int)P->pid);
 296 
 297         if ((ret = readlink(proc_cwd, cwd, PATH_MAX - 1)) > 0)
 298                 cwd[ret] = '\0';
 299 
 300         (void) Pfindexec(P, ret > 0 ? cwd : NULL, stat_exec, &st);
 301 
 302         return (NULL);
 303 }
 304 
 305 #if defined(__i386) || defined(__amd64)
 306 /*ARGSUSED*/
 307 static int
 308 Pldt_live(struct ps_prochandle *P, struct ssd *pldt, int nldt, void *data)
 309 {
 310         return (proc_get_ldt(P->pid, pldt, nldt));
 311 }
 312 #endif
 313 
 314 static const ps_ops_t P_live_ops = {
 315         .pop_pread      = Pread_live,
 316         .pop_pwrite     = Pwrite_live,
 317         .pop_read_maps  = Pread_maps_live,
 318         .pop_read_aux   = Pread_aux_live,
 319         .pop_cred       = Pcred_live,
 320         .pop_priv       = Ppriv_live,
 321         .pop_psinfo     = Ppsinfo_live,
 322         .pop_lstatus    = Plstatus_live,
 323         .pop_lpsinfo    = Plpsinfo_live,
 324         .pop_platform   = Pplatform_live,
 325         .pop_uname      = Puname_live,
 326         .pop_zonename   = Pzonename_live,
 327         .pop_execname   = Pexecname_live,
 328 #if defined(__i386) || defined(__amd64)
 329         .pop_ldt        = Pldt_live
 330 #endif
 331 };
 332 
 333 /*
 334  * This is the library's .init handler.
 335  */
 336 #pragma init(_libproc_init)
 337 void
 338 _libproc_init(void)
 339 {
 340         _libproc_debug = getenv("LIBPROC_DEBUG") != NULL;
 341         _libproc_no_qsort = getenv("LIBPROC_NO_QSORT") != NULL;
 342         _libproc_incore_elf = getenv("LIBPROC_INCORE_ELF") != NULL;
 343 
 344         (void) sigfillset(&blockable_sigs);
 345         (void) sigdelset(&blockable_sigs, SIGKILL);
 346         (void) sigdelset(&blockable_sigs, SIGSTOP);
 347 }
 348 
 349 void
 350 Pset_procfs_path(const char *path)
 351 {
 352         (void) snprintf(procfs_path, sizeof (procfs_path), "%s", path);
 353 }


 471                 if (envp)
 472                         environ = (char **)envp;
 473 
 474                 (void) execvp(file, argv);  /* execute the program */
 475                 _exit(127);
 476         }
 477 
 478         /*
 479          * Initialize the process structure.
 480          */
 481         (void) memset(P, 0, sizeof (*P));
 482         (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
 483         P->flags |= CREATED;
 484         P->state = PS_RUN;
 485         P->pid = pid;
 486         P->asfd = -1;
 487         P->ctlfd = -1;
 488         P->statfd = -1;
 489         P->agentctlfd = -1;
 490         P->agentstatfd = -1;
 491         Pinit_ops(&P->ops, &P_live_ops);
 492         Pinitsym(P);
 493 
 494         /*
 495          * Open the /proc/pid files.
 496          */
 497         (void) snprintf(procname, sizeof (procname), "%s/%d/",
 498             procfs_path, (int)pid);
 499         fname = procname + strlen(procname);
 500         (void) set_minfd();
 501 
 502         /*
 503          * Exclusive write open advises others not to interfere.
 504          * There is no reason for any of these open()s to fail.
 505          */
 506         (void) strcpy(fname, "as");
 507         if ((fd = open(procname, (O_RDWR|O_EXCL))) < 0 ||
 508             (fd = dupfd(fd, 0)) < 0) {
 509                 dprintf("Pcreate: failed to open %s: %s\n",
 510                     procname, strerror(errno));
 511                 rc = C_STRANGE;


 774         }
 775 
 776         P->asfd = -1;
 777         P->ctlfd = -1;
 778         P->statfd = -1;
 779 
 780 again:  /* Come back here if we lose it in the Window of Vulnerability */
 781         if (P->ctlfd >= 0)
 782                 (void) close(P->ctlfd);
 783         if (P->asfd >= 0)
 784                 (void) close(P->asfd);
 785         if (P->statfd >= 0)
 786                 (void) close(P->statfd);
 787         (void) memset(P, 0, sizeof (*P));
 788         (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
 789         P->ctlfd = -1;
 790         P->asfd = -1;
 791         P->statfd = -1;
 792         P->agentctlfd = -1;
 793         P->agentstatfd = -1;
 794         Pinit_ops(&P->ops, &P_live_ops);
 795         Pinitsym(P);
 796 
 797         /*
 798          * Open the /proc/pid files
 799          */
 800         (void) snprintf(procname, sizeof (procname), "%s/%d/",
 801             procfs_path, (int)pid);
 802         fname = procname + strlen(procname);
 803         (void) set_minfd();
 804 
 805         /*
 806          * Request exclusive open to avoid grabbing someone else's
 807          * process and to prevent others from interfering afterwards.
 808          * If this fails and the 'PGRAB_FORCE' flag is set, attempt to
 809          * open non-exclusively.
 810          */
 811         (void) strcpy(fname, "as");
 812         omode = (flags & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
 813 
 814         if (((fd = open(procname, omode | O_EXCL)) < 0 &&


1155         case G_NOFD:
1156                 str = "too many open files";
1157                 break;
1158         default:
1159                 str = "unknown error";
1160                 break;
1161         }
1162 
1163         return (str);
1164 }
1165 
1166 /*
1167  * Free a process control structure.
1168  * Close the file descriptors but don't do the Prelease logic.
1169  */
1170 void
1171 Pfree(struct ps_prochandle *P)
1172 {
1173         uint_t i;
1174 







































1175         if (P->ucaddrs != NULL) {
1176                 free(P->ucaddrs);
1177                 P->ucaddrs = NULL;
1178                 P->ucnelems = 0;
1179         }
1180 
1181         (void) mutex_lock(&P->proc_lock);
1182         if (P->hashtab != NULL) {
1183                 struct ps_lwphandle *L;
1184                 for (i = 0; i < HASHSIZE; i++) {
1185                         while ((L = P->hashtab[i]) != NULL)
1186                                 Lfree_internal(P, L);
1187                 }
1188                 free(P->hashtab);
1189         }
1190 
1191         while (P->num_fd > 0) {
1192                 fd_info_t *fip = list_next(&P->fd_head);
1193                 list_unlink(fip);
1194                 free(fip);
1195                 P->num_fd--;
1196         }
1197         (void) mutex_unlock(&P->proc_lock);
1198         (void) mutex_destroy(&P->proc_lock);
1199 
1200         if (P->agentctlfd >= 0)
1201                 (void) close(P->agentctlfd);
1202         if (P->agentstatfd >= 0)
1203                 (void) close(P->agentstatfd);
1204         if (P->ctlfd >= 0)
1205                 (void) close(P->ctlfd);
1206         if (P->asfd >= 0)
1207                 (void) close(P->asfd);
1208         if (P->statfd >= 0)
1209                 (void) close(P->statfd);
1210         Preset_maps(P);
1211         P->ops.pop_fini(P, P->data);
1212 
1213         /* clear out the structure as a precaution against reuse */
1214         (void) memset(P, 0, sizeof (*P));
1215         P->ctlfd = -1;
1216         P->asfd = -1;
1217         P->statfd = -1;
1218         P->agentctlfd = -1;
1219         P->agentstatfd = -1;
1220 
1221         free(P);
1222 }
1223 
1224 /*
1225  * Return the state of the process, one of the PS_* values.
1226  */
1227 int
1228 Pstate(struct ps_prochandle *P)
1229 {
1230         return (P->state);
1231 }


1243 
1244 /*
1245  * Return the open control file descriptor for the process.
1246  * Clients must not close this file descriptor, not use it
1247  * after the process is freed.
1248  */
1249 int
1250 Pctlfd(struct ps_prochandle *P)
1251 {
1252         return (P->ctlfd);
1253 }
1254 
1255 /*
1256  * Return a pointer to the process psinfo structure.
1257  * Clients should not hold on to this pointer indefinitely.
1258  * It will become invalid on Prelease().
1259  */
1260 const psinfo_t *
1261 Ppsinfo(struct ps_prochandle *P)
1262 {
1263         return (P->ops.pop_psinfo(P, &P->psinfo, P->data));








1264 }
1265 
1266 /*
1267  * Return a pointer to the process status structure.
1268  * Clients should not hold on to this pointer indefinitely.
1269  * It will become invalid on Prelease().
1270  */
1271 const pstatus_t *
1272 Pstatus(struct ps_prochandle *P)
1273 {
1274         return (&P->status);
1275 }
1276 
1277 static void
1278 Pread_status(struct ps_prochandle *P)
1279 {
1280         P->ops.pop_status(P, &P->status, P->data);
1281 }
1282 
1283 /*
1284  * Fill in a pointer to a process credentials structure.  The ngroups parameter
1285  * is the number of supplementary group entries allocated in the caller's cred
1286  * structure.  It should equal zero or one unless extra space has been
1287  * allocated for the group list by the caller.
1288  */
1289 int
1290 Pcred(struct ps_prochandle *P, prcred_t *pcrp, int ngroups)
1291 {
1292         return (P->ops.pop_cred(P, pcrp, ngroups, P->data));
1293 }


1294 
1295 static prheader_t *
1296 Plstatus(struct ps_prochandle *P)
1297 {
1298         return (P->ops.pop_lstatus(P, P->data));
1299 }
1300 
1301 static prheader_t *
1302 Plpsinfo(struct ps_prochandle *P)
1303 {
1304         return (P->ops.pop_lpsinfo(P, P->data));












1305 }
1306 
1307 
1308 #if defined(__i386) || defined(__amd64)
1309 /*
1310  * Fill in a pointer to a process LDT structure.
1311  * The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
1312  * If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
1313  * Otherwise we return the actual number of LDT entries fetched (<= nldt).
1314  */
1315 int
1316 Pldt(struct ps_prochandle *P, struct ssd *pldt, int nldt)
1317 {
1318         return (P->ops.pop_ldt(P, pldt, nldt, P->data));



1319 

















1320 }
1321 #endif  /* __i386 */
1322 
1323 /*
1324  * Return a malloced process privilege structure in *pprv.
1325  */
1326 int
1327 Ppriv(struct ps_prochandle *P, prpriv_t **pprv)
1328 {
1329         return (P->ops.pop_priv(P, pprv, P->data));

















1330 }
1331 
1332 int
1333 Psetpriv(struct ps_prochandle *P, prpriv_t *pprv)
1334 {
1335         int rc;
1336         long *ctl;
1337         size_t sz;
1338 
1339         if (P->state == PS_DEAD) {
1340                 errno = EBADF;
1341                 return (-1);
1342         }
1343 
1344         sz = PRIV_PRPRIV_SIZE(pprv) + sizeof (long);
1345 
1346         sz = ((sz - 1) / sizeof (long) + 1) * sizeof (long);
1347 
1348         ctl = malloc(sz);
1349         if (ctl == NULL)
1350                 return (-1);
1351 
1352         ctl[0] = PCSPRIV;
1353 
1354         (void) memcpy(&ctl[1], pprv, PRIV_PRPRIV_SIZE(pprv));
1355 
1356         if (write(P->ctlfd, ctl, sz) != sz)
1357                 rc = -1;
1358         else
1359                 rc = 0;
1360 
1361         free(ctl);
1362 
1363         return (rc);
1364 }
1365 
1366 void *
1367 Pprivinfo(struct ps_prochandle *P)
1368 {
1369         core_info_t *core = P->data;
1370 
1371         /* Use default from libc */
1372         if (P->state != PS_DEAD)
1373                 return (NULL);
1374 
1375         return (core->core_privinfo);
1376 }
1377 
1378 /*
1379  * Ensure that all cached state is written to the process.
1380  * The cached state is the LWP's signal mask and registers
1381  * and the process's tracing flags.
1382  */
1383 void
1384 Psync(struct ps_prochandle *P)
1385 {
1386         int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
1387         long cmd[6];
1388         iovec_t iov[12];
1389         int n = 0;
1390 
1391         if (P->flags & SETHOLD) {
1392                 cmd[0] = PCSHOLD;
1393                 iov[n].iov_base = (caddr_t)&cmd[0];
1394                 iov[n++].iov_len = sizeof (long);
1395                 iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_lwphold;


2115                 }
2116                 /* If it is not in a jobcontrol stop, issue an error message */
2117                 if (errno != EBUSY ||
2118                     P->status.pr_lwp.pr_why != PR_JOBCONTROL) {
2119                         dprintf("Psetrun: %s\n", strerror(errno));
2120                         return (-1);
2121                 }
2122                 /* Otherwise pretend that the job-stopped process is running */
2123         }
2124 
2125         P->state = PS_RUN;
2126         return (0);
2127 }
2128 
2129 ssize_t
2130 Pread(struct ps_prochandle *P,
2131         void *buf,              /* caller's buffer */
2132         size_t nbyte,           /* number of bytes to read */
2133         uintptr_t address)      /* address in process */
2134 {
2135         return (P->ops.pop_pread(P, buf, nbyte, address, P->data));
2136 }
2137 
2138 ssize_t
2139 Pread_string(struct ps_prochandle *P,
2140         char *buf,              /* caller's buffer */
2141         size_t size,            /* upper limit on bytes to read */
2142         uintptr_t addr)         /* address in process */
2143 {
2144         enum { STRSZ = 40 };
2145         char string[STRSZ + 1];
2146         ssize_t leng = 0;
2147         int nbyte;
2148 
2149         if (size < 2) {
2150                 errno = EINVAL;
2151                 return (-1);
2152         }
2153 
2154         size--;                 /* ensure trailing null fits in buffer */
2155 
2156         *buf = '\0';
2157         string[STRSZ] = '\0';
2158 
2159         for (nbyte = STRSZ; nbyte == STRSZ && leng < size; addr += STRSZ) {
2160                 if ((nbyte = P->ops.pop_pread(P, string, STRSZ, addr,
2161                     P->data)) <= 0) {
2162                         buf[leng] = '\0';
2163                         return (leng ? leng : -1);
2164                 }
2165                 if ((nbyte = strlen(string)) > 0) {
2166                         if (leng + nbyte > size)
2167                                 nbyte = size - leng;
2168                         (void) strncpy(buf + leng, string, nbyte);
2169                         leng += nbyte;
2170                 }
2171         }
2172         buf[leng] = '\0';
2173         return (leng);
2174 }
2175 
2176 ssize_t
2177 Pwrite(struct ps_prochandle *P,
2178         const void *buf,        /* caller's buffer */
2179         size_t nbyte,           /* number of bytes to write */
2180         uintptr_t address)      /* address in process */
2181 {
2182         return (P->ops.pop_pwrite(P, buf, nbyte, address, P->data));
2183 }
2184 
2185 int
2186 Pclearsig(struct ps_prochandle *P)
2187 {
2188         int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2189         long ctl = PCCSIG;
2190 
2191         if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
2192                 return (-1);
2193         P->status.pr_lwp.pr_cursig = 0;
2194         return (0);
2195 }
2196 
2197 int
2198 Pclearfault(struct ps_prochandle *P)
2199 {
2200         int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
2201         long ctl = PCCFAULT;
2202 


2903                 Psync(P);
2904                 break;
2905 
2906         case PS_IDLE:
2907                 errno = ENODATA;
2908                 return (-1);
2909         }
2910 
2911         /*
2912          * For either live processes or cores, the single LWP case is easy:
2913          * the pstatus_t contains the lwpstatus_t for the only LWP.
2914          */
2915         if (P->status.pr_nlwp <= 1)
2916                 return (func(cd, &P->status.pr_lwp));
2917 
2918         /*
2919          * For the core file multi-LWP case, we just iterate through the
2920          * list of LWP structs we read in from the core file.
2921          */
2922         if (P->state == PS_DEAD) {
2923                 core_info_t *core = P->data;
2924                 lwp_info_t *lwp = list_prev(&core->core_lwp_head);
2925                 uint_t i;
2926 
2927                 for (i = 0; i < core->core_nlwp; i++, lwp = list_prev(lwp)) {
2928                         if (lwp->lwp_psinfo.pr_sname != 'Z' &&
2929                             (rv = func(cd, &lwp->lwp_status)) != 0)
2930                                 break;
2931                 }
2932 
2933                 return (rv);
2934         }
2935 
2936         /*
2937          * For the live process multi-LWP case, we have to work a little
2938          * harder: the /proc/pid/lstatus file has the array of LWP structs.
2939          */
2940         if ((Lhp = Plstatus(P)) == NULL)
2941                 return (-1);
2942 
2943         for (nlwp = Lhp->pr_nent, Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
2944             nlwp > 0;
2945             nlwp--, Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize)) {
2946                 if ((rv = func(cd, Lsp)) != 0)
2947                         break;
2948         }
2949 
2950         free(Lhp);
2951         return (rv);
2952 }
2953 
2954 /*
2955  * Extended LWP iteration interface.
2956  * Iterate over all LWPs, active and zombie.
2957  */
2958 int
2959 Plwp_iter_all(struct ps_prochandle *P, proc_lwp_all_f *func, void *cd)
2960 {


2975         if (P->state == PS_RUN)
2976                 (void) Pstopstatus(P, PCNULL, 0);
2977         (void) Ppsinfo(P);
2978 
2979         if (P->state == PS_STOP)
2980                 Psync(P);
2981 
2982         /*
2983          * For either live processes or cores, the single LWP case is easy:
2984          * the pstatus_t contains the lwpstatus_t for the only LWP and
2985          * the psinfo_t contains the lwpsinfo_t for the only LWP.
2986          */
2987         if (P->status.pr_nlwp + P->status.pr_nzomb <= 1)
2988                 return (func(cd, &P->status.pr_lwp, &P->psinfo.pr_lwp));
2989 
2990         /*
2991          * For the core file multi-LWP case, we just iterate through the
2992          * list of LWP structs we read in from the core file.
2993          */
2994         if (P->state == PS_DEAD) {
2995                 core_info_t *core = P->data;
2996                 lwp_info_t *lwp = list_prev(&core->core_lwp_head);
2997                 uint_t i;
2998 
2999                 for (i = 0; i < core->core_nlwp; i++, lwp = list_prev(lwp)) {
3000                         sp = (lwp->lwp_psinfo.pr_sname == 'Z')? NULL :
3001                             &lwp->lwp_status;
3002                         if ((rv = func(cd, sp, &lwp->lwp_psinfo)) != 0)
3003                                 break;
3004                 }
3005 
3006                 return (rv);
3007         }
3008 
3009         /*
3010          * For all other cases retrieve the array of lwpstatus_t's and
3011          * lwpsinfo_t's.

3012          */
3013         if ((Lhp = Plstatus(P)) == NULL)
3014                 return (-1);
3015         if ((Lphp = Plpsinfo(P)) == NULL) {
3016                 free(Lhp);
3017                 return (-1);
3018         }
3019 
3020         /*
3021          * If we are looking at a running process, or one we do not control,
3022          * the active and zombie lwps in the process may have changed since
3023          * we read the process status structure.  If so, just start over.
3024          */
3025         if (Lhp->pr_nent != P->status.pr_nlwp ||
3026             Lphp->pr_nent != P->status.pr_nlwp + P->status.pr_nzomb)
3027                 goto retry;
3028 
3029         /*
3030          * To be perfectly safe, prescan the two arrays, checking consistency.
3031          * We rely on /proc giving us lwpstatus_t's and lwpsinfo_t's in the
3032          * same order (the lwp directory order) in their respective files.
3033          * We also rely on there being (possibly) more lwpsinfo_t's than
3034          * lwpstatus_t's (the extra lwpsinfo_t's are for zombie lwps).
3035          */


3059         for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
3060                 if (Lpsp->pr_sname != 'Z') {
3061                         sp = Lsp;
3062                         Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
3063                 } else {
3064                         sp = NULL;
3065                 }
3066                 if ((rv = func(cd, sp, Lpsp)) != 0)
3067                         break;
3068                 Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
3069         }
3070 
3071         free(Lhp);
3072         free(Lphp);
3073         return (rv);
3074 }
3075 
3076 core_content_t
3077 Pcontent(struct ps_prochandle *P)
3078 {
3079         core_info_t *core = P->data;
3080 
3081         if (P->state == PS_DEAD)
3082                 return (core->core_content);
3083         if (P->state == PS_IDLE)
3084                 return (CC_CONTENT_TEXT | CC_CONTENT_DATA | CC_CONTENT_CTF);
3085 
3086         return (CC_CONTENT_ALL);
3087 }
3088 
3089 /*
3090  * =================================================================
3091  * The remainder of the functions in this file are for the
3092  * control of individual LWPs in the controlled process.
3093  * =================================================================
3094  */
3095 
3096 /*
3097  * Find an entry in the process hash table for the specified lwpid.
3098  * The entry will either point to an existing struct ps_lwphandle
3099  * or it will point to an empty slot for a new struct ps_lwphandle.
3100  */
3101 static struct ps_lwphandle **
3102 Lfind(struct ps_prochandle *P, lwpid_t lwpid)


3868  * Sort the current set of mappings.  Should be called during target
3869  * initialization after all calls to Padd_mapping() have been made.
3870  */
3871 void
3872 Psort_mappings(struct ps_prochandle *P)
3873 {
3874         int i;
3875         map_info_t *mp;
3876 
3877         qsort(P->mappings, P->map_count, sizeof (map_info_t), map_sort);
3878 
3879         /*
3880          * Update all the file_map pointers to refer to the new locations.
3881          */
3882         for (i = 0; i < P->map_count; i++) {
3883                 mp = &P->mappings[i];
3884                 if (mp->map_relocate)
3885                         mp->map_file->file_map = mp;
3886                 mp->map_relocate = 0;
3887         }
3888 }
3889 
3890 struct ps_prochandle *
3891 Pgrab_ops(pid_t pid, void *data, const ps_ops_t *ops, int flags)
3892 {
3893         struct ps_prochandle *P;
3894 
3895         if ((P = calloc(1, sizeof (*P))) == NULL) {
3896                 return (NULL);
3897         }
3898 
3899         Pinit_ops(&P->ops, ops);
3900         (void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
3901         P->pid = pid;
3902         P->state = PS_STOP;
3903         P->asfd = -1;
3904         P->ctlfd = -1;
3905         P->statfd = -1;
3906         P->agentctlfd = -1;
3907         P->agentstatfd = -1;
3908         Pinitsym(P);
3909         P->data = data;
3910         Pread_status(P);
3911 
3912         if (flags & PGRAB_INCORE) {
3913                 P->flags |= INCORE;
3914         }
3915 
3916         return (P);
3917 }