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