2 * SPDX-License-Identifier: LGPL-2.1-or-later
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>
16 #include <sys/types.h>
19 #include "common/elf.h"
20 #include "common/macros.h"
21 #include "lttng-tracer-core.h"
22 #include "lttng-ust-statedump.h"
25 #include "ust-events-internal.h"
27 #define TRACEPOINT_DEFINE
28 #include "ust_lib.h" /* Only define. */
30 #define TRACEPOINT_CREATE_PROBES
31 #define TP_SESSION_CHECK
32 #include "lttng-ust-statedump-provider.h" /* Define and create probes. */
34 struct dl_iterate_data
{
40 struct bin_info_data
{
42 char resolved_path
[PATH_MAX
];
51 uint8_t has_debug_link
;
54 struct lttng_ust_dl_node
{
55 struct bin_info_data bin_data
;
56 struct cds_hlist_node node
;
61 #define UST_DL_STATE_HASH_BITS 8
62 #define UST_DL_STATE_TABLE_SIZE (1 << UST_DL_STATE_HASH_BITS)
63 struct cds_hlist_head dl_state_table
[UST_DL_STATE_TABLE_SIZE
];
65 typedef void (*tracepoint_cb
)(struct lttng_ust_session
*session
, void *priv
);
68 struct lttng_ust_dl_node
*alloc_dl_node(const struct bin_info_data
*bin_data
)
70 struct lttng_ust_dl_node
*e
;
72 e
= zmalloc(sizeof(struct lttng_ust_dl_node
));
75 if (bin_data
->dbg_file
) {
76 e
->bin_data
.dbg_file
= strdup(bin_data
->dbg_file
);
77 if (!e
->bin_data
.dbg_file
)
80 if (bin_data
->build_id
) {
81 e
->bin_data
.build_id
= zmalloc(bin_data
->build_id_len
);
82 if (!e
->bin_data
.build_id
)
84 memcpy(e
->bin_data
.build_id
, bin_data
->build_id
,
85 bin_data
->build_id_len
);
87 e
->bin_data
.base_addr_ptr
= bin_data
->base_addr_ptr
;
88 memcpy(e
->bin_data
.resolved_path
, bin_data
->resolved_path
, PATH_MAX
);
89 e
->bin_data
.memsz
= bin_data
->memsz
;
90 e
->bin_data
.build_id_len
= bin_data
->build_id_len
;
91 e
->bin_data
.vdso
= bin_data
->vdso
;
92 e
->bin_data
.crc
= bin_data
->crc
;
93 e
->bin_data
.is_pic
= bin_data
->is_pic
;
94 e
->bin_data
.has_build_id
= bin_data
->has_build_id
;
95 e
->bin_data
.has_debug_link
= bin_data
->has_debug_link
;
99 free(e
->bin_data
.build_id
);
100 free(e
->bin_data
.dbg_file
);
106 void free_dl_node(struct lttng_ust_dl_node
*e
)
108 free(e
->bin_data
.build_id
);
109 free(e
->bin_data
.dbg_file
);
113 /* Return 0 if same, nonzero if not. */
115 int compare_bin_data(const struct bin_info_data
*a
,
116 const struct bin_info_data
*b
)
118 if (a
->base_addr_ptr
!= b
->base_addr_ptr
)
120 if (strcmp(a
->resolved_path
, b
->resolved_path
) != 0)
122 if (a
->dbg_file
&& !b
->dbg_file
)
124 if (!a
->dbg_file
&& b
->dbg_file
)
126 if (a
->dbg_file
&& strcmp(a
->dbg_file
, b
->dbg_file
) != 0)
128 if (a
->build_id
&& !b
->build_id
)
130 if (!a
->build_id
&& b
->build_id
)
132 if (a
->build_id_len
!= b
->build_id_len
)
135 memcmp(a
->build_id
, b
->build_id
, a
->build_id_len
) != 0)
137 if (a
->memsz
!= b
->memsz
)
139 if (a
->vdso
!= b
->vdso
)
141 if (a
->crc
!= b
->crc
)
143 if (a
->is_pic
!= b
->is_pic
)
145 if (a
->has_build_id
!= b
->has_build_id
)
147 if (a
->has_debug_link
!= b
->has_debug_link
)
153 struct lttng_ust_dl_node
*find_or_create_dl_node(struct bin_info_data
*bin_data
)
155 struct cds_hlist_head
*head
;
156 struct lttng_ust_dl_node
*e
;
160 hash
= jhash(&bin_data
->base_addr_ptr
,
161 sizeof(bin_data
->base_addr_ptr
), 0);
162 head
= &dl_state_table
[hash
& (UST_DL_STATE_TABLE_SIZE
- 1)];
163 cds_hlist_for_each_entry_2(e
, head
, node
) {
164 if (compare_bin_data(&e
->bin_data
, bin_data
) != 0)
171 e
= alloc_dl_node(bin_data
);
174 cds_hlist_add_head(&e
->node
, head
);
180 void remove_dl_node(struct lttng_ust_dl_node
*e
)
182 cds_hlist_del(&e
->node
);
186 * Trace statedump event into all sessions owned by the caller thread
187 * for which statedump is pending.
190 void trace_statedump_event(tracepoint_cb tp_cb
, void *owner
, void *priv
)
192 struct cds_list_head
*sessionsp
;
193 struct lttng_ust_session_private
*session_priv
;
195 sessionsp
= lttng_get_sessions();
196 cds_list_for_each_entry(session_priv
, sessionsp
, node
) {
197 if (session_priv
->owner
!= owner
)
199 if (!session_priv
->statedump_pending
)
201 tp_cb(session_priv
->pub
, priv
);
206 void trace_bin_info_cb(struct lttng_ust_session
*session
, void *priv
)
208 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
210 tracepoint(lttng_ust_statedump
, bin_info
,
211 session
, bin_data
->base_addr_ptr
,
212 bin_data
->resolved_path
, bin_data
->memsz
,
213 bin_data
->is_pic
, bin_data
->has_build_id
,
214 bin_data
->has_debug_link
);
218 void trace_build_id_cb(struct lttng_ust_session
*session
, void *priv
)
220 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
222 tracepoint(lttng_ust_statedump
, build_id
,
223 session
, bin_data
->base_addr_ptr
,
224 bin_data
->build_id
, bin_data
->build_id_len
);
228 void trace_debug_link_cb(struct lttng_ust_session
*session
, void *priv
)
230 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
232 tracepoint(lttng_ust_statedump
, debug_link
,
233 session
, bin_data
->base_addr_ptr
,
234 bin_data
->dbg_file
, bin_data
->crc
);
238 void procname_cb(struct lttng_ust_session
*session
, void *priv
)
240 char *procname
= (char *) priv
;
241 tracepoint(lttng_ust_statedump
, procname
, session
, procname
);
245 void trace_start_cb(struct lttng_ust_session
*session
, void *priv
__attribute__((unused
)))
247 tracepoint(lttng_ust_statedump
, start
, session
);
251 void trace_end_cb(struct lttng_ust_session
*session
, void *priv
__attribute__((unused
)))
253 tracepoint(lttng_ust_statedump
, end
, session
);
257 int get_elf_info(struct bin_info_data
*bin_data
)
259 struct lttng_ust_elf
*elf
;
262 elf
= lttng_ust_elf_create(bin_data
->resolved_path
);
268 ret
= lttng_ust_elf_get_memsz(elf
, &bin_data
->memsz
);
274 ret
= lttng_ust_elf_get_build_id(elf
, &bin_data
->build_id
,
275 &bin_data
->build_id_len
,
280 bin_data
->has_build_id
= !!found
;
282 ret
= lttng_ust_elf_get_debug_link(elf
, &bin_data
->dbg_file
,
288 bin_data
->has_debug_link
= !!found
;
290 bin_data
->is_pic
= lttng_ust_elf_is_pic(elf
);
293 lttng_ust_elf_destroy(elf
);
298 void trace_baddr(struct bin_info_data
*bin_data
, void *owner
)
300 trace_statedump_event(trace_bin_info_cb
, owner
, bin_data
);
302 if (bin_data
->has_build_id
)
303 trace_statedump_event(trace_build_id_cb
, owner
, bin_data
);
305 if (bin_data
->has_debug_link
)
306 trace_statedump_event(trace_debug_link_cb
, owner
, bin_data
);
310 int extract_baddr(struct bin_info_data
*bin_data
)
313 struct lttng_ust_dl_node
*e
;
315 if (!bin_data
->vdso
) {
316 ret
= get_elf_info(bin_data
);
322 bin_data
->has_build_id
= 0;
323 bin_data
->has_debug_link
= 0;
326 e
= find_or_create_dl_node(bin_data
);
333 free(bin_data
->build_id
);
334 bin_data
->build_id
= NULL
;
335 free(bin_data
->dbg_file
);
336 bin_data
->dbg_file
= NULL
;
341 void trace_statedump_start(void *owner
)
343 trace_statedump_event(trace_start_cb
, owner
, NULL
);
347 void trace_statedump_end(void *owner
)
349 trace_statedump_event(trace_end_cb
, owner
, NULL
);
353 void iter_begin(struct dl_iterate_data
*data
)
358 * UST lock nests within dynamic loader lock.
360 * Hold this lock across handling of the module listing to
361 * protect memory allocation at early process start, due to
362 * interactions with libc-wrapper lttng malloc instrumentation.
369 /* Ensure all entries are unmarked. */
370 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
371 struct cds_hlist_head
*head
;
372 struct lttng_ust_dl_node
*e
;
374 head
= &dl_state_table
[i
];
375 cds_hlist_for_each_entry_2(e
, head
, node
)
381 void trace_lib_load(const struct bin_info_data
*bin_data
, void *ip
)
383 tracepoint(lttng_ust_lib
, load
,
384 ip
, bin_data
->base_addr_ptr
, bin_data
->resolved_path
,
385 bin_data
->memsz
, bin_data
->has_build_id
,
386 bin_data
->has_debug_link
);
388 if (bin_data
->has_build_id
) {
389 tracepoint(lttng_ust_lib
, build_id
,
390 ip
, bin_data
->base_addr_ptr
, bin_data
->build_id
,
391 bin_data
->build_id_len
);
394 if (bin_data
->has_debug_link
) {
395 tracepoint(lttng_ust_lib
, debug_link
,
396 ip
, bin_data
->base_addr_ptr
, bin_data
->dbg_file
,
402 void trace_lib_unload(const struct bin_info_data
*bin_data
, void *ip
)
404 tracepoint(lttng_ust_lib
, unload
, ip
, bin_data
->base_addr_ptr
);
408 void iter_end(struct dl_iterate_data
*data
, void *ip
)
415 * Iterate on hash table.
416 * For each marked, traced, do nothing.
417 * For each marked, not traced, trace lib open event. traced = true.
418 * For each unmarked, traced, trace lib close event. remove node.
419 * For each unmarked, not traced, remove node.
421 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
422 struct cds_hlist_head
*head
;
423 struct lttng_ust_dl_node
*e
;
425 head
= &dl_state_table
[i
];
426 cds_hlist_for_each_entry_2(e
, head
, node
) {
429 trace_lib_load(&e
->bin_data
, ip
);
435 trace_lib_unload(&e
->bin_data
, ip
);
446 int extract_bin_info_events(struct dl_phdr_info
*info
, size_t size
__attribute__((unused
)), void *_data
)
449 struct dl_iterate_data
*data
= _data
;
459 for (j
= 0; j
< info
->dlpi_phnum
; j
++) {
460 struct bin_info_data bin_data
;
462 if (info
->dlpi_phdr
[j
].p_type
!= PT_LOAD
)
465 memset(&bin_data
, 0, sizeof(bin_data
));
467 /* Calculate virtual memory address of the loadable segment */
468 bin_data
.base_addr_ptr
= (void *) info
->dlpi_addr
+
469 info
->dlpi_phdr
[j
].p_vaddr
;
471 if ((info
->dlpi_name
== NULL
|| info
->dlpi_name
[0] == 0)) {
473 * Only the first phdr without a dlpi_name
474 * encountered is considered as the program
475 * executable. The rest are vdsos.
477 if (!data
->exec_found
) {
479 data
->exec_found
= 1;
482 * Use /proc/self/exe to resolve the
483 * executable's full path.
485 path_len
= readlink("/proc/self/exe",
486 bin_data
.resolved_path
,
491 bin_data
.resolved_path
[path_len
] = '\0';
494 snprintf(bin_data
.resolved_path
,
495 PATH_MAX
- 1, "[vdso]");
500 * For regular dl_phdr_info entries check if
501 * the path to the binary really exists. If not,
502 * treat as vdso and use dlpi_name as 'path'.
504 if (!realpath(info
->dlpi_name
,
505 bin_data
.resolved_path
)) {
506 snprintf(bin_data
.resolved_path
,
507 PATH_MAX
- 1, "[%s]",
515 ret
= extract_baddr(&bin_data
);
523 void ust_dl_table_statedump(void *owner
)
530 /* Statedump each traced table entry into session for owner. */
531 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
532 struct cds_hlist_head
*head
;
533 struct lttng_ust_dl_node
*e
;
535 head
= &dl_state_table
[i
];
536 cds_hlist_for_each_entry_2(e
, head
, node
) {
538 trace_baddr(&e
->bin_data
, owner
);
546 void lttng_ust_dl_update(void *ip
)
548 struct dl_iterate_data data
;
550 if (lttng_ust_getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
554 * Fixup lttng-ust TLS when called from dlopen/dlclose
557 lttng_ust_fixup_tls();
563 * Iterate through the list of currently loaded shared objects and
564 * generate tables entries for loadable segments using
565 * extract_bin_info_events.
566 * Removed libraries are detected by mark-and-sweep: marking is
567 * done in the iteration over libraries, and sweeping is
568 * performed by iter_end().
570 dl_iterate_phdr(extract_bin_info_events
, &data
);
577 * Generate a statedump of base addresses of all shared objects loaded
578 * by the traced application, as well as for the application's
582 int do_baddr_statedump(void *owner
)
584 if (lttng_ust_getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
586 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
587 ust_dl_table_statedump(owner
);
592 int do_procname_statedump(void *owner
)
594 if (lttng_ust_getenv("LTTNG_UST_WITHOUT_PROCNAME_STATEDUMP"))
597 trace_statedump_event(procname_cb
, owner
, lttng_ust_sockinfo_get_procname(owner
));
602 * Generate a statedump of a given traced application. A statedump is
603 * delimited by start and end events. For a given (process, session)
604 * pair, begin/end events are serialized and will match. However, in a
605 * session, statedumps from different processes may be
606 * interleaved. The vpid context should be used to identify which
607 * events belong to which process.
609 * Grab the ust_lock outside of the RCU read-side lock because we
610 * perform synchronize_rcu with the ust_lock held, which can trigger
611 * deadlocks otherwise.
613 int do_lttng_ust_statedump(void *owner
)
616 trace_statedump_start(owner
);
619 do_procname_statedump(owner
);
620 do_baddr_statedump(owner
);
623 trace_statedump_end(owner
);
629 void lttng_ust_statedump_init(void)
631 __tracepoints__init();
632 __tracepoints__ptrs_init();
633 __lttng_events_init__lttng_ust_statedump();
634 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
638 void ust_dl_state_destroy(void)
642 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
643 struct cds_hlist_head
*head
;
644 struct lttng_ust_dl_node
*e
, *tmp
;
646 head
= &dl_state_table
[i
];
647 cds_hlist_for_each_entry_safe_2(e
, tmp
, head
, node
)
649 CDS_INIT_HLIST_HEAD(head
);
653 void lttng_ust_statedump_destroy(void)
655 __lttng_events_exit__lttng_ust_statedump();
656 __tracepoints__ptrs_destroy();
657 __tracepoints__destroy();
658 ust_dl_state_destroy();