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/value-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_msg_iter
{
47 struct dmesg_component
*dmesg_comp
;
48 bt_self_message_iterator
*pc_msg_iter
; /* Weak */
52 bt_message
*tmp_event_msg
;
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 bt_self_component_source
*self_comp
;
72 bt_trace_class
*trace_class
;
73 bt_stream_class
*stream_class
;
74 bt_event_class
*event_class
;
78 bt_clock_class
*clock_class
;
82 bt_field_class
*create_event_payload_fc(bt_trace_class
*trace_class
)
84 bt_field_class
*root_fc
= NULL
;
85 bt_field_class
*fc
= NULL
;
88 root_fc
= bt_field_class_structure_create(trace_class
);
90 BT_LOGE_STR("Cannot create an empty structure field class object.");
94 fc
= bt_field_class_string_create(trace_class
);
96 BT_LOGE_STR("Cannot create a string field class object.");
100 ret
= bt_field_class_structure_append_member(root_fc
,
103 BT_LOGE("Cannot add `str` member to structure field class: "
111 BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc
);
114 bt_field_class_put_ref(fc
);
119 int create_meta(struct dmesg_component
*dmesg_comp
, bool has_ts
)
121 bt_field_class
*fc
= NULL
;
124 dmesg_comp
->trace_class
= bt_trace_class_create(
125 bt_self_component_source_as_self_component(
126 dmesg_comp
->self_comp
));
127 if (!dmesg_comp
->trace_class
) {
128 BT_LOGE_STR("Cannot create an empty trace class object.");
132 dmesg_comp
->stream_class
= bt_stream_class_create(
133 dmesg_comp
->trace_class
);
134 if (!dmesg_comp
->stream_class
) {
135 BT_LOGE_STR("Cannot create a stream class object.");
140 dmesg_comp
->clock_class
= bt_clock_class_create(
141 dmesg_comp
->trace_class
);
142 if (!dmesg_comp
->clock_class
) {
143 BT_LOGE_STR("Cannot create clock class.");
147 ret
= bt_stream_class_set_default_clock_class(
148 dmesg_comp
->stream_class
, dmesg_comp
->clock_class
);
150 BT_LOGE_STR("Cannot set stream class's default clock class.");
155 dmesg_comp
->event_class
= bt_event_class_create(
156 dmesg_comp
->stream_class
);
157 if (!dmesg_comp
->event_class
) {
158 BT_LOGE_STR("Cannot create an event class object.");
162 ret
= bt_event_class_set_name(dmesg_comp
->event_class
, "string");
164 BT_LOGE_STR("Cannot set event class's name.");
168 fc
= create_event_payload_fc(dmesg_comp
->trace_class
);
170 BT_LOGE_STR("Cannot create event payload field class.");
174 ret
= bt_event_class_set_payload_field_class(dmesg_comp
->event_class
, fc
);
176 BT_LOGE_STR("Cannot set event class's event payload field class.");
186 bt_field_class_put_ref(fc
);
191 int handle_params(struct dmesg_component
*dmesg_comp
,
192 const bt_value
*params
)
194 const bt_value
*no_timestamp
= NULL
;
195 const bt_value
*path
= NULL
;
196 const char *path_str
;
199 no_timestamp
= bt_value_map_borrow_entry_value_const(params
,
200 "no-extract-timestamp");
202 if (!bt_value_is_bool(no_timestamp
)) {
203 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
205 bt_common_value_type_string(
206 bt_value_get_type(no_timestamp
)));
210 dmesg_comp
->params
.no_timestamp
=
211 bt_value_bool_get(no_timestamp
);
214 path
= bt_value_map_borrow_entry_value_const(params
, "path");
216 if (dmesg_comp
->params
.read_from_stdin
) {
217 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
221 if (!bt_value_is_string(path
)) {
222 BT_LOGE("Expecting a string value for the `path` parameter: "
224 bt_common_value_type_string(
225 bt_value_get_type(path
)));
229 path_str
= bt_value_string_get(path
);
230 g_string_assign(dmesg_comp
->params
.path
, path_str
);
232 dmesg_comp
->params
.read_from_stdin
= true;
245 int create_packet_and_stream_and_trace(struct dmesg_component
*dmesg_comp
)
248 const char *trace_name
;
249 gchar
*basename
= NULL
;
251 dmesg_comp
->trace
= bt_trace_create(dmesg_comp
->trace_class
);
252 if (!dmesg_comp
->trace
) {
253 BT_LOGE_STR("Cannot create trace object.");
257 if (dmesg_comp
->params
.read_from_stdin
) {
258 trace_name
= "STDIN";
260 basename
= g_path_get_basename(dmesg_comp
->params
.path
->str
);
263 if (strcmp(basename
, G_DIR_SEPARATOR_S
) != 0 &&
264 strcmp(basename
, ".") != 0) {
265 trace_name
= basename
;
270 ret
= bt_trace_set_name(dmesg_comp
->trace
, trace_name
);
272 BT_LOGE("Cannot set trace's name: name=\"%s\"",
278 dmesg_comp
->stream
= bt_stream_create(dmesg_comp
->stream_class
,
280 if (!dmesg_comp
->stream
) {
281 BT_LOGE_STR("Cannot create stream object.");
285 dmesg_comp
->packet
= bt_packet_create(dmesg_comp
->stream
);
286 if (!dmesg_comp
->packet
) {
287 BT_LOGE_STR("Cannot create packet object.");
291 ret
= bt_trace_make_static(dmesg_comp
->trace
);
293 BT_LOGE_STR("Cannot make trace static.");
311 int try_create_meta_stream_packet(struct dmesg_component
*dmesg_comp
,
316 if (dmesg_comp
->trace
) {
317 /* Already created */
321 ret
= create_meta(dmesg_comp
, has_ts
);
323 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
328 ret
= create_packet_and_stream_and_trace(dmesg_comp
);
330 BT_LOGE("Cannot create packet and stream objects: "
331 "dmesg-comp-addr=%p", dmesg_comp
);
345 void destroy_dmesg_component(struct dmesg_component
*dmesg_comp
)
351 if (dmesg_comp
->params
.path
) {
352 g_string_free(dmesg_comp
->params
.path
, TRUE
);
355 bt_packet_put_ref(dmesg_comp
->packet
);
356 bt_trace_put_ref(dmesg_comp
->trace
);
357 bt_stream_class_put_ref(dmesg_comp
->stream_class
);
358 bt_event_class_put_ref(dmesg_comp
->event_class
);
359 bt_stream_put_ref(dmesg_comp
->stream
);
360 bt_clock_class_put_ref(dmesg_comp
->clock_class
);
365 bt_self_component_status
create_port(
366 bt_self_component_source
*self_comp
)
368 return bt_self_component_source_add_output_port(self_comp
,
373 bt_self_component_status
dmesg_init(
374 bt_self_component_source
*self_comp
,
375 bt_value
*params
, void *init_method_data
)
378 struct dmesg_component
*dmesg_comp
= g_new0(struct dmesg_component
, 1);
379 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
382 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
386 dmesg_comp
->self_comp
= self_comp
;
387 dmesg_comp
->params
.path
= g_string_new(NULL
);
388 if (!dmesg_comp
->params
.path
) {
389 BT_LOGE_STR("Failed to allocate a GString.");
393 ret
= handle_params(dmesg_comp
, params
);
395 BT_LOGE("Invalid parameters: comp-addr=%p", self_comp
);
399 if (!dmesg_comp
->params
.read_from_stdin
&&
400 !g_file_test(dmesg_comp
->params
.path
->str
,
401 G_FILE_TEST_IS_REGULAR
)) {
402 BT_LOGE("Input path is not a regular file: "
403 "comp-addr=%p, path=\"%s\"", self_comp
,
404 dmesg_comp
->params
.path
->str
);
408 status
= create_port(self_comp
);
409 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
413 bt_self_component_set_data(
414 bt_self_component_source_as_self_component(self_comp
),
419 destroy_dmesg_component(dmesg_comp
);
420 bt_self_component_set_data(
421 bt_self_component_source_as_self_component(self_comp
),
425 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
433 void dmesg_finalize(bt_self_component_source
*self_comp
)
435 destroy_dmesg_component(bt_self_component_get_data(
436 bt_self_component_source_as_self_component(self_comp
)));
440 bt_message
*create_init_event_msg_from_line(
441 struct dmesg_msg_iter
*msg_iter
,
442 const char *line
, const char **new_start
)
445 bt_message
*msg
= NULL
;
446 bool has_timestamp
= false;
447 unsigned long sec
, usec
, msec
;
448 unsigned int year
, mon
, mday
, hour
, min
;
451 struct dmesg_component
*dmesg_comp
= msg_iter
->dmesg_comp
;
455 if (dmesg_comp
->params
.no_timestamp
) {
459 /* Extract time from input line */
460 if (sscanf(line
, "[%lu.%lu] ", &sec
, &usec
) == 2) {
461 ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
464 * The clock class we use has a 1 GHz frequency: convert
468 has_timestamp
= true;
469 } else if (sscanf(line
, "[%u-%u-%u %u:%u:%lu.%lu] ",
470 &year
, &mon
, &mday
, &hour
, &min
,
475 memset(&ti
, 0, sizeof(ti
));
476 ti
.tm_year
= year
- 1900; /* From 1900 */
477 ti
.tm_mon
= mon
- 1; /* 0 to 11 */
483 ep_sec
= bt_timegm(&ti
);
484 if (ep_sec
!= (time_t) -1) {
485 ts
= (uint64_t) ep_sec
* NSEC_PER_SEC
486 + (uint64_t) msec
* NSEC_PER_MSEC
;
489 has_timestamp
= true;
493 /* Set new start for the message portion of the line */
494 *new_start
= strchr(line
, ']');
495 BT_ASSERT(*new_start
);
498 if ((*new_start
)[0] == ' ') {
505 * At this point, we know if the stream class's event header
506 * field class should have a timestamp or not, so we can lazily
507 * create the metadata, stream, and packet objects.
509 ret
= try_create_meta_stream_packet(dmesg_comp
, has_timestamp
);
511 /* try_create_meta_stream_packet() logs errors */
515 msg
= bt_message_event_create(msg_iter
->pc_msg_iter
,
516 dmesg_comp
->event_class
, dmesg_comp
->packet
);
518 BT_LOGE_STR("Cannot create event message.");
522 event
= bt_message_event_borrow_event(msg
);
525 if (dmesg_comp
->clock_class
) {
526 bt_event_set_default_clock_snapshot(event
, ts
);
532 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
539 int fill_event_payload_from_line(const char *line
,
542 bt_field
*ep_field
= NULL
;
543 bt_field
*str_field
= NULL
;
547 ep_field
= bt_event_borrow_payload_field(event
);
549 str_field
= bt_field_structure_borrow_member_field_by_index(
552 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
557 if (line
[len
- 1] == '\n') {
558 /* Do not include the newline character in the payload */
562 ret
= bt_field_string_clear(str_field
);
564 BT_LOGE_STR("Cannot clear string field object.");
568 ret
= bt_field_string_append_with_length(str_field
, line
, len
);
570 BT_LOGE("Cannot append value to string field object: "
585 bt_message
*create_msg_from_line(
586 struct dmesg_msg_iter
*dmesg_msg_iter
, const char *line
)
588 bt_event
*event
= NULL
;
589 bt_message
*msg
= NULL
;
590 const char *new_start
;
593 msg
= create_init_event_msg_from_line(dmesg_msg_iter
,
596 BT_LOGE_STR("Cannot create and initialize event message from line.");
600 event
= bt_message_event_borrow_event(msg
);
602 ret
= fill_event_payload_from_line(new_start
, event
);
604 BT_LOGE("Cannot fill event payload field from line: "
612 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
619 void destroy_dmesg_msg_iter(struct dmesg_msg_iter
*dmesg_msg_iter
)
621 if (!dmesg_msg_iter
) {
625 if (dmesg_msg_iter
->fp
&& dmesg_msg_iter
->fp
!= stdin
) {
626 if (fclose(dmesg_msg_iter
->fp
)) {
627 BT_LOGE_ERRNO("Cannot close input file", ".");
631 bt_message_put_ref(dmesg_msg_iter
->tmp_event_msg
);
632 free(dmesg_msg_iter
->linebuf
);
633 g_free(dmesg_msg_iter
);
637 bt_self_message_iterator_status
dmesg_msg_iter_init(
638 bt_self_message_iterator
*self_msg_iter
,
639 bt_self_component_source
*self_comp
,
640 bt_self_component_port_output
*self_port
)
642 struct dmesg_component
*dmesg_comp
;
643 struct dmesg_msg_iter
*dmesg_msg_iter
=
644 g_new0(struct dmesg_msg_iter
, 1);
645 bt_self_message_iterator_status status
=
646 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
648 if (!dmesg_msg_iter
) {
649 BT_LOGE_STR("Failed to allocate on dmesg message iterator structure.");
653 dmesg_comp
= bt_self_component_get_data(
654 bt_self_component_source_as_self_component(self_comp
));
655 BT_ASSERT(dmesg_comp
);
656 dmesg_msg_iter
->dmesg_comp
= dmesg_comp
;
657 dmesg_msg_iter
->pc_msg_iter
= self_msg_iter
;
659 if (dmesg_comp
->params
.read_from_stdin
) {
660 dmesg_msg_iter
->fp
= stdin
;
662 dmesg_msg_iter
->fp
= fopen(dmesg_comp
->params
.path
->str
, "r");
663 if (!dmesg_msg_iter
->fp
) {
664 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
665 dmesg_comp
->params
.path
->str
);
670 bt_self_message_iterator_set_data(self_msg_iter
,
675 destroy_dmesg_msg_iter(dmesg_msg_iter
);
676 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
678 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
686 void dmesg_msg_iter_finalize(
687 bt_self_message_iterator
*priv_msg_iter
)
689 destroy_dmesg_msg_iter(bt_self_message_iterator_get_data(
694 bt_self_message_iterator_status
dmesg_msg_iter_next_one(
695 struct dmesg_msg_iter
*dmesg_msg_iter
,
699 struct dmesg_component
*dmesg_comp
;
700 bt_self_message_iterator_status status
=
701 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
703 BT_ASSERT(dmesg_msg_iter
);
704 dmesg_comp
= dmesg_msg_iter
->dmesg_comp
;
705 BT_ASSERT(dmesg_comp
);
707 if (dmesg_msg_iter
->state
== STATE_DONE
) {
708 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
712 if (dmesg_msg_iter
->tmp_event_msg
||
713 dmesg_msg_iter
->state
== STATE_EMIT_PACKET_END
||
714 dmesg_msg_iter
->state
== STATE_EMIT_STREAM_END
) {
720 bool only_spaces
= true;
722 len
= bt_getline(&dmesg_msg_iter
->linebuf
,
723 &dmesg_msg_iter
->linebuf_len
, dmesg_msg_iter
->fp
);
725 if (errno
== EINVAL
) {
726 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
727 } else if (errno
== ENOMEM
) {
729 BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
731 if (dmesg_msg_iter
->state
== STATE_EMIT_STREAM_BEGINNING
) {
732 /* Stream did not even begin */
733 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
736 /* End current packet now */
737 dmesg_msg_iter
->state
=
738 STATE_EMIT_PACKET_END
;
746 BT_ASSERT(dmesg_msg_iter
->linebuf
);
748 /* Ignore empty lines, once trimmed */
749 for (ch
= dmesg_msg_iter
->linebuf
; *ch
!= '\0'; ch
++) {
761 dmesg_msg_iter
->tmp_event_msg
= create_msg_from_line(
762 dmesg_msg_iter
, dmesg_msg_iter
->linebuf
);
763 if (!dmesg_msg_iter
->tmp_event_msg
) {
764 BT_LOGE("Cannot create event message from line: "
765 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp
,
766 dmesg_msg_iter
->linebuf
);
771 BT_ASSERT(dmesg_comp
->trace
);
773 switch (dmesg_msg_iter
->state
) {
774 case STATE_EMIT_STREAM_BEGINNING
:
775 BT_ASSERT(dmesg_msg_iter
->tmp_event_msg
);
776 *msg
= bt_message_stream_beginning_create(
777 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->stream
);
778 dmesg_msg_iter
->state
= STATE_EMIT_PACKET_BEGINNING
;
780 case STATE_EMIT_PACKET_BEGINNING
:
781 BT_ASSERT(dmesg_msg_iter
->tmp_event_msg
);
782 *msg
= bt_message_packet_beginning_create(
783 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->packet
);
784 dmesg_msg_iter
->state
= STATE_EMIT_EVENT
;
786 case STATE_EMIT_EVENT
:
787 BT_ASSERT(dmesg_msg_iter
->tmp_event_msg
);
788 *msg
= dmesg_msg_iter
->tmp_event_msg
;
789 dmesg_msg_iter
->tmp_event_msg
= NULL
;
791 case STATE_EMIT_PACKET_END
:
792 *msg
= bt_message_packet_end_create(
793 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->packet
);
794 dmesg_msg_iter
->state
= STATE_EMIT_STREAM_END
;
796 case STATE_EMIT_STREAM_END
:
797 *msg
= bt_message_stream_end_create(
798 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->stream
);
799 dmesg_msg_iter
->state
= STATE_DONE
;
806 BT_LOGE("Cannot create message: dmesg-comp-addr=%p",
808 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
816 bt_self_message_iterator_status
dmesg_msg_iter_next(
817 bt_self_message_iterator
*self_msg_iter
,
818 bt_message_array_const msgs
, uint64_t capacity
,
821 struct dmesg_msg_iter
*dmesg_msg_iter
=
822 bt_self_message_iterator_get_data(
824 bt_self_message_iterator_status status
=
825 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
828 while (i
< capacity
&&
829 status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
830 bt_message
*priv_msg
= NULL
;
832 status
= dmesg_msg_iter_next_one(dmesg_msg_iter
,
835 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
842 * Even if dmesg_msg_iter_next_one() returned
843 * something else than
844 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we
845 * accumulated message objects in the output
846 * message array, so we need to return
847 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they
848 * are transfered to downstream. This other status
849 * occurs again the next time muxer_msg_iter_do_next()
850 * is called, possibly without any accumulated
851 * message, in which case we'll return it.
854 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;