Split notification iterator API into base and specialized functions
[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 <assert.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_ctf_trace *trace;
60 struct bt_ctf_stream_class *stream_class;
61 struct bt_ctf_event_class *event_class;
62 struct bt_ctf_stream *stream;
63 struct bt_ctf_packet *packet;
64 struct bt_ctf_clock_class *clock_class;
65 struct bt_clock_class_priority_map *cc_prio_map;
66 };
67
68 static
69 struct bt_ctf_field_type *create_packet_header_ft(void)
70 {
71 struct bt_ctf_field_type *root_ft = NULL;
72 struct bt_ctf_field_type *ft = NULL;
73 int ret;
74
75 root_ft = bt_ctf_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_ctf_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_ctf_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_ctf_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_ctf_field_type *create_packet_context_ft(void)
113 {
114 struct bt_ctf_field_type *root_ft = NULL;
115 struct bt_ctf_field_type *ft = NULL;
116 int ret;
117
118 root_ft = bt_ctf_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_ctf_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_ctf_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_ctf_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_ctf_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_ctf_field_type *create_event_header_ft(
165 struct bt_ctf_clock_class *clock_class)
166 {
167 struct bt_ctf_field_type *root_ft = NULL;
168 struct bt_ctf_field_type *ft = NULL;
169 int ret;
170
171 root_ft = bt_ctf_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_ctf_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_ctf_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_ctf_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_ctf_field_type *create_event_payload_ft(void)
210 {
211 struct bt_ctf_field_type *root_ft = NULL;
212 struct bt_ctf_field_type *ft = NULL;
213 int ret;
214
215 root_ft = bt_ctf_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_ctf_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_ctf_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_ctf_clock_class *create_clock_class(void)
247 {
248 return bt_ctf_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_ctf_field_type *ft = NULL;
255 const char *trace_name = NULL;
256 gchar *basename = NULL;
257 int ret = 0;
258
259 dmesg_comp->trace = bt_ctf_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_ctf_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 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_ctf_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_ctf_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_ctf_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_ctf_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_ctf_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_ctf_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_ctf_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_ctf_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_ctf_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 read_from_stdin = bt_value_map_get(params, "read-from-stdin");
417 if (read_from_stdin) {
418 if (!bt_value_is_bool(read_from_stdin)) {
419 BT_LOGE("Expecting a boolean value for the `read-from-stdin` parameter: "
420 "type=%s",
421 bt_value_type_string(
422 bt_value_get_type(read_from_stdin)));
423 goto error;
424 }
425
426 ret = bt_value_bool_get(read_from_stdin,
427 &dmesg_comp->params.read_from_stdin);
428 assert(ret == 0);
429 }
430
431 no_timestamp = bt_value_map_get(params, "no-extract-timestamp");
432 if (no_timestamp) {
433 if (!bt_value_is_bool(no_timestamp)) {
434 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
435 "type=%s",
436 bt_value_type_string(
437 bt_value_get_type(no_timestamp)));
438 goto error;
439 }
440
441 ret = bt_value_bool_get(no_timestamp,
442 &dmesg_comp->params.no_timestamp);
443 assert(ret == 0);
444 }
445
446 path = bt_value_map_get(params, "path");
447 if (path) {
448 if (dmesg_comp->params.read_from_stdin) {
449 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
450 goto error;
451 }
452
453 if (!bt_value_is_string(path)) {
454 BT_LOGE("Expecting a string value for the `path` parameter: "
455 "type=%s",
456 bt_value_type_string(
457 bt_value_get_type(path)));
458 goto error;
459 }
460
461 ret = bt_value_string_get(path, &path_str);
462 assert(ret == 0);
463 g_string_assign(dmesg_comp->params.path, path_str);
464 } else {
465 if (!dmesg_comp->params.read_from_stdin) {
466 BT_LOGE_STR("Expecting `path` parameter or true `read-from-stdin` parameter.");
467 goto error;
468 }
469 }
470
471 goto end;
472
473 error:
474 ret = -1;
475
476 end:
477 bt_put(read_from_stdin);
478 bt_put(path);
479 bt_put(no_timestamp);
480 return ret;
481 }
482
483 static
484 struct bt_ctf_field *create_packet_header_field(struct bt_ctf_field_type *ft)
485 {
486 struct bt_ctf_field *ph = NULL;
487 struct bt_ctf_field *magic = NULL;
488 int ret;
489
490 ph = bt_ctf_field_create(ft);
491 if (!ph) {
492 BT_LOGE_STR("Cannot create field object.");
493 goto error;
494 }
495
496 magic = bt_ctf_field_structure_get_field_by_name(ph, "magic");
497 if (!magic) {
498 BT_LOGE_STR("Cannot get `magic` field from structure field.");
499 goto error;
500 }
501
502 ret = bt_ctf_field_unsigned_integer_set_value(magic, 0xc1fc1fc1);
503 if (ret) {
504 BT_LOGE_STR("Cannot set integer field's value.");
505 goto error;
506 }
507
508 goto end;
509
510 error:
511 BT_PUT(ph);
512
513 end:
514 bt_put(magic);
515 return ph;
516 }
517
518 static
519 struct bt_ctf_field *create_packet_context_field(struct bt_ctf_field_type *ft)
520 {
521 struct bt_ctf_field *pc = NULL;
522 struct bt_ctf_field *field = NULL;
523 int ret;
524
525 pc = bt_ctf_field_create(ft);
526 if (!pc) {
527 BT_LOGE_STR("Cannot create field object.");
528 goto error;
529 }
530
531 field = bt_ctf_field_structure_get_field_by_name(pc, "content_size");
532 if (!field) {
533 BT_LOGE_STR("Cannot get `content_size` field from structure field.");
534 goto error;
535 }
536
537 ret = bt_ctf_field_unsigned_integer_set_value(field, 0);
538 if (ret) {
539 BT_LOGE_STR("Cannot set integer field's value.");
540 goto error;
541 }
542
543 bt_put(field);
544 field = bt_ctf_field_structure_get_field_by_name(pc, "packet_size");
545 if (!field) {
546 BT_LOGE_STR("Cannot get `packet_size` field from structure field.");
547 goto error;
548 }
549
550 ret = bt_ctf_field_unsigned_integer_set_value(field, 0);
551 if (ret) {
552 BT_LOGE_STR("Cannot set integer field's value.");
553 goto error;
554 }
555
556 goto end;
557
558 error:
559 BT_PUT(pc);
560
561 end:
562 bt_put(field);
563 return pc;
564 }
565
566 static
567 int create_packet_and_stream(struct dmesg_component *dmesg_comp)
568 {
569 int ret = 0;
570 struct bt_ctf_field_type *ft = NULL;
571 struct bt_ctf_field *field = NULL;
572
573 dmesg_comp->stream = bt_ctf_stream_create(dmesg_comp->stream_class,
574 NULL);
575 if (!dmesg_comp->stream) {
576 BT_LOGE_STR("Cannot create stream object.");
577 goto error;
578 }
579
580 dmesg_comp->packet = bt_ctf_packet_create(dmesg_comp->stream);
581 if (!dmesg_comp->packet) {
582 BT_LOGE_STR("Cannot create packet object.");
583 goto error;
584 }
585
586 ft = bt_ctf_trace_get_packet_header_type(dmesg_comp->trace);
587 assert(ft);
588 field = create_packet_header_field(ft);
589 if (!field) {
590 BT_LOGE_STR("Cannot create packet header field.");
591 goto error;
592 }
593
594 ret = bt_ctf_packet_set_header(dmesg_comp->packet, field);
595 if (ret) {
596 BT_LOGE_STR("Cannot set packet's header field.");
597 goto error;
598 }
599
600 bt_put(ft);
601 bt_put(field);
602 ft = bt_ctf_stream_class_get_packet_context_type(
603 dmesg_comp->stream_class);
604 assert(ft);
605 field = create_packet_context_field(ft);
606 if (!field) {
607 BT_LOGE_STR("Cannot create packet context field.");
608 goto error;
609 }
610
611 ret = bt_ctf_packet_set_context(dmesg_comp->packet, field);
612 if (ret) {
613 BT_LOGE_STR("Cannot set packet's context field.");
614 goto error;
615 }
616
617 ret = bt_ctf_trace_set_is_static(dmesg_comp->trace);
618 if (ret) {
619 BT_LOGE_STR("Cannot make trace static.");
620 goto error;
621 }
622
623 goto end;
624
625 error:
626 ret = -1;
627
628 end:
629 bt_put(field);
630 bt_put(ft);
631 return ret;
632 }
633
634 static
635 int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp,
636 bool has_ts)
637 {
638 int ret = 0;
639
640 if (dmesg_comp->trace) {
641 /* Already created */
642 goto end;
643 }
644
645 ret = create_meta(dmesg_comp, has_ts);
646 if (ret) {
647 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
648 dmesg_comp);
649 goto error;
650 }
651
652 ret = create_packet_and_stream(dmesg_comp);
653 if (ret) {
654 BT_LOGE("Cannot create packet and stream objects: "
655 "dmesg-comp-addr=%p", dmesg_comp);
656 goto error;
657 }
658
659 goto end;
660
661 error:
662 ret = -1;
663
664 end:
665 return ret;
666 }
667
668 static
669 void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
670 {
671 if (!dmesg_comp) {
672 return;
673 }
674
675 if (dmesg_comp->params.path) {
676 g_string_free(dmesg_comp->params.path, TRUE);
677 }
678
679 bt_put(dmesg_comp->packet);
680 bt_put(dmesg_comp->trace);
681 bt_put(dmesg_comp->stream_class);
682 bt_put(dmesg_comp->event_class);
683 bt_put(dmesg_comp->stream);
684 bt_put(dmesg_comp->clock_class);
685 bt_put(dmesg_comp->cc_prio_map);
686 g_free(dmesg_comp);
687 }
688
689 static
690 enum bt_component_status create_port(struct bt_private_component *priv_comp)
691 {
692 return bt_private_component_source_add_output_private_port(priv_comp,
693 "out", NULL, NULL);
694 }
695
696 BT_HIDDEN
697 enum bt_component_status dmesg_init(struct bt_private_component *priv_comp,
698 struct bt_value *params, void *init_method_data)
699 {
700 int ret = 0;
701 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
702 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
703
704 if (!dmesg_comp) {
705 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
706 goto error;
707 }
708
709 dmesg_comp->params.path = g_string_new(NULL);
710 if (!dmesg_comp->params.path) {
711 BT_LOGE_STR("Failed to allocate a GString.");
712 goto error;
713 }
714
715 ret = handle_params(dmesg_comp, params);
716 if (ret) {
717 BT_LOGE("Invalid parameters: comp-addr=%p", priv_comp);
718 goto error;
719 }
720
721 if (!dmesg_comp->params.read_from_stdin &&
722 !g_file_test(dmesg_comp->params.path->str,
723 G_FILE_TEST_IS_REGULAR)) {
724 BT_LOGE("Input path is not a regular file: "
725 "comp-addr=%p, path=\"%s\"", priv_comp,
726 dmesg_comp->params.path->str);
727 goto error;
728 }
729
730 status = create_port(priv_comp);
731 if (status != BT_COMPONENT_STATUS_OK) {
732 goto error;
733 }
734
735 (void) bt_private_component_set_user_data(priv_comp, dmesg_comp);
736 goto end;
737
738 error:
739 destroy_dmesg_component(dmesg_comp);
740 (void) bt_private_component_set_user_data(priv_comp, NULL);
741
742 if (status >= 0) {
743 status = BT_COMPONENT_STATUS_ERROR;
744 }
745
746 end:
747 return status;
748 }
749
750 BT_HIDDEN
751 void dmesg_finalize(struct bt_private_component *priv_comp)
752 {
753 void *data = bt_private_component_get_user_data(priv_comp);
754
755 destroy_dmesg_component(data);
756 }
757
758 static
759 int create_event_header_from_line(
760 struct dmesg_component *dmesg_comp,
761 const char *line, const char **new_start,
762 struct bt_ctf_field **user_field,
763 struct bt_ctf_clock_value **user_clock_value)
764 {
765 bool has_timestamp = false;
766 unsigned long sec, usec, msec;
767 unsigned int year, mon, mday, hour, min;
768 uint64_t ts = 0;
769 struct bt_ctf_clock_value *clock_value = NULL;
770 struct bt_ctf_field_type *ft = NULL;
771 struct bt_ctf_field *eh_field = NULL;
772 struct bt_ctf_field *ts_field = NULL;
773 int ret = 0;
774
775 assert(user_clock_value);
776 assert(user_field);
777 *new_start = line;
778
779 if (dmesg_comp->params.no_timestamp) {
780 goto skip_ts;
781 }
782
783 /* Extract time from input line */
784 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
785 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
786
787 /*
788 * The clock class we use has a 1 GHz frequency: convert
789 * from µs to ns.
790 */
791 ts *= NSEC_PER_USEC;
792 has_timestamp = true;
793 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
794 &year, &mon, &mday, &hour, &min,
795 &sec, &msec) == 7) {
796 time_t ep_sec;
797 struct tm ti;
798
799 memset(&ti, 0, sizeof(ti));
800 ti.tm_year = year - 1900; /* From 1900 */
801 ti.tm_mon = mon - 1; /* 0 to 11 */
802 ti.tm_mday = mday;
803 ti.tm_hour = hour;
804 ti.tm_min = min;
805 ti.tm_sec = sec;
806
807 ep_sec = bt_timegm(&ti);
808 if (ep_sec != (time_t) -1) {
809 ts = (uint64_t) ep_sec * NSEC_PER_SEC
810 + (uint64_t) msec * NSEC_PER_MSEC;
811 }
812
813 has_timestamp = true;
814 }
815
816 if (has_timestamp) {
817 /* Set new start for the message portion of the line */
818 *new_start = strchr(line, ']');
819 assert(*new_start);
820 (*new_start)++;
821
822 if ((*new_start)[0] == ' ') {
823 (*new_start)++;
824 }
825 }
826
827 skip_ts:
828 /*
829 * At this point, we know if the stream class's event header
830 * field type should have a timestamp or not, so we can lazily
831 * create the metadata, stream, and packet objects.
832 */
833 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
834 if (ret) {
835 /* try_create_meta_stream_packet() logs errors */
836 goto error;
837 }
838
839 if (dmesg_comp->clock_class) {
840 clock_value = bt_ctf_clock_value_create(dmesg_comp->clock_class,
841 ts);
842 if (!clock_value) {
843 BT_LOGE_STR("Cannot create clock value object.");
844 goto error;
845 }
846
847 ft = bt_ctf_stream_class_get_event_header_type(
848 dmesg_comp->stream_class);
849 assert(ft);
850 eh_field = bt_ctf_field_create(ft);
851 if (!eh_field) {
852 BT_LOGE_STR("Cannot create event header field object.");
853 goto error;
854 }
855
856 ts_field = bt_ctf_field_structure_get_field_by_name(eh_field,
857 "timestamp");
858 if (!ts_field) {
859 BT_LOGE_STR("Cannot get `timestamp` field from structure field.");
860 goto error;
861 }
862
863 ret = bt_ctf_field_unsigned_integer_set_value(ts_field, ts);
864 if (ret) {
865 BT_LOGE_STR("Cannot set integer field's value.");
866 goto error;
867 }
868
869 *user_clock_value = clock_value;
870 clock_value = NULL;
871 *user_field = eh_field;
872 eh_field = NULL;
873 }
874
875 goto end;
876
877 error:
878 ret = -1;
879
880 end:
881 bt_put(ft);
882 bt_put(ts_field);
883 bt_put(clock_value);
884 bt_put(eh_field);
885 return ret;
886 }
887
888 static
889 int create_event_payload_from_line(
890 struct dmesg_component *dmesg_comp,
891 const char *line, struct bt_ctf_field **user_field)
892 {
893 struct bt_ctf_field_type *ft = NULL;
894 struct bt_ctf_field *ep_field = NULL;
895 struct bt_ctf_field *str_field = NULL;
896 size_t len;
897 int ret;
898
899 assert(user_field);
900 ft = bt_ctf_event_class_get_payload_type(dmesg_comp->event_class);
901 assert(ft);
902 ep_field = bt_ctf_field_create(ft);
903 if (!ep_field) {
904 BT_LOGE_STR("Cannot create event payload field object.");
905 goto error;
906 }
907
908 str_field = bt_ctf_field_structure_get_field_by_name(ep_field, "str");
909 if (!str_field) {
910 BT_LOGE_STR("Cannot get `timestamp` field from structure field.");
911 goto error;
912 }
913
914 len = strlen(line);
915 if (line[len - 1] == '\n') {
916 /* Do not include the newline character in the payload */
917 len--;
918 }
919
920 ret = bt_ctf_field_string_append_len(str_field, line, len);
921 if (ret) {
922 BT_LOGE("Cannot append value to string field object: "
923 "len=%zu", len);
924 goto error;
925 }
926
927 *user_field = ep_field;
928 ep_field = NULL;
929 goto end;
930
931 error:
932 ret = -1;
933
934 end:
935 bt_put(ft);
936 bt_put(ep_field);
937 bt_put(str_field);
938 return ret;
939 }
940
941 static
942 struct bt_notification *create_notif_from_line(
943 struct dmesg_component *dmesg_comp, const char *line)
944 {
945 struct bt_ctf_field *eh_field = NULL;
946 struct bt_ctf_field *ep_field = NULL;
947 struct bt_ctf_clock_value *clock_value = NULL;
948 struct bt_ctf_event *event = NULL;
949 struct bt_notification *notif = NULL;
950 const char *new_start;
951 int ret;
952
953 ret = create_event_header_from_line(dmesg_comp, line, &new_start,
954 &eh_field, &clock_value);
955 if (ret) {
956 BT_LOGE("Cannot create event header field from line: "
957 "ret=%d", ret);
958 goto error;
959 }
960
961 ret = create_event_payload_from_line(dmesg_comp, new_start,
962 &ep_field);
963 if (ret) {
964 BT_LOGE("Cannot create event payload field from line: "
965 "ret=%d", ret);
966 goto error;
967 }
968
969 assert(ep_field);
970 event = bt_ctf_event_create(dmesg_comp->event_class);
971 if (!event) {
972 BT_LOGE_STR("Cannot create event object.");
973 goto error;
974 }
975
976 ret = bt_ctf_event_set_packet(event, dmesg_comp->packet);
977 if (ret) {
978 BT_LOGE_STR("Cannot set event's packet.");
979 goto error;
980 }
981
982 if (eh_field) {
983 ret = bt_ctf_event_set_header(event, eh_field);
984 if (ret) {
985 BT_LOGE_STR("Cannot set event's header field.");
986 goto error;
987 }
988 }
989
990 ret = bt_ctf_event_set_event_payload(event, ep_field);
991 if (ret) {
992 BT_LOGE_STR("Cannot set event's payload field.");
993 goto error;
994 }
995
996 if (clock_value) {
997 ret = bt_ctf_event_set_clock_value(event, clock_value);
998 if (ret) {
999 BT_LOGE_STR("Cannot set event's clock value.");
1000 goto error;
1001 }
1002 }
1003
1004 notif = bt_notification_event_create(event, dmesg_comp->cc_prio_map);
1005 if (!notif) {
1006 BT_LOGE_STR("Cannot create event notification.");
1007 goto error;
1008 }
1009
1010 goto end;
1011
1012 error:
1013 BT_PUT(notif);
1014
1015 end:
1016 bt_put(eh_field);
1017 bt_put(ep_field);
1018 bt_put(clock_value);
1019 bt_put(event);
1020 return notif;
1021 }
1022
1023 static
1024 void destroy_dmesg_notif_iter(struct dmesg_notif_iter *dmesg_notif_iter)
1025 {
1026 if (!dmesg_notif_iter) {
1027 return;
1028 }
1029
1030 if (dmesg_notif_iter->fp && dmesg_notif_iter->fp != stdin) {
1031 if (fclose(dmesg_notif_iter->fp)) {
1032 BT_LOGE_ERRNO("Cannot close input file", ".");
1033 }
1034 }
1035
1036 free(dmesg_notif_iter->linebuf);
1037 g_free(dmesg_notif_iter);
1038 }
1039
1040 BT_HIDDEN
1041 enum bt_notification_iterator_status dmesg_notif_iter_init(
1042 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
1043 struct bt_private_port *priv_port)
1044 {
1045 struct bt_private_component *priv_comp = NULL;
1046 struct dmesg_component *dmesg_comp;
1047 struct dmesg_notif_iter *dmesg_notif_iter =
1048 g_new0(struct dmesg_notif_iter, 1);
1049 enum bt_notification_iterator_status status =
1050 BT_NOTIFICATION_ITERATOR_STATUS_OK;
1051
1052 if (!dmesg_notif_iter) {
1053 BT_LOGE_STR("Failed to allocate on dmesg notification iterator structure.");
1054 goto error;
1055 }
1056
1057 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
1058 priv_notif_iter);
1059 assert(priv_comp);
1060 dmesg_comp = bt_private_component_get_user_data(priv_comp);
1061 assert(dmesg_comp);
1062 dmesg_notif_iter->dmesg_comp = dmesg_comp;
1063
1064 if (dmesg_comp->params.read_from_stdin) {
1065 dmesg_notif_iter->fp = stdin;
1066 } else {
1067 dmesg_notif_iter->fp = fopen(dmesg_comp->params.path->str, "r");
1068 if (!dmesg_notif_iter->fp) {
1069 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
1070 dmesg_comp->params.path->str);
1071 goto error;
1072 }
1073 }
1074
1075 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
1076 dmesg_notif_iter);
1077 goto end;
1078
1079 error:
1080 destroy_dmesg_notif_iter(dmesg_notif_iter);
1081 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
1082 NULL);
1083 if (status >= 0) {
1084 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1085 }
1086
1087 end:
1088 bt_put(priv_comp);
1089 return status;
1090 }
1091
1092 BT_HIDDEN
1093 void dmesg_notif_iter_finalize(
1094 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
1095 {
1096 destroy_dmesg_notif_iter(bt_private_connection_private_notification_iterator_get_user_data(
1097 priv_notif_iter));
1098 }
1099
1100 BT_HIDDEN
1101 struct bt_notification_iterator_next_method_return dmesg_notif_iter_next(
1102 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
1103 {
1104 ssize_t len;
1105 struct dmesg_notif_iter *dmesg_notif_iter =
1106 bt_private_connection_private_notification_iterator_get_user_data(
1107 priv_notif_iter);
1108 struct dmesg_component *dmesg_comp;
1109 struct bt_notification_iterator_next_method_return next_ret = {
1110 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
1111 .notification = NULL
1112 };
1113
1114 assert(dmesg_notif_iter);
1115 dmesg_comp = dmesg_notif_iter->dmesg_comp;
1116 assert(dmesg_comp);
1117
1118 while (true) {
1119 const char *ch;
1120 bool only_spaces = true;
1121
1122 len = bt_getline(&dmesg_notif_iter->linebuf,
1123 &dmesg_notif_iter->linebuf_len, dmesg_notif_iter->fp);
1124 if (len < 0) {
1125 if (errno == EINVAL) {
1126 next_ret.status =
1127 BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1128 } else if (errno == ENOMEM) {
1129 next_ret.status =
1130 BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
1131 } else {
1132 next_ret.status =
1133 BT_NOTIFICATION_ITERATOR_STATUS_END;
1134 }
1135
1136 goto end;
1137 }
1138
1139 assert(dmesg_notif_iter->linebuf);
1140
1141 /* Ignore empty lines, once trimmed */
1142 for (ch = dmesg_notif_iter->linebuf; *ch != '\0'; ch++) {
1143 if (!isspace(*ch)) {
1144 only_spaces = false;
1145 break;
1146 }
1147 }
1148
1149 if (!only_spaces) {
1150 break;
1151 }
1152 }
1153
1154 next_ret.notification = create_notif_from_line(dmesg_comp,
1155 dmesg_notif_iter->linebuf);
1156 if (!next_ret.notification) {
1157 BT_LOGE("Cannot create event notification from line: "
1158 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
1159 dmesg_notif_iter->linebuf);
1160 }
1161
1162 end:
1163 return next_ret;
1164 }
This page took 0.06558 seconds and 5 git commands to generate.