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>
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.
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.
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
29 #include <sys/types.h>
32 #include <lttng/ust-elf.h>
34 #include "lttng-tracer-core.h"
35 #include "lttng-ust-statedump.h"
38 #define TRACEPOINT_DEFINE
39 #include "ust_lib.h" /* Only define. */
41 #define TRACEPOINT_CREATE_PROBES
42 #define TP_SESSION_CHECK
43 #include "lttng-ust-statedump-provider.h" /* Define and create probes. */
45 struct dl_iterate_data
{
51 struct bin_info_data
{
53 char resolved_path
[PATH_MAX
];
62 uint8_t has_debug_link
;
65 struct lttng_ust_dl_node
{
66 struct bin_info_data bin_data
;
67 struct cds_hlist_node node
;
72 #define UST_DL_STATE_HASH_BITS 8
73 #define UST_DL_STATE_TABLE_SIZE (1 << UST_DL_STATE_HASH_BITS)
74 struct cds_hlist_head dl_state_table
[UST_DL_STATE_TABLE_SIZE
];
76 typedef void (*tracepoint_cb
)(struct lttng_session
*session
, void *priv
);
79 struct lttng_ust_dl_node
*alloc_dl_node(const struct bin_info_data
*bin_data
)
81 struct lttng_ust_dl_node
*e
;
83 e
= zmalloc(sizeof(struct lttng_ust_dl_node
));
86 if (bin_data
->dbg_file
) {
87 e
->bin_data
.dbg_file
= strdup(bin_data
->dbg_file
);
88 if (!e
->bin_data
.dbg_file
)
91 if (bin_data
->build_id
) {
92 e
->bin_data
.build_id
= zmalloc(bin_data
->build_id_len
);
93 if (!e
->bin_data
.build_id
)
95 memcpy(e
->bin_data
.build_id
, bin_data
->build_id
,
96 bin_data
->build_id_len
);
98 e
->bin_data
.base_addr_ptr
= bin_data
->base_addr_ptr
;
99 memcpy(e
->bin_data
.resolved_path
, bin_data
->resolved_path
, PATH_MAX
);
100 e
->bin_data
.memsz
= bin_data
->memsz
;
101 e
->bin_data
.build_id_len
= bin_data
->build_id_len
;
102 e
->bin_data
.vdso
= bin_data
->vdso
;
103 e
->bin_data
.crc
= bin_data
->crc
;
104 e
->bin_data
.is_pic
= bin_data
->is_pic
;
105 e
->bin_data
.has_build_id
= bin_data
->has_build_id
;
106 e
->bin_data
.has_debug_link
= bin_data
->has_debug_link
;
110 free(e
->bin_data
.build_id
);
111 free(e
->bin_data
.dbg_file
);
117 void free_dl_node(struct lttng_ust_dl_node
*e
)
119 free(e
->bin_data
.build_id
);
120 free(e
->bin_data
.dbg_file
);
124 /* Return 0 if same, nonzero if not. */
126 int compare_bin_data(const struct bin_info_data
*a
,
127 const struct bin_info_data
*b
)
129 if (a
->base_addr_ptr
!= b
->base_addr_ptr
)
131 if (strcmp(a
->resolved_path
, b
->resolved_path
) != 0)
133 if (a
->dbg_file
&& !b
->dbg_file
)
135 if (!a
->dbg_file
&& b
->dbg_file
)
137 if (a
->dbg_file
&& strcmp(a
->dbg_file
, b
->dbg_file
) != 0)
139 if (a
->build_id
&& !b
->build_id
)
141 if (!a
->build_id
&& b
->build_id
)
143 if (a
->build_id_len
!= b
->build_id_len
)
146 memcmp(a
->build_id
, b
->build_id
, a
->build_id_len
) != 0)
148 if (a
->memsz
!= b
->memsz
)
150 if (a
->vdso
!= b
->vdso
)
152 if (a
->crc
!= b
->crc
)
154 if (a
->is_pic
!= b
->is_pic
)
156 if (a
->has_build_id
!= b
->has_build_id
)
158 if (a
->has_debug_link
!= b
->has_debug_link
)
164 struct lttng_ust_dl_node
*find_or_create_dl_node(struct bin_info_data
*bin_data
)
166 struct cds_hlist_head
*head
;
167 struct lttng_ust_dl_node
*e
;
171 hash
= jhash(&bin_data
->base_addr_ptr
,
172 sizeof(bin_data
->base_addr_ptr
), 0);
173 head
= &dl_state_table
[hash
& (UST_DL_STATE_TABLE_SIZE
- 1)];
174 cds_hlist_for_each_entry_2(e
, head
, node
) {
175 if (compare_bin_data(&e
->bin_data
, bin_data
) != 0)
182 e
= alloc_dl_node(bin_data
);
185 cds_hlist_add_head(&e
->node
, head
);
191 void remove_dl_node(struct lttng_ust_dl_node
*e
)
193 cds_hlist_del(&e
->node
);
197 * Trace statedump event into all sessions owned by the caller thread
198 * for which statedump is pending.
201 void trace_statedump_event(tracepoint_cb tp_cb
, void *owner
, void *priv
)
203 struct cds_list_head
*sessionsp
;
204 struct lttng_session
*session
;
206 sessionsp
= _lttng_get_sessions();
207 cds_list_for_each_entry(session
, sessionsp
, node
) {
208 if (session
->owner
!= owner
)
210 if (!session
->statedump_pending
)
212 tp_cb(session
, priv
);
217 void trace_bin_info_cb(struct lttng_session
*session
, void *priv
)
219 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
221 tracepoint(lttng_ust_statedump
, bin_info
,
222 session
, bin_data
->base_addr_ptr
,
223 bin_data
->resolved_path
, bin_data
->memsz
,
224 bin_data
->is_pic
, bin_data
->has_build_id
,
225 bin_data
->has_debug_link
);
229 void trace_build_id_cb(struct lttng_session
*session
, void *priv
)
231 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
233 tracepoint(lttng_ust_statedump
, build_id
,
234 session
, bin_data
->base_addr_ptr
,
235 bin_data
->build_id
, bin_data
->build_id_len
);
239 void trace_debug_link_cb(struct lttng_session
*session
, void *priv
)
241 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
243 tracepoint(lttng_ust_statedump
, debug_link
,
244 session
, bin_data
->base_addr_ptr
,
245 bin_data
->dbg_file
, bin_data
->crc
);
249 void trace_start_cb(struct lttng_session
*session
, void *priv
)
251 tracepoint(lttng_ust_statedump
, start
, session
);
255 void trace_end_cb(struct lttng_session
*session
, void *priv
)
257 tracepoint(lttng_ust_statedump
, end
, session
);
261 int get_elf_info(struct bin_info_data
*bin_data
)
263 struct lttng_ust_elf
*elf
;
266 elf
= lttng_ust_elf_create(bin_data
->resolved_path
);
272 ret
= lttng_ust_elf_get_memsz(elf
, &bin_data
->memsz
);
278 ret
= lttng_ust_elf_get_build_id(elf
, &bin_data
->build_id
,
279 &bin_data
->build_id_len
,
284 bin_data
->has_build_id
= !!found
;
286 ret
= lttng_ust_elf_get_debug_link(elf
, &bin_data
->dbg_file
,
292 bin_data
->has_debug_link
= !!found
;
294 bin_data
->is_pic
= lttng_ust_elf_is_pic(elf
);
297 lttng_ust_elf_destroy(elf
);
302 void trace_baddr(struct bin_info_data
*bin_data
, void *owner
)
304 trace_statedump_event(trace_bin_info_cb
, owner
, bin_data
);
306 if (bin_data
->has_build_id
)
307 trace_statedump_event(trace_build_id_cb
, owner
, bin_data
);
309 if (bin_data
->has_debug_link
)
310 trace_statedump_event(trace_debug_link_cb
, owner
, bin_data
);
314 int extract_baddr(struct bin_info_data
*bin_data
)
317 struct lttng_ust_dl_node
*e
;
319 if (!bin_data
->vdso
) {
320 ret
= get_elf_info(bin_data
);
326 bin_data
->has_build_id
= 0;
327 bin_data
->has_debug_link
= 0;
330 e
= find_or_create_dl_node(bin_data
);
337 free(bin_data
->build_id
);
338 bin_data
->build_id
= NULL
;
339 free(bin_data
->dbg_file
);
340 bin_data
->dbg_file
= NULL
;
345 void trace_statedump_start(void *owner
)
347 trace_statedump_event(trace_start_cb
, owner
, NULL
);
351 void trace_statedump_end(void *owner
)
353 trace_statedump_event(trace_end_cb
, owner
, NULL
);
357 void iter_begin(struct dl_iterate_data
*data
)
362 * UST lock nests within dynamic loader lock.
364 * Hold this lock across handling of the module listing to
365 * protect memory allocation at early process start, due to
366 * interactions with libc-wrapper lttng malloc instrumentation.
373 /* Ensure all entries are unmarked. */
374 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
375 struct cds_hlist_head
*head
;
376 struct lttng_ust_dl_node
*e
;
378 head
= &dl_state_table
[i
];
379 cds_hlist_for_each_entry_2(e
, head
, node
)
385 void trace_lib_load(const struct bin_info_data
*bin_data
, void *ip
)
387 tracepoint(lttng_ust_lib
, load
,
388 ip
, bin_data
->base_addr_ptr
, bin_data
->resolved_path
,
389 bin_data
->memsz
, bin_data
->has_build_id
,
390 bin_data
->has_debug_link
);
392 if (bin_data
->has_build_id
) {
393 tracepoint(lttng_ust_lib
, build_id
,
394 ip
, bin_data
->base_addr_ptr
, bin_data
->build_id
,
395 bin_data
->build_id_len
);
398 if (bin_data
->has_debug_link
) {
399 tracepoint(lttng_ust_lib
, debug_link
,
400 ip
, bin_data
->base_addr_ptr
, bin_data
->dbg_file
,
406 void trace_lib_unload(const struct bin_info_data
*bin_data
, void *ip
)
408 tracepoint(lttng_ust_lib
, unload
, ip
, bin_data
->base_addr_ptr
);
412 void iter_end(struct dl_iterate_data
*data
, void *ip
)
417 * Iterate on hash table.
418 * For each marked, traced, do nothing.
419 * For each marked, not traced, trace lib open event. traced = true.
420 * For each unmarked, traced, trace lib close event. remove node.
421 * For each unmarked, not traced, remove node.
423 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
424 struct cds_hlist_head
*head
;
425 struct lttng_ust_dl_node
*e
;
427 head
= &dl_state_table
[i
];
428 cds_hlist_for_each_entry_2(e
, head
, node
) {
431 trace_lib_load(&e
->bin_data
, ip
);
437 trace_lib_unload(&e
->bin_data
, ip
);
447 int extract_bin_info_events(struct dl_phdr_info
*info
, size_t size
, void *_data
)
450 struct dl_iterate_data
*data
= _data
;
460 for (j
= 0; j
< info
->dlpi_phnum
; j
++) {
461 struct bin_info_data bin_data
;
463 if (info
->dlpi_phdr
[j
].p_type
!= PT_LOAD
)
466 memset(&bin_data
, 0, sizeof(bin_data
));
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
;
472 if ((info
->dlpi_name
== NULL
|| info
->dlpi_name
[0] == 0)) {
474 * Only the first phdr without a dlpi_name
475 * encountered is considered as the program
476 * executable. The rest are vdsos.
478 if (!data
->exec_found
) {
480 data
->exec_found
= 1;
483 * Use /proc/self/exe to resolve the
484 * executable's full path.
486 path_len
= readlink("/proc/self/exe",
487 bin_data
.resolved_path
,
492 bin_data
.resolved_path
[path_len
] = '\0';
495 snprintf(bin_data
.resolved_path
,
496 PATH_MAX
- 1, "[vdso]");
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'.
505 if (!realpath(info
->dlpi_name
,
506 bin_data
.resolved_path
)) {
507 snprintf(bin_data
.resolved_path
,
508 PATH_MAX
- 1, "[%s]",
516 ret
= extract_baddr(&bin_data
);
524 void ust_dl_table_statedump(void *owner
)
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
;
536 head
= &dl_state_table
[i
];
537 cds_hlist_for_each_entry_2(e
, head
, node
) {
539 trace_baddr(&e
->bin_data
, owner
);
547 void lttng_ust_dl_update(void *ip
)
549 struct dl_iterate_data data
;
551 if (getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
555 * Fixup lttng-ust TLS when called from dlopen/dlclose
558 lttng_ust_fixup_tls();
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().
571 dl_iterate_phdr(extract_bin_info_events
, &data
);
578 * Generate a statedump of base addresses of all shared objects loaded
579 * by the traced application, as well as for the application's
583 int do_baddr_statedump(void *owner
)
585 if (getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
587 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
588 ust_dl_table_statedump(owner
);
593 * Generate a statedump of a given traced application. A statedump is
594 * delimited by start and end events. For a given (process, session)
595 * pair, begin/end events are serialized and will match. However, in a
596 * session, statedumps from different processes may be
597 * interleaved. The vpid context should be used to identify which
598 * events belong to which process.
600 * Grab the ust_lock outside of the RCU read-side lock because we
601 * perform synchronize_rcu with the ust_lock held, which can trigger
602 * deadlocks otherwise.
604 int do_lttng_ust_statedump(void *owner
)
607 trace_statedump_start(owner
);
610 do_baddr_statedump(owner
);
613 trace_statedump_end(owner
);
619 void lttng_ust_statedump_init(void)
621 __tracepoints__init();
622 __tracepoints__ptrs_init();
623 __lttng_events_init__lttng_ust_statedump();
624 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
628 void ust_dl_state_destroy(void)
632 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
633 struct cds_hlist_head
*head
;
634 struct lttng_ust_dl_node
*e
, *tmp
;
636 head
= &dl_state_table
[i
];
637 cds_hlist_for_each_entry_safe_2(e
, tmp
, head
, node
)
639 CDS_INIT_HLIST_HEAD(head
);
643 void lttng_ust_statedump_destroy(void)
645 __lttng_events_exit__lttng_ust_statedump();
646 __tracepoints__ptrs_destroy();
647 __tracepoints__destroy();
648 ust_dl_state_destroy();