2 * Copyright (C) 2013 Paul Woegerer <paul.woegerer@mentor.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; version 2.1 of
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <lttng/ust-compiler.h>
31 #include <lttng/ust.h>
33 static void *(*__lttng_ust_plibc_dlopen
)(const char *filename
, int flag
);
34 static int (*__lttng_ust_plibc_dlclose
)(void *handle
);
35 static void *__lttng_ust_baddr_handle
;
38 void *_lttng_ust_dl_libc_dlopen(const char *filename
, int flag
)
40 if (!__lttng_ust_plibc_dlopen
) {
41 __lttng_ust_plibc_dlopen
= dlsym(RTLD_NEXT
, "dlopen");
42 if (__lttng_ust_plibc_dlopen
== NULL
) {
43 fprintf(stderr
, "%s\n", dlerror());
47 return __lttng_ust_plibc_dlopen(filename
, flag
);
51 int _lttng_ust_dl_libc_dlclose(void *handle
)
53 if (!__lttng_ust_plibc_dlclose
) {
54 __lttng_ust_plibc_dlclose
= dlsym(RTLD_NEXT
, "dlclose");
55 if (__lttng_ust_plibc_dlclose
== NULL
) {
56 fprintf(stderr
, "%s\n", dlerror());
60 return __lttng_ust_plibc_dlclose(handle
);
64 void *lttng_ust_baddr_handle(void)
66 if (!__lttng_ust_baddr_handle
) {
67 __lttng_ust_baddr_handle
= _lttng_ust_dl_libc_dlopen(
68 "liblttng-ust-baddr.so.0", RTLD_NOW
| RTLD_GLOBAL
);
69 if (__lttng_ust_baddr_handle
== NULL
)
70 fprintf(stderr
, "%s\n", dlerror());
72 return __lttng_ust_baddr_handle
;
76 int lttng_ust_baddr_push(void *so_base
, const char *so_name
)
79 (*lttng_ust_baddr_push_fn
)(void *so_base
, const char *so_name
);
80 if (!lttng_ust_baddr_push_fn
) {
81 void *baddr_handle
= lttng_ust_baddr_handle();
83 lttng_ust_baddr_push_fn
= dlsym(baddr_handle
,
84 "lttng_ust_push_baddr");
85 if (lttng_ust_baddr_push_fn
== NULL
)
86 fprintf(stderr
, "%s\n", dlerror());
88 if (!lttng_ust_baddr_push_fn
)
91 return lttng_ust_baddr_push_fn(so_base
, so_name
);
95 int lttng_ust_baddr_pop(void *so_base
)
98 (*lttng_ust_baddr_pop_fn
)(void *so_base
);
99 if (!lttng_ust_baddr_pop_fn
) {
100 void *baddr_handle
= lttng_ust_baddr_handle();
102 lttng_ust_baddr_pop_fn
= dlsym(baddr_handle
,
103 "lttng_ust_pop_baddr");
104 if (lttng_ust_baddr_pop_fn
== NULL
)
105 fprintf(stderr
, "%s\n", dlerror());
107 if (!lttng_ust_baddr_pop_fn
)
110 return lttng_ust_baddr_pop_fn(so_base
);
113 void *dlopen(const char *filename
, int flag
)
115 void *handle
= _lttng_ust_dl_libc_dlopen(filename
, flag
);
117 struct link_map
*p
= NULL
;
118 if (dlinfo(handle
, RTLD_DI_LINKMAP
, &p
) != -1 && p
!= NULL
120 lttng_ust_baddr_push((void *) p
->l_addr
, p
->l_name
);
125 int dlclose(void *handle
)
128 struct link_map
*p
= NULL
;
129 if (dlinfo(handle
, RTLD_DI_LINKMAP
, &p
) != -1 && p
!= NULL
131 lttng_ust_baddr_pop((void *) p
->l_addr
);
133 return _lttng_ust_dl_libc_dlclose(handle
);
136 static void __attribute__((destructor
))
137 lttng_ust_baddr_handle_fini(void);
139 lttng_ust_baddr_handle_fini(void)
141 if (__lttng_ust_baddr_handle
) {
142 int ret
= _lttng_ust_dl_libc_dlclose(__lttng_ust_baddr_handle
);
144 fprintf(stderr
, "%s\n", dlerror());