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