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