Fix: all lttng-ust source files should be tagged _LGPL_SOURCE
[lttng-ust.git] / liblttng-ust / lttng-ust-statedump.c
1 /*
2 * Copyright (C) 2013 Paul Woegerer <paul_woegerer@mentor.com>
3 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
4 *
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; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
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.
14 *
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
18 */
19
20 #define _GNU_SOURCE
21 #define _LGPL_SOURCE
22 #include <link.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 #include <lttng/ust-elf.h>
31 #include "lttng-tracer-core.h"
32 #include "lttng-ust-statedump.h"
33
34 #define TRACEPOINT_DEFINE
35 #define TRACEPOINT_CREATE_PROBES
36 #define TP_SESSION_CHECK
37 #include "lttng-ust-statedump-provider.h"
38
39 struct dl_iterate_data {
40 void *owner;
41 int exec_found;
42 };
43
44 struct bin_info_data {
45 void *owner;
46 void *base_addr_ptr;
47 const char *resolved_path;
48 char *dbg_file;
49 uint8_t *build_id;
50 uint64_t memsz;
51 size_t build_id_len;
52 int vdso;
53 uint32_t crc;
54 uint8_t is_pic;
55 uint8_t has_build_id;
56 uint8_t has_debug_link;
57 };
58
59 typedef void (*tracepoint_cb)(struct lttng_session *session, void *priv);
60
61 /*
62 * Trace statedump event into all sessions owned by the caller thread
63 * for which statedump is pending.
64 */
65 static
66 int trace_statedump_event(tracepoint_cb tp_cb, void *owner, void *priv)
67 {
68 struct cds_list_head *sessionsp;
69 struct lttng_session *session;
70
71 sessionsp = _lttng_get_sessions();
72 cds_list_for_each_entry(session, sessionsp, node) {
73 if (session->owner != owner)
74 continue;
75 if (!session->statedump_pending)
76 continue;
77 tp_cb(session, priv);
78 }
79 return 0;
80 }
81
82 static
83 void trace_bin_info_cb(struct lttng_session *session, void *priv)
84 {
85 struct bin_info_data *bin_data = (struct bin_info_data *) priv;
86
87 tracepoint(lttng_ust_statedump, bin_info,
88 session, bin_data->base_addr_ptr,
89 bin_data->resolved_path, bin_data->memsz,
90 bin_data->is_pic, bin_data->has_build_id,
91 bin_data->has_debug_link);
92 }
93
94 static
95 void trace_build_id_cb(struct lttng_session *session, void *priv)
96 {
97 struct bin_info_data *bin_data = (struct bin_info_data *) priv;
98
99 tracepoint(lttng_ust_statedump, build_id,
100 session, bin_data->base_addr_ptr,
101 bin_data->build_id, bin_data->build_id_len);
102 }
103
104 static
105 void trace_debug_link_cb(struct lttng_session *session, void *priv)
106 {
107 struct bin_info_data *bin_data = (struct bin_info_data *) priv;
108
109 tracepoint(lttng_ust_statedump, debug_link,
110 session, bin_data->base_addr_ptr,
111 bin_data->dbg_file, bin_data->crc);
112 }
113
114 static
115 void trace_start_cb(struct lttng_session *session, void *priv)
116 {
117 tracepoint(lttng_ust_statedump, start, session);
118 }
119
120 static
121 void trace_end_cb(struct lttng_session *session, void *priv)
122 {
123 tracepoint(lttng_ust_statedump, end, session);
124 }
125
126 static
127 int get_elf_info(struct bin_info_data *bin_data)
128 {
129 struct lttng_ust_elf *elf;
130 int ret = 0, found;
131
132 elf = lttng_ust_elf_create(bin_data->resolved_path);
133 if (!elf) {
134 ret = -1;
135 goto end;
136 }
137
138 ret = lttng_ust_elf_get_memsz(elf, &bin_data->memsz);
139 if (ret) {
140 goto end;
141 }
142
143 found = 0;
144 ret = lttng_ust_elf_get_build_id(elf, &bin_data->build_id,
145 &bin_data->build_id_len,
146 &found);
147 if (ret) {
148 goto end;
149 }
150 bin_data->has_build_id = !!found;
151 found = 0;
152 ret = lttng_ust_elf_get_debug_link(elf, &bin_data->dbg_file,
153 &bin_data->crc,
154 &found);
155 if (ret) {
156 goto end;
157 }
158 bin_data->has_debug_link = !!found;
159
160 bin_data->is_pic = lttng_ust_elf_is_pic(elf);
161
162 end:
163 lttng_ust_elf_destroy(elf);
164 return ret;
165 }
166
167 static
168 int trace_baddr(struct bin_info_data *bin_data)
169 {
170 int ret = 0;
171
172 if (!bin_data->vdso) {
173 ret = get_elf_info(bin_data);
174 if (ret) {
175 goto end;
176 }
177 } else {
178 bin_data->memsz = 0;
179 bin_data->has_build_id = 0;
180 bin_data->has_debug_link = 0;
181 }
182
183 ret = trace_statedump_event(trace_bin_info_cb, bin_data->owner,
184 bin_data);
185 if (ret) {
186 goto end;
187 }
188
189 if (bin_data->has_build_id) {
190 ret = trace_statedump_event(
191 trace_build_id_cb, bin_data->owner, bin_data);
192 free(bin_data->build_id);
193 if (ret) {
194 goto end;
195 }
196 }
197
198 if (bin_data->has_debug_link) {
199 ret = trace_statedump_event(
200 trace_debug_link_cb, bin_data->owner, bin_data);
201 free(bin_data->dbg_file);
202 if (ret) {
203 goto end;
204 }
205 }
206
207 end:
208 return ret;
209 }
210
211 static
212 int trace_statedump_start(void *owner)
213 {
214 return trace_statedump_event(trace_start_cb, owner, NULL);
215 }
216
217 static
218 int trace_statedump_end(void *owner)
219 {
220 return trace_statedump_event(trace_end_cb, owner, NULL);
221 }
222
223 static
224 int extract_bin_info_events(struct dl_phdr_info *info, size_t size, void *_data)
225 {
226 int j, ret = 0;
227 struct dl_iterate_data *data = _data;
228
229 /*
230 * UST lock nests within dynamic loader lock.
231 *
232 * Hold this lock across handling of the entire module to
233 * protect memory allocation at early process start, due to
234 * interactions with libc-wrapper lttng malloc instrumentation.
235 */
236 if (ust_lock()) {
237 goto end;
238 }
239
240 for (j = 0; j < info->dlpi_phnum; j++) {
241 struct bin_info_data bin_data;
242 char resolved_path[PATH_MAX];
243 void *base_addr_ptr;
244
245 if (info->dlpi_phdr[j].p_type != PT_LOAD)
246 continue;
247
248 /* Calculate virtual memory address of the loadable segment */
249 base_addr_ptr = (void *) info->dlpi_addr +
250 info->dlpi_phdr[j].p_vaddr;
251
252 if ((info->dlpi_name == NULL || info->dlpi_name[0] == 0)) {
253 /*
254 * Only the first phdr without a dlpi_name
255 * encountered is considered as the program
256 * executable. The rest are vdsos.
257 */
258 if (!data->exec_found) {
259 ssize_t path_len;
260 data->exec_found = 1;
261
262 /*
263 * Use /proc/self/exe to resolve the
264 * executable's full path.
265 */
266 path_len = readlink("/proc/self/exe",
267 resolved_path,
268 PATH_MAX - 1);
269 if (path_len <= 0)
270 break;
271
272 resolved_path[path_len] = '\0';
273 bin_data.vdso = 0;
274 } else {
275 snprintf(resolved_path, PATH_MAX - 1, "[vdso]");
276 bin_data.vdso = 1;
277 }
278 } else {
279 /*
280 * For regular dl_phdr_info entries check if
281 * the path to the binary really exists. If not,
282 * treat as vdso and use dlpi_name as 'path'.
283 */
284 if (!realpath(info->dlpi_name, resolved_path)) {
285 snprintf(resolved_path, PATH_MAX - 1, "[%s]",
286 info->dlpi_name);
287 bin_data.vdso = 1;
288 } else {
289 bin_data.vdso = 0;
290 }
291 }
292
293 bin_data.owner = data->owner;
294 bin_data.base_addr_ptr = base_addr_ptr;
295 bin_data.resolved_path = resolved_path;
296 ret = trace_baddr(&bin_data);
297 break;
298 }
299 end:
300 ust_unlock();
301 return ret;
302 }
303
304 /*
305 * Generate a statedump of base addresses of all shared objects loaded
306 * by the traced application, as well as for the application's
307 * executable itself.
308 */
309 static
310 int do_baddr_statedump(void *owner)
311 {
312 struct dl_iterate_data data;
313
314 if (getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
315 return 0;
316
317 data.owner = owner;
318 data.exec_found = 0;
319 /*
320 * Iterate through the list of currently loaded shared objects and
321 * generate events for loadable segments using
322 * extract_bin_info_events.
323 */
324 dl_iterate_phdr(extract_bin_info_events, &data);
325
326 return 0;
327 }
328
329 /*
330 * Generate a statedump of a given traced application. A statedump is
331 * delimited by start and end events. For a given (process, session)
332 * pair, begin/end events are serialized and will match. However, in a
333 * session, statedumps from different processes may be
334 * interleaved. The vpid context should be used to identify which
335 * events belong to which process.
336 */
337 int do_lttng_ust_statedump(void *owner)
338 {
339 trace_statedump_start(owner);
340 do_baddr_statedump(owner);
341 trace_statedump_end(owner);
342
343 return 0;
344 }
345
346 void lttng_ust_statedump_init(void)
347 {
348 __tracepoints__init();
349 __tracepoints__ptrs_init();
350 __lttng_events_init__lttng_ust_statedump();
351 }
352
353 void lttng_ust_statedump_destroy(void)
354 {
355 __lttng_events_exit__lttng_ust_statedump();
356 __tracepoints__ptrs_destroy();
357 __tracepoints__destroy();
358 }
This page took 0.043803 seconds and 5 git commands to generate.