Add library load/unload tracking events
[lttng-ust.git] / liblttng-ust-dl / lttng-ust-dl.c
1 /*
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>
5 *
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
9 * the License.
10 *
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.
15 *
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
19 */
20
21 #define _GNU_SOURCE
22 #define _LGPL_SOURCE
23 #include <limits.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include <lttng/ust-dlfcn.h>
29 #include <lttng/ust-elf.h>
30 #include <lttng/ust-events.h>
31 #include <helper.h>
32 #include "usterr-signal-safe.h"
33
34 /* Include link.h last else it conflicts with ust-dlfcn. */
35 #include <link.h>
36
37 #define TRACEPOINT_DEFINE
38 #include "ust_dl.h"
39
40 static void *(*__lttng_ust_plibc_dlopen)(const char *filename, int flag);
41 static int (*__lttng_ust_plibc_dlclose)(void *handle);
42
43 static
44 void *_lttng_ust_dl_libc_dlopen(const char *filename, int flag)
45 {
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());
50 return NULL;
51 }
52 }
53 return __lttng_ust_plibc_dlopen(filename, flag);
54 }
55
56 static
57 int _lttng_ust_dl_libc_dlclose(void *handle)
58 {
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());
63 return -1;
64 }
65 }
66 return __lttng_ust_plibc_dlclose(handle);
67 }
68
69 static
70 void lttng_ust_dl_dlopen(void *so_base, const char *so_name, void *ip)
71 {
72 char resolved_path[PATH_MAX];
73 struct lttng_ust_elf *elf;
74 uint64_t memsz;
75 uint8_t *build_id = NULL;
76 size_t build_id_len;
77 char *dbg_file = NULL;
78 uint32_t crc;
79 int has_build_id = 0, has_debug_link = 0;
80 int ret;
81
82 if (!realpath(so_name, resolved_path)) {
83 ERR("could not resolve path '%s'", so_name);
84 return;
85 }
86
87 elf = lttng_ust_elf_create(resolved_path);
88 if (!elf) {
89 ERR("could not acces file %s", resolved_path);
90 return;
91 }
92
93 ret = lttng_ust_elf_get_memsz(elf, &memsz);
94 if (ret) {
95 goto end;
96 }
97 ret = lttng_ust_elf_get_build_id(
98 elf, &build_id, &build_id_len, &has_build_id);
99 if (ret) {
100 goto end;
101 }
102 ret = lttng_ust_elf_get_debug_link(
103 elf, &dbg_file, &crc, &has_debug_link);
104 if (ret) {
105 goto end;
106 }
107
108 tracepoint(lttng_ust_dl, dlopen,
109 ip, so_base, resolved_path, memsz,
110 has_build_id, has_debug_link);
111
112 if (has_build_id) {
113 tracepoint(lttng_ust_dl, build_id,
114 ip, so_base, build_id, build_id_len);
115 }
116
117 if (has_debug_link) {
118 tracepoint(lttng_ust_dl, debug_link,
119 ip, so_base, dbg_file, crc);
120 }
121
122 end:
123 free(dbg_file);
124 free(build_id);
125 lttng_ust_elf_destroy(elf);
126 return;
127 }
128
129 void *dlopen(const char *filename, int flag)
130 {
131 void *handle;
132
133 handle = _lttng_ust_dl_libc_dlopen(filename, flag);
134 if (__tracepoint_ptrs_registered && handle) {
135 struct link_map *p = NULL;
136 int ret;
137
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,
141 p->l_name,
142 LTTNG_UST_CALLER_IP());
143 }
144 }
145 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
146 return handle;
147 }
148
149 int dlclose(void *handle)
150 {
151 int ret;
152
153 if (__tracepoint_ptrs_registered) {
154 struct link_map *p = NULL;
155
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(),
160 (void *) p->l_addr);
161 }
162 }
163 ret = _lttng_ust_dl_libc_dlclose(handle);
164 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
165 return ret;
166 }
This page took 0.03221 seconds and 4 git commands to generate.