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 /*
  22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #include <stdarg.h>
  27 #include <string.h>
  28 #include "Pcontrol.h"
  29 
  30 /*
  31  * This file implements the process services declared in <proc_service.h>.
  32  * This enables libproc to be used in conjunction with libc_db and
  33  * librtld_db.  As most of these facilities are already provided by
  34  * (more elegant) interfaces in <libproc.h>, we can just call those.
  35  *
  36  * NOTE: We explicitly do *not* implement the functions ps_kill() and
  37  * ps_lrolltoaddr() in this library.  The very existence of these functions
  38  * causes libc_db to create an "agent thread" in the target process.
  39  * The only way to turn off this behavior is to omit these functions.
  40  */
  41 
  42 #pragma weak ps_pdread = ps_pread
  43 #pragma weak ps_ptread = ps_pread
  44 #pragma weak ps_pdwrite = ps_pwrite
  45 #pragma weak ps_ptwrite = ps_pwrite
  46 
  47 ps_err_e
  48 ps_pdmodel(struct ps_prochandle *P, int *modelp)
  49 {
  50         *modelp = P->status.pr_dmodel;
  51         return (PS_OK);
  52 }
  53 
  54 ps_err_e
  55 ps_pread(struct ps_prochandle *P, psaddr_t addr, void *buf, size_t size)
  56 {
  57         if (P->ops->p_pread(P, buf, size, addr) != size)
  58                 return (PS_BADADDR);
  59         return (PS_OK);
  60 }
  61 
  62 ps_err_e
  63 ps_pwrite(struct ps_prochandle *P, psaddr_t addr, const void *buf, size_t size)
  64 {
  65         if (P->ops->p_pwrite(P, buf, size, addr) != size)
  66                 return (PS_BADADDR);
  67         return (PS_OK);
  68 }
  69 
  70 /*
  71  * libc_db calls matched pairs of ps_pstop()/ps_pcontinue()
  72  * in the belief that the client may have left the process
  73  * running while calling in to the libc_db interfaces.
  74  *
  75  * We interpret the meaning of these functions to be an inquiry
  76  * as to whether the process is stopped, not an action to be
  77  * performed to make it stopped.  For similar reasons, we also
  78  * return PS_OK for core files in order to allow libc_db to
  79  * operate on these as well.
  80  */
  81 ps_err_e
  82 ps_pstop(struct ps_prochandle *P)
  83 {
  84         if (P->state != PS_STOP && P->state != PS_DEAD)
  85                 return (PS_ERR);
  86         return (PS_OK);
  87 }
  88 
  89 ps_err_e
  90 ps_pcontinue(struct ps_prochandle *P)
  91 {
  92         if (P->state != PS_STOP && P->state != PS_DEAD)
  93                 return (PS_ERR);
  94         return (PS_OK);
  95 }
  96 
  97 /*
  98  * ps_lstop() and ps_lcontinue() are not called by any code in libc_db
  99  * or librtld_db.  We make them behave like ps_pstop() and ps_pcontinue().
 100  */
 101 /* ARGSUSED1 */
 102 ps_err_e
 103 ps_lstop(struct ps_prochandle *P, lwpid_t lwpid)
 104 {
 105         if (P->state != PS_STOP && P->state != PS_DEAD)
 106                 return (PS_ERR);
 107         return (PS_OK);
 108 }
 109 
 110 /* ARGSUSED1 */
 111 ps_err_e
 112 ps_lcontinue(struct ps_prochandle *P, lwpid_t lwpid)
 113 {
 114         if (P->state != PS_STOP && P->state != PS_DEAD)
 115                 return (PS_ERR);
 116         return (PS_OK);
 117 }
 118 
 119 ps_err_e
 120 ps_lgetregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t regs)
 121 {
 122         if (P->state != PS_STOP && P->state != PS_DEAD)
 123                 return (PS_ERR);
 124 
 125         if (Plwp_getregs(P, lwpid, regs) == 0)
 126                 return (PS_OK);
 127 
 128         return (PS_BADLID);
 129 }
 130 
 131 ps_err_e
 132 ps_lsetregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t regs)
 133 {
 134         if (P->state != PS_STOP)
 135                 return (PS_ERR);
 136 
 137         if (Plwp_setregs(P, lwpid, regs) == 0)
 138                 return (PS_OK);
 139 
 140         return (PS_BADLID);
 141 }
 142 
 143 ps_err_e
 144 ps_lgetfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *regs)
 145 {
 146         if (P->state != PS_STOP && P->state != PS_DEAD)
 147                 return (PS_ERR);
 148 
 149         if (Plwp_getfpregs(P, lwpid, regs) == 0)
 150                 return (PS_OK);
 151 
 152         return (PS_BADLID);
 153 }
 154 
 155 ps_err_e
 156 ps_lsetfpregs(struct ps_prochandle *P, lwpid_t lwpid, const prfpregset_t *regs)
 157 {
 158         if (P->state != PS_STOP)
 159                 return (PS_ERR);
 160 
 161         if (Plwp_setfpregs(P, lwpid, regs) == 0)
 162                 return (PS_OK);
 163 
 164         return (PS_BADLID);
 165 }
 166 
 167 ps_err_e
 168 ps_lgetxregsize(struct ps_prochandle *P, lwpid_t lwpid, int *xrsize)
 169 {
 170         char fname[PATH_MAX];
 171         struct stat statb;
 172 
 173         if (P->state == PS_DEAD) {
 174                 lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
 175                 uint_t i;
 176 
 177                 for (i = 0; i < P->core->core_nlwp; i++, lwp = list_next(lwp)) {
 178                         if (lwp->lwp_id == lwpid) {
 179                                 if (lwp->lwp_xregs != NULL)
 180                                         *xrsize = sizeof (prxregset_t);
 181                                 else
 182                                         *xrsize = 0;
 183                                 return (PS_OK);
 184                         }
 185                 }
 186 
 187                 return (PS_BADLID);
 188         }
 189 
 190         (void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/xregs",
 191             procfs_path, (int)P->status.pr_pid, (int)lwpid);
 192 
 193         if (stat(fname, &statb) != 0)
 194                 return (PS_BADLID);
 195 
 196         *xrsize = (int)statb.st_size;
 197         return (PS_OK);
 198 }
 199 
 200 ps_err_e
 201 ps_lgetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
 202 {
 203         if (P->state != PS_STOP && P->state != PS_DEAD)
 204                 return (PS_ERR);
 205 
 206         /* LINTED - alignment */
 207         if (Plwp_getxregs(P, lwpid, (prxregset_t *)xregs) == 0)
 208                 return (PS_OK);
 209 
 210         return (PS_BADLID);
 211 }
 212 
 213 ps_err_e
 214 ps_lsetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
 215 {
 216         if (P->state != PS_STOP)
 217                 return (PS_ERR);
 218 
 219         /* LINTED - alignment */
 220         if (Plwp_setxregs(P, lwpid, (prxregset_t *)xregs) == 0)
 221                 return (PS_OK);
 222 
 223         return (PS_BADLID);
 224 }
 225 
 226 #if defined(__i386) || defined(__amd64)
 227 
 228 ps_err_e
 229 ps_lgetLDT(struct ps_prochandle *P, lwpid_t lwpid, struct ssd *ldt)
 230 {
 231 #if defined(__amd64) && defined(_LP64)
 232         if (P->status.pr_dmodel != PR_MODEL_NATIVE) {
 233 #endif
 234         prgregset_t regs;
 235         struct ssd *ldtarray;
 236         ps_err_e error;
 237         uint_t gs;
 238         int nldt;
 239         int i;
 240 
 241         if (P->state != PS_STOP && P->state != PS_DEAD)
 242                 return (PS_ERR);
 243 
 244         /*
 245          * We need to get the ldt entry that matches the
 246          * value in the lwp's GS register.
 247          */
 248         if ((error = ps_lgetregs(P, lwpid, regs)) != PS_OK)
 249                 return (error);
 250 
 251         gs = regs[GS];
 252 
 253         if ((nldt = Pldt(P, NULL, 0)) <= 0 ||
 254             (ldtarray = malloc(nldt * sizeof (struct ssd))) == NULL)
 255                 return (PS_ERR);
 256         if ((nldt = Pldt(P, ldtarray, nldt)) <= 0) {
 257                 free(ldtarray);
 258                 return (PS_ERR);
 259         }
 260 
 261         for (i = 0; i < nldt; i++) {
 262                 if (gs == ldtarray[i].sel) {
 263                         *ldt = ldtarray[i];
 264                         break;
 265                 }
 266         }
 267         free(ldtarray);
 268 
 269         if (i < nldt)
 270                 return (PS_OK);
 271 #if defined(__amd64) && defined(_LP64)
 272         }
 273 #endif
 274 
 275         return (PS_ERR);
 276 }
 277 
 278 #endif  /* __i386 || __amd64 */
 279 
 280 /*
 281  * Libthread_db doesn't use this function currently, but librtld_db uses
 282  * it for its debugging output.  We turn this on via rd_log if our debugging
 283  * switch is on, and then echo the messages sent to ps_plog to stderr.
 284  */
 285 void
 286 ps_plog(const char *fmt, ...)
 287 {
 288         va_list ap;
 289 
 290         if (_libproc_debug && fmt != NULL && *fmt != '\0') {
 291                 va_start(ap, fmt);
 292                 (void) vfprintf(stderr, fmt, ap);
 293                 va_end(ap);
 294                 if (fmt[strlen(fmt) - 1] != '\n')
 295                         (void) fputc('\n', stderr);
 296         }
 297 }
 298 
 299 /*
 300  * Store a pointer to our internal copy of the aux vector at the address
 301  * specified by the caller.  It should not hold on to this data for too long.
 302  */
 303 ps_err_e
 304 ps_pauxv(struct ps_prochandle *P, const auxv_t **aux)
 305 {
 306         if (P->auxv == NULL)
 307                 Preadauxvec(P);
 308 
 309         if (P->auxv == NULL)
 310                 return (PS_ERR);
 311 
 312         *aux = (const auxv_t *)P->auxv;
 313         return (PS_OK);
 314 }
 315 
 316 ps_err_e
 317 ps_pbrandname(struct ps_prochandle *P, char *buf, size_t len)
 318 {
 319         return (Pbrandname(P, buf, len) ? PS_OK : PS_ERR);
 320 }
 321 
 322 /*
 323  * Search for a symbol by name and return the corresponding address.
 324  */
 325 ps_err_e
 326 ps_pglobal_lookup(struct ps_prochandle *P, const char *object_name,
 327         const char *sym_name, psaddr_t *sym_addr)
 328 {
 329         GElf_Sym sym;
 330 
 331         if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
 332                 dprintf("pglobal_lookup <%s> -> %p\n",
 333                     sym_name, (void *)(uintptr_t)sym.st_value);
 334                 *sym_addr = (psaddr_t)sym.st_value;
 335                 return (PS_OK);
 336         }
 337 
 338         return (PS_NOSYM);
 339 }
 340 
 341 /*
 342  * Search for a symbol by name and return the corresponding symbol
 343  * information.  If we're compiled _LP64, we just call Plookup_by_name
 344  * and return because ps_sym_t is defined to be an Elf64_Sym, which
 345  * is the same as a GElf_Sym.  In the _ILP32 case, we have to convert
 346  * Plookup_by_name's result back to a ps_sym_t (which is an Elf32_Sym).
 347  */
 348 ps_err_e
 349 ps_pglobal_sym(struct ps_prochandle *P, const char *object_name,
 350         const char *sym_name, ps_sym_t *symp)
 351 {
 352 #if defined(_ILP32)
 353         GElf_Sym sym;
 354 
 355         if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
 356                 symp->st_name = (Elf32_Word)sym.st_name;
 357                 symp->st_value = (Elf32_Addr)sym.st_value;
 358                 symp->st_size = (Elf32_Word)sym.st_size;
 359                 symp->st_info = ELF32_ST_INFO(
 360                     GELF_ST_BIND(sym.st_info), GELF_ST_TYPE(sym.st_info));
 361                 symp->st_other = sym.st_other;
 362                 symp->st_shndx = sym.st_shndx;
 363                 return (PS_OK);
 364         }
 365 
 366 #elif defined(_LP64)
 367         if (Plookup_by_name(P, object_name, sym_name, symp) == 0)
 368                 return (PS_OK);
 369 #endif
 370         return (PS_NOSYM);
 371 }