dt_pid.c revision a386cc11a86ecb60f5a48078d22c1500e2ad003e
/*
* 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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2013, Joyent, Inc. All rights reserved.
*/
#include <assert.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <alloca.h>
#include <libgen.h>
#include <stddef.h>
#include <sys/sysmacros.h>
#include <dt_impl.h>
#include <dt_program.h>
#include <dt_pid.h>
#include <dt_string.h>
#include <dt_module.h>
typedef struct dt_pid_probe {
struct ps_prochandle *dpp_pr;
const char *dpp_mod;
char *dpp_func;
const char *dpp_name;
const char *dpp_obj;
/*
* Compose the lmid and object name into the canonical representation. We
* omit the lmid for the default link map for convenience.
*/
static void
{
if (lmid == LM_ID_BASE)
else
}
static int
{
int len;
} else {
}
return (1);
}
static int
{
char *end;
return (1); /* errno is set for us */
}
D_PROC_CREATEFAIL, "failed to create return probe "
"for '%s': %s", func,
}
nmatches++;
}
D_PROC_CREATEFAIL, "failed to create entry probe "
"for '%s': %s", func,
}
nmatches++;
}
if (*end != '\0') {
}
"offset 0x%llx outside of function '%s'",
}
if (err == DT_PROC_ERR) {
D_PROC_CREATEFAIL, "failed to create probe at "
}
if (err == DT_PROC_ALIGN) {
"offset 0x%llx is not aligned on an instruction",
(u_longlong_t)off));
}
nmatches++;
"failed to create offset probes in '%s': %s", func,
}
nmatches++;
}
return (0);
}
static int
{
return (0);
return (0);
}
if (pp->dpp_last_taken == 0 ||
/*
* Due to 4524008, _init and _fini may have a bloated st_size.
* While this bug has been fixed for a while, old binaries
* may exist that still exhibit this problem. As a result, we
* don't match _init and _fini though we allow users to
* specify them explicitly.
*/
return (0);
}
}
return (0);
}
static int
{
return (0);
else
NULL) == 0)
else
NULL) == 0)
else
NULL) == 0)
else
NULL) == 0)
else
/*
* If pp->dpp_func contains any globbing meta-characters, we need
* to iterate over the symbol table and compare each function name
* against the pattern.
*/
/*
* If we fail to lookup the symbol, try interpreting the
* function as the special "-" function that indicates that the
* probe name should be interpreted as a absolute virtual
* address. If that fails and we were matching a specific
* function in a specific module, report the error, otherwise
* just fail silently in the hopes that some other object will
* contain the desired symbol.
*/
"failed to lookup '%s' in module '%s'",
} else {
return (0);
}
}
/*
* Only match defined functions of non-zero size.
*/
return (0);
/*
* We don't instrument PLTs -- they're dynamically rewritten,
* and, so, inherently dicey to instrument.
*/
return (0);
} else {
return (1);
/*
* If we didn't match anything in the PR_SYMTAB, try
* the PR_DYNSYM.
*/
return (1);
}
}
return (0);
}
static int
{
char name[DTRACE_MODNAMELEN];
else
return (0);
}
static const prmap_t *
{
char m[MAXPATHLEN];
const char *obj;
/*
* Pick apart the link map from the library name.
*/
char *end;
return (NULL);
return (NULL);
} else {
}
return (NULL);
obj = &m[0];
else
obj++;
return (pmp);
}
static int
{
int ret = 0;
/*
* We can only trace dynamically-linked executables (since we've
*/
"process %s is not a dynamically-linked executable",
}
pp.dpp_last_taken = 0;
"only the a.out module is valid with the "
"'-' function"));
}
"only individual addresses may be specified "
"with the '-' function"));
}
}
/*
* If pp.dpp_mod contains any globbing meta-characters, we need
* to iterate over each module and compare its name against the
* pattern. An empty module name is treated as '*'.
*/
} else {
char *obj;
/*
* If we can't find a matching module, don't sweat it -- either
* we'll fail the enabling because the probes don't exist or
* we'll wait for that module to come along.
*/
else
obj++;
}
}
return (ret);
}
static int
{
struct ps_prochandle *P = data;
const char *mname;
int i, fd = -1;
/*
* The symbol ___SUNW_dof is for lazy-loaded DOF sections, and
* __SUNW_dof is for actively-loaded DOF sections. We try to force
* in both types of DOF section since the process may not yet have
* run the code to instantiate these providers.
*/
for (i = 0; i < 2; i++) {
&sip) != 0) {
continue;
}
else
mname++;
dt_dprintf("read of ELF header failed");
continue;
}
if (fd == -1 &&
dt_dprintf("pr_open of helper device failed: %s\n",
return (-1); /* errno is set for us */
}
}
if (fd != -1)
return (0);
}
static int
{
int ret = 0;
(void) Pupdate_maps(P);
if (Pobject_iter(P, dt_pid_usdt_mapping, P) != 0) {
ret = -1;
"failed to instantiate probes for pid %d: %s",
}
/*
* Put the module name in its canonical form.
*/
(void) dt_pid_fix_mod(pdp, P);
return (ret);
}
static pid_t
{
if (!isdigit(*c))
last = c;
}
return (-1);
}
errno = 0;
return (-1);
}
return (pid);
}
int
{
char provname[DTRACE_PROVNAMELEN];
struct ps_prochandle *P;
int err = 0;
return (-1);
"pid provider is not installed on this system");
} else {
"pid provider is not available: %s",
}
return (-1);
}
0)) == NULL) {
"failed to grab process %d", (int)pid);
return (-1);
}
/*
* Alert other retained enablings which may match
* against the newly created probes.
*/
}
dt_proc_release(dtp, P);
}
/*
* If it's not strictly a pid provider, we might match a USDT provider.
*/
"failed to grab process %d", (int)pid);
return (-1);
}
}
dt_proc_release(dtp, P);
}
return (err ? -1 : 0);
}
int
{
char provname[DTRACE_PROVNAMELEN];
continue;
ret = 1;
/*
* If it's not strictly a pid provider, we might match
* a USDT provider.
*/
ret = 1;
}
}
if (found) {
/*
* Give DTrace a shot to the ribs to get it to check
* out the newly created probes.
*/
}
return (ret);
}
/*
* libdtrace has a backroom deal with us to ask us for type information on
* behalf of pid provider probes when fasttrap doesn't return any type
* information. Instead we'll look up the module and see if there is type
* information available. However, if there is no type information available due
* to a lack of CTF data, then we want to make sure that DTrace still carries on
* in face of that. As such we don't have a meaningful exit code about failure.
* We emit information about why we failed to the dtrace debug log so someone
* can figure it out by asking nicely for DTRACE_DEBUG.
*/
void
{
ctf_file_t *fp;
struct ps_prochandle *p;
int i, args;
char buf[DTRACE_ARGTYPELEN];
const char *mptr;
char *eptr;
int ret = 0;
/* Set up a potential outcome */
*nargs = 0;
/*
* If we don't have an entry or return probe then we can just stop right
* now as we don't have arguments for offset probes.
*/
return;
dt_dprintf("failed to find module for %s\n",
pdp->dtpd_provider);
return;
}
dt_dprintf("failed to load module for %s\n",
pdp->dtpd_provider);
return;
}
/*
* We may be working with a module that doesn't have ctf. If that's the
* case then we just return now and move on with life.
*/
dt_dprintf("no ctf container for %s\n",
return;
}
if (p == NULL) {
dt_dprintf("failed to grab pid\n");
return;
}
dt_proc_lock(dtp, p);
/*
* Check to see if the D module has a link map ID and separate that out
* for properly interrogating libproc.
*/
dt_dprintf("found weird modname with linkmap, "
goto out;
}
dt_dprintf("missing leading 'LM', "
goto out;
}
errno = 0;
dt_dprintf("failed to parse out lmid, aborting: %s\n",
goto out;
}
mptr++;
} else {
lmid = 0;
}
dt_dprintf("failed to find function %s in %s`%s\n",
goto out;
}
dt_dprintf("failed to get ctf information for %s in %s`%s\n",
goto out;
}
if (args < 2)
goto out;
adp->dtargd_ndx = 0;
/*
* We explicitly leave out the library here, we only care that
* it is some int. We are assuming that there is no ctf
* container in here that is lying about what an int is.
*/
adp++;
"userland ");
*nargs = 2;
} else {
goto out;
adp->dtargd_ndx = i;
"userland ");
}
}
out:
dt_proc_unlock(dtp, p);
dt_proc_release(dtp, p);
}