1*edb34883SAdam H. Leventhal /* 2*edb34883SAdam H. Leventhal * CDDL HEADER START 3*edb34883SAdam H. Leventhal * 4*edb34883SAdam H. Leventhal * The contents of this file are subject to the terms of the 5*edb34883SAdam H. Leventhal * Common Development and Distribution License (the "License"). 6*edb34883SAdam H. Leventhal * You may not use this file except in compliance with the License. 7*edb34883SAdam H. Leventhal * 8*edb34883SAdam H. Leventhal * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*edb34883SAdam H. Leventhal * or http://www.opensolaris.org/os/licensing. 10*edb34883SAdam H. Leventhal * See the License for the specific language governing permissions 11*edb34883SAdam H. Leventhal * and limitations under the License. 12*edb34883SAdam H. Leventhal * 13*edb34883SAdam H. Leventhal * When distributing Covered Code, include this CDDL HEADER in each 14*edb34883SAdam H. Leventhal * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*edb34883SAdam H. Leventhal * If applicable, add the following below this CDDL HEADER, with the 16*edb34883SAdam H. Leventhal * fields enclosed by brackets "[]" replaced with your own identifying 17*edb34883SAdam H. Leventhal * information: Portions Copyright [yyyy] [name of copyright owner] 18*edb34883SAdam H. Leventhal * 19*edb34883SAdam H. Leventhal * CDDL HEADER END 20*edb34883SAdam H. Leventhal */ 21*edb34883SAdam H. Leventhal 22*edb34883SAdam H. Leventhal /* 23*edb34883SAdam H. Leventhal * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24*edb34883SAdam H. Leventhal * Use is subject to license terms. 25*edb34883SAdam H. Leventhal */ 26*edb34883SAdam H. Leventhal 27*edb34883SAdam H. Leventhal /* 28*edb34883SAdam H. Leventhal * Copyright (c) 2012 by Delphix. All rights reserved. 29*edb34883SAdam H. Leventhal */ 30*edb34883SAdam H. Leventhal 31*edb34883SAdam H. Leventhal /* 32*edb34883SAdam H. Leventhal * This is the basis for drti.o which dt_link.c links into the object file 33*edb34883SAdam H. Leventhal * generated by dtrace(1M) -G by default (note that -xlazyload disables this). 34*edb34883SAdam H. Leventhal */ 35*edb34883SAdam H. Leventhal 36*edb34883SAdam H. Leventhal #include <dlfcn.h> 37*edb34883SAdam H. Leventhal #include <stdlib.h> 38*edb34883SAdam H. Leventhal #include <fcntl.h> 39*edb34883SAdam H. Leventhal #include <unistd.h> 40*edb34883SAdam H. Leventhal 41*edb34883SAdam H. Leventhal #include <dlink.h> 42*edb34883SAdam H. Leventhal 43*edb34883SAdam H. Leventhal static int gen; /* DOF helper generation */ 44*edb34883SAdam H. Leventhal extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */ 45*edb34883SAdam H. Leventhal 46*edb34883SAdam H. Leventhal #pragma init(dtrace_drti_init) 47*edb34883SAdam H. Leventhal static void 48*edb34883SAdam H. Leventhal dtrace_drti_init(void) 49*edb34883SAdam H. Leventhal { 50*edb34883SAdam H. Leventhal Link_map *lmp; 51*edb34883SAdam H. Leventhal Lmid_t lmid; 52*edb34883SAdam H. Leventhal 53*edb34883SAdam H. Leventhal dtrace_link_init(); 54*edb34883SAdam H. Leventhal 55*edb34883SAdam H. Leventhal if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) { 56*edb34883SAdam H. Leventhal dprintf(1, "couldn't discover module name or address\n"); 57*edb34883SAdam H. Leventhal return; 58*edb34883SAdam H. Leventhal } 59*edb34883SAdam H. Leventhal 60*edb34883SAdam H. Leventhal if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) { 61*edb34883SAdam H. Leventhal dprintf(1, "couldn't discover link map ID\n"); 62*edb34883SAdam H. Leventhal return; 63*edb34883SAdam H. Leventhal } 64*edb34883SAdam H. Leventhal 65*edb34883SAdam H. Leventhal dtrace_link_dof(&__SUNW_dof, lmid, lmp->l_name, lmp->l_addr); 66*edb34883SAdam H. Leventhal } 67*edb34883SAdam H. Leventhal 68*edb34883SAdam H. Leventhal #pragma fini(dtrace_drti_fini) 69*edb34883SAdam H. Leventhal static void 70*edb34883SAdam H. Leventhal dtrace_drti_fini(void) 71*edb34883SAdam H. Leventhal { 72*edb34883SAdam H. Leventhal int fd; 73*edb34883SAdam H. Leventhal 74*edb34883SAdam H. Leventhal if ((fd = open64(devname, O_RDWR)) < 0) { 75*edb34883SAdam H. Leventhal dprintf(1, "failed to open helper device %s", devname); 76*edb34883SAdam H. Leventhal return; 77*edb34883SAdam H. Leventhal } 78*edb34883SAdam H. Leventhal 79*edb34883SAdam H. Leventhal if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1) 80*edb34883SAdam H. Leventhal dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen); 81*edb34883SAdam H. Leventhal else 82*edb34883SAdam H. Leventhal dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen); 83*edb34883SAdam H. Leventhal 84*edb34883SAdam H. Leventhal (void) close(fd); 85*edb34883SAdam H. Leventhal } 86