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
28 #include <sys/types.h>
31 #include <lttng/ust-elf.h>
33 #include "lttng-tracer-core.h"
34 #include "lttng-ust-statedump.h"
39 #define TRACEPOINT_DEFINE
40 #include "ust_lib.h" /* Only define. */
42 #define TRACEPOINT_CREATE_PROBES
43 #define TP_SESSION_CHECK
44 #include "lttng-ust-statedump-provider.h" /* Define and create probes. */
46 struct dl_iterate_data
{
52 struct bin_info_data
{
54 char resolved_path
[PATH_MAX
];
63 uint8_t has_debug_link
;
66 struct lttng_ust_dl_node
{
67 struct bin_info_data bin_data
;
68 struct cds_hlist_node node
;
73 #define UST_DL_STATE_HASH_BITS 8
74 #define UST_DL_STATE_TABLE_SIZE (1 << UST_DL_STATE_HASH_BITS)
75 struct cds_hlist_head dl_state_table
[UST_DL_STATE_TABLE_SIZE
];
77 typedef void (*tracepoint_cb
)(struct lttng_session
*session
, void *priv
);
80 struct lttng_ust_dl_node
*alloc_dl_node(const struct bin_info_data
*bin_data
)
82 struct lttng_ust_dl_node
*e
;
84 e
= zmalloc(sizeof(struct lttng_ust_dl_node
));
87 if (bin_data
->dbg_file
) {
88 e
->bin_data
.dbg_file
= strdup(bin_data
->dbg_file
);
89 if (!e
->bin_data
.dbg_file
)
92 if (bin_data
->build_id
) {
93 e
->bin_data
.build_id
= zmalloc(bin_data
->build_id_len
);
94 if (!e
->bin_data
.build_id
)
96 memcpy(e
->bin_data
.build_id
, bin_data
->build_id
,
97 bin_data
->build_id_len
);
99 e
->bin_data
.base_addr_ptr
= bin_data
->base_addr_ptr
;
100 memcpy(e
->bin_data
.resolved_path
, bin_data
->resolved_path
, PATH_MAX
);
101 e
->bin_data
.memsz
= bin_data
->memsz
;
102 e
->bin_data
.build_id_len
= bin_data
->build_id_len
;
103 e
->bin_data
.vdso
= bin_data
->vdso
;
104 e
->bin_data
.crc
= bin_data
->crc
;
105 e
->bin_data
.is_pic
= bin_data
->is_pic
;
106 e
->bin_data
.has_build_id
= bin_data
->has_build_id
;
107 e
->bin_data
.has_debug_link
= bin_data
->has_debug_link
;
111 free(e
->bin_data
.build_id
);
112 free(e
->bin_data
.dbg_file
);
118 void free_dl_node(struct lttng_ust_dl_node
*e
)
120 free(e
->bin_data
.build_id
);
121 free(e
->bin_data
.dbg_file
);
125 /* Return 0 if same, nonzero if not. */
127 int compare_bin_data(const struct bin_info_data
*a
,
128 const struct bin_info_data
*b
)
130 if (a
->base_addr_ptr
!= b
->base_addr_ptr
)
132 if (strcmp(a
->resolved_path
, b
->resolved_path
) != 0)
134 if (a
->dbg_file
&& !b
->dbg_file
)
136 if (!a
->dbg_file
&& b
->dbg_file
)
138 if (a
->dbg_file
&& strcmp(a
->dbg_file
, b
->dbg_file
) != 0)
140 if (a
->build_id
&& !b
->build_id
)
142 if (!a
->build_id
&& b
->build_id
)
144 if (a
->build_id_len
!= b
->build_id_len
)
147 memcmp(a
->build_id
, b
->build_id
, a
->build_id_len
) != 0)
149 if (a
->memsz
!= b
->memsz
)
151 if (a
->vdso
!= b
->vdso
)
153 if (a
->crc
!= b
->crc
)
155 if (a
->is_pic
!= b
->is_pic
)
157 if (a
->has_build_id
!= b
->has_build_id
)
159 if (a
->has_debug_link
!= b
->has_debug_link
)
165 struct lttng_ust_dl_node
*find_or_create_dl_node(struct bin_info_data
*bin_data
)
167 struct cds_hlist_head
*head
;
168 struct lttng_ust_dl_node
*e
;
172 hash
= jhash(&bin_data
->base_addr_ptr
,
173 sizeof(bin_data
->base_addr_ptr
), 0);
174 head
= &dl_state_table
[hash
& (UST_DL_STATE_TABLE_SIZE
- 1)];
175 cds_hlist_for_each_entry_2(e
, head
, node
) {
176 if (compare_bin_data(&e
->bin_data
, bin_data
) != 0)
183 e
= alloc_dl_node(bin_data
);
186 cds_hlist_add_head(&e
->node
, head
);
192 void remove_dl_node(struct lttng_ust_dl_node
*e
)
194 cds_hlist_del(&e
->node
);
198 * Trace statedump event into all sessions owned by the caller thread
199 * for which statedump is pending.
202 void trace_statedump_event(tracepoint_cb tp_cb
, void *owner
, void *priv
)
204 struct cds_list_head
*sessionsp
;
205 struct lttng_session
*session
;
207 sessionsp
= _lttng_get_sessions();
208 cds_list_for_each_entry(session
, sessionsp
, node
) {
209 if (session
->owner
!= owner
)
211 if (!session
->statedump_pending
)
213 tp_cb(session
, priv
);
218 void trace_bin_info_cb(struct lttng_session
*session
, void *priv
)
220 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
222 tracepoint(lttng_ust_statedump
, bin_info
,
223 session
, bin_data
->base_addr_ptr
,
224 bin_data
->resolved_path
, bin_data
->memsz
,
225 bin_data
->is_pic
, bin_data
->has_build_id
,
226 bin_data
->has_debug_link
);
230 void trace_build_id_cb(struct lttng_session
*session
, void *priv
)
232 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
234 tracepoint(lttng_ust_statedump
, build_id
,
235 session
, bin_data
->base_addr_ptr
,
236 bin_data
->build_id
, bin_data
->build_id_len
);
240 void trace_debug_link_cb(struct lttng_session
*session
, void *priv
)
242 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
244 tracepoint(lttng_ust_statedump
, debug_link
,
245 session
, bin_data
->base_addr_ptr
,
246 bin_data
->dbg_file
, bin_data
->crc
);
250 void procname_cb(struct lttng_session
*session
, void *priv
)
252 char *procname
= (char *) priv
;
253 tracepoint(lttng_ust_statedump
, procname
, session
, procname
);
257 void trace_start_cb(struct lttng_session
*session
, void *priv
)
259 tracepoint(lttng_ust_statedump
, start
, session
);
263 void trace_end_cb(struct lttng_session
*session
, void *priv
)
265 tracepoint(lttng_ust_statedump
, end
, session
);
269 int get_elf_info(struct bin_info_data
*bin_data
)
271 struct lttng_ust_elf
*elf
;
274 elf
= lttng_ust_elf_create(bin_data
->resolved_path
);
280 ret
= lttng_ust_elf_get_memsz(elf
, &bin_data
->memsz
);
286 ret
= lttng_ust_elf_get_build_id(elf
, &bin_data
->build_id
,
287 &bin_data
->build_id_len
,
292 bin_data
->has_build_id
= !!found
;
294 ret
= lttng_ust_elf_get_debug_link(elf
, &bin_data
->dbg_file
,
300 bin_data
->has_debug_link
= !!found
;
302 bin_data
->is_pic
= lttng_ust_elf_is_pic(elf
);
305 lttng_ust_elf_destroy(elf
);
310 void trace_baddr(struct bin_info_data
*bin_data
, void *owner
)
312 trace_statedump_event(trace_bin_info_cb
, owner
, bin_data
);
314 if (bin_data
->has_build_id
)
315 trace_statedump_event(trace_build_id_cb
, owner
, bin_data
);
317 if (bin_data
->has_debug_link
)
318 trace_statedump_event(trace_debug_link_cb
, owner
, bin_data
);
322 int extract_baddr(struct bin_info_data
*bin_data
)
325 struct lttng_ust_dl_node
*e
;
327 if (!bin_data
->vdso
) {
328 ret
= get_elf_info(bin_data
);
334 bin_data
->has_build_id
= 0;
335 bin_data
->has_debug_link
= 0;
338 e
= find_or_create_dl_node(bin_data
);
345 free(bin_data
->build_id
);
346 bin_data
->build_id
= NULL
;
347 free(bin_data
->dbg_file
);
348 bin_data
->dbg_file
= NULL
;
353 void trace_statedump_start(void *owner
)
355 trace_statedump_event(trace_start_cb
, owner
, NULL
);
359 void trace_statedump_end(void *owner
)
361 trace_statedump_event(trace_end_cb
, owner
, NULL
);
365 void iter_begin(struct dl_iterate_data
*data
)
370 * UST lock nests within dynamic loader lock.
372 * Hold this lock across handling of the module listing to
373 * protect memory allocation at early process start, due to
374 * interactions with libc-wrapper lttng malloc instrumentation.
381 /* Ensure all entries are unmarked. */
382 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
383 struct cds_hlist_head
*head
;
384 struct lttng_ust_dl_node
*e
;
386 head
= &dl_state_table
[i
];
387 cds_hlist_for_each_entry_2(e
, head
, node
)
393 void trace_lib_load(const struct bin_info_data
*bin_data
, void *ip
)
395 tracepoint(lttng_ust_lib
, load
,
396 ip
, bin_data
->base_addr_ptr
, bin_data
->resolved_path
,
397 bin_data
->memsz
, bin_data
->has_build_id
,
398 bin_data
->has_debug_link
);
400 if (bin_data
->has_build_id
) {
401 tracepoint(lttng_ust_lib
, build_id
,
402 ip
, bin_data
->base_addr_ptr
, bin_data
->build_id
,
403 bin_data
->build_id_len
);
406 if (bin_data
->has_debug_link
) {
407 tracepoint(lttng_ust_lib
, debug_link
,
408 ip
, bin_data
->base_addr_ptr
, bin_data
->dbg_file
,
414 void trace_lib_unload(const struct bin_info_data
*bin_data
, void *ip
)
416 tracepoint(lttng_ust_lib
, unload
, ip
, bin_data
->base_addr_ptr
);
420 void iter_end(struct dl_iterate_data
*data
, void *ip
)
427 * Iterate on hash table.
428 * For each marked, traced, do nothing.
429 * For each marked, not traced, trace lib open event. traced = true.
430 * For each unmarked, traced, trace lib close event. remove node.
431 * For each unmarked, not traced, remove node.
433 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
434 struct cds_hlist_head
*head
;
435 struct lttng_ust_dl_node
*e
;
437 head
= &dl_state_table
[i
];
438 cds_hlist_for_each_entry_2(e
, head
, node
) {
441 trace_lib_load(&e
->bin_data
, ip
);
447 trace_lib_unload(&e
->bin_data
, ip
);
458 int extract_bin_info_events(struct dl_phdr_info
*info
, size_t size
, void *_data
)
461 struct dl_iterate_data
*data
= _data
;
471 for (j
= 0; j
< info
->dlpi_phnum
; j
++) {
472 struct bin_info_data bin_data
;
474 if (info
->dlpi_phdr
[j
].p_type
!= PT_LOAD
)
477 memset(&bin_data
, 0, sizeof(bin_data
));
479 /* Calculate virtual memory address of the loadable segment */
480 bin_data
.base_addr_ptr
= (void *) info
->dlpi_addr
+
481 info
->dlpi_phdr
[j
].p_vaddr
;
483 if ((info
->dlpi_name
== NULL
|| info
->dlpi_name
[0] == 0)) {
485 * Only the first phdr without a dlpi_name
486 * encountered is considered as the program
487 * executable. The rest are vdsos.
489 if (!data
->exec_found
) {
491 data
->exec_found
= 1;
494 * Use /proc/self/exe to resolve the
495 * executable's full path.
497 path_len
= readlink("/proc/self/exe",
498 bin_data
.resolved_path
,
503 bin_data
.resolved_path
[path_len
] = '\0';
506 snprintf(bin_data
.resolved_path
,
507 PATH_MAX
- 1, "[vdso]");
512 * For regular dl_phdr_info entries check if
513 * the path to the binary really exists. If not,
514 * treat as vdso and use dlpi_name as 'path'.
516 if (!realpath(info
->dlpi_name
,
517 bin_data
.resolved_path
)) {
518 snprintf(bin_data
.resolved_path
,
519 PATH_MAX
- 1, "[%s]",
527 ret
= extract_baddr(&bin_data
);
535 void ust_dl_table_statedump(void *owner
)
542 /* Statedump each traced table entry into session for owner. */
543 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
544 struct cds_hlist_head
*head
;
545 struct lttng_ust_dl_node
*e
;
547 head
= &dl_state_table
[i
];
548 cds_hlist_for_each_entry_2(e
, head
, node
) {
550 trace_baddr(&e
->bin_data
, owner
);
558 void lttng_ust_dl_update(void *ip
)
560 struct dl_iterate_data data
;
562 if (lttng_getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
566 * Fixup lttng-ust TLS when called from dlopen/dlclose
569 lttng_ust_fixup_tls();
575 * Iterate through the list of currently loaded shared objects and
576 * generate tables entries for loadable segments using
577 * extract_bin_info_events.
578 * Removed libraries are detected by mark-and-sweep: marking is
579 * done in the iteration over libraries, and sweeping is
580 * performed by iter_end().
582 dl_iterate_phdr(extract_bin_info_events
, &data
);
589 * Generate a statedump of base addresses of all shared objects loaded
590 * by the traced application, as well as for the application's
594 int do_baddr_statedump(void *owner
)
596 if (lttng_getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
598 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
599 ust_dl_table_statedump(owner
);
604 int do_procname_statedump(void *owner
)
606 if (lttng_getenv("LTTNG_UST_WITHOUT_PROCNAME_STATEDUMP"))
609 trace_statedump_event(procname_cb
, owner
, lttng_ust_sockinfo_get_procname(owner
));
614 * Generate a statedump of a given traced application. A statedump is
615 * delimited by start and end events. For a given (process, session)
616 * pair, begin/end events are serialized and will match. However, in a
617 * session, statedumps from different processes may be
618 * interleaved. The vpid context should be used to identify which
619 * events belong to which process.
621 * Grab the ust_lock outside of the RCU read-side lock because we
622 * perform synchronize_rcu with the ust_lock held, which can trigger
623 * deadlocks otherwise.
625 int do_lttng_ust_statedump(void *owner
)
628 trace_statedump_start(owner
);
631 do_procname_statedump(owner
);
632 do_baddr_statedump(owner
);
635 trace_statedump_end(owner
);
641 void lttng_ust_statedump_init(void)
643 __tracepoints__init();
644 __tracepoints__ptrs_init();
645 __lttng_events_init__lttng_ust_statedump();
646 lttng_ust_dl_update(LTTNG_UST_CALLER_IP());
650 void ust_dl_state_destroy(void)
654 for (i
= 0; i
< UST_DL_STATE_TABLE_SIZE
; i
++) {
655 struct cds_hlist_head
*head
;
656 struct lttng_ust_dl_node
*e
, *tmp
;
658 head
= &dl_state_table
[i
];
659 cds_hlist_for_each_entry_safe_2(e
, tmp
, head
, node
)
661 CDS_INIT_HLIST_HEAD(head
);
665 void lttng_ust_statedump_destroy(void)
667 __lttng_events_exit__lttng_ust_statedump();
668 __tracepoints__ptrs_destroy();
669 __tracepoints__destroy();
670 ust_dl_state_destroy();