2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
3 * Copyright 2017 Philippe Proulx <jeremie.galarneau@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/babeltrace.h>
33 #include <babeltrace/values-internal.h>
34 #include <babeltrace/compat/utc-internal.h>
35 #include <babeltrace/compat/stdio-internal.h>
38 #define NSEC_PER_USEC 1000UL
39 #define NSEC_PER_MSEC 1000000UL
40 #define NSEC_PER_SEC 1000000000ULL
41 #define USEC_PER_SEC 1000000UL
43 struct dmesg_component
;
45 struct dmesg_notif_iter
{
46 struct dmesg_component
*dmesg_comp
;
47 struct bt_private_connection_private_notification_iterator
*pc_notif_iter
; /* Weak */
51 struct bt_notification
*tmp_event_notif
;
54 STATE_EMIT_STREAM_BEGINNING
,
55 STATE_EMIT_PACKET_BEGINNING
,
57 STATE_EMIT_PACKET_END
,
58 STATE_EMIT_STREAM_END
,
63 struct dmesg_component
{
66 bt_bool read_from_stdin
;
70 struct bt_trace
*trace
;
71 struct bt_stream_class
*stream_class
;
72 struct bt_event_class
*event_class
;
73 struct bt_stream
*stream
;
74 struct bt_packet
*packet
;
75 struct bt_clock_class
*clock_class
;
76 struct bt_clock_class_priority_map
*cc_prio_map
;
80 struct bt_field_class
*create_event_payload_fc(void)
82 struct bt_field_class
*root_fc
= NULL
;
83 struct bt_field_class
*fc
= NULL
;
86 root_fc
= bt_field_class_structure_create();
88 BT_LOGE_STR("Cannot create an empty structure field class object.");
92 fc
= bt_field_class_string_create();
94 BT_LOGE_STR("Cannot create a string field class object.");
98 ret
= bt_field_class_structure_append_member(root_fc
, "str", fc
);
100 BT_LOGE("Cannot add `str` member to structure field class: "
116 int create_meta(struct dmesg_component
*dmesg_comp
, bool has_ts
)
118 struct bt_field_class
*fc
= NULL
;
119 const char *trace_name
= NULL
;
120 gchar
*basename
= NULL
;
123 dmesg_comp
->trace
= bt_trace_create();
124 if (!dmesg_comp
->trace
) {
125 BT_LOGE_STR("Cannot create an empty trace object.");
129 if (dmesg_comp
->params
.read_from_stdin
) {
130 trace_name
= "STDIN";
132 basename
= g_path_get_basename(dmesg_comp
->params
.path
->str
);
135 if (strcmp(basename
, G_DIR_SEPARATOR_S
) != 0 &&
136 strcmp(basename
, ".") != 0) {
137 trace_name
= basename
;
142 ret
= bt_trace_set_name(dmesg_comp
->trace
, trace_name
);
144 BT_LOGE("Cannot set trace's name: name=\"%s\"", trace_name
);
149 dmesg_comp
->stream_class
= bt_stream_class_create(dmesg_comp
->trace
);
150 if (!dmesg_comp
->stream_class
) {
151 BT_LOGE_STR("Cannot create a stream class object.");
156 dmesg_comp
->clock_class
= bt_clock_class_create();
157 if (!dmesg_comp
->clock_class
) {
158 BT_LOGE_STR("Cannot create clock class.");
162 ret
= bt_stream_class_set_default_clock_class(
163 dmesg_comp
->stream_class
, dmesg_comp
->clock_class
);
165 BT_LOGE_STR("Cannot set stream class's default clock class.");
170 dmesg_comp
->event_class
= bt_event_class_create(
171 dmesg_comp
->stream_class
);
172 if (!dmesg_comp
->event_class
) {
173 BT_LOGE_STR("Cannot create an event class object.");
177 ret
= bt_event_class_set_name(dmesg_comp
->event_class
, "string");
179 BT_LOGE_STR("Cannot set event class's name.");
183 fc
= create_event_payload_fc();
185 BT_LOGE_STR("Cannot create event payload field class.");
189 ret
= bt_event_class_set_payload_field_class(dmesg_comp
->event_class
, fc
);
191 BT_LOGE_STR("Cannot set event class's event payload field class.");
211 int handle_params(struct dmesg_component
*dmesg_comp
, struct bt_value
*params
)
213 struct bt_value
*read_from_stdin
= NULL
;
214 struct bt_value
*no_timestamp
= NULL
;
215 struct bt_value
*path
= NULL
;
216 const char *path_str
;
219 no_timestamp
= bt_value_map_get(params
, "no-extract-timestamp");
221 if (!bt_value_is_bool(no_timestamp
)) {
222 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
224 bt_value_type_string(
225 bt_value_get_type(no_timestamp
)));
229 ret
= bt_value_bool_get(no_timestamp
,
230 &dmesg_comp
->params
.no_timestamp
);
234 path
= bt_value_map_get(params
, "path");
236 if (dmesg_comp
->params
.read_from_stdin
) {
237 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
241 if (!bt_value_is_string(path
)) {
242 BT_LOGE("Expecting a string value for the `path` parameter: "
244 bt_value_type_string(
245 bt_value_get_type(path
)));
249 ret
= bt_value_string_get(path
, &path_str
);
251 g_string_assign(dmesg_comp
->params
.path
, path_str
);
253 dmesg_comp
->params
.read_from_stdin
= true;
262 bt_put(read_from_stdin
);
264 bt_put(no_timestamp
);
269 int create_packet_and_stream(struct dmesg_component
*dmesg_comp
)
273 dmesg_comp
->stream
= bt_stream_create(dmesg_comp
->stream_class
);
274 if (!dmesg_comp
->stream
) {
275 BT_LOGE_STR("Cannot create stream object.");
279 dmesg_comp
->packet
= bt_packet_create(dmesg_comp
->stream
);
280 if (!dmesg_comp
->packet
) {
281 BT_LOGE_STR("Cannot create packet object.");
285 ret
= bt_trace_make_static(dmesg_comp
->trace
);
287 BT_LOGE_STR("Cannot make trace static.");
301 int try_create_meta_stream_packet(struct dmesg_component
*dmesg_comp
,
306 if (dmesg_comp
->trace
) {
307 /* Already created */
311 ret
= create_meta(dmesg_comp
, has_ts
);
313 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
318 ret
= create_packet_and_stream(dmesg_comp
);
320 BT_LOGE("Cannot create packet and stream objects: "
321 "dmesg-comp-addr=%p", dmesg_comp
);
335 void destroy_dmesg_component(struct dmesg_component
*dmesg_comp
)
341 if (dmesg_comp
->params
.path
) {
342 g_string_free(dmesg_comp
->params
.path
, TRUE
);
345 bt_put(dmesg_comp
->packet
);
346 bt_put(dmesg_comp
->trace
);
347 bt_put(dmesg_comp
->stream_class
);
348 bt_put(dmesg_comp
->event_class
);
349 bt_put(dmesg_comp
->stream
);
350 bt_put(dmesg_comp
->clock_class
);
351 bt_put(dmesg_comp
->cc_prio_map
);
356 enum bt_component_status
create_port(struct bt_private_component
*priv_comp
)
358 return bt_private_component_source_add_output_private_port(priv_comp
,
363 enum bt_component_status
dmesg_init(struct bt_private_component
*priv_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_component_status status
= BT_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", priv_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\"", priv_comp
,
392 dmesg_comp
->params
.path
->str
);
396 status
= create_port(priv_comp
);
397 if (status
!= BT_COMPONENT_STATUS_OK
) {
401 (void) bt_private_component_set_user_data(priv_comp
, dmesg_comp
);
405 destroy_dmesg_component(dmesg_comp
);
406 (void) bt_private_component_set_user_data(priv_comp
, NULL
);
409 status
= BT_COMPONENT_STATUS_ERROR
;
417 void dmesg_finalize(struct bt_private_component
*priv_comp
)
419 void *data
= bt_private_component_get_user_data(priv_comp
);
421 destroy_dmesg_component(data
);
425 struct bt_notification
*create_init_event_notif_from_line(
426 struct dmesg_notif_iter
*notif_iter
,
427 const char *line
, const char **new_start
)
429 struct bt_event
*event
;
430 struct bt_notification
*notif
= NULL
;
431 bool has_timestamp
= false;
432 unsigned long sec
, usec
, msec
;
433 unsigned int year
, mon
, mday
, hour
, min
;
436 struct dmesg_component
*dmesg_comp
= notif_iter
->dmesg_comp
;
440 if (dmesg_comp
->params
.no_timestamp
) {
444 /* Extract time from input line */
445 if (sscanf(line
, "[%lu.%lu] ", &sec
, &usec
) == 2) {
446 ts
= (uint64_t) sec
* USEC_PER_SEC
+ (uint64_t) usec
;
449 * The clock class we use has a 1 GHz frequency: convert
453 has_timestamp
= true;
454 } else if (sscanf(line
, "[%u-%u-%u %u:%u:%lu.%lu] ",
455 &year
, &mon
, &mday
, &hour
, &min
,
460 memset(&ti
, 0, sizeof(ti
));
461 ti
.tm_year
= year
- 1900; /* From 1900 */
462 ti
.tm_mon
= mon
- 1; /* 0 to 11 */
468 ep_sec
= bt_timegm(&ti
);
469 if (ep_sec
!= (time_t) -1) {
470 ts
= (uint64_t) ep_sec
* NSEC_PER_SEC
471 + (uint64_t) msec
* NSEC_PER_MSEC
;
474 has_timestamp
= true;
478 /* Set new start for the message portion of the line */
479 *new_start
= strchr(line
, ']');
480 BT_ASSERT(*new_start
);
483 if ((*new_start
)[0] == ' ') {
490 * At this point, we know if the stream class's event header
491 * field class should have a timestamp or not, so we can lazily
492 * create the metadata, stream, and packet objects.
494 ret
= try_create_meta_stream_packet(dmesg_comp
, has_timestamp
);
496 /* try_create_meta_stream_packet() logs errors */
500 notif
= bt_notification_event_create(notif_iter
->pc_notif_iter
,
501 dmesg_comp
->event_class
, dmesg_comp
->packet
);
503 BT_LOGE_STR("Cannot create event notification.");
507 event
= bt_notification_event_borrow_event(notif
);
510 if (dmesg_comp
->clock_class
) {
511 ret
= bt_event_set_default_clock_value(event
, ts
);
525 int fill_event_payload_from_line(const char *line
, struct bt_event
*event
)
527 struct bt_field
*ep_field
= NULL
;
528 struct bt_field
*str_field
= NULL
;
532 ep_field
= bt_event_borrow_payload_field(event
);
534 str_field
= bt_field_structure_borrow_member_field_by_index(ep_field
,
537 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
542 if (line
[len
- 1] == '\n') {
543 /* Do not include the newline character in the payload */
547 ret
= bt_field_string_clear(str_field
);
549 BT_LOGE_STR("Cannot clear string field object.");
553 ret
= bt_field_string_append_with_length(str_field
, line
, len
);
555 BT_LOGE("Cannot append value to string field object: "
570 struct bt_notification
*create_notif_from_line(
571 struct dmesg_notif_iter
*dmesg_notif_iter
, const char *line
)
573 struct bt_event
*event
= NULL
;
574 struct bt_notification
*notif
= NULL
;
575 const char *new_start
;
578 notif
= create_init_event_notif_from_line(dmesg_notif_iter
,
581 BT_LOGE_STR("Cannot create and initialize event notification from line.");
585 event
= bt_notification_event_borrow_event(notif
);
587 ret
= fill_event_payload_from_line(new_start
, event
);
589 BT_LOGE("Cannot fill event payload field from line: "
604 void destroy_dmesg_notif_iter(struct dmesg_notif_iter
*dmesg_notif_iter
)
606 if (!dmesg_notif_iter
) {
610 if (dmesg_notif_iter
->fp
&& dmesg_notif_iter
->fp
!= stdin
) {
611 if (fclose(dmesg_notif_iter
->fp
)) {
612 BT_LOGE_ERRNO("Cannot close input file", ".");
616 bt_put(dmesg_notif_iter
->tmp_event_notif
);
617 free(dmesg_notif_iter
->linebuf
);
618 g_free(dmesg_notif_iter
);
622 enum bt_notification_iterator_status
dmesg_notif_iter_init(
623 struct bt_private_connection_private_notification_iterator
*priv_notif_iter
,
624 struct bt_private_port
*priv_port
)
626 struct bt_private_component
*priv_comp
= NULL
;
627 struct dmesg_component
*dmesg_comp
;
628 struct dmesg_notif_iter
*dmesg_notif_iter
=
629 g_new0(struct dmesg_notif_iter
, 1);
630 enum bt_notification_iterator_status status
=
631 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
633 if (!dmesg_notif_iter
) {
634 BT_LOGE_STR("Failed to allocate on dmesg notification iterator structure.");
638 priv_comp
= bt_private_connection_private_notification_iterator_get_private_component(
640 BT_ASSERT(priv_comp
);
641 dmesg_comp
= bt_private_component_get_user_data(priv_comp
);
642 BT_ASSERT(dmesg_comp
);
643 dmesg_notif_iter
->dmesg_comp
= dmesg_comp
;
644 dmesg_notif_iter
->pc_notif_iter
= priv_notif_iter
;
646 if (dmesg_comp
->params
.read_from_stdin
) {
647 dmesg_notif_iter
->fp
= stdin
;
649 dmesg_notif_iter
->fp
= fopen(dmesg_comp
->params
.path
->str
, "r");
650 if (!dmesg_notif_iter
->fp
) {
651 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
652 dmesg_comp
->params
.path
->str
);
657 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter
,
662 destroy_dmesg_notif_iter(dmesg_notif_iter
);
663 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter
,
666 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
675 void dmesg_notif_iter_finalize(
676 struct bt_private_connection_private_notification_iterator
*priv_notif_iter
)
678 destroy_dmesg_notif_iter(bt_private_connection_private_notification_iterator_get_user_data(
683 enum bt_notification_iterator_status
dmesg_notif_iter_next_one(
684 struct dmesg_notif_iter
*dmesg_notif_iter
,
685 struct bt_notification
**notif
)
688 struct dmesg_component
*dmesg_comp
;
689 enum bt_notification_iterator_status status
=
690 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
692 BT_ASSERT(dmesg_notif_iter
);
693 dmesg_comp
= dmesg_notif_iter
->dmesg_comp
;
694 BT_ASSERT(dmesg_comp
);
696 if (dmesg_notif_iter
->state
== STATE_DONE
) {
697 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
701 if (dmesg_notif_iter
->tmp_event_notif
||
702 dmesg_notif_iter
->state
== STATE_EMIT_PACKET_END
||
703 dmesg_notif_iter
->state
== STATE_EMIT_STREAM_END
) {
709 bool only_spaces
= true;
711 len
= bt_getline(&dmesg_notif_iter
->linebuf
,
712 &dmesg_notif_iter
->linebuf_len
, dmesg_notif_iter
->fp
);
714 if (errno
== EINVAL
) {
715 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
716 } else if (errno
== ENOMEM
) {
718 BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
;
720 if (dmesg_notif_iter
->state
== STATE_EMIT_STREAM_BEGINNING
) {
721 /* Stream did not even begin */
722 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
725 /* End current packet now */
726 dmesg_notif_iter
->state
=
727 STATE_EMIT_PACKET_END
;
735 BT_ASSERT(dmesg_notif_iter
->linebuf
);
737 /* Ignore empty lines, once trimmed */
738 for (ch
= dmesg_notif_iter
->linebuf
; *ch
!= '\0'; ch
++) {
750 dmesg_notif_iter
->tmp_event_notif
= create_notif_from_line(
751 dmesg_notif_iter
, dmesg_notif_iter
->linebuf
);
752 if (!dmesg_notif_iter
->tmp_event_notif
) {
753 BT_LOGE("Cannot create event notification from line: "
754 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp
,
755 dmesg_notif_iter
->linebuf
);
760 BT_ASSERT(dmesg_comp
->trace
);
762 switch (dmesg_notif_iter
->state
) {
763 case STATE_EMIT_STREAM_BEGINNING
:
764 BT_ASSERT(dmesg_notif_iter
->tmp_event_notif
);
765 *notif
= bt_notification_stream_begin_create(
766 dmesg_notif_iter
->pc_notif_iter
, dmesg_comp
->stream
);
767 dmesg_notif_iter
->state
= STATE_EMIT_PACKET_BEGINNING
;
769 case STATE_EMIT_PACKET_BEGINNING
:
770 BT_ASSERT(dmesg_notif_iter
->tmp_event_notif
);
771 *notif
= bt_notification_packet_begin_create(
772 dmesg_notif_iter
->pc_notif_iter
, dmesg_comp
->packet
);
773 dmesg_notif_iter
->state
= STATE_EMIT_EVENT
;
775 case STATE_EMIT_EVENT
:
776 BT_ASSERT(dmesg_notif_iter
->tmp_event_notif
);
777 *notif
= dmesg_notif_iter
->tmp_event_notif
;
778 dmesg_notif_iter
->tmp_event_notif
= NULL
;
780 case STATE_EMIT_PACKET_END
:
781 *notif
= bt_notification_packet_end_create(
782 dmesg_notif_iter
->pc_notif_iter
, dmesg_comp
->packet
);
783 dmesg_notif_iter
->state
= STATE_EMIT_STREAM_END
;
785 case STATE_EMIT_STREAM_END
:
786 *notif
= bt_notification_stream_end_create(
787 dmesg_notif_iter
->pc_notif_iter
, dmesg_comp
->stream
);
788 dmesg_notif_iter
->state
= STATE_DONE
;
795 BT_LOGE("Cannot create notification: dmesg-comp-addr=%p",
797 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
805 enum bt_notification_iterator_status
dmesg_notif_iter_next(
806 struct bt_private_connection_private_notification_iterator
*priv_notif_iter
,
807 bt_notification_array notifs
, uint64_t capacity
,
810 struct dmesg_notif_iter
*dmesg_notif_iter
=
811 bt_private_connection_private_notification_iterator_get_user_data(
813 enum bt_notification_iterator_status status
=
814 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
817 while (i
< capacity
&& status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
818 status
= dmesg_notif_iter_next_one(dmesg_notif_iter
, ¬ifs
[i
]);
819 if (status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
826 * Even if dmesg_notif_iter_next_one() returned
827 * something else than
828 * BT_NOTIFICATION_ITERATOR_STATUS_OK, we accumulated
829 * notification objects in the output notification
830 * array, so we need to return
831 * BT_NOTIFICATION_ITERATOR_STATUS_OK so that they are
832 * transfered to downstream. This other status occurs
833 * again the next time muxer_notif_iter_do_next() is
834 * called, possibly without any accumulated
835 * notification, in which case we'll return it.
838 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;