2 * Copyright (C) 2013 Paul Woegerer <paul_woegerer@mentor.com>
3 * Copyright (C) 2015 Antoine Busque <abusque@efficios.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <sys/types.h>
31 #include <lttng/ust-elf.h>
32 #include "lttng-tracer-core.h"
33 #include "lttng-ust-statedump.h"
35 #define TRACEPOINT_DEFINE
36 #define TRACEPOINT_CREATE_PROBES
37 #define TP_SESSION_CHECK
38 #include "lttng-ust-statedump-provider.h"
40 struct dl_iterate_data
{
45 struct bin_info_data
{
48 const char *resolved_path
;
57 uint8_t has_debug_link
;
60 typedef void (*tracepoint_cb
)(struct lttng_session
*session
, void *priv
);
63 * Trace statedump event into all sessions owned by the caller thread
64 * for which statedump is pending.
67 int trace_statedump_event(tracepoint_cb tp_cb
, void *owner
, void *priv
)
69 struct cds_list_head
*sessionsp
;
70 struct lttng_session
*session
;
72 sessionsp
= _lttng_get_sessions();
73 cds_list_for_each_entry(session
, sessionsp
, node
) {
74 if (session
->owner
!= owner
)
76 if (!session
->statedump_pending
)
84 void trace_bin_info_cb(struct lttng_session
*session
, void *priv
)
86 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
88 tracepoint(lttng_ust_statedump
, bin_info
,
89 session
, bin_data
->base_addr_ptr
,
90 bin_data
->resolved_path
, bin_data
->memsz
,
91 bin_data
->is_pic
, bin_data
->has_build_id
,
92 bin_data
->has_debug_link
);
96 void trace_build_id_cb(struct lttng_session
*session
, void *priv
)
98 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
100 tracepoint(lttng_ust_statedump
, build_id
,
101 session
, bin_data
->base_addr_ptr
,
102 bin_data
->build_id
, bin_data
->build_id_len
);
106 void trace_debug_link_cb(struct lttng_session
*session
, void *priv
)
108 struct bin_info_data
*bin_data
= (struct bin_info_data
*) priv
;
110 tracepoint(lttng_ust_statedump
, debug_link
,
111 session
, bin_data
->base_addr_ptr
,
112 bin_data
->dbg_file
, bin_data
->crc
);
116 void trace_start_cb(struct lttng_session
*session
, void *priv
)
118 tracepoint(lttng_ust_statedump
, start
, session
);
122 void trace_end_cb(struct lttng_session
*session
, void *priv
)
124 tracepoint(lttng_ust_statedump
, end
, session
);
128 int get_elf_info(struct bin_info_data
*bin_data
)
130 struct lttng_ust_elf
*elf
;
133 elf
= lttng_ust_elf_create(bin_data
->resolved_path
);
139 ret
= lttng_ust_elf_get_memsz(elf
, &bin_data
->memsz
);
145 ret
= lttng_ust_elf_get_build_id(elf
, &bin_data
->build_id
,
146 &bin_data
->build_id_len
,
151 bin_data
->has_build_id
= !!found
;
153 ret
= lttng_ust_elf_get_debug_link(elf
, &bin_data
->dbg_file
,
159 bin_data
->has_debug_link
= !!found
;
161 bin_data
->is_pic
= lttng_ust_elf_is_pic(elf
);
164 lttng_ust_elf_destroy(elf
);
169 int trace_baddr(struct bin_info_data
*bin_data
)
173 if (!bin_data
->vdso
) {
174 ret
= get_elf_info(bin_data
);
182 ret
= trace_statedump_event(trace_bin_info_cb
, bin_data
->owner
,
188 if (bin_data
->has_build_id
) {
189 ret
= trace_statedump_event(
190 trace_build_id_cb
, bin_data
->owner
, bin_data
);
191 free(bin_data
->build_id
);
197 if (bin_data
->has_debug_link
) {
198 ret
= trace_statedump_event(
199 trace_debug_link_cb
, bin_data
->owner
, bin_data
);
200 free(bin_data
->dbg_file
);
211 int trace_statedump_start(void *owner
)
213 return trace_statedump_event(trace_start_cb
, owner
, NULL
);
217 int trace_statedump_end(void *owner
)
219 return trace_statedump_event(trace_end_cb
, owner
, NULL
);
223 int extract_bin_info_events(struct dl_phdr_info
*info
, size_t size
, void *_data
)
226 struct dl_iterate_data
*data
= _data
;
229 * UST lock nests within dynamic loader lock.
231 * Hold this lock across handling of the entire module to
232 * protect memory allocation at early process start, due to
233 * interactions with libc-wrapper lttng malloc instrumentation.
239 for (j
= 0; j
< info
->dlpi_phnum
; j
++) {
240 struct bin_info_data bin_data
;
241 char resolved_path
[PATH_MAX
];
244 if (info
->dlpi_phdr
[j
].p_type
!= PT_LOAD
)
247 /* Calculate virtual memory address of the loadable segment */
248 base_addr_ptr
= (void *) info
->dlpi_addr
+
249 info
->dlpi_phdr
[j
].p_vaddr
;
251 if ((info
->dlpi_name
== NULL
|| info
->dlpi_name
[0] == 0)) {
253 * Only the first phdr without a dlpi_name
254 * encountered is considered as the program
255 * executable. The rest are vdsos.
257 if (!data
->exec_found
) {
259 data
->exec_found
= 1;
262 * Use /proc/self/exe to resolve the
263 * executable's full path.
265 path_len
= readlink("/proc/self/exe",
271 resolved_path
[path_len
] = '\0';
274 snprintf(resolved_path
, PATH_MAX
- 1, "[vdso]");
279 * For regular dl_phdr_info entries check if
280 * the path to the binary really exists. If not,
281 * treat as vdso and use dlpi_name as 'path'.
283 if (!realpath(info
->dlpi_name
, resolved_path
)) {
284 snprintf(resolved_path
, PATH_MAX
- 1, "[%s]",
292 bin_data
.owner
= data
->owner
;
293 bin_data
.base_addr_ptr
= base_addr_ptr
;
294 bin_data
.resolved_path
= resolved_path
;
295 ret
= trace_baddr(&bin_data
);
304 * Generate a statedump of base addresses of all shared objects loaded
305 * by the traced application, as well as for the application's
309 int do_baddr_statedump(void *owner
)
311 struct dl_iterate_data data
;
313 if (getenv("LTTNG_UST_WITHOUT_BADDR_STATEDUMP"))
319 * Iterate through the list of currently loaded shared objects and
320 * generate events for loadable segments using
321 * extract_bin_info_events.
323 dl_iterate_phdr(extract_bin_info_events
, &data
);
329 * Generate a statedump of a given traced application. A statedump is
330 * delimited by start and end events. For a given (process, session)
331 * pair, begin/end events are serialized and will match. However, in a
332 * session, statedumps from different processes may be
333 * interleaved. The vpid context should be used to identify which
334 * events belong to which process.
336 int do_lttng_ust_statedump(void *owner
)
338 trace_statedump_start(owner
);
339 do_baddr_statedump(owner
);
340 trace_statedump_end(owner
);
345 void lttng_ust_statedump_init(void)
347 __tracepoints__init();
348 __tracepoints__ptrs_init();
349 __lttng_events_init__lttng_ust_statedump();
352 void lttng_ust_statedump_destroy(void)
354 __lttng_events_exit__lttng_ust_statedump();
355 __tracepoints__ptrs_destroy();
356 __tracepoints__destroy();
This page took 0.037253 seconds and 4 git commands to generate.