Split ust-events-internal.h between common and liblttng-ust
[lttng-ust.git] / src / lib / lttng-ust-dl / lttng-ust-dl.c
CommitLineData
b13d93c2 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
b13d93c2 3 *
c0c0989a
MJ
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>
b13d93c2
PW
7 */
8
1ddceb36 9#define _LGPL_SOURCE
9d315d6d
MJ
10
11/* Has to be included first to override dlfcn.h */
12#include <common/compat/dlfcn.h>
13
13436238 14#include <limits.h>
8e2aed3f 15#include <stdio.h>
fb31eb73 16#include <stdint.h>
13436238 17#include <sys/types.h>
8e2aed3f
AB
18#include <unistd.h>
19
9d315d6d 20#include "common/elf.h"
97c7c238 21#include <lttng/ust-events.h>
9d315d6d
MJ
22#include "common/macros.h"
23#include "common/logging.h"
36c52fff 24#include "common/events.h"
cbba5e04 25
8e2aed3f
AB
26/* Include link.h last else it conflicts with ust-dlfcn. */
27#include <link.h>
b13d93c2 28
13436238 29#define TRACEPOINT_DEFINE
6d4658aa 30#include "ust_dl.h"
13436238 31
7b0fdd83 32static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flags);
42330adc 33#ifdef HAVE_DLMOPEN
7b0fdd83
MD
34static void *(*__lttng_ust_plibc_dlmopen)(Lmid_t nsid, const char *filename,
35 int flags);
42330adc 36#endif
b13d93c2 37static int (*__lttng_ust_plibc_dlclose)(void *handle);
b13d93c2 38
465a0d04
MJ
39static
40void _lttng_ust_dl_init(void)
41 __attribute__((constructor));
42static
2b6f4374
MJ
43void _lttng_ust_dl_init(void)
44{
c6e6343c 45 lttng_ust_logging_init();
2b6f4374
MJ
46}
47
b13d93c2 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.039912 seconds and 4 git commands to generate.