b0737b65f9f4222d6beef931b7ce3a07eef0ea1c
[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 <sys/types.h>
26 #include <unistd.h>
27
28 #include <lttng/ust-dlfcn.h>
29 #include <lttng/ust-elf.h>
30 #include <lttng/ust-events.h>
31 #include <helper.h>
32 #include "usterr-signal-safe.h"
33
34 /* Include link.h last else it conflicts with ust-dlfcn. */
35 #include <link.h>
36
37 #define TRACEPOINT_DEFINE
38 #include "ust_dl.h"
39
40 static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flags);
41 static void *(*__lttng_ust_plibc_dlmopen)(Lmid_t nsid, const char *filename,
42 int flags);
43 static int (*__lttng_ust_plibc_dlclose)(void *handle);
44
45 static
46 void *_lttng_ust_dl_libc_dlopen(const char *filename, int flags)
47 {
48 if (!__lttng_ust_plibc_dlopen) {
49 __lttng_ust_plibc_dlopen = dlsym(RTLD_NEXT, "dlopen");
50 if (!__lttng_ust_plibc_dlopen) {
51 fprintf(stderr, "%s\n", dlerror());
52 return NULL;
53 }
54 }
55 return __lttng_ust_plibc_dlopen(filename, flags);
56 }
57
58 static
59 void *_lttng_ust_dl_libc_dlmopen(Lmid_t nsid, const char *filename,
60 int flags)
61 {
62 if (!__lttng_ust_plibc_dlmopen) {
63 __lttng_ust_plibc_dlmopen = dlsym(RTLD_NEXT, "dlmopen");
64 if (!__lttng_ust_plibc_dlmopen) {
65 fprintf(stderr, "%s\n", dlerror());
66 return NULL;
67 }
68 }
69 return __lttng_ust_plibc_dlmopen(nsid, filename, flags);
70 }
71
72 static
73 int _lttng_ust_dl_libc_dlclose(void *handle)
74 {
75 if (!__lttng_ust_plibc_dlclose) {
76 __lttng_ust_plibc_dlclose = dlsym(RTLD_NEXT, "dlclose");
77 if (!__lttng_ust_plibc_dlclose) {
78 fprintf(stderr, "%s\n", dlerror());
79 return -1;
80 }
81 }
82 return __lttng_ust_plibc_dlclose(handle);
83 }
84
85 static
86 void lttng_ust_dl_dlopen(void *so_base, const char *so_name,
87 int flags, void *ip)
88 {
89 char resolved_path[PATH_MAX];
90 struct lttng_ust_elf *elf;
91 uint64_t memsz;
92 uint8_t *build_id = NULL;
93 size_t build_id_len;
94 char *dbg_file = NULL;
95 uint32_t crc;
96 int has_build_id = 0, has_debug_link = 0;
97 int ret;
98
99 if (!realpath(so_name, resolved_path)) {
100 ERR("could not resolve path '%s'", so_name);
101 return;
102 }
103
104 elf = lttng_ust_elf_create(resolved_path);
105 if (!elf) {
106 ERR("could not acces file %s", resolved_path);
107 return;
108 }
109
110 ret = lttng_ust_elf_get_memsz(elf, &memsz);
111 if (ret) {
112 goto end;
113 }
114 ret = lttng_ust_elf_get_build_id(
115 elf, &build_id, &build_id_len, &has_build_id);
116 if (ret) {
117 goto end;
118 }
119 ret = lttng_ust_elf_get_debug_link(
120 elf, &dbg_file, &crc, &has_debug_link);
121 if (ret) {
122 goto end;
123 }
124
125 tracepoint(lttng_ust_dl, dlopen,
126 ip, so_base, resolved_path, flags, memsz,
127 has_build_id, has_debug_link);
128
129 if (has_build_id) {
130 tracepoint(lttng_ust_dl, build_id,
131 ip, so_base, build_id, build_id_len);
132 }
133
134 if (has_debug_link) {
135 tracepoint(lttng_ust_dl, debug_link,
136 ip, so_base, dbg_file, crc);
137 }
138
139 end:
140 free(dbg_file);
141 free(build_id);
142 lttng_ust_elf_destroy(elf);
143 return;
144 }
145
146 static
147 void lttng_ust_dl_dlmopen(void *so_base, Lmid_t nsid, const char *so_name,
148 int flags, void *ip)
149 {
150 char resolved_path[PATH_MAX];
151 struct lttng_ust_elf *elf;
152 uint64_t memsz;
153 uint8_t *build_id = NULL;
154 size_t build_id_len;
155 char *dbg_file = NULL;
156 uint32_t crc;
157 int has_build_id = 0, has_debug_link = 0;
158 int ret;
159
160 if (!realpath(so_name, resolved_path)) {
161 ERR("could not resolve path '%s'", so_name);
162 return;
163 }
164
165 elf = lttng_ust_elf_create(resolved_path);
166 if (!elf) {
167 ERR("could not acces file %s", resolved_path);
168 return;
169 }
170
171 ret = lttng_ust_elf_get_memsz(elf, &memsz);
172 if (ret) {
173 goto end;
174 }
175 ret = lttng_ust_elf_get_build_id(
176 elf, &build_id, &build_id_len, &has_build_id);
177 if (ret) {
178 goto end;
179 }
180 ret = lttng_ust_elf_get_debug_link(
181 elf, &dbg_file, &crc, &has_debug_link);
182 if (ret) {
183 goto end;
184 }
185
186 tracepoint(lttng_ust_dl, dlmopen,
187 ip, so_base, nsid, resolved_path, flags, memsz,
188 has_build_id, has_debug_link);
189
190 if (has_build_id) {
191 tracepoint(lttng_ust_dl, build_id,
192 ip, so_base, build_id, build_id_len);
193 }
194
195 if (has_debug_link) {
196 tracepoint(lttng_ust_dl, debug_link,
197 ip, so_base, dbg_file, crc);
198 }
199
200 end:
201 free(dbg_file);
202 free(build_id);
203 lttng_ust_elf_destroy(elf);
204 return;
205 }
206
207 void *dlopen(const char *filename, int flags)
208 {
209 void *handle;
210
211 handle = _lttng_ust_dl_libc_dlopen(filename, flags);
212 if (__tracepoint_ptrs_registered && handle) {
213 struct link_map *p = NULL;
214 int ret;
215
216 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
217 if (ret != -1 && p != NULL && p->l_addr != 0) {
218 lttng_ust_dl_dlopen((void *) p->l_addr,
219 p->l_name, flags, LTTNG_UST_CALLER_IP());
220 }
221 }
222 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
223 return handle;
224 }
225
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 (__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
247 int dlclose(void *handle)
248 {
249 int ret;
250
251 if (__tracepoint_ptrs_registered) {
252 struct link_map *p = NULL;
253
254 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
255 if (ret != -1 && p != NULL && p->l_addr != 0) {
256 tracepoint(lttng_ust_dl, dlclose,
257 LTTNG_UST_CALLER_IP(),
258 (void *) p->l_addr);
259 }
260 }
261 ret = _lttng_ust_dl_libc_dlclose(handle);
262 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
263 return ret;
264 }
This page took 0.033279 seconds and 3 git commands to generate.