lib: rename "notification" -> "message"
[babeltrace.git] / plugins / text / dmesg / dmesg.c
CommitLineData
d8866baa
PP
1/*
2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
834e9996 3 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
d8866baa
PP
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>
17582c6d 32#include <babeltrace/common-internal.h>
e2531d3b 33#include <babeltrace/babeltrace.h>
0f15f666 34#include <babeltrace/value-internal.h>
e2531d3b
PP
35#include <babeltrace/compat/utc-internal.h>
36#include <babeltrace/compat/stdio-internal.h>
d8866baa
PP
37#include <glib.h>
38
e2531d3b
PP
39#define NSEC_PER_USEC 1000UL
40#define NSEC_PER_MSEC 1000000UL
41#define NSEC_PER_SEC 1000000000ULL
42#define USEC_PER_SEC 1000000UL
43
d8866baa
PP
44struct dmesg_component;
45
b09a5592 46struct dmesg_msg_iter {
d8866baa 47 struct dmesg_component *dmesg_comp;
b09a5592 48 bt_self_message_iterator *pc_msg_iter; /* Weak */
e2531d3b
PP
49 char *linebuf;
50 size_t linebuf_len;
d8866baa 51 FILE *fp;
b09a5592 52 bt_message *tmp_event_msg;
a6918753
PP
53
54 enum {
55 STATE_EMIT_STREAM_BEGINNING,
56 STATE_EMIT_PACKET_BEGINNING,
57 STATE_EMIT_EVENT,
58 STATE_EMIT_PACKET_END,
59 STATE_EMIT_STREAM_END,
60 STATE_DONE,
61 } state;
d8866baa
PP
62};
63
64struct dmesg_component {
65 struct {
66 GString *path;
e2531d3b 67 bt_bool read_from_stdin;
8743317e 68 bt_bool no_timestamp;
d8866baa
PP
69 } params;
70
8eee8ea2
PP
71 bt_trace_class *trace_class;
72 bt_stream_class *stream_class;
73 bt_event_class *event_class;
74 bt_trace *trace;
75 bt_stream *stream;
76 bt_packet *packet;
77 bt_clock_class *clock_class;
d8866baa
PP
78};
79
e2531d3b 80static
8eee8ea2 81bt_field_class *create_event_payload_fc(void)
e2531d3b 82{
8eee8ea2
PP
83 bt_field_class *root_fc = NULL;
84 bt_field_class *fc = NULL;
e2531d3b
PP
85 int ret;
86
78cf9df6 87 root_fc = bt_field_class_structure_create();
939190b3
PP
88 if (!root_fc) {
89 BT_LOGE_STR("Cannot create an empty structure field class object.");
e2531d3b
PP
90 goto error;
91 }
92
78cf9df6 93 fc = bt_field_class_string_create();
939190b3
PP
94 if (!fc) {
95 BT_LOGE_STR("Cannot create a string field class object.");
e2531d3b
PP
96 goto error;
97 }
98
78cf9df6 99 ret = bt_field_class_structure_append_member(root_fc,
9e550e5f 100 "str", fc);
e2531d3b 101 if (ret) {
939190b3 102 BT_LOGE("Cannot add `str` member to structure field class: "
e2531d3b
PP
103 "ret=%d", ret);
104 goto error;
105 }
106
107 goto end;
108
109error:
8c6884d9 110 BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc);
e2531d3b
PP
111
112end:
8c6884d9 113 bt_field_class_put_ref(fc);
939190b3 114 return root_fc;
e2531d3b
PP
115}
116
e2531d3b
PP
117static
118int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
119{
8eee8ea2 120 bt_field_class *fc = NULL;
d8866baa
PP
121 int ret = 0;
122
10b7a2e4
PP
123 dmesg_comp->trace_class = bt_trace_class_create();
124 if (!dmesg_comp->trace_class) {
125 BT_LOGE_STR("Cannot create an empty trace class object.");
e2531d3b
PP
126 goto error;
127 }
128
78cf9df6 129 dmesg_comp->stream_class = bt_stream_class_create(
10b7a2e4 130 dmesg_comp->trace_class);
e2531d3b 131 if (!dmesg_comp->stream_class) {
7b33a0e0 132 BT_LOGE_STR("Cannot create a stream class object.");
e2531d3b
PP
133 goto error;
134 }
135
e2531d3b 136 if (has_ts) {
78cf9df6 137 dmesg_comp->clock_class = bt_clock_class_create();
e2531d3b
PP
138 if (!dmesg_comp->clock_class) {
139 BT_LOGE_STR("Cannot create clock class.");
140 goto error;
141 }
142
78cf9df6
PP
143 ret = bt_stream_class_set_default_clock_class(
144 dmesg_comp->stream_class, dmesg_comp->clock_class);
e2531d3b 145 if (ret) {
7b33a0e0 146 BT_LOGE_STR("Cannot set stream class's default clock class.");
e2531d3b
PP
147 goto error;
148 }
149 }
150
78cf9df6 151 dmesg_comp->event_class = bt_event_class_create(
7b33a0e0 152 dmesg_comp->stream_class);
e2531d3b 153 if (!dmesg_comp->event_class) {
7b33a0e0
PP
154 BT_LOGE_STR("Cannot create an event class object.");
155 goto error;
156 }
157
78cf9df6 158 ret = bt_event_class_set_name(dmesg_comp->event_class, "string");
7b33a0e0
PP
159 if (ret) {
160 BT_LOGE_STR("Cannot set event class's name.");
e2531d3b
PP
161 goto error;
162 }
163
939190b3
PP
164 fc = create_event_payload_fc();
165 if (!fc) {
166 BT_LOGE_STR("Cannot create event payload field class.");
d8866baa
PP
167 goto error;
168 }
169
78cf9df6 170 ret = bt_event_class_set_payload_field_class(dmesg_comp->event_class, fc);
e2531d3b 171 if (ret) {
939190b3 172 BT_LOGE_STR("Cannot set event class's event payload field class.");
e2531d3b
PP
173 goto error;
174 }
175
e2531d3b
PP
176 goto end;
177
178error:
179 ret = -1;
180
181end:
8c6884d9 182 bt_field_class_put_ref(fc);
e2531d3b
PP
183 return ret;
184}
185
186static
ce141536 187int handle_params(struct dmesg_component *dmesg_comp,
8eee8ea2 188 const bt_value *params)
e2531d3b 189{
8eee8ea2
PP
190 const bt_value *no_timestamp = NULL;
191 const bt_value *path = NULL;
e2531d3b
PP
192 const char *path_str;
193 int ret = 0;
194
ce141536 195 no_timestamp = bt_value_map_borrow_entry_value_const(params,
44514773 196 "no-extract-timestamp");
8743317e
PP
197 if (no_timestamp) {
198 if (!bt_value_is_bool(no_timestamp)) {
199 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
200 "type=%s",
17582c6d 201 bt_common_value_type_string(
8743317e
PP
202 bt_value_get_type(no_timestamp)));
203 goto error;
204 }
205
b5cdc106
PP
206 dmesg_comp->params.no_timestamp =
207 bt_value_bool_get(no_timestamp);
8743317e
PP
208 }
209
ce141536 210 path = bt_value_map_borrow_entry_value_const(params, "path");
d8866baa
PP
211 if (path) {
212 if (dmesg_comp->params.read_from_stdin) {
e2531d3b 213 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
d8866baa
PP
214 goto error;
215 }
216
217 if (!bt_value_is_string(path)) {
e2531d3b
PP
218 BT_LOGE("Expecting a string value for the `path` parameter: "
219 "type=%s",
17582c6d 220 bt_common_value_type_string(
e2531d3b 221 bt_value_get_type(path)));
d8866baa
PP
222 goto error;
223 }
224
b5cdc106 225 path_str = bt_value_string_get(path);
d8866baa
PP
226 g_string_assign(dmesg_comp->params.path, path_str);
227 } else {
2a2f36a2 228 dmesg_comp->params.read_from_stdin = true;
d8866baa
PP
229 }
230
231 goto end;
232
233error:
234 ret = -1;
235
236end:
d8866baa
PP
237 return ret;
238}
239
e2531d3b 240static
10b7a2e4 241int create_packet_and_stream_and_trace(struct dmesg_component *dmesg_comp)
e2531d3b
PP
242{
243 int ret = 0;
10b7a2e4
PP
244 const char *trace_name;
245 gchar *basename = NULL;
246
247 dmesg_comp->trace = bt_trace_create(dmesg_comp->trace_class);
248 if (!dmesg_comp->trace) {
249 BT_LOGE_STR("Cannot create trace object.");
250 goto error;
251 }
e2531d3b 252
10b7a2e4
PP
253 if (dmesg_comp->params.read_from_stdin) {
254 trace_name = "STDIN";
255 } else {
256 basename = g_path_get_basename(dmesg_comp->params.path->str);
257 BT_ASSERT(basename);
258
259 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
260 strcmp(basename, ".") != 0) {
261 trace_name = basename;
262 }
263 }
264
265 if (trace_name) {
266 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
267 if (ret) {
268 BT_LOGE("Cannot set trace's name: name=\"%s\"",
269 trace_name);
270 goto error;
271 }
272 }
273
274 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class,
275 dmesg_comp->trace);
e2531d3b
PP
276 if (!dmesg_comp->stream) {
277 BT_LOGE_STR("Cannot create stream object.");
278 goto error;
279 }
280
78cf9df6 281 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream);
e2531d3b
PP
282 if (!dmesg_comp->packet) {
283 BT_LOGE_STR("Cannot create packet object.");
284 goto error;
285 }
286
78cf9df6 287 ret = bt_trace_make_static(dmesg_comp->trace);
e2531d3b
PP
288 if (ret) {
289 BT_LOGE_STR("Cannot make trace static.");
290 goto error;
291 }
292
293 goto end;
294
295error:
296 ret = -1;
297
298end:
10b7a2e4
PP
299 if (basename) {
300 g_free(basename);
301 }
302
e2531d3b
PP
303 return ret;
304}
305
306static
307int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp,
308 bool has_ts)
309{
310 int ret = 0;
311
312 if (dmesg_comp->trace) {
313 /* Already created */
314 goto end;
315 }
316
317 ret = create_meta(dmesg_comp, has_ts);
318 if (ret) {
319 BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
320 dmesg_comp);
321 goto error;
322 }
323
10b7a2e4 324 ret = create_packet_and_stream_and_trace(dmesg_comp);
e2531d3b
PP
325 if (ret) {
326 BT_LOGE("Cannot create packet and stream objects: "
327 "dmesg-comp-addr=%p", dmesg_comp);
328 goto error;
329 }
330
331 goto end;
332
333error:
334 ret = -1;
335
336end:
337 return ret;
338}
d8866baa
PP
339
340static
341void destroy_dmesg_component(struct dmesg_component *dmesg_comp)
342{
343 if (!dmesg_comp) {
344 return;
345 }
346
347 if (dmesg_comp->params.path) {
348 g_string_free(dmesg_comp->params.path, TRUE);
349 }
350
8c6884d9
PP
351 bt_packet_put_ref(dmesg_comp->packet);
352 bt_trace_put_ref(dmesg_comp->trace);
353 bt_stream_class_put_ref(dmesg_comp->stream_class);
354 bt_event_class_put_ref(dmesg_comp->event_class);
355 bt_stream_put_ref(dmesg_comp->stream);
356 bt_clock_class_put_ref(dmesg_comp->clock_class);
d8866baa
PP
357 g_free(dmesg_comp);
358}
359
e2531d3b 360static
834e9996 361enum bt_self_component_status create_port(
8eee8ea2 362 bt_self_component_source *self_comp)
e2531d3b 363{
834e9996 364 return bt_self_component_source_add_output_port(self_comp,
e2531d3b
PP
365 "out", NULL, NULL);
366}
367
d8866baa 368BT_HIDDEN
834e9996 369enum bt_self_component_status dmesg_init(
8eee8ea2
PP
370 bt_self_component_source *self_comp,
371 bt_value *params, void *init_method_data)
d8866baa
PP
372{
373 int ret = 0;
374 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
834e9996 375 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
d8866baa
PP
376
377 if (!dmesg_comp) {
e2531d3b 378 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
d8866baa
PP
379 goto error;
380 }
381
382 dmesg_comp->params.path = g_string_new(NULL);
383 if (!dmesg_comp->params.path) {
e2531d3b 384 BT_LOGE_STR("Failed to allocate a GString.");
d8866baa
PP
385 goto error;
386 }
387
e2531d3b 388 ret = handle_params(dmesg_comp, params);
d8866baa 389 if (ret) {
834e9996 390 BT_LOGE("Invalid parameters: comp-addr=%p", self_comp);
d8866baa
PP
391 goto error;
392 }
393
e2531d3b
PP
394 if (!dmesg_comp->params.read_from_stdin &&
395 !g_file_test(dmesg_comp->params.path->str,
396 G_FILE_TEST_IS_REGULAR)) {
397 BT_LOGE("Input path is not a regular file: "
834e9996 398 "comp-addr=%p, path=\"%s\"", self_comp,
e2531d3b
PP
399 dmesg_comp->params.path->str);
400 goto error;
401 }
d8866baa 402
834e9996
PP
403 status = create_port(self_comp);
404 if (status != BT_SELF_COMPONENT_STATUS_OK) {
e2531d3b
PP
405 goto error;
406 }
d8866baa 407
834e9996 408 bt_self_component_set_data(
bb61965b 409 bt_self_component_source_as_self_component(self_comp),
834e9996 410 dmesg_comp);
d8866baa
PP
411 goto end;
412
413error:
414 destroy_dmesg_component(dmesg_comp);
834e9996 415 bt_self_component_set_data(
bb61965b 416 bt_self_component_source_as_self_component(self_comp),
834e9996 417 NULL);
e2531d3b
PP
418
419 if (status >= 0) {
834e9996 420 status = BT_SELF_COMPONENT_STATUS_ERROR;
e2531d3b 421 }
d8866baa
PP
422
423end:
424 return status;
425}
426
427BT_HIDDEN
8eee8ea2 428void dmesg_finalize(bt_self_component_source *self_comp)
d8866baa 429{
834e9996 430 destroy_dmesg_component(bt_self_component_get_data(
bb61965b 431 bt_self_component_source_as_self_component(self_comp)));
e2531d3b
PP
432}
433
434static
b09a5592
PP
435bt_message *create_init_event_msg_from_line(
436 struct dmesg_msg_iter *msg_iter,
a6918753 437 const char *line, const char **new_start)
e2531d3b 438{
8eee8ea2 439 bt_event *event;
b09a5592 440 bt_message *msg = NULL;
e2531d3b
PP
441 bool has_timestamp = false;
442 unsigned long sec, usec, msec;
443 unsigned int year, mon, mday, hour, min;
444 uint64_t ts = 0;
e2531d3b 445 int ret = 0;
b09a5592 446 struct dmesg_component *dmesg_comp = msg_iter->dmesg_comp;
e2531d3b 447
e2531d3b
PP
448 *new_start = line;
449
8743317e
PP
450 if (dmesg_comp->params.no_timestamp) {
451 goto skip_ts;
452 }
453
e2531d3b
PP
454 /* Extract time from input line */
455 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
456 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
457
458 /*
459 * The clock class we use has a 1 GHz frequency: convert
460 * from µs to ns.
461 */
462 ts *= NSEC_PER_USEC;
463 has_timestamp = true;
464 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
465 &year, &mon, &mday, &hour, &min,
466 &sec, &msec) == 7) {
467 time_t ep_sec;
468 struct tm ti;
469
470 memset(&ti, 0, sizeof(ti));
471 ti.tm_year = year - 1900; /* From 1900 */
472 ti.tm_mon = mon - 1; /* 0 to 11 */
473 ti.tm_mday = mday;
474 ti.tm_hour = hour;
475 ti.tm_min = min;
476 ti.tm_sec = sec;
477
478 ep_sec = bt_timegm(&ti);
479 if (ep_sec != (time_t) -1) {
480 ts = (uint64_t) ep_sec * NSEC_PER_SEC
481 + (uint64_t) msec * NSEC_PER_MSEC;
482 }
483
484 has_timestamp = true;
485 }
486
487 if (has_timestamp) {
488 /* Set new start for the message portion of the line */
489 *new_start = strchr(line, ']');
8b45963b 490 BT_ASSERT(*new_start);
e2531d3b
PP
491 (*new_start)++;
492
493 if ((*new_start)[0] == ' ') {
494 (*new_start)++;
495 }
496 }
497
8743317e 498skip_ts:
e2531d3b
PP
499 /*
500 * At this point, we know if the stream class's event header
939190b3 501 * field class should have a timestamp or not, so we can lazily
e2531d3b
PP
502 * create the metadata, stream, and packet objects.
503 */
504 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
505 if (ret) {
506 /* try_create_meta_stream_packet() logs errors */
507 goto error;
508 }
509
b09a5592 510 msg = bt_message_event_create(msg_iter->pc_msg_iter,
8fc063a2 511 dmesg_comp->event_class, dmesg_comp->packet);
b09a5592
PP
512 if (!msg) {
513 BT_LOGE_STR("Cannot create event message.");
a6918753
PP
514 goto error;
515 }
e2531d3b 516
b09a5592 517 event = bt_message_event_borrow_event(msg);
a6918753 518 BT_ASSERT(event);
e2531d3b 519
a6918753 520 if (dmesg_comp->clock_class) {
78cf9df6 521 bt_event_set_default_clock_value(event, ts);
e2531d3b
PP
522 }
523
524 goto end;
525
526error:
b09a5592 527 BT_MESSAGE_PUT_REF_AND_RESET(msg);
e2531d3b
PP
528
529end:
b09a5592 530 return msg;
e2531d3b
PP
531}
532
533static
9e550e5f 534int fill_event_payload_from_line(const char *line,
8eee8ea2 535 bt_event *event)
e2531d3b 536{
8eee8ea2
PP
537 bt_field *ep_field = NULL;
538 bt_field *str_field = NULL;
e2531d3b
PP
539 size_t len;
540 int ret;
541
78cf9df6 542 ep_field = bt_event_borrow_payload_field(event);
a6918753 543 BT_ASSERT(ep_field);
78cf9df6 544 str_field = bt_field_structure_borrow_member_field_by_index(
9e550e5f 545 ep_field, 0);
e2531d3b 546 if (!str_field) {
a6918753 547 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
e2531d3b
PP
548 goto error;
549 }
550
551 len = strlen(line);
552 if (line[len - 1] == '\n') {
553 /* Do not include the newline character in the payload */
554 len--;
555 }
556
78cf9df6 557 ret = bt_field_string_clear(str_field);
a6918753
PP
558 if (ret) {
559 BT_LOGE_STR("Cannot clear string field object.");
560 goto error;
561 }
562
78cf9df6 563 ret = bt_field_string_append_with_length(str_field, line, len);
e2531d3b
PP
564 if (ret) {
565 BT_LOGE("Cannot append value to string field object: "
566 "len=%zu", len);
567 goto error;
568 }
569
e2531d3b
PP
570 goto end;
571
572error:
573 ret = -1;
574
575end:
e2531d3b
PP
576 return ret;
577}
578
579static
b09a5592
PP
580bt_message *create_msg_from_line(
581 struct dmesg_msg_iter *dmesg_msg_iter, const char *line)
e2531d3b 582{
8eee8ea2 583 bt_event *event = NULL;
b09a5592 584 bt_message *msg = NULL;
e2531d3b
PP
585 const char *new_start;
586 int ret;
587
b09a5592 588 msg = create_init_event_msg_from_line(dmesg_msg_iter,
03e18d5d 589 line, &new_start);
b09a5592
PP
590 if (!msg) {
591 BT_LOGE_STR("Cannot create and initialize event message from line.");
e2531d3b
PP
592 goto error;
593 }
594
b09a5592 595 event = bt_message_event_borrow_event(msg);
a6918753 596 BT_ASSERT(event);
03e18d5d 597 ret = fill_event_payload_from_line(new_start, event);
e2531d3b 598 if (ret) {
a6918753 599 BT_LOGE("Cannot fill event payload field from line: "
e2531d3b
PP
600 "ret=%d", ret);
601 goto error;
602 }
603
e2531d3b
PP
604 goto end;
605
606error:
b09a5592 607 BT_MESSAGE_PUT_REF_AND_RESET(msg);
e2531d3b
PP
608
609end:
b09a5592 610 return msg;
e2531d3b
PP
611}
612
613static
b09a5592 614void destroy_dmesg_msg_iter(struct dmesg_msg_iter *dmesg_msg_iter)
e2531d3b 615{
b09a5592 616 if (!dmesg_msg_iter) {
e2531d3b
PP
617 return;
618 }
619
b09a5592
PP
620 if (dmesg_msg_iter->fp && dmesg_msg_iter->fp != stdin) {
621 if (fclose(dmesg_msg_iter->fp)) {
e2531d3b
PP
622 BT_LOGE_ERRNO("Cannot close input file", ".");
623 }
624 }
d8866baa 625
b09a5592
PP
626 bt_message_put_ref(dmesg_msg_iter->tmp_event_msg);
627 free(dmesg_msg_iter->linebuf);
628 g_free(dmesg_msg_iter);
d8866baa
PP
629}
630
631BT_HIDDEN
b09a5592
PP
632enum bt_self_message_iterator_status dmesg_msg_iter_init(
633 bt_self_message_iterator *self_msg_iter,
8eee8ea2
PP
634 bt_self_component_source *self_comp,
635 bt_self_component_port_output *self_port)
d8866baa 636{
e2531d3b 637 struct dmesg_component *dmesg_comp;
b09a5592
PP
638 struct dmesg_msg_iter *dmesg_msg_iter =
639 g_new0(struct dmesg_msg_iter, 1);
640 enum bt_self_message_iterator_status status =
641 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
e2531d3b 642
b09a5592
PP
643 if (!dmesg_msg_iter) {
644 BT_LOGE_STR("Failed to allocate on dmesg message iterator structure.");
e2531d3b
PP
645 goto error;
646 }
647
834e9996 648 dmesg_comp = bt_self_component_get_data(
bb61965b 649 bt_self_component_source_as_self_component(self_comp));
8b45963b 650 BT_ASSERT(dmesg_comp);
b09a5592
PP
651 dmesg_msg_iter->dmesg_comp = dmesg_comp;
652 dmesg_msg_iter->pc_msg_iter = self_msg_iter;
d8866baa 653
e2531d3b 654 if (dmesg_comp->params.read_from_stdin) {
b09a5592 655 dmesg_msg_iter->fp = stdin;
e2531d3b 656 } else {
b09a5592
PP
657 dmesg_msg_iter->fp = fopen(dmesg_comp->params.path->str, "r");
658 if (!dmesg_msg_iter->fp) {
e2531d3b
PP
659 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
660 dmesg_comp->params.path->str);
661 goto error;
662 }
663 }
664
b09a5592
PP
665 bt_self_message_iterator_set_data(self_msg_iter,
666 dmesg_msg_iter);
e2531d3b
PP
667 goto end;
668
669error:
b09a5592
PP
670 destroy_dmesg_msg_iter(dmesg_msg_iter);
671 bt_self_message_iterator_set_data(self_msg_iter, NULL);
e2531d3b 672 if (status >= 0) {
b09a5592 673 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
e2531d3b
PP
674 }
675
676end:
e2531d3b 677 return status;
d8866baa
PP
678}
679
680BT_HIDDEN
b09a5592
PP
681void dmesg_msg_iter_finalize(
682 bt_self_message_iterator *priv_msg_iter)
d8866baa 683{
b09a5592
PP
684 destroy_dmesg_msg_iter(bt_self_message_iterator_get_data(
685 priv_msg_iter));
d8866baa
PP
686}
687
3fd7b79d 688static
b09a5592
PP
689enum bt_self_message_iterator_status dmesg_msg_iter_next_one(
690 struct dmesg_msg_iter *dmesg_msg_iter,
691 bt_message **msg)
d8866baa 692{
e2531d3b 693 ssize_t len;
e2531d3b 694 struct dmesg_component *dmesg_comp;
b09a5592
PP
695 enum bt_self_message_iterator_status status =
696 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
d8866baa 697
b09a5592
PP
698 BT_ASSERT(dmesg_msg_iter);
699 dmesg_comp = dmesg_msg_iter->dmesg_comp;
8b45963b 700 BT_ASSERT(dmesg_comp);
e2531d3b 701
b09a5592
PP
702 if (dmesg_msg_iter->state == STATE_DONE) {
703 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
a6918753
PP
704 goto end;
705 }
706
b09a5592
PP
707 if (dmesg_msg_iter->tmp_event_msg ||
708 dmesg_msg_iter->state == STATE_EMIT_PACKET_END ||
709 dmesg_msg_iter->state == STATE_EMIT_STREAM_END) {
a6918753
PP
710 goto handle_state;
711 }
712
e2531d3b
PP
713 while (true) {
714 const char *ch;
715 bool only_spaces = true;
716
b09a5592
PP
717 len = bt_getline(&dmesg_msg_iter->linebuf,
718 &dmesg_msg_iter->linebuf_len, dmesg_msg_iter->fp);
e2531d3b
PP
719 if (len < 0) {
720 if (errno == EINVAL) {
b09a5592 721 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
e2531d3b 722 } else if (errno == ENOMEM) {
3fd7b79d 723 status =
b09a5592 724 BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM;
e2531d3b 725 } else {
b09a5592 726 if (dmesg_msg_iter->state == STATE_EMIT_STREAM_BEGINNING) {
a6918753 727 /* Stream did not even begin */
b09a5592 728 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
a6918753
PP
729 goto end;
730 } else {
731 /* End current packet now */
b09a5592 732 dmesg_msg_iter->state =
a6918753
PP
733 STATE_EMIT_PACKET_END;
734 goto handle_state;
735 }
e2531d3b
PP
736 }
737
738 goto end;
739 }
740
b09a5592 741 BT_ASSERT(dmesg_msg_iter->linebuf);
e2531d3b
PP
742
743 /* Ignore empty lines, once trimmed */
b09a5592 744 for (ch = dmesg_msg_iter->linebuf; *ch != '\0'; ch++) {
e2531d3b
PP
745 if (!isspace(*ch)) {
746 only_spaces = false;
747 break;
748 }
749 }
750
751 if (!only_spaces) {
752 break;
753 }
754 }
755
b09a5592
PP
756 dmesg_msg_iter->tmp_event_msg = create_msg_from_line(
757 dmesg_msg_iter, dmesg_msg_iter->linebuf);
758 if (!dmesg_msg_iter->tmp_event_msg) {
759 BT_LOGE("Cannot create event message from line: "
e2531d3b 760 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
b09a5592 761 dmesg_msg_iter->linebuf);
a6918753
PP
762 goto end;
763 }
764
765handle_state:
766 BT_ASSERT(dmesg_comp->trace);
767
b09a5592 768 switch (dmesg_msg_iter->state) {
a6918753 769 case STATE_EMIT_STREAM_BEGINNING:
b09a5592
PP
770 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
771 *msg = bt_message_stream_beginning_create(
772 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
773 dmesg_msg_iter->state = STATE_EMIT_PACKET_BEGINNING;
a6918753
PP
774 break;
775 case STATE_EMIT_PACKET_BEGINNING:
b09a5592
PP
776 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
777 *msg = bt_message_packet_beginning_create(
778 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet);
779 dmesg_msg_iter->state = STATE_EMIT_EVENT;
a6918753
PP
780 break;
781 case STATE_EMIT_EVENT:
b09a5592
PP
782 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
783 *msg = dmesg_msg_iter->tmp_event_msg;
784 dmesg_msg_iter->tmp_event_msg = NULL;
a6918753
PP
785 break;
786 case STATE_EMIT_PACKET_END:
b09a5592
PP
787 *msg = bt_message_packet_end_create(
788 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet);
789 dmesg_msg_iter->state = STATE_EMIT_STREAM_END;
a6918753
PP
790 break;
791 case STATE_EMIT_STREAM_END:
b09a5592
PP
792 *msg = bt_message_stream_end_create(
793 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
794 dmesg_msg_iter->state = STATE_DONE;
a6918753
PP
795 break;
796 default:
797 break;
798 }
799
b09a5592
PP
800 if (!*msg) {
801 BT_LOGE("Cannot create message: dmesg-comp-addr=%p",
a6918753 802 dmesg_comp);
b09a5592 803 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
e2531d3b
PP
804 }
805
806end:
3fd7b79d
PP
807 return status;
808}
809
810BT_HIDDEN
b09a5592
PP
811enum bt_self_message_iterator_status dmesg_msg_iter_next(
812 bt_self_message_iterator *self_msg_iter,
813 bt_message_array_const msgs, uint64_t capacity,
3fd7b79d
PP
814 uint64_t *count)
815{
b09a5592
PP
816 struct dmesg_msg_iter *dmesg_msg_iter =
817 bt_self_message_iterator_get_data(
818 self_msg_iter);
819 enum bt_self_message_iterator_status status =
820 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
3fd7b79d
PP
821 uint64_t i = 0;
822
834e9996 823 while (i < capacity &&
b09a5592
PP
824 status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
825 bt_message *priv_msg = NULL;
9e550e5f 826
b09a5592
PP
827 status = dmesg_msg_iter_next_one(dmesg_msg_iter,
828 &priv_msg);
829 msgs[i] = priv_msg;
830 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
3fd7b79d
PP
831 i++;
832 }
833 }
834
835 if (i > 0) {
836 /*
b09a5592 837 * Even if dmesg_msg_iter_next_one() returned
3fd7b79d 838 * something else than
b09a5592
PP
839 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we
840 * accumulated message objects in the output
841 * message array, so we need to return
842 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they
834e9996 843 * are transfered to downstream. This other status
b09a5592 844 * occurs again the next time muxer_msg_iter_do_next()
834e9996 845 * is called, possibly without any accumulated
b09a5592 846 * message, in which case we'll return it.
3fd7b79d
PP
847 */
848 *count = i;
b09a5592 849 status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
3fd7b79d
PP
850 }
851
852 return status;
d8866baa 853}
This page took 0.075083 seconds and 4 git commands to generate.