import less(1)
[unleashed/tickless.git] / usr / src / lib / libdtrace / common / dlink_init.c
blob7c00a49c21344362fa134065d3d2d146c43917ce
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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 * This is the basis for drti.o which dt_link.c links into the object file
33 * generated by dtrace(1M) -G by default (note that -xlazyload disables this).
36 #include <dlfcn.h>
37 #include <stdlib.h>
38 #include <fcntl.h>
39 #include <unistd.h>
41 #include <dlink.h>
43 static int gen; /* DOF helper generation */
44 extern dof_hdr_t __SUNW_dof; /* DOF defined in the .SUNW_dof section */
46 #pragma init(dtrace_drti_init)
47 static void
48 dtrace_drti_init(void)
50 Link_map *lmp;
51 Lmid_t lmid;
53 dtrace_link_init();
55 if (dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &lmp) == -1 || lmp == NULL) {
56 dprintf(1, "couldn't discover module name or address\n");
57 return;
60 if (dlinfo(RTLD_SELF, RTLD_DI_LMID, &lmid) == -1) {
61 dprintf(1, "couldn't discover link map ID\n");
62 return;
65 dtrace_link_dof(&__SUNW_dof, lmid, lmp->l_name, lmp->l_addr);
68 #pragma fini(dtrace_drti_fini)
69 static void
70 dtrace_drti_fini(void)
72 int fd;
74 if ((fd = open64(devname, O_RDWR)) < 0) {
75 dprintf(1, "failed to open helper device %s", devname);
76 return;
79 if ((gen = ioctl(fd, DTRACEHIOC_REMOVE, gen)) == -1)
80 dprintf(1, "DTrace ioctl failed to remove DOF (%d)\n", gen);
81 else
82 dprintf(1, "DTrace ioctl removed DOF (%d)\n", gen);
84 (void) close(fd);