Commit | Line | Data |
---|---|---|
b13d93c2 PW |
1 | /* |
2 | * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com> | |
8e2aed3f | 3 | * Copyright (C) 2015 Antoine Busque <abusque@efficios.com> |
b13d93c2 PW |
4 | * |
5 | * This library is free software; you can redistribute it and/or | |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; version 2.1 of | |
8 | * the License. | |
9 | * | |
10 | * This library is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
5aed31fc | 20 | #define _LGPL_SOURCE |
b13d93c2 | 21 | #define _GNU_SOURCE |
8e2aed3f | 22 | |
13436238 | 23 | #include <limits.h> |
8e2aed3f | 24 | #include <stdio.h> |
13436238 | 25 | #include <sys/types.h> |
8e2aed3f AB |
26 | #include <unistd.h> |
27 | ||
28 | #include <lttng/ust-dlfcn.h> | |
29 | #include <lttng/ust-elf.h> | |
1de7abff | 30 | #include <helper.h> |
eb2b066f | 31 | #include "usterr-signal-safe.h" |
b13d93c2 | 32 | |
8e2aed3f AB |
33 | /* Include link.h last else it conflicts with ust-dlfcn. */ |
34 | #include <link.h> | |
b13d93c2 | 35 | |
13436238 | 36 | #define TRACEPOINT_DEFINE |
6d4658aa | 37 | #include "ust_dl.h" |
13436238 | 38 | |
b13d93c2 PW |
39 | static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flag); |
40 | static int (*__lttng_ust_plibc_dlclose)(void *handle); | |
b13d93c2 PW |
41 | |
42 | static | |
43 | void *_lttng_ust_dl_libc_dlopen(const char *filename, int flag) | |
44 | { | |
45 | if (!__lttng_ust_plibc_dlopen) { | |
46 | __lttng_ust_plibc_dlopen = dlsym(RTLD_NEXT, "dlopen"); | |
8e2aed3f | 47 | if (!__lttng_ust_plibc_dlopen) { |
b13d93c2 PW |
48 | fprintf(stderr, "%s\n", dlerror()); |
49 | return NULL; | |
50 | } | |
51 | } | |
52 | return __lttng_ust_plibc_dlopen(filename, flag); | |
53 | } | |
54 | ||
55 | static | |
56 | int _lttng_ust_dl_libc_dlclose(void *handle) | |
57 | { | |
58 | if (!__lttng_ust_plibc_dlclose) { | |
59 | __lttng_ust_plibc_dlclose = dlsym(RTLD_NEXT, "dlclose"); | |
8e2aed3f | 60 | if (!__lttng_ust_plibc_dlclose) { |
b13d93c2 PW |
61 | fprintf(stderr, "%s\n", dlerror()); |
62 | return -1; | |
63 | } | |
64 | } | |
65 | return __lttng_ust_plibc_dlclose(handle); | |
66 | } | |
67 | ||
68 | static | |
03db42de | 69 | void lttng_ust_dl_dlopen(void *so_base, const char *so_name, void *ip) |
b13d93c2 | 70 | { |
13436238 | 71 | char resolved_path[PATH_MAX]; |
8e2aed3f AB |
72 | struct lttng_ust_elf *elf; |
73 | uint64_t memsz; | |
83215d66 | 74 | uint8_t *build_id = NULL; |
8e2aed3f | 75 | size_t build_id_len; |
83215d66 | 76 | char *dbg_file = NULL; |
8e2aed3f AB |
77 | uint32_t crc; |
78 | int has_build_id = 0, has_debug_link = 0; | |
79 | int ret; | |
b13d93c2 | 80 | |
13436238 PW |
81 | if (!realpath(so_name, resolved_path)) { |
82 | ERR("could not resolve path '%s'", so_name); | |
83 | return; | |
b13d93c2 | 84 | } |
b13d93c2 | 85 | |
8e2aed3f AB |
86 | elf = lttng_ust_elf_create(resolved_path); |
87 | if (!elf) { | |
88 | ERR("could not acces file %s", resolved_path); | |
13436238 | 89 | return; |
b13d93c2 | 90 | } |
13436238 | 91 | |
8e2aed3f AB |
92 | ret = lttng_ust_elf_get_memsz(elf, &memsz); |
93 | if (ret) { | |
94 | goto end; | |
95 | } | |
96 | ret = lttng_ust_elf_get_build_id( | |
97 | elf, &build_id, &build_id_len, &has_build_id); | |
98 | if (ret) { | |
99 | goto end; | |
100 | } | |
101 | ret = lttng_ust_elf_get_debug_link( | |
102 | elf, &dbg_file, &crc, &has_debug_link); | |
103 | if (ret) { | |
104 | goto end; | |
105 | } | |
106 | ||
6d4658aa | 107 | tracepoint(lttng_ust_dl, dlopen, |
a435064b MD |
108 | ip, so_base, resolved_path, memsz, |
109 | has_build_id, has_debug_link); | |
8e2aed3f AB |
110 | |
111 | if (has_build_id) { | |
112 | tracepoint(lttng_ust_dl, build_id, | |
113 | ip, so_base, build_id, build_id_len); | |
8e2aed3f AB |
114 | } |
115 | ||
116 | if (has_debug_link) { | |
117 | tracepoint(lttng_ust_dl, debug_link, | |
118 | ip, so_base, dbg_file, crc); | |
8e2aed3f AB |
119 | } |
120 | ||
121 | end: | |
83215d66 MD |
122 | free(dbg_file); |
123 | free(build_id); | |
8e2aed3f | 124 | lttng_ust_elf_destroy(elf); |
13436238 | 125 | return; |
b13d93c2 PW |
126 | } |
127 | ||
128 | void *dlopen(const char *filename, int flag) | |
129 | { | |
8e2aed3f AB |
130 | void *handle; |
131 | ||
132 | handle = _lttng_ust_dl_libc_dlopen(filename, flag); | |
bd703713 | 133 | if (__tracepoint_ptrs_registered && handle) { |
b13d93c2 | 134 | struct link_map *p = NULL; |
8e2aed3f AB |
135 | int ret; |
136 | ||
137 | ret = dlinfo(handle, RTLD_DI_LINKMAP, &p); | |
138 | if (ret != -1 && p != NULL && p->l_addr != 0) { | |
03db42de | 139 | lttng_ust_dl_dlopen((void *) p->l_addr, p->l_name, |
1de7abff | 140 | LTTNG_UST_CALLER_IP()); |
8e2aed3f | 141 | } |
b13d93c2 | 142 | } |
8e2aed3f | 143 | |
b13d93c2 PW |
144 | return handle; |
145 | } | |
146 | ||
147 | int dlclose(void *handle) | |
148 | { | |
62734f7e | 149 | if (__tracepoint_ptrs_registered) { |
b13d93c2 | 150 | struct link_map *p = NULL; |
8e2aed3f AB |
151 | int ret; |
152 | ||
153 | ret = dlinfo(handle, RTLD_DI_LINKMAP, &p); | |
154 | if (ret != -1 && p != NULL && p->l_addr != 0) { | |
155 | tracepoint(lttng_ust_dl, dlclose, | |
1de7abff | 156 | LTTNG_UST_CALLER_IP(), |
8e2aed3f AB |
157 | (void *) p->l_addr); |
158 | } | |
b13d93c2 | 159 | } |
8e2aed3f | 160 | |
b13d93c2 PW |
161 | return _lttng_ust_dl_libc_dlclose(handle); |
162 | } |