Introduce LTTNG_UST_MAP_POPULATE_POLICY environment variable
[lttng-ust.git] / liblttng-ust-dl / lttng-ust-dl.c
1 /*
2 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
3 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; version 2.1 of
9 * the License.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #define _GNU_SOURCE
22 #define _LGPL_SOURCE
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <lttng/ust-dlfcn.h>
30 #include <lttng/ust-elf.h>
31 #include <lttng/ust-events.h>
32 #include <helper.h>
33 #include "usterr-signal-safe.h"
34
35 /* Include link.h last else it conflicts with ust-dlfcn. */
36 #include <link.h>
37
38 #define TRACEPOINT_DEFINE
39 #include "ust_dl.h"
40
41 static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flags);
42 #ifdef HAVE_DLMOPEN
43 static void *(*__lttng_ust_plibc_dlmopen)(Lmid_t nsid, const char *filename,
44 int flags);
45 #endif
46 static int (*__lttng_ust_plibc_dlclose)(void *handle);
47
48 static
49 void *_lttng_ust_dl_libc_dlopen(const char *filename, int flags)
50 {
51 if (!__lttng_ust_plibc_dlopen) {
52 __lttng_ust_plibc_dlopen = dlsym(RTLD_NEXT, "dlopen");
53 if (!__lttng_ust_plibc_dlopen) {
54 fprintf(stderr, "%s\n", dlerror());
55 return NULL;
56 }
57 }
58 return __lttng_ust_plibc_dlopen(filename, flags);
59 }
60
61 #ifdef HAVE_DLMOPEN
62 static
63 void *_lttng_ust_dl_libc_dlmopen(Lmid_t nsid, const char *filename,
64 int flags)
65 {
66 if (!__lttng_ust_plibc_dlmopen) {
67 __lttng_ust_plibc_dlmopen = dlsym(RTLD_NEXT, "dlmopen");
68 if (!__lttng_ust_plibc_dlmopen) {
69 fprintf(stderr, "%s\n", dlerror());
70 return NULL;
71 }
72 }
73 return __lttng_ust_plibc_dlmopen(nsid, filename, flags);
74 }
75 #endif
76
77 static
78 int _lttng_ust_dl_libc_dlclose(void *handle)
79 {
80 if (!__lttng_ust_plibc_dlclose) {
81 __lttng_ust_plibc_dlclose = dlsym(RTLD_NEXT, "dlclose");
82 if (!__lttng_ust_plibc_dlclose) {
83 fprintf(stderr, "%s\n", dlerror());
84 return -1;
85 }
86 }
87 return __lttng_ust_plibc_dlclose(handle);
88 }
89
90 static
91 void lttng_ust_dl_dlopen(void *so_base, const char *so_name,
92 int flags, void *ip)
93 {
94 char resolved_path[PATH_MAX];
95 struct lttng_ust_elf *elf;
96 uint64_t memsz;
97 uint8_t *build_id = NULL;
98 size_t build_id_len;
99 char *dbg_file = NULL;
100 uint32_t crc;
101 int has_build_id = 0, has_debug_link = 0;
102 int ret;
103
104 if (!realpath(so_name, resolved_path)) {
105 ERR("could not resolve path '%s'", so_name);
106 return;
107 }
108
109 elf = lttng_ust_elf_create(resolved_path);
110 if (!elf) {
111 ERR("could not access file %s", resolved_path);
112 return;
113 }
114
115 ret = lttng_ust_elf_get_memsz(elf, &memsz);
116 if (ret) {
117 goto end;
118 }
119 ret = lttng_ust_elf_get_build_id(
120 elf, &build_id, &build_id_len, &has_build_id);
121 if (ret) {
122 goto end;
123 }
124 ret = lttng_ust_elf_get_debug_link(
125 elf, &dbg_file, &crc, &has_debug_link);
126 if (ret) {
127 goto end;
128 }
129
130 tracepoint(lttng_ust_dl, dlopen,
131 ip, so_base, resolved_path, flags, memsz,
132 has_build_id, has_debug_link);
133
134 if (has_build_id) {
135 tracepoint(lttng_ust_dl, build_id,
136 ip, so_base, build_id, build_id_len);
137 }
138
139 if (has_debug_link) {
140 tracepoint(lttng_ust_dl, debug_link,
141 ip, so_base, dbg_file, crc);
142 }
143
144 end:
145 free(dbg_file);
146 free(build_id);
147 lttng_ust_elf_destroy(elf);
148 return;
149 }
150
151 #ifdef HAVE_DLMOPEN
152 static
153 void lttng_ust_dl_dlmopen(void *so_base, Lmid_t nsid, const char *so_name,
154 int flags, void *ip)
155 {
156 char resolved_path[PATH_MAX];
157 struct lttng_ust_elf *elf;
158 uint64_t memsz;
159 uint8_t *build_id = NULL;
160 size_t build_id_len;
161 char *dbg_file = NULL;
162 uint32_t crc;
163 int has_build_id = 0, has_debug_link = 0;
164 int ret;
165
166 if (!realpath(so_name, resolved_path)) {
167 ERR("could not resolve path '%s'", so_name);
168 return;
169 }
170
171 elf = lttng_ust_elf_create(resolved_path);
172 if (!elf) {
173 ERR("could not access file %s", resolved_path);
174 return;
175 }
176
177 ret = lttng_ust_elf_get_memsz(elf, &memsz);
178 if (ret) {
179 goto end;
180 }
181 ret = lttng_ust_elf_get_build_id(
182 elf, &build_id, &build_id_len, &has_build_id);
183 if (ret) {
184 goto end;
185 }
186 ret = lttng_ust_elf_get_debug_link(
187 elf, &dbg_file, &crc, &has_debug_link);
188 if (ret) {
189 goto end;
190 }
191
192 tracepoint(lttng_ust_dl, dlmopen,
193 ip, so_base, nsid, resolved_path, flags, memsz,
194 has_build_id, has_debug_link);
195
196 if (has_build_id) {
197 tracepoint(lttng_ust_dl, build_id,
198 ip, so_base, build_id, build_id_len);
199 }
200
201 if (has_debug_link) {
202 tracepoint(lttng_ust_dl, debug_link,
203 ip, so_base, dbg_file, crc);
204 }
205
206 end:
207 free(dbg_file);
208 free(build_id);
209 lttng_ust_elf_destroy(elf);
210 return;
211 }
212 #endif
213
214 void *dlopen(const char *filename, int flags)
215 {
216 void *handle;
217
218 handle = _lttng_ust_dl_libc_dlopen(filename, flags);
219 if (__tracepoint_ptrs_registered && handle) {
220 struct link_map *p = NULL;
221 int ret;
222
223 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
224 if (ret != -1 && p != NULL && p->l_addr != 0) {
225 lttng_ust_dl_dlopen((void *) p->l_addr,
226 p->l_name, flags, LTTNG_UST_CALLER_IP());
227 }
228 }
229 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
230 return handle;
231 }
232
233 #ifdef HAVE_DLMOPEN
234 void *dlmopen(Lmid_t nsid, const char *filename, int flags)
235 {
236 void *handle;
237
238 handle = _lttng_ust_dl_libc_dlmopen(nsid, filename, flags);
239 if (__tracepoint_ptrs_registered && handle) {
240 struct link_map *p = NULL;
241 int ret;
242
243 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
244 if (ret != -1 && p != NULL && p->l_addr != 0) {
245 lttng_ust_dl_dlmopen((void *) p->l_addr,
246 nsid, p->l_name, flags,
247 LTTNG_UST_CALLER_IP());
248 }
249 }
250 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
251 return handle;
252
253 }
254 #endif
255
256 int dlclose(void *handle)
257 {
258 int ret;
259
260 if (__tracepoint_ptrs_registered) {
261 struct link_map *p = NULL;
262
263 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
264 if (ret != -1 && p != NULL && p->l_addr != 0) {
265 tracepoint(lttng_ust_dl, dlclose,
266 LTTNG_UST_CALLER_IP(),
267 (void *) p->l_addr);
268 }
269 }
270 ret = _lttng_ust_dl_libc_dlclose(handle);
271 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
272 return ret;
273 }
This page took 0.033565 seconds and 4 git commands to generate.