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