src.text.dmesg: honor component's initial log level
[babeltrace.git] / src / plugins / text / dmesg / dmesg.c
1 /*
2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
3 * Copyright 2017 Philippe Proulx <pproulx@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
24 #define BT_LOG_OUTPUT_LEVEL (dmesg_comp->log_level)
25 #define BT_LOG_TAG "PLUGIN/SRC.TEXT.DMESG"
26 #include "logging/log.h"
27
28 #include <stdbool.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <stdio.h>
32 #include "common/common.h"
33 #include "common/assert.h"
34 #include <babeltrace2/babeltrace.h>
35 #include "compat/utc.h"
36 #include "compat/stdio.h"
37 #include <glib.h>
38
39 #define NSEC_PER_USEC 1000UL
40 #define NSEC_PER_MSEC 1000000UL
41 #define NSEC_PER_SEC 1000000000ULL
42 #define USEC_PER_SEC 1000000UL
43
44 struct dmesg_component;
45
46 struct dmesg_msg_iter {
47 struct dmesg_component *dmesg_comp;
48 bt_self_message_iterator *pc_msg_iter; /* Weak */
49 char *linebuf;
50 size_t linebuf_len;
51 FILE *fp;
52 bt_message *tmp_event_msg;
53 uint64_t last_clock_value;
54
55 enum {
56 STATE_EMIT_STREAM_BEGINNING,
57 STATE_EMIT_STREAM_ACTIVITY_BEGINNING,
58 STATE_EMIT_PACKET_BEGINNING,
59 STATE_EMIT_EVENT,
60 STATE_EMIT_PACKET_END,
61 STATE_EMIT_STREAM_ACTIVITY_END,
62 STATE_EMIT_STREAM_END,
63 STATE_DONE,
64 } state;
65 };
66
67 struct dmesg_component {
68 bt_logging_level log_level;
69
70 struct {
71 GString *path;
72 bt_bool read_from_stdin;
73 bt_bool no_timestamp;
74 } params;
75
76 bt_self_component_source *self_comp;
77 bt_trace_class *trace_class;
78 bt_stream_class *stream_class;
79 bt_event_class *event_class;
80 bt_trace *trace;
81 bt_stream *stream;
82 bt_packet *packet;
83 bt_clock_class *clock_class;
84 };
85
86 static
87 bt_field_class *create_event_payload_fc(struct dmesg_component *dmesg_comp,
88 bt_trace_class *trace_class)
89 {
90 bt_field_class *root_fc = NULL;
91 bt_field_class *fc = NULL;
92 int ret;
93
94 root_fc = bt_field_class_structure_create(trace_class);
95 if (!root_fc) {
96 BT_LOGE_STR("Cannot create an empty structure field class object.");
97 goto error;
98 }
99
100 fc = bt_field_class_string_create(trace_class);
101 if (!fc) {
102 BT_LOGE_STR("Cannot create a string field class object.");
103 goto error;
104 }
105
106 ret = bt_field_class_structure_append_member(root_fc,
107 "str", fc);
108 if (ret) {
109 BT_LOGE("Cannot add `str` member to structure field class: "
110 "ret=%d", ret);
111 goto error;
112 }
113
114 goto end;
115
116 error:
117 BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc);
118
119 end:
120 bt_field_class_put_ref(fc);
121 return root_fc;
122 }
123
124 static
125 int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
126 {
127 bt_field_class *fc = NULL;
128 int ret = 0;
129
130 dmesg_comp->trace_class = bt_trace_class_create(
131 bt_self_component_source_as_self_component(
132 dmesg_comp->self_comp));
133 if (!dmesg_comp->trace_class) {
134 BT_LOGE_STR("Cannot create an empty trace class object.");
135 goto error;
136 }
137
138 dmesg_comp->stream_class = bt_stream_class_create(
139 dmesg_comp->trace_class);
140 if (!dmesg_comp->stream_class) {
141 BT_LOGE_STR("Cannot create a stream class object.");
142 goto error;
143 }
144
145 if (has_ts) {
146 dmesg_comp->clock_class = bt_clock_class_create(
147 bt_self_component_source_as_self_component(
148 dmesg_comp->self_comp));
149 if (!dmesg_comp->clock_class) {
150 BT_LOGE_STR("Cannot create clock class.");
151 goto error;
152 }
153
154 /*
155 * The `dmesg` timestamp's origin is not the Unix epoch,
156 * it's the boot time.
157 */
158 bt_clock_class_set_origin_is_unix_epoch(dmesg_comp->clock_class,
159 BT_FALSE);
160
161 ret = bt_stream_class_set_default_clock_class(
162 dmesg_comp->stream_class, dmesg_comp->clock_class);
163 if (ret) {
164 BT_LOGE_STR("Cannot set stream class's default clock class.");
165 goto error;
166 }
167
168 bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
169 dmesg_comp->stream_class, BT_TRUE);
170 bt_stream_class_set_packets_have_end_default_clock_snapshot(
171 dmesg_comp->stream_class, BT_TRUE);
172 }
173
174 dmesg_comp->event_class = bt_event_class_create(
175 dmesg_comp->stream_class);
176 if (!dmesg_comp->event_class) {
177 BT_LOGE_STR("Cannot create an event class object.");
178 goto error;
179 }
180
181 ret = bt_event_class_set_name(dmesg_comp->event_class, "string");
182 if (ret) {
183 BT_LOGE_STR("Cannot set event class's name.");
184 goto error;
185 }
186
187 fc = create_event_payload_fc(dmesg_comp, dmesg_comp->trace_class);
188 if (!fc) {
189 BT_LOGE_STR("Cannot create event payload field class.");
190 goto error;
191 }
192
193 ret = bt_event_class_set_payload_field_class(dmesg_comp->event_class, fc);
194 if (ret) {
195 BT_LOGE_STR("Cannot set event class's event payload field class.");
196 goto error;
197 }
198
199 goto end;
200
201 error:
202 ret = -1;
203
204 end:
205 bt_field_class_put_ref(fc);
206 return ret;
207 }
208
209 static
210 int handle_params(struct dmesg_component *dmesg_comp,
211 const bt_value *params)
212 {
213 const bt_value *no_timestamp = NULL;
214 const bt_value *path = NULL;
215 const char *path_str;
216 int ret = 0;
217
218 no_timestamp = bt_value_map_borrow_entry_value_const(params,
219 "no-extract-timestamp");
220 if (no_timestamp) {
221 if (!bt_value_is_bool(no_timestamp)) {
222 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
223 "type=%s",
224 bt_common_value_type_string(
225 bt_value_get_type(no_timestamp)));
226 goto error;
227 }
228
229 dmesg_comp->params.no_timestamp =
230 bt_value_bool_get(no_timestamp);
231 }
232
233 path = bt_value_map_borrow_entry_value_const(params, "path");
234 if (path) {
235 if (dmesg_comp->params.read_from_stdin) {
236 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
237 goto error;
238 }
239
240 if (!bt_value_is_string(path)) {
241 BT_LOGE("Expecting a string value for the `path` parameter: "
242 "type=%s",
243 bt_common_value_type_string(
244 bt_value_get_type(path)));
245 goto error;
246 }
247
248 path_str = bt_value_string_get(path);
249 g_string_assign(dmesg_comp->params.path, path_str);
250 } else {
251 dmesg_comp->params.read_from_stdin = true;
252 }
253
254 goto end;
255
256 error:
257 ret = -1;
258
259 end:
260 return ret;
261 }
262
263 static
264 int create_packet_and_stream_and_trace(struct dmesg_component *dmesg_comp)
265 {
266 int ret = 0;
267 const char *trace_name = NULL;
268 gchar *basename = NULL;
269
270 dmesg_comp->trace = bt_trace_create(dmesg_comp->trace_class);
271 if (!dmesg_comp->trace) {
272 BT_LOGE_STR("Cannot create trace object.");
273 goto error;
274 }
275
276 if (dmesg_comp->params.read_from_stdin) {
277 trace_name = "STDIN";
278 } else {
279 basename = g_path_get_basename(dmesg_comp->params.path->str);
280 BT_ASSERT(basename);
281
282 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
283 strcmp(basename, ".") != 0) {
284 trace_name = basename;
285 }
286 }
287
288 if (trace_name) {
289 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
290 if (ret) {
291 BT_LOGE("Cannot set trace's name: name=\"%s\"",
292 trace_name);
293 goto error;
294 }
295 }
296
297 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class,
298 dmesg_comp->trace);
299 if (!dmesg_comp->stream) {
300 BT_LOGE_STR("Cannot create stream object.");
301 goto error;
302 }
303
304 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream);
305 if (!dmesg_comp->packet) {
306 BT_LOGE_STR("Cannot create packet object.");
307 goto error;
308 }
309
310 goto end;
311
312 error:
313 ret = -1;
314
315 end:
316 if (basename) {
317 g_free(basename);
318 }
319
320 return ret;
321 }
322
323 static
324 int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp,
325 bool has_ts)
326 {
327 int ret = 0;
328
329 if (dmesg_comp->trace) {
330 /* Already created */
331 goto end;
332 }
333
334 ret = create_meta(dmesg_comp, has_ts);
335 if (ret) {
336 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
337 dmesg_comp);
338 goto error;
339 }
340
341 ret = create_packet_and_stream_and_trace(dmesg_comp);
342 if (ret) {
343 BT_LOGE("Cannot create packet and stream objects: "
344 "dmesg-comp-addr=%p", dmesg_comp);
345 goto error;
346 }
347
348 goto end;
349
350 error:
351 ret = -1;
352
353 end:
354 return ret;
355 }
356
357 static
358 void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
359 {
360 if (!dmesg_comp) {
361 return;
362 }
363
364 if (dmesg_comp->params.path) {
365 g_string_free(dmesg_comp->params.path, TRUE);
366 }
367
368 bt_packet_put_ref(dmesg_comp->packet);
369 bt_trace_put_ref(dmesg_comp->trace);
370 bt_stream_class_put_ref(dmesg_comp->stream_class);
371 bt_event_class_put_ref(dmesg_comp->event_class);
372 bt_stream_put_ref(dmesg_comp->stream);
373 bt_clock_class_put_ref(dmesg_comp->clock_class);
374 bt_trace_class_put_ref(dmesg_comp->trace_class);
375 g_free(dmesg_comp);
376 }
377
378 static
379 bt_self_component_status create_port(
380 bt_self_component_source *self_comp)
381 {
382 return bt_self_component_source_add_output_port(self_comp,
383 "out", NULL, NULL);
384 }
385
386 BT_HIDDEN
387 bt_self_component_status dmesg_init(
388 bt_self_component_source *self_comp,
389 bt_value *params, void *init_method_data)
390 {
391 int ret = 0;
392 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
393 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
394 const bt_component *comp = bt_self_component_as_component(
395 bt_self_component_source_as_self_component(self_comp));
396 bt_logging_level log_level = bt_component_get_logging_level(comp);
397
398 if (!dmesg_comp) {
399 /* Implicit log level is not available here */
400 BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
401 "Failed to allocate one dmesg component structure.");
402 goto error;
403 }
404
405 dmesg_comp->log_level = log_level;
406 dmesg_comp->self_comp = self_comp;
407 dmesg_comp->params.path = g_string_new(NULL);
408 if (!dmesg_comp->params.path) {
409 BT_LOGE_STR("Failed to allocate a GString.");
410 goto error;
411 }
412
413 ret = handle_params(dmesg_comp, params);
414 if (ret) {
415 BT_LOGE("Invalid parameters: comp-addr=%p", self_comp);
416 goto error;
417 }
418
419 if (!dmesg_comp->params.read_from_stdin &&
420 !g_file_test(dmesg_comp->params.path->str,
421 G_FILE_TEST_IS_REGULAR)) {
422 BT_LOGE("Input path is not a regular file: "
423 "comp-addr=%p, path=\"%s\"", self_comp,
424 dmesg_comp->params.path->str);
425 goto error;
426 }
427
428 status = create_port(self_comp);
429 if (status != BT_SELF_COMPONENT_STATUS_OK) {
430 goto error;
431 }
432
433 bt_self_component_set_data(
434 bt_self_component_source_as_self_component(self_comp),
435 dmesg_comp);
436 BT_LOGI("`src.text.dmesg` component initialized: name=\"%s\"",
437 bt_component_get_name(comp));
438 goto end;
439
440 error:
441 destroy_dmesg_component(dmesg_comp);
442 bt_self_component_set_data(
443 bt_self_component_source_as_self_component(self_comp),
444 NULL);
445
446 if (status >= 0) {
447 status = BT_SELF_COMPONENT_STATUS_ERROR;
448 }
449
450 end:
451 return status;
452 }
453
454 BT_HIDDEN
455 void dmesg_finalize(bt_self_component_source *self_comp)
456 {
457 destroy_dmesg_component(bt_self_component_get_data(
458 bt_self_component_source_as_self_component(self_comp)));
459 }
460
461 static
462 bt_message *create_init_event_msg_from_line(
463 struct dmesg_msg_iter *msg_iter,
464 const char *line, const char **new_start)
465 {
466 bt_event *event;
467 bt_message *msg = NULL;
468 bool has_timestamp = false;
469 unsigned long sec, usec, msec;
470 unsigned int year, mon, mday, hour, min;
471 uint64_t ts = 0;
472 int ret = 0;
473 struct dmesg_component *dmesg_comp = msg_iter->dmesg_comp;
474
475 *new_start = line;
476
477 if (dmesg_comp->params.no_timestamp) {
478 goto skip_ts;
479 }
480
481 /* Extract time from input line */
482 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
483 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
484
485 /*
486 * The clock class we use has a 1 GHz frequency: convert
487 * from µs to ns.
488 */
489 ts *= NSEC_PER_USEC;
490 has_timestamp = true;
491 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
492 &year, &mon, &mday, &hour, &min,
493 &sec, &msec) == 7) {
494 time_t ep_sec;
495 struct tm ti;
496
497 memset(&ti, 0, sizeof(ti));
498 ti.tm_year = year - 1900; /* From 1900 */
499 ti.tm_mon = mon - 1; /* 0 to 11 */
500 ti.tm_mday = mday;
501 ti.tm_hour = hour;
502 ti.tm_min = min;
503 ti.tm_sec = sec;
504
505 ep_sec = bt_timegm(&ti);
506 if (ep_sec != (time_t) -1) {
507 ts = (uint64_t) ep_sec * NSEC_PER_SEC
508 + (uint64_t) msec * NSEC_PER_MSEC;
509 }
510
511 has_timestamp = true;
512 }
513
514 if (has_timestamp) {
515 /* Set new start for the message portion of the line */
516 *new_start = strchr(line, ']');
517 BT_ASSERT(*new_start);
518 (*new_start)++;
519
520 if ((*new_start)[0] == ' ') {
521 (*new_start)++;
522 }
523 }
524
525 skip_ts:
526 /*
527 * At this point, we know if the stream class's event header
528 * field class should have a timestamp or not, so we can lazily
529 * create the metadata, stream, and packet objects.
530 */
531 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
532 if (ret) {
533 /* try_create_meta_stream_packet() logs errors */
534 goto error;
535 }
536
537 if (dmesg_comp->clock_class) {
538 msg = bt_message_event_create_with_default_clock_snapshot(
539 msg_iter->pc_msg_iter,
540 dmesg_comp->event_class, dmesg_comp->packet, ts);
541 msg_iter->last_clock_value = ts;
542 } else {
543 msg = bt_message_event_create(msg_iter->pc_msg_iter,
544 dmesg_comp->event_class, dmesg_comp->packet);
545 }
546
547 if (!msg) {
548 BT_LOGE_STR("Cannot create event message.");
549 goto error;
550 }
551
552 event = bt_message_event_borrow_event(msg);
553 BT_ASSERT(event);
554 goto end;
555
556 error:
557 BT_MESSAGE_PUT_REF_AND_RESET(msg);
558
559 end:
560 return msg;
561 }
562
563 static
564 int fill_event_payload_from_line(struct dmesg_component *dmesg_comp,
565 const char *line, bt_event *event)
566 {
567 bt_field *ep_field = NULL;
568 bt_field *str_field = NULL;
569 size_t len;
570 int ret;
571
572 ep_field = bt_event_borrow_payload_field(event);
573 BT_ASSERT(ep_field);
574 str_field = bt_field_structure_borrow_member_field_by_index(
575 ep_field, 0);
576 if (!str_field) {
577 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
578 goto error;
579 }
580
581 len = strlen(line);
582 if (line[len - 1] == '\n') {
583 /* Do not include the newline character in the payload */
584 len--;
585 }
586
587 ret = bt_field_string_clear(str_field);
588 if (ret) {
589 BT_LOGE_STR("Cannot clear string field object.");
590 goto error;
591 }
592
593 ret = bt_field_string_append_with_length(str_field, line, len);
594 if (ret) {
595 BT_LOGE("Cannot append value to string field object: "
596 "len=%zu", len);
597 goto error;
598 }
599
600 goto end;
601
602 error:
603 ret = -1;
604
605 end:
606 return ret;
607 }
608
609 static
610 bt_message *create_msg_from_line(
611 struct dmesg_msg_iter *dmesg_msg_iter, const char *line)
612 {
613 struct dmesg_component *dmesg_comp = dmesg_msg_iter->dmesg_comp;
614 bt_event *event = NULL;
615 bt_message *msg = NULL;
616 const char *new_start;
617 int ret;
618
619 msg = create_init_event_msg_from_line(dmesg_msg_iter,
620 line, &new_start);
621 if (!msg) {
622 BT_LOGE_STR("Cannot create and initialize event message from line.");
623 goto error;
624 }
625
626 event = bt_message_event_borrow_event(msg);
627 BT_ASSERT(event);
628 ret = fill_event_payload_from_line(dmesg_comp, new_start, event);
629 if (ret) {
630 BT_LOGE("Cannot fill event payload field from line: "
631 "ret=%d", ret);
632 goto error;
633 }
634
635 goto end;
636
637 error:
638 BT_MESSAGE_PUT_REF_AND_RESET(msg);
639
640 end:
641 return msg;
642 }
643
644 static
645 void destroy_dmesg_msg_iter(struct dmesg_msg_iter *dmesg_msg_iter)
646 {
647 struct dmesg_component *dmesg_comp = dmesg_msg_iter->dmesg_comp;
648
649 if (!dmesg_msg_iter) {
650 return;
651 }
652
653 if (dmesg_msg_iter->fp && dmesg_msg_iter->fp != stdin) {
654 if (fclose(dmesg_msg_iter->fp)) {
655 BT_LOGE_ERRNO("Cannot close input file", ".");
656 }
657 }
658
659 bt_message_put_ref(dmesg_msg_iter->tmp_event_msg);
660 free(dmesg_msg_iter->linebuf);
661 g_free(dmesg_msg_iter);
662 }
663
664
665
666 BT_HIDDEN
667 bt_self_message_iterator_status dmesg_msg_iter_init(
668 bt_self_message_iterator *self_msg_iter,
669 bt_self_component_source *self_comp,
670 bt_self_component_port_output *self_port)
671 {
672 struct dmesg_component *dmesg_comp = bt_self_component_get_data(
673 bt_self_component_source_as_self_component(self_comp));
674 struct dmesg_msg_iter *dmesg_msg_iter =
675 g_new0(struct dmesg_msg_iter, 1);
676 bt_self_message_iterator_status status =
677 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
678
679 if (!dmesg_msg_iter) {
680 BT_LOGE_STR("Failed to allocate on dmesg message iterator structure.");
681 goto error;
682 }
683
684 BT_ASSERT(dmesg_comp);
685 dmesg_msg_iter->dmesg_comp = dmesg_comp;
686 dmesg_msg_iter->pc_msg_iter = self_msg_iter;
687
688 if (dmesg_comp->params.read_from_stdin) {
689 dmesg_msg_iter->fp = stdin;
690 } else {
691 dmesg_msg_iter->fp = fopen(dmesg_comp->params.path->str, "r");
692 if (!dmesg_msg_iter->fp) {
693 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
694 dmesg_comp->params.path->str);
695 goto error;
696 }
697 }
698
699 bt_self_message_iterator_set_data(self_msg_iter,
700 dmesg_msg_iter);
701 goto end;
702
703 error:
704 destroy_dmesg_msg_iter(dmesg_msg_iter);
705 bt_self_message_iterator_set_data(self_msg_iter, NULL);
706 if (status >= 0) {
707 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
708 }
709
710 end:
711 return status;
712 }
713
714 BT_HIDDEN
715 void dmesg_msg_iter_finalize(
716 bt_self_message_iterator *priv_msg_iter)
717 {
718 destroy_dmesg_msg_iter(bt_self_message_iterator_get_data(
719 priv_msg_iter));
720 }
721
722 static
723 bt_self_message_iterator_status dmesg_msg_iter_next_one(
724 struct dmesg_msg_iter *dmesg_msg_iter,
725 bt_message **msg)
726 {
727 ssize_t len;
728 struct dmesg_component *dmesg_comp;
729 bt_self_message_iterator_status status =
730 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
731
732 BT_ASSERT(dmesg_msg_iter);
733 dmesg_comp = dmesg_msg_iter->dmesg_comp;
734 BT_ASSERT(dmesg_comp);
735
736 if (dmesg_msg_iter->state == STATE_DONE) {
737 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
738 goto end;
739 }
740
741 if (dmesg_msg_iter->tmp_event_msg ||
742 dmesg_msg_iter->state == STATE_EMIT_PACKET_END ||
743 dmesg_msg_iter->state == STATE_EMIT_STREAM_ACTIVITY_END ||
744 dmesg_msg_iter->state == STATE_EMIT_STREAM_END) {
745 goto handle_state;
746 }
747
748 while (true) {
749 const char *ch;
750 bool only_spaces = true;
751
752 len = bt_getline(&dmesg_msg_iter->linebuf,
753 &dmesg_msg_iter->linebuf_len, dmesg_msg_iter->fp);
754 if (len < 0) {
755 if (errno == EINVAL) {
756 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
757 } else if (errno == ENOMEM) {
758 status =
759 BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM;
760 } else {
761 if (dmesg_msg_iter->state == STATE_EMIT_STREAM_BEGINNING) {
762 /* Stream did not even begin */
763 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
764 goto end;
765 } else {
766 /* End current packet now */
767 dmesg_msg_iter->state =
768 STATE_EMIT_PACKET_END;
769 goto handle_state;
770 }
771 }
772
773 goto end;
774 }
775
776 BT_ASSERT(dmesg_msg_iter->linebuf);
777
778 /* Ignore empty lines, once trimmed */
779 for (ch = dmesg_msg_iter->linebuf; *ch != '\0'; ch++) {
780 if (!isspace(*ch)) {
781 only_spaces = false;
782 break;
783 }
784 }
785
786 if (!only_spaces) {
787 break;
788 }
789 }
790
791 dmesg_msg_iter->tmp_event_msg = create_msg_from_line(
792 dmesg_msg_iter, dmesg_msg_iter->linebuf);
793 if (!dmesg_msg_iter->tmp_event_msg) {
794 BT_LOGE("Cannot create event message from line: "
795 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
796 dmesg_msg_iter->linebuf);
797 goto end;
798 }
799
800 handle_state:
801 BT_ASSERT(dmesg_comp->trace);
802
803 switch (dmesg_msg_iter->state) {
804 case STATE_EMIT_STREAM_BEGINNING:
805 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
806 *msg = bt_message_stream_beginning_create(
807 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
808 dmesg_msg_iter->state = STATE_EMIT_STREAM_ACTIVITY_BEGINNING;
809 break;
810 case STATE_EMIT_STREAM_ACTIVITY_BEGINNING:
811 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
812 *msg = bt_message_stream_activity_beginning_create(
813 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
814 dmesg_msg_iter->state = STATE_EMIT_PACKET_BEGINNING;
815 break;
816 case STATE_EMIT_PACKET_BEGINNING:
817 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
818
819 if (dmesg_comp->clock_class) {
820 *msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
821 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet,
822 dmesg_msg_iter->last_clock_value);
823 } else {
824 *msg = bt_message_packet_beginning_create(
825 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet);
826 }
827
828 dmesg_msg_iter->state = STATE_EMIT_EVENT;
829 break;
830 case STATE_EMIT_EVENT:
831 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
832 *msg = dmesg_msg_iter->tmp_event_msg;
833 dmesg_msg_iter->tmp_event_msg = NULL;
834 break;
835 case STATE_EMIT_PACKET_END:
836 if (dmesg_comp->clock_class) {
837 *msg = bt_message_packet_end_create_with_default_clock_snapshot(
838 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet,
839 dmesg_msg_iter->last_clock_value);
840 } else {
841 *msg = bt_message_packet_end_create(
842 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet);
843 }
844
845 dmesg_msg_iter->state = STATE_EMIT_STREAM_ACTIVITY_END;
846 break;
847 case STATE_EMIT_STREAM_ACTIVITY_END:
848 *msg = bt_message_stream_activity_end_create(
849 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
850 dmesg_msg_iter->state = STATE_EMIT_STREAM_END;
851 break;
852 case STATE_EMIT_STREAM_END:
853 *msg = bt_message_stream_end_create(
854 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
855 dmesg_msg_iter->state = STATE_DONE;
856 break;
857 default:
858 break;
859 }
860
861 if (!*msg) {
862 BT_LOGE("Cannot create message: dmesg-comp-addr=%p",
863 dmesg_comp);
864 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
865 }
866
867 end:
868 return status;
869 }
870
871 BT_HIDDEN
872 bt_self_message_iterator_status dmesg_msg_iter_next(
873 bt_self_message_iterator *self_msg_iter,
874 bt_message_array_const msgs, uint64_t capacity,
875 uint64_t *count)
876 {
877 struct dmesg_msg_iter *dmesg_msg_iter =
878 bt_self_message_iterator_get_data(
879 self_msg_iter);
880 bt_self_message_iterator_status status =
881 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
882 uint64_t i = 0;
883
884 while (i < capacity &&
885 status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
886 bt_message *priv_msg = NULL;
887
888 status = dmesg_msg_iter_next_one(dmesg_msg_iter,
889 &priv_msg);
890 msgs[i] = priv_msg;
891 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
892 i++;
893 }
894 }
895
896 if (i > 0) {
897 /*
898 * Even if dmesg_msg_iter_next_one() returned
899 * something else than
900 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we
901 * accumulated message objects in the output
902 * message array, so we need to return
903 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they
904 * are transfered to downstream. This other status
905 * occurs again the next time muxer_msg_iter_do_next()
906 * is called, possibly without any accumulated
907 * message, in which case we'll return it.
908 */
909 *count = i;
910 status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
911 }
912
913 return status;
914 }
915
916 BT_HIDDEN
917 bt_bool dmesg_msg_iter_can_seek_beginning(
918 bt_self_message_iterator *self_msg_iter)
919 {
920 struct dmesg_msg_iter *dmesg_msg_iter =
921 bt_self_message_iterator_get_data(self_msg_iter);
922
923 /* Can't seek the beginning of the standard input stream */
924 return !dmesg_msg_iter->dmesg_comp->params.read_from_stdin;
925 }
926
927 BT_HIDDEN
928 bt_self_message_iterator_status dmesg_msg_iter_seek_beginning(
929 bt_self_message_iterator *self_msg_iter)
930 {
931 struct dmesg_msg_iter *dmesg_msg_iter =
932 bt_self_message_iterator_get_data(self_msg_iter);
933
934 BT_ASSERT(!dmesg_msg_iter->dmesg_comp->params.read_from_stdin);
935
936 BT_MESSAGE_PUT_REF_AND_RESET(dmesg_msg_iter->tmp_event_msg);
937 dmesg_msg_iter->last_clock_value = 0;
938 dmesg_msg_iter->state = STATE_EMIT_STREAM_BEGINNING;
939 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
940 }
This page took 0.049594 seconds and 5 git commands to generate.