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>
18 #include <lttng/ust-events.h>
19 #include <ust-helper.h>
20 #include "usterr-signal-safe.h"
22 #include "../liblttng-ust/ust-events-internal.h"
24 /* Include link.h last else it conflicts with ust-dlfcn. */
27 #define TRACEPOINT_DEFINE
30 static void *(*__lttng_ust_plibc_dlopen
)(const char *filename
, int flags
);
32 static void *(*__lttng_ust_plibc_dlmopen
)(Lmid_t nsid
, const char *filename
,
35 static int (*__lttng_ust_plibc_dlclose
)(void *handle
);
37 static __attribute__((constructor
))
38 void _lttng_ust_dl_init(void)
44 void *_lttng_ust_dl_libc_dlopen(const char *filename
, int flags
)
46 if (!__lttng_ust_plibc_dlopen
) {
47 __lttng_ust_plibc_dlopen
= dlsym(RTLD_NEXT
, "dlopen");
48 if (!__lttng_ust_plibc_dlopen
) {
49 fprintf(stderr
, "%s\n", dlerror());
53 return __lttng_ust_plibc_dlopen(filename
, flags
);
58 void *_lttng_ust_dl_libc_dlmopen(Lmid_t nsid
, const char *filename
,
61 if (!__lttng_ust_plibc_dlmopen
) {
62 __lttng_ust_plibc_dlmopen
= dlsym(RTLD_NEXT
, "dlmopen");
63 if (!__lttng_ust_plibc_dlmopen
) {
64 fprintf(stderr
, "%s\n", dlerror());
68 return __lttng_ust_plibc_dlmopen(nsid
, filename
, flags
);
73 int _lttng_ust_dl_libc_dlclose(void *handle
)
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());
82 return __lttng_ust_plibc_dlclose(handle
);
86 void lttng_ust_dl_dlopen(void *so_base
, const char *so_name
,
89 char resolved_path
[PATH_MAX
];
90 struct lttng_ust_elf
*elf
;
92 uint8_t *build_id
= NULL
;
94 char *dbg_file
= NULL
;
96 int has_build_id
= 0, has_debug_link
= 0;
99 if (!realpath(so_name
, resolved_path
)) {
100 ERR("could not resolve path '%s'", so_name
);
104 elf
= lttng_ust_elf_create(resolved_path
);
106 ERR("could not access file %s", resolved_path
);
110 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
114 ret
= lttng_ust_elf_get_build_id(
115 elf
, &build_id
, &build_id_len
, &has_build_id
);
119 ret
= lttng_ust_elf_get_debug_link(
120 elf
, &dbg_file
, &crc
, &has_debug_link
);
125 tracepoint(lttng_ust_dl
, dlopen
,
126 ip
, so_base
, resolved_path
, flags
, memsz
,
127 has_build_id
, has_debug_link
);
130 tracepoint(lttng_ust_dl
, build_id
,
131 ip
, so_base
, build_id
, build_id_len
);
134 if (has_debug_link
) {
135 tracepoint(lttng_ust_dl
, debug_link
,
136 ip
, so_base
, dbg_file
, crc
);
142 lttng_ust_elf_destroy(elf
);
148 void lttng_ust_dl_dlmopen(void *so_base
, Lmid_t nsid
, const char *so_name
,
151 char resolved_path
[PATH_MAX
];
152 struct lttng_ust_elf
*elf
;
154 uint8_t *build_id
= NULL
;
156 char *dbg_file
= NULL
;
158 int has_build_id
= 0, has_debug_link
= 0;
161 if (!realpath(so_name
, resolved_path
)) {
162 ERR("could not resolve path '%s'", so_name
);
166 elf
= lttng_ust_elf_create(resolved_path
);
168 ERR("could not access file %s", resolved_path
);
172 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
176 ret
= lttng_ust_elf_get_build_id(
177 elf
, &build_id
, &build_id_len
, &has_build_id
);
181 ret
= lttng_ust_elf_get_debug_link(
182 elf
, &dbg_file
, &crc
, &has_debug_link
);
187 tracepoint(lttng_ust_dl
, dlmopen
,
188 ip
, so_base
, nsid
, resolved_path
, flags
, memsz
,
189 has_build_id
, has_debug_link
);
192 tracepoint(lttng_ust_dl
, build_id
,
193 ip
, so_base
, build_id
, build_id_len
);
196 if (has_debug_link
) {
197 tracepoint(lttng_ust_dl
, debug_link
,
198 ip
, so_base
, dbg_file
, crc
);
204 lttng_ust_elf_destroy(elf
);
209 void *dlopen(const char *filename
, int flags
)
213 handle
= _lttng_ust_dl_libc_dlopen(filename
, flags
);
214 if (__tracepoint_ptrs_registered
&& handle
) {
215 struct link_map
*p
= NULL
;
218 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
219 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
220 lttng_ust_dl_dlopen((void *) p
->l_addr
,
221 p
->l_name
, flags
, LTTNG_UST_CALLER_IP());
224 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
229 void *dlmopen(Lmid_t nsid
, const char *filename
, int flags
)
233 handle
= _lttng_ust_dl_libc_dlmopen(nsid
, filename
, flags
);
234 if (__tracepoint_ptrs_registered
&& handle
) {
235 struct link_map
*p
= NULL
;
238 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
239 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
240 lttng_ust_dl_dlmopen((void *) p
->l_addr
,
241 nsid
, p
->l_name
, flags
,
242 LTTNG_UST_CALLER_IP());
245 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
251 int dlclose(void *handle
)
255 if (__tracepoint_ptrs_registered
) {
256 struct link_map
*p
= NULL
;
258 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
259 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
260 tracepoint(lttng_ust_dl
, dlclose
,
261 LTTNG_UST_CALLER_IP(),
265 ret
= _lttng_ust_dl_libc_dlclose(handle
);
266 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());