2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
10 #include <babeltrace2/babeltrace.h>
12 #include "common/assert.h"
14 #include "fs-sink-ctf-meta.hpp"
15 #include "fs-sink-stream.hpp"
16 #include "fs-sink-trace.hpp"
17 #include "fs-sink.hpp"
18 #include "translate-ctf-ir-to-tsdl.hpp"
19 #include "translate-trace-ir-to-ctf-ir.hpp"
22 * Sanitizes `path` so as to:
24 * * Replace `.` subdirectories with `_`.
25 * * Replace `..` subdirectories with `__`.
26 * * Remove trailing slashes.
28 static GString
*sanitize_trace_path(const char *path
)
30 GString
*san_path
= g_string_new(NULL
);
31 const char *ch
= path
;
32 bool dir_start
= true;
40 /* Start of directory */
42 g_string_append_c(san_path
, *ch
);
51 g_string_append_c(san_path
, '_');
59 g_string_append(san_path
, "__");
73 /* Not a special character */
74 g_string_append_c(san_path
, *ch
);
79 /* Remove trailing slashes */
80 while (san_path
->len
> 0 && san_path
->str
[san_path
->len
- 1] == '/') {
81 /* Remove trailing slash */
82 g_string_set_size(san_path
, san_path
->len
- 1);
85 if (san_path
->len
== 0) {
86 /* Looks like there's nothing left: just use `trace` */
87 g_string_assign(san_path
, "trace");
94 * Find a path based on `path` that doesn't exist yet. First, try `path`
95 * itself, then try with incrementing suffixes.
98 static GString
*make_unique_trace_path(const char *path
)
100 GString
*unique_path
;
101 unsigned int suffix
= 0;
103 unique_path
= g_string_new(path
);
105 while (g_file_test(unique_path
->str
, G_FILE_TEST_EXISTS
)) {
106 g_string_printf(unique_path
, "%s-%u", path
, suffix
);
114 * Disable `deprecated-declarations` warnings for
115 * lttng_validate_datetime() because we're using `GTimeVal` and
116 * g_time_val_from_iso8601() which are deprecated since GLib 2.56
117 * (Babeltrace supports older versions too).
119 #pragma GCC diagnostic push
120 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
123 * Validate that the input string `datetime` is an ISO8601-compliant string (the
124 * format used by LTTng in the metadata).
127 static int lttng_validate_datetime(const struct fs_sink_trace
*trace
, const char *datetime
)
133 * We are using g_time_val_from_iso8601, as the safer/more modern
134 * alternative, g_date_time_new_from_iso8601, is only available in
135 * glib >= 2.56, and this is sufficient for our use case of validating
138 if (!g_time_val_from_iso8601(datetime
, &tv
)) {
139 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't parse datetime as ISO 8601: date=\"{}\"",
150 #pragma GCC diagnostic pop
152 static int append_lttng_trace_path_ust_uid(const struct fs_sink_trace
*trace
, GString
*path
,
158 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "tracer_buffering_id");
159 if (!v
|| !bt_value_is_signed_integer(v
)) {
160 BT_CPPLOGI_SPEC(trace
->logger
,
161 "Couldn't get environment value: name=\"tracer_buffering_id\"");
165 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%" PRId64
, bt_value_integer_signed_get(v
));
167 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "architecture_bit_width");
168 if (!v
|| !bt_value_is_signed_integer(v
)) {
169 BT_CPPLOGI_SPEC(trace
->logger
,
170 "Couldn't get environment value: name=\"architecture_bit_width\"");
174 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%" PRIu64
"-bit",
175 bt_value_integer_signed_get(v
));
187 static int append_lttng_trace_path_ust_pid(const struct fs_sink_trace
*trace
, GString
*path
,
191 const char *datetime
;
194 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "procname");
195 if (!v
|| !bt_value_is_string(v
)) {
196 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't get environment value: name=\"procname\"");
200 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", bt_value_string_get(v
));
202 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "vpid");
203 if (!v
|| !bt_value_is_signed_integer(v
)) {
204 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't get environment value: name=\"vpid\"");
208 g_string_append_printf(path
, "-%" PRId64
, bt_value_integer_signed_get(v
));
210 v
= bt_trace_borrow_environment_entry_value_by_name_const(tc
, "vpid_datetime");
211 if (!v
|| !bt_value_is_string(v
)) {
212 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't get environment value: name=\"vpid_datetime\"");
216 datetime
= bt_value_string_get(v
);
218 if (lttng_validate_datetime(trace
, datetime
)) {
222 g_string_append_printf(path
, "-%s", datetime
);
235 * Try to build a trace path based on environment values put in the trace
236 * environment by the LTTng tracer, starting with version 2.11.
238 static GString
*make_lttng_trace_path_rel(const struct fs_sink_trace
*trace
)
241 const char *tracer_name
, *domain
, *datetime
;
242 int64_t tracer_major
, tracer_minor
;
245 path
= g_string_new(NULL
);
250 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "tracer_name");
251 if (!v
|| !bt_value_is_string(v
)) {
252 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't get environment value: name=\"tracer_name\"");
256 tracer_name
= bt_value_string_get(v
);
258 if (!g_str_equal(tracer_name
, "lttng-ust") && !g_str_equal(tracer_name
, "lttng-modules")) {
259 BT_CPPLOGI_SPEC(trace
->logger
, "Unrecognized tracer name: name=\"{}\"", tracer_name
);
263 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "tracer_major");
264 if (!v
|| !bt_value_is_signed_integer(v
)) {
265 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't get environment value: name=\"tracer_major\"");
269 tracer_major
= bt_value_integer_signed_get(v
);
271 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "tracer_minor");
272 if (!v
|| !bt_value_is_signed_integer(v
)) {
273 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't get environment value: name=\"tracer_minor\"");
277 tracer_minor
= bt_value_integer_signed_get(v
);
279 if (!(tracer_major
>= 3 || (tracer_major
== 2 && tracer_minor
>= 11))) {
280 BT_CPPLOGI_SPEC(trace
->logger
,
281 "Unsupported LTTng version for automatic trace path: major={}, minor={}",
282 tracer_major
, tracer_minor
);
286 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "hostname");
287 if (!v
|| !bt_value_is_string(v
)) {
288 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't get environment value: name=\"tracer_hostname\"");
292 g_string_assign(path
, bt_value_string_get(v
));
294 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "trace_name");
295 if (!v
|| !bt_value_is_string(v
)) {
296 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't get environment value: name=\"trace_name\"");
300 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", bt_value_string_get(v
));
302 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
,
303 "trace_creation_datetime");
304 if (!v
|| !bt_value_is_string(v
)) {
305 BT_CPPLOGI_SPEC(trace
->logger
,
306 "Couldn't get environment value: name=\"trace_creation_datetime\"");
310 datetime
= bt_value_string_get(v
);
312 if (lttng_validate_datetime(trace
, datetime
)) {
316 g_string_append_printf(path
, "-%s", datetime
);
318 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
, "domain");
319 if (!v
|| !bt_value_is_string(v
)) {
320 BT_CPPLOGI_SPEC(trace
->logger
, "Couldn't get environment value: name=\"domain\"");
324 domain
= bt_value_string_get(v
);
325 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", domain
);
327 if (g_str_equal(domain
, "ust")) {
328 const char *tracer_buffering_scheme
;
330 v
= bt_trace_borrow_environment_entry_value_by_name_const(trace
->ir_trace
,
331 "tracer_buffering_scheme");
332 if (!v
|| !bt_value_is_string(v
)) {
333 BT_CPPLOGI_SPEC(trace
->logger
,
334 "Couldn't get environment value: name=\"tracer_buffering_scheme\"");
338 tracer_buffering_scheme
= bt_value_string_get(v
);
339 g_string_append_printf(path
, G_DIR_SEPARATOR_S
"%s", tracer_buffering_scheme
);
341 if (g_str_equal(tracer_buffering_scheme
, "uid")) {
342 if (append_lttng_trace_path_ust_uid(trace
, path
, trace
->ir_trace
)) {
345 } else if (g_str_equal(tracer_buffering_scheme
, "pid")) {
346 if (append_lttng_trace_path_ust_pid(trace
, path
, trace
->ir_trace
)) {
350 /* Unknown buffering scheme. */
351 BT_CPPLOGI_SPEC(trace
->logger
,
352 "Unknown buffering scheme: tracer_buffering_scheme=\"{}\"",
353 tracer_buffering_scheme
);
356 } else if (!g_str_equal(domain
, "kernel")) {
357 /* Unknown domain. */
358 BT_CPPLOGI_SPEC(trace
->logger
, "Unknown domain: domain=\"{}\"", domain
);
366 g_string_free(path
, TRUE
);
374 /* Build the relative output path for `trace`. */
376 static GString
*make_trace_path_rel(const struct fs_sink_trace
*trace
)
378 GString
*path
= NULL
;
379 const char *trace_name
;
381 BT_ASSERT(!trace
->fs_sink
->assume_single_trace
);
383 /* First, try to build a path using environment fields written by LTTng. */
384 path
= make_lttng_trace_path_rel(trace
);
389 /* Otherwise, use the trace name, if available. */
390 trace_name
= bt_trace_get_name(trace
->ir_trace
);
392 path
= g_string_new(trace_name
);
396 /* Otherwise, use "trace". */
397 path
= g_string_new("trace");
404 * Compute the trace output path for `trace`, rooted at `output_base_directory`.
407 static GString
*make_trace_path(const struct fs_sink_trace
*trace
,
408 const char *output_base_directory
)
410 GString
*rel_path
= NULL
;
411 GString
*rel_path_san
= NULL
;
412 GString
*full_path
= NULL
;
413 GString
*unique_full_path
= NULL
;
415 if (trace
->fs_sink
->assume_single_trace
) {
416 /* Use output directory directly */
417 unique_full_path
= g_string_new(output_base_directory
);
418 if (!unique_full_path
) {
422 rel_path
= make_trace_path_rel(trace
);
427 rel_path_san
= sanitize_trace_path(rel_path
->str
);
432 full_path
= g_string_new(NULL
);
437 g_string_printf(full_path
, "%s" G_DIR_SEPARATOR_S
"%s", output_base_directory
,
440 unique_full_path
= make_unique_trace_path(full_path
->str
);
445 g_string_free(rel_path
, TRUE
);
449 g_string_free(rel_path_san
, TRUE
);
453 g_string_free(full_path
, TRUE
);
456 return unique_full_path
;
459 void fs_sink_trace_destroy(struct fs_sink_trace
*trace
)
461 GString
*tsdl
= NULL
;
469 if (trace
->ir_trace_destruction_listener_id
!= UINT64_C(-1)) {
471 * Remove the destruction listener, otherwise it could
472 * be called in the future, and its private data is this
473 * CTF FS sink trace object which won't exist anymore.
475 (void) bt_trace_remove_destruction_listener(trace
->ir_trace
,
476 trace
->ir_trace_destruction_listener_id
);
477 trace
->ir_trace_destruction_listener_id
= UINT64_C(-1);
480 if (trace
->streams
) {
481 g_hash_table_destroy(trace
->streams
);
482 trace
->streams
= NULL
;
485 tsdl
= g_string_new(NULL
);
487 translate_trace_ctf_ir_to_tsdl(trace
->trace
, tsdl
);
489 BT_ASSERT(trace
->metadata_path
);
490 fh
= fopen(trace
->metadata_path
->str
, "wb");
492 BT_CPPLOGF_ERRNO_SPEC(trace
->logger
,
493 "In trace destruction listener: "
494 "cannot open metadata file for writing",
495 ": path=\"{}\"", trace
->metadata_path
->str
);
499 len
= fwrite(tsdl
->str
, sizeof(*tsdl
->str
), tsdl
->len
, fh
);
500 if (len
!= tsdl
->len
) {
501 BT_CPPLOGF_ERRNO_SPEC(trace
->logger
,
502 "In trace destruction listener: "
503 "cannot write metadata file",
504 ": path=\"{}\"", trace
->metadata_path
->str
);
508 if (!trace
->fs_sink
->quiet
) {
509 printf("Created CTF trace `%s`.\n", trace
->path
->str
);
513 g_string_free(trace
->path
, TRUE
);
518 int ret
= fclose(fh
);
521 BT_CPPLOGW_ERRNO_SPEC(trace
->logger
,
522 "In trace destruction listener: "
523 "cannot close metadata file",
524 ": path=\"{}\"", trace
->metadata_path
->str
);
528 g_string_free(trace
->metadata_path
, TRUE
);
529 trace
->metadata_path
= NULL
;
531 fs_sink_ctf_trace_destroy(trace
->trace
);
535 g_string_free(tsdl
, TRUE
);
541 static void ir_trace_destruction_listener(const bt_trace
*ir_trace
, void *data
)
543 struct fs_sink_trace
*trace
= (fs_sink_trace
*) data
;
546 * Prevent bt_trace_remove_destruction_listener() from being
547 * called in fs_sink_trace_destroy(), which is called by
548 * g_hash_table_remove() below.
550 trace
->ir_trace_destruction_listener_id
= UINT64_C(-1);
551 g_hash_table_remove(trace
->fs_sink
->traces
, ir_trace
);
554 struct fs_sink_trace
*fs_sink_trace_create(struct fs_sink_comp
*fs_sink
, const bt_trace
*ir_trace
)
557 fs_sink_trace
*trace
= new fs_sink_trace
{fs_sink
->logger
};
558 bt_trace_add_listener_status trace_status
;
560 trace
->fs_sink
= fs_sink
;
561 trace
->ir_trace
= ir_trace
;
562 trace
->ir_trace_destruction_listener_id
= UINT64_C(-1);
563 trace
->trace
= translate_trace_trace_ir_to_ctf_ir(fs_sink
, ir_trace
);
568 trace
->path
= make_trace_path(trace
, fs_sink
->output_dir_path
->str
);
569 BT_ASSERT(trace
->path
);
570 ret
= g_mkdir_with_parents(trace
->path
->str
, 0755);
572 BT_CPPLOGE_ERRNO_SPEC(trace
->logger
, "Cannot create directories for trace directory",
573 ": path=\"{}\"", trace
->path
->str
);
577 trace
->metadata_path
= g_string_new(trace
->path
->str
);
578 BT_ASSERT(trace
->metadata_path
);
579 g_string_append(trace
->metadata_path
, "/metadata");
580 trace
->streams
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
, NULL
,
581 (GDestroyNotify
) fs_sink_stream_destroy
);
582 BT_ASSERT(trace
->streams
);
583 trace_status
= bt_trace_add_destruction_listener(ir_trace
, ir_trace_destruction_listener
, trace
,
584 &trace
->ir_trace_destruction_listener_id
);
589 g_hash_table_insert(fs_sink
->traces
, (gpointer
) ir_trace
, trace
);
593 fs_sink_trace_destroy(trace
);