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