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
);
38 void _lttng_ust_dl_init(void)
39 __attribute__((constructor
));
41 void _lttng_ust_dl_init(void)
47 void *_lttng_ust_dl_libc_dlopen(const char *filename
, int flags
)
49 if (!__lttng_ust_plibc_dlopen
) {
50 __lttng_ust_plibc_dlopen
= dlsym(RTLD_NEXT
, "dlopen");
51 if (!__lttng_ust_plibc_dlopen
) {
52 fprintf(stderr
, "%s\n", dlerror());
56 return __lttng_ust_plibc_dlopen(filename
, flags
);
61 void *_lttng_ust_dl_libc_dlmopen(Lmid_t nsid
, const char *filename
,
64 if (!__lttng_ust_plibc_dlmopen
) {
65 __lttng_ust_plibc_dlmopen
= dlsym(RTLD_NEXT
, "dlmopen");
66 if (!__lttng_ust_plibc_dlmopen
) {
67 fprintf(stderr
, "%s\n", dlerror());
71 return __lttng_ust_plibc_dlmopen(nsid
, filename
, flags
);
76 int _lttng_ust_dl_libc_dlclose(void *handle
)
78 if (!__lttng_ust_plibc_dlclose
) {
79 __lttng_ust_plibc_dlclose
= dlsym(RTLD_NEXT
, "dlclose");
80 if (!__lttng_ust_plibc_dlclose
) {
81 fprintf(stderr
, "%s\n", dlerror());
85 return __lttng_ust_plibc_dlclose(handle
);
89 void lttng_ust_dl_dlopen(void *so_base
, const char *so_name
,
92 char resolved_path
[PATH_MAX
];
93 struct lttng_ust_elf
*elf
;
95 uint8_t *build_id
= NULL
;
97 char *dbg_file
= NULL
;
99 int has_build_id
= 0, has_debug_link
= 0;
102 if (!realpath(so_name
, resolved_path
)) {
103 ERR("could not resolve path '%s'", so_name
);
107 elf
= lttng_ust_elf_create(resolved_path
);
109 ERR("could not access file %s", resolved_path
);
113 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
117 ret
= lttng_ust_elf_get_build_id(
118 elf
, &build_id
, &build_id_len
, &has_build_id
);
122 ret
= lttng_ust_elf_get_debug_link(
123 elf
, &dbg_file
, &crc
, &has_debug_link
);
128 tracepoint(lttng_ust_dl
, dlopen
,
129 ip
, so_base
, resolved_path
, flags
, memsz
,
130 has_build_id
, has_debug_link
);
133 tracepoint(lttng_ust_dl
, build_id
,
134 ip
, so_base
, build_id
, build_id_len
);
137 if (has_debug_link
) {
138 tracepoint(lttng_ust_dl
, debug_link
,
139 ip
, so_base
, dbg_file
, crc
);
145 lttng_ust_elf_destroy(elf
);
151 void lttng_ust_dl_dlmopen(void *so_base
, Lmid_t nsid
, const char *so_name
,
154 char resolved_path
[PATH_MAX
];
155 struct lttng_ust_elf
*elf
;
157 uint8_t *build_id
= NULL
;
159 char *dbg_file
= NULL
;
161 int has_build_id
= 0, has_debug_link
= 0;
164 if (!realpath(so_name
, resolved_path
)) {
165 ERR("could not resolve path '%s'", so_name
);
169 elf
= lttng_ust_elf_create(resolved_path
);
171 ERR("could not access file %s", resolved_path
);
175 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
179 ret
= lttng_ust_elf_get_build_id(
180 elf
, &build_id
, &build_id_len
, &has_build_id
);
184 ret
= lttng_ust_elf_get_debug_link(
185 elf
, &dbg_file
, &crc
, &has_debug_link
);
190 tracepoint(lttng_ust_dl
, dlmopen
,
191 ip
, so_base
, nsid
, resolved_path
, flags
, memsz
,
192 has_build_id
, has_debug_link
);
195 tracepoint(lttng_ust_dl
, build_id
,
196 ip
, so_base
, build_id
, build_id_len
);
199 if (has_debug_link
) {
200 tracepoint(lttng_ust_dl
, debug_link
,
201 ip
, so_base
, dbg_file
, crc
);
207 lttng_ust_elf_destroy(elf
);
212 void *dlopen(const char *filename
, int flags
)
216 handle
= _lttng_ust_dl_libc_dlopen(filename
, flags
);
217 if (__tracepoint_ptrs_registered
&& handle
) {
218 struct link_map
*p
= NULL
;
221 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
222 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
223 lttng_ust_dl_dlopen((void *) p
->l_addr
,
224 p
->l_name
, flags
, LTTNG_UST_CALLER_IP());
227 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
232 void *dlmopen(Lmid_t nsid
, const char *filename
, int flags
)
236 handle
= _lttng_ust_dl_libc_dlmopen(nsid
, filename
, flags
);
237 if (__tracepoint_ptrs_registered
&& handle
) {
238 struct link_map
*p
= NULL
;
241 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
242 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
243 lttng_ust_dl_dlmopen((void *) p
->l_addr
,
244 nsid
, p
->l_name
, flags
,
245 LTTNG_UST_CALLER_IP());
248 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
254 int dlclose(void *handle
)
258 if (__tracepoint_ptrs_registered
) {
259 struct link_map
*p
= NULL
;
261 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
262 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
263 tracepoint(lttng_ust_dl
, dlclose
,
264 LTTNG_UST_CALLER_IP(),
268 ret
= _lttng_ust_dl_libc_dlclose(handle
);
269 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());