2 * SPDX-License-Identifier: LGPL-2.1-only
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>
13 #include <sys/types.h>
16 #include <ust-dlfcn.h>
17 #include <lttng/ust-elf.h>
18 #include <lttng/ust-events.h>
19 #include <ust-helper.h>
20 #include "usterr-signal-safe.h"
22 /* Include link.h last else it conflicts with ust-dlfcn. */
25 #define TRACEPOINT_DEFINE
28 static void *(*__lttng_ust_plibc_dlopen
)(const char *filename
, int flags
);
30 static void *(*__lttng_ust_plibc_dlmopen
)(Lmid_t nsid
, const char *filename
,
33 static int (*__lttng_ust_plibc_dlclose
)(void *handle
);
35 static __attribute__((constructor
))
36 void _lttng_ust_dl_init(void)
42 void *_lttng_ust_dl_libc_dlopen(const char *filename
, int flags
)
44 if (!__lttng_ust_plibc_dlopen
) {
45 __lttng_ust_plibc_dlopen
= dlsym(RTLD_NEXT
, "dlopen");
46 if (!__lttng_ust_plibc_dlopen
) {
47 fprintf(stderr
, "%s\n", dlerror());
51 return __lttng_ust_plibc_dlopen(filename
, flags
);
56 void *_lttng_ust_dl_libc_dlmopen(Lmid_t nsid
, const char *filename
,
59 if (!__lttng_ust_plibc_dlmopen
) {
60 __lttng_ust_plibc_dlmopen
= dlsym(RTLD_NEXT
, "dlmopen");
61 if (!__lttng_ust_plibc_dlmopen
) {
62 fprintf(stderr
, "%s\n", dlerror());
66 return __lttng_ust_plibc_dlmopen(nsid
, filename
, flags
);
71 int _lttng_ust_dl_libc_dlclose(void *handle
)
73 if (!__lttng_ust_plibc_dlclose
) {
74 __lttng_ust_plibc_dlclose
= dlsym(RTLD_NEXT
, "dlclose");
75 if (!__lttng_ust_plibc_dlclose
) {
76 fprintf(stderr
, "%s\n", dlerror());
80 return __lttng_ust_plibc_dlclose(handle
);
84 void lttng_ust_dl_dlopen(void *so_base
, const char *so_name
,
87 char resolved_path
[PATH_MAX
];
88 struct lttng_ust_elf
*elf
;
90 uint8_t *build_id
= NULL
;
92 char *dbg_file
= NULL
;
94 int has_build_id
= 0, has_debug_link
= 0;
97 if (!realpath(so_name
, resolved_path
)) {
98 ERR("could not resolve path '%s'", so_name
);
102 elf
= lttng_ust_elf_create(resolved_path
);
104 ERR("could not access file %s", resolved_path
);
108 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
112 ret
= lttng_ust_elf_get_build_id(
113 elf
, &build_id
, &build_id_len
, &has_build_id
);
117 ret
= lttng_ust_elf_get_debug_link(
118 elf
, &dbg_file
, &crc
, &has_debug_link
);
123 tracepoint(lttng_ust_dl
, dlopen
,
124 ip
, so_base
, resolved_path
, flags
, memsz
,
125 has_build_id
, has_debug_link
);
128 tracepoint(lttng_ust_dl
, build_id
,
129 ip
, so_base
, build_id
, build_id_len
);
132 if (has_debug_link
) {
133 tracepoint(lttng_ust_dl
, debug_link
,
134 ip
, so_base
, dbg_file
, crc
);
140 lttng_ust_elf_destroy(elf
);
146 void lttng_ust_dl_dlmopen(void *so_base
, Lmid_t nsid
, const char *so_name
,
149 char resolved_path
[PATH_MAX
];
150 struct lttng_ust_elf
*elf
;
152 uint8_t *build_id
= NULL
;
154 char *dbg_file
= NULL
;
156 int has_build_id
= 0, has_debug_link
= 0;
159 if (!realpath(so_name
, resolved_path
)) {
160 ERR("could not resolve path '%s'", so_name
);
164 elf
= lttng_ust_elf_create(resolved_path
);
166 ERR("could not access file %s", resolved_path
);
170 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
174 ret
= lttng_ust_elf_get_build_id(
175 elf
, &build_id
, &build_id_len
, &has_build_id
);
179 ret
= lttng_ust_elf_get_debug_link(
180 elf
, &dbg_file
, &crc
, &has_debug_link
);
185 tracepoint(lttng_ust_dl
, dlmopen
,
186 ip
, so_base
, nsid
, resolved_path
, flags
, memsz
,
187 has_build_id
, has_debug_link
);
190 tracepoint(lttng_ust_dl
, build_id
,
191 ip
, so_base
, build_id
, build_id_len
);
194 if (has_debug_link
) {
195 tracepoint(lttng_ust_dl
, debug_link
,
196 ip
, so_base
, dbg_file
, crc
);
202 lttng_ust_elf_destroy(elf
);
207 void *dlopen(const char *filename
, int flags
)
211 handle
= _lttng_ust_dl_libc_dlopen(filename
, flags
);
212 if (__tracepoint_ptrs_registered
&& handle
) {
213 struct link_map
*p
= NULL
;
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());
222 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
227 void *dlmopen(Lmid_t nsid
, const char *filename
, int flags
)
231 handle
= _lttng_ust_dl_libc_dlmopen(nsid
, filename
, flags
);
232 if (__tracepoint_ptrs_registered
&& handle
) {
233 struct link_map
*p
= NULL
;
236 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
237 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
238 lttng_ust_dl_dlmopen((void *) p
->l_addr
,
239 nsid
, p
->l_name
, flags
,
240 LTTNG_UST_CALLER_IP());
243 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
249 int dlclose(void *handle
)
253 if (__tracepoint_ptrs_registered
) {
254 struct link_map
*p
= NULL
;
256 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
257 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
258 tracepoint(lttng_ust_dl
, dlclose
,
259 LTTNG_UST_CALLER_IP(),
263 ret
= _lttng_ust_dl_libc_dlclose(handle
);
264 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());