Make API CTF-agnostic
[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>
33ec5dfa
PP
32#include <babeltrace/babeltrace.h>
33#include <babeltrace/values-internal.h>
34#include <babeltrace/compat/utc-internal.h>
35#include <babeltrace/compat/stdio-internal.h>
d8866baa
PP
36#include <glib.h>
37
33ec5dfa
PP
38#define NSEC_PER_USEC 1000UL
39#define NSEC_PER_MSEC 1000000UL
40#define NSEC_PER_SEC 1000000000ULL
41#define USEC_PER_SEC 1000000UL
42
d8866baa
PP
43struct dmesg_component;
44
45struct dmesg_notif_iter {
46 struct dmesg_component *dmesg_comp;
f0010051 47 struct bt_private_connection_private_notification_iterator *pc_notif_iter; /* Weak */
33ec5dfa
PP
48 char *linebuf;
49 size_t linebuf_len;
d8866baa 50 FILE *fp;
312c056a
PP
51 struct bt_notification *tmp_event_notif;
52
53 enum {
54 STATE_EMIT_STREAM_BEGINNING,
55 STATE_EMIT_PACKET_BEGINNING,
56 STATE_EMIT_EVENT,
57 STATE_EMIT_PACKET_END,
58 STATE_EMIT_STREAM_END,
59 STATE_DONE,
60 } state;
d8866baa
PP
61};
62
63struct dmesg_component {
64 struct {
65 GString *path;
33ec5dfa 66 bt_bool read_from_stdin;
707b323a 67 bt_bool no_timestamp;
d8866baa
PP
68 } params;
69
50842bdc
PP
70 struct bt_trace *trace;
71 struct bt_stream_class *stream_class;
72 struct bt_event_class *event_class;
73 struct bt_stream *stream;
74 struct bt_packet *packet;
75 struct bt_clock_class *clock_class;
d8866baa
PP
76 struct bt_clock_class_priority_map *cc_prio_map;
77};
78
33ec5dfa 79static
50842bdc 80struct bt_field_type *create_event_payload_ft(void)
33ec5dfa 81{
50842bdc
PP
82 struct bt_field_type *root_ft = NULL;
83 struct bt_field_type *ft = NULL;
33ec5dfa
PP
84 int ret;
85
50842bdc 86 root_ft = bt_field_type_structure_create();
33ec5dfa
PP
87 if (!root_ft) {
88 BT_LOGE_STR("Cannot create an empty structure field type object.");
89 goto error;
90 }
91
50842bdc 92 ft = bt_field_type_string_create();
33ec5dfa
PP
93 if (!ft) {
94 BT_LOGE_STR("Cannot create a string field type object.");
95 goto error;
96 }
97
44c440bc 98 ret = bt_field_type_structure_append_member(root_ft, "str", ft);
33ec5dfa 99 if (ret) {
44c440bc 100 BT_LOGE("Cannot add `str` member to structure field type: "
33ec5dfa
PP
101 "ret=%d", ret);
102 goto error;
103 }
104
105 goto end;
106
107error:
108 BT_PUT(root_ft);
109
110end:
111 bt_put(ft);
112 return root_ft;
113}
114
33ec5dfa
PP
115static
116int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
117{
50842bdc 118 struct bt_field_type *ft = NULL;
33ec5dfa
PP
119 const char *trace_name = NULL;
120 gchar *basename = NULL;
d8866baa
PP
121 int ret = 0;
122
50842bdc 123 dmesg_comp->trace = bt_trace_create();
33ec5dfa
PP
124 if (!dmesg_comp->trace) {
125 BT_LOGE_STR("Cannot create an empty trace object.");
126 goto error;
127 }
128
33ec5dfa
PP
129 if (dmesg_comp->params.read_from_stdin) {
130 trace_name = "STDIN";
131 } else {
132 basename = g_path_get_basename(dmesg_comp->params.path->str);
f6ccaed9 133 BT_ASSERT(basename);
33ec5dfa
PP
134
135 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
136 strcmp(basename, ".") != 0) {
137 trace_name = basename;
138 }
139 }
140
141 if (trace_name) {
50842bdc 142 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
33ec5dfa
PP
143 if (ret) {
144 BT_LOGE("Cannot set trace's name: name=\"%s\"", trace_name);
145 goto error;
146 }
147 }
148
44c440bc 149 dmesg_comp->stream_class = bt_stream_class_create(dmesg_comp->trace);
33ec5dfa 150 if (!dmesg_comp->stream_class) {
44c440bc 151 BT_LOGE_STR("Cannot create a stream class object.");
33ec5dfa
PP
152 goto error;
153 }
154
33ec5dfa 155 if (has_ts) {
44c440bc 156 dmesg_comp->clock_class = bt_clock_class_create();
33ec5dfa
PP
157 if (!dmesg_comp->clock_class) {
158 BT_LOGE_STR("Cannot create clock class.");
159 goto error;
160 }
161
44c440bc
PP
162 ret = bt_stream_class_set_default_clock_class(
163 dmesg_comp->stream_class, dmesg_comp->clock_class);
33ec5dfa 164 if (ret) {
44c440bc 165 BT_LOGE_STR("Cannot set stream class's default clock class.");
33ec5dfa
PP
166 goto error;
167 }
168 }
169
44c440bc
PP
170 dmesg_comp->event_class = bt_event_class_create(
171 dmesg_comp->stream_class);
33ec5dfa 172 if (!dmesg_comp->event_class) {
44c440bc
PP
173 BT_LOGE_STR("Cannot create an event class object.");
174 goto error;
175 }
176
177 ret = bt_event_class_set_name(dmesg_comp->event_class, "string");
178 if (ret) {
179 BT_LOGE_STR("Cannot set event class's name.");
33ec5dfa
PP
180 goto error;
181 }
182
33ec5dfa
PP
183 ft = create_event_payload_ft();
184 if (!ft) {
185 BT_LOGE_STR("Cannot create event payload field type.");
d8866baa
PP
186 goto error;
187 }
188
312c056a 189 ret = bt_event_class_set_payload_field_type(dmesg_comp->event_class, ft);
33ec5dfa
PP
190 if (ret) {
191 BT_LOGE_STR("Cannot set event class's event payload field type.");
192 goto error;
193 }
194
33ec5dfa
PP
195 goto end;
196
197error:
198 ret = -1;
199
200end:
201 bt_put(ft);
202
203 if (basename) {
204 g_free(basename);
205 }
206
207 return ret;
208}
209
210static
211int handle_params(struct dmesg_component *dmesg_comp, struct bt_value *params)
212{
213 struct bt_value *read_from_stdin = NULL;
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
707b323a
PP
219 no_timestamp = bt_value_map_get(params, "no-extract-timestamp");
220 if (no_timestamp) {
221 if (!bt_value_is_bool(no_timestamp)) {
222 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
223 "type=%s",
224 bt_value_type_string(
225 bt_value_get_type(no_timestamp)));
226 goto error;
227 }
228
229 ret = bt_value_bool_get(no_timestamp,
230 &dmesg_comp->params.no_timestamp);
f6ccaed9 231 BT_ASSERT(ret == 0);
707b323a
PP
232 }
233
d8866baa
PP
234 path = bt_value_map_get(params, "path");
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",
244 bt_value_type_string(
245 bt_value_get_type(path)));
d8866baa
PP
246 goto error;
247 }
248
33ec5dfa 249 ret = bt_value_string_get(path, &path_str);
f6ccaed9 250 BT_ASSERT(ret == 0);
d8866baa
PP
251 g_string_assign(dmesg_comp->params.path, path_str);
252 } else {
4da23e31 253 dmesg_comp->params.read_from_stdin = true;
d8866baa
PP
254 }
255
256 goto end;
257
258error:
259 ret = -1;
260
261end:
262 bt_put(read_from_stdin);
263 bt_put(path);
707b323a 264 bt_put(no_timestamp);
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
44c440bc 273 dmesg_comp->stream = bt_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
44c440bc 279 dmesg_comp->packet = bt_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
44c440bc 285 ret = bt_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
345 bt_put(dmesg_comp->packet);
33ec5dfa
PP
346 bt_put(dmesg_comp->trace);
347 bt_put(dmesg_comp->stream_class);
d8866baa
PP
348 bt_put(dmesg_comp->event_class);
349 bt_put(dmesg_comp->stream);
33ec5dfa 350 bt_put(dmesg_comp->clock_class);
d8866baa
PP
351 bt_put(dmesg_comp->cc_prio_map);
352 g_free(dmesg_comp);
353}
354
33ec5dfa
PP
355static
356enum bt_component_status create_port(struct bt_private_component *priv_comp)
357{
358 return bt_private_component_source_add_output_private_port(priv_comp,
359 "out", NULL, NULL);
360}
361
d8866baa
PP
362BT_HIDDEN
363enum bt_component_status dmesg_init(struct bt_private_component *priv_comp,
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);
368 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
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) {
33ec5dfa 383 BT_LOGE("Invalid parameters: comp-addr=%p", priv_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: "
391 "comp-addr=%p, path=\"%s\"", priv_comp,
392 dmesg_comp->params.path->str);
393 goto error;
394 }
d8866baa 395
33ec5dfa
PP
396 status = create_port(priv_comp);
397 if (status != BT_COMPONENT_STATUS_OK) {
398 goto error;
399 }
d8866baa 400
33ec5dfa 401 (void) bt_private_component_set_user_data(priv_comp, dmesg_comp);
d8866baa
PP
402 goto end;
403
404error:
405 destroy_dmesg_component(dmesg_comp);
33ec5dfa
PP
406 (void) bt_private_component_set_user_data(priv_comp, NULL);
407
408 if (status >= 0) {
409 status = BT_COMPONENT_STATUS_ERROR;
410 }
d8866baa
PP
411
412end:
413 return status;
414}
415
416BT_HIDDEN
417void dmesg_finalize(struct bt_private_component *priv_comp)
418{
33ec5dfa
PP
419 void *data = bt_private_component_get_user_data(priv_comp);
420
421 destroy_dmesg_component(data);
422}
423
424static
312c056a 425struct bt_notification *create_init_event_notif_from_line(
f0010051 426 struct dmesg_notif_iter *notif_iter,
312c056a 427 const char *line, const char **new_start)
33ec5dfa 428{
312c056a
PP
429 struct bt_event *event;
430 struct bt_notification *notif = NULL;
33ec5dfa
PP
431 bool has_timestamp = false;
432 unsigned long sec, usec, msec;
433 unsigned int year, mon, mday, hour, min;
434 uint64_t ts = 0;
33ec5dfa 435 int ret = 0;
f0010051 436 struct dmesg_component *dmesg_comp = notif_iter->dmesg_comp;
33ec5dfa 437
33ec5dfa
PP
438 *new_start = line;
439
707b323a
PP
440 if (dmesg_comp->params.no_timestamp) {
441 goto skip_ts;
442 }
443
33ec5dfa
PP
444 /* Extract time from input line */
445 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
446 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
447
448 /*
449 * The clock class we use has a 1 GHz frequency: convert
450 * from µs to ns.
451 */
452 ts *= NSEC_PER_USEC;
453 has_timestamp = true;
454 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
455 &year, &mon, &mday, &hour, &min,
456 &sec, &msec) == 7) {
457 time_t ep_sec;
458 struct tm ti;
459
460 memset(&ti, 0, sizeof(ti));
461 ti.tm_year = year - 1900; /* From 1900 */
462 ti.tm_mon = mon - 1; /* 0 to 11 */
463 ti.tm_mday = mday;
464 ti.tm_hour = hour;
465 ti.tm_min = min;
466 ti.tm_sec = sec;
467
468 ep_sec = bt_timegm(&ti);
469 if (ep_sec != (time_t) -1) {
470 ts = (uint64_t) ep_sec * NSEC_PER_SEC
471 + (uint64_t) msec * NSEC_PER_MSEC;
472 }
473
474 has_timestamp = true;
475 }
476
477 if (has_timestamp) {
478 /* Set new start for the message portion of the line */
479 *new_start = strchr(line, ']');
f6ccaed9 480 BT_ASSERT(*new_start);
33ec5dfa
PP
481 (*new_start)++;
482
483 if ((*new_start)[0] == ' ') {
484 (*new_start)++;
485 }
486 }
487
707b323a 488skip_ts:
33ec5dfa
PP
489 /*
490 * At this point, we know if the stream class's event header
491 * field type should have a timestamp or not, so we can lazily
492 * create the metadata, stream, and packet objects.
493 */
494 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
495 if (ret) {
496 /* try_create_meta_stream_packet() logs errors */
497 goto error;
498 }
499
f0010051 500 notif = bt_notification_event_create(notif_iter->pc_notif_iter,
e22b45d0 501 dmesg_comp->event_class, dmesg_comp->packet);
312c056a
PP
502 if (!notif) {
503 BT_LOGE_STR("Cannot create event notification.");
504 goto error;
505 }
33ec5dfa 506
312c056a
PP
507 event = bt_notification_event_borrow_event(notif);
508 BT_ASSERT(event);
33ec5dfa 509
312c056a 510 if (dmesg_comp->clock_class) {
44c440bc 511 ret = bt_event_set_default_clock_value(event, ts);
312c056a 512 BT_ASSERT(ret == 0);
33ec5dfa
PP
513 }
514
515 goto end;
516
517error:
312c056a 518 BT_PUT(notif);
33ec5dfa
PP
519
520end:
312c056a 521 return notif;
33ec5dfa
PP
522}
523
524static
f0010051 525int fill_event_payload_from_line(const char *line, struct bt_event *event)
33ec5dfa 526{
50842bdc
PP
527 struct bt_field *ep_field = NULL;
528 struct bt_field *str_field = NULL;
33ec5dfa
PP
529 size_t len;
530 int ret;
531
44c440bc 532 ep_field = bt_event_borrow_payload_field(event);
312c056a 533 BT_ASSERT(ep_field);
44c440bc
PP
534 str_field = bt_field_structure_borrow_member_field_by_index(ep_field,
535 0);
33ec5dfa 536 if (!str_field) {
312c056a 537 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
33ec5dfa
PP
538 goto error;
539 }
540
541 len = strlen(line);
542 if (line[len - 1] == '\n') {
543 /* Do not include the newline character in the payload */
544 len--;
545 }
546
312c056a
PP
547 ret = bt_field_string_clear(str_field);
548 if (ret) {
549 BT_LOGE_STR("Cannot clear string field object.");
550 goto error;
551 }
552
44c440bc 553 ret = bt_field_string_append_with_length(str_field, line, len);
33ec5dfa
PP
554 if (ret) {
555 BT_LOGE("Cannot append value to string field object: "
556 "len=%zu", len);
557 goto error;
558 }
559
33ec5dfa
PP
560 goto end;
561
562error:
563 ret = -1;
564
565end:
33ec5dfa
PP
566 return ret;
567}
568
569static
570struct bt_notification *create_notif_from_line(
f0010051 571 struct dmesg_notif_iter *dmesg_notif_iter, const char *line)
33ec5dfa 572{
50842bdc 573 struct bt_event *event = NULL;
33ec5dfa
PP
574 struct bt_notification *notif = NULL;
575 const char *new_start;
576 int ret;
577
f0010051
PP
578 notif = create_init_event_notif_from_line(dmesg_notif_iter,
579 line, &new_start);
312c056a
PP
580 if (!notif) {
581 BT_LOGE_STR("Cannot create and initialize event notification from line.");
33ec5dfa
PP
582 goto error;
583 }
584
312c056a
PP
585 event = bt_notification_event_borrow_event(notif);
586 BT_ASSERT(event);
f0010051 587 ret = fill_event_payload_from_line(new_start, event);
33ec5dfa 588 if (ret) {
312c056a 589 BT_LOGE("Cannot fill event payload field from line: "
33ec5dfa
PP
590 "ret=%d", ret);
591 goto error;
592 }
593
33ec5dfa
PP
594 goto end;
595
596error:
597 BT_PUT(notif);
598
599end:
33ec5dfa
PP
600 return notif;
601}
602
603static
604void destroy_dmesg_notif_iter(struct dmesg_notif_iter *dmesg_notif_iter)
605{
606 if (!dmesg_notif_iter) {
607 return;
608 }
609
610 if (dmesg_notif_iter->fp && dmesg_notif_iter->fp != stdin) {
611 if (fclose(dmesg_notif_iter->fp)) {
612 BT_LOGE_ERRNO("Cannot close input file", ".");
613 }
614 }
d8866baa 615
312c056a 616 bt_put(dmesg_notif_iter->tmp_event_notif);
33ec5dfa
PP
617 free(dmesg_notif_iter->linebuf);
618 g_free(dmesg_notif_iter);
d8866baa
PP
619}
620
621BT_HIDDEN
622enum bt_notification_iterator_status dmesg_notif_iter_init(
90157d89 623 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
d8866baa
PP
624 struct bt_private_port *priv_port)
625{
33ec5dfa
PP
626 struct bt_private_component *priv_comp = NULL;
627 struct dmesg_component *dmesg_comp;
628 struct dmesg_notif_iter *dmesg_notif_iter =
629 g_new0(struct dmesg_notif_iter, 1);
630 enum bt_notification_iterator_status status =
631 BT_NOTIFICATION_ITERATOR_STATUS_OK;
632
633 if (!dmesg_notif_iter) {
634 BT_LOGE_STR("Failed to allocate on dmesg notification iterator structure.");
635 goto error;
636 }
637
90157d89 638 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
33ec5dfa 639 priv_notif_iter);
f6ccaed9 640 BT_ASSERT(priv_comp);
33ec5dfa 641 dmesg_comp = bt_private_component_get_user_data(priv_comp);
f6ccaed9 642 BT_ASSERT(dmesg_comp);
33ec5dfa 643 dmesg_notif_iter->dmesg_comp = dmesg_comp;
f0010051 644 dmesg_notif_iter->pc_notif_iter = priv_notif_iter;
d8866baa 645
33ec5dfa
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
90157d89 657 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
33ec5dfa
PP
658 dmesg_notif_iter);
659 goto end;
660
661error:
662 destroy_dmesg_notif_iter(dmesg_notif_iter);
90157d89 663 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
33ec5dfa
PP
664 NULL);
665 if (status >= 0) {
666 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
667 }
668
669end:
670 bt_put(priv_comp);
671 return status;
d8866baa
PP
672}
673
674BT_HIDDEN
33ec5dfa 675void dmesg_notif_iter_finalize(
90157d89 676 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
d8866baa 677{
90157d89 678 destroy_dmesg_notif_iter(bt_private_connection_private_notification_iterator_get_user_data(
33ec5dfa 679 priv_notif_iter));
d8866baa
PP
680}
681
d4393e08
PP
682static
683enum bt_notification_iterator_status dmesg_notif_iter_next_one(
684 struct dmesg_notif_iter *dmesg_notif_iter,
685 struct bt_notification **notif)
d8866baa 686{
33ec5dfa 687 ssize_t len;
33ec5dfa 688 struct dmesg_component *dmesg_comp;
d4393e08
PP
689 enum bt_notification_iterator_status status =
690 BT_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) {
d4393e08 697 status = BT_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) {
d4393e08 715 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
33ec5dfa 716 } else if (errno == ENOMEM) {
d4393e08 717 status =
33ec5dfa
PP
718 BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
719 } else {
312c056a
PP
720 if (dmesg_notif_iter->state == STATE_EMIT_STREAM_BEGINNING) {
721 /* Stream did not even begin */
d4393e08 722 status = BT_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);
d4393e08 765 *notif = bt_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);
d4393e08 771 *notif = bt_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:
d4393e08 781 *notif = bt_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:
d4393e08 786 *notif = bt_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);
d4393e08 797 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
33ec5dfa
PP
798 }
799
800end:
d4393e08
PP
801 return status;
802}
803
804BT_HIDDEN
805enum bt_notification_iterator_status dmesg_notif_iter_next(
806 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
807 bt_notification_array notifs, uint64_t capacity,
808 uint64_t *count)
809{
810 struct dmesg_notif_iter *dmesg_notif_iter =
811 bt_private_connection_private_notification_iterator_get_user_data(
812 priv_notif_iter);
813 enum bt_notification_iterator_status status =
814 BT_NOTIFICATION_ITERATOR_STATUS_OK;
815 uint64_t i = 0;
816
817 while (i < capacity && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
818 status = dmesg_notif_iter_next_one(dmesg_notif_iter, &notifs[i]);
819 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
820 i++;
821 }
822 }
823
824 if (i > 0) {
825 /*
826 * Even if dmesg_notif_iter_next_one() returned
827 * something else than
828 * BT_NOTIFICATION_ITERATOR_STATUS_OK, we accumulated
829 * notification objects in the output notification
830 * array, so we need to return
831 * BT_NOTIFICATION_ITERATOR_STATUS_OK so that they are
832 * transfered to downstream. This other status occurs
833 * again the next time muxer_notif_iter_do_next() is
834 * called, possibly without any accumulated
835 * notification, in which case we'll return it.
836 */
837 *count = i;
838 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
839 }
840
841 return status;
d8866baa 842}
This page took 0.069532 seconds and 4 git commands to generate.