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