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 int Plwp_getxregs(struct ps_prochandle *Pr, lwpid_t i, prxregset_t *xr);
 116 int Plwp_setxregs(struct ps_prochandle *Pr, lwpid_t i, const prxregset_t *xr);
 117 #if defined(__sparcv9)
 118 int Plwp_getasrs(struct ps_prochandle *Pr, lwpid_t i, asrset_t asrs);
 119 int Plwp_setasrs(struct ps_prochandle *Pr, lwpid_t i, const asrset_t asrs);
 120 #endif  /* __sparcv9 */
 121 int Plwp_getpsinfo(struct ps_prochandle *Pr, lwpid_t i, lwpsinfo_t *lps);
 122 
 123 /* Pcore.c */
 124 struct ps_prochandle *Pfgrab_core(int fd, const char *aout, int *perr);
 125 struct ps_prochandle *Pgrab_core(const char *core, const char *aout,
 126         int gflag, int *perr);
 127 
 128 /* Pisprocdir.c */
 129 int     Pisprocdir(struct ps_prochandle *Pr, const char *dir);
 130 
 131 /* Pservice.c */
 132 ps_err_e ps_pdmodel(struct ps_prochandle *Pr, int *modelp);
 133 ps_err_e ps_pread(struct ps_prochandle *Pr,
 134                 psaddr_t addr, void *buf, size_t size);
 135 ps_err_e ps_pwrite(struct ps_prochandle *Pr,
 136                 psaddr_t addr, const void *buf, size_t size);
 137 ps_err_e ps_pdread(struct ps_prochandle *Pr,
 138                 psaddr_t addr, void *buf, size_t size);
 139 ps_err_e ps_pdwrite(struct ps_prochandle *Pr,
 140                 psaddr_t addr, const void *buf, size_t size);
 141 ps_err_e ps_ptread(struct ps_prochandle *Pr,
 142                 psaddr_t addr, void *buf, size_t size);
 143 ps_err_e ps_ptwrite(struct ps_prochandle *Pr,
 144                 psaddr_t addr, const void *buf, size_t size);
 145 ps_err_e ps_pstop(struct ps_prochandle *Pr);
 146 ps_err_e ps_pcontinue(struct ps_prochandle *Pr);
 147 ps_err_e ps_lstop(struct ps_prochandle *Pr, lwpid_t lwpid);
 148 ps_err_e ps_lcontinue(struct ps_prochandle *Pr, lwpid_t lwpid);
 149 ps_err_e ps_lgetregs(struct ps_prochandle *Pr,
 150                 lwpid_t lwpid, prgregset_t regs);
 151 ps_err_e ps_lsetregs(struct ps_prochandle *Pr,
 152                 lwpid_t lwpid, const prgregset_t regs);
 153 ps_err_e ps_lgetfpregs(struct ps_prochandle *Pr,
 154                 lwpid_t lwpid, prfpregset_t *regs);
 155 ps_err_e ps_lsetfpregs(struct ps_prochandle *Pr,
 156                 lwpid_t lwpid, const prfpregset_t *regs);
 157 #if defined(sparc) || defined(__sparc)
 158 ps_err_e ps_lgetxregsize(struct ps_prochandle *Pr,
 159                 lwpid_t lwpid, int *xrsize);
 160 ps_err_e ps_lgetxregs(struct ps_prochandle *Pr,
 161                 lwpid_t lwpid, caddr_t xregs);
 162 ps_err_e ps_lsetxregs(struct ps_prochandle *Pr,
 163                 lwpid_t lwpid, caddr_t xregs);
 164 #endif  /* sparc */
 165 #if defined(__i386) || defined(__amd64)
 166 ps_err_e ps_lgetLDT(struct ps_prochandle *Pr,
 167                 lwpid_t lwpid, struct ssd *ldt);
 168 #endif  /* __i386 || __amd6464 */
 169 void    ps_plog(const char *fmt, ...);
 170 
 171 /* Psymtab.c */
 172 void    Pupdate_maps(struct ps_prochandle *Pr);
 173 void    Pupdate_syms(struct ps_prochandle *Pr);
 174 rd_agent_t *Prd_agent(struct ps_prochandle *Pr);
 175 const prmap_t *Paddr_to_map(struct ps_prochandle *Pr, uintptr_t addr);
 176 const prmap_t *Paddr_to_text_map(struct ps_prochandle *Pr, uintptr_t addr);
 177 const prmap_t *Pname_to_map(struct ps_prochandle *Pr, const char *name);
 178 const prmap_t *Plmid_to_map(struct ps_prochandle *Pr, Lmid_t lmid,
 179                 const char *name);
 180 int     Plookup_by_addr(struct ps_prochandle *Pr, uintptr_t addr,
 181                 char *sym_name_buffer, size_t bufsize, GElf_Sym *symbolp);
 182 int     Plookup_by_name(struct ps_prochandle *Pr,
 183                 const char *object_name, const char *symbol_name,
 184                 GElf_Sym *sym);
 185 int     Plookup_by_lmid(struct ps_prochandle *Pr,
 186                 Lmid_t lmid, const char *object_name, const char *symbol_name,
 187                 GElf_Sym *sym);
 188 const rd_loadobj_t *Paddr_to_loadobj(struct ps_prochandle *, uintptr_t);
 189 const rd_loadobj_t *Pname_to_loadobj(struct ps_prochandle *, const char *);
 190 const rd_loadobj_t *Plmid_to_loadobj(struct ps_prochandle *, Lmid_t,
 191                 const char *);
 192 int     Pmapping_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd);
 193 int     Pmapping_iter_resolved(struct ps_prochandle *Pr, proc_map_f *func,
 194                 void *cd);
 195 int     Pobject_iter(struct ps_prochandle *Pr, proc_map_f *func, void *cd);
 196 int     Pobject_iter_resolved(struct ps_prochandle *Pr, proc_map_f *func,
 197                 void *cd);
 198 char    *Pobjname(struct ps_prochandle *Pr, uintptr_t addr,
 199                 char *buffer, size_t bufsize);
 200 char    *Pobjname_resolved(struct ps_prochandle *Pr, uintptr_t addr,
 201                 char *buffer, size_t bufsize);
 202 int     Plmid(struct ps_prochandle *Pr, uintptr_t addr, Lmid_t *lmidp);
 203 int     Psymbol_iter(struct ps_prochandle *Pr, const char *object_name,
 204                 int which, int type, proc_sym_f *func, void *cd);
 205 int     Psymbol_iter_by_lmid(struct ps_prochandle *Pr, Lmid_t lmid,
 206                 const char *object_name, int which, int type,
 207                 proc_sym_f *func, void *cd);
 208 char    *Pgetenv(struct ps_prochandle *Pr, const char *name,
 209                 char *buffer, size_t bufsize);
 210 char    *Pplatform(struct ps_prochandle *Pr, char *s, size_t n);
 211 int     Puname(struct ps_prochandle *Pr, struct utsname *u);
 212 char    *Pzonename(struct ps_prochandle *Pr, char *s, size_t n);
 213 char    *Pfindobj(struct ps_prochandle *Pr, const char *path,
 214                 char *s, size_t n);
 215 char    *Pexecname(struct ps_prochandle *Pr, char *buffer, size_t bufsize);
 216 void    Preset_maps(struct ps_prochandle *Pr);
 217 
 218 ps_err_e ps_pglobal_lookup(struct ps_prochandle *Pr,
 219                 const char *object_name, const char *sym_name,
 220                 psaddr_t *sym_addr);
 221 
 222 ps_err_e ps_pglobal_sym(struct ps_prochandle *Pr,
 223                 const char *object_name, const char *sym_name,
 224                 ps_sym_t *symp);
 225 
 226 long    Pgetauxval(struct ps_prochandle *Pr, int type);
 227 const auxv_t *Pgetauxvec(struct ps_prochandle *Pr);
 228 ps_err_e ps_pauxv(struct ps_prochandle *Pr, const auxv_t **aux);
 229 
 230 /* Putil.c */
 231 void    Perror_printf(struct ps_prochandle *Pr, const char *format, ...);
 232 
 233 /* pr_door.c */
 234 int     pr_door_info(struct ps_prochandle *Pr, int did, door_info_t *di);
 235 
 236 /* pr_exit.c */
 237 int     pr_exit(struct ps_prochandle *Pr, int status);
 238 int     pr_lwp_exit(struct ps_prochandle *Pr);
 239 
 240 /* pr_fcntl.c */
 241 int     pr_fcntl(struct ps_prochandle *Pr, int fd, int cmd, void *argp);
 242 
 243 /* pr_getitimer.c */
 244 int     pr_getitimer(struct ps_prochandle *Pr,
 245                 int which, struct itimerval *itv);
 246 int     pr_setitimer(struct ps_prochandle *Pr,
 247                 int which, const struct itimerval *itv, struct itimerval *oitv);
 248 
 249 /* pr_getrctl.c */
 250 int     pr_getrctl(struct ps_prochandle *Pr, const char *rname,
 251                 rctlblk_t *old_blk, rctlblk_t *new_blk, int rflag);
 252 int   pr_setrctl(struct ps_prochandle *Pr, const char *rname,
 253                 rctlblk_t *old_blk, rctlblk_t *new_blk, int rflag);
 254 int   pr_setprojrctl(struct ps_prochandle *Pr, const char *rname,
 255                 rctlblk_t *new_blk, size_t size, int rflag);
 256 
 257 /* pr_getrlimit.c */
 258 int     pr_getrlimit(struct ps_prochandle *Pr,
 259                 int resource, struct rlimit *rlp);
 260 int     pr_setrlimit(struct ps_prochandle *Pr,
 261                 int resource, const struct rlimit *rlp);
 262 int     pr_getrlimit64(struct ps_prochandle *Pr,
 263                 int resource, struct rlimit64 *rlp);
 264 int     pr_setrlimit64(struct ps_prochandle *Pr,
 265                 int resource, const struct rlimit64 *rlp);
 266 
 267 /* pr_getsockname.c */
 268 int     pr_getsockname(struct ps_prochandle *Pr,
 269                 int sock, struct sockaddr *name, socklen_t *namelen);
 270 int     pr_getpeername(struct ps_prochandle *Pr,
 271                 int sock, struct sockaddr *name, socklen_t *namelen);
 272 
 273 /* pr_ioctl.c */
 274 int     pr_ioctl(struct ps_prochandle *Pr,
 275                 int fd, int code, void *buf, size_t size);
 276 
 277 /* pr_lseek.c */
 278 off_t   pr_lseek(struct ps_prochandle *Pr,
 279                 int filedes, off_t offset, int whence);
 280 offset_t pr_llseek(struct ps_prochandle *Pr,
 281                 int filedes, offset_t offset, int whence);
 282 
 283 /* pr_memcntl.c */
 284 int     pr_memcntl(struct ps_prochandle *Pr,
 285         caddr_t addr, size_t len, int cmd, caddr_t arg, int attr, int mask);
 286 
 287 /* pr_mmap.c */
 288 void    *pr_mmap(struct ps_prochandle *Pr,
 289                 void *addr, size_t len, int prot, int flags, int fd, off_t off);
 290 int     pr_munmap(struct ps_prochandle *Pr,
 291                 void *addr, size_t len);
 292 void    *pr_zmap(struct ps_prochandle *Pr,
 293                 void *addr, size_t len, int prot, int flags);
 294 
 295 /* pr_open.c */
 296 int     pr_open(struct ps_prochandle *Pr,
 297                 const char *filename, int flags, mode_t mode);
 298 int     pr_creat(struct ps_prochandle *Pr,
 299                 const char *filename, mode_t mode);
 300 int     pr_close(struct ps_prochandle *Pr, int fd);
 301 int     pr_access(struct ps_prochandle *Pr, const char *path, int amode);
 302 
 303 /* pr_pbind.c */
 304 int     pr_processor_bind(struct ps_prochandle *Pr, idtype_t, id_t, int, int *);
 305 
 306 /* pr_rename.c */
 307 int     pr_rename(struct ps_prochandle *Pr, const char *old, const char *new);
 308 int     pr_link(struct ps_prochandle *Pr, const char *exist, const char *new);
 309 int     pr_unlink(struct ps_prochandle *Pr, const char *);
 310 
 311 /* pr_sigaction.c */
 312 int     pr_sigaction(struct ps_prochandle *Pr,
 313                 int sig, const struct sigaction *act, struct sigaction *oact);
 314 
 315 /* pr_stat.c */
 316 int     pr_stat(struct ps_prochandle *Pr, const char *path, struct stat *buf);
 317 int     pr_lstat(struct ps_prochandle *Pr, const char *path, struct stat *buf);
 318 int     pr_fstat(struct ps_prochandle *Pr, int fd, struct stat *buf);
 319 int     pr_stat64(struct ps_prochandle *Pr, const char *path,
 320                 struct stat64 *buf);
 321 int     pr_lstat64(struct ps_prochandle *Pr, const char *path,
 322                 struct stat64 *buf);
 323 int     pr_fstat64(struct ps_prochandle *Pr, int fd, struct stat64 *buf);
 324 
 325 /* pr_statvfs.c */
 326 int     pr_statvfs(struct ps_prochandle *Pr, const char *path, statvfs_t *buf);
 327 int     pr_fstatvfs(struct ps_prochandle *Pr, int fd, statvfs_t *buf);
 328 
 329 /* pr_tasksys.c */
 330 projid_t pr_getprojid(struct ps_prochandle *Pr);
 331 taskid_t pr_gettaskid(struct ps_prochandle *Pr);
 332 taskid_t pr_settaskid(struct ps_prochandle *Pr, projid_t project, int flags);
 333 
 334 /* pr_waitid.c */
 335 int     pr_waitid(struct ps_prochandle *Pr,
 336                 idtype_t idtype, id_t id, siginfo_t *infop, int options);
 337 
 338 /* proc_get_info.c */
 339 int     proc_get_cred(pid_t pid, prcred_t *credp, int ngroups);
 340 prpriv_t *proc_get_priv(pid_t pid);
 341 int     proc_get_psinfo(pid_t pid, psinfo_t *psp);
 342 int     proc_get_status(pid_t pid, pstatus_t *psp);
 343 int     proc_get_auxv(pid_t pid, auxv_t *pauxv, int naux);
 344 
 345 /* proc_names.c */
 346 char    *proc_fltname(int flt, char *buf, size_t bufsz);
 347 char    *proc_signame(int sig, char *buf, size_t bufsz);
 348 char    *proc_sysname(int sys, char *buf, size_t bufsz);
 349 
 350 int     proc_str2flt(const char *str, int *fltnum);
 351 int     proc_str2sig(const char *str, int *signum);
 352 int     proc_str2sys(const char *str, int *sysnum);
 353 
 354 char    *proc_fltset2str(const fltset_t *set, const char *delim, int members,
 355                 char *buf, size_t nbytes);
 356 char    *proc_sigset2str(const sigset_t *set, const char *delim, int members,
 357                 char *buf, size_t nbytes);
 358 char    *proc_sysset2str(const sysset_t *set, const char *delim, int members,
 359                 char *buf, size_t nbytes);
 360 
 361 char    *proc_str2fltset(const char *str, const char *delim, int members,
 362                 fltset_t *set);
 363 char    *proc_str2sigset(const char *str, const char *delim, int members,
 364                 sigset_t *set);
 365 char    *proc_str2sysset(const char *str, const char *delim, int members,
 366                 sysset_t *set);
 367 
 368 int     proc_walk(proc_walk_f *func, void *arg, int flags);
 369 
 370 /* proc_arg.c */
 371 struct ps_prochandle *proc_arg_grab(const char *arg,
 372         int oflag, int gflag, int *perr);
 373 
 374 pid_t   proc_arg_psinfo(const char *arg, int oflag, psinfo_t *psp, int *perr);
 375 void    proc_unctrl_psinfo(psinfo_t *psp);
 376 
 377 /* proc_set.c */
 378 int     Psetcred(struct ps_prochandle *Pr, const prcred_t *pcred);
 379 
 380 /* Pstack.c */
 381 int     Pstack_iter(struct ps_prochandle *Pr,
 382                 const prgregset_t regs, proc_stack_f *func, void *arg);
 383 
 384 /* Pisadep.c */
 385 const char *Ppltdest(struct ps_prochandle *Pr, uintptr_t addr);