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