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 flag
);
41 static int (*__lttng_ust_plibc_dlclose
)(void *handle
);
44 void *_lttng_ust_dl_libc_dlopen(const char *filename
, int flag
)
46 if (!__lttng_ust_plibc_dlopen
) {
47 __lttng_ust_plibc_dlopen
= dlsym(RTLD_NEXT
, "dlopen");
48 if (!__lttng_ust_plibc_dlopen
) {
49 fprintf(stderr
, "%s\n", dlerror());
53 return __lttng_ust_plibc_dlopen(filename
, flag
);
57 int _lttng_ust_dl_libc_dlclose(void *handle
)
59 if (!__lttng_ust_plibc_dlclose
) {
60 __lttng_ust_plibc_dlclose
= dlsym(RTLD_NEXT
, "dlclose");
61 if (!__lttng_ust_plibc_dlclose
) {
62 fprintf(stderr
, "%s\n", dlerror());
66 return __lttng_ust_plibc_dlclose(handle
);
70 void lttng_ust_dl_dlopen(void *so_base
, const char *so_name
, void *ip
)
72 char resolved_path
[PATH_MAX
];
73 struct lttng_ust_elf
*elf
;
75 uint8_t *build_id
= NULL
;
77 char *dbg_file
= NULL
;
79 int has_build_id
= 0, has_debug_link
= 0;
82 if (!realpath(so_name
, resolved_path
)) {
83 ERR("could not resolve path '%s'", so_name
);
87 elf
= lttng_ust_elf_create(resolved_path
);
89 ERR("could not acces file %s", resolved_path
);
93 ret
= lttng_ust_elf_get_memsz(elf
, &memsz
);
97 ret
= lttng_ust_elf_get_build_id(
98 elf
, &build_id
, &build_id_len
, &has_build_id
);
102 ret
= lttng_ust_elf_get_debug_link(
103 elf
, &dbg_file
, &crc
, &has_debug_link
);
108 tracepoint(lttng_ust_dl
, dlopen
,
109 ip
, so_base
, resolved_path
, memsz
,
110 has_build_id
, has_debug_link
);
113 tracepoint(lttng_ust_dl
, build_id
,
114 ip
, so_base
, build_id
, build_id_len
);
117 if (has_debug_link
) {
118 tracepoint(lttng_ust_dl
, debug_link
,
119 ip
, so_base
, dbg_file
, crc
);
125 lttng_ust_elf_destroy(elf
);
129 void *dlopen(const char *filename
, int flag
)
133 handle
= _lttng_ust_dl_libc_dlopen(filename
, flag
);
134 if (__tracepoint_ptrs_registered
&& handle
) {
135 struct link_map
*p
= NULL
;
138 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
139 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
140 lttng_ust_dl_dlopen((void *) p
->l_addr
,
142 LTTNG_UST_CALLER_IP());
145 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
149 int dlclose(void *handle
)
153 if (__tracepoint_ptrs_registered
) {
154 struct link_map
*p
= NULL
;
156 ret
= dlinfo(handle
, RTLD_DI_LINKMAP
, &p
);
157 if (ret
!= -1 && p
!= NULL
&& p
->l_addr
!= 0) {
158 tracepoint(lttng_ust_dl
, dlclose
,
159 LTTNG_UST_CALLER_IP(),
163 ret
= _lttng_ust_dl_libc_dlclose(handle
);
164 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());