lib: remove internal stream destroy listener API
[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;
33ec5dfa
PP
47 char *linebuf;
48 size_t linebuf_len;
d8866baa 49 FILE *fp;
312c056a
PP
50 struct bt_notification *tmp_event_notif;
51
52 enum {
53 STATE_EMIT_STREAM_BEGINNING,
54 STATE_EMIT_PACKET_BEGINNING,
55 STATE_EMIT_EVENT,
56 STATE_EMIT_PACKET_END,
57 STATE_EMIT_STREAM_END,
58 STATE_DONE,
59 } state;
d8866baa
PP
60};
61
62struct dmesg_component {
63 struct {
64 GString *path;
33ec5dfa 65 bt_bool read_from_stdin;
707b323a 66 bt_bool no_timestamp;
d8866baa
PP
67 } params;
68
5c563278 69 struct bt_graph *graph; /* Weak */
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
79static
50842bdc 80struct bt_field_type *create_packet_header_ft(void)
d8866baa 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_integer_create(32);
33ec5dfa
PP
93 if (!ft) {
94 BT_LOGE_STR("Cannot create an integer field type object.");
95 goto error;
96 }
97
50842bdc 98 ret = bt_field_type_structure_add_field(root_ft, ft, "magic");
33ec5dfa
PP
99 if (ret) {
100 BT_LOGE("Cannot add `magic` field type to structure field type: "
101 "ret=%d", ret);
102 goto error;
103 }
104
105 BT_PUT(ft);
50842bdc 106 ft = bt_field_type_integer_create(8);
33ec5dfa
PP
107 if (!ft) {
108 BT_LOGE_STR("Cannot create an integer field type object.");
109 goto error;
110 }
111
112 goto end;
113
114error:
115 BT_PUT(root_ft);
116
117end:
118 bt_put(ft);
119 return root_ft;
120}
121
33ec5dfa 122static
50842bdc
PP
123struct bt_field_type *create_event_header_ft(
124 struct bt_clock_class *clock_class)
33ec5dfa 125{
50842bdc
PP
126 struct bt_field_type *root_ft = NULL;
127 struct bt_field_type *ft = NULL;
33ec5dfa
PP
128 int ret;
129
50842bdc 130 root_ft = bt_field_type_structure_create();
33ec5dfa
PP
131 if (!root_ft) {
132 BT_LOGE_STR("Cannot create an empty structure field type object.");
133 goto error;
134 }
135
50842bdc 136 ft = bt_field_type_integer_create(64);
33ec5dfa
PP
137 if (!ft) {
138 BT_LOGE_STR("Cannot create an integer field type object.");
139 goto error;
140 }
141
50842bdc 142 ret = bt_field_type_integer_set_mapped_clock_class(ft, clock_class);
33ec5dfa
PP
143 if (ret) {
144 BT_LOGE("Cannot map integer field type to clock class: "
145 "ret=%d", ret);
146 goto error;
147 }
148
50842bdc 149 ret = bt_field_type_structure_add_field(root_ft,
33ec5dfa
PP
150 ft, "timestamp");
151 if (ret) {
152 BT_LOGE("Cannot add `timestamp` field type to structure field type: "
153 "ret=%d", ret);
154 goto error;
155 }
156
157 goto end;
158
159error:
160 BT_PUT(root_ft);
161
162end:
163 bt_put(ft);
164 return root_ft;
165}
166
167static
50842bdc 168struct bt_field_type *create_event_payload_ft(void)
33ec5dfa 169{
50842bdc
PP
170 struct bt_field_type *root_ft = NULL;
171 struct bt_field_type *ft = NULL;
33ec5dfa
PP
172 int ret;
173
50842bdc 174 root_ft = bt_field_type_structure_create();
33ec5dfa
PP
175 if (!root_ft) {
176 BT_LOGE_STR("Cannot create an empty structure field type object.");
177 goto error;
178 }
179
50842bdc 180 ft = bt_field_type_string_create();
33ec5dfa
PP
181 if (!ft) {
182 BT_LOGE_STR("Cannot create a string field type object.");
183 goto error;
184 }
185
50842bdc 186 ret = bt_field_type_structure_add_field(root_ft,
33ec5dfa
PP
187 ft, "str");
188 if (ret) {
189 BT_LOGE("Cannot add `str` field type to structure field type: "
190 "ret=%d", ret);
191 goto error;
192 }
193
194 goto end;
195
196error:
197 BT_PUT(root_ft);
198
199end:
200 bt_put(ft);
201 return root_ft;
202}
203
204static
50842bdc 205struct bt_clock_class *create_clock_class(void)
33ec5dfa 206{
50842bdc 207 return bt_clock_class_create("the_clock", 1000000000);
33ec5dfa
PP
208}
209
210static
211int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
212{
50842bdc 213 struct bt_field_type *ft = NULL;
33ec5dfa
PP
214 const char *trace_name = NULL;
215 gchar *basename = NULL;
d8866baa
PP
216 int ret = 0;
217
50842bdc 218 dmesg_comp->trace = bt_trace_create();
33ec5dfa
PP
219 if (!dmesg_comp->trace) {
220 BT_LOGE_STR("Cannot create an empty trace object.");
221 goto error;
222 }
223
224 ft = create_packet_header_ft();
225 if (!ft) {
226 BT_LOGE_STR("Cannot create packet header field type.");
227 goto error;
228 }
229
312c056a 230 ret = bt_trace_set_packet_header_field_type(dmesg_comp->trace, ft);
33ec5dfa
PP
231 if (ret) {
232 BT_LOGE_STR("Cannot set trace's packet header field type.");
233 goto error;
234 }
235
236 if (dmesg_comp->params.read_from_stdin) {
237 trace_name = "STDIN";
238 } else {
239 basename = g_path_get_basename(dmesg_comp->params.path->str);
f6ccaed9 240 BT_ASSERT(basename);
33ec5dfa
PP
241
242 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
243 strcmp(basename, ".") != 0) {
244 trace_name = basename;
245 }
246 }
247
248 if (trace_name) {
50842bdc 249 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
33ec5dfa
PP
250 if (ret) {
251 BT_LOGE("Cannot set trace's name: name=\"%s\"", trace_name);
252 goto error;
253 }
254 }
255
312c056a 256 dmesg_comp->stream_class = bt_stream_class_create(NULL);
33ec5dfa
PP
257 if (!dmesg_comp->stream_class) {
258 BT_LOGE_STR("Cannot create an empty stream class object.");
259 goto error;
260 }
261
33ec5dfa
PP
262 dmesg_comp->cc_prio_map = bt_clock_class_priority_map_create();
263 if (!dmesg_comp->cc_prio_map) {
264 BT_LOGE_STR("Cannot create empty clock class priority map.");
265 goto error;
266 }
267
268 if (has_ts) {
269 dmesg_comp->clock_class = create_clock_class();
270 if (!dmesg_comp->clock_class) {
271 BT_LOGE_STR("Cannot create clock class.");
272 goto error;
273 }
274
50842bdc 275 ret = bt_trace_add_clock_class(dmesg_comp->trace,
33ec5dfa
PP
276 dmesg_comp->clock_class);
277 if (ret) {
278 BT_LOGE_STR("Cannot add clock class to trace.");
279 goto error;
280 }
281
282 ret = bt_clock_class_priority_map_add_clock_class(
283 dmesg_comp->cc_prio_map, dmesg_comp->clock_class, 0);
284 if (ret) {
285 BT_LOGE_STR("Cannot add clock class to clock class priority map.");
286 goto error;
287 }
288
289 bt_put(ft);
290 ft = create_event_header_ft(dmesg_comp->clock_class);
291 if (!ft) {
292 BT_LOGE_STR("Cannot create event header field type.");
293 goto error;
294 }
295
312c056a 296 ret = bt_stream_class_set_event_header_field_type(
33ec5dfa
PP
297 dmesg_comp->stream_class, ft);
298 if (ret) {
299 BT_LOGE_STR("Cannot set stream class's event header field type.");
300 goto error;
301 }
302 }
303
50842bdc 304 dmesg_comp->event_class = bt_event_class_create("string");
33ec5dfa
PP
305 if (!dmesg_comp->event_class) {
306 BT_LOGE_STR("Cannot create an empty event class object.");
307 goto error;
308 }
309
310 bt_put(ft);
311 ft = create_event_payload_ft();
312 if (!ft) {
313 BT_LOGE_STR("Cannot create event payload field type.");
d8866baa
PP
314 goto error;
315 }
316
312c056a 317 ret = bt_event_class_set_payload_field_type(dmesg_comp->event_class, ft);
33ec5dfa
PP
318 if (ret) {
319 BT_LOGE_STR("Cannot set event class's event payload field type.");
320 goto error;
321 }
322
50842bdc 323 ret = bt_stream_class_add_event_class(dmesg_comp->stream_class,
33ec5dfa
PP
324 dmesg_comp->event_class);
325 if (ret) {
326 BT_LOGE("Cannot add event class to stream class: ret=%d", ret);
327 goto error;
328 }
329
50842bdc 330 ret = bt_trace_add_stream_class(dmesg_comp->trace,
33ec5dfa
PP
331 dmesg_comp->stream_class);
332 if (ret) {
333 BT_LOGE("Cannot add event class to stream class: ret=%d", ret);
334 goto error;
335 }
336
337 goto end;
338
339error:
340 ret = -1;
341
342end:
343 bt_put(ft);
344
345 if (basename) {
346 g_free(basename);
347 }
348
349 return ret;
350}
351
352static
353int handle_params(struct dmesg_component *dmesg_comp, struct bt_value *params)
354{
355 struct bt_value *read_from_stdin = NULL;
707b323a 356 struct bt_value *no_timestamp = NULL;
33ec5dfa
PP
357 struct bt_value *path = NULL;
358 const char *path_str;
359 int ret = 0;
360
707b323a
PP
361 no_timestamp = bt_value_map_get(params, "no-extract-timestamp");
362 if (no_timestamp) {
363 if (!bt_value_is_bool(no_timestamp)) {
364 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
365 "type=%s",
366 bt_value_type_string(
367 bt_value_get_type(no_timestamp)));
368 goto error;
369 }
370
371 ret = bt_value_bool_get(no_timestamp,
372 &dmesg_comp->params.no_timestamp);
f6ccaed9 373 BT_ASSERT(ret == 0);
707b323a
PP
374 }
375
d8866baa
PP
376 path = bt_value_map_get(params, "path");
377 if (path) {
378 if (dmesg_comp->params.read_from_stdin) {
33ec5dfa 379 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
d8866baa
PP
380 goto error;
381 }
382
383 if (!bt_value_is_string(path)) {
33ec5dfa
PP
384 BT_LOGE("Expecting a string value for the `path` parameter: "
385 "type=%s",
386 bt_value_type_string(
387 bt_value_get_type(path)));
d8866baa
PP
388 goto error;
389 }
390
33ec5dfa 391 ret = bt_value_string_get(path, &path_str);
f6ccaed9 392 BT_ASSERT(ret == 0);
d8866baa
PP
393 g_string_assign(dmesg_comp->params.path, path_str);
394 } else {
4da23e31 395 dmesg_comp->params.read_from_stdin = true;
d8866baa
PP
396 }
397
398 goto end;
399
400error:
401 ret = -1;
402
403end:
404 bt_put(read_from_stdin);
405 bt_put(path);
707b323a 406 bt_put(no_timestamp);
d8866baa
PP
407 return ret;
408}
409
410static
312c056a 411int fill_packet_header_field(struct bt_packet *packet)
33ec5dfa 412{
50842bdc
PP
413 struct bt_field *ph = NULL;
414 struct bt_field *magic = NULL;
33ec5dfa
PP
415 int ret;
416
312c056a
PP
417 ph = bt_packet_borrow_header(packet);
418 BT_ASSERT(ph);
419 magic = bt_field_structure_borrow_field_by_name(ph, "magic");
33ec5dfa 420 if (!magic) {
312c056a 421 BT_LOGE_STR("Cannot borrow `magic` field from structure field.");
33ec5dfa
PP
422 goto error;
423 }
424
312c056a
PP
425 ret = bt_field_integer_unsigned_set_value(magic, 0xc1fc1fc1);
426 BT_ASSERT(ret == 0);
33ec5dfa
PP
427 goto end;
428
429error:
312c056a 430 ret = -1;
33ec5dfa
PP
431
432end:
312c056a 433 return ret;
33ec5dfa
PP
434}
435
436static
437int create_packet_and_stream(struct dmesg_component *dmesg_comp)
438{
439 int ret = 0;
33ec5dfa 440
50842bdc 441 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class,
312c056a 442 NULL, 0);
33ec5dfa
PP
443 if (!dmesg_comp->stream) {
444 BT_LOGE_STR("Cannot create stream object.");
445 goto error;
446 }
447
50842bdc 448 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream);
33ec5dfa
PP
449 if (!dmesg_comp->packet) {
450 BT_LOGE_STR("Cannot create packet object.");
451 goto error;
452 }
453
312c056a 454 ret = fill_packet_header_field(dmesg_comp->packet);
33ec5dfa 455 if (ret) {
312c056a 456 BT_LOGE_STR("Cannot fill packet header field.");
33ec5dfa
PP
457 goto error;
458 }
459
50842bdc 460 ret = bt_trace_set_is_static(dmesg_comp->trace);
33ec5dfa
PP
461 if (ret) {
462 BT_LOGE_STR("Cannot make trace static.");
463 goto error;
464 }
465
466 goto end;
467
468error:
469 ret = -1;
470
471end:
33ec5dfa
PP
472 return ret;
473}
474
475static
476int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp,
477 bool has_ts)
478{
479 int ret = 0;
480
481 if (dmesg_comp->trace) {
482 /* Already created */
483 goto end;
484 }
485
486 ret = create_meta(dmesg_comp, has_ts);
487 if (ret) {
488 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
489 dmesg_comp);
490 goto error;
491 }
492
493 ret = create_packet_and_stream(dmesg_comp);
494 if (ret) {
495 BT_LOGE("Cannot create packet and stream objects: "
496 "dmesg-comp-addr=%p", dmesg_comp);
497 goto error;
498 }
499
500 goto end;
501
502error:
503 ret = -1;
504
505end:
506 return ret;
507}
d8866baa
PP
508
509static
510void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
511{
512 if (!dmesg_comp) {
513 return;
514 }
515
516 if (dmesg_comp->params.path) {
517 g_string_free(dmesg_comp->params.path, TRUE);
518 }
519
520 bt_put(dmesg_comp->packet);
33ec5dfa
PP
521 bt_put(dmesg_comp->trace);
522 bt_put(dmesg_comp->stream_class);
d8866baa
PP
523 bt_put(dmesg_comp->event_class);
524 bt_put(dmesg_comp->stream);
33ec5dfa 525 bt_put(dmesg_comp->clock_class);
d8866baa
PP
526 bt_put(dmesg_comp->cc_prio_map);
527 g_free(dmesg_comp);
528}
529
33ec5dfa
PP
530static
531enum bt_component_status create_port(struct bt_private_component *priv_comp)
532{
533 return bt_private_component_source_add_output_private_port(priv_comp,
534 "out", NULL, NULL);
535}
536
d8866baa
PP
537BT_HIDDEN
538enum bt_component_status dmesg_init(struct bt_private_component *priv_comp,
539 struct bt_value *params, void *init_method_data)
540{
541 int ret = 0;
542 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
543 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
544
545 if (!dmesg_comp) {
33ec5dfa 546 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
d8866baa
PP
547 goto error;
548 }
549
5c563278
PP
550 dmesg_comp->graph = bt_component_borrow_graph(
551 bt_component_borrow_from_private(priv_comp));
552 BT_ASSERT(dmesg_comp->graph);
d8866baa
PP
553 dmesg_comp->params.path = g_string_new(NULL);
554 if (!dmesg_comp->params.path) {
33ec5dfa 555 BT_LOGE_STR("Failed to allocate a GString.");
d8866baa
PP
556 goto error;
557 }
558
33ec5dfa 559 ret = handle_params(dmesg_comp, params);
d8866baa 560 if (ret) {
33ec5dfa 561 BT_LOGE("Invalid parameters: comp-addr=%p", priv_comp);
d8866baa
PP
562 goto error;
563 }
564
33ec5dfa
PP
565 if (!dmesg_comp->params.read_from_stdin &&
566 !g_file_test(dmesg_comp->params.path->str,
567 G_FILE_TEST_IS_REGULAR)) {
568 BT_LOGE("Input path is not a regular file: "
569 "comp-addr=%p, path=\"%s\"", priv_comp,
570 dmesg_comp->params.path->str);
571 goto error;
572 }
d8866baa 573
33ec5dfa
PP
574 status = create_port(priv_comp);
575 if (status != BT_COMPONENT_STATUS_OK) {
576 goto error;
577 }
d8866baa 578
33ec5dfa 579 (void) bt_private_component_set_user_data(priv_comp, dmesg_comp);
d8866baa
PP
580 goto end;
581
582error:
583 destroy_dmesg_component(dmesg_comp);
33ec5dfa
PP
584 (void) bt_private_component_set_user_data(priv_comp, NULL);
585
586 if (status >= 0) {
587 status = BT_COMPONENT_STATUS_ERROR;
588 }
d8866baa
PP
589
590end:
591 return status;
592}
593
594BT_HIDDEN
595void dmesg_finalize(struct bt_private_component *priv_comp)
596{
33ec5dfa
PP
597 void *data = bt_private_component_get_user_data(priv_comp);
598
599 destroy_dmesg_component(data);
600}
601
602static
312c056a 603struct bt_notification *create_init_event_notif_from_line(
33ec5dfa 604 struct dmesg_component *dmesg_comp,
312c056a 605 const char *line, const char **new_start)
33ec5dfa 606{
312c056a
PP
607 struct bt_event *event;
608 struct bt_notification *notif = NULL;
33ec5dfa
PP
609 bool has_timestamp = false;
610 unsigned long sec, usec, msec;
611 unsigned int year, mon, mday, hour, min;
612 uint64_t ts = 0;
50842bdc
PP
613 struct bt_field *eh_field = NULL;
614 struct bt_field *ts_field = NULL;
33ec5dfa
PP
615 int ret = 0;
616
33ec5dfa
PP
617 *new_start = line;
618
707b323a
PP
619 if (dmesg_comp->params.no_timestamp) {
620 goto skip_ts;
621 }
622
33ec5dfa
PP
623 /* Extract time from input line */
624 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
625 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
626
627 /*
628 * The clock class we use has a 1 GHz frequency: convert
629 * from µs to ns.
630 */
631 ts *= NSEC_PER_USEC;
632 has_timestamp = true;
633 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
634 &year, &mon, &mday, &hour, &min,
635 &sec, &msec) == 7) {
636 time_t ep_sec;
637 struct tm ti;
638
639 memset(&ti, 0, sizeof(ti));
640 ti.tm_year = year - 1900; /* From 1900 */
641 ti.tm_mon = mon - 1; /* 0 to 11 */
642 ti.tm_mday = mday;
643 ti.tm_hour = hour;
644 ti.tm_min = min;
645 ti.tm_sec = sec;
646
647 ep_sec = bt_timegm(&ti);
648 if (ep_sec != (time_t) -1) {
649 ts = (uint64_t) ep_sec * NSEC_PER_SEC
650 + (uint64_t) msec * NSEC_PER_MSEC;
651 }
652
653 has_timestamp = true;
654 }
655
656 if (has_timestamp) {
657 /* Set new start for the message portion of the line */
658 *new_start = strchr(line, ']');
f6ccaed9 659 BT_ASSERT(*new_start);
33ec5dfa
PP
660 (*new_start)++;
661
662 if ((*new_start)[0] == ' ') {
663 (*new_start)++;
664 }
665 }
666
707b323a 667skip_ts:
33ec5dfa
PP
668 /*
669 * At this point, we know if the stream class's event header
670 * field type should have a timestamp or not, so we can lazily
671 * create the metadata, stream, and packet objects.
672 */
673 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
674 if (ret) {
675 /* try_create_meta_stream_packet() logs errors */
676 goto error;
677 }
678
5c563278
PP
679 notif = bt_notification_event_create(dmesg_comp->graph,
680 dmesg_comp->event_class, dmesg_comp->packet,
681 dmesg_comp->cc_prio_map);
312c056a
PP
682 if (!notif) {
683 BT_LOGE_STR("Cannot create event notification.");
684 goto error;
685 }
33ec5dfa 686
312c056a
PP
687 event = bt_notification_event_borrow_event(notif);
688 BT_ASSERT(event);
33ec5dfa 689
312c056a
PP
690 if (dmesg_comp->clock_class) {
691 struct bt_clock_value *cv = bt_event_borrow_clock_value(event,
692 dmesg_comp->clock_class);
693
694 ret = bt_clock_value_set_value(cv, ts);
695 BT_ASSERT(ret == 0);
696 eh_field = bt_event_borrow_header(event);
697 BT_ASSERT(eh_field);
698 ts_field = bt_field_structure_borrow_field_by_name(eh_field,
33ec5dfa
PP
699 "timestamp");
700 if (!ts_field) {
312c056a 701 BT_LOGE_STR("Cannot borrow `timestamp` field from event header structure field.");
33ec5dfa
PP
702 goto error;
703 }
704
312c056a
PP
705 ret = bt_field_integer_unsigned_set_value(ts_field, ts);
706 BT_ASSERT(ret == 0);
33ec5dfa
PP
707 }
708
709 goto end;
710
711error:
312c056a 712 BT_PUT(notif);
33ec5dfa
PP
713
714end:
312c056a 715 return notif;
33ec5dfa
PP
716}
717
718static
312c056a
PP
719int fill_event_payload_from_line(struct dmesg_component *dmesg_comp,
720 const char *line, struct bt_event *event)
33ec5dfa 721{
50842bdc
PP
722 struct bt_field *ep_field = NULL;
723 struct bt_field *str_field = NULL;
33ec5dfa
PP
724 size_t len;
725 int ret;
726
312c056a
PP
727 ep_field = bt_event_borrow_payload(event);
728 BT_ASSERT(ep_field);
729 str_field = bt_field_structure_borrow_field_by_name(ep_field, "str");
33ec5dfa 730 if (!str_field) {
312c056a 731 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
33ec5dfa
PP
732 goto error;
733 }
734
735 len = strlen(line);
736 if (line[len - 1] == '\n') {
737 /* Do not include the newline character in the payload */
738 len--;
739 }
740
312c056a
PP
741 ret = bt_field_string_clear(str_field);
742 if (ret) {
743 BT_LOGE_STR("Cannot clear string field object.");
744 goto error;
745 }
746
50842bdc 747 ret = bt_field_string_append_len(str_field, line, len);
33ec5dfa
PP
748 if (ret) {
749 BT_LOGE("Cannot append value to string field object: "
750 "len=%zu", len);
751 goto error;
752 }
753
33ec5dfa
PP
754 goto end;
755
756error:
757 ret = -1;
758
759end:
33ec5dfa
PP
760 return ret;
761}
762
763static
764struct bt_notification *create_notif_from_line(
765 struct dmesg_component *dmesg_comp, const char *line)
766{
50842bdc 767 struct bt_event *event = NULL;
33ec5dfa
PP
768 struct bt_notification *notif = NULL;
769 const char *new_start;
770 int ret;
771
312c056a
PP
772 notif = create_init_event_notif_from_line(dmesg_comp, line, &new_start);
773 if (!notif) {
774 BT_LOGE_STR("Cannot create and initialize event notification from line.");
33ec5dfa
PP
775 goto error;
776 }
777
312c056a
PP
778 event = bt_notification_event_borrow_event(notif);
779 BT_ASSERT(event);
780 ret = fill_event_payload_from_line(dmesg_comp, new_start,
781 event);
33ec5dfa 782 if (ret) {
312c056a 783 BT_LOGE("Cannot fill event payload field from line: "
33ec5dfa
PP
784 "ret=%d", ret);
785 goto error;
786 }
787
33ec5dfa
PP
788 goto end;
789
790error:
791 BT_PUT(notif);
792
793end:
33ec5dfa
PP
794 return notif;
795}
796
797static
798void destroy_dmesg_notif_iter(struct dmesg_notif_iter *dmesg_notif_iter)
799{
800 if (!dmesg_notif_iter) {
801 return;
802 }
803
804 if (dmesg_notif_iter->fp && dmesg_notif_iter->fp != stdin) {
805 if (fclose(dmesg_notif_iter->fp)) {
806 BT_LOGE_ERRNO("Cannot close input file", ".");
807 }
808 }
d8866baa 809
312c056a 810 bt_put(dmesg_notif_iter->tmp_event_notif);
33ec5dfa
PP
811 free(dmesg_notif_iter->linebuf);
812 g_free(dmesg_notif_iter);
d8866baa
PP
813}
814
815BT_HIDDEN
816enum bt_notification_iterator_status dmesg_notif_iter_init(
90157d89 817 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
d8866baa
PP
818 struct bt_private_port *priv_port)
819{
33ec5dfa
PP
820 struct bt_private_component *priv_comp = NULL;
821 struct dmesg_component *dmesg_comp;
822 struct dmesg_notif_iter *dmesg_notif_iter =
823 g_new0(struct dmesg_notif_iter, 1);
824 enum bt_notification_iterator_status status =
825 BT_NOTIFICATION_ITERATOR_STATUS_OK;
826
827 if (!dmesg_notif_iter) {
828 BT_LOGE_STR("Failed to allocate on dmesg notification iterator structure.");
829 goto error;
830 }
831
90157d89 832 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
33ec5dfa 833 priv_notif_iter);
f6ccaed9 834 BT_ASSERT(priv_comp);
33ec5dfa 835 dmesg_comp = bt_private_component_get_user_data(priv_comp);
f6ccaed9 836 BT_ASSERT(dmesg_comp);
33ec5dfa 837 dmesg_notif_iter->dmesg_comp = dmesg_comp;
d8866baa 838
33ec5dfa
PP
839 if (dmesg_comp->params.read_from_stdin) {
840 dmesg_notif_iter->fp = stdin;
841 } else {
842 dmesg_notif_iter->fp = fopen(dmesg_comp->params.path->str, "r");
843 if (!dmesg_notif_iter->fp) {
844 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
845 dmesg_comp->params.path->str);
846 goto error;
847 }
848 }
849
90157d89 850 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
33ec5dfa
PP
851 dmesg_notif_iter);
852 goto end;
853
854error:
855 destroy_dmesg_notif_iter(dmesg_notif_iter);
90157d89 856 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
33ec5dfa
PP
857 NULL);
858 if (status >= 0) {
859 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
860 }
861
862end:
863 bt_put(priv_comp);
864 return status;
d8866baa
PP
865}
866
867BT_HIDDEN
33ec5dfa 868void dmesg_notif_iter_finalize(
90157d89 869 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
d8866baa 870{
90157d89 871 destroy_dmesg_notif_iter(bt_private_connection_private_notification_iterator_get_user_data(
33ec5dfa 872 priv_notif_iter));
d8866baa
PP
873}
874
875BT_HIDDEN
90157d89
PP
876struct bt_notification_iterator_next_method_return dmesg_notif_iter_next(
877 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
d8866baa 878{
33ec5dfa
PP
879 ssize_t len;
880 struct dmesg_notif_iter *dmesg_notif_iter =
90157d89 881 bt_private_connection_private_notification_iterator_get_user_data(
33ec5dfa
PP
882 priv_notif_iter);
883 struct dmesg_component *dmesg_comp;
90157d89 884 struct bt_notification_iterator_next_method_return next_ret = {
33ec5dfa
PP
885 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
886 .notification = NULL
887 };
d8866baa 888
f6ccaed9 889 BT_ASSERT(dmesg_notif_iter);
33ec5dfa 890 dmesg_comp = dmesg_notif_iter->dmesg_comp;
f6ccaed9 891 BT_ASSERT(dmesg_comp);
33ec5dfa 892
312c056a
PP
893 if (dmesg_notif_iter->state == STATE_DONE) {
894 next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
895 goto end;
896 }
897
898 if (dmesg_notif_iter->tmp_event_notif ||
899 dmesg_notif_iter->state == STATE_EMIT_PACKET_END ||
900 dmesg_notif_iter->state == STATE_EMIT_STREAM_END) {
901 goto handle_state;
902 }
903
33ec5dfa
PP
904 while (true) {
905 const char *ch;
906 bool only_spaces = true;
907
908 len = bt_getline(&dmesg_notif_iter->linebuf,
909 &dmesg_notif_iter->linebuf_len, dmesg_notif_iter->fp);
910 if (len < 0) {
911 if (errno == EINVAL) {
912 next_ret.status =
913 BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
914 } else if (errno == ENOMEM) {
915 next_ret.status =
916 BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
917 } else {
312c056a
PP
918 if (dmesg_notif_iter->state == STATE_EMIT_STREAM_BEGINNING) {
919 /* Stream did not even begin */
920 next_ret.status =
921 BT_NOTIFICATION_ITERATOR_STATUS_END;
922 goto end;
923 } else {
924 /* End current packet now */
925 dmesg_notif_iter->state =
926 STATE_EMIT_PACKET_END;
927 goto handle_state;
928 }
33ec5dfa
PP
929 }
930
931 goto end;
932 }
933
f6ccaed9 934 BT_ASSERT(dmesg_notif_iter->linebuf);
33ec5dfa
PP
935
936 /* Ignore empty lines, once trimmed */
937 for (ch = dmesg_notif_iter->linebuf; *ch != '\0'; ch++) {
938 if (!isspace(*ch)) {
939 only_spaces = false;
940 break;
941 }
942 }
943
944 if (!only_spaces) {
945 break;
946 }
947 }
948
312c056a 949 dmesg_notif_iter->tmp_event_notif = create_notif_from_line(dmesg_comp,
33ec5dfa 950 dmesg_notif_iter->linebuf);
312c056a 951 if (!dmesg_notif_iter->tmp_event_notif) {
33ec5dfa
PP
952 BT_LOGE("Cannot create event notification from line: "
953 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
954 dmesg_notif_iter->linebuf);
312c056a
PP
955 goto end;
956 }
957
958handle_state:
959 BT_ASSERT(dmesg_comp->trace);
960
961 switch (dmesg_notif_iter->state) {
962 case STATE_EMIT_STREAM_BEGINNING:
963 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
964 next_ret.notification = bt_notification_stream_begin_create(
5c563278 965 dmesg_comp->graph, dmesg_comp->stream);
312c056a
PP
966 dmesg_notif_iter->state = STATE_EMIT_PACKET_BEGINNING;
967 break;
968 case STATE_EMIT_PACKET_BEGINNING:
969 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
970 next_ret.notification = bt_notification_packet_begin_create(
5c563278 971 dmesg_comp->graph, dmesg_comp->packet);
312c056a
PP
972 dmesg_notif_iter->state = STATE_EMIT_EVENT;
973 break;
974 case STATE_EMIT_EVENT:
975 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
976 BT_MOVE(next_ret.notification,
977 dmesg_notif_iter->tmp_event_notif);
978 break;
979 case STATE_EMIT_PACKET_END:
980 next_ret.notification = bt_notification_packet_end_create(
5c563278 981 dmesg_comp->graph, dmesg_comp->packet);
312c056a
PP
982 dmesg_notif_iter->state = STATE_EMIT_STREAM_END;
983 break;
984 case STATE_EMIT_STREAM_END:
985 next_ret.notification = bt_notification_stream_end_create(
5c563278 986 dmesg_comp->graph, dmesg_comp->stream);
312c056a
PP
987 dmesg_notif_iter->state = STATE_DONE;
988 break;
989 default:
990 break;
991 }
992
993 if (!next_ret.notification) {
994 BT_LOGE("Cannot create notification: dmesg-comp-addr=%p",
995 dmesg_comp);
996 next_ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
33ec5dfa
PP
997 }
998
999end:
1000 return next_ret;
d8866baa 1001}
This page took 0.075588 seconds and 4 git commands to generate.