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