2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #define BT_LOG_TAG "PLUGIN-TEXT-DMESG-SRC"
31 #include <babeltrace/assert-internal.h>
32 #include <babeltrace/common-internal.h>
33 #include <babeltrace/babeltrace.h>
34 #include <babeltrace/values-internal.h>
35 #include <babeltrace/compat/utc-internal.h>
36 #include <babeltrace/compat/stdio-internal.h>
39 #define NSEC_PER_USEC 1000UL
40 #define NSEC_PER_MSEC 1000000UL
41 #define NSEC_PER_SEC 1000000000ULL
42 #define USEC_PER_SEC 1000000UL
44 struct dmesg_component
;
46 struct dmesg_notif_iter
{
47 struct dmesg_component
*dmesg_comp
;
48 struct bt_self_notification_iterator
*pc_notif_iter
; /* Weak */
52 struct bt_private_notification
*tmp_event_notif
;
55 STATE_EMIT_STREAM_BEGINNING
,
56 STATE_EMIT_PACKET_BEGINNING
,
58 STATE_EMIT_PACKET_END
,
59 STATE_EMIT_STREAM_END
,
64 struct dmesg_component
{
67 bt_bool read_from_stdin
;
71 struct bt_private_trace
*trace
;
72 struct bt_private_stream_class
*stream_class
;
73 struct bt_private_event_class
*event_class
;
74 struct bt_private_stream
*stream
;
75 struct bt_private_packet
*packet
;
76 struct bt_private_clock_class
*clock_class
;
80 struct bt_private_field_class
*create_event_payload_fc(void)
82 struct bt_private_field_class
*root_fc
= NULL
;
83 struct bt_private_field_class
*fc
= NULL
;
86 root_fc
= bt_private_field_class_structure_create();
88 BT_LOGE_STR("Cannot create an empty structure field class object.");
92 fc
= bt_private_field_class_string_create();
94 BT_LOGE_STR("Cannot create a string field class object.");
98 ret
= bt_private_field_class_structure_append_member(root_fc
,
101 BT_LOGE("Cannot add `str` member to structure field class: "
109 BT_OBJECT_PUT_REF_AND_RESET(root_fc
);
112 bt_object_put_ref(fc
);
117 int create_meta(struct dmesg_component
*dmesg_comp
, bool has_ts
)
119 struct bt_private_field_class
*fc
= NULL
;
120 const char *trace_name
= NULL
;
121 gchar
*basename
= NULL
;
124 dmesg_comp
->trace
= bt_private_trace_create();
125 if (!dmesg_comp
->trace
) {
126 BT_LOGE_STR("Cannot create an empty trace object.");
130 if (dmesg_comp
->params
.read_from_stdin
) {
131 trace_name
= "STDIN";
133 basename
= g_path_get_basename(dmesg_comp
->params
.path
->str
);
136 if (strcmp(basename
, G_DIR_SEPARATOR_S
) != 0 &&
137 strcmp(basename
, ".") != 0) {
138 trace_name
= basename
;
143 ret
= bt_private_trace_set_name(dmesg_comp
->trace
, trace_name
);
145 BT_LOGE("Cannot set trace's name: name=\"%s\"", trace_name
);
150 dmesg_comp
->stream_class
= bt_private_stream_class_create(
152 if (!dmesg_comp
->stream_class
) {
153 BT_LOGE_STR("Cannot create a stream class object.");
158 dmesg_comp
->clock_class
= bt_private_clock_class_create();
159 if (!dmesg_comp
->clock_class
) {
160 BT_LOGE_STR("Cannot create clock class.");
164 ret
= bt_private_stream_class_set_default_clock_class(
165 dmesg_comp
->stream_class
,
166 bt_private_clock_class_borrow_clock_class(
167 dmesg_comp
->clock_class
));
169 BT_LOGE_STR("Cannot set stream class's default clock class.");
174 dmesg_comp
->event_class
= bt_private_event_class_create(
175 dmesg_comp
->stream_class
);
176 if (!dmesg_comp
->event_class
) {
177 BT_LOGE_STR("Cannot create an event class object.");
181 ret
= bt_private_event_class_set_name(dmesg_comp
->event_class
, "string");
183 BT_LOGE_STR("Cannot set event class's name.");
187 fc
= create_event_payload_fc();
189 BT_LOGE_STR("Cannot create event payload field class.");
193 ret
= bt_private_event_class_set_payload_field_class(dmesg_comp
->event_class
, fc
);
195 BT_LOGE_STR("Cannot set event class's event payload field class.");
205 bt_object_put_ref(fc
);
215 int handle_params(struct dmesg_component
*dmesg_comp
, struct bt_value
*params
)
217 struct bt_value
*no_timestamp
= NULL
;
218 struct bt_value
*path
= NULL
;
219 const char *path_str
;
222 no_timestamp
= bt_value_map_borrow_entry_value(params
,
223 "no-extract-timestamp");
225 if (!bt_value_is_bool(no_timestamp
)) {
226 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
228 bt_common_value_type_string(
229 bt_value_get_type(no_timestamp
)));
233 dmesg_comp
->params
.no_timestamp
=
234 bt_value_bool_get(no_timestamp
);
237 path
= bt_value_map_borrow_entry_value(params
, "path");
239 if (dmesg_comp
->params
.read_from_stdin
) {
240 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
244 if (!bt_value_is_string(path
)) {
245 BT_LOGE("Expecting a string value for the `path` parameter: "
247 bt_common_value_type_string(
248 bt_value_get_type(path
)));
252 path_str
= bt_value_string_get(path
);
253 g_string_assign(dmesg_comp
->params
.path
, path_str
);
255 dmesg_comp
->params
.read_from_stdin
= true;
268 int create_packet_and_stream(struct dmesg_component
*dmesg_comp
)
272 dmesg_comp
->stream
= bt_private_stream_create(dmesg_comp
->stream_class
);
273 if (!dmesg_comp
->stream
) {
274 BT_LOGE_STR("Cannot create stream object.");
278 dmesg_comp
->packet
= bt_private_packet_create(dmesg_comp
->stream
);
279 if (!dmesg_comp
->packet
) {
280 BT_LOGE_STR("Cannot create packet object.");
284 ret
= bt_private_trace_make_static(dmesg_comp
->trace
);
286 BT_LOGE_STR("Cannot make trace static.");
300 int try_create_meta_stream_packet(struct dmesg_component
*dmesg_comp
,
305 if (dmesg_comp
->trace
) {
306 /* Already created */
310 ret
= create_meta(dmesg_comp
, has_ts
);
312 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
317 ret
= create_packet_and_stream(dmesg_comp
);
319 BT_LOGE("Cannot create packet and stream objects: "
320 "dmesg-comp-addr=%p", dmesg_comp
);
334 void destroy_dmesg_component(struct dmesg_component
*dmesg_comp
)
340 if (dmesg_comp
->params
.path
) {
341 g_string_free(dmesg_comp
->params
.path
, TRUE
);
344 bt_object_put_ref(dmesg_comp
->packet
);
345 bt_object_put_ref(dmesg_comp
->trace
);
346 bt_object_put_ref(dmesg_comp
->stream_class
);
347 bt_object_put_ref(dmesg_comp
->event_class
);
348 bt_object_put_ref(dmesg_comp
->stream
);
349 bt_object_put_ref(dmesg_comp
->clock_class
);
354 enum bt_self_component_status
create_port(
355 struct bt_self_component_source
*self_comp
)
357 return bt_self_component_source_add_output_port(self_comp
,
362 enum bt_self_component_status
dmesg_init(
363 struct bt_self_component_source
*self_comp
,
364 struct bt_value
*params
, void *init_method_data
)
367 struct dmesg_component
*dmesg_comp
= g_new0(struct dmesg_component
, 1);
368 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
371 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
375 dmesg_comp
->params
.path
= g_string_new(NULL
);
376 if (!dmesg_comp
->params
.path
) {
377 BT_LOGE_STR("Failed to allocate a GString.");
381 ret
= handle_params(dmesg_comp
, params
);
383 BT_LOGE("Invalid parameters: comp-addr=%p", self_comp
);
387 if (!dmesg_comp
->params
.read_from_stdin
&&
388 !g_file_test(dmesg_comp
->params
.path
->str
,
389 G_FILE_TEST_IS_REGULAR
)) {
390 BT_LOGE("Input path is not a regular file: "
391 "comp-addr=%p, path=\"%s\"", self_comp
,
392 dmesg_comp
->params
.path
->str
);
396 status
= create_port(self_comp
);
397 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
401 bt_self_component_set_data(
402 bt_self_component_source_borrow_self_component(self_comp
),
407 destroy_dmesg_component(dmesg_comp
);
408 bt_self_component_set_data(
409 bt_self_component_source_borrow_self_component(self_comp
),
413 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
421 void dmesg_finalize(struct bt_self_component_source
*self_comp
)
423 destroy_dmesg_component(bt_self_component_get_data(
424 bt_self_component_source_borrow_self_component(self_comp
)));
428 struct bt_private_notification
*create_init_event_notif_from_line(
429 struct dmesg_notif_iter
*notif_iter
,
430 const char *line
, const char **new_start
)
432 struct bt_private_event
*event
;
433 struct bt_private_notification
*notif
= NULL
;
434 bool has_timestamp
= false;
435 unsigned long sec
, usec
, msec
;
436 unsigned int year
, mon
, mday
, hour
, min
;
439 struct dmesg_component
*dmesg_comp
= notif_iter
->dmesg_comp
;
443 if (dmesg_comp
->params
.no_timestamp
) {
447 /* Extract time from input line */
448 if (sscanf(line
, "[%lu.%lu] ", &sec
, &usec
) == 2) {
449 ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
452 * The clock class we use has a 1 GHz frequency: convert
456 has_timestamp
= true;
457 } else if (sscanf(line
, "[%u-%u-%u %u:%u:%lu.%lu] ",
458 &year
, &mon
, &mday
, &hour
, &min
,
463 memset(&ti
, 0, sizeof(ti
));
464 ti
.tm_year
= year
- 1900; /* From 1900 */
465 ti
.tm_mon
= mon
- 1; /* 0 to 11 */
471 ep_sec
= bt_timegm(&ti
);
472 if (ep_sec
!= (time_t) -1) {
473 ts
= (uint64_t) ep_sec
* NSEC_PER_SEC
474 + (uint64_t) msec
* NSEC_PER_MSEC
;
477 has_timestamp
= true;
481 /* Set new start for the message portion of the line */
482 *new_start
= strchr(line
, ']');
483 BT_ASSERT(*new_start
);
486 if ((*new_start
)[0] == ' ') {
493 * At this point, we know if the stream class's event header
494 * field class should have a timestamp or not, so we can lazily
495 * create the metadata, stream, and packet objects.
497 ret
= try_create_meta_stream_packet(dmesg_comp
, has_timestamp
);
499 /* try_create_meta_stream_packet() logs errors */
503 notif
= bt_private_notification_event_create(notif_iter
->pc_notif_iter
,
504 dmesg_comp
->event_class
, dmesg_comp
->packet
);
506 BT_LOGE_STR("Cannot create event notification.");
510 event
= bt_private_notification_event_borrow_event(notif
);
513 if (dmesg_comp
->clock_class
) {
514 bt_private_event_set_default_clock_value(event
, ts
);
520 BT_OBJECT_PUT_REF_AND_RESET(notif
);
527 int fill_event_payload_from_line(const char *line
,
528 struct bt_private_event
*event
)
530 struct bt_private_field
*ep_field
= NULL
;
531 struct bt_private_field
*str_field
= NULL
;
535 ep_field
= bt_private_event_borrow_payload_field(event
);
537 str_field
= bt_private_field_structure_borrow_member_field_by_index(
540 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
545 if (line
[len
- 1] == '\n') {
546 /* Do not include the newline character in the payload */
550 ret
= bt_private_field_string_clear(str_field
);
552 BT_LOGE_STR("Cannot clear string field object.");
556 ret
= bt_private_field_string_append_with_length(str_field
, line
, len
);
558 BT_LOGE("Cannot append value to string field object: "
573 struct bt_private_notification
*create_notif_from_line(
574 struct dmesg_notif_iter
*dmesg_notif_iter
, const char *line
)
576 struct bt_private_event
*event
= NULL
;
577 struct bt_private_notification
*notif
= NULL
;
578 const char *new_start
;
581 notif
= create_init_event_notif_from_line(dmesg_notif_iter
,
584 BT_LOGE_STR("Cannot create and initialize event notification from line.");
588 event
= bt_private_notification_event_borrow_event(notif
);
590 ret
= fill_event_payload_from_line(new_start
, event
);
592 BT_LOGE("Cannot fill event payload field from line: "
600 BT_OBJECT_PUT_REF_AND_RESET(notif
);
607 void destroy_dmesg_notif_iter(struct dmesg_notif_iter
*dmesg_notif_iter
)
609 if (!dmesg_notif_iter
) {
613 if (dmesg_notif_iter
->fp
&& dmesg_notif_iter
->fp
!= stdin
) {
614 if (fclose(dmesg_notif_iter
->fp
)) {
615 BT_LOGE_ERRNO("Cannot close input file", ".");
619 bt_object_put_ref(dmesg_notif_iter
->tmp_event_notif
);
620 free(dmesg_notif_iter
->linebuf
);
621 g_free(dmesg_notif_iter
);
625 enum bt_self_notification_iterator_status
dmesg_notif_iter_init(
626 struct bt_self_notification_iterator
*self_notif_iter
,
627 struct bt_self_component_source
*self_comp
,
628 struct bt_self_component_port_output
*self_port
)
630 struct dmesg_component
*dmesg_comp
;
631 struct dmesg_notif_iter
*dmesg_notif_iter
=
632 g_new0(struct dmesg_notif_iter
, 1);
633 enum bt_self_notification_iterator_status status
=
634 BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK
;
636 if (!dmesg_notif_iter
) {
637 BT_LOGE_STR("Failed to allocate on dmesg notification iterator structure.");
641 dmesg_comp
= bt_self_component_get_data(
642 bt_self_component_source_borrow_self_component(self_comp
));
643 BT_ASSERT(dmesg_comp
);
644 dmesg_notif_iter
->dmesg_comp
= dmesg_comp
;
645 dmesg_notif_iter
->pc_notif_iter
= self_notif_iter
;
647 if (dmesg_comp
->params
.read_from_stdin
) {
648 dmesg_notif_iter
->fp
= stdin
;
650 dmesg_notif_iter
->fp
= fopen(dmesg_comp
->params
.path
->str
, "r");
651 if (!dmesg_notif_iter
->fp
) {
652 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
653 dmesg_comp
->params
.path
->str
);
658 bt_self_notification_iterator_set_data(self_notif_iter
,
663 destroy_dmesg_notif_iter(dmesg_notif_iter
);
664 bt_self_notification_iterator_set_data(self_notif_iter
, NULL
);
666 status
= BT_SELF_NOTIFICATION_ITERATOR_STATUS_ERROR
;
674 void dmesg_notif_iter_finalize(
675 struct bt_self_notification_iterator
*priv_notif_iter
)
677 destroy_dmesg_notif_iter(bt_self_notification_iterator_get_data(
682 enum bt_self_notification_iterator_status
dmesg_notif_iter_next_one(
683 struct dmesg_notif_iter
*dmesg_notif_iter
,
684 struct bt_private_notification
**notif
)
687 struct dmesg_component
*dmesg_comp
;
688 enum bt_self_notification_iterator_status status
=
689 BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK
;
691 BT_ASSERT(dmesg_notif_iter
);
692 dmesg_comp
= dmesg_notif_iter
->dmesg_comp
;
693 BT_ASSERT(dmesg_comp
);
695 if (dmesg_notif_iter
->state
== STATE_DONE
) {
696 status
= BT_SELF_NOTIFICATION_ITERATOR_STATUS_END
;
700 if (dmesg_notif_iter
->tmp_event_notif
||
701 dmesg_notif_iter
->state
== STATE_EMIT_PACKET_END
||
702 dmesg_notif_iter
->state
== STATE_EMIT_STREAM_END
) {
708 bool only_spaces
= true;
710 len
= bt_getline(&dmesg_notif_iter
->linebuf
,
711 &dmesg_notif_iter
->linebuf_len
, dmesg_notif_iter
->fp
);
713 if (errno
== EINVAL
) {
714 status
= BT_SELF_NOTIFICATION_ITERATOR_STATUS_ERROR
;
715 } else if (errno
== ENOMEM
) {
717 BT_SELF_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
719 if (dmesg_notif_iter
->state
== STATE_EMIT_STREAM_BEGINNING
) {
720 /* Stream did not even begin */
721 status
= BT_SELF_NOTIFICATION_ITERATOR_STATUS_END
;
724 /* End current packet now */
725 dmesg_notif_iter
->state
=
726 STATE_EMIT_PACKET_END
;
734 BT_ASSERT(dmesg_notif_iter
->linebuf
);
736 /* Ignore empty lines, once trimmed */
737 for (ch
= dmesg_notif_iter
->linebuf
; *ch
!= '\0'; ch
++) {
749 dmesg_notif_iter
->tmp_event_notif
= create_notif_from_line(
750 dmesg_notif_iter
, dmesg_notif_iter
->linebuf
);
751 if (!dmesg_notif_iter
->tmp_event_notif
) {
752 BT_LOGE("Cannot create event notification from line: "
753 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp
,
754 dmesg_notif_iter
->linebuf
);
759 BT_ASSERT(dmesg_comp
->trace
);
761 switch (dmesg_notif_iter
->state
) {
762 case STATE_EMIT_STREAM_BEGINNING
:
763 BT_ASSERT(dmesg_notif_iter
->tmp_event_notif
);
764 *notif
= bt_private_notification_stream_begin_create(
765 dmesg_notif_iter
->pc_notif_iter
, dmesg_comp
->stream
);
766 dmesg_notif_iter
->state
= STATE_EMIT_PACKET_BEGINNING
;
768 case STATE_EMIT_PACKET_BEGINNING
:
769 BT_ASSERT(dmesg_notif_iter
->tmp_event_notif
);
770 *notif
= bt_private_notification_packet_begin_create(
771 dmesg_notif_iter
->pc_notif_iter
, dmesg_comp
->packet
);
772 dmesg_notif_iter
->state
= STATE_EMIT_EVENT
;
774 case STATE_EMIT_EVENT
:
775 BT_ASSERT(dmesg_notif_iter
->tmp_event_notif
);
776 *notif
= dmesg_notif_iter
->tmp_event_notif
;
777 dmesg_notif_iter
->tmp_event_notif
= NULL
;
779 case STATE_EMIT_PACKET_END
:
780 *notif
= bt_private_notification_packet_end_create(
781 dmesg_notif_iter
->pc_notif_iter
, dmesg_comp
->packet
);
782 dmesg_notif_iter
->state
= STATE_EMIT_STREAM_END
;
784 case STATE_EMIT_STREAM_END
:
785 *notif
= bt_private_notification_stream_end_create(
786 dmesg_notif_iter
->pc_notif_iter
, dmesg_comp
->stream
);
787 dmesg_notif_iter
->state
= STATE_DONE
;
794 BT_LOGE("Cannot create notification: dmesg-comp-addr=%p",
796 status
= BT_SELF_NOTIFICATION_ITERATOR_STATUS_ERROR
;
804 enum bt_self_notification_iterator_status
dmesg_notif_iter_next(
805 struct bt_self_notification_iterator
*self_notif_iter
,
806 bt_notification_array notifs
, uint64_t capacity
,
809 struct dmesg_notif_iter
*dmesg_notif_iter
=
810 bt_self_notification_iterator_get_data(
812 enum bt_self_notification_iterator_status status
=
813 BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK
;
816 while (i
< capacity
&&
817 status
== BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK
) {
818 struct bt_private_notification
*priv_notif
= NULL
;
820 status
= dmesg_notif_iter_next_one(dmesg_notif_iter
,
822 notifs
[i
] = bt_private_notification_borrow_notification(
824 if (status
== BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK
) {
831 * Even if dmesg_notif_iter_next_one() returned
832 * something else than
833 * BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK, we
834 * accumulated notification objects in the output
835 * notification array, so we need to return
836 * BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK so that they
837 * are transfered to downstream. This other status
838 * occurs again the next time muxer_notif_iter_do_next()
839 * is called, possibly without any accumulated
840 * notification, in which case we'll return it.
843 status
= BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK
;