Pservice.c revision 2
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <stdarg.h>
2N/A#include <string.h>
2N/A#include "Pcontrol.h"
2N/A
2N/A/*
2N/A * This file implements the process services declared in <proc_service.h>.
2N/A * This enables libproc to be used in conjunction with libc_db and
2N/A * librtld_db. As most of these facilities are already provided by
2N/A * (more elegant) interfaces in <libproc.h>, we can just call those.
2N/A *
2N/A * NOTE: We explicitly do *not* implement the functions ps_kill() and
2N/A * ps_lrolltoaddr() in this library. The very existence of these functions
2N/A * causes libc_db to create an "agent thread" in the target process.
2N/A * The only way to turn off this behavior is to omit these functions.
2N/A */
2N/A
2N/A#pragma weak ps_pdread = ps_pread
2N/A#pragma weak ps_ptread = ps_pread
2N/A#pragma weak ps_pdwrite = ps_pwrite
2N/A#pragma weak ps_ptwrite = ps_pwrite
2N/A
2N/Aps_err_e
2N/Aps_pdmodel(struct ps_prochandle *P, int *modelp)
2N/A{
2N/A *modelp = P->status.pr_dmodel;
2N/A return (PS_OK);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_pread(struct ps_prochandle *P, psaddr_t addr, void *buf, size_t size)
2N/A{
2N/A if (P->ops->p_pread(P, buf, size, addr) != size)
2N/A return (PS_BADADDR);
2N/A return (PS_OK);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_pwrite(struct ps_prochandle *P, psaddr_t addr, const void *buf, size_t size)
2N/A{
2N/A if (P->ops->p_pwrite(P, buf, size, addr) != size)
2N/A return (PS_BADADDR);
2N/A return (PS_OK);
2N/A}
2N/A
2N/A/*
2N/A * libc_db calls matched pairs of ps_pstop()/ps_pcontinue()
2N/A * in the belief that the client may have left the process
2N/A * running while calling in to the libc_db interfaces.
2N/A *
2N/A * We interpret the meaning of these functions to be an inquiry
2N/A * as to whether the process is stopped, not an action to be
2N/A * performed to make it stopped. For similar reasons, we also
2N/A * return PS_OK for core files in order to allow libc_db to
2N/A * operate on these as well.
2N/A */
2N/Aps_err_e
2N/Aps_pstop(struct ps_prochandle *P)
2N/A{
2N/A if (P->state != PS_STOP && P->state != PS_DEAD)
2N/A return (PS_ERR);
2N/A return (PS_OK);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_pcontinue(struct ps_prochandle *P)
2N/A{
2N/A if (P->state != PS_STOP && P->state != PS_DEAD)
2N/A return (PS_ERR);
2N/A return (PS_OK);
2N/A}
2N/A
2N/A/*
2N/A * ps_lstop() and ps_lcontinue() are not called by any code in libc_db
2N/A * or librtld_db. We make them behave like ps_pstop() and ps_pcontinue().
2N/A */
2N/A/* ARGSUSED1 */
2N/Aps_err_e
2N/Aps_lstop(struct ps_prochandle *P, lwpid_t lwpid)
2N/A{
2N/A if (P->state != PS_STOP && P->state != PS_DEAD)
2N/A return (PS_ERR);
2N/A return (PS_OK);
2N/A}
2N/A
2N/A/* ARGSUSED1 */
2N/Aps_err_e
2N/Aps_lcontinue(struct ps_prochandle *P, lwpid_t lwpid)
2N/A{
2N/A if (P->state != PS_STOP && P->state != PS_DEAD)
2N/A return (PS_ERR);
2N/A return (PS_OK);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_lgetregs(struct ps_prochandle *P, lwpid_t lwpid, prgregset_t regs)
2N/A{
2N/A if (P->state != PS_STOP && P->state != PS_DEAD)
2N/A return (PS_ERR);
2N/A
2N/A if (Plwp_getregs(P, lwpid, regs) == 0)
2N/A return (PS_OK);
2N/A
2N/A return (PS_BADLID);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_lsetregs(struct ps_prochandle *P, lwpid_t lwpid, const prgregset_t regs)
2N/A{
2N/A if (P->state != PS_STOP)
2N/A return (PS_ERR);
2N/A
2N/A if (Plwp_setregs(P, lwpid, regs) == 0)
2N/A return (PS_OK);
2N/A
2N/A return (PS_BADLID);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_lgetfpregs(struct ps_prochandle *P, lwpid_t lwpid, prfpregset_t *regs)
2N/A{
2N/A if (P->state != PS_STOP && P->state != PS_DEAD)
2N/A return (PS_ERR);
2N/A
2N/A if (Plwp_getfpregs(P, lwpid, regs) == 0)
2N/A return (PS_OK);
2N/A
2N/A return (PS_BADLID);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_lsetfpregs(struct ps_prochandle *P, lwpid_t lwpid, const prfpregset_t *regs)
2N/A{
2N/A if (P->state != PS_STOP)
2N/A return (PS_ERR);
2N/A
2N/A if (Plwp_setfpregs(P, lwpid, regs) == 0)
2N/A return (PS_OK);
2N/A
2N/A return (PS_BADLID);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_lgetxregsize(struct ps_prochandle *P, lwpid_t lwpid, int *xrsize)
2N/A{
2N/A char fname[PATH_MAX];
2N/A struct stat statb;
2N/A
2N/A if (P->state == PS_DEAD) {
2N/A lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
2N/A uint_t i;
2N/A
2N/A for (i = 0; i < P->core->core_nlwp; i++, lwp = list_next(lwp)) {
2N/A if (lwp->lwp_id == lwpid) {
2N/A if (lwp->lwp_xregs != NULL)
2N/A *xrsize = sizeof (prxregset_t);
2N/A else {
2N/A *xrsize = 0;
2N/A#if defined(__i386) || defined(__amd64)
2N/A /*
2N/A * In x86 we return PS_NOXREGS when AVX
2N/A * extension is not found.
2N/A */
2N/A return (PS_NOXREGS);
2N/A#endif
2N/A }
2N/A return (PS_OK);
2N/A }
2N/A }
2N/A
2N/A return (PS_BADLID);
2N/A }
2N/A
2N/A (void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/xregs",
2N/A procfs_path, (int)P->status.pr_pid, (int)lwpid);
2N/A
2N/A if (stat(fname, &statb) != 0)
2N/A return (PS_BADLID);
2N/A
2N/A *xrsize = (int)statb.st_size;
2N/A
2N/A#if defined(__i386) || defined(__amd64)
2N/A if (*xrsize == 0) {
2N/A /*
2N/A * In x86 we return PS_NOXREGS when AVX extension is not found.
2N/A */
2N/A return (PS_NOXREGS);
2N/A }
2N/A#endif
2N/A
2N/A return (PS_OK);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_lgetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
2N/A{
2N/A#if defined(__i386) || defined(__amd64)
2N/A ps_err_e err;
2N/A int xrsize;
2N/A#endif
2N/A
2N/A if (P->state != PS_STOP && P->state != PS_DEAD)
2N/A return (PS_ERR);
2N/A
2N/A /* LINTED - alignment */
2N/A if (Plwp_getxregs(P, lwpid, (prxregset_t *)xregs) == 0)
2N/A return (PS_OK);
2N/A
2N/A#if defined(__i386) || defined(__amd64)
2N/A err = ps_lgetxregsize(P, lwpid, &xrsize);
2N/A if (err == PS_NOXREGS || (err == PS_OK && xrsize == 0)) {
2N/A return (PS_NOXREGS);
2N/A }
2N/A#endif
2N/A
2N/A return (PS_BADLID);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_lsetxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t xregs)
2N/A{
2N/A#if defined(__i386) || defined(__amd64)
2N/A ps_err_e err;
2N/A int xrsize;
2N/A#endif
2N/A
2N/A if (P->state != PS_STOP)
2N/A return (PS_ERR);
2N/A
2N/A /* LINTED - alignment */
2N/A if (Plwp_setxregs(P, lwpid, (prxregset_t *)xregs) == 0)
2N/A return (PS_OK);
2N/A
2N/A#if defined(__i386) || defined(__amd64)
2N/A err = ps_lgetxregsize(P, lwpid, &xrsize);
2N/A if (err == PS_NOXREGS || (err == PS_OK && xrsize == 0)) {
2N/A return (PS_NOXREGS);
2N/A }
2N/A#endif
2N/A
2N/A return (PS_BADLID);
2N/A}
2N/A
2N/A#if defined(__sparc)
2N/A
2N/A/* Extended CPU registers support */
2N/A
2N/Aps_err_e
2N/Aps_lgetcxregsize(struct ps_prochandle *P, lwpid_t lwpid,
2N/A int *cxrsize) {
2N/A char fname[PATH_MAX];
2N/A struct stat statb;
2N/A
2N/A if (P->state == PS_DEAD) {
2N/A lwp_info_t *lwp = list_next(&P->core->core_lwp_head);
2N/A uint_t i;
2N/A
2N/A for (i = 0; i < P->core->core_nlwp; i++, lwp = list_next(lwp)) {
2N/A if (lwp->lwp_id == lwpid) {
2N/A if (lwp->lwp_cxregs != NULL)
2N/A *cxrsize = lwp->lwp_cxsize;
2N/A else {
2N/A *cxrsize = 0;
2N/A return (PS_NOCXREGS);
2N/A }
2N/A return (PS_OK);
2N/A }
2N/A }
2N/A
2N/A return (PS_BADLID);
2N/A }
2N/A
2N/A (void) snprintf(fname, sizeof (fname), "%s/%d/lwp/%d/cxregs",
2N/A procfs_path, (int)P->status.pr_pid, (int)lwpid);
2N/A
2N/A if (stat(fname, &statb) != 0)
2N/A return (PS_BADLID);
2N/A
2N/A *cxrsize = (int)statb.st_size;
2N/A
2N/A if (*cxrsize == 0) {
2N/A return (PS_NOCXREGS);
2N/A }
2N/A
2N/A return (PS_OK);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_lgetcxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t cxregs)
2N/A{
2N/A if (P->state != PS_STOP && P->state != PS_DEAD)
2N/A return (PS_ERR);
2N/A
2N/A /*LINTED ALIGNMENT*/
2N/A if (Plwp_getcxregs(P, lwpid, (prcpuxregset_t *)cxregs) == 0)
2N/A return (PS_OK);
2N/A
2N/A return (PS_BADLID);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_lsetcxregs(struct ps_prochandle *P, lwpid_t lwpid, caddr_t cxregs)
2N/A{
2N/A if (P->state != PS_STOP)
2N/A return (PS_ERR);
2N/A
2N/A /*LINTED ALIGNMENT*/
2N/A if (Plwp_setcxregs(P, lwpid, (prcpuxregset_t *)cxregs) == 0)
2N/A return (PS_OK);
2N/A
2N/A return (PS_BADLID);
2N/A}
2N/A#endif /* __sparc */
2N/A
2N/A#if defined(__i386) || defined(__amd64)
2N/A
2N/Aps_err_e
2N/Aps_lgetLDT(struct ps_prochandle *P, lwpid_t lwpid, struct ssd *ldt)
2N/A{
2N/A#if defined(__amd64) && defined(_LP64)
2N/A if (P->status.pr_dmodel != PR_MODEL_NATIVE) {
2N/A#endif
2N/A prgregset_t regs;
2N/A struct ssd *ldtarray;
2N/A ps_err_e error;
2N/A uint_t gs;
2N/A int nldt;
2N/A int i;
2N/A
2N/A if (P->state != PS_STOP && P->state != PS_DEAD)
2N/A return (PS_ERR);
2N/A
2N/A /*
2N/A * We need to get the ldt entry that matches the
2N/A * value in the lwp's GS register.
2N/A */
2N/A if ((error = ps_lgetregs(P, lwpid, regs)) != PS_OK)
2N/A return (error);
2N/A
2N/A gs = regs[GS];
2N/A
2N/A if ((nldt = Pldt(P, NULL, 0)) <= 0 ||
2N/A (ldtarray = malloc(nldt * sizeof (struct ssd))) == NULL)
2N/A return (PS_ERR);
2N/A if ((nldt = Pldt(P, ldtarray, nldt)) <= 0) {
2N/A free(ldtarray);
2N/A return (PS_ERR);
2N/A }
2N/A
2N/A for (i = 0; i < nldt; i++) {
2N/A if (gs == ldtarray[i].sel) {
2N/A *ldt = ldtarray[i];
2N/A break;
2N/A }
2N/A }
2N/A free(ldtarray);
2N/A
2N/A if (i < nldt)
2N/A return (PS_OK);
2N/A#if defined(__amd64) && defined(_LP64)
2N/A }
2N/A#endif
2N/A
2N/A return (PS_ERR);
2N/A}
2N/A
2N/A#endif /* __i386 || __amd64 */
2N/A
2N/A/*
2N/A * Libthread_db doesn't use this function currently, but librtld_db uses
2N/A * it for its debugging output. We turn this on via rd_log if our debugging
2N/A * switch is on, and then echo the messages sent to ps_plog to stderr.
2N/A */
2N/Avoid
2N/Aps_plog(const char *fmt, ...)
2N/A{
2N/A va_list ap;
2N/A
2N/A if (_libproc_debug && fmt != NULL && *fmt != '\0') {
2N/A va_start(ap, fmt);
2N/A (void) vfprintf(stderr, fmt, ap);
2N/A va_end(ap);
2N/A if (fmt[strlen(fmt) - 1] != '\n')
2N/A (void) fputc('\n', stderr);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Store a pointer to our internal copy of the aux vector at the address
2N/A * specified by the caller. It should not hold on to this data for too long.
2N/A */
2N/Aps_err_e
2N/Aps_pauxv(struct ps_prochandle *P, const auxv_t **aux)
2N/A{
2N/A if (P->auxv == NULL)
2N/A Preadauxvec(P);
2N/A
2N/A if (P->auxv == NULL)
2N/A return (PS_ERR);
2N/A
2N/A *aux = (const auxv_t *)P->auxv;
2N/A return (PS_OK);
2N/A}
2N/A
2N/Aps_err_e
2N/Aps_pbrandname(struct ps_prochandle *P, char *buf, size_t len)
2N/A{
2N/A return (Pbrandname(P, buf, len) ? PS_OK : PS_ERR);
2N/A}
2N/A
2N/A/*
2N/A * Search for a symbol by name and return the corresponding address.
2N/A */
2N/Aps_err_e
2N/Aps_pglobal_lookup(struct ps_prochandle *P, const char *object_name,
2N/A const char *sym_name, psaddr_t *sym_addr)
2N/A{
2N/A GElf_Sym sym;
2N/A
2N/A if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
2N/A dprintf("pglobal_lookup <%s> -> %p\n",
2N/A sym_name, (void *)(uintptr_t)sym.st_value);
2N/A *sym_addr = (psaddr_t)sym.st_value;
2N/A return (PS_OK);
2N/A }
2N/A
2N/A return (PS_NOSYM);
2N/A}
2N/A
2N/A/*
2N/A * Search for a symbol by name and return the corresponding symbol
2N/A * information. If we're compiled _LP64, we just call Plookup_by_name
2N/A * and return because ps_sym_t is defined to be an Elf64_Sym, which
2N/A * is the same as a GElf_Sym. In the _ILP32 case, we have to convert
2N/A * Plookup_by_name's result back to a ps_sym_t (which is an Elf32_Sym).
2N/A */
2N/Aps_err_e
2N/Aps_pglobal_sym(struct ps_prochandle *P, const char *object_name,
2N/A const char *sym_name, ps_sym_t *symp)
2N/A{
2N/A#if defined(_ILP32)
2N/A GElf_Sym sym;
2N/A
2N/A if (Plookup_by_name(P, object_name, sym_name, &sym) == 0) {
2N/A symp->st_name = (Elf32_Word)sym.st_name;
2N/A symp->st_value = (Elf32_Addr)sym.st_value;
2N/A symp->st_size = (Elf32_Word)sym.st_size;
2N/A symp->st_info = ELF32_ST_INFO(
2N/A GELF_ST_BIND(sym.st_info), GELF_ST_TYPE(sym.st_info));
2N/A symp->st_other = sym.st_other;
2N/A symp->st_shndx = sym.st_shndx;
2N/A return (PS_OK);
2N/A }
2N/A
2N/A#elif defined(_LP64)
2N/A if (Plookup_by_name(P, object_name, sym_name, symp) == 0)
2N/A return (PS_OK);
2N/A#endif
2N/A return (PS_NOSYM);
2N/A}