assert-pre-internal.h: add BT_ASSERT_PRE_VALID_INDEX()
[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
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 if (has_ts) {
263 dmesg_comp->clock_class = create_clock_class();
264 if (!dmesg_comp->clock_class) {
265 BT_LOGE_STR("Cannot create clock class.");
266 goto error;
267 }
268
50842bdc 269 ret = bt_trace_add_clock_class(dmesg_comp->trace,
33ec5dfa
PP
270 dmesg_comp->clock_class);
271 if (ret) {
272 BT_LOGE_STR("Cannot add clock class to trace.");
273 goto error;
274 }
275
33ec5dfa
PP
276 bt_put(ft);
277 ft = create_event_header_ft(dmesg_comp->clock_class);
278 if (!ft) {
279 BT_LOGE_STR("Cannot create event header field type.");
280 goto error;
281 }
282
312c056a 283 ret = bt_stream_class_set_event_header_field_type(
33ec5dfa
PP
284 dmesg_comp->stream_class, ft);
285 if (ret) {
286 BT_LOGE_STR("Cannot set stream class's event header field type.");
287 goto error;
288 }
289 }
290
50842bdc 291 dmesg_comp->event_class = bt_event_class_create("string");
33ec5dfa
PP
292 if (!dmesg_comp->event_class) {
293 BT_LOGE_STR("Cannot create an empty event class object.");
294 goto error;
295 }
296
297 bt_put(ft);
298 ft = create_event_payload_ft();
299 if (!ft) {
300 BT_LOGE_STR("Cannot create event payload field type.");
d8866baa
PP
301 goto error;
302 }
303
312c056a 304 ret = bt_event_class_set_payload_field_type(dmesg_comp->event_class, ft);
33ec5dfa
PP
305 if (ret) {
306 BT_LOGE_STR("Cannot set event class's event payload field type.");
307 goto error;
308 }
309
50842bdc 310 ret = bt_stream_class_add_event_class(dmesg_comp->stream_class,
33ec5dfa
PP
311 dmesg_comp->event_class);
312 if (ret) {
313 BT_LOGE("Cannot add event class to stream class: ret=%d", ret);
314 goto error;
315 }
316
50842bdc 317 ret = bt_trace_add_stream_class(dmesg_comp->trace,
33ec5dfa
PP
318 dmesg_comp->stream_class);
319 if (ret) {
320 BT_LOGE("Cannot add event class to stream class: ret=%d", ret);
321 goto error;
322 }
323
324 goto end;
325
326error:
327 ret = -1;
328
329end:
330 bt_put(ft);
331
332 if (basename) {
333 g_free(basename);
334 }
335
336 return ret;
337}
338
339static
340int handle_params(struct dmesg_component *dmesg_comp, struct bt_value *params)
341{
342 struct bt_value *read_from_stdin = NULL;
707b323a 343 struct bt_value *no_timestamp = NULL;
33ec5dfa
PP
344 struct bt_value *path = NULL;
345 const char *path_str;
346 int ret = 0;
347
707b323a
PP
348 no_timestamp = bt_value_map_get(params, "no-extract-timestamp");
349 if (no_timestamp) {
350 if (!bt_value_is_bool(no_timestamp)) {
351 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
352 "type=%s",
353 bt_value_type_string(
354 bt_value_get_type(no_timestamp)));
355 goto error;
356 }
357
358 ret = bt_value_bool_get(no_timestamp,
359 &dmesg_comp->params.no_timestamp);
f6ccaed9 360 BT_ASSERT(ret == 0);
707b323a
PP
361 }
362
d8866baa
PP
363 path = bt_value_map_get(params, "path");
364 if (path) {
365 if (dmesg_comp->params.read_from_stdin) {
33ec5dfa 366 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
d8866baa
PP
367 goto error;
368 }
369
370 if (!bt_value_is_string(path)) {
33ec5dfa
PP
371 BT_LOGE("Expecting a string value for the `path` parameter: "
372 "type=%s",
373 bt_value_type_string(
374 bt_value_get_type(path)));
d8866baa
PP
375 goto error;
376 }
377
33ec5dfa 378 ret = bt_value_string_get(path, &path_str);
f6ccaed9 379 BT_ASSERT(ret == 0);
d8866baa
PP
380 g_string_assign(dmesg_comp->params.path, path_str);
381 } else {
4da23e31 382 dmesg_comp->params.read_from_stdin = true;
d8866baa
PP
383 }
384
385 goto end;
386
387error:
388 ret = -1;
389
390end:
391 bt_put(read_from_stdin);
392 bt_put(path);
707b323a 393 bt_put(no_timestamp);
d8866baa
PP
394 return ret;
395}
396
397static
312c056a 398int fill_packet_header_field(struct bt_packet *packet)
33ec5dfa 399{
50842bdc
PP
400 struct bt_field *ph = NULL;
401 struct bt_field *magic = NULL;
33ec5dfa
PP
402 int ret;
403
312c056a
PP
404 ph = bt_packet_borrow_header(packet);
405 BT_ASSERT(ph);
406 magic = bt_field_structure_borrow_field_by_name(ph, "magic");
33ec5dfa 407 if (!magic) {
312c056a 408 BT_LOGE_STR("Cannot borrow `magic` field from structure field.");
33ec5dfa
PP
409 goto error;
410 }
411
312c056a
PP
412 ret = bt_field_integer_unsigned_set_value(magic, 0xc1fc1fc1);
413 BT_ASSERT(ret == 0);
33ec5dfa
PP
414 goto end;
415
416error:
312c056a 417 ret = -1;
33ec5dfa
PP
418
419end:
312c056a 420 return ret;
33ec5dfa
PP
421}
422
423static
424int create_packet_and_stream(struct dmesg_component *dmesg_comp)
425{
426 int ret = 0;
33ec5dfa 427
50842bdc 428 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class,
312c056a 429 NULL, 0);
33ec5dfa
PP
430 if (!dmesg_comp->stream) {
431 BT_LOGE_STR("Cannot create stream object.");
432 goto error;
433 }
434
ccf82993
PP
435 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream,
436 BT_PACKET_PREVIOUS_PACKET_AVAILABILITY_NONE, NULL);
33ec5dfa
PP
437 if (!dmesg_comp->packet) {
438 BT_LOGE_STR("Cannot create packet object.");
439 goto error;
440 }
441
312c056a 442 ret = fill_packet_header_field(dmesg_comp->packet);
33ec5dfa 443 if (ret) {
312c056a 444 BT_LOGE_STR("Cannot fill packet header field.");
33ec5dfa
PP
445 goto error;
446 }
447
50842bdc 448 ret = bt_trace_set_is_static(dmesg_comp->trace);
33ec5dfa
PP
449 if (ret) {
450 BT_LOGE_STR("Cannot make trace static.");
451 goto error;
452 }
453
454 goto end;
455
456error:
457 ret = -1;
458
459end:
33ec5dfa
PP
460 return ret;
461}
462
463static
464int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp,
465 bool has_ts)
466{
467 int ret = 0;
468
469 if (dmesg_comp->trace) {
470 /* Already created */
471 goto end;
472 }
473
474 ret = create_meta(dmesg_comp, has_ts);
475 if (ret) {
476 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
477 dmesg_comp);
478 goto error;
479 }
480
481 ret = create_packet_and_stream(dmesg_comp);
482 if (ret) {
483 BT_LOGE("Cannot create packet and stream objects: "
484 "dmesg-comp-addr=%p", dmesg_comp);
485 goto error;
486 }
487
488 goto end;
489
490error:
491 ret = -1;
492
493end:
494 return ret;
495}
d8866baa
PP
496
497static
498void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
499{
500 if (!dmesg_comp) {
501 return;
502 }
503
504 if (dmesg_comp->params.path) {
505 g_string_free(dmesg_comp->params.path, TRUE);
506 }
507
508 bt_put(dmesg_comp->packet);
33ec5dfa
PP
509 bt_put(dmesg_comp->trace);
510 bt_put(dmesg_comp->stream_class);
d8866baa
PP
511 bt_put(dmesg_comp->event_class);
512 bt_put(dmesg_comp->stream);
33ec5dfa 513 bt_put(dmesg_comp->clock_class);
d8866baa
PP
514 bt_put(dmesg_comp->cc_prio_map);
515 g_free(dmesg_comp);
516}
517
33ec5dfa
PP
518static
519enum bt_component_status create_port(struct bt_private_component *priv_comp)
520{
521 return bt_private_component_source_add_output_private_port(priv_comp,
522 "out", NULL, NULL);
523}
524
d8866baa
PP
525BT_HIDDEN
526enum bt_component_status dmesg_init(struct bt_private_component *priv_comp,
527 struct bt_value *params, void *init_method_data)
528{
529 int ret = 0;
530 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
531 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
532
533 if (!dmesg_comp) {
33ec5dfa 534 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
d8866baa
PP
535 goto error;
536 }
537
538 dmesg_comp->params.path = g_string_new(NULL);
539 if (!dmesg_comp->params.path) {
33ec5dfa 540 BT_LOGE_STR("Failed to allocate a GString.");
d8866baa
PP
541 goto error;
542 }
543
33ec5dfa 544 ret = handle_params(dmesg_comp, params);
d8866baa 545 if (ret) {
33ec5dfa 546 BT_LOGE("Invalid parameters: comp-addr=%p", priv_comp);
d8866baa
PP
547 goto error;
548 }
549
33ec5dfa
PP
550 if (!dmesg_comp->params.read_from_stdin &&
551 !g_file_test(dmesg_comp->params.path->str,
552 G_FILE_TEST_IS_REGULAR)) {
553 BT_LOGE("Input path is not a regular file: "
554 "comp-addr=%p, path=\"%s\"", priv_comp,
555 dmesg_comp->params.path->str);
556 goto error;
557 }
d8866baa 558
33ec5dfa
PP
559 status = create_port(priv_comp);
560 if (status != BT_COMPONENT_STATUS_OK) {
561 goto error;
562 }
d8866baa 563
33ec5dfa 564 (void) bt_private_component_set_user_data(priv_comp, dmesg_comp);
d8866baa
PP
565 goto end;
566
567error:
568 destroy_dmesg_component(dmesg_comp);
33ec5dfa
PP
569 (void) bt_private_component_set_user_data(priv_comp, NULL);
570
571 if (status >= 0) {
572 status = BT_COMPONENT_STATUS_ERROR;
573 }
d8866baa
PP
574
575end:
576 return status;
577}
578
579BT_HIDDEN
580void dmesg_finalize(struct bt_private_component *priv_comp)
581{
33ec5dfa
PP
582 void *data = bt_private_component_get_user_data(priv_comp);
583
584 destroy_dmesg_component(data);
585}
586
587static
312c056a 588struct bt_notification *create_init_event_notif_from_line(
f0010051 589 struct dmesg_notif_iter *notif_iter,
312c056a 590 const char *line, const char **new_start)
33ec5dfa 591{
312c056a
PP
592 struct bt_event *event;
593 struct bt_notification *notif = NULL;
33ec5dfa
PP
594 bool has_timestamp = false;
595 unsigned long sec, usec, msec;
596 unsigned int year, mon, mday, hour, min;
597 uint64_t ts = 0;
50842bdc
PP
598 struct bt_field *eh_field = NULL;
599 struct bt_field *ts_field = NULL;
33ec5dfa 600 int ret = 0;
f0010051 601 struct dmesg_component *dmesg_comp = notif_iter->dmesg_comp;
33ec5dfa 602
33ec5dfa
PP
603 *new_start = line;
604
707b323a
PP
605 if (dmesg_comp->params.no_timestamp) {
606 goto skip_ts;
607 }
608
33ec5dfa
PP
609 /* Extract time from input line */
610 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
611 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
612
613 /*
614 * The clock class we use has a 1 GHz frequency: convert
615 * from µs to ns.
616 */
617 ts *= NSEC_PER_USEC;
618 has_timestamp = true;
619 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
620 &year, &mon, &mday, &hour, &min,
621 &sec, &msec) == 7) {
622 time_t ep_sec;
623 struct tm ti;
624
625 memset(&ti, 0, sizeof(ti));
626 ti.tm_year = year - 1900; /* From 1900 */
627 ti.tm_mon = mon - 1; /* 0 to 11 */
628 ti.tm_mday = mday;
629 ti.tm_hour = hour;
630 ti.tm_min = min;
631 ti.tm_sec = sec;
632
633 ep_sec = bt_timegm(&ti);
634 if (ep_sec != (time_t) -1) {
635 ts = (uint64_t) ep_sec * NSEC_PER_SEC
636 + (uint64_t) msec * NSEC_PER_MSEC;
637 }
638
639 has_timestamp = true;
640 }
641
642 if (has_timestamp) {
643 /* Set new start for the message portion of the line */
644 *new_start = strchr(line, ']');
f6ccaed9 645 BT_ASSERT(*new_start);
33ec5dfa
PP
646 (*new_start)++;
647
648 if ((*new_start)[0] == ' ') {
649 (*new_start)++;
650 }
651 }
652
707b323a 653skip_ts:
33ec5dfa
PP
654 /*
655 * At this point, we know if the stream class's event header
656 * field type should have a timestamp or not, so we can lazily
657 * create the metadata, stream, and packet objects.
658 */
659 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
660 if (ret) {
661 /* try_create_meta_stream_packet() logs errors */
662 goto error;
663 }
664
f0010051 665 notif = bt_notification_event_create(notif_iter->pc_notif_iter,
e22b45d0 666 dmesg_comp->event_class, dmesg_comp->packet);
312c056a
PP
667 if (!notif) {
668 BT_LOGE_STR("Cannot create event notification.");
669 goto error;
670 }
33ec5dfa 671
312c056a
PP
672 event = bt_notification_event_borrow_event(notif);
673 BT_ASSERT(event);
33ec5dfa 674
312c056a 675 if (dmesg_comp->clock_class) {
e22b45d0
PP
676 ret = bt_event_set_clock_value(event,
677 dmesg_comp->clock_class, ts, BT_TRUE);
312c056a
PP
678 BT_ASSERT(ret == 0);
679 eh_field = bt_event_borrow_header(event);
680 BT_ASSERT(eh_field);
681 ts_field = bt_field_structure_borrow_field_by_name(eh_field,
33ec5dfa
PP
682 "timestamp");
683 if (!ts_field) {
312c056a 684 BT_LOGE_STR("Cannot borrow `timestamp` field from event header structure field.");
33ec5dfa
PP
685 goto error;
686 }
687
312c056a
PP
688 ret = bt_field_integer_unsigned_set_value(ts_field, ts);
689 BT_ASSERT(ret == 0);
33ec5dfa
PP
690 }
691
692 goto end;
693
694error:
312c056a 695 BT_PUT(notif);
33ec5dfa
PP
696
697end:
312c056a 698 return notif;
33ec5dfa
PP
699}
700
701static
f0010051 702int fill_event_payload_from_line(const char *line, struct bt_event *event)
33ec5dfa 703{
50842bdc
PP
704 struct bt_field *ep_field = NULL;
705 struct bt_field *str_field = NULL;
33ec5dfa
PP
706 size_t len;
707 int ret;
708
312c056a
PP
709 ep_field = bt_event_borrow_payload(event);
710 BT_ASSERT(ep_field);
711 str_field = bt_field_structure_borrow_field_by_name(ep_field, "str");
33ec5dfa 712 if (!str_field) {
312c056a 713 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
33ec5dfa
PP
714 goto error;
715 }
716
717 len = strlen(line);
718 if (line[len - 1] == '\n') {
719 /* Do not include the newline character in the payload */
720 len--;
721 }
722
312c056a
PP
723 ret = bt_field_string_clear(str_field);
724 if (ret) {
725 BT_LOGE_STR("Cannot clear string field object.");
726 goto error;
727 }
728
50842bdc 729 ret = bt_field_string_append_len(str_field, line, len);
33ec5dfa
PP
730 if (ret) {
731 BT_LOGE("Cannot append value to string field object: "
732 "len=%zu", len);
733 goto error;
734 }
735
33ec5dfa
PP
736 goto end;
737
738error:
739 ret = -1;
740
741end:
33ec5dfa
PP
742 return ret;
743}
744
745static
746struct bt_notification *create_notif_from_line(
f0010051 747 struct dmesg_notif_iter *dmesg_notif_iter, const char *line)
33ec5dfa 748{
50842bdc 749 struct bt_event *event = NULL;
33ec5dfa
PP
750 struct bt_notification *notif = NULL;
751 const char *new_start;
752 int ret;
753
f0010051
PP
754 notif = create_init_event_notif_from_line(dmesg_notif_iter,
755 line, &new_start);
312c056a
PP
756 if (!notif) {
757 BT_LOGE_STR("Cannot create and initialize event notification from line.");
33ec5dfa
PP
758 goto error;
759 }
760
312c056a
PP
761 event = bt_notification_event_borrow_event(notif);
762 BT_ASSERT(event);
f0010051 763 ret = fill_event_payload_from_line(new_start, event);
33ec5dfa 764 if (ret) {
312c056a 765 BT_LOGE("Cannot fill event payload field from line: "
33ec5dfa
PP
766 "ret=%d", ret);
767 goto error;
768 }
769
33ec5dfa
PP
770 goto end;
771
772error:
773 BT_PUT(notif);
774
775end:
33ec5dfa
PP
776 return notif;
777}
778
779static
780void destroy_dmesg_notif_iter(struct dmesg_notif_iter *dmesg_notif_iter)
781{
782 if (!dmesg_notif_iter) {
783 return;
784 }
785
786 if (dmesg_notif_iter->fp && dmesg_notif_iter->fp != stdin) {
787 if (fclose(dmesg_notif_iter->fp)) {
788 BT_LOGE_ERRNO("Cannot close input file", ".");
789 }
790 }
d8866baa 791
312c056a 792 bt_put(dmesg_notif_iter->tmp_event_notif);
33ec5dfa
PP
793 free(dmesg_notif_iter->linebuf);
794 g_free(dmesg_notif_iter);
d8866baa
PP
795}
796
797BT_HIDDEN
798enum bt_notification_iterator_status dmesg_notif_iter_init(
90157d89 799 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
d8866baa
PP
800 struct bt_private_port *priv_port)
801{
33ec5dfa
PP
802 struct bt_private_component *priv_comp = NULL;
803 struct dmesg_component *dmesg_comp;
804 struct dmesg_notif_iter *dmesg_notif_iter =
805 g_new0(struct dmesg_notif_iter, 1);
806 enum bt_notification_iterator_status status =
807 BT_NOTIFICATION_ITERATOR_STATUS_OK;
808
809 if (!dmesg_notif_iter) {
810 BT_LOGE_STR("Failed to allocate on dmesg notification iterator structure.");
811 goto error;
812 }
813
90157d89 814 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
33ec5dfa 815 priv_notif_iter);
f6ccaed9 816 BT_ASSERT(priv_comp);
33ec5dfa 817 dmesg_comp = bt_private_component_get_user_data(priv_comp);
f6ccaed9 818 BT_ASSERT(dmesg_comp);
33ec5dfa 819 dmesg_notif_iter->dmesg_comp = dmesg_comp;
f0010051 820 dmesg_notif_iter->pc_notif_iter = priv_notif_iter;
d8866baa 821
33ec5dfa
PP
822 if (dmesg_comp->params.read_from_stdin) {
823 dmesg_notif_iter->fp = stdin;
824 } else {
825 dmesg_notif_iter->fp = fopen(dmesg_comp->params.path->str, "r");
826 if (!dmesg_notif_iter->fp) {
827 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
828 dmesg_comp->params.path->str);
829 goto error;
830 }
831 }
832
90157d89 833 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
33ec5dfa
PP
834 dmesg_notif_iter);
835 goto end;
836
837error:
838 destroy_dmesg_notif_iter(dmesg_notif_iter);
90157d89 839 (void) bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
33ec5dfa
PP
840 NULL);
841 if (status >= 0) {
842 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
843 }
844
845end:
846 bt_put(priv_comp);
847 return status;
d8866baa
PP
848}
849
850BT_HIDDEN
33ec5dfa 851void dmesg_notif_iter_finalize(
90157d89 852 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
d8866baa 853{
90157d89 854 destroy_dmesg_notif_iter(bt_private_connection_private_notification_iterator_get_user_data(
33ec5dfa 855 priv_notif_iter));
d8866baa
PP
856}
857
d4393e08
PP
858static
859enum bt_notification_iterator_status dmesg_notif_iter_next_one(
860 struct dmesg_notif_iter *dmesg_notif_iter,
861 struct bt_notification **notif)
d8866baa 862{
33ec5dfa 863 ssize_t len;
33ec5dfa 864 struct dmesg_component *dmesg_comp;
d4393e08
PP
865 enum bt_notification_iterator_status status =
866 BT_NOTIFICATION_ITERATOR_STATUS_OK;
d8866baa 867
f6ccaed9 868 BT_ASSERT(dmesg_notif_iter);
33ec5dfa 869 dmesg_comp = dmesg_notif_iter->dmesg_comp;
f6ccaed9 870 BT_ASSERT(dmesg_comp);
33ec5dfa 871
312c056a 872 if (dmesg_notif_iter->state == STATE_DONE) {
d4393e08 873 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
312c056a
PP
874 goto end;
875 }
876
877 if (dmesg_notif_iter->tmp_event_notif ||
878 dmesg_notif_iter->state == STATE_EMIT_PACKET_END ||
879 dmesg_notif_iter->state == STATE_EMIT_STREAM_END) {
880 goto handle_state;
881 }
882
33ec5dfa
PP
883 while (true) {
884 const char *ch;
885 bool only_spaces = true;
886
887 len = bt_getline(&dmesg_notif_iter->linebuf,
888 &dmesg_notif_iter->linebuf_len, dmesg_notif_iter->fp);
889 if (len < 0) {
890 if (errno == EINVAL) {
d4393e08 891 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
33ec5dfa 892 } else if (errno == ENOMEM) {
d4393e08 893 status =
33ec5dfa
PP
894 BT_NOTIFICATION_ITERATOR_STATUS_NOMEM;
895 } else {
312c056a
PP
896 if (dmesg_notif_iter->state == STATE_EMIT_STREAM_BEGINNING) {
897 /* Stream did not even begin */
d4393e08 898 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
312c056a
PP
899 goto end;
900 } else {
901 /* End current packet now */
902 dmesg_notif_iter->state =
903 STATE_EMIT_PACKET_END;
904 goto handle_state;
905 }
33ec5dfa
PP
906 }
907
908 goto end;
909 }
910
f6ccaed9 911 BT_ASSERT(dmesg_notif_iter->linebuf);
33ec5dfa
PP
912
913 /* Ignore empty lines, once trimmed */
914 for (ch = dmesg_notif_iter->linebuf; *ch != '\0'; ch++) {
915 if (!isspace(*ch)) {
916 only_spaces = false;
917 break;
918 }
919 }
920
921 if (!only_spaces) {
922 break;
923 }
924 }
925
f0010051
PP
926 dmesg_notif_iter->tmp_event_notif = create_notif_from_line(
927 dmesg_notif_iter, dmesg_notif_iter->linebuf);
312c056a 928 if (!dmesg_notif_iter->tmp_event_notif) {
33ec5dfa
PP
929 BT_LOGE("Cannot create event notification from line: "
930 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
931 dmesg_notif_iter->linebuf);
312c056a
PP
932 goto end;
933 }
934
935handle_state:
936 BT_ASSERT(dmesg_comp->trace);
937
938 switch (dmesg_notif_iter->state) {
939 case STATE_EMIT_STREAM_BEGINNING:
940 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
d4393e08 941 *notif = bt_notification_stream_begin_create(
f0010051 942 dmesg_notif_iter->pc_notif_iter, dmesg_comp->stream);
312c056a
PP
943 dmesg_notif_iter->state = STATE_EMIT_PACKET_BEGINNING;
944 break;
945 case STATE_EMIT_PACKET_BEGINNING:
946 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
d4393e08 947 *notif = bt_notification_packet_begin_create(
f0010051 948 dmesg_notif_iter->pc_notif_iter, dmesg_comp->packet);
312c056a
PP
949 dmesg_notif_iter->state = STATE_EMIT_EVENT;
950 break;
951 case STATE_EMIT_EVENT:
952 BT_ASSERT(dmesg_notif_iter->tmp_event_notif);
d4393e08
PP
953 *notif = dmesg_notif_iter->tmp_event_notif;
954 dmesg_notif_iter->tmp_event_notif = NULL;
312c056a
PP
955 break;
956 case STATE_EMIT_PACKET_END:
d4393e08 957 *notif = bt_notification_packet_end_create(
f0010051 958 dmesg_notif_iter->pc_notif_iter, dmesg_comp->packet);
312c056a
PP
959 dmesg_notif_iter->state = STATE_EMIT_STREAM_END;
960 break;
961 case STATE_EMIT_STREAM_END:
d4393e08 962 *notif = bt_notification_stream_end_create(
f0010051 963 dmesg_notif_iter->pc_notif_iter, dmesg_comp->stream);
312c056a
PP
964 dmesg_notif_iter->state = STATE_DONE;
965 break;
966 default:
967 break;
968 }
969
d4393e08 970 if (!*notif) {
312c056a
PP
971 BT_LOGE("Cannot create notification: dmesg-comp-addr=%p",
972 dmesg_comp);
d4393e08 973 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
33ec5dfa
PP
974 }
975
976end:
d4393e08
PP
977 return status;
978}
979
980BT_HIDDEN
981enum bt_notification_iterator_status dmesg_notif_iter_next(
982 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
983 bt_notification_array notifs, uint64_t capacity,
984 uint64_t *count)
985{
986 struct dmesg_notif_iter *dmesg_notif_iter =
987 bt_private_connection_private_notification_iterator_get_user_data(
988 priv_notif_iter);
989 enum bt_notification_iterator_status status =
990 BT_NOTIFICATION_ITERATOR_STATUS_OK;
991 uint64_t i = 0;
992
993 while (i < capacity && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
994 status = dmesg_notif_iter_next_one(dmesg_notif_iter, &notifs[i]);
995 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
996 i++;
997 }
998 }
999
1000 if (i > 0) {
1001 /*
1002 * Even if dmesg_notif_iter_next_one() returned
1003 * something else than
1004 * BT_NOTIFICATION_ITERATOR_STATUS_OK, we accumulated
1005 * notification objects in the output notification
1006 * array, so we need to return
1007 * BT_NOTIFICATION_ITERATOR_STATUS_OK so that they are
1008 * transfered to downstream. This other status occurs
1009 * again the next time muxer_notif_iter_do_next() is
1010 * called, possibly without any accumulated
1011 * notification, in which case we'll return it.
1012 */
1013 *count = i;
1014 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
1015 }
1016
1017 return status;
d8866baa 1018}
This page took 0.077838 seconds and 4 git commands to generate.