332322b83e4f410be598444456e114d916bc914b
[babeltrace.git] / 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_TAG "PLUGIN-TEXT-DMESG-SRC"
25 #include "logging.h"
26
27 #include <stdbool.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <stdio.h>
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>
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
54 enum {
55 STATE_EMIT_STREAM_BEGINNING,
56 STATE_EMIT_PACKET_BEGINNING,
57 STATE_EMIT_EVENT,
58 STATE_EMIT_PACKET_END,
59 STATE_EMIT_STREAM_END,
60 STATE_DONE,
61 } state;
62 };
63
64 struct dmesg_component {
65 struct {
66 GString *path;
67 bt_bool read_from_stdin;
68 bt_bool no_timestamp;
69 } params;
70
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;
75 bt_trace *trace;
76 bt_stream *stream;
77 bt_packet *packet;
78 bt_clock_class *clock_class;
79 };
80
81 static
82 bt_field_class *create_event_payload_fc(void)
83 {
84 bt_field_class *root_fc = NULL;
85 bt_field_class *fc = NULL;
86 int ret;
87
88 root_fc = bt_field_class_structure_create();
89 if (!root_fc) {
90 BT_LOGE_STR("Cannot create an empty structure field class object.");
91 goto error;
92 }
93
94 fc = bt_field_class_string_create();
95 if (!fc) {
96 BT_LOGE_STR("Cannot create a string field class object.");
97 goto error;
98 }
99
100 ret = bt_field_class_structure_append_member(root_fc,
101 "str", fc);
102 if (ret) {
103 BT_LOGE("Cannot add `str` member to structure field class: "
104 "ret=%d", ret);
105 goto error;
106 }
107
108 goto end;
109
110 error:
111 BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc);
112
113 end:
114 bt_field_class_put_ref(fc);
115 return root_fc;
116 }
117
118 static
119 int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
120 {
121 bt_field_class *fc = NULL;
122 int ret = 0;
123
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.");
129 goto error;
130 }
131
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.");
136 goto error;
137 }
138
139 if (has_ts) {
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.");
144 goto error;
145 }
146
147 ret = bt_stream_class_set_default_clock_class(
148 dmesg_comp->stream_class, dmesg_comp->clock_class);
149 if (ret) {
150 BT_LOGE_STR("Cannot set stream class's default clock class.");
151 goto error;
152 }
153 }
154
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.");
159 goto error;
160 }
161
162 ret = bt_event_class_set_name(dmesg_comp->event_class, "string");
163 if (ret) {
164 BT_LOGE_STR("Cannot set event class's name.");
165 goto error;
166 }
167
168 fc = create_event_payload_fc();
169 if (!fc) {
170 BT_LOGE_STR("Cannot create event payload field class.");
171 goto error;
172 }
173
174 ret = bt_event_class_set_payload_field_class(dmesg_comp->event_class, fc);
175 if (ret) {
176 BT_LOGE_STR("Cannot set event class's event payload field class.");
177 goto error;
178 }
179
180 goto end;
181
182 error:
183 ret = -1;
184
185 end:
186 bt_field_class_put_ref(fc);
187 return ret;
188 }
189
190 static
191 int handle_params(struct dmesg_component *dmesg_comp,
192 const bt_value *params)
193 {
194 const bt_value *no_timestamp = NULL;
195 const bt_value *path = NULL;
196 const char *path_str;
197 int ret = 0;
198
199 no_timestamp = bt_value_map_borrow_entry_value_const(params,
200 "no-extract-timestamp");
201 if (no_timestamp) {
202 if (!bt_value_is_bool(no_timestamp)) {
203 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
204 "type=%s",
205 bt_common_value_type_string(
206 bt_value_get_type(no_timestamp)));
207 goto error;
208 }
209
210 dmesg_comp->params.no_timestamp =
211 bt_value_bool_get(no_timestamp);
212 }
213
214 path = bt_value_map_borrow_entry_value_const(params, "path");
215 if (path) {
216 if (dmesg_comp->params.read_from_stdin) {
217 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
218 goto error;
219 }
220
221 if (!bt_value_is_string(path)) {
222 BT_LOGE("Expecting a string value for the `path` parameter: "
223 "type=%s",
224 bt_common_value_type_string(
225 bt_value_get_type(path)));
226 goto error;
227 }
228
229 path_str = bt_value_string_get(path);
230 g_string_assign(dmesg_comp->params.path, path_str);
231 } else {
232 dmesg_comp->params.read_from_stdin = true;
233 }
234
235 goto end;
236
237 error:
238 ret = -1;
239
240 end:
241 return ret;
242 }
243
244 static
245 int create_packet_and_stream_and_trace(struct dmesg_component *dmesg_comp)
246 {
247 int ret = 0;
248 const char *trace_name;
249 gchar *basename = NULL;
250
251 dmesg_comp->trace = bt_trace_create(dmesg_comp->trace_class);
252 if (!dmesg_comp->trace) {
253 BT_LOGE_STR("Cannot create trace object.");
254 goto error;
255 }
256
257 if (dmesg_comp->params.read_from_stdin) {
258 trace_name = "STDIN";
259 } else {
260 basename = g_path_get_basename(dmesg_comp->params.path->str);
261 BT_ASSERT(basename);
262
263 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
264 strcmp(basename, ".") != 0) {
265 trace_name = basename;
266 }
267 }
268
269 if (trace_name) {
270 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
271 if (ret) {
272 BT_LOGE("Cannot set trace's name: name=\"%s\"",
273 trace_name);
274 goto error;
275 }
276 }
277
278 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class,
279 dmesg_comp->trace);
280 if (!dmesg_comp->stream) {
281 BT_LOGE_STR("Cannot create stream object.");
282 goto error;
283 }
284
285 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream);
286 if (!dmesg_comp->packet) {
287 BT_LOGE_STR("Cannot create packet object.");
288 goto error;
289 }
290
291 ret = bt_trace_make_static(dmesg_comp->trace);
292 if (ret) {
293 BT_LOGE_STR("Cannot make trace static.");
294 goto error;
295 }
296
297 goto end;
298
299 error:
300 ret = -1;
301
302 end:
303 if (basename) {
304 g_free(basename);
305 }
306
307 return ret;
308 }
309
310 static
311 int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp,
312 bool has_ts)
313 {
314 int ret = 0;
315
316 if (dmesg_comp->trace) {
317 /* Already created */
318 goto end;
319 }
320
321 ret = create_meta(dmesg_comp, has_ts);
322 if (ret) {
323 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
324 dmesg_comp);
325 goto error;
326 }
327
328 ret = create_packet_and_stream_and_trace(dmesg_comp);
329 if (ret) {
330 BT_LOGE("Cannot create packet and stream objects: "
331 "dmesg-comp-addr=%p", dmesg_comp);
332 goto error;
333 }
334
335 goto end;
336
337 error:
338 ret = -1;
339
340 end:
341 return ret;
342 }
343
344 static
345 void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
346 {
347 if (!dmesg_comp) {
348 return;
349 }
350
351 if (dmesg_comp->params.path) {
352 g_string_free(dmesg_comp->params.path, TRUE);
353 }
354
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);
361 g_free(dmesg_comp);
362 }
363
364 static
365 bt_self_component_status create_port(
366 bt_self_component_source *self_comp)
367 {
368 return bt_self_component_source_add_output_port(self_comp,
369 "out", NULL, NULL);
370 }
371
372 BT_HIDDEN
373 bt_self_component_status dmesg_init(
374 bt_self_component_source *self_comp,
375 bt_value *params, void *init_method_data)
376 {
377 int ret = 0;
378 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
379 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
380
381 if (!dmesg_comp) {
382 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
383 goto error;
384 }
385
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.");
390 goto error;
391 }
392
393 ret = handle_params(dmesg_comp, params);
394 if (ret) {
395 BT_LOGE("Invalid parameters: comp-addr=%p", self_comp);
396 goto error;
397 }
398
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);
405 goto error;
406 }
407
408 status = create_port(self_comp);
409 if (status != BT_SELF_COMPONENT_STATUS_OK) {
410 goto error;
411 }
412
413 bt_self_component_set_data(
414 bt_self_component_source_as_self_component(self_comp),
415 dmesg_comp);
416 goto end;
417
418 error:
419 destroy_dmesg_component(dmesg_comp);
420 bt_self_component_set_data(
421 bt_self_component_source_as_self_component(self_comp),
422 NULL);
423
424 if (status >= 0) {
425 status = BT_SELF_COMPONENT_STATUS_ERROR;
426 }
427
428 end:
429 return status;
430 }
431
432 BT_HIDDEN
433 void dmesg_finalize(bt_self_component_source *self_comp)
434 {
435 destroy_dmesg_component(bt_self_component_get_data(
436 bt_self_component_source_as_self_component(self_comp)));
437 }
438
439 static
440 bt_message *create_init_event_msg_from_line(
441 struct dmesg_msg_iter *msg_iter,
442 const char *line, const char **new_start)
443 {
444 bt_event *event;
445 bt_message *msg = NULL;
446 bool has_timestamp = false;
447 unsigned long sec, usec, msec;
448 unsigned int year, mon, mday, hour, min;
449 uint64_t ts = 0;
450 int ret = 0;
451 struct dmesg_component *dmesg_comp = msg_iter->dmesg_comp;
452
453 *new_start = line;
454
455 if (dmesg_comp->params.no_timestamp) {
456 goto skip_ts;
457 }
458
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;
462
463 /*
464 * The clock class we use has a 1 GHz frequency: convert
465 * from µs to ns.
466 */
467 ts *= NSEC_PER_USEC;
468 has_timestamp = true;
469 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
470 &year, &mon, &mday, &hour, &min,
471 &sec, &msec) == 7) {
472 time_t ep_sec;
473 struct tm ti;
474
475 memset(&ti, 0, sizeof(ti));
476 ti.tm_year = year - 1900; /* From 1900 */
477 ti.tm_mon = mon - 1; /* 0 to 11 */
478 ti.tm_mday = mday;
479 ti.tm_hour = hour;
480 ti.tm_min = min;
481 ti.tm_sec = sec;
482
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;
487 }
488
489 has_timestamp = true;
490 }
491
492 if (has_timestamp) {
493 /* Set new start for the message portion of the line */
494 *new_start = strchr(line, ']');
495 BT_ASSERT(*new_start);
496 (*new_start)++;
497
498 if ((*new_start)[0] == ' ') {
499 (*new_start)++;
500 }
501 }
502
503 skip_ts:
504 /*
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.
508 */
509 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
510 if (ret) {
511 /* try_create_meta_stream_packet() logs errors */
512 goto error;
513 }
514
515 msg = bt_message_event_create(msg_iter->pc_msg_iter,
516 dmesg_comp->event_class, dmesg_comp->packet);
517 if (!msg) {
518 BT_LOGE_STR("Cannot create event message.");
519 goto error;
520 }
521
522 event = bt_message_event_borrow_event(msg);
523 BT_ASSERT(event);
524
525 if (dmesg_comp->clock_class) {
526 bt_event_set_default_clock_snapshot(event, ts);
527 }
528
529 goto end;
530
531 error:
532 BT_MESSAGE_PUT_REF_AND_RESET(msg);
533
534 end:
535 return msg;
536 }
537
538 static
539 int fill_event_payload_from_line(const char *line,
540 bt_event *event)
541 {
542 bt_field *ep_field = NULL;
543 bt_field *str_field = NULL;
544 size_t len;
545 int ret;
546
547 ep_field = bt_event_borrow_payload_field(event);
548 BT_ASSERT(ep_field);
549 str_field = bt_field_structure_borrow_member_field_by_index(
550 ep_field, 0);
551 if (!str_field) {
552 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
553 goto error;
554 }
555
556 len = strlen(line);
557 if (line[len - 1] == '\n') {
558 /* Do not include the newline character in the payload */
559 len--;
560 }
561
562 ret = bt_field_string_clear(str_field);
563 if (ret) {
564 BT_LOGE_STR("Cannot clear string field object.");
565 goto error;
566 }
567
568 ret = bt_field_string_append_with_length(str_field, line, len);
569 if (ret) {
570 BT_LOGE("Cannot append value to string field object: "
571 "len=%zu", len);
572 goto error;
573 }
574
575 goto end;
576
577 error:
578 ret = -1;
579
580 end:
581 return ret;
582 }
583
584 static
585 bt_message *create_msg_from_line(
586 struct dmesg_msg_iter *dmesg_msg_iter, const char *line)
587 {
588 bt_event *event = NULL;
589 bt_message *msg = NULL;
590 const char *new_start;
591 int ret;
592
593 msg = create_init_event_msg_from_line(dmesg_msg_iter,
594 line, &new_start);
595 if (!msg) {
596 BT_LOGE_STR("Cannot create and initialize event message from line.");
597 goto error;
598 }
599
600 event = bt_message_event_borrow_event(msg);
601 BT_ASSERT(event);
602 ret = fill_event_payload_from_line(new_start, event);
603 if (ret) {
604 BT_LOGE("Cannot fill event payload field from line: "
605 "ret=%d", ret);
606 goto error;
607 }
608
609 goto end;
610
611 error:
612 BT_MESSAGE_PUT_REF_AND_RESET(msg);
613
614 end:
615 return msg;
616 }
617
618 static
619 void destroy_dmesg_msg_iter(struct dmesg_msg_iter *dmesg_msg_iter)
620 {
621 if (!dmesg_msg_iter) {
622 return;
623 }
624
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", ".");
628 }
629 }
630
631 bt_message_put_ref(dmesg_msg_iter->tmp_event_msg);
632 free(dmesg_msg_iter->linebuf);
633 g_free(dmesg_msg_iter);
634 }
635
636 BT_HIDDEN
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)
641 {
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;
647
648 if (!dmesg_msg_iter) {
649 BT_LOGE_STR("Failed to allocate on dmesg message iterator structure.");
650 goto error;
651 }
652
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;
658
659 if (dmesg_comp->params.read_from_stdin) {
660 dmesg_msg_iter->fp = stdin;
661 } else {
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);
666 goto error;
667 }
668 }
669
670 bt_self_message_iterator_set_data(self_msg_iter,
671 dmesg_msg_iter);
672 goto end;
673
674 error:
675 destroy_dmesg_msg_iter(dmesg_msg_iter);
676 bt_self_message_iterator_set_data(self_msg_iter, NULL);
677 if (status >= 0) {
678 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
679 }
680
681 end:
682 return status;
683 }
684
685 BT_HIDDEN
686 void dmesg_msg_iter_finalize(
687 bt_self_message_iterator *priv_msg_iter)
688 {
689 destroy_dmesg_msg_iter(bt_self_message_iterator_get_data(
690 priv_msg_iter));
691 }
692
693 static
694 bt_self_message_iterator_status dmesg_msg_iter_next_one(
695 struct dmesg_msg_iter *dmesg_msg_iter,
696 bt_message **msg)
697 {
698 ssize_t len;
699 struct dmesg_component *dmesg_comp;
700 bt_self_message_iterator_status status =
701 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
702
703 BT_ASSERT(dmesg_msg_iter);
704 dmesg_comp = dmesg_msg_iter->dmesg_comp;
705 BT_ASSERT(dmesg_comp);
706
707 if (dmesg_msg_iter->state == STATE_DONE) {
708 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
709 goto end;
710 }
711
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) {
715 goto handle_state;
716 }
717
718 while (true) {
719 const char *ch;
720 bool only_spaces = true;
721
722 len = bt_getline(&dmesg_msg_iter->linebuf,
723 &dmesg_msg_iter->linebuf_len, dmesg_msg_iter->fp);
724 if (len < 0) {
725 if (errno == EINVAL) {
726 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
727 } else if (errno == ENOMEM) {
728 status =
729 BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM;
730 } else {
731 if (dmesg_msg_iter->state == STATE_EMIT_STREAM_BEGINNING) {
732 /* Stream did not even begin */
733 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
734 goto end;
735 } else {
736 /* End current packet now */
737 dmesg_msg_iter->state =
738 STATE_EMIT_PACKET_END;
739 goto handle_state;
740 }
741 }
742
743 goto end;
744 }
745
746 BT_ASSERT(dmesg_msg_iter->linebuf);
747
748 /* Ignore empty lines, once trimmed */
749 for (ch = dmesg_msg_iter->linebuf; *ch != '\0'; ch++) {
750 if (!isspace(*ch)) {
751 only_spaces = false;
752 break;
753 }
754 }
755
756 if (!only_spaces) {
757 break;
758 }
759 }
760
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);
767 goto end;
768 }
769
770 handle_state:
771 BT_ASSERT(dmesg_comp->trace);
772
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;
779 break;
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;
785 break;
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;
790 break;
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;
795 break;
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;
800 break;
801 default:
802 break;
803 }
804
805 if (!*msg) {
806 BT_LOGE("Cannot create message: dmesg-comp-addr=%p",
807 dmesg_comp);
808 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
809 }
810
811 end:
812 return status;
813 }
814
815 BT_HIDDEN
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,
819 uint64_t *count)
820 {
821 struct dmesg_msg_iter *dmesg_msg_iter =
822 bt_self_message_iterator_get_data(
823 self_msg_iter);
824 bt_self_message_iterator_status status =
825 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
826 uint64_t i = 0;
827
828 while (i < capacity &&
829 status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
830 bt_message *priv_msg = NULL;
831
832 status = dmesg_msg_iter_next_one(dmesg_msg_iter,
833 &priv_msg);
834 msgs[i] = priv_msg;
835 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
836 i++;
837 }
838 }
839
840 if (i > 0) {
841 /*
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.
852 */
853 *count = i;
854 status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
855 }
856
857 return status;
858 }
This page took 0.045091 seconds and 3 git commands to generate.