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_STREAM_ACTIVITY_BEGINNING
,
57 STATE_EMIT_PACKET_BEGINNING
,
59 STATE_EMIT_PACKET_END
,
60 STATE_EMIT_STREAM_ACTIVITY_END
,
61 STATE_EMIT_STREAM_END
,
66 struct dmesg_component
{
69 bt_bool read_from_stdin
;
73 bt_self_component_source
*self_comp
;
74 bt_trace_class
*trace_class
;
75 bt_stream_class
*stream_class
;
76 bt_event_class
*event_class
;
80 bt_clock_class
*clock_class
;
84 bt_field_class
*create_event_payload_fc(bt_trace_class
*trace_class
)
86 bt_field_class
*root_fc
= NULL
;
87 bt_field_class
*fc
= NULL
;
90 root_fc
= bt_field_class_structure_create(trace_class
);
92 BT_LOGE_STR("Cannot create an empty structure field class object.");
96 fc
= bt_field_class_string_create(trace_class
);
98 BT_LOGE_STR("Cannot create a string field class object.");
102 ret
= bt_field_class_structure_append_member(root_fc
,
105 BT_LOGE("Cannot add `str` member to structure field class: "
113 BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc
);
116 bt_field_class_put_ref(fc
);
121 int create_meta(struct dmesg_component
*dmesg_comp
, bool has_ts
)
123 bt_field_class
*fc
= NULL
;
126 dmesg_comp
->trace_class
= bt_trace_class_create(
127 bt_self_component_source_as_self_component(
128 dmesg_comp
->self_comp
));
129 if (!dmesg_comp
->trace_class
) {
130 BT_LOGE_STR("Cannot create an empty trace class object.");
134 dmesg_comp
->stream_class
= bt_stream_class_create(
135 dmesg_comp
->trace_class
);
136 if (!dmesg_comp
->stream_class
) {
137 BT_LOGE_STR("Cannot create a stream class object.");
142 dmesg_comp
->clock_class
= bt_clock_class_create(
143 dmesg_comp
->trace_class
);
144 if (!dmesg_comp
->clock_class
) {
145 BT_LOGE_STR("Cannot create clock class.");
149 ret
= bt_stream_class_set_default_clock_class(
150 dmesg_comp
->stream_class
, dmesg_comp
->clock_class
);
152 BT_LOGE_STR("Cannot set stream class's default clock class.");
157 dmesg_comp
->event_class
= bt_event_class_create(
158 dmesg_comp
->stream_class
);
159 if (!dmesg_comp
->event_class
) {
160 BT_LOGE_STR("Cannot create an event class object.");
164 ret
= bt_event_class_set_name(dmesg_comp
->event_class
, "string");
166 BT_LOGE_STR("Cannot set event class's name.");
170 fc
= create_event_payload_fc(dmesg_comp
->trace_class
);
172 BT_LOGE_STR("Cannot create event payload field class.");
176 ret
= bt_event_class_set_payload_field_class(dmesg_comp
->event_class
, fc
);
178 BT_LOGE_STR("Cannot set event class's event payload field class.");
188 bt_field_class_put_ref(fc
);
193 int handle_params(struct dmesg_component
*dmesg_comp
,
194 const bt_value
*params
)
196 const bt_value
*no_timestamp
= NULL
;
197 const bt_value
*path
= NULL
;
198 const char *path_str
;
201 no_timestamp
= bt_value_map_borrow_entry_value_const(params
,
202 "no-extract-timestamp");
204 if (!bt_value_is_bool(no_timestamp
)) {
205 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
207 bt_common_value_type_string(
208 bt_value_get_type(no_timestamp
)));
212 dmesg_comp
->params
.no_timestamp
=
213 bt_value_bool_get(no_timestamp
);
216 path
= bt_value_map_borrow_entry_value_const(params
, "path");
218 if (dmesg_comp
->params
.read_from_stdin
) {
219 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
223 if (!bt_value_is_string(path
)) {
224 BT_LOGE("Expecting a string value for the `path` parameter: "
226 bt_common_value_type_string(
227 bt_value_get_type(path
)));
231 path_str
= bt_value_string_get(path
);
232 g_string_assign(dmesg_comp
->params
.path
, path_str
);
234 dmesg_comp
->params
.read_from_stdin
= true;
247 int create_packet_and_stream_and_trace(struct dmesg_component
*dmesg_comp
)
250 const char *trace_name
= NULL
;
251 gchar
*basename
= NULL
;
253 dmesg_comp
->trace
= bt_trace_create(dmesg_comp
->trace_class
);
254 if (!dmesg_comp
->trace
) {
255 BT_LOGE_STR("Cannot create trace object.");
259 if (dmesg_comp
->params
.read_from_stdin
) {
260 trace_name
= "STDIN";
262 basename
= g_path_get_basename(dmesg_comp
->params
.path
->str
);
265 if (strcmp(basename
, G_DIR_SEPARATOR_S
) != 0 &&
266 strcmp(basename
, ".") != 0) {
267 trace_name
= basename
;
272 ret
= bt_trace_set_name(dmesg_comp
->trace
, trace_name
);
274 BT_LOGE("Cannot set trace's name: name=\"%s\"",
280 dmesg_comp
->stream
= bt_stream_create(dmesg_comp
->stream_class
,
282 if (!dmesg_comp
->stream
) {
283 BT_LOGE_STR("Cannot create stream object.");
287 dmesg_comp
->packet
= bt_packet_create(dmesg_comp
->stream
);
288 if (!dmesg_comp
->packet
) {
289 BT_LOGE_STR("Cannot create packet object.");
293 ret
= bt_trace_make_static(dmesg_comp
->trace
);
295 BT_LOGE_STR("Cannot make trace static.");
313 int try_create_meta_stream_packet(struct dmesg_component
*dmesg_comp
,
318 if (dmesg_comp
->trace
) {
319 /* Already created */
323 ret
= create_meta(dmesg_comp
, has_ts
);
325 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
330 ret
= create_packet_and_stream_and_trace(dmesg_comp
);
332 BT_LOGE("Cannot create packet and stream objects: "
333 "dmesg-comp-addr=%p", dmesg_comp
);
347 void destroy_dmesg_component(struct dmesg_component
*dmesg_comp
)
353 if (dmesg_comp
->params
.path
) {
354 g_string_free(dmesg_comp
->params
.path
, TRUE
);
357 bt_packet_put_ref(dmesg_comp
->packet
);
358 bt_trace_put_ref(dmesg_comp
->trace
);
359 bt_stream_class_put_ref(dmesg_comp
->stream_class
);
360 bt_event_class_put_ref(dmesg_comp
->event_class
);
361 bt_stream_put_ref(dmesg_comp
->stream
);
362 bt_clock_class_put_ref(dmesg_comp
->clock_class
);
367 bt_self_component_status
create_port(
368 bt_self_component_source
*self_comp
)
370 return bt_self_component_source_add_output_port(self_comp
,
375 bt_self_component_status
dmesg_init(
376 bt_self_component_source
*self_comp
,
377 bt_value
*params
, void *init_method_data
)
380 struct dmesg_component
*dmesg_comp
= g_new0(struct dmesg_component
, 1);
381 bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
384 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
388 dmesg_comp
->self_comp
= self_comp
;
389 dmesg_comp
->params
.path
= g_string_new(NULL
);
390 if (!dmesg_comp
->params
.path
) {
391 BT_LOGE_STR("Failed to allocate a GString.");
395 ret
= handle_params(dmesg_comp
, params
);
397 BT_LOGE("Invalid parameters: comp-addr=%p", self_comp
);
401 if (!dmesg_comp
->params
.read_from_stdin
&&
402 !g_file_test(dmesg_comp
->params
.path
->str
,
403 G_FILE_TEST_IS_REGULAR
)) {
404 BT_LOGE("Input path is not a regular file: "
405 "comp-addr=%p, path=\"%s\"", self_comp
,
406 dmesg_comp
->params
.path
->str
);
410 status
= create_port(self_comp
);
411 if (status
!= BT_SELF_COMPONENT_STATUS_OK
) {
415 bt_self_component_set_data(
416 bt_self_component_source_as_self_component(self_comp
),
421 destroy_dmesg_component(dmesg_comp
);
422 bt_self_component_set_data(
423 bt_self_component_source_as_self_component(self_comp
),
427 status
= BT_SELF_COMPONENT_STATUS_ERROR
;
435 void dmesg_finalize(bt_self_component_source
*self_comp
)
437 destroy_dmesg_component(bt_self_component_get_data(
438 bt_self_component_source_as_self_component(self_comp
)));
442 bt_message
*create_init_event_msg_from_line(
443 struct dmesg_msg_iter
*msg_iter
,
444 const char *line
, const char **new_start
)
447 bt_message
*msg
= NULL
;
448 bool has_timestamp
= false;
449 unsigned long sec
, usec
, msec
;
450 unsigned int year
, mon
, mday
, hour
, min
;
453 struct dmesg_component
*dmesg_comp
= msg_iter
->dmesg_comp
;
457 if (dmesg_comp
->params
.no_timestamp
) {
461 /* Extract time from input line */
462 if (sscanf(line
, "[%lu.%lu] ", &sec
, &usec
) == 2) {
463 ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
466 * The clock class we use has a 1 GHz frequency: convert
470 has_timestamp
= true;
471 } else if (sscanf(line
, "[%u-%u-%u %u:%u:%lu.%lu] ",
472 &year
, &mon
, &mday
, &hour
, &min
,
477 memset(&ti
, 0, sizeof(ti
));
478 ti
.tm_year
= year
- 1900; /* From 1900 */
479 ti
.tm_mon
= mon
- 1; /* 0 to 11 */
485 ep_sec
= bt_timegm(&ti
);
486 if (ep_sec
!= (time_t) -1) {
487 ts
= (uint64_t) ep_sec
* NSEC_PER_SEC
488 + (uint64_t) msec
* NSEC_PER_MSEC
;
491 has_timestamp
= true;
495 /* Set new start for the message portion of the line */
496 *new_start
= strchr(line
, ']');
497 BT_ASSERT(*new_start
);
500 if ((*new_start
)[0] == ' ') {
507 * At this point, we know if the stream class's event header
508 * field class should have a timestamp or not, so we can lazily
509 * create the metadata, stream, and packet objects.
511 ret
= try_create_meta_stream_packet(dmesg_comp
, has_timestamp
);
513 /* try_create_meta_stream_packet() logs errors */
517 msg
= bt_message_event_create(msg_iter
->pc_msg_iter
,
518 dmesg_comp
->event_class
, dmesg_comp
->packet
);
520 BT_LOGE_STR("Cannot create event message.");
524 event
= bt_message_event_borrow_event(msg
);
527 if (dmesg_comp
->clock_class
) {
528 bt_event_set_default_clock_snapshot(event
, ts
);
534 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
541 int fill_event_payload_from_line(const char *line
,
544 bt_field
*ep_field
= NULL
;
545 bt_field
*str_field
= NULL
;
549 ep_field
= bt_event_borrow_payload_field(event
);
551 str_field
= bt_field_structure_borrow_member_field_by_index(
554 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
559 if (line
[len
- 1] == '\n') {
560 /* Do not include the newline character in the payload */
564 ret
= bt_field_string_clear(str_field
);
566 BT_LOGE_STR("Cannot clear string field object.");
570 ret
= bt_field_string_append_with_length(str_field
, line
, len
);
572 BT_LOGE("Cannot append value to string field object: "
587 bt_message
*create_msg_from_line(
588 struct dmesg_msg_iter
*dmesg_msg_iter
, const char *line
)
590 bt_event
*event
= NULL
;
591 bt_message
*msg
= NULL
;
592 const char *new_start
;
595 msg
= create_init_event_msg_from_line(dmesg_msg_iter
,
598 BT_LOGE_STR("Cannot create and initialize event message from line.");
602 event
= bt_message_event_borrow_event(msg
);
604 ret
= fill_event_payload_from_line(new_start
, event
);
606 BT_LOGE("Cannot fill event payload field from line: "
614 BT_MESSAGE_PUT_REF_AND_RESET(msg
);
621 void destroy_dmesg_msg_iter(struct dmesg_msg_iter
*dmesg_msg_iter
)
623 if (!dmesg_msg_iter
) {
627 if (dmesg_msg_iter
->fp
&& dmesg_msg_iter
->fp
!= stdin
) {
628 if (fclose(dmesg_msg_iter
->fp
)) {
629 BT_LOGE_ERRNO("Cannot close input file", ".");
633 bt_message_put_ref(dmesg_msg_iter
->tmp_event_msg
);
634 free(dmesg_msg_iter
->linebuf
);
635 g_free(dmesg_msg_iter
);
639 bt_self_message_iterator_status
dmesg_msg_iter_init(
640 bt_self_message_iterator
*self_msg_iter
,
641 bt_self_component_source
*self_comp
,
642 bt_self_component_port_output
*self_port
)
644 struct dmesg_component
*dmesg_comp
;
645 struct dmesg_msg_iter
*dmesg_msg_iter
=
646 g_new0(struct dmesg_msg_iter
, 1);
647 bt_self_message_iterator_status status
=
648 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
650 if (!dmesg_msg_iter
) {
651 BT_LOGE_STR("Failed to allocate on dmesg message iterator structure.");
655 dmesg_comp
= bt_self_component_get_data(
656 bt_self_component_source_as_self_component(self_comp
));
657 BT_ASSERT(dmesg_comp
);
658 dmesg_msg_iter
->dmesg_comp
= dmesg_comp
;
659 dmesg_msg_iter
->pc_msg_iter
= self_msg_iter
;
661 if (dmesg_comp
->params
.read_from_stdin
) {
662 dmesg_msg_iter
->fp
= stdin
;
664 dmesg_msg_iter
->fp
= fopen(dmesg_comp
->params
.path
->str
, "r");
665 if (!dmesg_msg_iter
->fp
) {
666 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
667 dmesg_comp
->params
.path
->str
);
672 bt_self_message_iterator_set_data(self_msg_iter
,
677 destroy_dmesg_msg_iter(dmesg_msg_iter
);
678 bt_self_message_iterator_set_data(self_msg_iter
, NULL
);
680 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
688 void dmesg_msg_iter_finalize(
689 bt_self_message_iterator
*priv_msg_iter
)
691 destroy_dmesg_msg_iter(bt_self_message_iterator_get_data(
696 bt_self_message_iterator_status
dmesg_msg_iter_next_one(
697 struct dmesg_msg_iter
*dmesg_msg_iter
,
701 struct dmesg_component
*dmesg_comp
;
702 bt_self_message_iterator_status status
=
703 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
705 BT_ASSERT(dmesg_msg_iter
);
706 dmesg_comp
= dmesg_msg_iter
->dmesg_comp
;
707 BT_ASSERT(dmesg_comp
);
709 if (dmesg_msg_iter
->state
== STATE_DONE
) {
710 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
714 if (dmesg_msg_iter
->tmp_event_msg
||
715 dmesg_msg_iter
->state
== STATE_EMIT_PACKET_END
||
716 dmesg_msg_iter
->state
== STATE_EMIT_STREAM_ACTIVITY_END
||
717 dmesg_msg_iter
->state
== STATE_EMIT_STREAM_END
) {
723 bool only_spaces
= true;
725 len
= bt_getline(&dmesg_msg_iter
->linebuf
,
726 &dmesg_msg_iter
->linebuf_len
, dmesg_msg_iter
->fp
);
728 if (errno
== EINVAL
) {
729 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
730 } else if (errno
== ENOMEM
) {
732 BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
;
734 if (dmesg_msg_iter
->state
== STATE_EMIT_STREAM_BEGINNING
) {
735 /* Stream did not even begin */
736 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
739 /* End current packet now */
740 dmesg_msg_iter
->state
=
741 STATE_EMIT_PACKET_END
;
749 BT_ASSERT(dmesg_msg_iter
->linebuf
);
751 /* Ignore empty lines, once trimmed */
752 for (ch
= dmesg_msg_iter
->linebuf
; *ch
!= '\0'; ch
++) {
764 dmesg_msg_iter
->tmp_event_msg
= create_msg_from_line(
765 dmesg_msg_iter
, dmesg_msg_iter
->linebuf
);
766 if (!dmesg_msg_iter
->tmp_event_msg
) {
767 BT_LOGE("Cannot create event message from line: "
768 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp
,
769 dmesg_msg_iter
->linebuf
);
774 BT_ASSERT(dmesg_comp
->trace
);
776 switch (dmesg_msg_iter
->state
) {
777 case STATE_EMIT_STREAM_BEGINNING
:
778 BT_ASSERT(dmesg_msg_iter
->tmp_event_msg
);
779 *msg
= bt_message_stream_beginning_create(
780 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->stream
);
781 dmesg_msg_iter
->state
= STATE_EMIT_STREAM_ACTIVITY_BEGINNING
;
783 case STATE_EMIT_STREAM_ACTIVITY_BEGINNING
:
784 BT_ASSERT(dmesg_msg_iter
->tmp_event_msg
);
785 *msg
= bt_message_stream_activity_beginning_create(
786 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->stream
);
787 dmesg_msg_iter
->state
= STATE_EMIT_PACKET_BEGINNING
;
789 case STATE_EMIT_PACKET_BEGINNING
:
790 BT_ASSERT(dmesg_msg_iter
->tmp_event_msg
);
791 *msg
= bt_message_packet_beginning_create(
792 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->packet
);
793 dmesg_msg_iter
->state
= STATE_EMIT_EVENT
;
795 case STATE_EMIT_EVENT
:
796 BT_ASSERT(dmesg_msg_iter
->tmp_event_msg
);
797 *msg
= dmesg_msg_iter
->tmp_event_msg
;
798 dmesg_msg_iter
->tmp_event_msg
= NULL
;
800 case STATE_EMIT_PACKET_END
:
801 *msg
= bt_message_packet_end_create(
802 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->packet
);
803 dmesg_msg_iter
->state
= STATE_EMIT_STREAM_ACTIVITY_END
;
805 case STATE_EMIT_STREAM_ACTIVITY_END
:
806 *msg
= bt_message_stream_activity_end_create(
807 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->stream
);
808 dmesg_msg_iter
->state
= STATE_EMIT_STREAM_END
;
810 case STATE_EMIT_STREAM_END
:
811 *msg
= bt_message_stream_end_create(
812 dmesg_msg_iter
->pc_msg_iter
, dmesg_comp
->stream
);
813 dmesg_msg_iter
->state
= STATE_DONE
;
820 BT_LOGE("Cannot create message: dmesg-comp-addr=%p",
822 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
830 bt_self_message_iterator_status
dmesg_msg_iter_next(
831 bt_self_message_iterator
*self_msg_iter
,
832 bt_message_array_const msgs
, uint64_t capacity
,
835 struct dmesg_msg_iter
*dmesg_msg_iter
=
836 bt_self_message_iterator_get_data(
838 bt_self_message_iterator_status status
=
839 BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
842 while (i
< capacity
&&
843 status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
844 bt_message
*priv_msg
= NULL
;
846 status
= dmesg_msg_iter_next_one(dmesg_msg_iter
,
849 if (status
== BT_SELF_MESSAGE_ITERATOR_STATUS_OK
) {
856 * Even if dmesg_msg_iter_next_one() returned
857 * something else than
858 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we
859 * accumulated message objects in the output
860 * message array, so we need to return
861 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they
862 * are transfered to downstream. This other status
863 * occurs again the next time muxer_msg_iter_do_next()
864 * is called, possibly without any accumulated
865 * message, in which case we'll return it.
868 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;