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