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