edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * CDDL HEADER START
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal *
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * The contents of this file are subject to the terms of the
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * Common Development and Distribution License (the "License").
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * You may not use this file except in compliance with the License.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal *
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * or http://www.opensolaris.org/os/licensing.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * See the License for the specific language governing permissions
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * and limitations under the License.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal *
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * When distributing Covered Code, include this CDDL HEADER in each
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * If applicable, add the following below this CDDL HEADER, with the
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * fields enclosed by brackets "[]" replaced with your own identifying
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * information: Portions Copyright [yyyy] [name of copyright owner]
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal *
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * CDDL HEADER END
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * Use is subject to license terms.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * Copyright (c) 2012 by Delphix. All rights reserved.
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal/*
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * This is the basis for drti.o which dt_link.c links into the object file
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal * generated by dtrace(1M) -G by default (note that -xlazyload disables this).
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <dlfcn.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <stdlib.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <fcntl.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <unistd.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#include <dlink.h>
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalstatic int gen; /* DOF helper generation */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalextern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#pragma init(dtrace_drti_init)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalstatic void
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhaldtrace_drti_init(void)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal{
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal Link_map *lmp;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal Lmid_t lmid;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dtrace_link_init();
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dprintf(1, "couldn't discover module name or address\n");
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal return;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal }
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dprintf(1, "couldn't discover link map ID\n");
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal return;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal }
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dtrace_link_dof(&__SUNW_dof, lmid, lmp->l_name, lmp->l_addr);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal}
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal#pragma fini(dtrace_drti_fini)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhalstatic void
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhaldtrace_drti_fini(void)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal{
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal int fd;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal if ((fd = open64(devname, O_RDWR)) < 0) {
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dprintf(1, "failed to open helper device %s", devname);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal return;
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal }
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1)
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal else
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal (void) close(fd);
edb348833aaacfa1176e502ad38875fd0b2717abAdam H. Leventhal}