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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Copyright (c) 2012 by Delphix. All rights reserved.
32 * Common functions for helper provider loading both compiled into the
33 * executable via drti.o and dtrace(1M) -G, and the libdaudit.so library.
47 * In Solaris 10 GA, the only mechanism for communicating helper information
48 * is through the DTrace helper pseudo-device node in /devices; there is
49 * no /dev link. Because of this, USDT providers and helper actions don't
50 * work inside of non-global zones. This issue was addressed by adding
51 * the /dev and having this initialization code use that /dev link. If the
52 * /dev link doesn't exist it falls back to looking for the /devices node
53 * as this code may be embedded in a binary which runs on Solaris 10 GA.
55 const char *devname
= "/dev/dtrace/helper";
56 static const char *olddevname
= "/devices/pseudo/dtrace@0:helper";
58 static boolean_t dof_init_debug
= B_FALSE
;
61 dprintf(int debug
, const char *fmt
, ...)
65 if (debug
&& !dof_init_debug
)
70 (void) fprintf(stderr
, "dtrace DOF: ");
72 (void) vfprintf(stderr
, fmt
, ap
);
74 if (fmt
[strlen(fmt
) - 1] != '\n')
75 (void) fprintf(stderr
, ": %s\n", strerror(errno
));
81 * Users may set the following environment variable to affect the way
82 * helper initialization takes place:
84 * DTRACE_DOF_INIT_DEBUG enable debugging output
85 * DTRACE_DOF_INIT_DISABLE disable helper loading
86 * DTRACE_DOF_INIT_DEVNAME set the path to the helper node
89 dtrace_link_init(void)
91 if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL
)
92 dof_init_debug
= B_TRUE
;
96 dtrace_link_dof(dof_hdr_t
*dof
, Lmid_t lmid
, const char *name
, uintptr_t addr
)
108 if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL
)
111 if ((modname
= strrchr(name
, '/')) == NULL
)
116 if (dof
->dofh_ident
[DOF_ID_MAG0
] != DOF_MAG_MAG0
||
117 dof
->dofh_ident
[DOF_ID_MAG1
] != DOF_MAG_MAG1
||
118 dof
->dofh_ident
[DOF_ID_MAG2
] != DOF_MAG_MAG2
||
119 dof
->dofh_ident
[DOF_ID_MAG3
] != DOF_MAG_MAG3
) {
120 dprintf(0, ".SUNW_dof section corrupt for %s\n", modname
);
126 dh
.dofhp_dof
= (uintptr_t)dof
;
127 dh
.dofhp_addr
= elf
->e_type
== ET_DYN
? addr
: 0;
129 if (lmid
== LM_ID_BASE
) {
130 (void) snprintf(dh
.dofhp_mod
, sizeof (dh
.dofhp_mod
),
133 (void) snprintf(dh
.dofhp_mod
, sizeof (dh
.dofhp_mod
),
134 "LM%lu`%s", lmid
, modname
);
137 if ((p
= getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL
)
140 if ((fd
= open64(devname
, O_RDWR
)) < 0) {
141 dprintf(1, "failed to open helper device %s", devname
);
144 * If the device path wasn't explicitly set, try again with
145 * the old device path.
150 devname
= olddevname
;
152 if ((fd
= open64(devname
, O_RDWR
)) < 0) {
153 dprintf(1, "failed to open helper device %s", devname
);
158 if (ioctl(fd
, DTRACEHIOC_ADDDOF
, &dh
) == -1) {
159 dprintf(1, "DTrace ioctl failed for DOF at %p in %s", dof
,
162 dprintf(1, "DTrace ioctl succeeded for DOF at %p in %s\n", dof
,