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