2 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
3 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; version 2.1 of
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * 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>
31 #include "usterr-signal-safe.h"
33 /* Include link.h last else it conflicts with ust-dlfcn. */
36 #define TRACEPOINT_DEFINE
39 static void *(*__lttng_ust_plibc_dlopen
)(const char *filename
, int flag
);
40 static int (*__lttng_ust_plibc_dlclose
)(void *handle
);
43 void *_lttng_ust_dl_libc_dlopen(const char *filename
, int flag
)
45 if (!__lttng_ust_plibc_dlopen
) {
46 __lttng_ust_plibc_dlopen
= dlsym(RTLD_NEXT
, "dlopen");
47 if (!__lttng_ust_plibc_dlopen
) {
48 fprintf(stderr
, "%s\n", dlerror());
52 return __lttng_ust_plibc_dlopen(filename
, flag
);
56 int _lttng_ust_dl_libc_dlclose(void *handle
)
58 if (!__lttng_ust_plibc_dlclose
) {
59 __lttng_ust_plibc_dlclose
= dlsym(RTLD_NEXT
, "dlclose");
60 if (!__lttng_ust_plibc_dlclose
) {
61 fprintf(stderr
, "%s\n", dlerror());
65 return __lttng_ust_plibc_dlclose(handle
);
69 void lttng_ust_dl_dlopen(void *so_base
, const char *so_name
, void *ip
)
71 char resolved_path
[PATH_MAX
];
72 struct lttng_ust_elf
*elf
;
74 uint8_t *build_id
= NULL
;
76 char *dbg_file
= NULL
;
78 int has_build_id
= 0, has_debug_link
= 0;
81 if (!realpath(so_name
, resolved_path
)) {
82 ERR("could not resolve path '%s'", so_name
);
86 elf
= lttng_ust_elf_create(resolved_path
);
88 ERR("could not acces file %s", resolved_path
);
92 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
96 ret
= lttng_ust_elf_get_build_id(
97 elf
, &build_id
, &build_id_len
, &has_build_id
);
101 ret
= lttng_ust_elf_get_debug_link(
102 elf
, &dbg_file
, &crc
, &has_debug_link
);
107 tracepoint(lttng_ust_dl
, dlopen
,
108 ip
, so_base
, resolved_path
, memsz
,
109 has_build_id
, has_debug_link
);
112 tracepoint(lttng_ust_dl
, build_id
,
113 ip
, so_base
, build_id
, build_id_len
);
116 if (has_debug_link
) {
117 tracepoint(lttng_ust_dl
, debug_link
,
118 ip
, so_base
, dbg_file
, crc
);
124 lttng_ust_elf_destroy(elf
);
128 void *dlopen(const char *filename
, int flag
)
132 handle
= _lttng_ust_dl_libc_dlopen(filename
, flag
);
133 if (__tracepoint_ptrs_registered
&& handle
) {
134 struct link_map
*p
= NULL
;
137 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
138 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
139 lttng_ust_dl_dlopen((void *) p
->l_addr
, p
->l_name
,
140 LTTNG_UST_CALLER_IP());
147 int dlclose(void *handle
)
149 if (__tracepoint_ptrs_registered
) {
150 struct link_map
*p
= NULL
;
153 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
154 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
155 tracepoint(lttng_ust_dl
, dlclose
,
156 LTTNG_UST_CALLER_IP(),
161 return _lttng_ust_dl_libc_dlclose(handle
);