source.text.dmesg: use BT_COMP_LOGE_APPEND_CAUSE
[babeltrace.git] / src / plugins / text / dmesg / dmesg.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
5 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
6 */
7
8 #define BT_COMP_LOG_SELF_COMP (dmesg_comp->self_comp)
9 #define BT_LOG_OUTPUT_LEVEL (dmesg_comp->log_level)
10 #define BT_LOG_TAG "PLUGIN/SRC.TEXT.DMESG"
11 #include "logging/comp-logging.h"
12
13 #include "dmesg.h"
14
15 #include <stdbool.h>
16 #include <string.h>
17 #include <ctype.h>
18 #include <stdio.h>
19 #include "common/common.h"
20 #include "common/assert.h"
21 #include <babeltrace2/babeltrace.h>
22 #include "compat/utc.h"
23 #include "compat/stdio.h"
24 #include <glib.h>
25 #include "plugins/common/param-validation/param-validation.h"
26
27 #define NSEC_PER_USEC 1000UL
28 #define NSEC_PER_MSEC 1000000UL
29 #define NSEC_PER_SEC 1000000000ULL
30 #define USEC_PER_SEC 1000000UL
31
32 struct dmesg_component;
33
34 struct dmesg_msg_iter {
35 struct dmesg_component *dmesg_comp;
36
37 /* Weak */
38 bt_self_message_iterator *self_msg_iter;
39
40 char *linebuf;
41 size_t linebuf_len;
42 FILE *fp;
43 bt_message *tmp_event_msg;
44 uint64_t last_clock_value;
45
46 enum {
47 STATE_EMIT_STREAM_BEGINNING,
48 STATE_EMIT_EVENT,
49 STATE_EMIT_STREAM_END,
50 STATE_DONE,
51 } state;
52 };
53
54 struct dmesg_component {
55 bt_logging_level log_level;
56
57 struct {
58 GString *path;
59 bt_bool read_from_stdin;
60 bt_bool no_timestamp;
61 } params;
62
63 bt_self_component_source *self_comp_src;
64 bt_self_component *self_comp;
65 bt_trace_class *trace_class;
66 bt_stream_class *stream_class;
67 bt_event_class *event_class;
68 bt_trace *trace;
69 bt_stream *stream;
70 bt_clock_class *clock_class;
71 };
72
73 static
74 bt_field_class *create_event_payload_fc(struct dmesg_component *dmesg_comp,
75 bt_trace_class *trace_class)
76 {
77 bt_field_class *root_fc = NULL;
78 bt_field_class *fc = NULL;
79 bt_field_class_structure_append_member_status append_member_status;
80
81 root_fc = bt_field_class_structure_create(trace_class);
82 if (!root_fc) {
83 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
84 "Cannot create an empty structure field class object.");
85 goto error;
86 }
87
88 fc = bt_field_class_string_create(trace_class);
89 if (!fc) {
90 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
91 "Cannot create a string field class object.");
92 goto error;
93 }
94
95 append_member_status = bt_field_class_structure_append_member(root_fc,
96 "str", fc);
97 if (append_member_status != BT_FIELD_CLASS_STRUCTURE_APPEND_MEMBER_STATUS_OK) {
98 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
99 "Cannot add `str` member to structure field class: ret=%d",
100 append_member_status);
101 goto error;
102 }
103
104 goto end;
105
106 error:
107 BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc);
108
109 end:
110 bt_field_class_put_ref(fc);
111 return root_fc;
112 }
113
114 static
115 int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
116 {
117 bt_field_class *fc = NULL;
118 int ret = 0;
119
120 dmesg_comp->trace_class = bt_trace_class_create(dmesg_comp->self_comp);
121 if (!dmesg_comp->trace_class) {
122 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
123 "Cannot create an empty trace class object.");
124 goto error;
125 }
126
127 dmesg_comp->stream_class = bt_stream_class_create(
128 dmesg_comp->trace_class);
129 if (!dmesg_comp->stream_class) {
130 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
131 "Cannot create a stream class object.");
132 goto error;
133 }
134
135 if (has_ts) {
136 dmesg_comp->clock_class = bt_clock_class_create(
137 dmesg_comp->self_comp);
138 if (!dmesg_comp->clock_class) {
139 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
140 "Cannot create clock class.");
141 goto error;
142 }
143
144 /*
145 * The `dmesg` timestamp's origin is not the Unix epoch,
146 * it's the boot time.
147 */
148 bt_clock_class_set_origin_is_unix_epoch(dmesg_comp->clock_class,
149 BT_FALSE);
150
151 ret = bt_stream_class_set_default_clock_class(
152 dmesg_comp->stream_class, dmesg_comp->clock_class);
153 if (ret) {
154 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
155 "Cannot set stream class's default clock class.");
156 goto error;
157 }
158 }
159
160 dmesg_comp->event_class = bt_event_class_create(
161 dmesg_comp->stream_class);
162 if (!dmesg_comp->event_class) {
163 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
164 "Cannot create an event class object.");
165 goto error;
166 }
167
168 ret = bt_event_class_set_name(dmesg_comp->event_class, "string");
169 if (ret) {
170 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
171 "Cannot set event class's name.");
172 goto error;
173 }
174
175 fc = create_event_payload_fc(dmesg_comp, dmesg_comp->trace_class);
176 if (!fc) {
177 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
178 "Cannot create event payload field class.");
179 goto error;
180 }
181
182 ret = bt_event_class_set_payload_field_class(dmesg_comp->event_class, fc);
183 if (ret) {
184 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
185 "Cannot set event class's event payload field class.");
186 goto error;
187 }
188
189 goto end;
190
191 error:
192 ret = -1;
193
194 end:
195 bt_field_class_put_ref(fc);
196 return ret;
197 }
198
199 static
200 struct bt_param_validation_map_value_entry_descr dmesg_params[] = {
201 { "no-extract-timestamp", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } },
202 { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } },
203 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
204 };
205
206 static
207 bt_component_class_initialize_method_status handle_params(
208 struct dmesg_component *dmesg_comp,
209 const bt_value *params)
210 {
211 const bt_value *no_timestamp = NULL;
212 const bt_value *path = NULL;
213 bt_component_class_initialize_method_status status;
214 enum bt_param_validation_status validation_status;
215 gchar *validate_error = NULL;
216
217 validation_status = bt_param_validation_validate(params,
218 dmesg_params, &validate_error);
219 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
220 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
221 goto end;
222 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
223 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
224 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
225 "%s", validate_error);
226 goto end;
227 }
228
229 no_timestamp = bt_value_map_borrow_entry_value_const(params,
230 "no-extract-timestamp");
231 if (no_timestamp) {
232 dmesg_comp->params.no_timestamp =
233 bt_value_bool_get(no_timestamp);
234 }
235
236 path = bt_value_map_borrow_entry_value_const(params, "path");
237 if (path) {
238 const char *path_str = bt_value_string_get(path);
239
240 g_string_assign(dmesg_comp->params.path, path_str);
241 } else {
242 dmesg_comp->params.read_from_stdin = true;
243 }
244
245 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
246 end:
247 g_free(validate_error);
248
249 return status;
250 }
251
252 static
253 int create_stream_and_trace(struct dmesg_component *dmesg_comp)
254 {
255 int ret = 0;
256 const char *trace_name = NULL;
257 gchar *basename = NULL;
258
259 dmesg_comp->trace = bt_trace_create(dmesg_comp->trace_class);
260 if (!dmesg_comp->trace) {
261 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
262 "Cannot create trace object.");
263 goto error;
264 }
265
266 if (dmesg_comp->params.read_from_stdin) {
267 trace_name = "STDIN";
268 } else {
269 basename = g_path_get_basename(dmesg_comp->params.path->str);
270 BT_ASSERT(basename);
271
272 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
273 strcmp(basename, ".") != 0) {
274 trace_name = basename;
275 }
276 }
277
278 if (trace_name) {
279 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
280 if (ret) {
281 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
282 "Cannot set trace's name: name=\"%s\"",
283 trace_name);
284 goto error;
285 }
286 }
287
288 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class,
289 dmesg_comp->trace);
290 if (!dmesg_comp->stream) {
291 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
292 "Cannot create stream object.");
293 goto error;
294 }
295
296 goto end;
297
298 error:
299 ret = -1;
300
301 end:
302 g_free(basename);
303
304 return ret;
305 }
306
307 static
308 int try_create_meta_stream(struct dmesg_component *dmesg_comp, bool has_ts)
309 {
310 int ret = 0;
311
312 if (dmesg_comp->trace) {
313 /* Already created */
314 goto end;
315 }
316
317 ret = create_meta(dmesg_comp, has_ts);
318 if (ret) {
319 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
320 "Cannot create metadata objects: dmesg-comp-addr=%p",
321 dmesg_comp);
322 goto error;
323 }
324
325 ret = create_stream_and_trace(dmesg_comp);
326 if (ret) {
327 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
328 "Cannot create stream object: dmesg-comp-addr=%p",
329 dmesg_comp);
330 goto error;
331 }
332
333 goto end;
334
335 error:
336 ret = -1;
337
338 end:
339 return ret;
340 }
341
342 static
343 void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
344 {
345 if (!dmesg_comp) {
346 return;
347 }
348
349 if (dmesg_comp->params.path) {
350 g_string_free(dmesg_comp->params.path, TRUE);
351 }
352
353 bt_trace_put_ref(dmesg_comp->trace);
354 bt_stream_class_put_ref(dmesg_comp->stream_class);
355 bt_event_class_put_ref(dmesg_comp->event_class);
356 bt_stream_put_ref(dmesg_comp->stream);
357 bt_clock_class_put_ref(dmesg_comp->clock_class);
358 bt_trace_class_put_ref(dmesg_comp->trace_class);
359 g_free(dmesg_comp);
360 }
361
362 BT_HIDDEN
363 bt_component_class_initialize_method_status dmesg_init(
364 bt_self_component_source *self_comp_src,
365 bt_self_component_source_configuration *config,
366 const bt_value *params, void *init_method_data)
367 {
368 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
369 bt_component_class_initialize_method_status status;
370 bt_self_component *self_comp =
371 bt_self_component_source_as_self_component(self_comp_src);
372 const bt_component *comp = bt_self_component_as_component(self_comp);
373 bt_logging_level log_level = bt_component_get_logging_level(comp);
374 bt_self_component_add_port_status add_port_status;
375
376 if (!dmesg_comp) {
377 /*
378 * Don't use BT_COMP_LOGE_APPEND_CAUSE, as `dmesg_comp` is not
379 * initialized.
380 */
381 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
382 "Failed to allocate one dmesg component structure.");
383 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_COMPONENT(self_comp,
384 "Failed to allocate one dmesg component structure.");
385 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
386 goto error;
387 }
388
389 dmesg_comp->log_level = log_level;
390 dmesg_comp->self_comp = self_comp;
391 dmesg_comp->self_comp_src = self_comp_src;
392 dmesg_comp->params.path = g_string_new(NULL);
393 if (!dmesg_comp->params.path) {
394 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate a GString.");
395 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
396 goto error;
397 }
398
399 status = handle_params(dmesg_comp, params);
400 if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
401 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
402 "Invalid parameters: comp-addr=%p", self_comp);
403 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
404 goto error;
405 }
406
407 if (!dmesg_comp->params.read_from_stdin &&
408 !g_file_test(dmesg_comp->params.path->str,
409 G_FILE_TEST_IS_REGULAR)) {
410 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
411 "Input path is not a regular file: "
412 "comp-addr=%p, path=\"%s\"", self_comp,
413 dmesg_comp->params.path->str);
414 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
415 goto error;
416 }
417
418 add_port_status = bt_self_component_source_add_output_port(
419 self_comp_src, "out", NULL, NULL);
420 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
421 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to add output port.");
422 status = (int) add_port_status;
423 goto error;
424 }
425
426 bt_self_component_set_data(self_comp, dmesg_comp);
427 BT_COMP_LOGI_STR("Component initialized.");
428
429 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
430 goto end;
431
432 error:
433 destroy_dmesg_component(dmesg_comp);
434 bt_self_component_set_data(self_comp, NULL);
435
436 end:
437 return status;
438 }
439
440 BT_HIDDEN
441 void dmesg_finalize(bt_self_component_source *self_comp)
442 {
443 destroy_dmesg_component(bt_self_component_get_data(
444 bt_self_component_source_as_self_component(self_comp)));
445 }
446
447 static
448 bt_message *create_init_event_msg_from_line(
449 struct dmesg_msg_iter *msg_iter,
450 const char *line, const char **new_start)
451 {
452 bt_event *event;
453 bt_message *msg = NULL;
454 bool has_timestamp = false;
455 unsigned long sec, usec, msec;
456 unsigned int year, mon, mday, hour, min;
457 uint64_t ts = 0;
458 int ret = 0;
459 struct dmesg_component *dmesg_comp = msg_iter->dmesg_comp;
460
461 *new_start = line;
462
463 if (dmesg_comp->params.no_timestamp) {
464 goto skip_ts;
465 }
466
467 /* Extract time from input line */
468 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
469 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
470
471 /*
472 * The clock class we use has a 1 GHz frequency: convert
473 * from µs to ns.
474 */
475 ts *= NSEC_PER_USEC;
476 has_timestamp = true;
477 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
478 &year, &mon, &mday, &hour, &min,
479 &sec, &msec) == 7) {
480 time_t ep_sec;
481 struct tm ti;
482
483 memset(&ti, 0, sizeof(ti));
484 ti.tm_year = year - 1900; /* From 1900 */
485 ti.tm_mon = mon - 1; /* 0 to 11 */
486 ti.tm_mday = mday;
487 ti.tm_hour = hour;
488 ti.tm_min = min;
489 ti.tm_sec = sec;
490
491 ep_sec = bt_timegm(&ti);
492 if (ep_sec != (time_t) -1) {
493 ts = (uint64_t) ep_sec * NSEC_PER_SEC
494 + (uint64_t) msec * NSEC_PER_MSEC;
495 }
496
497 has_timestamp = true;
498 }
499
500 if (has_timestamp) {
501 /* Set new start for the message portion of the line */
502 *new_start = strchr(line, ']');
503 BT_ASSERT_DBG(*new_start);
504 (*new_start)++;
505
506 if ((*new_start)[0] == ' ') {
507 (*new_start)++;
508 }
509 }
510
511 skip_ts:
512 /*
513 * At this point, we know if the stream class's event header
514 * field class should have a timestamp or not, so we can lazily
515 * create the metadata and stream objects.
516 */
517 ret = try_create_meta_stream(dmesg_comp, has_timestamp);
518 if (ret) {
519 /* try_create_meta_stream() logs errors */
520 goto error;
521 }
522
523 if (dmesg_comp->clock_class) {
524 msg = bt_message_event_create_with_default_clock_snapshot(
525 msg_iter->self_msg_iter,
526 dmesg_comp->event_class, dmesg_comp->stream, ts);
527 msg_iter->last_clock_value = ts;
528 } else {
529 msg = bt_message_event_create(msg_iter->self_msg_iter,
530 dmesg_comp->event_class, dmesg_comp->stream);
531 }
532
533 if (!msg) {
534 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
535 "Cannot create event message.");
536 goto error;
537 }
538
539 event = bt_message_event_borrow_event(msg);
540 BT_ASSERT_DBG(event);
541 goto end;
542
543 error:
544 BT_MESSAGE_PUT_REF_AND_RESET(msg);
545
546 end:
547 return msg;
548 }
549
550 static
551 int fill_event_payload_from_line(struct dmesg_component *dmesg_comp,
552 const char *line, bt_event *event)
553 {
554 bt_field *ep_field = NULL;
555 bt_field *str_field = NULL;
556 size_t len;
557 int ret;
558
559 ep_field = bt_event_borrow_payload_field(event);
560 BT_ASSERT_DBG(ep_field);
561 str_field = bt_field_structure_borrow_member_field_by_index(
562 ep_field, 0);
563 if (!str_field) {
564 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
565 "Cannot borrow `timestamp` field from event payload structure field.");
566 goto error;
567 }
568
569 len = strlen(line);
570 if (line[len - 1] == '\n') {
571 /* Do not include the newline character in the payload */
572 len--;
573 }
574
575 bt_field_string_clear(str_field);
576 ret = bt_field_string_append_with_length(str_field, line, len);
577 if (ret) {
578 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
579 "Cannot append value to string field object: len=%zu", len);
580 goto error;
581 }
582
583 goto end;
584
585 error:
586 ret = -1;
587
588 end:
589 return ret;
590 }
591
592 static
593 bt_message *create_msg_from_line(
594 struct dmesg_msg_iter *dmesg_msg_iter, const char *line)
595 {
596 struct dmesg_component *dmesg_comp = dmesg_msg_iter->dmesg_comp;
597 bt_event *event = NULL;
598 bt_message *msg = NULL;
599 const char *new_start;
600 int ret;
601
602 msg = create_init_event_msg_from_line(dmesg_msg_iter,
603 line, &new_start);
604 if (!msg) {
605 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
606 "Cannot create and initialize event message from line.");
607 goto error;
608 }
609
610 event = bt_message_event_borrow_event(msg);
611 BT_ASSERT_DBG(event);
612 ret = fill_event_payload_from_line(dmesg_comp, new_start, event);
613 if (ret) {
614 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
615 "Cannot fill event payload field from line: ret=%d", ret);
616 goto error;
617 }
618
619 goto end;
620
621 error:
622 BT_MESSAGE_PUT_REF_AND_RESET(msg);
623
624 end:
625 return msg;
626 }
627
628 static
629 void destroy_dmesg_msg_iter(struct dmesg_msg_iter *dmesg_msg_iter)
630 {
631 struct dmesg_component *dmesg_comp;
632
633 if (!dmesg_msg_iter) {
634 return;
635 }
636
637 dmesg_comp = dmesg_msg_iter->dmesg_comp;
638
639 if (dmesg_msg_iter->fp && dmesg_msg_iter->fp != stdin) {
640 if (fclose(dmesg_msg_iter->fp)) {
641 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(dmesg_comp->self_comp,
642 "Cannot close input file", ".");
643 }
644 }
645
646 bt_message_put_ref(dmesg_msg_iter->tmp_event_msg);
647 free(dmesg_msg_iter->linebuf);
648 g_free(dmesg_msg_iter);
649 }
650
651
652
653 BT_HIDDEN
654 bt_message_iterator_class_initialize_method_status dmesg_msg_iter_init(
655 bt_self_message_iterator *self_msg_iter,
656 bt_self_message_iterator_configuration *config,
657 bt_self_component_port_output *self_port)
658 {
659 bt_self_component *self_comp =
660 bt_self_message_iterator_borrow_component(self_msg_iter);
661 struct dmesg_component *dmesg_comp = bt_self_component_get_data(self_comp);
662 struct dmesg_msg_iter *dmesg_msg_iter =
663 g_new0(struct dmesg_msg_iter, 1);
664 bt_message_iterator_class_initialize_method_status status;
665
666 if (!dmesg_msg_iter) {
667 BT_COMP_LOGE_APPEND_CAUSE(self_comp,
668 "Failed to allocate on dmesg message iterator structure.");
669 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
670 goto error;
671 }
672
673 BT_ASSERT(dmesg_comp);
674 dmesg_msg_iter->dmesg_comp = dmesg_comp;
675 dmesg_msg_iter->self_msg_iter = self_msg_iter;
676
677 if (dmesg_comp->params.read_from_stdin) {
678 dmesg_msg_iter->fp = stdin;
679 } else {
680 dmesg_msg_iter->fp = fopen(dmesg_comp->params.path->str, "r");
681 if (!dmesg_msg_iter->fp) {
682 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp,
683 "Cannot open input file in read mode", ": path=\"%s\"",
684 dmesg_comp->params.path->str);
685 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
686 goto error;
687 }
688 }
689
690 bt_self_message_iterator_set_data(self_msg_iter,
691 dmesg_msg_iter);
692
693 status = BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK;
694 goto end;
695
696 error:
697 destroy_dmesg_msg_iter(dmesg_msg_iter);
698 bt_self_message_iterator_set_data(self_msg_iter, NULL);
699
700 end:
701 return status;
702 }
703
704 BT_HIDDEN
705 void dmesg_msg_iter_finalize(
706 bt_self_message_iterator *priv_msg_iter)
707 {
708 destroy_dmesg_msg_iter(bt_self_message_iterator_get_data(
709 priv_msg_iter));
710 }
711
712 static
713 bt_message_iterator_class_next_method_status dmesg_msg_iter_next_one(
714 struct dmesg_msg_iter *dmesg_msg_iter,
715 bt_message **msg)
716 {
717 ssize_t len;
718 struct dmesg_component *dmesg_comp;
719 bt_message_iterator_class_next_method_status status;
720
721 BT_ASSERT_DBG(dmesg_msg_iter);
722 dmesg_comp = dmesg_msg_iter->dmesg_comp;
723 BT_ASSERT_DBG(dmesg_comp);
724
725 if (dmesg_msg_iter->state == STATE_DONE) {
726 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
727 goto end;
728 }
729
730 if (dmesg_msg_iter->tmp_event_msg ||
731 dmesg_msg_iter->state == STATE_EMIT_STREAM_END) {
732 goto handle_state;
733 }
734
735 while (true) {
736 const char *ch;
737 bool only_spaces = true;
738
739 len = bt_getline(&dmesg_msg_iter->linebuf,
740 &dmesg_msg_iter->linebuf_len, dmesg_msg_iter->fp);
741 if (len < 0) {
742 if (errno == EINVAL) {
743 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
744 goto end;
745 } else if (errno == ENOMEM) {
746 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_MEMORY_ERROR;
747 goto end;
748 } else {
749 if (dmesg_msg_iter->state == STATE_EMIT_STREAM_BEGINNING) {
750 /* Stream did not even begin */
751 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
752 goto end;
753 } else {
754 /* End stream now */
755 dmesg_msg_iter->state =
756 STATE_EMIT_STREAM_END;
757 goto handle_state;
758 }
759 }
760 }
761
762 BT_ASSERT_DBG(dmesg_msg_iter->linebuf);
763
764 /* Ignore empty lines, once trimmed */
765 for (ch = dmesg_msg_iter->linebuf; *ch != '\0'; ch++) {
766 if (!isspace((unsigned char) *ch)) {
767 only_spaces = false;
768 break;
769 }
770 }
771
772 if (!only_spaces) {
773 break;
774 }
775 }
776
777 dmesg_msg_iter->tmp_event_msg = create_msg_from_line(
778 dmesg_msg_iter, dmesg_msg_iter->linebuf);
779 if (!dmesg_msg_iter->tmp_event_msg) {
780 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
781 "Cannot create event message from line: "
782 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
783 dmesg_msg_iter->linebuf);
784 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
785 goto end;
786 }
787
788 handle_state:
789 BT_ASSERT_DBG(dmesg_comp->trace);
790
791 switch (dmesg_msg_iter->state) {
792 case STATE_EMIT_STREAM_BEGINNING:
793 BT_ASSERT_DBG(dmesg_msg_iter->tmp_event_msg);
794 *msg = bt_message_stream_beginning_create(
795 dmesg_msg_iter->self_msg_iter, dmesg_comp->stream);
796 dmesg_msg_iter->state = STATE_EMIT_EVENT;
797 break;
798 case STATE_EMIT_EVENT:
799 BT_ASSERT_DBG(dmesg_msg_iter->tmp_event_msg);
800 *msg = dmesg_msg_iter->tmp_event_msg;
801 dmesg_msg_iter->tmp_event_msg = NULL;
802 break;
803 case STATE_EMIT_STREAM_END:
804 *msg = bt_message_stream_end_create(
805 dmesg_msg_iter->self_msg_iter, dmesg_comp->stream);
806 dmesg_msg_iter->state = STATE_DONE;
807 break;
808 default:
809 break;
810 }
811
812 if (!*msg) {
813 BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp,
814 "Cannot create message: dmesg-comp-addr=%p",
815 dmesg_comp);
816 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR;
817 goto end;
818 }
819
820 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
821
822 end:
823 return status;
824 }
825
826 BT_HIDDEN
827 bt_message_iterator_class_next_method_status dmesg_msg_iter_next(
828 bt_self_message_iterator *self_msg_iter,
829 bt_message_array_const msgs, uint64_t capacity,
830 uint64_t *count)
831 {
832 struct dmesg_msg_iter *dmesg_msg_iter =
833 bt_self_message_iterator_get_data(
834 self_msg_iter);
835 bt_message_iterator_class_next_method_status status =
836 BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
837 uint64_t i = 0;
838
839 while (i < capacity &&
840 status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
841 bt_message *priv_msg = NULL;
842
843 status = dmesg_msg_iter_next_one(dmesg_msg_iter,
844 &priv_msg);
845 msgs[i] = priv_msg;
846 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
847 i++;
848 }
849 }
850
851 if (i > 0) {
852 /*
853 * Even if dmesg_msg_iter_next_one() returned
854 * something else than
855 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we
856 * accumulated message objects in the output
857 * message array, so we need to return
858 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they
859 * are transfered to downstream. This other status
860 * occurs again the next time muxer_msg_iter_do_next()
861 * is called, possibly without any accumulated
862 * message, in which case we'll return it.
863 */
864 *count = i;
865 status = BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK;
866 }
867
868 return status;
869 }
870
871 BT_HIDDEN
872 bt_message_iterator_class_can_seek_beginning_method_status
873 dmesg_msg_iter_can_seek_beginning(
874 bt_self_message_iterator *self_msg_iter, bt_bool *can_seek)
875 {
876 struct dmesg_msg_iter *dmesg_msg_iter =
877 bt_self_message_iterator_get_data(self_msg_iter);
878
879 /* Can't seek the beginning of the standard input stream */
880 *can_seek = !dmesg_msg_iter->dmesg_comp->params.read_from_stdin;
881
882 return BT_MESSAGE_ITERATOR_CLASS_CAN_SEEK_BEGINNING_METHOD_STATUS_OK;
883 }
884
885 BT_HIDDEN
886 bt_message_iterator_class_seek_beginning_method_status
887 dmesg_msg_iter_seek_beginning(
888 bt_self_message_iterator *self_msg_iter)
889 {
890 struct dmesg_msg_iter *dmesg_msg_iter =
891 bt_self_message_iterator_get_data(self_msg_iter);
892
893 BT_ASSERT(!dmesg_msg_iter->dmesg_comp->params.read_from_stdin);
894
895 BT_MESSAGE_PUT_REF_AND_RESET(dmesg_msg_iter->tmp_event_msg);
896 dmesg_msg_iter->last_clock_value = 0;
897 dmesg_msg_iter->state = STATE_EMIT_STREAM_BEGINNING;
898 return BT_MESSAGE_ITERATOR_CLASS_SEEK_BEGINNING_METHOD_STATUS_OK;
899 }
This page took 0.05089 seconds and 4 git commands to generate.