Fix: statedump: invalid read during iter_end
[lttng-ust.git] / src / lib / lttng-ust / lttng-ust-statedump.c
... / ...
CommitLineData
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
37struct dl_iterate_data {
38 int exec_found;
39 bool first;
40 bool cancel;
41};
42
43struct 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
57struct 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)
66static struct cds_hlist_head dl_state_table[UST_DL_STATE_TABLE_SIZE];
67
68typedef void (*tracepoint_cb)(struct lttng_ust_session *session, void *priv);
69
70static
71struct 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
101error:
102 free(e->bin_data.build_id);
103 free(e->bin_data.dbg_file);
104 free(e);
105 return NULL;
106}
107
108static
109void 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. */
117static
118int 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
155static
156struct 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
182static
183void 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 */
192static
193void 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
208static
209void 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
220static
221void 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
230static
231void 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
240static
241void 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
247static
248void trace_start_cb(struct lttng_ust_session *session, void *priv __attribute__((unused)))
249{
250 lttng_ust_tracepoint(lttng_ust_statedump, start, session);
251}
252
253static
254void trace_end_cb(struct lttng_ust_session *session, void *priv __attribute__((unused)))
255{
256 lttng_ust_tracepoint(lttng_ust_statedump, end, session);
257}
258
259static
260int 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
295end:
296 lttng_ust_elf_destroy(elf);
297 return ret;
298}
299
300static
301void 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
312static
313int 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;
335end:
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
343static
344void trace_statedump_start(void *owner)
345{
346 trace_statedump_event(trace_start_cb, owner, NULL);
347}
348
349static
350void trace_statedump_end(void *owner)
351{
352 trace_statedump_event(trace_end_cb, owner, NULL);
353}
354
355static
356void 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
383static
384void 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
404static
405void 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
410static
411void 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 }
444end:
445 ust_unlock();
446}
447
448static
449int 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 }
521end:
522 return ret;
523}
524
525static
526void 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
545end:
546 ust_unlock();
547}
548
549void lttng_ust_dl_update(void *ip)
550{
551 struct dl_iterate_data data;
552
553 if (lttng_ust_getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
554 return;
555
556 /*
557 * Force the allocation of lttng-ust TLS variables when called from
558 * dlopen/dlclose instrumentation.
559 */
560 lttng_ust_alloc_tls();
561
562 data.exec_found = 0;
563 data.first = true;
564 data.cancel = false;
565 /*
566 * Iterate through the list of currently loaded shared objects and
567 * generate tables entries for loadable segments using
568 * extract_bin_info_events.
569 * Removed libraries are detected by mark-and-sweep: marking is
570 * done in the iteration over libraries, and sweeping is
571 * performed by iter_end().
572 */
573 dl_iterate_phdr(extract_bin_info_events, &data);
574 if (data.first)
575 iter_begin(&data);
576 iter_end(&data, ip);
577}
578
579/*
580 * Generate a statedump of base addresses of all shared objects loaded
581 * by the traced application, as well as for the application's
582 * executable itself.
583 */
584static
585int do_baddr_statedump(void *owner)
586{
587 if (lttng_ust_getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
588 return 0;
589 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
590 ust_dl_table_statedump(owner);
591 return 0;
592}
593
594static
595int do_procname_statedump(void *owner)
596{
597 if (lttng_ust_getenv("LTTNG_UST_WITHOUT_PROCNAME_STATEDUMP"))
598 return 0;
599
600 trace_statedump_event(procname_cb, owner, lttng_ust_sockinfo_get_procname(owner));
601 return 0;
602}
603
604/*
605 * Generate a statedump of a given traced application. A statedump is
606 * delimited by start and end events. For a given (process, session)
607 * pair, begin/end events are serialized and will match. However, in a
608 * session, statedumps from different processes may be
609 * interleaved. The vpid context should be used to identify which
610 * events belong to which process.
611 *
612 * Grab the ust_lock outside of the RCU read-side lock because we
613 * perform synchronize_rcu with the ust_lock held, which can trigger
614 * deadlocks otherwise.
615 */
616int do_lttng_ust_statedump(void *owner)
617{
618 ust_lock_nocheck();
619 trace_statedump_start(owner);
620 ust_unlock();
621
622 do_procname_statedump(owner);
623 do_baddr_statedump(owner);
624
625 ust_lock_nocheck();
626 trace_statedump_end(owner);
627 ust_unlock();
628
629 return 0;
630}
631
632void lttng_ust_statedump_init(void)
633{
634 lttng_ust__tracepoints__init();
635 lttng_ust__tracepoints__ptrs_init();
636 lttng_ust__events_init__lttng_ust_statedump();
637 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
638}
639
640static
641void ust_dl_state_destroy(void)
642{
643 unsigned int i;
644
645 for (i = 0; i < UST_DL_STATE_TABLE_SIZE; i++) {
646 struct cds_hlist_head *head;
647 struct lttng_ust_dl_node *e, *tmp;
648
649 head = &dl_state_table[i];
650 cds_hlist_for_each_entry_safe_2(e, tmp, head, node)
651 free_dl_node(e);
652 CDS_INIT_HLIST_HEAD(head);
653 }
654}
655
656void lttng_ust_statedump_destroy(void)
657{
658 lttng_ust__events_exit__lttng_ust_statedump();
659 lttng_ust__tracepoints__ptrs_destroy();
660 lttng_ust__tracepoints__destroy();
661 ust_dl_state_destroy();
662}
This page took 0.035306 seconds and 4 git commands to generate.