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