6b0d0567f69ce5c82bf783f01cdf3e0bde9a19c8
[lttng-ust.git] / src / lib / lttng-ust / lttng-ust-statedump.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 *
4 * Copyright (C) 2013 Paul Woegerer <paul_woegerer@mentor.com>
5 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
6 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 */
8
9 #define _LGPL_SOURCE
10 #include <link.h>
11 #include <limits.h>
12 #include <stdio.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <stdbool.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18
19 #include "common/elf.h"
20 #include "common/macros.h"
21 #include "lttng-tracer-core.h"
22 #include "lttng-ust-statedump.h"
23 #include "common/jhash.h"
24 #include "common/getenv.h"
25 #include "lib/lttng-ust/events.h"
26
27 #define LTTNG_UST_TRACEPOINT_HIDDEN_DEFINITION
28 #define LTTNG_UST_TRACEPOINT_PROVIDER_HIDDEN_DEFINITION
29
30 #define LTTNG_UST_TRACEPOINT_DEFINE
31 #include "ust_lib.h" /* Only define. */
32
33 #define LTTNG_UST_TRACEPOINT_CREATE_PROBES
34 #define LTTNG_UST_TP_SESSION_CHECK
35 #include "lttng-ust-statedump-provider.h" /* Define and create probes. */
36
37 struct dl_iterate_data {
38 int exec_found;
39 bool first;
40 bool cancel;
41 };
42
43 struct bin_info_data {
44 void *base_addr_ptr;
45 char resolved_path[PATH_MAX];
46 char *dbg_file;
47 uint8_t *build_id;
48 uint64_t memsz;
49 size_t build_id_len;
50 int vdso;
51 uint32_t crc;
52 uint8_t is_pic;
53 uint8_t has_build_id;
54 uint8_t has_debug_link;
55 };
56
57 struct lttng_ust_dl_node {
58 struct bin_info_data bin_data;
59 struct cds_hlist_node node;
60 bool traced;
61 bool marked;
62 };
63
64 #define UST_DL_STATE_HASH_BITS 8
65 #define UST_DL_STATE_TABLE_SIZE (1 << UST_DL_STATE_HASH_BITS)
66 static struct cds_hlist_head dl_state_table[UST_DL_STATE_TABLE_SIZE];
67
68 typedef void (*tracepoint_cb)(struct lttng_ust_session *session, void *priv);
69
70 static
71 struct lttng_ust_dl_node *alloc_dl_node(const struct bin_info_data *bin_data)
72 {
73 struct lttng_ust_dl_node *e;
74
75 e = zmalloc(sizeof(struct lttng_ust_dl_node));
76 if (!e)
77 return NULL;
78 if (bin_data->dbg_file) {
79 e->bin_data.dbg_file = strdup(bin_data->dbg_file);
80 if (!e->bin_data.dbg_file)
81 goto error;
82 }
83 if (bin_data->build_id) {
84 e->bin_data.build_id = zmalloc(bin_data->build_id_len);
85 if (!e->bin_data.build_id)
86 goto error;
87 memcpy(e->bin_data.build_id, bin_data->build_id,
88 bin_data->build_id_len);
89 }
90 e->bin_data.base_addr_ptr = bin_data->base_addr_ptr;
91 memcpy(e->bin_data.resolved_path, bin_data->resolved_path, PATH_MAX);
92 e->bin_data.memsz = bin_data->memsz;
93 e->bin_data.build_id_len = bin_data->build_id_len;
94 e->bin_data.vdso = bin_data->vdso;
95 e->bin_data.crc = bin_data->crc;
96 e->bin_data.is_pic = bin_data->is_pic;
97 e->bin_data.has_build_id = bin_data->has_build_id;
98 e->bin_data.has_debug_link = bin_data->has_debug_link;
99 return e;
100
101 error:
102 free(e->bin_data.build_id);
103 free(e->bin_data.dbg_file);
104 free(e);
105 return NULL;
106 }
107
108 static
109 void free_dl_node(struct lttng_ust_dl_node *e)
110 {
111 free(e->bin_data.build_id);
112 free(e->bin_data.dbg_file);
113 free(e);
114 }
115
116 /* Return 0 if same, nonzero if not. */
117 static
118 int compare_bin_data(const struct bin_info_data *a,
119 const struct bin_info_data *b)
120 {
121 if (a->base_addr_ptr != b->base_addr_ptr)
122 return -1;
123 if (strcmp(a->resolved_path, b->resolved_path) != 0)
124 return -1;
125 if (a->dbg_file && !b->dbg_file)
126 return -1;
127 if (!a->dbg_file && b->dbg_file)
128 return -1;
129 if (a->dbg_file && strcmp(a->dbg_file, b->dbg_file) != 0)
130 return -1;
131 if (a->build_id && !b->build_id)
132 return -1;
133 if (!a->build_id && b->build_id)
134 return -1;
135 if (a->build_id_len != b->build_id_len)
136 return -1;
137 if (a->build_id &&
138 memcmp(a->build_id, b->build_id, a->build_id_len) != 0)
139 return -1;
140 if (a->memsz != b->memsz)
141 return -1;
142 if (a->vdso != b->vdso)
143 return -1;
144 if (a->crc != b->crc)
145 return -1;
146 if (a->is_pic != b->is_pic)
147 return -1;
148 if (a->has_build_id != b->has_build_id)
149 return -1;
150 if (a->has_debug_link != b->has_debug_link)
151 return -1;
152 return 0;
153 }
154
155 static
156 struct lttng_ust_dl_node *find_or_create_dl_node(struct bin_info_data *bin_data)
157 {
158 struct cds_hlist_head *head;
159 struct lttng_ust_dl_node *e;
160 unsigned int hash;
161 bool found = false;
162
163 hash = jhash(&bin_data->base_addr_ptr,
164 sizeof(bin_data->base_addr_ptr), 0);
165 head = &dl_state_table[hash & (UST_DL_STATE_TABLE_SIZE - 1)];
166 cds_hlist_for_each_entry_2(e, head, node) {
167 if (compare_bin_data(&e->bin_data, bin_data) != 0)
168 continue;
169 found = true;
170 break;
171 }
172 if (!found) {
173 /* Create */
174 e = alloc_dl_node(bin_data);
175 if (!e)
176 return NULL;
177 cds_hlist_add_head(&e->node, head);
178 }
179 return e;
180 }
181
182 static
183 void remove_dl_node(struct lttng_ust_dl_node *e)
184 {
185 cds_hlist_del(&e->node);
186 }
187
188 /*
189 * Trace statedump event into all sessions owned by the caller thread
190 * for which statedump is pending.
191 */
192 static
193 void trace_statedump_event(tracepoint_cb tp_cb, void *owner, void *priv)
194 {
195 struct cds_list_head *sessionsp;
196 struct lttng_ust_session_private *session_priv;
197
198 sessionsp = lttng_get_sessions();
199 cds_list_for_each_entry(session_priv, sessionsp, node) {
200 if (session_priv->owner != owner)
201 continue;
202 if (!session_priv->statedump_pending)
203 continue;
204 tp_cb(session_priv->pub, priv);
205 }
206 }
207
208 static
209 void trace_bin_info_cb(struct lttng_ust_session *session, void *priv)
210 {
211 struct bin_info_data *bin_data = (struct bin_info_data *) priv;
212
213 lttng_ust_tracepoint(lttng_ust_statedump, bin_info,
214 session, bin_data->base_addr_ptr,
215 bin_data->resolved_path, bin_data->memsz,
216 bin_data->is_pic, bin_data->has_build_id,
217 bin_data->has_debug_link);
218 }
219
220 static
221 void trace_build_id_cb(struct lttng_ust_session *session, void *priv)
222 {
223 struct bin_info_data *bin_data = (struct bin_info_data *) priv;
224
225 lttng_ust_tracepoint(lttng_ust_statedump, build_id,
226 session, bin_data->base_addr_ptr,
227 bin_data->build_id, bin_data->build_id_len);
228 }
229
230 static
231 void trace_debug_link_cb(struct lttng_ust_session *session, void *priv)
232 {
233 struct bin_info_data *bin_data = (struct bin_info_data *) priv;
234
235 lttng_ust_tracepoint(lttng_ust_statedump, debug_link,
236 session, bin_data->base_addr_ptr,
237 bin_data->dbg_file, bin_data->crc);
238 }
239
240 static
241 void procname_cb(struct lttng_ust_session *session, void *priv)
242 {
243 char *procname = (char *) priv;
244 lttng_ust_tracepoint(lttng_ust_statedump, procname, session, procname);
245 }
246
247 static
248 void trace_start_cb(struct lttng_ust_session *session, void *priv __attribute__((unused)))
249 {
250 lttng_ust_tracepoint(lttng_ust_statedump, start, session);
251 }
252
253 static
254 void trace_end_cb(struct lttng_ust_session *session, void *priv __attribute__((unused)))
255 {
256 lttng_ust_tracepoint(lttng_ust_statedump, end, session);
257 }
258
259 static
260 int get_elf_info(struct bin_info_data *bin_data)
261 {
262 struct lttng_ust_elf *elf;
263 int ret = 0, found;
264
265 elf = lttng_ust_elf_create(bin_data->resolved_path);
266 if (!elf) {
267 ret = -1;
268 goto end;
269 }
270
271 ret = lttng_ust_elf_get_memsz(elf, &bin_data->memsz);
272 if (ret) {
273 goto end;
274 }
275
276 found = 0;
277 ret = lttng_ust_elf_get_build_id(elf, &bin_data->build_id,
278 &bin_data->build_id_len,
279 &found);
280 if (ret) {
281 goto end;
282 }
283 bin_data->has_build_id = !!found;
284 found = 0;
285 ret = lttng_ust_elf_get_debug_link(elf, &bin_data->dbg_file,
286 &bin_data->crc,
287 &found);
288 if (ret) {
289 goto end;
290 }
291 bin_data->has_debug_link = !!found;
292
293 bin_data->is_pic = lttng_ust_elf_is_pic(elf);
294
295 end:
296 lttng_ust_elf_destroy(elf);
297 return ret;
298 }
299
300 static
301 void trace_baddr(struct bin_info_data *bin_data, void *owner)
302 {
303 trace_statedump_event(trace_bin_info_cb, owner, bin_data);
304
305 if (bin_data->has_build_id)
306 trace_statedump_event(trace_build_id_cb, owner, bin_data);
307
308 if (bin_data->has_debug_link)
309 trace_statedump_event(trace_debug_link_cb, owner, bin_data);
310 }
311
312 static
313 int extract_baddr(struct bin_info_data *bin_data)
314 {
315 int ret = 0;
316 struct lttng_ust_dl_node *e;
317
318 if (!bin_data->vdso) {
319 ret = get_elf_info(bin_data);
320 if (ret) {
321 goto end;
322 }
323 } else {
324 bin_data->memsz = 0;
325 bin_data->has_build_id = 0;
326 bin_data->has_debug_link = 0;
327 }
328
329 e = find_or_create_dl_node(bin_data);
330 if (!e) {
331 ret = -1;
332 goto end;
333 }
334 e->marked = true;
335 end:
336 free(bin_data->build_id);
337 bin_data->build_id = NULL;
338 free(bin_data->dbg_file);
339 bin_data->dbg_file = NULL;
340 return ret;
341 }
342
343 static
344 void trace_statedump_start(void *owner)
345 {
346 trace_statedump_event(trace_start_cb, owner, NULL);
347 }
348
349 static
350 void trace_statedump_end(void *owner)
351 {
352 trace_statedump_event(trace_end_cb, owner, NULL);
353 }
354
355 static
356 void iter_begin(struct dl_iterate_data *data)
357 {
358 unsigned int i;
359
360 /*
361 * UST lock nests within dynamic loader lock.
362 *
363 * Hold this lock across handling of the module listing to
364 * protect memory allocation at early process start, due to
365 * interactions with libc-wrapper lttng malloc instrumentation.
366 */
367 if (ust_lock()) {
368 data->cancel = true;
369 return;
370 }
371
372 /* Ensure all entries are unmarked. */
373 for (i = 0; i < UST_DL_STATE_TABLE_SIZE; i++) {
374 struct cds_hlist_head *head;
375 struct lttng_ust_dl_node *e;
376
377 head = &dl_state_table[i];
378 cds_hlist_for_each_entry_2(e, head, node)
379 assert(!e->marked);
380 }
381 }
382
383 static
384 void trace_lib_load(const struct bin_info_data *bin_data, void *ip)
385 {
386 lttng_ust_tracepoint(lttng_ust_lib, load,
387 ip, bin_data->base_addr_ptr, bin_data->resolved_path,
388 bin_data->memsz, bin_data->has_build_id,
389 bin_data->has_debug_link);
390
391 if (bin_data->has_build_id) {
392 lttng_ust_tracepoint(lttng_ust_lib, build_id,
393 ip, bin_data->base_addr_ptr, bin_data->build_id,
394 bin_data->build_id_len);
395 }
396
397 if (bin_data->has_debug_link) {
398 lttng_ust_tracepoint(lttng_ust_lib, debug_link,
399 ip, bin_data->base_addr_ptr, bin_data->dbg_file,
400 bin_data->crc);
401 }
402 }
403
404 static
405 void trace_lib_unload(const struct bin_info_data *bin_data, void *ip)
406 {
407 lttng_ust_tracepoint(lttng_ust_lib, unload, ip, bin_data->base_addr_ptr);
408 }
409
410 static
411 void iter_end(struct dl_iterate_data *data, void *ip)
412 {
413 unsigned int i;
414
415 if (data->cancel)
416 goto end;
417 /*
418 * Iterate on hash table.
419 * For each marked, traced, do nothing.
420 * For each marked, not traced, trace lib open event. traced = true.
421 * For each unmarked, traced, trace lib close event. remove node.
422 * For each unmarked, not traced, remove node.
423 */
424 for (i = 0; i < UST_DL_STATE_TABLE_SIZE; i++) {
425 struct cds_hlist_head *head;
426 struct lttng_ust_dl_node *e, *tmp;
427
428 head = &dl_state_table[i];
429 cds_hlist_for_each_entry_safe_2(e, tmp, head, node) {
430 if (e->marked) {
431 if (!e->traced) {
432 trace_lib_load(&e->bin_data, ip);
433 e->traced = true;
434 }
435 e->marked = false;
436 } else {
437 if (e->traced)
438 trace_lib_unload(&e->bin_data, ip);
439 remove_dl_node(e);
440 free_dl_node(e);
441 }
442 }
443 }
444 end:
445 ust_unlock();
446 }
447
448 static
449 int extract_bin_info_events(struct dl_phdr_info *info, size_t size __attribute__((unused)), void *_data)
450 {
451 int j, ret = 0;
452 struct dl_iterate_data *data = _data;
453
454 if (data->first) {
455 iter_begin(data);
456 data->first = false;
457 }
458
459 if (data->cancel)
460 goto end;
461
462 for (j = 0; j < info->dlpi_phnum; j++) {
463 struct bin_info_data bin_data;
464
465 if (info->dlpi_phdr[j].p_type != PT_LOAD)
466 continue;
467
468 memset(&bin_data, 0, sizeof(bin_data));
469
470 /* Calculate virtual memory address of the loadable segment */
471 bin_data.base_addr_ptr = (void *) info->dlpi_addr +
472 info->dlpi_phdr[j].p_vaddr;
473
474 if ((info->dlpi_name == NULL || info->dlpi_name[0] == 0)) {
475 /*
476 * Only the first phdr without a dlpi_name
477 * encountered is considered as the program
478 * executable. The rest are vdsos.
479 */
480 if (!data->exec_found) {
481 ssize_t path_len;
482 data->exec_found = 1;
483
484 /*
485 * Use /proc/self/exe to resolve the
486 * executable's full path.
487 */
488 path_len = readlink("/proc/self/exe",
489 bin_data.resolved_path,
490 PATH_MAX - 1);
491 if (path_len <= 0)
492 break;
493
494 bin_data.resolved_path[path_len] = '\0';
495 bin_data.vdso = 0;
496 } else {
497 snprintf(bin_data.resolved_path,
498 PATH_MAX - 1, "[vdso]");
499 bin_data.vdso = 1;
500 }
501 } else {
502 /*
503 * For regular dl_phdr_info entries check if
504 * the path to the binary really exists. If not,
505 * treat as vdso and use dlpi_name as 'path'.
506 */
507 if (!realpath(info->dlpi_name,
508 bin_data.resolved_path)) {
509 snprintf(bin_data.resolved_path,
510 PATH_MAX - 1, "[%s]",
511 info->dlpi_name);
512 bin_data.vdso = 1;
513 } else {
514 bin_data.vdso = 0;
515 }
516 }
517
518 ret = extract_baddr(&bin_data);
519 break;
520 }
521 end:
522 return ret;
523 }
524
525 static
526 void ust_dl_table_statedump(void *owner)
527 {
528 unsigned int i;
529
530 if (ust_lock())
531 goto end;
532
533 /* Statedump each traced table entry into session for owner. */
534 for (i = 0; i < UST_DL_STATE_TABLE_SIZE; i++) {
535 struct cds_hlist_head *head;
536 struct lttng_ust_dl_node *e;
537
538 head = &dl_state_table[i];
539 cds_hlist_for_each_entry_2(e, head, node) {
540 if (e->traced)
541 trace_baddr(&e->bin_data, owner);
542 }
543 }
544
545 end:
546 ust_unlock();
547 }
548
549 static
550 void lttng_ust_dl_update_orig(void *ip)
551 {
552 struct dl_iterate_data data;
553
554 if (lttng_ust_getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
555 return;
556
557 /*
558 * Force the allocation of lttng-ust TLS variables when called from
559 * dlopen/dlclose instrumentation.
560 */
561 lttng_ust_alloc_tls();
562
563 data.exec_found = 0;
564 data.first = true;
565 data.cancel = false;
566 /*
567 * Iterate through the list of currently loaded shared objects and
568 * generate tables entries for loadable segments using
569 * extract_bin_info_events.
570 * Removed libraries are detected by mark-and-sweep: marking is
571 * done in the iteration over libraries, and sweeping is
572 * performed by iter_end().
573 */
574 dl_iterate_phdr(extract_bin_info_events, &data);
575 if (data.first)
576 iter_begin(&data);
577 iter_end(&data, ip);
578 }
579
580 /*
581 * Generate a statedump of base addresses of all shared objects loaded
582 * by the traced application, as well as for the application's
583 * executable itself.
584 */
585 static
586 int do_baddr_statedump(void *owner)
587 {
588 if (lttng_ust_getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
589 return 0;
590 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
591 ust_dl_table_statedump(owner);
592 return 0;
593 }
594
595 static
596 int do_procname_statedump(void *owner)
597 {
598 if (lttng_ust_getenv("LTTNG_UST_WITHOUT_PROCNAME_STATEDUMP"))
599 return 0;
600
601 trace_statedump_event(procname_cb, owner, lttng_ust_sockinfo_get_procname(owner));
602 return 0;
603 }
604
605 /*
606 * Generate a statedump of a given traced application. A statedump is
607 * delimited by start and end events. For a given (process, session)
608 * pair, begin/end events are serialized and will match. However, in a
609 * session, statedumps from different processes may be
610 * interleaved. The vpid context should be used to identify which
611 * events belong to which process.
612 *
613 * Grab the ust_lock outside of the RCU read-side lock because we
614 * perform synchronize_rcu with the ust_lock held, which can trigger
615 * deadlocks otherwise.
616 */
617 int do_lttng_ust_statedump(void *owner)
618 {
619 ust_lock_nocheck();
620 trace_statedump_start(owner);
621 ust_unlock();
622
623 do_procname_statedump(owner);
624 do_baddr_statedump(owner);
625
626 ust_lock_nocheck();
627 trace_statedump_end(owner);
628 ust_unlock();
629
630 return 0;
631 }
632
633 void lttng_ust_statedump_init(void)
634 {
635 lttng_ust__tracepoints__init();
636 lttng_ust__tracepoints__ptrs_init();
637 lttng_ust__events_init__lttng_ust_statedump();
638 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
639 }
640
641 static
642 void ust_dl_state_destroy(void)
643 {
644 unsigned int i;
645
646 for (i = 0; i < UST_DL_STATE_TABLE_SIZE; i++) {
647 struct cds_hlist_head *head;
648 struct lttng_ust_dl_node *e, *tmp;
649
650 head = &dl_state_table[i];
651 cds_hlist_for_each_entry_safe_2(e, tmp, head, node)
652 free_dl_node(e);
653 CDS_INIT_HLIST_HEAD(head);
654 }
655 }
656
657 void lttng_ust_statedump_destroy(void)
658 {
659 lttng_ust__events_exit__lttng_ust_statedump();
660 lttng_ust__tracepoints__ptrs_destroy();
661 lttng_ust__tracepoints__destroy();
662 ust_dl_state_destroy();
663 }
664
665 /* Custom upgrade 2.12 to 2.13 */
666 #undef lttng_ust_dl_update
667 void lttng_ust_dl_update1(void *ip)
668 __attribute__ ((alias ("lttng_ust_dl_update_orig")));
669
670 #ifdef LTTNG_UST_CUSTOM_UPGRADE_CONFLICTING_SYMBOLS
671 void lttng_ust_dl_update(void *ip)
672 __attribute__ ((alias ("lttng_ust_dl_update_orig")));
673 #endif
This page took 0.115017 seconds and 5 git commands to generate.