Hide internal tracepoint and providers data symbols
[lttng-ust.git] / src / lib / lttng-ust-dl / lttng-ust-dl.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
5 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
6 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 */
8
9 #define _LGPL_SOURCE
10
11 /* Has to be included first to override dlfcn.h */
12 #include <common/compat/dlfcn.h>
13
14 #include <limits.h>
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19
20 #include "common/elf.h"
21 #include <lttng/ust-events.h>
22 #include "common/macros.h"
23 #include "common/logging.h"
24 #include "common/events.h"
25
26 /* Include link.h last else it conflicts with ust-dlfcn. */
27 #include <link.h>
28
29 #define LTTNG_UST_TRACEPOINT_HIDDEN_DEFINITION
30 #define LTTNG_UST_TRACEPOINT_DEFINE
31 #include "ust_dl.h"
32
33 static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flags);
34 #ifdef HAVE_DLMOPEN
35 static void *(*__lttng_ust_plibc_dlmopen)(Lmid_t nsid, const char *filename,
36 int flags);
37 #endif
38 static int (*__lttng_ust_plibc_dlclose)(void *handle);
39
40 static
41 void *_lttng_ust_dl_libc_dlopen(const char *filename, int flags)
42 {
43 if (!__lttng_ust_plibc_dlopen) {
44 __lttng_ust_plibc_dlopen = dlsym(RTLD_NEXT, "dlopen");
45 if (!__lttng_ust_plibc_dlopen) {
46 fprintf(stderr, "%s\n", dlerror());
47 return NULL;
48 }
49 }
50 return __lttng_ust_plibc_dlopen(filename, flags);
51 }
52
53 #ifdef HAVE_DLMOPEN
54 static
55 void *_lttng_ust_dl_libc_dlmopen(Lmid_t nsid, const char *filename,
56 int flags)
57 {
58 if (!__lttng_ust_plibc_dlmopen) {
59 __lttng_ust_plibc_dlmopen = dlsym(RTLD_NEXT, "dlmopen");
60 if (!__lttng_ust_plibc_dlmopen) {
61 fprintf(stderr, "%s\n", dlerror());
62 return NULL;
63 }
64 }
65 return __lttng_ust_plibc_dlmopen(nsid, filename, flags);
66 }
67 #endif
68
69 static
70 int _lttng_ust_dl_libc_dlclose(void *handle)
71 {
72 if (!__lttng_ust_plibc_dlclose) {
73 __lttng_ust_plibc_dlclose = dlsym(RTLD_NEXT, "dlclose");
74 if (!__lttng_ust_plibc_dlclose) {
75 fprintf(stderr, "%s\n", dlerror());
76 return -1;
77 }
78 }
79 return __lttng_ust_plibc_dlclose(handle);
80 }
81
82 static
83 void lttng_ust_dl_dlopen(void *so_base, const char *so_name,
84 int flags, void *ip)
85 {
86 char resolved_path[PATH_MAX];
87 struct lttng_ust_elf *elf;
88 uint64_t memsz;
89 uint8_t *build_id = NULL;
90 size_t build_id_len;
91 char *dbg_file = NULL;
92 uint32_t crc;
93 int has_build_id = 0, has_debug_link = 0;
94 int ret;
95
96 if (!realpath(so_name, resolved_path)) {
97 ERR("could not resolve path '%s'", so_name);
98 return;
99 }
100
101 elf = lttng_ust_elf_create(resolved_path);
102 if (!elf) {
103 ERR("could not access file %s", resolved_path);
104 return;
105 }
106
107 ret = lttng_ust_elf_get_memsz(elf, &memsz);
108 if (ret) {
109 goto end;
110 }
111 ret = lttng_ust_elf_get_build_id(
112 elf, &build_id, &build_id_len, &has_build_id);
113 if (ret) {
114 goto end;
115 }
116 ret = lttng_ust_elf_get_debug_link(
117 elf, &dbg_file, &crc, &has_debug_link);
118 if (ret) {
119 goto end;
120 }
121
122 lttng_ust_tracepoint(lttng_ust_dl, dlopen,
123 ip, so_base, resolved_path, flags, memsz,
124 has_build_id, has_debug_link);
125
126 if (has_build_id) {
127 lttng_ust_tracepoint(lttng_ust_dl, build_id,
128 ip, so_base, build_id, build_id_len);
129 }
130
131 if (has_debug_link) {
132 lttng_ust_tracepoint(lttng_ust_dl, debug_link,
133 ip, so_base, dbg_file, crc);
134 }
135
136 end:
137 free(dbg_file);
138 free(build_id);
139 lttng_ust_elf_destroy(elf);
140 return;
141 }
142
143 #ifdef HAVE_DLMOPEN
144 static
145 void lttng_ust_dl_dlmopen(void *so_base, Lmid_t nsid, const char *so_name,
146 int flags, void *ip)
147 {
148 char resolved_path[PATH_MAX];
149 struct lttng_ust_elf *elf;
150 uint64_t memsz;
151 uint8_t *build_id = NULL;
152 size_t build_id_len;
153 char *dbg_file = NULL;
154 uint32_t crc;
155 int has_build_id = 0, has_debug_link = 0;
156 int ret;
157
158 if (!realpath(so_name, resolved_path)) {
159 ERR("could not resolve path '%s'", so_name);
160 return;
161 }
162
163 elf = lttng_ust_elf_create(resolved_path);
164 if (!elf) {
165 ERR("could not access file %s", resolved_path);
166 return;
167 }
168
169 ret = lttng_ust_elf_get_memsz(elf, &memsz);
170 if (ret) {
171 goto end;
172 }
173 ret = lttng_ust_elf_get_build_id(
174 elf, &build_id, &build_id_len, &has_build_id);
175 if (ret) {
176 goto end;
177 }
178 ret = lttng_ust_elf_get_debug_link(
179 elf, &dbg_file, &crc, &has_debug_link);
180 if (ret) {
181 goto end;
182 }
183
184 lttng_ust_tracepoint(lttng_ust_dl, dlmopen,
185 ip, so_base, nsid, resolved_path, flags, memsz,
186 has_build_id, has_debug_link);
187
188 if (has_build_id) {
189 lttng_ust_tracepoint(lttng_ust_dl, build_id,
190 ip, so_base, build_id, build_id_len);
191 }
192
193 if (has_debug_link) {
194 lttng_ust_tracepoint(lttng_ust_dl, debug_link,
195 ip, so_base, dbg_file, crc);
196 }
197
198 end:
199 free(dbg_file);
200 free(build_id);
201 lttng_ust_elf_destroy(elf);
202 return;
203 }
204 #endif
205
206 void *dlopen(const char *filename, int flags)
207 {
208 void *handle;
209
210 handle = _lttng_ust_dl_libc_dlopen(filename, flags);
211 if (lttng_ust_tracepoint_ptrs_registered && handle) {
212 struct link_map *p = NULL;
213 int ret;
214
215 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
216 if (ret != -1 && p != NULL && p->l_addr != 0) {
217 lttng_ust_dl_dlopen((void *) p->l_addr,
218 p->l_name, flags, LTTNG_UST_CALLER_IP());
219 }
220 }
221 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
222 return handle;
223 }
224
225 #ifdef HAVE_DLMOPEN
226 void *dlmopen(Lmid_t nsid, const char *filename, int flags)
227 {
228 void *handle;
229
230 handle = _lttng_ust_dl_libc_dlmopen(nsid, filename, flags);
231 if (lttng_ust_tracepoint_ptrs_registered && handle) {
232 struct link_map *p = NULL;
233 int ret;
234
235 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
236 if (ret != -1 && p != NULL && p->l_addr != 0) {
237 lttng_ust_dl_dlmopen((void *) p->l_addr,
238 nsid, p->l_name, flags,
239 LTTNG_UST_CALLER_IP());
240 }
241 }
242 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
243 return handle;
244
245 }
246 #endif
247
248 int dlclose(void *handle)
249 {
250 int ret;
251
252 if (lttng_ust_tracepoint_ptrs_registered) {
253 struct link_map *p = NULL;
254
255 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
256 if (ret != -1 && p != NULL && p->l_addr != 0) {
257 lttng_ust_tracepoint(lttng_ust_dl, dlclose,
258 LTTNG_UST_CALLER_IP(),
259 (void *) p->l_addr);
260 }
261 }
262 ret = _lttng_ust_dl_libc_dlclose(handle);
263 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
264 return ret;
265 }
This page took 0.033773 seconds and 4 git commands to generate.