sink.ctf.fs: cleanup: duplicated `:` in log statements
[babeltrace.git] / src / plugins / ctf / fs-sink / fs-sink-trace.c
CommitLineData
15fe47e0
PP
1/*
2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
aa1a7452 23#define BT_COMP_LOG_SELF_COMP (trace->fs_sink->self_comp)
ffa3b2b3 24#define BT_LOG_OUTPUT_LEVEL (trace->log_level)
350ad6c1 25#define BT_LOG_TAG "PLUGIN/SINK.CTF.FS/TRACE"
d9c39b0a 26#include "logging/comp-logging.h"
15fe47e0 27
3fadfbc0 28#include <babeltrace2/babeltrace.h>
15fe47e0
PP
29#include <stdio.h>
30#include <stdbool.h>
31#include <glib.h>
578e048b
MJ
32#include "common/assert.h"
33#include "ctfser/ctfser.h"
15fe47e0
PP
34
35#include "translate-trace-ir-to-ctf-ir.h"
36#include "translate-ctf-ir-to-tsdl.h"
37#include "fs-sink.h"
38#include "fs-sink-trace.h"
39#include "fs-sink-stream.h"
40
41/*
42 * Sanitizes `path` so as to:
43 *
44 * * Replace `.` subdirectories with `_`.
45 * * Replace `..` subdirectories with `__`.
46 * * Remove trailing slashes.
47 */
48static
49GString *sanitize_trace_path(const char *path)
50{
51 GString *san_path = g_string_new(NULL);
52 const char *ch = path;
53 bool dir_start = true;
54
55 BT_ASSERT(san_path);
56 BT_ASSERT(path);
57
58 while (*ch != '\0') {
59 switch (*ch) {
60 case '/':
61 /* Start of directory */
62 dir_start = true;
63 g_string_append_c(san_path, *ch);
64 ch++;
65 continue;
66 case '.':
67 if (dir_start) {
68 switch (ch[1]) {
69 case '\0':
70 case '/':
71 /* `.` -> `_` */
72 g_string_append_c(san_path, '_');
73 ch++;
74 continue;
75 case '.':
76 switch (ch[2]) {
77 case '\0':
78 case '/':
79 /* `..` -> `__` */
80 g_string_append(san_path, "__");
81 ch += 2;
82 continue;
83 default:
84 break;
85 }
86 default:
87 break;
88 }
89 }
90 default:
91 break;
92 }
93
94 /* Not a special character */
95 g_string_append_c(san_path, *ch);
96 ch++;
97 dir_start = false;
98 }
99
100 /* Remove trailing slashes */
101 while (san_path->len > 0 &&
102 san_path->str[san_path->len - 1] == '/') {
103 /* Remove trailing slash */
104 g_string_set_size(san_path, san_path->len - 1);
105 }
106
107 if (san_path->len == 0) {
108 /* Looks like there's nothing left: just use `trace` */
109 g_string_assign(san_path, "trace");
110 }
111
112 return san_path;
113}
114
be38c486
SM
115/*
116 * Find a path based on `path` that doesn't exist yet. First, try `path`
117 * itself, then try with incrementing suffixes.
118 */
119
15fe47e0 120static
be38c486 121GString *make_unique_trace_path(const char *path)
15fe47e0 122{
be38c486 123 GString *unique_path;
15fe47e0
PP
124 unsigned int suffix = 0;
125
be38c486
SM
126 unique_path = g_string_new(path);
127
128 while (g_file_test(unique_path->str, G_FILE_TEST_EXISTS)) {
129 g_string_printf(unique_path, "%s-%u", path, suffix);
130 suffix++;
131 }
132
133 return unique_path;
134}
135
136/*
137 * Validate that the input string `datetime` is an ISO8601-compliant string (the
138 * format used by LTTng in the metadata).
139 */
140
141static
ffa3b2b3
PP
142int lttng_validate_datetime(const struct fs_sink_trace *trace,
143 const char *datetime)
be38c486 144{
5ec1bd5c 145 GTimeVal tv;
be38c486
SM
146 int ret = -1;
147
5ec1bd5c
SM
148 /*
149 * We are using g_time_val_from_iso8601, as the safer/more modern
150 * alternative, g_date_time_new_from_iso8601, is only available in
151 * glib >= 2.56, and this is sufficient for our use case of validating
152 * the format.
153 */
154 if (!g_time_val_from_iso8601(datetime, &tv)) {
aa1a7452 155 BT_COMP_LOGI("Couldn't parse datetime as ISO 8601: date=\"%s\"", datetime);
be38c486
SM
156 goto end;
157 }
15fe47e0 158
be38c486
SM
159 ret = 0;
160
161end:
be38c486
SM
162 return ret;
163}
164
165static
ffa3b2b3 166int append_lttng_trace_path_ust_uid(const struct fs_sink_trace *trace,
335a2da5 167 GString *path, const bt_trace *tc)
be38c486
SM
168{
169 const bt_value *v;
170 int ret;
171
335a2da5 172 v = bt_trace_borrow_environment_entry_value_by_name_const(tc, "tracer_buffering_id");
fdd3a2da 173 if (!v || !bt_value_is_signed_integer(v)) {
aa1a7452 174 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_buffering_id\"");
be38c486
SM
175 goto error;
176 }
177
fdd3a2da 178 g_string_append_printf(path, G_DIR_SEPARATOR_S "%" PRId64,
9c08c816 179 bt_value_integer_signed_get(v));
be38c486 180
4a543d06 181 v = bt_trace_borrow_environment_entry_value_by_name_const(tc, "architecture_bit_width");
fdd3a2da 182 if (!v || !bt_value_is_signed_integer(v)) {
4a543d06 183 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"architecture_bit_width\"");
be38c486
SM
184 goto error;
185 }
186
fdd3a2da 187 g_string_append_printf(path, G_DIR_SEPARATOR_S "%" PRIu64 "-bit",
9c08c816 188 bt_value_integer_signed_get(v));
be38c486
SM
189
190 ret = 0;
191 goto end;
192
193error:
194 ret = -1;
195
15fe47e0 196end:
be38c486
SM
197 return ret;
198}
199
200static
ffa3b2b3 201int append_lttng_trace_path_ust_pid(const struct fs_sink_trace *trace,
335a2da5 202 GString *path, const bt_trace *tc)
be38c486
SM
203{
204 const bt_value *v;
205 const char *datetime;
206 int ret;
207
335a2da5 208 v = bt_trace_borrow_environment_entry_value_by_name_const(tc, "procname");
be38c486 209 if (!v || !bt_value_is_string(v)) {
aa1a7452 210 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"procname\"");
be38c486
SM
211 goto error;
212 }
213
214 g_string_append_printf(path, G_DIR_SEPARATOR_S "%s", bt_value_string_get(v));
215
335a2da5 216 v = bt_trace_borrow_environment_entry_value_by_name_const(tc, "vpid");
fdd3a2da 217 if (!v || !bt_value_is_signed_integer(v)) {
aa1a7452 218 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"vpid\"");
be38c486
SM
219 goto error;
220 }
221
9c08c816 222 g_string_append_printf(path, "-%" PRId64, bt_value_integer_signed_get(v));
be38c486 223
335a2da5 224 v = bt_trace_borrow_environment_entry_value_by_name_const(tc, "vpid_datetime");
be38c486 225 if (!v || !bt_value_is_string(v)) {
aa1a7452 226 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"vpid_datetime\"");
be38c486
SM
227 goto error;
228 }
229
230 datetime = bt_value_string_get(v);
231
ffa3b2b3 232 if (lttng_validate_datetime(trace, datetime)) {
be38c486
SM
233 goto error;
234 }
235
236 g_string_append_printf(path, "-%s", datetime);
237
238 ret = 0;
239 goto end;
240
241error:
242 ret = -1;
243
244end:
245 return ret;
246}
247
248/*
249 * Try to build a trace path based on environment values put in the trace
250 * environment by the LTTng tracer, starting with version 2.11.
251 */
252static
253GString *make_lttng_trace_path_rel(const struct fs_sink_trace *trace)
254{
be38c486
SM
255 const bt_value *v;
256 const char *tracer_name, *domain, *datetime;
257 int64_t tracer_major, tracer_minor;
258 GString *path;
259
260 path = g_string_new(NULL);
261 if (!path) {
262 goto error;
263 }
264
335a2da5
PP
265 v = bt_trace_borrow_environment_entry_value_by_name_const(
266 trace->ir_trace, "tracer_name");
be38c486 267 if (!v || !bt_value_is_string(v)) {
aa1a7452 268 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_name\"");
be38c486
SM
269 goto error;
270 }
271
272 tracer_name = bt_value_string_get(v);
273
274 if (!g_str_equal(tracer_name, "lttng-ust")
275 && !g_str_equal(tracer_name, "lttng-modules")) {
aa1a7452 276 BT_COMP_LOGI("Unrecognized tracer name: name=\"%s\"", tracer_name);
be38c486 277 goto error;
15fe47e0
PP
278 }
279
335a2da5
PP
280 v = bt_trace_borrow_environment_entry_value_by_name_const(
281 trace->ir_trace, "tracer_major");
fdd3a2da 282 if (!v || !bt_value_is_signed_integer(v)) {
aa1a7452 283 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_major\"");
be38c486
SM
284 goto error;
285 }
286
9c08c816 287 tracer_major = bt_value_integer_signed_get(v);
be38c486 288
335a2da5
PP
289 v = bt_trace_borrow_environment_entry_value_by_name_const(
290 trace->ir_trace, "tracer_minor");
fdd3a2da 291 if (!v || !bt_value_is_signed_integer(v)) {
aa1a7452 292 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_minor\"");
be38c486
SM
293 goto error;
294 }
295
9c08c816 296 tracer_minor = bt_value_integer_signed_get(v);
be38c486
SM
297
298 if (!(tracer_major >= 3 || (tracer_major == 2 && tracer_minor >= 11))) {
aa1a7452 299 BT_COMP_LOGI("Unsupported LTTng version for automatic trace path: major=%" PRId64 ", minor=%" PRId64,
be38c486
SM
300 tracer_major, tracer_minor);
301 goto error;
302 }
303
335a2da5
PP
304 v = bt_trace_borrow_environment_entry_value_by_name_const(
305 trace->ir_trace, "hostname");
be38c486 306 if (!v || !bt_value_is_string(v)) {
aa1a7452 307 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_hostname\"");
be38c486
SM
308 goto error;
309 }
310
311 g_string_assign(path, bt_value_string_get(v));
312
335a2da5
PP
313 v = bt_trace_borrow_environment_entry_value_by_name_const(
314 trace->ir_trace, "trace_name");
be38c486 315 if (!v || !bt_value_is_string(v)) {
aa1a7452 316 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"trace_name\"");
be38c486
SM
317 goto error;
318 }
319
320 g_string_append_printf(path, G_DIR_SEPARATOR_S "%s", bt_value_string_get(v));
321
335a2da5
PP
322 v = bt_trace_borrow_environment_entry_value_by_name_const(
323 trace->ir_trace, "trace_creation_datetime");
be38c486 324 if (!v || !bt_value_is_string(v)) {
aa1a7452 325 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"trace_creation_datetime\"");
be38c486
SM
326 goto error;
327 }
328
329 datetime = bt_value_string_get(v);
330
ffa3b2b3 331 if (lttng_validate_datetime(trace, datetime)) {
be38c486
SM
332 goto error;
333 }
334
335 g_string_append_printf(path, "-%s", datetime);
336
335a2da5
PP
337 v = bt_trace_borrow_environment_entry_value_by_name_const(
338 trace->ir_trace, "domain");
be38c486 339 if (!v || !bt_value_is_string(v)) {
aa1a7452 340 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"domain\"");
be38c486
SM
341 goto error;
342 }
343
344 domain = bt_value_string_get(v);
345 g_string_append_printf(path, G_DIR_SEPARATOR_S "%s", domain);
346
347 if (g_str_equal(domain, "ust")) {
348 const char *tracer_buffering_scheme;
349
335a2da5
PP
350 v = bt_trace_borrow_environment_entry_value_by_name_const(
351 trace->ir_trace, "tracer_buffering_scheme");
be38c486 352 if (!v || !bt_value_is_string(v)) {
aa1a7452 353 BT_COMP_LOGI_STR("Couldn't get environment value: name=\"tracer_buffering_scheme\"");
be38c486
SM
354 goto error;
355 }
356
357 tracer_buffering_scheme = bt_value_string_get(v);
358 g_string_append_printf(path, G_DIR_SEPARATOR_S "%s", tracer_buffering_scheme);
359
360 if (g_str_equal(tracer_buffering_scheme, "uid")) {
335a2da5
PP
361 if (append_lttng_trace_path_ust_uid(trace, path,
362 trace->ir_trace)) {
be38c486
SM
363 goto error;
364 }
365 } else if (g_str_equal(tracer_buffering_scheme, "pid")){
335a2da5
PP
366 if (append_lttng_trace_path_ust_pid(trace, path,
367 trace->ir_trace)) {
be38c486
SM
368 goto error;
369 }
370 } else {
371 /* Unknown buffering scheme. */
aa1a7452 372 BT_COMP_LOGI("Unknown buffering scheme: tracer_buffering_scheme=\"%s\"", tracer_buffering_scheme);
be38c486
SM
373 goto error;
374 }
375 } else if (!g_str_equal(domain, "kernel")) {
376 /* Unknown domain. */
aa1a7452 377 BT_COMP_LOGI("Unknown domain: domain=\"%s\"", domain);
be38c486
SM
378 goto error;
379 }
380
381 goto end;
382
383error:
384 if (path) {
385 g_string_free(path, TRUE);
386 path = NULL;
387 }
388
389end:
390 return path;
391}
392
393/* Build the relative output path for `trace`. */
394
395static
396GString *make_trace_path_rel(const struct fs_sink_trace *trace)
397{
398 GString *path = NULL;
399
400 if (trace->fs_sink->assume_single_trace) {
401 /* Use output directory directly */
402 path = g_string_new("");
403 goto end;
404 }
405
406 /* First, try to build a path using environment fields written by LTTng. */
407 path = make_lttng_trace_path_rel(trace);
408 if (path) {
409 goto end;
410 }
411
412 /* Otherwise, use the trace name, if available. */
413 const char *trace_name = bt_trace_get_name(trace->ir_trace);
414 if (trace_name) {
415 path = g_string_new(trace_name);
416 goto end;
417 }
418
419 /* Otherwise, use "trace". */
420 path = g_string_new("trace");
421
422end:
15fe47e0
PP
423 return path;
424}
425
be38c486
SM
426/*
427 * Compute the trace output path for `trace`, rooted at `output_base_directory`.
428 */
429
430static
431GString *make_trace_path(const struct fs_sink_trace *trace, const char *output_base_directory)
432{
433 GString *rel_path = NULL;
434 GString *rel_path_san = NULL;
435 GString *full_path = NULL;
436 GString *unique_full_path = NULL;
437
438 rel_path = make_trace_path_rel(trace);
439 if (!rel_path) {
440 goto end;
441 }
442
443 rel_path_san = sanitize_trace_path(rel_path->str);
444 if (!rel_path_san) {
445 goto end;
446 }
447
448 full_path = g_string_new(NULL);
449 if (!full_path) {
450 goto end;
451 }
452
453 g_string_printf(full_path, "%s" G_DIR_SEPARATOR_S "%s",
454 output_base_directory, rel_path_san->str);
455
456 unique_full_path = make_unique_trace_path(full_path->str);
457
458end:
459 if (rel_path) {
460 g_string_free(rel_path, TRUE);
461 }
462
463 if (rel_path_san) {
464 g_string_free(rel_path_san, TRUE);
465 }
466
467 if (full_path) {
468 g_string_free(full_path, TRUE);
469 }
470
471 return unique_full_path;
472}
473
15fe47e0
PP
474BT_HIDDEN
475void fs_sink_trace_destroy(struct fs_sink_trace *trace)
476{
477 GString *tsdl = NULL;
478 FILE *fh = NULL;
479 size_t len;
480
481 if (!trace) {
482 goto end;
483 }
484
485 if (trace->ir_trace_destruction_listener_id != UINT64_C(-1)) {
486 /*
487 * Remove the destruction listener, otherwise it could
488 * be called in the future, and its private data is this
489 * CTF FS sink trace object which won't exist anymore.
490 */
491 (void) bt_trace_remove_destruction_listener(trace->ir_trace,
492 trace->ir_trace_destruction_listener_id);
493 trace->ir_trace_destruction_listener_id = UINT64_C(-1);
494 }
495
496 if (trace->streams) {
497 g_hash_table_destroy(trace->streams);
498 trace->streams = NULL;
499 }
500
501 tsdl = g_string_new(NULL);
502 BT_ASSERT(tsdl);
335a2da5 503 translate_trace_ctf_ir_to_tsdl(trace->trace, tsdl);
20078f92
FD
504
505 BT_ASSERT(trace->metadata_path);
15fe47e0
PP
506 fh = fopen(trace->metadata_path->str, "wb");
507 if (!fh) {
aa1a7452 508 BT_COMP_LOGF_ERRNO("In trace destruction listener: "
c6eb0200 509 "cannot open metadata file for writing",
15fe47e0
PP
510 ": path=\"%s\"", trace->metadata_path->str);
511 abort();
512 }
513
514 len = fwrite(tsdl->str, sizeof(*tsdl->str), tsdl->len, fh);
515 if (len != tsdl->len) {
aa1a7452 516 BT_COMP_LOGF_ERRNO("In trace destruction listener: "
c6eb0200 517 "cannot write metadata file",
15fe47e0
PP
518 ": path=\"%s\"", trace->metadata_path->str);
519 abort();
520 }
521
522 if (!trace->fs_sink->quiet) {
523 printf("Created CTF trace `%s`.\n", trace->path->str);
524 }
525
526 if (trace->path) {
527 g_string_free(trace->path, TRUE);
528 trace->path = NULL;
529 }
530
15fe47e0
PP
531 if (fh) {
532 int ret = fclose(fh);
533
534 if (ret != 0) {
aa1a7452 535 BT_COMP_LOGW_ERRNO("In trace destruction listener: "
c6eb0200 536 "cannot close metadata file",
15fe47e0
PP
537 ": path=\"%s\"", trace->metadata_path->str);
538 }
539 }
540
3e83e4f2
FD
541 g_string_free(trace->metadata_path, TRUE);
542 trace->metadata_path = NULL;
543
544 fs_sink_ctf_trace_destroy(trace->trace);
545 trace->trace = NULL;
546 g_free(trace);
547
15fe47e0
PP
548 if (tsdl) {
549 g_string_free(tsdl, TRUE);
550 }
551
3e83e4f2 552end:
15fe47e0
PP
553 return;
554}
555
556static
557void ir_trace_destruction_listener(const bt_trace *ir_trace, void *data)
558{
559 struct fs_sink_trace *trace = data;
560
561 /*
562 * Prevent bt_trace_remove_destruction_listener() from being
563 * called in fs_sink_trace_destroy(), which is called by
564 * g_hash_table_remove() below.
565 */
566 trace->ir_trace_destruction_listener_id = UINT64_C(-1);
567 g_hash_table_remove(trace->fs_sink->traces, ir_trace);
568}
569
570BT_HIDDEN
571struct fs_sink_trace *fs_sink_trace_create(struct fs_sink_comp *fs_sink,
572 const bt_trace *ir_trace)
573{
574 int ret;
575 struct fs_sink_trace *trace = g_new0(struct fs_sink_trace, 1);
d24d5663 576 bt_trace_add_listener_status trace_status;
15fe47e0
PP
577
578 if (!trace) {
579 goto end;
580 }
581
ffa3b2b3 582 trace->log_level = fs_sink->log_level;
15fe47e0
PP
583 trace->fs_sink = fs_sink;
584 trace->ir_trace = ir_trace;
585 trace->ir_trace_destruction_listener_id = UINT64_C(-1);
335a2da5
PP
586 trace->trace = translate_trace_trace_ir_to_ctf_ir(fs_sink, ir_trace);
587 if (!trace->trace) {
15fe47e0
PP
588 goto error;
589 }
590
be38c486 591 trace->path = make_trace_path(trace, fs_sink->output_dir_path->str);
15fe47e0
PP
592 BT_ASSERT(trace->path);
593 ret = g_mkdir_with_parents(trace->path->str, 0755);
594 if (ret) {
aa1a7452 595 BT_COMP_LOGE_ERRNO("Cannot create directories for trace directory",
15fe47e0
PP
596 ": path=\"%s\"", trace->path->str);
597 goto error;
598 }
599
600 trace->metadata_path = g_string_new(trace->path->str);
601 BT_ASSERT(trace->metadata_path);
602 g_string_append(trace->metadata_path, "/metadata");
603 trace->streams = g_hash_table_new_full(g_direct_hash, g_direct_equal,
604 NULL, (GDestroyNotify) fs_sink_stream_destroy);
605 BT_ASSERT(trace->streams);
606 trace_status = bt_trace_add_destruction_listener(ir_trace,
607 ir_trace_destruction_listener, trace,
608 &trace->ir_trace_destruction_listener_id);
609 if (trace_status) {
610 goto error;
611 }
612
613 g_hash_table_insert(fs_sink->traces, (gpointer) ir_trace, trace);
614 goto end;
615
616error:
617 fs_sink_trace_destroy(trace);
618 trace = NULL;
619
620end:
621 return trace;
622}
This page took 0.064753 seconds and 4 git commands to generate.