sink.text.dmesg: remove `read-from-stdin` parameter, use absent `path`
[babeltrace.git] / plugins / text / dmesg / dmesg.c
CommitLineData
d8866baa
PP
1/*
2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
3 * Copyright 2017 Philippe Proulx <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
33ec5dfa
PP
24#define BT_LOG_TAG "PLUGIN-TEXT-DMESG-SRC"
25#include "logging.h"
26
d8866baa 27#include <stdbool.h>
33ec5dfa
PP
28#include <string.h>
29#include <ctype.h>
d8866baa
PP
30#include <stdio.h>
31#include <assert.h>
33ec5dfa
PP
32#include <babeltrace/babeltrace.h>
33#include <babeltrace/values-internal.h>
34#include <babeltrace/compat/utc-internal.h>
35#include <babeltrace/compat/stdio-internal.h>
d8866baa
PP
36#include <glib.h>
37
33ec5dfa
PP
38#define NSEC_PER_USEC 1000UL
39#define NSEC_PER_MSEC 1000000UL
40#define NSEC_PER_SEC 1000000000ULL
41#define USEC_PER_SEC 1000000UL
42
d8866baa
PP
43struct dmesg_component;
44
45struct dmesg_notif_iter {
46 struct dmesg_component *dmesg_comp;
33ec5dfa
PP
47 char *linebuf;
48 size_t linebuf_len;
d8866baa
PP
49 FILE *fp;
50};
51
52struct dmesg_component {
53 struct {
54 GString *path;
33ec5dfa 55 bt_bool read_from_stdin;
707b323a 56 bt_bool no_timestamp;
d8866baa
PP
57 } params;
58
50842bdc
PP
59 struct bt_trace *trace;
60 struct bt_stream_class *stream_class;
61 struct bt_event_class *event_class;
62 struct bt_stream *stream;
63 struct bt_packet *packet;
64 struct bt_clock_class *clock_class;
d8866baa
PP
65 struct bt_clock_class_priority_map *cc_prio_map;
66};
67
68static
50842bdc 69struct bt_field_type *create_packet_header_ft(void)
d8866baa 70{
50842bdc
PP
71 struct bt_field_type *root_ft = NULL;
72 struct bt_field_type *ft = NULL;
33ec5dfa
PP
73 int ret;
74
50842bdc 75 root_ft = bt_field_type_structure_create();
33ec5dfa
PP
76 if (!root_ft) {
77 BT_LOGE_STR("Cannot create an empty structure field type object.");
78 goto error;
79 }
80
50842bdc 81 ft = bt_field_type_integer_create(32);
33ec5dfa
PP
82 if (!ft) {
83 BT_LOGE_STR("Cannot create an integer field type object.");
84 goto error;
85 }
86
50842bdc 87 ret = bt_field_type_structure_add_field(root_ft, ft, "magic");
33ec5dfa
PP
88 if (ret) {
89 BT_LOGE("Cannot add `magic` field type to structure field type: "
90 "ret=%d", ret);
91 goto error;
92 }
93
94 BT_PUT(ft);
50842bdc 95 ft = bt_field_type_integer_create(8);
33ec5dfa
PP
96 if (!ft) {
97 BT_LOGE_STR("Cannot create an integer field type object.");
98 goto error;
99 }
100
101 goto end;
102
103error:
104 BT_PUT(root_ft);
105
106end:
107 bt_put(ft);
108 return root_ft;
109}
110
111static
50842bdc 112struct bt_field_type *create_packet_context_ft(void)
33ec5dfa 113{
50842bdc
PP
114 struct bt_field_type *root_ft = NULL;
115 struct bt_field_type *ft = NULL;
33ec5dfa
PP
116 int ret;
117
50842bdc 118 root_ft = bt_field_type_structure_create();
33ec5dfa
PP
119 if (!root_ft) {
120 BT_LOGE_STR("Cannot create an empty structure field type object.");
121 goto error;
122 }
123
50842bdc 124 ft = bt_field_type_integer_create(64);
33ec5dfa
PP
125 if (!ft) {
126 BT_LOGE_STR("Cannot create an integer field type object.");
127 goto error;
128 }
129
50842bdc 130 ret = bt_field_type_structure_add_field(root_ft,
33ec5dfa
PP
131 ft, "content_size");
132 if (ret) {
133 BT_LOGE("Cannot add `content_size` field type to structure field type: "
134 "ret=%d", ret);
135 goto error;
136 }
137
138 BT_PUT(ft);
50842bdc 139 ft = bt_field_type_integer_create(64);
33ec5dfa
PP
140 if (!ft) {
141 BT_LOGE_STR("Cannot create an integer field type object.");
142 goto error;
143 }
144
50842bdc 145 ret = bt_field_type_structure_add_field(root_ft,
33ec5dfa
PP
146 ft, "packet_size");
147 if (ret) {
148 BT_LOGE("Cannot add `packet_size` field type to structure field type: "
149 "ret=%d", ret);
150 goto error;
151 }
152
153 goto end;
154
155error:
156 BT_PUT(root_ft);
157
158end:
159 bt_put(ft);
160 return root_ft;
161}
162
163static
50842bdc
PP
164struct bt_field_type *create_event_header_ft(
165 struct bt_clock_class *clock_class)
33ec5dfa 166{
50842bdc
PP
167 struct bt_field_type *root_ft = NULL;
168 struct bt_field_type *ft = NULL;
33ec5dfa
PP
169 int ret;
170
50842bdc 171 root_ft = bt_field_type_structure_create();
33ec5dfa
PP
172 if (!root_ft) {
173 BT_LOGE_STR("Cannot create an empty structure field type object.");
174 goto error;
175 }
176
50842bdc 177 ft = bt_field_type_integer_create(64);
33ec5dfa
PP
178 if (!ft) {
179 BT_LOGE_STR("Cannot create an integer field type object.");
180 goto error;
181 }
182
50842bdc 183 ret = bt_field_type_integer_set_mapped_clock_class(ft, clock_class);
33ec5dfa
PP
184 if (ret) {
185 BT_LOGE("Cannot map integer field type to clock class: "
186 "ret=%d", ret);
187 goto error;
188 }
189
50842bdc 190 ret = bt_field_type_structure_add_field(root_ft,
33ec5dfa
PP
191 ft, "timestamp");
192 if (ret) {
193 BT_LOGE("Cannot add `timestamp` field type to structure field type: "
194 "ret=%d", ret);
195 goto error;
196 }
197
198 goto end;
199
200error:
201 BT_PUT(root_ft);
202
203end:
204 bt_put(ft);
205 return root_ft;
206}
207
208static
50842bdc 209struct bt_field_type *create_event_payload_ft(void)
33ec5dfa 210{
50842bdc
PP
211 struct bt_field_type *root_ft = NULL;
212 struct bt_field_type *ft = NULL;
33ec5dfa
PP
213 int ret;
214
50842bdc 215 root_ft = bt_field_type_structure_create();
33ec5dfa
PP
216 if (!root_ft) {
217 BT_LOGE_STR("Cannot create an empty structure field type object.");
218 goto error;
219 }
220
50842bdc 221 ft = bt_field_type_string_create();
33ec5dfa
PP
222 if (!ft) {
223 BT_LOGE_STR("Cannot create a string field type object.");
224 goto error;
225 }
226
50842bdc 227 ret = bt_field_type_structure_add_field(root_ft,
33ec5dfa
PP
228 ft, "str");
229 if (ret) {
230 BT_LOGE("Cannot add `str` field type to structure field type: "
231 "ret=%d", ret);
232 goto error;
233 }
234
235 goto end;
236
237error:
238 BT_PUT(root_ft);
239
240end:
241 bt_put(ft);
242 return root_ft;
243}
244
245static
50842bdc 246struct bt_clock_class *create_clock_class(void)
33ec5dfa 247{
50842bdc 248 return bt_clock_class_create("the_clock", 1000000000);
33ec5dfa
PP
249}
250
251static
252int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
253{
50842bdc 254 struct bt_field_type *ft = NULL;
33ec5dfa
PP
255 const char *trace_name = NULL;
256 gchar *basename = NULL;
d8866baa
PP
257 int ret = 0;
258
50842bdc 259 dmesg_comp->trace = bt_trace_create();
33ec5dfa
PP
260 if (!dmesg_comp->trace) {
261 BT_LOGE_STR("Cannot create an empty trace object.");
262 goto error;
263 }
264
265 ft = create_packet_header_ft();
266 if (!ft) {
267 BT_LOGE_STR("Cannot create packet header field type.");
268 goto error;
269 }
270
50842bdc 271 ret = bt_trace_set_packet_header_type(dmesg_comp->trace, ft);
33ec5dfa
PP
272 if (ret) {
273 BT_LOGE_STR("Cannot set trace's packet header field type.");
274 goto error;
275 }
276
277 if (dmesg_comp->params.read_from_stdin) {
278 trace_name = "STDIN";
279 } else {
280 basename = g_path_get_basename(dmesg_comp->params.path->str);
281 assert(basename);
282
283 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
284 strcmp(basename, ".") != 0) {
285 trace_name = basename;
286 }
287 }
288
289 if (trace_name) {
50842bdc 290 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
33ec5dfa
PP
291 if (ret) {
292 BT_LOGE("Cannot set trace's name: name=\"%s\"", trace_name);
293 goto error;
294 }
295 }
296
50842bdc 297 dmesg_comp->stream_class = bt_stream_class_create_empty(NULL);
33ec5dfa
PP
298 if (!dmesg_comp->stream_class) {
299 BT_LOGE_STR("Cannot create an empty stream class object.");
300 goto error;
301 }
302
303 bt_put(ft);
304 ft = create_packet_context_ft();
305 if (!ft) {
306 BT_LOGE_STR("Cannot create packet context field type.");
307 goto error;
308 }
309
50842bdc 310 ret = bt_stream_class_set_packet_context_type(
33ec5dfa
PP
311 dmesg_comp->stream_class, ft);
312 if (ret) {
313 BT_LOGE_STR("Cannot set stream class's packet context field type.");
314 goto error;
315 }
316
317 dmesg_comp->cc_prio_map = bt_clock_class_priority_map_create();
318 if (!dmesg_comp->cc_prio_map) {
319 BT_LOGE_STR("Cannot create empty clock class priority map.");
320 goto error;
321 }
322
323 if (has_ts) {
324 dmesg_comp->clock_class = create_clock_class();
325 if (!dmesg_comp->clock_class) {
326 BT_LOGE_STR("Cannot create clock class.");
327 goto error;
328 }
329
50842bdc 330 ret = bt_trace_add_clock_class(dmesg_comp->trace,
33ec5dfa
PP
331 dmesg_comp->clock_class);
332 if (ret) {
333 BT_LOGE_STR("Cannot add clock class to trace.");
334 goto error;
335 }
336
337 ret = bt_clock_class_priority_map_add_clock_class(
338 dmesg_comp->cc_prio_map, dmesg_comp->clock_class, 0);
339 if (ret) {
340 BT_LOGE_STR("Cannot add clock class to clock class priority map.");
341 goto error;
342 }
343
344 bt_put(ft);
345 ft = create_event_header_ft(dmesg_comp->clock_class);
346 if (!ft) {
347 BT_LOGE_STR("Cannot create event header field type.");
348 goto error;
349 }
350
50842bdc 351 ret = bt_stream_class_set_event_header_type(
33ec5dfa
PP
352 dmesg_comp->stream_class, ft);
353 if (ret) {
354 BT_LOGE_STR("Cannot set stream class's event header field type.");
355 goto error;
356 }
357 }
358
50842bdc 359 dmesg_comp->event_class = bt_event_class_create("string");
33ec5dfa
PP
360 if (!dmesg_comp->event_class) {
361 BT_LOGE_STR("Cannot create an empty event class object.");
362 goto error;
363 }
364
365 bt_put(ft);
366 ft = create_event_payload_ft();
367 if (!ft) {
368 BT_LOGE_STR("Cannot create event payload field type.");
d8866baa
PP
369 goto error;
370 }
371
50842bdc 372 ret = bt_event_class_set_payload_type(dmesg_comp->event_class, ft);
33ec5dfa
PP
373 if (ret) {
374 BT_LOGE_STR("Cannot set event class's event payload field type.");
375 goto error;
376 }
377
50842bdc 378 ret = bt_stream_class_add_event_class(dmesg_comp->stream_class,
33ec5dfa
PP
379 dmesg_comp->event_class);
380 if (ret) {
381 BT_LOGE("Cannot add event class to stream class: ret=%d", ret);
382 goto error;
383 }
384
50842bdc 385 ret = bt_trace_add_stream_class(dmesg_comp->trace,
33ec5dfa
PP
386 dmesg_comp->stream_class);
387 if (ret) {
388 BT_LOGE("Cannot add event class to stream class: ret=%d", ret);
389 goto error;
390 }
391
392 goto end;
393
394error:
395 ret = -1;
396
397end:
398 bt_put(ft);
399
400 if (basename) {
401 g_free(basename);
402 }
403
404 return ret;
405}
406
407static
408int handle_params(struct dmesg_component *dmesg_comp, struct bt_value *params)
409{
410 struct bt_value *read_from_stdin = NULL;
707b323a 411 struct bt_value *no_timestamp = NULL;
33ec5dfa
PP
412 struct bt_value *path = NULL;
413 const char *path_str;
414 int ret = 0;
415
707b323a
PP
416 no_timestamp = bt_value_map_get(params, "no-extract-timestamp");
417 if (no_timestamp) {
418 if (!bt_value_is_bool(no_timestamp)) {
419 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
420 "type=%s",
421 bt_value_type_string(
422 bt_value_get_type(no_timestamp)));
423 goto error;
424 }
425
426 ret = bt_value_bool_get(no_timestamp,
427 &dmesg_comp->params.no_timestamp);
428 assert(ret == 0);
429 }
430
d8866baa
PP
431 path = bt_value_map_get(params, "path");
432 if (path) {
433 if (dmesg_comp->params.read_from_stdin) {
33ec5dfa 434 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
d8866baa
PP
435 goto error;
436 }
437
438 if (!bt_value_is_string(path)) {
33ec5dfa
PP
439 BT_LOGE("Expecting a string value for the `path` parameter: "
440 "type=%s",
441 bt_value_type_string(
442 bt_value_get_type(path)));
d8866baa
PP
443 goto error;
444 }
445
33ec5dfa 446 ret = bt_value_string_get(path, &path_str);
d8866baa
PP
447 assert(ret == 0);
448 g_string_assign(dmesg_comp->params.path, path_str);
449 } else {
4da23e31 450 dmesg_comp->params.read_from_stdin = true;
d8866baa
PP
451 }
452
453 goto end;
454
455error:
456 ret = -1;
457
458end:
459 bt_put(read_from_stdin);
460 bt_put(path);
707b323a 461 bt_put(no_timestamp);
d8866baa
PP
462 return ret;
463}
464
465static
50842bdc 466struct bt_field *create_packet_header_field(struct bt_field_type *ft)
33ec5dfa 467{
50842bdc
PP
468 struct bt_field *ph = NULL;
469 struct bt_field *magic = NULL;
33ec5dfa
PP
470 int ret;
471
50842bdc 472 ph = bt_field_create(ft);
33ec5dfa
PP
473 if (!ph) {
474 BT_LOGE_STR("Cannot create field object.");
475 goto error;
476 }
477
50842bdc 478 magic = bt_field_structure_get_field_by_name(ph, "magic");
33ec5dfa
PP
479 if (!magic) {
480 BT_LOGE_STR("Cannot get `magic` field from structure field.");
481 goto error;
482 }
483
50842bdc 484 ret = bt_field_unsigned_integer_set_value(magic, 0xc1fc1fc1);
33ec5dfa
PP
485 if (ret) {
486 BT_LOGE_STR("Cannot set integer field's value.");
487 goto error;
488 }
489
490 goto end;
491
492error:
493 BT_PUT(ph);
494
495end:
496 bt_put(magic);
497 return ph;
498}
499
500static
50842bdc 501struct bt_field *create_packet_context_field(struct bt_field_type *ft)
33ec5dfa 502{
50842bdc
PP
503 struct bt_field *pc = NULL;
504 struct bt_field *field = NULL;
33ec5dfa
PP
505 int ret;
506
50842bdc 507 pc = bt_field_create(ft);
33ec5dfa
PP
508 if (!pc) {
509 BT_LOGE_STR("Cannot create field object.");
510 goto error;
511 }
512
50842bdc 513 field = bt_field_structure_get_field_by_name(pc, "content_size");
33ec5dfa
PP
514 if (!field) {
515 BT_LOGE_STR("Cannot get `content_size` field from structure field.");
516 goto error;
517 }
518
50842bdc 519 ret = bt_field_unsigned_integer_set_value(field, 0);
33ec5dfa
PP
520 if (ret) {
521 BT_LOGE_STR("Cannot set integer field's value.");
522 goto error;
523 }
524
525 bt_put(field);
50842bdc 526 field = bt_field_structure_get_field_by_name(pc, "packet_size");
33ec5dfa
PP
527 if (!field) {
528 BT_LOGE_STR("Cannot get `packet_size` field from structure field.");
529 goto error;
530 }
531
50842bdc 532 ret = bt_field_unsigned_integer_set_value(field, 0);
33ec5dfa
PP
533 if (ret) {
534 BT_LOGE_STR("Cannot set integer field's value.");
535 goto error;
536 }
537
538 goto end;
539
540error:
541 BT_PUT(pc);
542
543end:
544 bt_put(field);
545 return pc;
546}
547
548static
549int create_packet_and_stream(struct dmesg_component *dmesg_comp)
550{
551 int ret = 0;
50842bdc
PP
552 struct bt_field_type *ft = NULL;
553 struct bt_field *field = NULL;
33ec5dfa 554
50842bdc 555 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class,
33ec5dfa
PP
556 NULL);
557 if (!dmesg_comp->stream) {
558 BT_LOGE_STR("Cannot create stream object.");
559 goto error;
560 }
561
50842bdc 562 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream);
33ec5dfa
PP
563 if (!dmesg_comp->packet) {
564 BT_LOGE_STR("Cannot create packet object.");
565 goto error;
566 }
567
50842bdc 568 ft = bt_trace_get_packet_header_type(dmesg_comp->trace);
33ec5dfa
PP
569 assert(ft);
570 field = create_packet_header_field(ft);
571 if (!field) {
572 BT_LOGE_STR("Cannot create packet header field.");
573 goto error;
574 }
575
50842bdc 576 ret = bt_packet_set_header(dmesg_comp->packet, field);
33ec5dfa
PP
577 if (ret) {
578 BT_LOGE_STR("Cannot set packet's header field.");
579 goto error;
580 }
581
582 bt_put(ft);
583 bt_put(field);
50842bdc 584 ft = bt_stream_class_get_packet_context_type(
33ec5dfa
PP
585 dmesg_comp->stream_class);
586 assert(ft);
587 field = create_packet_context_field(ft);
588 if (!field) {
589 BT_LOGE_STR("Cannot create packet context field.");
590 goto error;
591 }
592
50842bdc 593 ret = bt_packet_set_context(dmesg_comp->packet, field);
33ec5dfa
PP
594 if (ret) {
595 BT_LOGE_STR("Cannot set packet's context field.");
596 goto error;
597 }
598
50842bdc 599 ret = bt_trace_set_is_static(dmesg_comp->trace);
33ec5dfa
PP
600 if (ret) {
601 BT_LOGE_STR("Cannot make trace static.");
602 goto error;
603 }
604
605 goto end;
606
607error:
608 ret = -1;
609
610end:
611 bt_put(field);
612 bt_put(ft);
613 return ret;
614}
615
616static
617int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp,
618 bool has_ts)
619{
620 int ret = 0;
621
622 if (dmesg_comp->trace) {
623 /* Already created */
624 goto end;
625 }
626
627 ret = create_meta(dmesg_comp, has_ts);
628 if (ret) {
629 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
630 dmesg_comp);
631 goto error;
632 }
633
634 ret = create_packet_and_stream(dmesg_comp);
635 if (ret) {
636 BT_LOGE("Cannot create packet and stream objects: "
637 "dmesg-comp-addr=%p", dmesg_comp);
638 goto error;
639 }
640
641 goto end;
642
643error:
644 ret = -1;
645
646end:
647 return ret;
648}
d8866baa
PP
649
650static
651void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
652{
653 if (!dmesg_comp) {
654 return;
655 }
656
657 if (dmesg_comp->params.path) {
658 g_string_free(dmesg_comp->params.path, TRUE);
659 }
660
661 bt_put(dmesg_comp->packet);
33ec5dfa
PP
662 bt_put(dmesg_comp->trace);
663 bt_put(dmesg_comp->stream_class);
d8866baa
PP
664 bt_put(dmesg_comp->event_class);
665 bt_put(dmesg_comp->stream);
33ec5dfa 666 bt_put(dmesg_comp->clock_class);
d8866baa
PP
667 bt_put(dmesg_comp->cc_prio_map);
668 g_free(dmesg_comp);
669}
670
33ec5dfa
PP
671static
672enum bt_component_status create_port(struct bt_private_component *priv_comp)
673{
674 return bt_private_component_source_add_output_private_port(priv_comp,
675 "out", NULL, NULL);
676}
677
d8866baa
PP
678BT_HIDDEN
679enum bt_component_status dmesg_init(struct bt_private_component *priv_comp,
680 struct bt_value *params, void *init_method_data)
681{
682 int ret = 0;
683 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
684 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
685
686 if (!dmesg_comp) {
33ec5dfa 687 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
d8866baa
PP
688 goto error;
689 }
690
691 dmesg_comp->params.path = g_string_new(NULL);
692 if (!dmesg_comp->params.path) {
33ec5dfa 693 BT_LOGE_STR("Failed to allocate a GString.");
d8866baa
PP
694 goto error;
695 }
696
33ec5dfa 697 ret = handle_params(dmesg_comp, params);
d8866baa 698 if (ret) {
33ec5dfa 699 BT_LOGE("Invalid parameters: comp-addr=%p", priv_comp);
d8866baa
PP
700 goto error;
701 }
702
33ec5dfa
PP
703 if (!dmesg_comp->params.read_from_stdin &&
704 !g_file_test(dmesg_comp->params.path->str,
705 G_FILE_TEST_IS_REGULAR)) {
706 BT_LOGE("Input path is not a regular file: "
707 "comp-addr=%p, path=\"%s\"", priv_comp,
708 dmesg_comp->params.path->str);
709 goto error;
710 }
d8866baa 711
33ec5dfa
PP
712 status = create_port(priv_comp);
713 if (status != BT_COMPONENT_STATUS_OK) {
714 goto error;
715 }
d8866baa 716
33ec5dfa 717 (void) bt_private_component_set_user_data(priv_comp, dmesg_comp);
d8866baa
PP
718 goto end;
719
720error:
721 destroy_dmesg_component(dmesg_comp);
33ec5dfa
PP
722 (void) bt_private_component_set_user_data(priv_comp, NULL);
723
724 if (status >= 0) {
725 status = BT_COMPONENT_STATUS_ERROR;
726 }
d8866baa
PP
727
728end:
729 return status;
730}
731
732BT_HIDDEN
733void dmesg_finalize(struct bt_private_component *priv_comp)
734{
33ec5dfa
PP
735 void *data = bt_private_component_get_user_data(priv_comp);
736
737 destroy_dmesg_component(data);
738}
739
740static
741int create_event_header_from_line(
742 struct dmesg_component *dmesg_comp,
743 const char *line, const char **new_start,
50842bdc
PP
744 struct bt_field **user_field,
745 struct bt_clock_value **user_clock_value)
33ec5dfa
PP
746{
747 bool has_timestamp = false;
748 unsigned long sec, usec, msec;
749 unsigned int year, mon, mday, hour, min;
750 uint64_t ts = 0;
50842bdc
PP
751 struct bt_clock_value *clock_value = NULL;
752 struct bt_field_type *ft = NULL;
753 struct bt_field *eh_field = NULL;
754 struct bt_field *ts_field = NULL;
33ec5dfa
PP
755 int ret = 0;
756
757 assert(user_clock_value);
758 assert(user_field);
759 *new_start = line;
760
707b323a
PP
761 if (dmesg_comp->params.no_timestamp) {
762 goto skip_ts;
763 }
764
33ec5dfa
PP
765 /* Extract time from input line */
766 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
767 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
768
769 /*
770 * The clock class we use has a 1 GHz frequency: convert
771 * from µs to ns.
772 */
773 ts *= NSEC_PER_USEC;
774 has_timestamp = true;
775 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
776 &year, &mon, &mday, &hour, &min,
777 &sec, &msec) == 7) {
778 time_t ep_sec;
779 struct tm ti;
780
781 memset(&ti, 0, sizeof(ti));
782 ti.tm_year = year - 1900; /* From 1900 */
783 ti.tm_mon = mon - 1; /* 0 to 11 */
784 ti.tm_mday = mday;
785 ti.tm_hour = hour;
786 ti.tm_min = min;
787 ti.tm_sec = sec;
788
789 ep_sec = bt_timegm(&ti);
790 if (ep_sec != (time_t) -1) {
791 ts = (uint64_t) ep_sec * NSEC_PER_SEC
792 + (uint64_t) msec * NSEC_PER_MSEC;
793 }
794
795 has_timestamp = true;
796 }
797
798 if (has_timestamp) {
799 /* Set new start for the message portion of the line */
800 *new_start = strchr(line, ']');
801 assert(*new_start);
802 (*new_start)++;
803
804 if ((*new_start)[0] == ' ') {
805 (*new_start)++;
806 }
807 }
808
707b323a 809skip_ts:
33ec5dfa
PP
810 /*
811 * At this point, we know if the stream class's event header
812 * field type should have a timestamp or not, so we can lazily
813 * create the metadata, stream, and packet objects.
814 */
815 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
816 if (ret) {
817 /* try_create_meta_stream_packet() logs errors */
818 goto error;
819 }
820
821 if (dmesg_comp->clock_class) {
50842bdc 822 clock_value = bt_clock_value_create(dmesg_comp->clock_class,
33ec5dfa
PP
823 ts);
824 if (!clock_value) {
825 BT_LOGE_STR("Cannot create clock value object.");
826 goto error;
827 }
828
50842bdc 829 ft = bt_stream_class_get_event_header_type(
33ec5dfa
PP
830 dmesg_comp->stream_class);
831 assert(ft);
50842bdc 832 eh_field = bt_field_create(ft);
33ec5dfa
PP
833 if (!eh_field) {
834 BT_LOGE_STR("Cannot create event header field object.");
835 goto error;
836 }
837
50842bdc 838 ts_field = bt_field_structure_get_field_by_name(eh_field,
33ec5dfa
PP
839 "timestamp");
840 if (!ts_field) {
841 BT_LOGE_STR("Cannot get `timestamp` field from structure field.");
842 goto error;
843 }
844
50842bdc 845 ret = bt_field_unsigned_integer_set_value(ts_field, ts);
33ec5dfa
PP
846 if (ret) {
847 BT_LOGE_STR("Cannot set integer field's value.");
848 goto error;
849 }
850
851 *user_clock_value = clock_value;
852 clock_value = NULL;
853 *user_field = eh_field;
854 eh_field = NULL;
855 }
856
857 goto end;
858
859error:
860 ret = -1;
861
862end:
863 bt_put(ft);
864 bt_put(ts_field);
865 bt_put(clock_value);
866 bt_put(eh_field);
867 return ret;
868}
869
870static
871int create_event_payload_from_line(
872 struct dmesg_component *dmesg_comp,
50842bdc 873 const char *line, struct bt_field **user_field)
33ec5dfa 874{
50842bdc
PP
875 struct bt_field_type *ft = NULL;
876 struct bt_field *ep_field = NULL;
877 struct bt_field *str_field = NULL;
33ec5dfa
PP
878 size_t len;
879 int ret;
880
881 assert(user_field);
50842bdc 882 ft = bt_event_class_get_payload_type(dmesg_comp->event_class);
33ec5dfa 883 assert(ft);
50842bdc 884 ep_field = bt_field_create(ft);
33ec5dfa
PP
885 if (!ep_field) {
886 BT_LOGE_STR("Cannot create event payload field object.");
887 goto error;
888 }
889
50842bdc 890 str_field = bt_field_structure_get_field_by_name(ep_field, "str");
33ec5dfa
PP
891 if (!str_field) {
892 BT_LOGE_STR("Cannot get `timestamp` field from structure field.");
893 goto error;
894 }
895
896 len = strlen(line);
897 if (line[len - 1] == '\n') {
898 /* Do not include the newline character in the payload */
899 len--;
900 }
901
50842bdc 902 ret = bt_field_string_append_len(str_field, line, len);
33ec5dfa
PP
903 if (ret) {
904 BT_LOGE("Cannot append value to string field object: "
905 "len=%zu", len);
906 goto error;
907 }
908
909 *user_field = ep_field;
910 ep_field = NULL;
911 goto end;
912
913error:
914 ret = -1;
915
916end:
917 bt_put(ft);
918 bt_put(ep_field);
919 bt_put(str_field);
920 return ret;
921}
922
923static
924struct bt_notification *create_notif_from_line(
925 struct dmesg_component *dmesg_comp, const char *line)
926{
50842bdc
PP
927 struct bt_field *eh_field = NULL;
928 struct bt_field *ep_field = NULL;
929 struct bt_clock_value *clock_value = NULL;
930 struct bt_event *event = NULL;
33ec5dfa
PP
931 struct bt_notification *notif = NULL;
932 const char *new_start;
933 int ret;
934
935 ret = create_event_header_from_line(dmesg_comp, line, &new_start,
936 &eh_field, &clock_value);
937 if (ret) {
938 BT_LOGE("Cannot create event header field from line: "
939 "ret=%d", ret);
940 goto error;
941 }
942
943 ret = create_event_payload_from_line(dmesg_comp, new_start,
944 &ep_field);
945 if (ret) {
946 BT_LOGE("Cannot create event payload field from line: "
947 "ret=%d", ret);
948 goto error;
949 }
950
951 assert(ep_field);
50842bdc 952 event = bt_event_create(dmesg_comp->event_class);
33ec5dfa
PP
953 if (!event) {
954 BT_LOGE_STR("Cannot create event object.");
955 goto error;
956 }
957
50842bdc 958 ret = bt_event_set_packet(event, dmesg_comp->packet);
33ec5dfa
PP
959 if (ret) {
960 BT_LOGE_STR("Cannot set event's packet.");
961 goto error;
962 }
963
964 if (eh_field) {
50842bdc 965 ret = bt_event_set_header(event, eh_field);
33ec5dfa
PP
966 if (ret) {
967 BT_LOGE_STR("Cannot set event's header field.");
968 goto error;
969 }
970 }
971
50842bdc 972 ret = bt_event_set_event_payload(event, ep_field);
33ec5dfa
PP
973 if (ret) {
974 BT_LOGE_STR("Cannot set event's payload field.");
975 goto error;
976 }
977
978 if (clock_value) {
50842bdc 979 ret = bt_event_set_clock_value(event, clock_value);
33ec5dfa
PP
980 if (ret) {
981 BT_LOGE_STR("Cannot set event's clock value.");
982 goto error;
983 }
984 }
985
986 notif = bt_notification_event_create(event, dmesg_comp->cc_prio_map);
987 if (!notif) {
988 BT_LOGE_STR("Cannot create event notification.");
989 goto error;
990 }
991
992 goto end;
993
994error:
995 BT_PUT(notif);
996
997end:
998 bt_put(eh_field);
999 bt_put(ep_field);
1000 bt_put(clock_value);
1001 bt_put(event);
1002 return notif;
1003}
1004
1005static
1006void destroy_dmesg_notif_iter(struct dmesg_notif_iter *dmesg_notif_iter)
1007{
1008 if (!dmesg_notif_iter) {
1009 return;
1010 }
1011
1012 if (dmesg_notif_iter->fp && dmesg_notif_iter->fp != stdin) {
1013 if (fclose(dmesg_notif_iter->fp)) {
1014 BT_LOGE_ERRNO("Cannot close input file", ".");
1015 }
1016 }
d8866baa 1017
33ec5dfa
PP
1018 free(dmesg_notif_iter->linebuf);
1019 g_free(dmesg_notif_iter);
d8866baa
PP
1020}
1021
1022BT_HIDDEN
1023enum bt_notification_iterator_status dmesg_notif_iter_init(
90157d89 1024 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
d8866baa
PP
1025 struct bt_private_port *priv_port)
1026{
33ec5dfa
PP
1027 struct bt_private_component *priv_comp = NULL;
1028 struct dmesg_component *dmesg_comp;
1029 struct dmesg_notif_iter *dmesg_notif_iter =
1030 g_new0(struct dmesg_notif_iter, 1);
1031 enum bt_notification_iterator_status status =
1032 BT_NOTIFICATION_ITERATOR_STATUS_OK;
1033
1034 if (!dmesg_notif_iter) {
1035 BT_LOGE_STR("Failed to allocate on dmesg notification iterator structure.");
1036 goto error;
1037 }
1038
90157d89 1039 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
33ec5dfa
PP
1040 priv_notif_iter);
1041 assert(priv_comp);
1042 dmesg_comp = bt_private_component_get_user_data(priv_comp);
1043 assert(dmesg_comp);
1044 dmesg_notif_iter->dmesg_comp = dmesg_comp;
d8866baa 1045
33ec5dfa
PP
1046 if (dmesg_comp->params.read_from_stdin) {
1047 dmesg_notif_iter->fp = stdin;
1048 } else {
1049 dmesg_notif_iter->fp = fopen(dmesg_comp->params.path->str, "r");
1050 if (!dmesg_notif_iter->fp) {
1051 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
1052 dmesg_comp->params.path->str);
1053 goto error;
1054 }
1055 }
1056
90157d89 1057 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
33ec5dfa
PP
1058 dmesg_notif_iter);
1059 goto end;
1060
1061error:
1062 destroy_dmesg_notif_iter(dmesg_notif_iter);
90157d89 1063 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
33ec5dfa
PP
1064 NULL);
1065 if (status >= 0) {
1066 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1067 }
1068
1069end:
1070 bt_put(priv_comp);
1071 return status;
d8866baa
PP
1072}
1073
1074BT_HIDDEN
33ec5dfa 1075void dmesg_notif_iter_finalize(
90157d89 1076 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
d8866baa 1077{
90157d89 1078 destroy_dmesg_notif_iter(bt_private_connection_private_notification_iterator_get_user_data(
33ec5dfa 1079 priv_notif_iter));
d8866baa
PP
1080}
1081
1082BT_HIDDEN
90157d89
PP
1083struct bt_notification_iterator_next_method_return dmesg_notif_iter_next(
1084 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
d8866baa 1085{
33ec5dfa
PP
1086 ssize_t len;
1087 struct dmesg_notif_iter *dmesg_notif_iter =
90157d89 1088 bt_private_connection_private_notification_iterator_get_user_data(
33ec5dfa
PP
1089 priv_notif_iter);
1090 struct dmesg_component *dmesg_comp;
90157d89 1091 struct bt_notification_iterator_next_method_return next_ret = {
33ec5dfa
PP
1092 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
1093 .notification = NULL
1094 };
d8866baa 1095
33ec5dfa
PP
1096 assert(dmesg_notif_iter);
1097 dmesg_comp = dmesg_notif_iter->dmesg_comp;
1098 assert(dmesg_comp);
1099
1100 while (true) {
1101 const char *ch;
1102 bool only_spaces = true;
1103
1104 len = bt_getline(&dmesg_notif_iter->linebuf,
1105 &dmesg_notif_iter->linebuf_len, dmesg_notif_iter->fp);
1106 if (len < 0) {
1107 if (errno == EINVAL) {
1108 next_ret.status =
1109 BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1110 } else if (errno == ENOMEM) {
1111 next_ret.status =
1112 BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
1113 } else {
1114 next_ret.status =
1115 BT_NOTIFICATION_ITERATOR_STATUS_END;
1116 }
1117
1118 goto end;
1119 }
1120
1121 assert(dmesg_notif_iter->linebuf);
1122
1123 /* Ignore empty lines, once trimmed */
1124 for (ch = dmesg_notif_iter->linebuf; *ch != '\0'; ch++) {
1125 if (!isspace(*ch)) {
1126 only_spaces = false;
1127 break;
1128 }
1129 }
1130
1131 if (!only_spaces) {
1132 break;
1133 }
1134 }
1135
1136 next_ret.notification = create_notif_from_line(dmesg_comp,
1137 dmesg_notif_iter->linebuf);
1138 if (!next_ret.notification) {
1139 BT_LOGE("Cannot create event notification from line: "
1140 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
1141 dmesg_notif_iter->linebuf);
1142 }
1143
1144end:
1145 return next_ret;
d8866baa 1146}
This page took 0.078274 seconds and 4 git commands to generate.