17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54d27388dSJonathan Haslam  * Common Development and Distribution License (the "License").
64d27388dSJonathan Haslam  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21edb34883SAdam H. Leventhal 
227c478bd9Sstevel@tonic-gate /*
234d27388dSJonathan Haslam  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27edb34883SAdam H. Leventhal /*
28edb34883SAdam H. Leventhal  * Copyright (c) 2012 by Delphix. All rights reserved.
29edb34883SAdam H. Leventhal  */
30edb34883SAdam H. Leventhal 
31edb34883SAdam H. Leventhal /*
32edb34883SAdam H. Leventhal  * Common functions for helper provider loading both compiled into the
33*bbf21555SRichard Lowe  * executable via drti.o and dtrace(8) -G, and the libdaudit.so library.
34edb34883SAdam H. Leventhal  */
357c478bd9Sstevel@tonic-gate 
36edb34883SAdam H. Leventhal #include <errno.h>
37edb34883SAdam H. Leventhal #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <stdarg.h>
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
42edb34883SAdam H. Leventhal #include <unistd.h>
43edb34883SAdam H. Leventhal 
44edb34883SAdam H. Leventhal #include <dlink.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * In Solaris 10 GA, the only mechanism for communicating helper information
487c478bd9Sstevel@tonic-gate  * is through the DTrace helper pseudo-device node in /devices; there is
497c478bd9Sstevel@tonic-gate  * no /dev link. Because of this, USDT providers and helper actions don't
507c478bd9Sstevel@tonic-gate  * work inside of non-global zones. This issue was addressed by adding
517c478bd9Sstevel@tonic-gate  * the /dev and having this initialization code use that /dev link. If the
527c478bd9Sstevel@tonic-gate  * /dev link doesn't exist it falls back to looking for the /devices node
537c478bd9Sstevel@tonic-gate  * as this code may be embedded in a binary which runs on Solaris 10 GA.
547c478bd9Sstevel@tonic-gate  */
55edb34883SAdam H. Leventhal const char *devname = "/dev/dtrace/helper";
567c478bd9Sstevel@tonic-gate static const char *olddevname = "/devices/pseudo/dtrace@0:helper";
577c478bd9Sstevel@tonic-gate 
58edb34883SAdam H. Leventhal static boolean_t dof_init_debug = B_FALSE;
597c478bd9Sstevel@tonic-gate 
60edb34883SAdam H. Leventhal void
dprintf(int debug,const char * fmt,...)617c478bd9Sstevel@tonic-gate dprintf(int debug, const char *fmt, ...)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	va_list ap;
647c478bd9Sstevel@tonic-gate 
654d27388dSJonathan Haslam 	if (debug && !dof_init_debug)
667c478bd9Sstevel@tonic-gate 		return;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
697c478bd9Sstevel@tonic-gate 
70edb34883SAdam H. Leventhal 	(void) fprintf(stderr, "dtrace DOF: ");
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (fmt[strlen(fmt) - 1] != '\n')
757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s\n", strerror(errno));
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	va_end(ap);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
80edb34883SAdam H. Leventhal /*
81edb34883SAdam H. Leventhal  * Users may set the following environment variable to affect the way
82edb34883SAdam H. Leventhal  * helper initialization takes place:
83edb34883SAdam H. Leventhal  *
84edb34883SAdam H. Leventhal  *	DTRACE_DOF_INIT_DEBUG		enable debugging output
85edb34883SAdam H. Leventhal  *	DTRACE_DOF_INIT_DISABLE		disable helper loading
86edb34883SAdam H. Leventhal  *	DTRACE_DOF_INIT_DEVNAME		set the path to the helper node
87edb34883SAdam H. Leventhal  */
88edb34883SAdam H. Leventhal void
dtrace_link_init(void)89edb34883SAdam H. Leventhal dtrace_link_init(void)
907c478bd9Sstevel@tonic-gate {
91edb34883SAdam H. Leventhal 	if (getenv("DTRACE_DOF_INIT_DEBUG") != NULL)
92edb34883SAdam H. Leventhal 		dof_init_debug = B_TRUE;
93edb34883SAdam H. Leventhal }
94edb34883SAdam H. Leventhal 
95edb34883SAdam H. Leventhal void
dtrace_link_dof(dof_hdr_t * dof,Lmid_t lmid,const char * name,uintptr_t addr)96edb34883SAdam H. Leventhal dtrace_link_dof(dof_hdr_t *dof, Lmid_t lmid, const char *name, uintptr_t addr)
97edb34883SAdam H. Leventhal {
98edb34883SAdam H. Leventhal 	const char *modname;
99edb34883SAdam H. Leventhal 	const char *p;
1007c478bd9Sstevel@tonic-gate #ifdef _LP64
1017c478bd9Sstevel@tonic-gate 	Elf64_Ehdr *elf;
1027c478bd9Sstevel@tonic-gate #else
1037c478bd9Sstevel@tonic-gate 	Elf32_Ehdr *elf;
1047c478bd9Sstevel@tonic-gate #endif
1057c478bd9Sstevel@tonic-gate 	dof_helper_t dh;
1067c478bd9Sstevel@tonic-gate 	int fd;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (getenv("DTRACE_DOF_INIT_DISABLE") != NULL)
1097c478bd9Sstevel@tonic-gate 		return;
1107c478bd9Sstevel@tonic-gate 
111edb34883SAdam H. Leventhal 	if ((modname = strrchr(name, '/')) == NULL)
112edb34883SAdam H. Leventhal 		modname = name;
1137c478bd9Sstevel@tonic-gate 	else
1147c478bd9Sstevel@tonic-gate 		modname++;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if (dof->dofh_ident[DOF_ID_MAG0] != DOF_MAG_MAG0 ||
1177c478bd9Sstevel@tonic-gate 	    dof->dofh_ident[DOF_ID_MAG1] != DOF_MAG_MAG1 ||
1187c478bd9Sstevel@tonic-gate 	    dof->dofh_ident[DOF_ID_MAG2] != DOF_MAG_MAG2 ||
1197c478bd9Sstevel@tonic-gate 	    dof->dofh_ident[DOF_ID_MAG3] != DOF_MAG_MAG3) {
120edb34883SAdam H. Leventhal 		dprintf(0, ".SUNW_dof section corrupt for %s\n", modname);
1217c478bd9Sstevel@tonic-gate 		return;
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 
124edb34883SAdam H. Leventhal 	elf = (void *)addr;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	dh.dofhp_dof = (uintptr_t)dof;
127edb34883SAdam H. Leventhal 	dh.dofhp_addr = elf->e_type == ET_DYN ? addr : 0;
1287c478bd9Sstevel@tonic-gate 
129edb34883SAdam H. Leventhal 	if (lmid == LM_ID_BASE) {
1307c478bd9Sstevel@tonic-gate 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
1317c478bd9Sstevel@tonic-gate 		    "%s", modname);
1327c478bd9Sstevel@tonic-gate 	} else {
1337c478bd9Sstevel@tonic-gate 		(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
1347c478bd9Sstevel@tonic-gate 		    "LM%lu`%s", lmid, modname);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	if ((p = getenv("DTRACE_DOF_INIT_DEVNAME")) != NULL)
1387c478bd9Sstevel@tonic-gate 		devname = p;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	if ((fd = open64(devname, O_RDWR)) < 0) {
1417c478bd9Sstevel@tonic-gate 		dprintf(1, "failed to open helper device %s", devname);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		/*
1447c478bd9Sstevel@tonic-gate 		 * If the device path wasn't explicitly set, try again with
1457c478bd9Sstevel@tonic-gate 		 * the old device path.
1467c478bd9Sstevel@tonic-gate 		 */
1477c478bd9Sstevel@tonic-gate 		if (p != NULL)
1487c478bd9Sstevel@tonic-gate 			return;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		devname = olddevname;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		if ((fd = open64(devname, O_RDWR)) < 0) {
1537c478bd9Sstevel@tonic-gate 			dprintf(1, "failed to open helper device %s", devname);
1547c478bd9Sstevel@tonic-gate 			return;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
158edb34883SAdam H. Leventhal 	if (ioctl(fd, DTRACEHIOC_ADDDOF, &dh) == -1) {
159edb34883SAdam H. Leventhal 		dprintf(1, "DTrace ioctl failed for DOF at %p in %s", dof,
160edb34883SAdam H. Leventhal 		    name);
161edb34883SAdam H. Leventhal 	} else {
162edb34883SAdam H. Leventhal 		dprintf(1, "DTrace ioctl succeeded for DOF at %p in %s\n", dof,
163edb34883SAdam H. Leventhal 		    name);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	(void) close(fd);
1667c478bd9Sstevel@tonic-gate }
167