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