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
26 #include <sys/types.h>
29 #include <lttng/ust-dlfcn.h>
30 #include <lttng/ust-elf.h>
31 #include <lttng/ust-events.h>
33 #include "usterr-signal-safe.h"
35 /* Include link.h last else it conflicts with ust-dlfcn. */
38 #define TRACEPOINT_DEFINE
41 static void *(*__lttng_ust_plibc_dlopen
)(const char *filename
, int flags
);
43 static void *(*__lttng_ust_plibc_dlmopen
)(Lmid_t nsid
, const char *filename
,
46 static int (*__lttng_ust_plibc_dlclose
)(void *handle
);
49 void *_lttng_ust_dl_libc_dlopen(const char *filename
, int flags
)
51 if (!__lttng_ust_plibc_dlopen
) {
52 __lttng_ust_plibc_dlopen
= dlsym(RTLD_NEXT
, "dlopen");
53 if (!__lttng_ust_plibc_dlopen
) {
54 fprintf(stderr
, "%s\n", dlerror());
58 return __lttng_ust_plibc_dlopen(filename
, flags
);
63 void *_lttng_ust_dl_libc_dlmopen(Lmid_t nsid
, const char *filename
,
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());
73 return __lttng_ust_plibc_dlmopen(nsid
, filename
, flags
);
78 int _lttng_ust_dl_libc_dlclose(void *handle
)
80 if (!__lttng_ust_plibc_dlclose
) {
81 __lttng_ust_plibc_dlclose
= dlsym(RTLD_NEXT
, "dlclose");
82 if (!__lttng_ust_plibc_dlclose
) {
83 fprintf(stderr
, "%s\n", dlerror());
87 return __lttng_ust_plibc_dlclose(handle
);
91 void lttng_ust_dl_dlopen(void *so_base
, const char *so_name
,
94 char resolved_path
[PATH_MAX
];
95 struct lttng_ust_elf
*elf
;
97 uint8_t *build_id
= NULL
;
99 char *dbg_file
= NULL
;
101 int has_build_id
= 0, has_debug_link
= 0;
104 if (!realpath(so_name
, resolved_path
)) {
105 ERR("could not resolve path '%s'", so_name
);
109 elf
= lttng_ust_elf_create(resolved_path
);
111 ERR("could not access file %s", resolved_path
);
115 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
119 ret
= lttng_ust_elf_get_build_id(
120 elf
, &build_id
, &build_id_len
, &has_build_id
);
124 ret
= lttng_ust_elf_get_debug_link(
125 elf
, &dbg_file
, &crc
, &has_debug_link
);
130 tracepoint(lttng_ust_dl
, dlopen
,
131 ip
, so_base
, resolved_path
, flags
, memsz
,
132 has_build_id
, has_debug_link
);
135 tracepoint(lttng_ust_dl
, build_id
,
136 ip
, so_base
, build_id
, build_id_len
);
139 if (has_debug_link
) {
140 tracepoint(lttng_ust_dl
, debug_link
,
141 ip
, so_base
, dbg_file
, crc
);
147 lttng_ust_elf_destroy(elf
);
153 void lttng_ust_dl_dlmopen(void *so_base
, Lmid_t nsid
, const char *so_name
,
156 char resolved_path
[PATH_MAX
];
157 struct lttng_ust_elf
*elf
;
159 uint8_t *build_id
= NULL
;
161 char *dbg_file
= NULL
;
163 int has_build_id
= 0, has_debug_link
= 0;
166 if (!realpath(so_name
, resolved_path
)) {
167 ERR("could not resolve path '%s'", so_name
);
171 elf
= lttng_ust_elf_create(resolved_path
);
173 ERR("could not access file %s", resolved_path
);
177 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
181 ret
= lttng_ust_elf_get_build_id(
182 elf
, &build_id
, &build_id_len
, &has_build_id
);
186 ret
= lttng_ust_elf_get_debug_link(
187 elf
, &dbg_file
, &crc
, &has_debug_link
);
192 tracepoint(lttng_ust_dl
, dlmopen
,
193 ip
, so_base
, nsid
, resolved_path
, flags
, memsz
,
194 has_build_id
, has_debug_link
);
197 tracepoint(lttng_ust_dl
, build_id
,
198 ip
, so_base
, build_id
, build_id_len
);
201 if (has_debug_link
) {
202 tracepoint(lttng_ust_dl
, debug_link
,
203 ip
, so_base
, dbg_file
, crc
);
209 lttng_ust_elf_destroy(elf
);
214 void *dlopen(const char *filename
, int flags
)
218 handle
= _lttng_ust_dl_libc_dlopen(filename
, flags
);
219 if (__tracepoint_ptrs_registered
&& handle
) {
220 struct link_map
*p
= NULL
;
223 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
224 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
225 lttng_ust_dl_dlopen((void *) p
->l_addr
,
226 p
->l_name
, flags
, LTTNG_UST_CALLER_IP());
229 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
234 void *dlmopen(Lmid_t nsid
, const char *filename
, int flags
)
238 handle
= _lttng_ust_dl_libc_dlmopen(nsid
, filename
, flags
);
239 if (__tracepoint_ptrs_registered
&& handle
) {
240 struct link_map
*p
= NULL
;
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
,
247 LTTNG_UST_CALLER_IP());
250 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
256 int dlclose(void *handle
)
260 if (__tracepoint_ptrs_registered
) {
261 struct link_map
*p
= NULL
;
263 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
264 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
265 tracepoint(lttng_ust_dl
, dlclose
,
266 LTTNG_UST_CALLER_IP(),
270 ret
= _lttng_ust_dl_libc_dlclose(handle
);
271 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());