1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   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 /* LINTLIBRARY */
  22 /* PROTOLIB1 */
  23 
  24 /*
  25  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 /*
  29  * Copyright (c) 2013 by Delphix. All rights reserved.
  30  */
  31 #include "libproc.h"
  32 
  33 /*
  34  * usr/src/lib/libproc
  35  */
  36 
  37 /* Pcontrol.c */
  38 int     _libproc_debug;
  39 struct ps_prochandle *Pcreate(const char *file, char *const *argv,
  40                 int *perr, char *path, size_t len);
  41 const char *Pcreate_error(int error);
  42 void    Pcreate_callback(struct ps_prochandle *Pr);
  43 struct ps_prochandle *Pgrab(pid_t pid, int gflag, int *perr);
  44 const char *Pgrab_error(int error);
  45 void    Pfree(struct ps_prochandle *Pr);
  46 int     Pstate(struct ps_prochandle *Pr);
  47 int     Pasfd(struct ps_prochandle *Pr);
  48 int     Pctlfd(struct ps_prochandle *Pr);
  49 const psinfo_t *Ppsinfo(struct ps_prochandle *Pr);
  50 const pstatus_t *Pstatus(struct ps_prochandle *Pr);
  51 int     Pcred(struct ps_prochandle *Pr, prcred_t *pcrp, int ngroups);
  52 int     Ppriv(struct ps_prochandle *Pr, prpriv_t **pprivp);
  53 void    Psync(struct ps_prochandle *Pr);
  54 int     Pcreate_agent(struct ps_prochandle *Pr);
  55 void    Pdestroy_agent(struct ps_prochandle *Pr);
  56 int     Preopen(struct ps_prochandle *Pr);
  57 void    Prelease(struct ps_prochandle *Pr, int flags);
  58 int     Pstopstatus(struct ps_prochandle *Pr, long cmd, uint_t msec);
  59 int     Pwait(struct ps_prochandle *Pr, uint_t msec);
  60 int     Pstop(struct ps_prochandle *Pr, uint_t msec);
  61 int     Pdstop(struct ps_prochandle *Pr);
  62 int     Pgetareg(struct ps_prochandle *Pr, int regno, prgreg_t *preg);
  63 int     Pputareg(struct ps_prochandle *Pr, int regno, prgreg_t reg);
  64 int     Psetrun(struct ps_prochandle *Pr, int sig, int flags);
  65 ssize_t Pread(struct ps_prochandle *Pr,
  66                 void *buf, size_t nbyte, uintptr_t address);
  67 ssize_t Pread_string(struct ps_prochandle *Pr,
  68                 char *buf, size_t nbyte, uintptr_t address);
  69 ssize_t Pwrite(struct ps_prochandle *Pr,
  70                 const void *buf, size_t nbyte, uintptr_t address);
  71 int     Pclearsig(struct ps_prochandle *Pr);
  72 int     Pclearfault(struct ps_prochandle *Pr);
  73 int     Psetbkpt(struct ps_prochandle *Pr, uintptr_t address, ulong_t *saved);
  74 int     Pdelbkpt(struct ps_prochandle *Pr, uintptr_t address, ulong_t saved);
  75 int     Pxecbkpt(struct ps_prochandle *Pr, ulong_t saved);
  76 int     Psetwapt(struct ps_prochandle *Pr, const prwatch_t *wp);
  77 int     Pdelwapt(struct ps_prochandle *Pr, const prwatch_t *wp);
  78 int     Pxecwapt(struct ps_prochandle *Pr, const prwatch_t *wp);
  79 int     Psetflags(struct ps_prochandle *Pr, long flags);
  80 int     Punsetflags(struct ps_prochandle *Pr, long flags);
  81 int     Psignal(struct ps_prochandle *Pr, int which, int stop);
  82 void    Psetsignal(struct ps_prochandle *Pr, const sigset_t *set);
  83 int     Pfault(struct ps_prochandle *Pr, int which, int stop);
  84 void    Psetfault(struct ps_prochandle *Pr, const fltset_t *set);
  85 int     Psysentry(struct ps_prochandle *Pr, int which, int stop);
  86 void    Psetsysentry(struct ps_prochandle *Pr, const sysset_t *set);
  87 int     Psysexit(struct ps_prochandle *Pr, int which, int stop);
  88 void    Psetsysexit(struct ps_prochandle *Pr, const sysset_t *set);
  89 int     Plwp_iter(struct ps_prochandle *Pr, proc_lwp_f *func, void *cd);
  90 int     Psyscall(struct ps_prochandle *Pr, sysret_t *,
  91                 int sysindex, uint_t nargs, argdes_t *argp);
  92 
  93 struct ps_lwphandle *Lgrab(struct ps_prochandle *P, lwpid_t lwpid, int *perr);
  94 const char *Lgrab_error(int error);
  95 struct ps_prochandle *Lprochandle(struct ps_lwphandle *Lwp);
  96 void    Lfree(struct ps_lwphandle *Lwp);
  97 int     Lctlfd(struct ps_lwphandle *Lwp);
  98 int     Lwait(struct ps_lwphandle *Lwp, uint_t msec);
  99 int     Lstop(struct ps_lwphandle *Lwp, uint_t msec);
 100 int     Ldstop(struct ps_lwphandle *Lwp);
 101 int     Lstate(struct ps_lwphandle *Lwp);
 102 const lwpsinfo_t *Lpsinfo(struct ps_lwphandle *Lwp);
 103 const lwpstatus_t *Lstatus(struct ps_lwphandle *Lwp);
 104 int     Lgetareg(struct ps_lwphandle *Lwp, int regno, prgreg_t *preg);
 105 int     Lputareg(struct ps_lwphandle *Lwp, int regno, prgreg_t reg);
 106 int     Lsetrun(struct ps_lwphandle *Lwp, int sig, int flags);
 107 int     Lclearsig(struct ps_lwphandle *Lwp);
 108 int     Lclearfault(struct ps_lwphandle *Lwp);
 109 int     Lxecbkpt(struct ps_lwphandle *Lwp, ulong_t saved);
 110 int     Lxecwapt(struct ps_lwphandle *Lwp, const prwatch_t *wp);
 111 void    Lsync(struct ps_lwphandle *Lwp);
 112 
 113 /* Plwpregs.c */
 114 int Plwp_getregs(struct ps_prochandle *Pr, lwpid_t i, prgregset_t gr);
 115 int Plwp_setregs(struct ps_prochandle *Pr, lwpid_t i, const prgregset_t gr);
 116 int Plwp_getfpregs(struct ps_prochandle *Pr, lwpid_t i, prfpregset_t *fp);
 117 int Plwp_setfpregs(struct ps_prochandle *Pr, lwpid_t i, const prfpregset_t *fp);
 118 #if defined(sparc) || defined(__sparc)
 119 int Plwp_getxregs(struct ps_prochandle *Pr, lwpid_t i, prxregset_t *xr);
 120 int Plwp_setxregs(struct ps_prochandle *Pr, lwpid_t i, const prxregset_t *xr);
 121 #if defined(__sparcv9)
 122 int Plwp_getasrs(struct ps_prochandle *Pr, lwpid_t i, asrset_t asrs);
 123 int Plwp_setasrs(struct ps_prochandle *Pr, lwpid_t i, const asrset_t asrs);
 124 #endif  /* __sparcv9 */
 125 #endif  /* __sparc */
 126 int Plwp_getpsinfo(struct ps_prochandle *Pr, lwpid_t i, lwpsinfo_t *lps);
 127 
 128 /* Pcore.c */
 129 struct ps_prochandle *Pfgrab_core(int fd, const char *aout, int *perr);
 130 struct ps_prochandle *Pgrab_core(const char *core, const char *aout,
 131         int gflag, int *perr);
 132 
 133 /* Pisprocdir.c */
 134 int     Pisprocdir(struct ps_prochandle *Pr, const char *dir);
 135 
 136 /* Pservice.c */
 137 ps_err_e ps_pdmodel(struct ps_prochandle *Pr, int *modelp);
 138 ps_err_e ps_pread(struct ps_prochandle *Pr,
 139                 psaddr_t addr, void *buf, size_t size);
 140 ps_err_e ps_pwrite(struct ps_prochandle *Pr,
 141                 psaddr_t addr, const void *buf, size_t size);
 142 ps_err_e ps_pdread(struct ps_prochandle *Pr,
 143                 psaddr_t addr, void *buf, size_t size);
 144 ps_err_e ps_pdwrite(struct ps_prochandle *Pr,
 145                 psaddr_t addr, const void *buf, size_t size);
 146 ps_err_e ps_ptread(struct ps_prochandle *Pr,
 147                 psaddr_t addr, void *buf, size_t size);
 148 ps_err_e ps_ptwrite(struct ps_prochandle *Pr,
 149                 psaddr_t addr, const void *buf, size_t size);
 150 ps_err_e ps_pstop(struct ps_prochandle *Pr);
 151 ps_err_e ps_pcontinue(struct ps_prochandle *Pr);
 152 ps_err_e ps_lstop(struct ps_prochandle *Pr, lwpid_t lwpid);
 153 ps_err_e ps_lcontinue(struct ps_prochandle *Pr, lwpid_t lwpid);
 154 ps_err_e ps_lgetregs(struct ps_prochandle *Pr,
 155                 lwpid_t lwpid, prgregset_t regs);
 156 ps_err_e ps_lsetregs(struct ps_prochandle *Pr,
 157                 lwpid_t lwpid, const prgregset_t regs);
 158 ps_err_e ps_lgetfpregs(struct ps_prochandle *Pr,
 159                 lwpid_t lwpid, prfpregset_t *regs);
 160 ps_err_e ps_lsetfpregs(struct ps_prochandle *Pr,
 161                 lwpid_t lwpid, const prfpregset_t *regs);
 162 #if defined(sparc) || defined(__sparc)
 163 ps_err_e ps_lgetxregsize(struct ps_prochandle *Pr,
 164                 lwpid_t lwpid, int *xrsize);
 165 ps_err_e ps_lgetxregs(struct ps_prochandle *Pr,
 166                 lwpid_t lwpid, caddr_t xregs);
 167 ps_err_e ps_lsetxregs(struct ps_prochandle *Pr,
 168                 lwpid_t lwpid, caddr_t xregs);
 169 #endif  /* sparc */
 170 #if defined(__i386) || defined(__amd64)
 171 ps_err_e ps_lgetLDT(struct ps_prochandle *Pr,
 172                 lwpid_t lwpid, struct ssd *ldt);
 173 #endif  /* __i386 || __amd6464 */
 174 void    ps_plog(const char *fmt, ...);
 175 
 176 /* Psymtab.c */
 177 void    Pupdate_maps(struct ps_prochandle *Pr);
 178 void    Pupdate_syms(struct ps_prochandle *Pr);
 179 rd_agent_t *Prd_agent(struct ps_prochandle *Pr);
 180 const prmap_t *Paddr_to_map(struct ps_prochandle *Pr, uintptr_t addr);
 181 const prmap_t *Paddr_to_text_map(struct ps_prochandle *Pr, uintptr_t addr);
 182 const prmap_t *Pname_to_map(struct ps_prochandle *Pr, const char *name);
 183 const prmap_t *Plmid_to_map(struct ps_prochandle *Pr, Lmid_t lmid,
 184                 const char *name);
 185 int     Plookup_by_addr(struct ps_prochandle *Pr, uintptr_t addr,
 186                 char *sym_name_buffer, size_t bufsize, GElf_Sym *symbolp);
 187 int     Plookup_by_name(struct ps_prochandle *Pr,
 188                 const char *object_name, const char *symbol_name,
 189                 GElf_Sym *sym);
 190 int     Plookup_by_lmid(struct ps_prochandle *Pr,
 191                 Lmid_t lmid, const char *object_name, const char *symbol_name,
 192                 GElf_Sym *sym);
 193 const rd_loadobj_t *Paddr_to_loadobj(struct ps_prochandle *, uintptr_t);
 194 const rd_loadobj_t *Pname_to_loadobj(struct ps_prochandle *, const char *);
 195 const rd_loadobj_t *Plmid_to_loadobj(struct ps_prochandle *, Lmid_t,
 196                 const char *);
 197 int     Pmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd);
 198 int     Pmapping_iter_resolved(struct ps_prochandle *Pr, proc_map_f *func,
 199                 void *cd);
 200 int     Pobject_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd);
 201 int     Pobject_iter_resolved(struct ps_prochandle *Pr, proc_map_f *func,
 202                 void *cd);
 203 char    *Pobjname(struct ps_prochandle *Pr, uintptr_t addr,
 204                 char *buffer, size_t bufsize);
 205 char    *Pobjname_resolved(struct ps_prochandle *Pr, uintptr_t addr,
 206                 char *buffer, size_t bufsize);
 207 int     Plmid(struct ps_prochandle *Pr, uintptr_t addr, Lmid_t *lmidp);
 208 int     Psymbol_iter(struct ps_prochandle *Pr, const char *object_name,
 209                 int which, int type, proc_sym_f *func, void *cd);
 210 int     Psymbol_iter_by_lmid(struct ps_prochandle *Pr, Lmid_t lmid,
 211                 const char *object_name, int which, int type,
 212                 proc_sym_f *func, void *cd);
 213 char    *Pgetenv(struct ps_prochandle *Pr, const char *name,
 214                 char *buffer, size_t bufsize);
 215 char    *Pplatform(struct ps_prochandle *Pr, char *s, size_t n);
 216 int     Puname(struct ps_prochandle *Pr, struct utsname *u);
 217 char    *Pzonename(struct ps_prochandle *Pr, char *s, size_t n);
 218 char    *Pfindobj(struct ps_prochandle *Pr, const char *path,
 219                 char *s, size_t n);
 220 char    *Pexecname(struct ps_prochandle *Pr, char *buffer, size_t bufsize);
 221 void    Preset_maps(struct ps_prochandle *Pr);
 222 
 223 ps_err_e ps_pglobal_lookup(struct ps_prochandle *Pr,
 224                 const char *object_name, const char *sym_name,
 225                 psaddr_t *sym_addr);
 226 
 227 ps_err_e ps_pglobal_sym(struct ps_prochandle *Pr,
 228                 const char *object_name, const char *sym_name,
 229                 ps_sym_t *symp);
 230 
 231 long    Pgetauxval(struct ps_prochandle *Pr, int type);
 232 const auxv_t *Pgetauxvec(struct ps_prochandle *Pr);
 233 ps_err_e ps_pauxv(struct ps_prochandle *Pr, const auxv_t **aux);
 234 
 235 /* Putil.c */
 236 void    Perror_printf(struct ps_prochandle *Pr, const char *format, ...);
 237 
 238 /* pr_door.c */
 239 int     pr_door_info(struct ps_prochandle *Pr, int did, door_info_t *di);
 240 
 241 /* pr_exit.c */
 242 int     pr_exit(struct ps_prochandle *Pr, int status);
 243 int     pr_lwp_exit(struct ps_prochandle *Pr);
 244 
 245 /* pr_fcntl.c */
 246 int     pr_fcntl(struct ps_prochandle *Pr, int fd, int cmd, void *argp);
 247 
 248 /* pr_getitimer.c */
 249 int     pr_getitimer(struct ps_prochandle *Pr,
 250                 int which, struct itimerval *itv);
 251 int     pr_setitimer(struct ps_prochandle *Pr,
 252                 int which, const struct itimerval *itv, struct itimerval *oitv);
 253 
 254 /* pr_getrctl.c */
 255 int     pr_getrctl(struct ps_prochandle *Pr, const char *rname,
 256                 rctlblk_t *old_blk, rctlblk_t *new_blk, int rflag);
 257 int   pr_setrctl(struct ps_prochandle *Pr, const char *rname,
 258                 rctlblk_t *old_blk, rctlblk_t *new_blk, int rflag);
 259 int   pr_setprojrctl(struct ps_prochandle *Pr, const char *rname,
 260                 rctlblk_t *new_blk, size_t size, int rflag);
 261 
 262 /* pr_getrlimit.c */
 263 int     pr_getrlimit(struct ps_prochandle *Pr,
 264                 int resource, struct rlimit *rlp);
 265 int     pr_setrlimit(struct ps_prochandle *Pr,
 266                 int resource, const struct rlimit *rlp);
 267 int     pr_getrlimit64(struct ps_prochandle *Pr,
 268                 int resource, struct rlimit64 *rlp);
 269 int     pr_setrlimit64(struct ps_prochandle *Pr,
 270                 int resource, const struct rlimit64 *rlp);
 271 
 272 /* pr_getsockname.c */
 273 int     pr_getsockname(struct ps_prochandle *Pr,
 274                 int sock, struct sockaddr *name, socklen_t *namelen);
 275 int     pr_getpeername(struct ps_prochandle *Pr,
 276                 int sock, struct sockaddr *name, socklen_t *namelen);
 277 
 278 /* pr_ioctl.c */
 279 int     pr_ioctl(struct ps_prochandle *Pr,
 280                 int fd, int code, void *buf, size_t size);
 281 
 282 /* pr_lseek.c */
 283 off_t   pr_lseek(struct ps_prochandle *Pr,
 284                 int filedes, off_t offset, int whence);
 285 offset_t pr_llseek(struct ps_prochandle *Pr,
 286                 int filedes, offset_t offset, int whence);
 287 
 288 /* pr_memcntl.c */
 289 int     pr_memcntl(struct ps_prochandle *Pr,
 290         caddr_t addr, size_t len, int cmd, caddr_t arg, int attr, int mask);
 291 
 292 /* pr_mmap.c */
 293 void    *pr_mmap(struct ps_prochandle *Pr,
 294                 void *addr, size_t len, int prot, int flags, int fd, off_t off);
 295 int     pr_munmap(struct ps_prochandle *Pr,
 296                 void *addr, size_t len);
 297 void    *pr_zmap(struct ps_prochandle *Pr,
 298                 void *addr, size_t len, int prot, int flags);
 299 
 300 /* pr_open.c */
 301 int     pr_open(struct ps_prochandle *Pr,
 302                 const char *filename, int flags, mode_t mode);
 303 int     pr_creat(struct ps_prochandle *Pr,
 304                 const char *filename, mode_t mode);
 305 int     pr_close(struct ps_prochandle *Pr, int fd);
 306 int     pr_access(struct ps_prochandle *Pr, const char *path, int amode);
 307 
 308 /* pr_pbind.c */
 309 int     pr_processor_bind(struct ps_prochandle *Pr, idtype_t, id_t, int, int *);
 310 
 311 /* pr_rename.c */
 312 int     pr_rename(struct ps_prochandle *Pr, const char *old, const char *new);
 313 int     pr_link(struct ps_prochandle *Pr, const char *exist, const char *new);
 314 int     pr_unlink(struct ps_prochandle *Pr, const char *);
 315 
 316 /* pr_sigaction.c */
 317 int     pr_sigaction(struct ps_prochandle *Pr,
 318                 int sig, const struct sigaction *act, struct sigaction *oact);
 319 
 320 /* pr_stat.c */
 321 int     pr_stat(struct ps_prochandle *Pr, const char *path, struct stat *buf);
 322 int     pr_lstat(struct ps_prochandle *Pr, const char *path, struct stat *buf);
 323 int     pr_fstat(struct ps_prochandle *Pr, int fd, struct stat *buf);
 324 int     pr_stat64(struct ps_prochandle *Pr, const char *path,
 325                 struct stat64 *buf);
 326 int     pr_lstat64(struct ps_prochandle *Pr, const char *path,
 327                 struct stat64 *buf);
 328 int     pr_fstat64(struct ps_prochandle *Pr, int fd, struct stat64 *buf);
 329 
 330 /* pr_statvfs.c */
 331 int     pr_statvfs(struct ps_prochandle *Pr, const char *path, statvfs_t *buf);
 332 int     pr_fstatvfs(struct ps_prochandle *Pr, int fd, statvfs_t *buf);
 333 
 334 /* pr_tasksys.c */
 335 projid_t pr_getprojid(struct ps_prochandle *Pr);
 336 taskid_t pr_gettaskid(struct ps_prochandle *Pr);
 337 taskid_t pr_settaskid(struct ps_prochandle *Pr, projid_t project, int flags);
 338 
 339 /* pr_waitid.c */
 340 int     pr_waitid(struct ps_prochandle *Pr,
 341                 idtype_t idtype, id_t id, siginfo_t *infop, int options);
 342 
 343 /* proc_get_info.c */
 344 int     proc_get_cred(pid_t pid, prcred_t *credp, int ngroups);
 345 prpriv_t *proc_get_priv(pid_t pid);
 346 int     proc_get_psinfo(pid_t pid, psinfo_t *psp);
 347 int     proc_get_status(pid_t pid, pstatus_t *psp);
 348 int     proc_get_auxv(pid_t pid, auxv_t *pauxv, int naux);
 349 
 350 /* proc_names.c */
 351 char    *proc_fltname(int flt, char *buf, size_t bufsz);
 352 char    *proc_signame(int sig, char *buf, size_t bufsz);
 353 char    *proc_sysname(int sys, char *buf, size_t bufsz);
 354 
 355 int     proc_str2flt(const char *str, int *fltnum);
 356 int     proc_str2sig(const char *str, int *signum);
 357 int     proc_str2sys(const char *str, int *sysnum);
 358 
 359 char    *proc_fltset2str(const fltset_t *set, const char *delim, int members,
 360                 char *buf, size_t nbytes);
 361 char    *proc_sigset2str(const sigset_t *set, const char *delim, int members,
 362                 char *buf, size_t nbytes);
 363 char    *proc_sysset2str(const sysset_t *set, const char *delim, int members,
 364                 char *buf, size_t nbytes);
 365 
 366 char    *proc_str2fltset(const char *str, const char *delim, int members,
 367                 fltset_t *set);
 368 char    *proc_str2sigset(const char *str, const char *delim, int members,
 369                 sigset_t *set);
 370 char    *proc_str2sysset(const char *str, const char *delim, int members,
 371                 sysset_t *set);
 372 
 373 int     proc_walk(proc_walk_f *func, void *arg, int flags);
 374 
 375 /* proc_arg.c */
 376 struct ps_prochandle *proc_arg_grab(const char *arg,
 377         int oflag, int gflag, int *perr);
 378 
 379 pid_t   proc_arg_psinfo(const char *arg, int oflag, psinfo_t *psp, int *perr);
 380 void    proc_unctrl_psinfo(psinfo_t *psp);
 381 
 382 /* proc_set.c */
 383 int     Psetcred(struct ps_prochandle *Pr, const prcred_t *pcred);
 384 
 385 /* Pstack.c */
 386 int     Pstack_iter(struct ps_prochandle *Pr,
 387                 const prgregset_t regs, proc_stack_f *func, void *arg);
 388 
 389 /* Pisadep.c */
 390 const char *Ppltdest(struct ps_prochandle *Pr, uintptr_t addr);