Cleanup: apply `include-what-you-use` guideline for `uint*_t`
[lttng-ust.git] / liblttng-ust-dl / lttng-ust-dl.c
CommitLineData
b13d93c2
PW
1/*
2 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
8e2aed3f 3 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
97c7c238 4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
b13d93c2
PW
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
1ddceb36 22#define _LGPL_SOURCE
13436238 23#include <limits.h>
8e2aed3f 24#include <stdio.h>
fb31eb73 25#include <stdint.h>
13436238 26#include <sys/types.h>
8e2aed3f
AB
27#include <unistd.h>
28
29#include <lttng/ust-dlfcn.h>
30#include <lttng/ust-elf.h>
97c7c238 31#include <lttng/ust-events.h>
171fcc6f 32#include <helper.h>
eb2b066f 33#include "usterr-signal-safe.h"
b13d93c2 34
8e2aed3f
AB
35/* Include link.h last else it conflicts with ust-dlfcn. */
36#include <link.h>
b13d93c2 37
13436238 38#define TRACEPOINT_DEFINE
6d4658aa 39#include "ust_dl.h"
13436238 40
7b0fdd83 41static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flags);
42330adc 42#ifdef HAVE_DLMOPEN
7b0fdd83
MD
43static void *(*__lttng_ust_plibc_dlmopen)(Lmid_t nsid, const char *filename,
44 int flags);
42330adc 45#endif
b13d93c2 46static int (*__lttng_ust_plibc_dlclose)(void *handle);
b13d93c2
PW
47
48static
7b0fdd83 49void *_lttng_ust_dl_libc_dlopen(const char *filename, int flags)
b13d93c2
PW
50{
51 if (!__lttng_ust_plibc_dlopen) {
52 __lttng_ust_plibc_dlopen = dlsym(RTLD_NEXT, "dlopen");
8e2aed3f 53 if (!__lttng_ust_plibc_dlopen) {
b13d93c2
PW
54 fprintf(stderr, "%s\n", dlerror());
55 return NULL;
56 }
57 }
7b0fdd83
MD
58 return __lttng_ust_plibc_dlopen(filename, flags);
59}
60
42330adc 61#ifdef HAVE_DLMOPEN
7b0fdd83
MD
62static
63void *_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);
b13d93c2 74}
42330adc 75#endif
b13d93c2
PW
76
77static
78int _lttng_ust_dl_libc_dlclose(void *handle)
79{
80 if (!__lttng_ust_plibc_dlclose) {
81 __lttng_ust_plibc_dlclose = dlsym(RTLD_NEXT, "dlclose");
8e2aed3f 82 if (!__lttng_ust_plibc_dlclose) {
b13d93c2
PW
83 fprintf(stderr, "%s\n", dlerror());
84 return -1;
85 }
86 }
87 return __lttng_ust_plibc_dlclose(handle);
88}
89
90static
7b0fdd83
MD
91void lttng_ust_dl_dlopen(void *so_base, const char *so_name,
92 int flags, void *ip)
b13d93c2 93{
13436238 94 char resolved_path[PATH_MAX];
8e2aed3f
AB
95 struct lttng_ust_elf *elf;
96 uint64_t memsz;
83215d66 97 uint8_t *build_id = NULL;
8e2aed3f 98 size_t build_id_len;
83215d66 99 char *dbg_file = NULL;
8e2aed3f
AB
100 uint32_t crc;
101 int has_build_id = 0, has_debug_link = 0;
102 int ret;
b13d93c2 103
13436238
PW
104 if (!realpath(so_name, resolved_path)) {
105 ERR("could not resolve path '%s'", so_name);
106 return;
b13d93c2 107 }
b13d93c2 108
8e2aed3f
AB
109 elf = lttng_ust_elf_create(resolved_path);
110 if (!elf) {
09026ccf 111 ERR("could not access file %s", resolved_path);
13436238 112 return;
b13d93c2 113 }
13436238 114
8e2aed3f
AB
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
6d4658aa 130 tracepoint(lttng_ust_dl, dlopen,
7b0fdd83 131 ip, so_base, resolved_path, flags, memsz,
c5c4fd82 132 has_build_id, has_debug_link);
8e2aed3f
AB
133
134 if (has_build_id) {
135 tracepoint(lttng_ust_dl, build_id,
136 ip, so_base, build_id, build_id_len);
8e2aed3f
AB
137 }
138
139 if (has_debug_link) {
140 tracepoint(lttng_ust_dl, debug_link,
141 ip, so_base, dbg_file, crc);
8e2aed3f
AB
142 }
143
144end:
83215d66
MD
145 free(dbg_file);
146 free(build_id);
8e2aed3f 147 lttng_ust_elf_destroy(elf);
13436238 148 return;
b13d93c2
PW
149}
150
42330adc 151#ifdef HAVE_DLMOPEN
7b0fdd83
MD
152static
153void 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) {
09026ccf 173 ERR("could not access file %s", resolved_path);
7b0fdd83
MD
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
206end:
207 free(dbg_file);
208 free(build_id);
209 lttng_ust_elf_destroy(elf);
210 return;
211}
42330adc 212#endif
7b0fdd83
MD
213
214void *dlopen(const char *filename, int flags)
b13d93c2 215{
8e2aed3f
AB
216 void *handle;
217
7b0fdd83 218 handle = _lttng_ust_dl_libc_dlopen(filename, flags);
bd703713 219 if (__tracepoint_ptrs_registered && handle) {
b13d93c2 220 struct link_map *p = NULL;
8e2aed3f
AB
221 int ret;
222
223 ret = dlinfo(handle, RTLD_DI_LINKMAP, &p);
224 if (ret != -1 && p != NULL && p->l_addr != 0) {
97c7c238 225 lttng_ust_dl_dlopen((void *) p->l_addr,
7b0fdd83
MD
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
42330adc 233#ifdef HAVE_DLMOPEN
7b0fdd83
MD
234void *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,
171fcc6f 247 LTTNG_UST_CALLER_IP());
8e2aed3f 248 }
b13d93c2 249 }
97c7c238 250 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
b13d93c2 251 return handle;
7b0fdd83 252
b13d93c2 253}
42330adc 254#endif
b13d93c2
PW
255
256int dlclose(void *handle)
257{
97c7c238
MD
258 int ret;
259
e7953e6e 260 if (__tracepoint_ptrs_registered) {
b13d93c2 261 struct link_map *p = NULL;
8e2aed3f
AB
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,
171fcc6f 266 LTTNG_UST_CALLER_IP(),
8e2aed3f
AB
267 (void *) p->l_addr);
268 }
b13d93c2 269 }
97c7c238
MD
270 ret = _lttng_ust_dl_libc_dlclose(handle);
271 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
272 return ret;
b13d93c2 273}
This page took 0.037288 seconds and 4 git commands to generate.