lib: rename include dir to babeltrace2
[babeltrace.git] / plugins / text / dmesg / dmesg.c
CommitLineData
d8866baa
PP
1/*
2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
d94d92ac 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
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>
3fadfbc0
MJ
31#include <babeltrace2/assert-internal.h>
32#include <babeltrace2/common-internal.h>
33#include <babeltrace2/babeltrace.h>
34#include <babeltrace2/value-internal.h>
35#include <babeltrace2/compat/utc-internal.h>
36#include <babeltrace2/compat/stdio-internal.h>
d8866baa
PP
37#include <glib.h>
38
33ec5dfa
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
d6e69534 46struct dmesg_msg_iter {
d8866baa 47 struct dmesg_component *dmesg_comp;
d6e69534 48 bt_self_message_iterator *pc_msg_iter; /* Weak */
33ec5dfa
PP
49 char *linebuf;
50 size_t linebuf_len;
d8866baa 51 FILE *fp;
d6e69534 52 bt_message *tmp_event_msg;
a6d85d2f 53 uint64_t last_clock_value;
312c056a
PP
54
55 enum {
56 STATE_EMIT_STREAM_BEGINNING,
a59806b7 57 STATE_EMIT_STREAM_ACTIVITY_BEGINNING,
312c056a
PP
58 STATE_EMIT_PACKET_BEGINNING,
59 STATE_EMIT_EVENT,
60 STATE_EMIT_PACKET_END,
a59806b7 61 STATE_EMIT_STREAM_ACTIVITY_END,
312c056a
PP
62 STATE_EMIT_STREAM_END,
63 STATE_DONE,
64 } state;
d8866baa
PP
65};
66
67struct dmesg_component {
68 struct {
69 GString *path;
33ec5dfa 70 bt_bool read_from_stdin;
707b323a 71 bt_bool no_timestamp;
d8866baa
PP
72 } params;
73
41693723 74 bt_self_component_source *self_comp;
b19ff26f
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
33ec5dfa 84static
1122a43a 85bt_field_class *create_event_payload_fc(bt_trace_class *trace_class)
33ec5dfa 86{
b19ff26f
PP
87 bt_field_class *root_fc = NULL;
88 bt_field_class *fc = NULL;
33ec5dfa
PP
89 int ret;
90
1122a43a 91 root_fc = bt_field_class_structure_create(trace_class);
5cd6d0e5
PP
92 if (!root_fc) {
93 BT_LOGE_STR("Cannot create an empty structure field class object.");
33ec5dfa
PP
94 goto error;
95 }
96
1122a43a 97 fc = bt_field_class_string_create(trace_class);
5cd6d0e5
PP
98 if (!fc) {
99 BT_LOGE_STR("Cannot create a string field class object.");
33ec5dfa
PP
100 goto error;
101 }
102
40f4ba76 103 ret = bt_field_class_structure_append_member(root_fc,
e5be10ef 104 "str", fc);
33ec5dfa 105 if (ret) {
5cd6d0e5 106 BT_LOGE("Cannot add `str` member to structure field class: "
33ec5dfa
PP
107 "ret=%d", ret);
108 goto error;
109 }
110
111 goto end;
112
113error:
c5b9b441 114 BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc);
33ec5dfa
PP
115
116end:
c5b9b441 117 bt_field_class_put_ref(fc);
5cd6d0e5 118 return root_fc;
33ec5dfa
PP
119}
120
33ec5dfa
PP
121static
122int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
123{
b19ff26f 124 bt_field_class *fc = NULL;
d8866baa
PP
125 int ret = 0;
126
41693723
PP
127 dmesg_comp->trace_class = bt_trace_class_create(
128 bt_self_component_source_as_self_component(
129 dmesg_comp->self_comp));
862ca4ed
PP
130 if (!dmesg_comp->trace_class) {
131 BT_LOGE_STR("Cannot create an empty trace class object.");
33ec5dfa
PP
132 goto error;
133 }
134
40f4ba76 135 dmesg_comp->stream_class = bt_stream_class_create(
862ca4ed 136 dmesg_comp->trace_class);
33ec5dfa 137 if (!dmesg_comp->stream_class) {
44c440bc 138 BT_LOGE_STR("Cannot create a stream class object.");
33ec5dfa
PP
139 goto error;
140 }
141
33ec5dfa 142 if (has_ts) {
0f2d58c9 143 dmesg_comp->clock_class = bt_clock_class_create(
7fcdb0a9
FD
144 bt_self_component_source_as_self_component(
145 dmesg_comp->self_comp));
33ec5dfa
PP
146 if (!dmesg_comp->clock_class) {
147 BT_LOGE_STR("Cannot create clock class.");
148 goto error;
149 }
150
21029722 151 /*
5552377a
PP
152 * The `dmesg` timestamp's origin is not the Unix epoch,
153 * it's the boot time.
21029722 154 */
5552377a 155 bt_clock_class_set_origin_is_unix_epoch(dmesg_comp->clock_class,
21029722
PP
156 BT_FALSE);
157
40f4ba76
PP
158 ret = bt_stream_class_set_default_clock_class(
159 dmesg_comp->stream_class, dmesg_comp->clock_class);
33ec5dfa 160 if (ret) {
44c440bc 161 BT_LOGE_STR("Cannot set stream class's default clock class.");
33ec5dfa
PP
162 goto error;
163 }
649934d2 164
9b24b6aa 165 bt_stream_class_set_packets_have_beginning_default_clock_snapshot(
649934d2 166 dmesg_comp->stream_class, BT_TRUE);
9b24b6aa 167 bt_stream_class_set_packets_have_end_default_clock_snapshot(
649934d2 168 dmesg_comp->stream_class, BT_TRUE);
33ec5dfa
PP
169 }
170
40f4ba76 171 dmesg_comp->event_class = bt_event_class_create(
44c440bc 172 dmesg_comp->stream_class);
33ec5dfa 173 if (!dmesg_comp->event_class) {
44c440bc
PP
174 BT_LOGE_STR("Cannot create an event class object.");
175 goto error;
176 }
177
40f4ba76 178 ret = bt_event_class_set_name(dmesg_comp->event_class, "string");
44c440bc
PP
179 if (ret) {
180 BT_LOGE_STR("Cannot set event class's name.");
33ec5dfa
PP
181 goto error;
182 }
183
1122a43a 184 fc = create_event_payload_fc(dmesg_comp->trace_class);
5cd6d0e5
PP
185 if (!fc) {
186 BT_LOGE_STR("Cannot create event payload field class.");
d8866baa
PP
187 goto error;
188 }
189
40f4ba76 190 ret = bt_event_class_set_payload_field_class(dmesg_comp->event_class, fc);
33ec5dfa 191 if (ret) {
5cd6d0e5 192 BT_LOGE_STR("Cannot set event class's event payload field class.");
33ec5dfa
PP
193 goto error;
194 }
195
33ec5dfa
PP
196 goto end;
197
198error:
199 ret = -1;
200
201end:
c5b9b441 202 bt_field_class_put_ref(fc);
33ec5dfa
PP
203 return ret;
204}
205
206static
05e21286 207int handle_params(struct dmesg_component *dmesg_comp,
b19ff26f 208 const bt_value *params)
33ec5dfa 209{
b19ff26f
PP
210 const bt_value *no_timestamp = NULL;
211 const bt_value *path = NULL;
33ec5dfa
PP
212 const char *path_str;
213 int ret = 0;
214
05e21286 215 no_timestamp = bt_value_map_borrow_entry_value_const(params,
07208d85 216 "no-extract-timestamp");
707b323a
PP
217 if (no_timestamp) {
218 if (!bt_value_is_bool(no_timestamp)) {
219 BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
220 "type=%s",
da91b29a 221 bt_common_value_type_string(
707b323a
PP
222 bt_value_get_type(no_timestamp)));
223 goto error;
224 }
225
601b0d3c
PP
226 dmesg_comp->params.no_timestamp =
227 bt_value_bool_get(no_timestamp);
707b323a
PP
228 }
229
05e21286 230 path = bt_value_map_borrow_entry_value_const(params, "path");
d8866baa
PP
231 if (path) {
232 if (dmesg_comp->params.read_from_stdin) {
33ec5dfa 233 BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters.");
d8866baa
PP
234 goto error;
235 }
236
237 if (!bt_value_is_string(path)) {
33ec5dfa
PP
238 BT_LOGE("Expecting a string value for the `path` parameter: "
239 "type=%s",
da91b29a 240 bt_common_value_type_string(
33ec5dfa 241 bt_value_get_type(path)));
d8866baa
PP
242 goto error;
243 }
244
601b0d3c 245 path_str = bt_value_string_get(path);
d8866baa
PP
246 g_string_assign(dmesg_comp->params.path, path_str);
247 } else {
4da23e31 248 dmesg_comp->params.read_from_stdin = true;
d8866baa
PP
249 }
250
251 goto end;
252
253error:
254 ret = -1;
255
256end:
d8866baa
PP
257 return ret;
258}
259
33ec5dfa 260static
862ca4ed 261int create_packet_and_stream_and_trace(struct dmesg_component *dmesg_comp)
33ec5dfa
PP
262{
263 int ret = 0;
e54986cf 264 const char *trace_name = NULL;
862ca4ed
PP
265 gchar *basename = NULL;
266
267 dmesg_comp->trace = bt_trace_create(dmesg_comp->trace_class);
268 if (!dmesg_comp->trace) {
269 BT_LOGE_STR("Cannot create trace object.");
270 goto error;
271 }
33ec5dfa 272
862ca4ed
PP
273 if (dmesg_comp->params.read_from_stdin) {
274 trace_name = "STDIN";
275 } else {
276 basename = g_path_get_basename(dmesg_comp->params.path->str);
277 BT_ASSERT(basename);
278
279 if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 &&
280 strcmp(basename, ".") != 0) {
281 trace_name = basename;
282 }
283 }
284
285 if (trace_name) {
286 ret = bt_trace_set_name(dmesg_comp->trace, trace_name);
287 if (ret) {
288 BT_LOGE("Cannot set trace's name: name=\"%s\"",
289 trace_name);
290 goto error;
291 }
292 }
293
294 dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class,
295 dmesg_comp->trace);
33ec5dfa
PP
296 if (!dmesg_comp->stream) {
297 BT_LOGE_STR("Cannot create stream object.");
298 goto error;
299 }
300
40f4ba76 301 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream);
33ec5dfa
PP
302 if (!dmesg_comp->packet) {
303 BT_LOGE_STR("Cannot create packet object.");
304 goto error;
305 }
306
33ec5dfa
PP
307 goto end;
308
309error:
310 ret = -1;
311
312end:
862ca4ed
PP
313 if (basename) {
314 g_free(basename);
315 }
316
33ec5dfa
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
862ca4ed 338 ret = create_packet_and_stream_and_trace(dmesg_comp);
33ec5dfa
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
c5b9b441
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);
8b5bd70c 371 bt_trace_class_put_ref(dmesg_comp->trace_class);
d8866baa
PP
372 g_free(dmesg_comp);
373}
374
33ec5dfa 375static
4cdfc5e8 376bt_self_component_status create_port(
b19ff26f 377 bt_self_component_source *self_comp)
33ec5dfa 378{
d94d92ac 379 return bt_self_component_source_add_output_port(self_comp,
33ec5dfa
PP
380 "out", NULL, NULL);
381}
382
d8866baa 383BT_HIDDEN
4cdfc5e8 384bt_self_component_status dmesg_init(
b19ff26f
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);
4cdfc5e8 390 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
d8866baa
PP
391
392 if (!dmesg_comp) {
33ec5dfa 393 BT_LOGE_STR("Failed to allocate one dmesg component structure.");
d8866baa
PP
394 goto error;
395 }
396
41693723 397 dmesg_comp->self_comp = self_comp;
d8866baa
PP
398 dmesg_comp->params.path = g_string_new(NULL);
399 if (!dmesg_comp->params.path) {
33ec5dfa 400 BT_LOGE_STR("Failed to allocate a GString.");
d8866baa
PP
401 goto error;
402 }
403
33ec5dfa 404 ret = handle_params(dmesg_comp, params);
d8866baa 405 if (ret) {
d94d92ac 406 BT_LOGE("Invalid parameters: comp-addr=%p", self_comp);
d8866baa
PP
407 goto error;
408 }
409
33ec5dfa
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: "
d94d92ac 414 "comp-addr=%p, path=\"%s\"", self_comp,
33ec5dfa
PP
415 dmesg_comp->params.path->str);
416 goto error;
417 }
d8866baa 418
d94d92ac
PP
419 status = create_port(self_comp);
420 if (status != BT_SELF_COMPONENT_STATUS_OK) {
33ec5dfa
PP
421 goto error;
422 }
d8866baa 423
d94d92ac 424 bt_self_component_set_data(
707b7d35 425 bt_self_component_source_as_self_component(self_comp),
d94d92ac 426 dmesg_comp);
d8866baa
PP
427 goto end;
428
429error:
430 destroy_dmesg_component(dmesg_comp);
d94d92ac 431 bt_self_component_set_data(
707b7d35 432 bt_self_component_source_as_self_component(self_comp),
d94d92ac 433 NULL);
33ec5dfa
PP
434
435 if (status >= 0) {
d94d92ac 436 status = BT_SELF_COMPONENT_STATUS_ERROR;
33ec5dfa 437 }
d8866baa
PP
438
439end:
440 return status;
441}
442
443BT_HIDDEN
b19ff26f 444void dmesg_finalize(bt_self_component_source *self_comp)
d8866baa 445{
d94d92ac 446 destroy_dmesg_component(bt_self_component_get_data(
707b7d35 447 bt_self_component_source_as_self_component(self_comp)));
33ec5dfa
PP
448}
449
450static
d6e69534
PP
451bt_message *create_init_event_msg_from_line(
452 struct dmesg_msg_iter *msg_iter,
312c056a 453 const char *line, const char **new_start)
33ec5dfa 454{
b19ff26f 455 bt_event *event;
d6e69534 456 bt_message *msg = NULL;
33ec5dfa
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;
33ec5dfa 461 int ret = 0;
d6e69534 462 struct dmesg_component *dmesg_comp = msg_iter->dmesg_comp;
33ec5dfa 463
33ec5dfa
PP
464 *new_start = line;
465
707b323a
PP
466 if (dmesg_comp->params.no_timestamp) {
467 goto skip_ts;
468 }
469
33ec5dfa
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, ']');
f6ccaed9 506 BT_ASSERT(*new_start);
33ec5dfa
PP
507 (*new_start)++;
508
509 if ((*new_start)[0] == ' ') {
510 (*new_start)++;
511 }
512 }
513
707b323a 514skip_ts:
33ec5dfa
PP
515 /*
516 * At this point, we know if the stream class's event header
5cd6d0e5 517 * field class should have a timestamp or not, so we can lazily
33ec5dfa
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
2c091c04
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
d6e69534
PP
536 if (!msg) {
537 BT_LOGE_STR("Cannot create event message.");
312c056a
PP
538 goto error;
539 }
33ec5dfa 540
d6e69534 541 event = bt_message_event_borrow_event(msg);
312c056a 542 BT_ASSERT(event);
33ec5dfa
PP
543 goto end;
544
545error:
d6e69534 546 BT_MESSAGE_PUT_REF_AND_RESET(msg);
33ec5dfa
PP
547
548end:
d6e69534 549 return msg;
33ec5dfa
PP
550}
551
552static
e5be10ef 553int fill_event_payload_from_line(const char *line,
b19ff26f 554 bt_event *event)
33ec5dfa 555{
b19ff26f
PP
556 bt_field *ep_field = NULL;
557 bt_field *str_field = NULL;
33ec5dfa
PP
558 size_t len;
559 int ret;
560
40f4ba76 561 ep_field = bt_event_borrow_payload_field(event);
312c056a 562 BT_ASSERT(ep_field);
40f4ba76 563 str_field = bt_field_structure_borrow_member_field_by_index(
e5be10ef 564 ep_field, 0);
33ec5dfa 565 if (!str_field) {
312c056a 566 BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
33ec5dfa
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
40f4ba76 576 ret = bt_field_string_clear(str_field);
312c056a
PP
577 if (ret) {
578 BT_LOGE_STR("Cannot clear string field object.");
579 goto error;
580 }
581
40f4ba76 582 ret = bt_field_string_append_with_length(str_field, line, len);
33ec5dfa
PP
583 if (ret) {
584 BT_LOGE("Cannot append value to string field object: "
585 "len=%zu", len);
586 goto error;
587 }
588
33ec5dfa
PP
589 goto end;
590
591error:
592 ret = -1;
593
594end:
33ec5dfa
PP
595 return ret;
596}
597
598static
d6e69534
PP
599bt_message *create_msg_from_line(
600 struct dmesg_msg_iter *dmesg_msg_iter, const char *line)
33ec5dfa 601{
b19ff26f 602 bt_event *event = NULL;
d6e69534 603 bt_message *msg = NULL;
33ec5dfa
PP
604 const char *new_start;
605 int ret;
606
d6e69534 607 msg = create_init_event_msg_from_line(dmesg_msg_iter,
f0010051 608 line, &new_start);
d6e69534
PP
609 if (!msg) {
610 BT_LOGE_STR("Cannot create and initialize event message from line.");
33ec5dfa
PP
611 goto error;
612 }
613
d6e69534 614 event = bt_message_event_borrow_event(msg);
312c056a 615 BT_ASSERT(event);
f0010051 616 ret = fill_event_payload_from_line(new_start, event);
33ec5dfa 617 if (ret) {
312c056a 618 BT_LOGE("Cannot fill event payload field from line: "
33ec5dfa
PP
619 "ret=%d", ret);
620 goto error;
621 }
622
33ec5dfa
PP
623 goto end;
624
625error:
d6e69534 626 BT_MESSAGE_PUT_REF_AND_RESET(msg);
33ec5dfa
PP
627
628end:
d6e69534 629 return msg;
33ec5dfa
PP
630}
631
632static
d6e69534 633void destroy_dmesg_msg_iter(struct dmesg_msg_iter *dmesg_msg_iter)
33ec5dfa 634{
d6e69534 635 if (!dmesg_msg_iter) {
33ec5dfa
PP
636 return;
637 }
638
d6e69534
PP
639 if (dmesg_msg_iter->fp && dmesg_msg_iter->fp != stdin) {
640 if (fclose(dmesg_msg_iter->fp)) {
33ec5dfa
PP
641 BT_LOGE_ERRNO("Cannot close input file", ".");
642 }
643 }
d8866baa 644
d6e69534
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
2d6bab5c
PP
650
651
d8866baa 652BT_HIDDEN
4cdfc5e8 653bt_self_message_iterator_status dmesg_msg_iter_init(
d6e69534 654 bt_self_message_iterator *self_msg_iter,
b19ff26f
PP
655 bt_self_component_source *self_comp,
656 bt_self_component_port_output *self_port)
d8866baa 657{
33ec5dfa 658 struct dmesg_component *dmesg_comp;
d6e69534
PP
659 struct dmesg_msg_iter *dmesg_msg_iter =
660 g_new0(struct dmesg_msg_iter, 1);
4cdfc5e8 661 bt_self_message_iterator_status status =
d6e69534 662 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
33ec5dfa 663
d6e69534
PP
664 if (!dmesg_msg_iter) {
665 BT_LOGE_STR("Failed to allocate on dmesg message iterator structure.");
33ec5dfa
PP
666 goto error;
667 }
668
d94d92ac 669 dmesg_comp = bt_self_component_get_data(
707b7d35 670 bt_self_component_source_as_self_component(self_comp));
f6ccaed9 671 BT_ASSERT(dmesg_comp);
d6e69534
PP
672 dmesg_msg_iter->dmesg_comp = dmesg_comp;
673 dmesg_msg_iter->pc_msg_iter = self_msg_iter;
d8866baa 674
33ec5dfa 675 if (dmesg_comp->params.read_from_stdin) {
d6e69534 676 dmesg_msg_iter->fp = stdin;
33ec5dfa 677 } else {
d6e69534
PP
678 dmesg_msg_iter->fp = fopen(dmesg_comp->params.path->str, "r");
679 if (!dmesg_msg_iter->fp) {
33ec5dfa
PP
680 BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
681 dmesg_comp->params.path->str);
682 goto error;
683 }
684 }
685
d6e69534
PP
686 bt_self_message_iterator_set_data(self_msg_iter,
687 dmesg_msg_iter);
33ec5dfa
PP
688 goto end;
689
690error:
d6e69534
PP
691 destroy_dmesg_msg_iter(dmesg_msg_iter);
692 bt_self_message_iterator_set_data(self_msg_iter, NULL);
33ec5dfa 693 if (status >= 0) {
d6e69534 694 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
33ec5dfa
PP
695 }
696
697end:
33ec5dfa 698 return status;
d8866baa
PP
699}
700
701BT_HIDDEN
d6e69534
PP
702void dmesg_msg_iter_finalize(
703 bt_self_message_iterator *priv_msg_iter)
d8866baa 704{
d6e69534
PP
705 destroy_dmesg_msg_iter(bt_self_message_iterator_get_data(
706 priv_msg_iter));
d8866baa
PP
707}
708
d4393e08 709static
4cdfc5e8 710bt_self_message_iterator_status dmesg_msg_iter_next_one(
d6e69534
PP
711 struct dmesg_msg_iter *dmesg_msg_iter,
712 bt_message **msg)
d8866baa 713{
33ec5dfa 714 ssize_t len;
33ec5dfa 715 struct dmesg_component *dmesg_comp;
4cdfc5e8 716 bt_self_message_iterator_status status =
d6e69534 717 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
d8866baa 718
d6e69534
PP
719 BT_ASSERT(dmesg_msg_iter);
720 dmesg_comp = dmesg_msg_iter->dmesg_comp;
f6ccaed9 721 BT_ASSERT(dmesg_comp);
33ec5dfa 722
d6e69534
PP
723 if (dmesg_msg_iter->state == STATE_DONE) {
724 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
312c056a
PP
725 goto end;
726 }
727
d6e69534
PP
728 if (dmesg_msg_iter->tmp_event_msg ||
729 dmesg_msg_iter->state == STATE_EMIT_PACKET_END ||
a59806b7 730 dmesg_msg_iter->state == STATE_EMIT_STREAM_ACTIVITY_END ||
d6e69534 731 dmesg_msg_iter->state == STATE_EMIT_STREAM_END) {
312c056a
PP
732 goto handle_state;
733 }
734
33ec5dfa
PP
735 while (true) {
736 const char *ch;
737 bool only_spaces = true;
738
d6e69534
PP
739 len = bt_getline(&dmesg_msg_iter->linebuf,
740 &dmesg_msg_iter->linebuf_len, dmesg_msg_iter->fp);
33ec5dfa
PP
741 if (len < 0) {
742 if (errno == EINVAL) {
d6e69534 743 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
33ec5dfa 744 } else if (errno == ENOMEM) {
d4393e08 745 status =
d6e69534 746 BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM;
33ec5dfa 747 } else {
d6e69534 748 if (dmesg_msg_iter->state == STATE_EMIT_STREAM_BEGINNING) {
312c056a 749 /* Stream did not even begin */
d6e69534 750 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
312c056a
PP
751 goto end;
752 } else {
753 /* End current packet now */
d6e69534 754 dmesg_msg_iter->state =
312c056a
PP
755 STATE_EMIT_PACKET_END;
756 goto handle_state;
757 }
33ec5dfa
PP
758 }
759
760 goto end;
761 }
762
d6e69534 763 BT_ASSERT(dmesg_msg_iter->linebuf);
33ec5dfa
PP
764
765 /* Ignore empty lines, once trimmed */
d6e69534 766 for (ch = dmesg_msg_iter->linebuf; *ch != '\0'; ch++) {
33ec5dfa
PP
767 if (!isspace(*ch)) {
768 only_spaces = false;
769 break;
770 }
771 }
772
773 if (!only_spaces) {
774 break;
775 }
776 }
777
d6e69534
PP
778 dmesg_msg_iter->tmp_event_msg = create_msg_from_line(
779 dmesg_msg_iter, dmesg_msg_iter->linebuf);
780 if (!dmesg_msg_iter->tmp_event_msg) {
781 BT_LOGE("Cannot create event message from line: "
33ec5dfa 782 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
d6e69534 783 dmesg_msg_iter->linebuf);
312c056a
PP
784 goto end;
785 }
786
787handle_state:
788 BT_ASSERT(dmesg_comp->trace);
789
d6e69534 790 switch (dmesg_msg_iter->state) {
312c056a 791 case STATE_EMIT_STREAM_BEGINNING:
d6e69534
PP
792 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
793 *msg = bt_message_stream_beginning_create(
794 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
a59806b7
PP
795 dmesg_msg_iter->state = STATE_EMIT_STREAM_ACTIVITY_BEGINNING;
796 break;
797 case STATE_EMIT_STREAM_ACTIVITY_BEGINNING:
798 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
799 *msg = bt_message_stream_activity_beginning_create(
800 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
d6e69534 801 dmesg_msg_iter->state = STATE_EMIT_PACKET_BEGINNING;
312c056a
PP
802 break;
803 case STATE_EMIT_PACKET_BEGINNING:
d6e69534 804 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
a6d85d2f
PP
805
806 if (dmesg_comp->clock_class) {
807 *msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
808 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet,
809 dmesg_msg_iter->last_clock_value);
810 } else {
811 *msg = bt_message_packet_beginning_create(
812 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet);
813 }
814
d6e69534 815 dmesg_msg_iter->state = STATE_EMIT_EVENT;
312c056a
PP
816 break;
817 case STATE_EMIT_EVENT:
d6e69534
PP
818 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
819 *msg = dmesg_msg_iter->tmp_event_msg;
820 dmesg_msg_iter->tmp_event_msg = NULL;
312c056a
PP
821 break;
822 case STATE_EMIT_PACKET_END:
a6d85d2f
PP
823 if (dmesg_comp->clock_class) {
824 *msg = bt_message_packet_end_create_with_default_clock_snapshot(
825 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet,
826 dmesg_msg_iter->last_clock_value);
827 } else {
828 *msg = bt_message_packet_end_create(
829 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet);
830 }
831
a59806b7
PP
832 dmesg_msg_iter->state = STATE_EMIT_STREAM_ACTIVITY_END;
833 break;
834 case STATE_EMIT_STREAM_ACTIVITY_END:
835 *msg = bt_message_stream_activity_end_create(
836 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
d6e69534 837 dmesg_msg_iter->state = STATE_EMIT_STREAM_END;
312c056a
PP
838 break;
839 case STATE_EMIT_STREAM_END:
d6e69534
PP
840 *msg = bt_message_stream_end_create(
841 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
842 dmesg_msg_iter->state = STATE_DONE;
312c056a
PP
843 break;
844 default:
845 break;
846 }
847
d6e69534
PP
848 if (!*msg) {
849 BT_LOGE("Cannot create message: dmesg-comp-addr=%p",
312c056a 850 dmesg_comp);
d6e69534 851 status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
33ec5dfa
PP
852 }
853
854end:
d4393e08
PP
855 return status;
856}
857
858BT_HIDDEN
4cdfc5e8 859bt_self_message_iterator_status dmesg_msg_iter_next(
d6e69534
PP
860 bt_self_message_iterator *self_msg_iter,
861 bt_message_array_const msgs, uint64_t capacity,
d4393e08
PP
862 uint64_t *count)
863{
d6e69534
PP
864 struct dmesg_msg_iter *dmesg_msg_iter =
865 bt_self_message_iterator_get_data(
866 self_msg_iter);
4cdfc5e8 867 bt_self_message_iterator_status status =
d6e69534 868 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
d4393e08
PP
869 uint64_t i = 0;
870
d94d92ac 871 while (i < capacity &&
d6e69534
PP
872 status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
873 bt_message *priv_msg = NULL;
e5be10ef 874
d6e69534
PP
875 status = dmesg_msg_iter_next_one(dmesg_msg_iter,
876 &priv_msg);
877 msgs[i] = priv_msg;
878 if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) {
d4393e08
PP
879 i++;
880 }
881 }
882
883 if (i > 0) {
884 /*
d6e69534 885 * Even if dmesg_msg_iter_next_one() returned
d4393e08 886 * something else than
d6e69534
PP
887 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we
888 * accumulated message objects in the output
889 * message array, so we need to return
890 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they
d94d92ac 891 * are transfered to downstream. This other status
d6e69534 892 * occurs again the next time muxer_msg_iter_do_next()
d94d92ac 893 * is called, possibly without any accumulated
d6e69534 894 * message, in which case we'll return it.
d4393e08
PP
895 */
896 *count = i;
d6e69534 897 status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
d4393e08
PP
898 }
899
900 return status;
d8866baa 901}
2d6bab5c
PP
902
903BT_HIDDEN
904bt_bool dmesg_msg_iter_can_seek_beginning(
905 bt_self_message_iterator *self_msg_iter)
906{
907 struct dmesg_msg_iter *dmesg_msg_iter =
908 bt_self_message_iterator_get_data(self_msg_iter);
909
910 /* Can't seek the beginning of the standard input stream */
911 return !dmesg_msg_iter->dmesg_comp->params.read_from_stdin;
912}
913
914BT_HIDDEN
915bt_self_message_iterator_status dmesg_msg_iter_seek_beginning(
916 bt_self_message_iterator *self_msg_iter)
917{
918 struct dmesg_msg_iter *dmesg_msg_iter =
919 bt_self_message_iterator_get_data(self_msg_iter);
920
921 BT_ASSERT(!dmesg_msg_iter->dmesg_comp->params.read_from_stdin);
922
923 BT_MESSAGE_PUT_REF_AND_RESET(dmesg_msg_iter->tmp_event_msg);
924 dmesg_msg_iter->last_clock_value = 0;
925 dmesg_msg_iter->state = STATE_EMIT_STREAM_BEGINNING;
926 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
927}
This page took 0.109454 seconds and 4 git commands to generate.