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
);
41 static void *(*__lttng_ust_plibc_dlmopen
)(Lmid_t nsid
, const char *filename
,
43 static int (*__lttng_ust_plibc_dlclose
)(void *handle
);
46 void *_lttng_ust_dl_libc_dlopen(const char *filename
, int flags
)
48 if (!__lttng_ust_plibc_dlopen
) {
49 __lttng_ust_plibc_dlopen
= dlsym(RTLD_NEXT
, "dlopen");
50 if (!__lttng_ust_plibc_dlopen
) {
51 fprintf(stderr
, "%s\n", dlerror());
55 return __lttng_ust_plibc_dlopen(filename
, flags
);
59 void *_lttng_ust_dl_libc_dlmopen(Lmid_t nsid
, const char *filename
,
62 if (!__lttng_ust_plibc_dlmopen
) {
63 __lttng_ust_plibc_dlmopen
= dlsym(RTLD_NEXT
, "dlmopen");
64 if (!__lttng_ust_plibc_dlmopen
) {
65 fprintf(stderr
, "%s\n", dlerror());
69 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 acces 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
);
147 void lttng_ust_dl_dlmopen(void *so_base
, Lmid_t nsid
, const char *so_name
,
150 char resolved_path
[PATH_MAX
];
151 struct lttng_ust_elf
*elf
;
153 uint8_t *build_id
= NULL
;
155 char *dbg_file
= NULL
;
157 int has_build_id
= 0, has_debug_link
= 0;
160 if (!realpath(so_name
, resolved_path
)) {
161 ERR("could not resolve path '%s'", so_name
);
165 elf
= lttng_ust_elf_create(resolved_path
);
167 ERR("could not acces file %s", resolved_path
);
171 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
175 ret
= lttng_ust_elf_get_build_id(
176 elf
, &build_id
, &build_id_len
, &has_build_id
);
180 ret
= lttng_ust_elf_get_debug_link(
181 elf
, &dbg_file
, &crc
, &has_debug_link
);
186 tracepoint(lttng_ust_dl
, dlmopen
,
187 ip
, so_base
, nsid
, resolved_path
, flags
, memsz
,
188 has_build_id
, has_debug_link
);
191 tracepoint(lttng_ust_dl
, build_id
,
192 ip
, so_base
, build_id
, build_id_len
);
195 if (has_debug_link
) {
196 tracepoint(lttng_ust_dl
, debug_link
,
197 ip
, so_base
, dbg_file
, crc
);
203 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());
226 void *dlmopen(Lmid_t nsid
, const char *filename
, int flags
)
230 handle
= _lttng_ust_dl_libc_dlmopen(nsid
, filename
, flags
);
231 if (__tracepoint_ptrs_registered
&& handle
) {
232 struct link_map
*p
= NULL
;
235 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
236 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
237 lttng_ust_dl_dlmopen((void *) p
->l_addr
,
238 nsid
, p
->l_name
, flags
,
239 LTTNG_UST_CALLER_IP());
242 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
247 int dlclose(void *handle
)
251 if (__tracepoint_ptrs_registered
) {
252 struct link_map
*p
= NULL
;
254 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
255 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
256 tracepoint(lttng_ust_dl
, dlclose
,
257 LTTNG_UST_CALLER_IP(),
261 ret
= _lttng_ust_dl_libc_dlclose(handle
);
262 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());