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