mdb_pservice.c revision 9acbbeaf2a1ffe5c14b244867d427714fab43c5c
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Proc Service API Interposition Layer
*
* In order to allow multiple MDB targets to make use of librtld_db, we
* provide an interposition layer for functions in the proc_service.h API
* that are used by librtld_db. Each of the functions used by librtld_db
* can be conveniently expressed in terms of the MDB target API, so this
* layer simply selects the appropriate target, invokes the corresponding
* target API function, and then translates the error codes appropriately.
* We expect that each proc_service entry point will be invoked with a
* cookie (struct ps_prochandle *) which matches either a known MDB target,
* or the value of a target's t->t_pshandle. This allows us to re-vector
* calls to the proc service API around libproc (which also contains an
* implementation of the proc_service API) like this:
*
* Linker map:
* +---------+ +---------+ +------------+ Legend:
* | MDB | ->| libproc | ->| librtld_db | <1> function in this file
* +---------+ +---------+ +------------+ <2> function in libproc
* ps_pread<1> ps_pread<2> call ps_pread() --+
* |
* +---------------------------------------------+
* |
* +-> ps_pread<1>(P, ...)
* t = mdb_tgt_from_pshandle(P);
* mdb_tgt_vread(t, ...);
*
* If we are debugging a user process, we then make these calls (which form
* the equivalent of libproc's proc_service implementation):
*
* mdb_tgt_vread() -> proc target t->t_vread() -> libproc.so`Pread()
*
* If we are debugging a user process through a kernel crash dump (kproc
* target), we make these calls:
*
* mdb_tgt_vread() -> kproc target t->t_vread() -> mdb_tgt_aread(kvm target) ->
* kvm target t->t_aread() -> libkvm.so`kvm_aread()
*
* This design allows us to support both kproc's use of librtld_db, as well
* as libproc's use of librtld_db, but it does lead to one unfortunate problem
* in the creation of a proc target: when the proc target invokes libproc to
* construct a ps_prochandle, and libproc in turn invokes librtld_db, MDB does
* not yet know what ps_prochandle has been allocated inside of libproc since
* this call has not yet returned. We also can't translate this ps_prochandle
* to the target itself, since that target isn't ready to handle requests yet;
* we actually need to pass the call back through to libproc. In order to
* do that, we use libdl to lookup the address of libproc's definition of the
* various functions (RTLD_NEXT on the link map chain) and store these in the
* ps_ops structure below. If we ever fail to translate a ps_prochandle to
* an MDB target, we simply pass the call through to libproc.
*/
#include <proc_service.h>
#include <dlfcn.h>
#include <mdb/mdb_target_impl.h>
#include <mdb/mdb_debug.h>
static struct {
const char *, const char *, psaddr_t *);
const char *, const char *, ps_sym_t *);
const auxv_t **);
int *);
} ps_ops;
static mdb_tgt_t *
mdb_tgt_from_pshandle(void *P)
{
mdb_tgt_t *t;
if (t == P || t->t_pshandle == P)
return (t);
}
return (NULL);
}
/*
* Read from the specified target virtual address.
*/
{
mdb_tgt_t *t = mdb_tgt_from_pshandle(P);
if (t == NULL)
return (PS_BADADDR);
return (PS_OK);
}
/*
* Write to the specified target virtual address.
*/
{
mdb_tgt_t *t = mdb_tgt_from_pshandle(P);
if (t == NULL)
return (PS_BADADDR);
return (PS_OK);
}
/*
* Search for a symbol by name and return the corresponding address.
*/
{
mdb_tgt_t *t = mdb_tgt_from_pshandle(P);
if (t == NULL)
return (PS_OK);
}
return (PS_NOSYM);
}
/*
* Search for a symbol by name and return the corresponding symbol data.
* If we're compiled _LP64, we just call mdb_tgt_lookup_by_name and return
* because ps_sym_t is defined to be an Elf64_Sym, which is the same as a
* GElf_Sym. In the _ILP32 case, we have to convert mdb_tgt_lookup_by_name's
* result back to a ps_sym_t (which is an Elf32_Sym).
*/
{
mdb_tgt_t *t = mdb_tgt_from_pshandle(P);
#if defined(_ILP32)
if (t == NULL)
return (PS_OK);
}
if (t == NULL)
return (PS_OK);
#endif
return (PS_NOSYM);
}
/*
* Report a debug message. We allow proc_service API clients to report
* messages via our debug stream if the MDB_DBG_PSVC token is enabled.
*/
void
{
}
/*
* Return the auxv structure from the process being examined.
*/
{
mdb_tgt_t *t = mdb_tgt_from_pshandle(P);
if (t == NULL)
if (mdb_tgt_auxv(t, auxvp) != 0)
return (PS_ERR);
return (PS_OK);
}
/*
* Return the data model of the target.
*/
{
mdb_tgt_t *t = mdb_tgt_from_pshandle(P);
if (t == NULL)
switch (mdb_tgt_dmodel(t)) {
case MDB_TGT_MODEL_LP64:
*dm = PR_MODEL_LP64;
return (PS_OK);
case MDB_TGT_MODEL_ILP32:
*dm = PR_MODEL_ILP32;
return (PS_OK);
}
return (PS_ERR);
}
/*
* Stub function in case we cannot find the necessary symbols from libproc.
*/
static ps_err_e
ps_fail(struct ps_prochandle *P)
{
return (PS_BADPID);
}
/*
* Initialization function for the proc service interposition layer: we use
* libdl to look up the next definition of each function in the link map.
*/
void
mdb_pservice_init(void)
{
}