2 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
3 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
4 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
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.
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
25 #include <sys/types.h>
28 #include <lttng/ust-dlfcn.h>
29 #include <lttng/ust-elf.h>
30 #include <lttng/ust-events.h>
32 #include "usterr-signal-safe.h"
34 /* Include link.h last else it conflicts with ust-dlfcn. */
37 #define TRACEPOINT_DEFINE
40 static void *(*__lttng_ust_plibc_dlopen
)(const char *filename
, int flags
);
42 static void *(*__lttng_ust_plibc_dlmopen
)(Lmid_t nsid
, const char *filename
,
45 static int (*__lttng_ust_plibc_dlclose
)(void *handle
);
48 void *_lttng_ust_dl_libc_dlopen(const char *filename
, int flags
)
50 if (!__lttng_ust_plibc_dlopen
) {
51 __lttng_ust_plibc_dlopen
= dlsym(RTLD_NEXT
, "dlopen");
52 if (!__lttng_ust_plibc_dlopen
) {
53 fprintf(stderr
, "%s\n", dlerror());
57 return __lttng_ust_plibc_dlopen(filename
, flags
);
62 void *_lttng_ust_dl_libc_dlmopen(Lmid_t nsid
, const char *filename
,
65 if (!__lttng_ust_plibc_dlmopen
) {
66 __lttng_ust_plibc_dlmopen
= dlsym(RTLD_NEXT
, "dlmopen");
67 if (!__lttng_ust_plibc_dlmopen
) {
68 fprintf(stderr
, "%s\n", dlerror());
72 return __lttng_ust_plibc_dlmopen(nsid
, filename
, flags
);
77 int _lttng_ust_dl_libc_dlclose(void *handle
)
79 if (!__lttng_ust_plibc_dlclose
) {
80 __lttng_ust_plibc_dlclose
= dlsym(RTLD_NEXT
, "dlclose");
81 if (!__lttng_ust_plibc_dlclose
) {
82 fprintf(stderr
, "%s\n", dlerror());
86 return __lttng_ust_plibc_dlclose(handle
);
90 void lttng_ust_dl_dlopen(void *so_base
, const char *so_name
,
93 char resolved_path
[PATH_MAX
];
94 struct lttng_ust_elf
*elf
;
96 uint8_t *build_id
= NULL
;
98 char *dbg_file
= NULL
;
100 int has_build_id
= 0, has_debug_link
= 0;
103 if (!realpath(so_name
, resolved_path
)) {
104 ERR("could not resolve path '%s'", so_name
);
108 elf
= lttng_ust_elf_create(resolved_path
);
110 ERR("could not acces file %s", resolved_path
);
114 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
118 ret
= lttng_ust_elf_get_build_id(
119 elf
, &build_id
, &build_id_len
, &has_build_id
);
123 ret
= lttng_ust_elf_get_debug_link(
124 elf
, &dbg_file
, &crc
, &has_debug_link
);
129 tracepoint(lttng_ust_dl
, dlopen
,
130 ip
, so_base
, resolved_path
, flags
, memsz
,
131 has_build_id
, has_debug_link
);
134 tracepoint(lttng_ust_dl
, build_id
,
135 ip
, so_base
, build_id
, build_id_len
);
138 if (has_debug_link
) {
139 tracepoint(lttng_ust_dl
, debug_link
,
140 ip
, so_base
, dbg_file
, crc
);
146 lttng_ust_elf_destroy(elf
);
152 void lttng_ust_dl_dlmopen(void *so_base
, Lmid_t nsid
, const char *so_name
,
155 char resolved_path
[PATH_MAX
];
156 struct lttng_ust_elf
*elf
;
158 uint8_t *build_id
= NULL
;
160 char *dbg_file
= NULL
;
162 int has_build_id
= 0, has_debug_link
= 0;
165 if (!realpath(so_name
, resolved_path
)) {
166 ERR("could not resolve path '%s'", so_name
);
170 elf
= lttng_ust_elf_create(resolved_path
);
172 ERR("could not acces file %s", resolved_path
);
176 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
180 ret
= lttng_ust_elf_get_build_id(
181 elf
, &build_id
, &build_id_len
, &has_build_id
);
185 ret
= lttng_ust_elf_get_debug_link(
186 elf
, &dbg_file
, &crc
, &has_debug_link
);
191 tracepoint(lttng_ust_dl
, dlmopen
,
192 ip
, so_base
, nsid
, resolved_path
, flags
, memsz
,
193 has_build_id
, has_debug_link
);
196 tracepoint(lttng_ust_dl
, build_id
,
197 ip
, so_base
, build_id
, build_id_len
);
200 if (has_debug_link
) {
201 tracepoint(lttng_ust_dl
, debug_link
,
202 ip
, so_base
, dbg_file
, crc
);
208 lttng_ust_elf_destroy(elf
);
213 void *dlopen(const char *filename
, int flags
)
217 handle
= _lttng_ust_dl_libc_dlopen(filename
, flags
);
218 if (__tracepoint_ptrs_registered
&& handle
) {
219 struct link_map
*p
= NULL
;
222 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
223 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
224 lttng_ust_dl_dlopen((void *) p
->l_addr
,
225 p
->l_name
, flags
, LTTNG_UST_CALLER_IP());
228 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
233 void *dlmopen(Lmid_t nsid
, const char *filename
, int flags
)
237 handle
= _lttng_ust_dl_libc_dlmopen(nsid
, filename
, flags
);
238 if (__tracepoint_ptrs_registered
&& handle
) {
239 struct link_map
*p
= NULL
;
242 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
243 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
244 lttng_ust_dl_dlmopen((void *) p
->l_addr
,
245 nsid
, p
->l_name
, flags
,
246 LTTNG_UST_CALLER_IP());
249 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
255 int dlclose(void *handle
)
259 if (__tracepoint_ptrs_registered
) {
260 struct link_map
*p
= NULL
;
262 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
263 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
264 tracepoint(lttng_ust_dl
, dlclose
,
265 LTTNG_UST_CALLER_IP(),
269 ret
= _lttng_ust_dl_libc_dlclose(handle
);
270 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());