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