lib: use priv connection priv notif iterator to create notif, not graph
[babeltrace.git] / plugins / text / dmesg / dmesg.c
1 /*
2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
3 * Copyright 2017 Philippe Proulx <jeremie.galarneau@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/babeltrace.h>
33 #include <babeltrace/values-internal.h>
34 #include <babeltrace/compat/utc-internal.h>
35 #include <babeltrace/compat/stdio-internal.h>
36 #include <glib.h>
37
38 #define NSEC_PER_USEC 1000UL
39 #define NSEC_PER_MSEC 1000000UL
40 #define NSEC_PER_SEC 1000000000ULL
41 #define USEC_PER_SEC 1000000UL
42
43 struct dmesg_component;
44
45 struct dmesg_notif_iter {
46 struct dmesg_component *dmesg_comp;
47 struct bt_private_connection_private_notification_iterator *pc_notif_iter; /* Weak */
48 char *linebuf;
49 size_t linebuf_len;
50 FILE *fp;
51 struct bt_notification *tmp_event_notif;
52
53 enum {
54 STATE_EMIT_STREAM_BEGINNING,
55 STATE_EMIT_PACKET_BEGINNING,
56 STATE_EMIT_EVENT,
57 STATE_EMIT_PACKET_END,
58 STATE_EMIT_STREAM_END,
59 STATE_DONE,
60 } state;
61 };
62
63 struct dmesg_component {
64 struct {
65 GString *path;
66 bt_bool read_from_stdin;
67 bt_bool no_timestamp;
68 } params;
69
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;
77 };
78
79 static
80 struct bt_field_type *create_packet_header_ft(void)
81 {
82 struct bt_field_type *root_ft = NULL;
83 struct bt_field_type *ft = NULL;
84 int ret;
85
86 root_ft = bt_field_type_structure_create();
87 if (!root_ft) {
88 BT_LOGE_STR("Cannot create an empty structure field type object.");
89 goto error;
90 }
91
92 ft = bt_field_type_integer_create(32);
93 if (!ft) {
94 BT_LOGE_STR("Cannot create an integer field type object.");
95 goto error;
96 }
97
98 ret = bt_field_type_structure_add_field(root_ft, ft, "magic");
99 if (ret) {
100 BT_LOGE("Cannot add `magic` field type to structure field type: "
101 "ret=%d", ret);
102 goto error;
103 }
104
105 BT_PUT(ft);
106 ft = bt_field_type_integer_create(8);
107 if (!ft) {
108 BT_LOGE_STR("Cannot create an integer field type object.");
109 goto error;
110 }
111
112 goto end;
113
114 error:
115 BT_PUT(root_ft);
116
117 end:
118 bt_put(ft);
119 return root_ft;
120 }
121
122 static
123 struct bt_field_type *create_event_header_ft(
124 struct bt_clock_class *clock_class)
125 {
126 struct bt_field_type *root_ft = NULL;
127 struct bt_field_type *ft = NULL;
128 int ret;
129
130 root_ft = bt_field_type_structure_create();
131 if (!root_ft) {
132 BT_LOGE_STR("Cannot create an empty structure field type object.");
133 goto error;
134 }
135
136 ft = bt_field_type_integer_create(64);
137 if (!ft) {
138 BT_LOGE_STR("Cannot create an integer field type object.");
139 goto error;
140 }
141
142 ret = bt_field_type_integer_set_mapped_clock_class(ft, clock_class);
143 if (ret) {
144 BT_LOGE("Cannot map integer field type to clock class: "
145 "ret=%d", ret);
146 goto error;
147 }
148
149 ret = bt_field_type_structure_add_field(root_ft,
150 ft, "timestamp");
151 if (ret) {
152 BT_LOGE("Cannot add `timestamp` field type to structure field type: "
153 "ret=%d", ret);
154 goto error;
155 }
156
157 goto end;
158
159 error:
160 BT_PUT(root_ft);
161
162 end:
163 bt_put(ft);
164 return root_ft;
165 }
166
167 static
168 struct bt_field_type *create_event_payload_ft(void)
169 {
170 struct bt_field_type *root_ft = NULL;
171 struct bt_field_type *ft = NULL;
172 int ret;
173
174 root_ft = bt_field_type_structure_create();
175 if (!root_ft) {
176 BT_LOGE_STR("Cannot create an empty structure field type object.");
177 goto error;
178 }
179
180 ft = bt_field_type_string_create();
181 if (!ft) {
182 BT_LOGE_STR("Cannot create a string field type object.");
183 goto error;
184 }
185
186 ret = bt_field_type_structure_add_field(root_ft,
187 ft, "str");
188 if (ret) {
189 BT_LOGE("Cannot add `str` field type to structure field type: "
190 "ret=%d", ret);
191 goto error;
192 }
193
194 goto end;
195
196 error:
197 BT_PUT(root_ft);
198
199 end:
200 bt_put(ft);
201 return root_ft;
202 }
203
204 static
205 struct bt_clock_class *create_clock_class(void)
206 {
207 return bt_clock_class_create("the_clock", 1000000000);
208 }
209
210 static
211 int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
212 {
213 struct bt_field_type *ft = NULL;
214 const char *trace_name = NULL;
215 gchar *basename = NULL;
216 int ret = 0;
217
218 dmesg_comp->trace = bt_trace_create();
219 if (!dmesg_comp->trace) {
220 BT_LOGE_STR("Cannot create an empty trace object.");
221 goto error;
222 }
223
224 ft = create_packet_header_ft();
225 if (!ft) {
226 BT_LOGE_STR("Cannot create packet header field type.");
227 goto error;
228 }
229
230 ret = bt_trace_set_packet_header_field_type(dmesg_comp->trace, ft);
231 if (ret) {
232 BT_LOGE_STR("Cannot set trace's packet header field type.");
233 goto error;
234 }
235
236 if (dmesg_comp->params.read_from_stdin) {
237 trace_name = "STDIN";
238 } else {
239 basename = g_path_get_basename(dmesg_comp->params.path->str);
240 BT_ASSERT(basename);
241
242 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
243 strcmp(basename, ".") != 0) {
244 trace_name = basename;
245 }
246 }
247
248 if (trace_name) {
249 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
250 if (ret) {
251 BT_LOGE("Cannot set trace's name: name=\"%s\"", trace_name);
252 goto error;
253 }
254 }
255
256 dmesg_comp->stream_class = bt_stream_class_create(NULL);
257 if (!dmesg_comp->stream_class) {
258 BT_LOGE_STR("Cannot create an empty stream class object.");
259 goto error;
260 }
261
262 if (has_ts) {
263 dmesg_comp->clock_class = create_clock_class();
264 if (!dmesg_comp->clock_class) {
265 BT_LOGE_STR("Cannot create clock class.");
266 goto error;
267 }
268
269 ret = bt_trace_add_clock_class(dmesg_comp->trace,
270 dmesg_comp->clock_class);
271 if (ret) {
272 BT_LOGE_STR("Cannot add clock class to trace.");
273 goto error;
274 }
275
276 bt_put(ft);
277 ft = create_event_header_ft(dmesg_comp->clock_class);
278 if (!ft) {
279 BT_LOGE_STR("Cannot create event header field type.");
280 goto error;
281 }
282
283 ret = bt_stream_class_set_event_header_field_type(
284 dmesg_comp->stream_class, ft);
285 if (ret) {
286 BT_LOGE_STR("Cannot set stream class's event header field type.");
287 goto error;
288 }
289 }
290
291 dmesg_comp->event_class = bt_event_class_create("string");
292 if (!dmesg_comp->event_class) {
293 BT_LOGE_STR("Cannot create an empty event class object.");
294 goto error;
295 }
296
297 bt_put(ft);
298 ft = create_event_payload_ft();
299 if (!ft) {
300 BT_LOGE_STR("Cannot create event payload field type.");
301 goto error;
302 }
303
304 ret = bt_event_class_set_payload_field_type(dmesg_comp->event_class, ft);
305 if (ret) {
306 BT_LOGE_STR("Cannot set event class's event payload field type.");
307 goto error;
308 }
309
310 ret = bt_stream_class_add_event_class(dmesg_comp->stream_class,
311 dmesg_comp->event_class);
312 if (ret) {
313 BT_LOGE("Cannot add event class to stream class: ret=%d", ret);
314 goto error;
315 }
316
317 ret = bt_trace_add_stream_class(dmesg_comp->trace,
318 dmesg_comp->stream_class);
319 if (ret) {
320 BT_LOGE("Cannot add event class to stream class: ret=%d", ret);
321 goto error;
322 }
323
324 goto end;
325
326 error:
327 ret = -1;
328
329 end:
330 bt_put(ft);
331
332 if (basename) {
333 g_free(basename);
334 }
335
336 return ret;
337 }
338
339 static
340 int handle_params(struct dmesg_component *dmesg_comp, struct bt_value *params)
341 {
342 struct bt_value *read_from_stdin = NULL;
343 struct bt_value *no_timestamp = NULL;
344 struct bt_value *path = NULL;
345 const char *path_str;
346 int ret = 0;
347
348 no_timestamp = bt_value_map_get(params, "no-extract-timestamp");
349 if (no_timestamp) {
350 if (!bt_value_is_bool(no_timestamp)) {
351 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
352 "type=%s",
353 bt_value_type_string(
354 bt_value_get_type(no_timestamp)));
355 goto error;
356 }
357
358 ret = bt_value_bool_get(no_timestamp,
359 &dmesg_comp->params.no_timestamp);
360 BT_ASSERT(ret == 0);
361 }
362
363 path = bt_value_map_get(params, "path");
364 if (path) {
365 if (dmesg_comp->params.read_from_stdin) {
366 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
367 goto error;
368 }
369
370 if (!bt_value_is_string(path)) {
371 BT_LOGE("Expecting a string value for the `path` parameter: "
372 "type=%s",
373 bt_value_type_string(
374 bt_value_get_type(path)));
375 goto error;
376 }
377
378 ret = bt_value_string_get(path, &path_str);
379 BT_ASSERT(ret == 0);
380 g_string_assign(dmesg_comp->params.path, path_str);
381 } else {
382 dmesg_comp->params.read_from_stdin = true;
383 }
384
385 goto end;
386
387 error:
388 ret = -1;
389
390 end:
391 bt_put(read_from_stdin);
392 bt_put(path);
393 bt_put(no_timestamp);
394 return ret;
395 }
396
397 static
398 int fill_packet_header_field(struct bt_packet *packet)
399 {
400 struct bt_field *ph = NULL;
401 struct bt_field *magic = NULL;
402 int ret;
403
404 ph = bt_packet_borrow_header(packet);
405 BT_ASSERT(ph);
406 magic = bt_field_structure_borrow_field_by_name(ph, "magic");
407 if (!magic) {
408 BT_LOGE_STR("Cannot borrow `magic` field from structure field.");
409 goto error;
410 }
411
412 ret = bt_field_integer_unsigned_set_value(magic, 0xc1fc1fc1);
413 BT_ASSERT(ret == 0);
414 goto end;
415
416 error:
417 ret = -1;
418
419 end:
420 return ret;
421 }
422
423 static
424 int create_packet_and_stream(struct dmesg_component *dmesg_comp)
425 {
426 int ret = 0;
427
428 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class,
429 NULL, 0);
430 if (!dmesg_comp->stream) {
431 BT_LOGE_STR("Cannot create stream object.");
432 goto error;
433 }
434
435 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream);
436 if (!dmesg_comp->packet) {
437 BT_LOGE_STR("Cannot create packet object.");
438 goto error;
439 }
440
441 ret = fill_packet_header_field(dmesg_comp->packet);
442 if (ret) {
443 BT_LOGE_STR("Cannot fill packet header field.");
444 goto error;
445 }
446
447 ret = bt_trace_set_is_static(dmesg_comp->trace);
448 if (ret) {
449 BT_LOGE_STR("Cannot make trace static.");
450 goto error;
451 }
452
453 goto end;
454
455 error:
456 ret = -1;
457
458 end:
459 return ret;
460 }
461
462 static
463 int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp,
464 bool has_ts)
465 {
466 int ret = 0;
467
468 if (dmesg_comp->trace) {
469 /* Already created */
470 goto end;
471 }
472
473 ret = create_meta(dmesg_comp, has_ts);
474 if (ret) {
475 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
476 dmesg_comp);
477 goto error;
478 }
479
480 ret = create_packet_and_stream(dmesg_comp);
481 if (ret) {
482 BT_LOGE("Cannot create packet and stream objects: "
483 "dmesg-comp-addr=%p", dmesg_comp);
484 goto error;
485 }
486
487 goto end;
488
489 error:
490 ret = -1;
491
492 end:
493 return ret;
494 }
495
496 static
497 void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
498 {
499 if (!dmesg_comp) {
500 return;
501 }
502
503 if (dmesg_comp->params.path) {
504 g_string_free(dmesg_comp->params.path, TRUE);
505 }
506
507 bt_put(dmesg_comp->packet);
508 bt_put(dmesg_comp->trace);
509 bt_put(dmesg_comp->stream_class);
510 bt_put(dmesg_comp->event_class);
511 bt_put(dmesg_comp->stream);
512 bt_put(dmesg_comp->clock_class);
513 bt_put(dmesg_comp->cc_prio_map);
514 g_free(dmesg_comp);
515 }
516
517 static
518 enum bt_component_status create_port(struct bt_private_component *priv_comp)
519 {
520 return bt_private_component_source_add_output_private_port(priv_comp,
521 "out", NULL, NULL);
522 }
523
524 BT_HIDDEN
525 enum bt_component_status dmesg_init(struct bt_private_component *priv_comp,
526 struct bt_value *params, void *init_method_data)
527 {
528 int ret = 0;
529 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
530 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
531
532 if (!dmesg_comp) {
533 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
534 goto error;
535 }
536
537 dmesg_comp->params.path = g_string_new(NULL);
538 if (!dmesg_comp->params.path) {
539 BT_LOGE_STR("Failed to allocate a GString.");
540 goto error;
541 }
542
543 ret = handle_params(dmesg_comp, params);
544 if (ret) {
545 BT_LOGE("Invalid parameters: comp-addr=%p", priv_comp);
546 goto error;
547 }
548
549 if (!dmesg_comp->params.read_from_stdin &&
550 !g_file_test(dmesg_comp->params.path->str,
551 G_FILE_TEST_IS_REGULAR)) {
552 BT_LOGE("Input path is not a regular file: "
553 "comp-addr=%p, path=\"%s\"", priv_comp,
554 dmesg_comp->params.path->str);
555 goto error;
556 }
557
558 status = create_port(priv_comp);
559 if (status != BT_COMPONENT_STATUS_OK) {
560 goto error;
561 }
562
563 (void) bt_private_component_set_user_data(priv_comp, dmesg_comp);
564 goto end;
565
566 error:
567 destroy_dmesg_component(dmesg_comp);
568 (void) bt_private_component_set_user_data(priv_comp, NULL);
569
570 if (status >= 0) {
571 status = BT_COMPONENT_STATUS_ERROR;
572 }
573
574 end:
575 return status;
576 }
577
578 BT_HIDDEN
579 void dmesg_finalize(struct bt_private_component *priv_comp)
580 {
581 void *data = bt_private_component_get_user_data(priv_comp);
582
583 destroy_dmesg_component(data);
584 }
585
586 static
587 struct bt_notification *create_init_event_notif_from_line(
588 struct dmesg_notif_iter *notif_iter,
589 const char *line, const char **new_start)
590 {
591 struct bt_event *event;
592 struct bt_notification *notif = NULL;
593 bool has_timestamp = false;
594 unsigned long sec, usec, msec;
595 unsigned int year, mon, mday, hour, min;
596 uint64_t ts = 0;
597 struct bt_field *eh_field = NULL;
598 struct bt_field *ts_field = NULL;
599 int ret = 0;
600 struct dmesg_component *dmesg_comp = notif_iter->dmesg_comp;
601
602 *new_start = line;
603
604 if (dmesg_comp->params.no_timestamp) {
605 goto skip_ts;
606 }
607
608 /* Extract time from input line */
609 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
610 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
611
612 /*
613 * The clock class we use has a 1 GHz frequency: convert
614 * from µs to ns.
615 */
616 ts *= NSEC_PER_USEC;
617 has_timestamp = true;
618 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
619 &year, &mon, &mday, &hour, &min,
620 &sec, &msec) == 7) {
621 time_t ep_sec;
622 struct tm ti;
623
624 memset(&ti, 0, sizeof(ti));
625 ti.tm_year = year - 1900; /* From 1900 */
626 ti.tm_mon = mon - 1; /* 0 to 11 */
627 ti.tm_mday = mday;
628 ti.tm_hour = hour;
629 ti.tm_min = min;
630 ti.tm_sec = sec;
631
632 ep_sec = bt_timegm(&ti);
633 if (ep_sec != (time_t) -1) {
634 ts = (uint64_t) ep_sec * NSEC_PER_SEC
635 + (uint64_t) msec * NSEC_PER_MSEC;
636 }
637
638 has_timestamp = true;
639 }
640
641 if (has_timestamp) {
642 /* Set new start for the message portion of the line */
643 *new_start = strchr(line, ']');
644 BT_ASSERT(*new_start);
645 (*new_start)++;
646
647 if ((*new_start)[0] == ' ') {
648 (*new_start)++;
649 }
650 }
651
652 skip_ts:
653 /*
654 * At this point, we know if the stream class's event header
655 * field type should have a timestamp or not, so we can lazily
656 * create the metadata, stream, and packet objects.
657 */
658 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
659 if (ret) {
660 /* try_create_meta_stream_packet() logs errors */
661 goto error;
662 }
663
664 notif = bt_notification_event_create(notif_iter->pc_notif_iter,
665 dmesg_comp->event_class, dmesg_comp->packet);
666 if (!notif) {
667 BT_LOGE_STR("Cannot create event notification.");
668 goto error;
669 }
670
671 event = bt_notification_event_borrow_event(notif);
672 BT_ASSERT(event);
673
674 if (dmesg_comp->clock_class) {
675 ret = bt_event_set_clock_value(event,
676 dmesg_comp->clock_class, ts, BT_TRUE);
677 BT_ASSERT(ret == 0);
678 eh_field = bt_event_borrow_header(event);
679 BT_ASSERT(eh_field);
680 ts_field = bt_field_structure_borrow_field_by_name(eh_field,
681 "timestamp");
682 if (!ts_field) {
683 BT_LOGE_STR("Cannot borrow `timestamp` field from event header structure field.");
684 goto error;
685 }
686
687 ret = bt_field_integer_unsigned_set_value(ts_field, ts);
688 BT_ASSERT(ret == 0);
689 }
690
691 goto end;
692
693 error:
694 BT_PUT(notif);
695
696 end:
697 return notif;
698 }
699
700 static
701 int fill_event_payload_from_line(const char *line, struct bt_event *event)
702 {
703 struct bt_field *ep_field = NULL;
704 struct bt_field *str_field = NULL;
705 size_t len;
706 int ret;
707
708 ep_field = bt_event_borrow_payload(event);
709 BT_ASSERT(ep_field);
710 str_field = bt_field_structure_borrow_field_by_name(ep_field, "str");
711 if (!str_field) {
712 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
713 goto error;
714 }
715
716 len = strlen(line);
717 if (line[len - 1] == '\n') {
718 /* Do not include the newline character in the payload */
719 len--;
720 }
721
722 ret = bt_field_string_clear(str_field);
723 if (ret) {
724 BT_LOGE_STR("Cannot clear string field object.");
725 goto error;
726 }
727
728 ret = bt_field_string_append_len(str_field, line, len);
729 if (ret) {
730 BT_LOGE("Cannot append value to string field object: "
731 "len=%zu", len);
732 goto error;
733 }
734
735 goto end;
736
737 error:
738 ret = -1;
739
740 end:
741 return ret;
742 }
743
744 static
745 struct bt_notification *create_notif_from_line(
746 struct dmesg_notif_iter *dmesg_notif_iter, const char *line)
747 {
748 struct bt_event *event = NULL;
749 struct bt_notification *notif = NULL;
750 const char *new_start;
751 int ret;
752
753 notif = create_init_event_notif_from_line(dmesg_notif_iter,
754 line, &new_start);
755 if (!notif) {
756 BT_LOGE_STR("Cannot create and initialize event notification from line.");
757 goto error;
758 }
759
760 event = bt_notification_event_borrow_event(notif);
761 BT_ASSERT(event);
762 ret = fill_event_payload_from_line(new_start, event);
763 if (ret) {
764 BT_LOGE("Cannot fill event payload field from line: "
765 "ret=%d", ret);
766 goto error;
767 }
768
769 goto end;
770
771 error:
772 BT_PUT(notif);
773
774 end:
775 return notif;
776 }
777
778 static
779 void destroy_dmesg_notif_iter(struct dmesg_notif_iter *dmesg_notif_iter)
780 {
781 if (!dmesg_notif_iter) {
782 return;
783 }
784
785 if (dmesg_notif_iter->fp && dmesg_notif_iter->fp != stdin) {
786 if (fclose(dmesg_notif_iter->fp)) {
787 BT_LOGE_ERRNO("Cannot close input file", ".");
788 }
789 }
790
791 bt_put(dmesg_notif_iter->tmp_event_notif);
792 free(dmesg_notif_iter->linebuf);
793 g_free(dmesg_notif_iter);
794 }
795
796 BT_HIDDEN
797 enum bt_notification_iterator_status dmesg_notif_iter_init(
798 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
799 struct bt_private_port *priv_port)
800 {
801 struct bt_private_component *priv_comp = NULL;
802 struct dmesg_component *dmesg_comp;
803 struct dmesg_notif_iter *dmesg_notif_iter =
804 g_new0(struct dmesg_notif_iter, 1);
805 enum bt_notification_iterator_status status =
806 BT_NOTIFICATION_ITERATOR_STATUS_OK;
807
808 if (!dmesg_notif_iter) {
809 BT_LOGE_STR("Failed to allocate on dmesg notification iterator structure.");
810 goto error;
811 }
812
813 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
814 priv_notif_iter);
815 BT_ASSERT(priv_comp);
816 dmesg_comp = bt_private_component_get_user_data(priv_comp);
817 BT_ASSERT(dmesg_comp);
818 dmesg_notif_iter->dmesg_comp = dmesg_comp;
819 dmesg_notif_iter->pc_notif_iter = priv_notif_iter;
820
821 if (dmesg_comp->params.read_from_stdin) {
822 dmesg_notif_iter->fp = stdin;
823 } else {
824 dmesg_notif_iter->fp = fopen(dmesg_comp->params.path->str, "r");
825 if (!dmesg_notif_iter->fp) {
826 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
827 dmesg_comp->params.path->str);
828 goto error;
829 }
830 }
831
832 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
833 dmesg_notif_iter);
834 goto end;
835
836 error:
837 destroy_dmesg_notif_iter(dmesg_notif_iter);
838 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
839 NULL);
840 if (status >= 0) {
841 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
842 }
843
844 end:
845 bt_put(priv_comp);
846 return status;
847 }
848
849 BT_HIDDEN
850 void dmesg_notif_iter_finalize(
851 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
852 {
853 destroy_dmesg_notif_iter(bt_private_connection_private_notification_iterator_get_user_data(
854 priv_notif_iter));
855 }
856
857 static
858 enum bt_notification_iterator_status dmesg_notif_iter_next_one(
859 struct dmesg_notif_iter *dmesg_notif_iter,
860 struct bt_notification **notif)
861 {
862 ssize_t len;
863 struct dmesg_component *dmesg_comp;
864 enum bt_notification_iterator_status status =
865 BT_NOTIFICATION_ITERATOR_STATUS_OK;
866
867 BT_ASSERT(dmesg_notif_iter);
868 dmesg_comp = dmesg_notif_iter->dmesg_comp;
869 BT_ASSERT(dmesg_comp);
870
871 if (dmesg_notif_iter->state == STATE_DONE) {
872 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
873 goto end;
874 }
875
876 if (dmesg_notif_iter->tmp_event_notif ||
877 dmesg_notif_iter->state == STATE_EMIT_PACKET_END ||
878 dmesg_notif_iter->state == STATE_EMIT_STREAM_END) {
879 goto handle_state;
880 }
881
882 while (true) {
883 const char *ch;
884 bool only_spaces = true;
885
886 len = bt_getline(&dmesg_notif_iter->linebuf,
887 &dmesg_notif_iter->linebuf_len, dmesg_notif_iter->fp);
888 if (len < 0) {
889 if (errno == EINVAL) {
890 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
891 } else if (errno == ENOMEM) {
892 status =
893 BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
894 } else {
895 if (dmesg_notif_iter->state == STATE_EMIT_STREAM_BEGINNING) {
896 /* Stream did not even begin */
897 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
898 goto end;
899 } else {
900 /* End current packet now */
901 dmesg_notif_iter->state =
902 STATE_EMIT_PACKET_END;
903 goto handle_state;
904 }
905 }
906
907 goto end;
908 }
909
910 BT_ASSERT(dmesg_notif_iter->linebuf);
911
912 /* Ignore empty lines, once trimmed */
913 for (ch = dmesg_notif_iter->linebuf; *ch != '\0'; ch++) {
914 if (!isspace(*ch)) {
915 only_spaces = false;
916 break;
917 }
918 }
919
920 if (!only_spaces) {
921 break;
922 }
923 }
924
925 dmesg_notif_iter->tmp_event_notif = create_notif_from_line(
926 dmesg_notif_iter, dmesg_notif_iter->linebuf);
927 if (!dmesg_notif_iter->tmp_event_notif) {
928 BT_LOGE("Cannot create event notification from line: "
929 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
930 dmesg_notif_iter->linebuf);
931 goto end;
932 }
933
934 handle_state:
935 BT_ASSERT(dmesg_comp->trace);
936
937 switch (dmesg_notif_iter->state) {
938 case STATE_EMIT_STREAM_BEGINNING:
939 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
940 *notif = bt_notification_stream_begin_create(
941 dmesg_notif_iter->pc_notif_iter, dmesg_comp->stream);
942 dmesg_notif_iter->state = STATE_EMIT_PACKET_BEGINNING;
943 break;
944 case STATE_EMIT_PACKET_BEGINNING:
945 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
946 *notif = bt_notification_packet_begin_create(
947 dmesg_notif_iter->pc_notif_iter, dmesg_comp->packet);
948 dmesg_notif_iter->state = STATE_EMIT_EVENT;
949 break;
950 case STATE_EMIT_EVENT:
951 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
952 *notif = dmesg_notif_iter->tmp_event_notif;
953 dmesg_notif_iter->tmp_event_notif = NULL;
954 break;
955 case STATE_EMIT_PACKET_END:
956 *notif = bt_notification_packet_end_create(
957 dmesg_notif_iter->pc_notif_iter, dmesg_comp->packet);
958 dmesg_notif_iter->state = STATE_EMIT_STREAM_END;
959 break;
960 case STATE_EMIT_STREAM_END:
961 *notif = bt_notification_stream_end_create(
962 dmesg_notif_iter->pc_notif_iter, dmesg_comp->stream);
963 dmesg_notif_iter->state = STATE_DONE;
964 break;
965 default:
966 break;
967 }
968
969 if (!*notif) {
970 BT_LOGE("Cannot create notification: dmesg-comp-addr=%p",
971 dmesg_comp);
972 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
973 }
974
975 end:
976 return status;
977 }
978
979 BT_HIDDEN
980 enum bt_notification_iterator_status dmesg_notif_iter_next(
981 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
982 bt_notification_array notifs, uint64_t capacity,
983 uint64_t *count)
984 {
985 struct dmesg_notif_iter *dmesg_notif_iter =
986 bt_private_connection_private_notification_iterator_get_user_data(
987 priv_notif_iter);
988 enum bt_notification_iterator_status status =
989 BT_NOTIFICATION_ITERATOR_STATUS_OK;
990 uint64_t i = 0;
991
992 while (i < capacity && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
993 status = dmesg_notif_iter_next_one(dmesg_notif_iter, &notifs[i]);
994 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
995 i++;
996 }
997 }
998
999 if (i > 0) {
1000 /*
1001 * Even if dmesg_notif_iter_next_one() returned
1002 * something else than
1003 * BT_NOTIFICATION_ITERATOR_STATUS_OK, we accumulated
1004 * notification objects in the output notification
1005 * array, so we need to return
1006 * BT_NOTIFICATION_ITERATOR_STATUS_OK so that they are
1007 * transfered to downstream. This other status occurs
1008 * again the next time muxer_notif_iter_do_next() is
1009 * called, possibly without any accumulated
1010 * notification, in which case we'll return it.
1011 */
1012 *count = i;
1013 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
1014 }
1015
1016 return status;
1017 }
This page took 0.050895 seconds and 5 git commands to generate.