lib: remove stream activity messages
[babeltrace.git] / src / 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
9c8a5044 24#define BT_COMP_LOG_SELF_COMP (dmesg_comp->self_comp)
caedbbee 25#define BT_LOG_OUTPUT_LEVEL (dmesg_comp->log_level)
350ad6c1 26#define BT_LOG_TAG "PLUGIN/SRC.TEXT.DMESG"
9c8a5044 27#include "plugins/comp-logging.h"
33ec5dfa 28
d8866baa 29#include <stdbool.h>
33ec5dfa
PP
30#include <string.h>
31#include <ctype.h>
d8866baa 32#include <stdio.h>
578e048b 33#include "common/common.h"
caedbbee 34#include "common/assert.h"
3fadfbc0 35#include <babeltrace2/babeltrace.h>
578e048b
MJ
36#include "compat/utc.h"
37#include "compat/stdio.h"
d8866baa
PP
38#include <glib.h>
39
33ec5dfa
PP
40#define NSEC_PER_USEC 1000UL
41#define NSEC_PER_MSEC 1000000UL
42#define NSEC_PER_SEC 1000000000ULL
43#define USEC_PER_SEC 1000000UL
44
d8866baa
PP
45struct dmesg_component;
46
d6e69534 47struct dmesg_msg_iter {
d8866baa 48 struct dmesg_component *dmesg_comp;
d6e69534 49 bt_self_message_iterator *pc_msg_iter; /* Weak */
33ec5dfa
PP
50 char *linebuf;
51 size_t linebuf_len;
d8866baa 52 FILE *fp;
d6e69534 53 bt_message *tmp_event_msg;
a6d85d2f 54 uint64_t last_clock_value;
312c056a
PP
55
56 enum {
57 STATE_EMIT_STREAM_BEGINNING,
58 STATE_EMIT_PACKET_BEGINNING,
59 STATE_EMIT_EVENT,
60 STATE_EMIT_PACKET_END,
61 STATE_EMIT_STREAM_END,
62 STATE_DONE,
63 } state;
d8866baa
PP
64};
65
66struct dmesg_component {
caedbbee
PP
67 bt_logging_level log_level;
68
d8866baa
PP
69 struct {
70 GString *path;
33ec5dfa 71 bt_bool read_from_stdin;
707b323a 72 bt_bool no_timestamp;
d8866baa
PP
73 } params;
74
9c8a5044
PP
75 bt_self_component_source *self_comp_src;
76 bt_self_component *self_comp;
b19ff26f
PP
77 bt_trace_class *trace_class;
78 bt_stream_class *stream_class;
79 bt_event_class *event_class;
80 bt_trace *trace;
81 bt_stream *stream;
82 bt_packet *packet;
83 bt_clock_class *clock_class;
d8866baa
PP
84};
85
33ec5dfa 86static
caedbbee
PP
87bt_field_class *create_event_payload_fc(struct dmesg_component *dmesg_comp,
88 bt_trace_class *trace_class)
33ec5dfa 89{
b19ff26f
PP
90 bt_field_class *root_fc = NULL;
91 bt_field_class *fc = NULL;
33ec5dfa
PP
92 int ret;
93
1122a43a 94 root_fc = bt_field_class_structure_create(trace_class);
5cd6d0e5 95 if (!root_fc) {
9c8a5044 96 BT_COMP_LOGE_STR("Cannot create an empty structure field class object.");
33ec5dfa
PP
97 goto error;
98 }
99
1122a43a 100 fc = bt_field_class_string_create(trace_class);
5cd6d0e5 101 if (!fc) {
9c8a5044 102 BT_COMP_LOGE_STR("Cannot create a string field class object.");
33ec5dfa
PP
103 goto error;
104 }
105
40f4ba76 106 ret = bt_field_class_structure_append_member(root_fc,
e5be10ef 107 "str", fc);
33ec5dfa 108 if (ret) {
9c8a5044 109 BT_COMP_LOGE("Cannot add `str` member to structure field class: "
33ec5dfa
PP
110 "ret=%d", ret);
111 goto error;
112 }
113
114 goto end;
115
116error:
c5b9b441 117 BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc);
33ec5dfa
PP
118
119end:
c5b9b441 120 bt_field_class_put_ref(fc);
5cd6d0e5 121 return root_fc;
33ec5dfa
PP
122}
123
33ec5dfa
PP
124static
125int create_meta(struct dmesg_component *dmesg_comp, bool has_ts)
126{
b19ff26f 127 bt_field_class *fc = NULL;
d8866baa
PP
128 int ret = 0;
129
9c8a5044 130 dmesg_comp->trace_class = bt_trace_class_create(dmesg_comp->self_comp);
862ca4ed 131 if (!dmesg_comp->trace_class) {
9c8a5044 132 BT_COMP_LOGE_STR("Cannot create an empty trace class object.");
33ec5dfa
PP
133 goto error;
134 }
135
40f4ba76 136 dmesg_comp->stream_class = bt_stream_class_create(
862ca4ed 137 dmesg_comp->trace_class);
33ec5dfa 138 if (!dmesg_comp->stream_class) {
9c8a5044 139 BT_COMP_LOGE_STR("Cannot create a stream class object.");
33ec5dfa
PP
140 goto error;
141 }
142
33ec5dfa 143 if (has_ts) {
0f2d58c9 144 dmesg_comp->clock_class = bt_clock_class_create(
9c8a5044 145 dmesg_comp->self_comp);
33ec5dfa 146 if (!dmesg_comp->clock_class) {
9c8a5044 147 BT_COMP_LOGE_STR("Cannot create clock class.");
33ec5dfa
PP
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) {
9c8a5044 161 BT_COMP_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) {
9c8a5044 174 BT_COMP_LOGE_STR("Cannot create an event class object.");
44c440bc
PP
175 goto error;
176 }
177
40f4ba76 178 ret = bt_event_class_set_name(dmesg_comp->event_class, "string");
44c440bc 179 if (ret) {
9c8a5044 180 BT_COMP_LOGE_STR("Cannot set event class's name.");
33ec5dfa
PP
181 goto error;
182 }
183
caedbbee 184 fc = create_event_payload_fc(dmesg_comp, dmesg_comp->trace_class);
5cd6d0e5 185 if (!fc) {
9c8a5044 186 BT_COMP_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) {
9c8a5044 192 BT_COMP_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)) {
9c8a5044 219 BT_COMP_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: "
707b323a 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) {
9c8a5044 233 BT_COMP_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)) {
9c8a5044 238 BT_COMP_LOGE("Expecting a string value for the `path` parameter: "
33ec5dfa 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) {
9c8a5044 269 BT_COMP_LOGE_STR("Cannot create trace object.");
862ca4ed
PP
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) {
9c8a5044 288 BT_COMP_LOGE("Cannot set trace's name: name=\"%s\"",
862ca4ed
PP
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 296 if (!dmesg_comp->stream) {
9c8a5044 297 BT_COMP_LOGE_STR("Cannot create stream object.");
33ec5dfa
PP
298 goto error;
299 }
300
40f4ba76 301 dmesg_comp->packet = bt_packet_create(dmesg_comp->stream);
33ec5dfa 302 if (!dmesg_comp->packet) {
9c8a5044 303 BT_COMP_LOGE_STR("Cannot create packet object.");
33ec5dfa
PP
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) {
9c8a5044 333 BT_COMP_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p",
33ec5dfa
PP
334 dmesg_comp);
335 goto error;
336 }
337
862ca4ed 338 ret = create_packet_and_stream_and_trace(dmesg_comp);
33ec5dfa 339 if (ret) {
9c8a5044 340 BT_COMP_LOGE("Cannot create packet and stream objects: "
33ec5dfa
PP
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
d24d5663 376bt_component_class_init_method_status create_port(
b19ff26f 377 bt_self_component_source *self_comp)
33ec5dfa 378{
d24d5663
PP
379 bt_component_class_init_method_status status;
380 bt_self_component_add_port_status add_port_status;
381
382 add_port_status = bt_self_component_source_add_output_port(self_comp,
33ec5dfa 383 "out", NULL, NULL);
d24d5663
PP
384 switch (add_port_status) {
385 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
386 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
387 break;
388 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
389 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
390 break;
391 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
392 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
393 break;
394 default:
395 abort();
396 }
397
398 return status;
33ec5dfa
PP
399}
400
d8866baa 401BT_HIDDEN
d24d5663 402bt_component_class_init_method_status dmesg_init(
9c8a5044 403 bt_self_component_source *self_comp_src,
b19ff26f 404 bt_value *params, void *init_method_data)
d8866baa
PP
405{
406 int ret = 0;
407 struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1);
d24d5663
PP
408 bt_component_class_init_method_status status =
409 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
9c8a5044
PP
410 bt_self_component *self_comp =
411 bt_self_component_source_as_self_component(self_comp_src);
412 const bt_component *comp = bt_self_component_as_component(self_comp);
caedbbee 413 bt_logging_level log_level = bt_component_get_logging_level(comp);
d8866baa
PP
414
415 if (!dmesg_comp) {
caedbbee 416 /* Implicit log level is not available here */
9c8a5044 417 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
caedbbee 418 "Failed to allocate one dmesg component structure.");
d8866baa
PP
419 goto error;
420 }
421
caedbbee 422 dmesg_comp->log_level = log_level;
41693723 423 dmesg_comp->self_comp = self_comp;
9c8a5044 424 dmesg_comp->self_comp_src = self_comp_src;
d8866baa
PP
425 dmesg_comp->params.path = g_string_new(NULL);
426 if (!dmesg_comp->params.path) {
9c8a5044 427 BT_COMP_LOGE_STR("Failed to allocate a GString.");
d8866baa
PP
428 goto error;
429 }
430
33ec5dfa 431 ret = handle_params(dmesg_comp, params);
d8866baa 432 if (ret) {
9c8a5044 433 BT_COMP_LOGE("Invalid parameters: comp-addr=%p", self_comp);
d8866baa
PP
434 goto error;
435 }
436
33ec5dfa
PP
437 if (!dmesg_comp->params.read_from_stdin &&
438 !g_file_test(dmesg_comp->params.path->str,
439 G_FILE_TEST_IS_REGULAR)) {
9c8a5044 440 BT_COMP_LOGE("Input path is not a regular file: "
d94d92ac 441 "comp-addr=%p, path=\"%s\"", self_comp,
33ec5dfa
PP
442 dmesg_comp->params.path->str);
443 goto error;
444 }
d8866baa 445
9c8a5044 446 status = create_port(self_comp_src);
d24d5663 447 if (status != BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
33ec5dfa
PP
448 goto error;
449 }
d8866baa 450
9c8a5044
PP
451 bt_self_component_set_data(self_comp, dmesg_comp);
452 BT_COMP_LOGI_STR("Component initialized.");
d8866baa
PP
453 goto end;
454
455error:
456 destroy_dmesg_component(dmesg_comp);
9c8a5044 457 bt_self_component_set_data(self_comp, NULL);
33ec5dfa
PP
458
459 if (status >= 0) {
d24d5663 460 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
33ec5dfa 461 }
d8866baa
PP
462
463end:
464 return status;
465}
466
467BT_HIDDEN
b19ff26f 468void dmesg_finalize(bt_self_component_source *self_comp)
d8866baa 469{
d94d92ac 470 destroy_dmesg_component(bt_self_component_get_data(
707b7d35 471 bt_self_component_source_as_self_component(self_comp)));
33ec5dfa
PP
472}
473
474static
d6e69534
PP
475bt_message *create_init_event_msg_from_line(
476 struct dmesg_msg_iter *msg_iter,
312c056a 477 const char *line, const char **new_start)
33ec5dfa 478{
b19ff26f 479 bt_event *event;
d6e69534 480 bt_message *msg = NULL;
33ec5dfa
PP
481 bool has_timestamp = false;
482 unsigned long sec, usec, msec;
483 unsigned int year, mon, mday, hour, min;
484 uint64_t ts = 0;
33ec5dfa 485 int ret = 0;
d6e69534 486 struct dmesg_component *dmesg_comp = msg_iter->dmesg_comp;
33ec5dfa 487
33ec5dfa
PP
488 *new_start = line;
489
707b323a
PP
490 if (dmesg_comp->params.no_timestamp) {
491 goto skip_ts;
492 }
493
33ec5dfa
PP
494 /* Extract time from input line */
495 if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) {
496 ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec;
497
498 /*
499 * The clock class we use has a 1 GHz frequency: convert
500 * from µs to ns.
501 */
502 ts *= NSEC_PER_USEC;
503 has_timestamp = true;
504 } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ",
505 &year, &mon, &mday, &hour, &min,
506 &sec, &msec) == 7) {
507 time_t ep_sec;
508 struct tm ti;
509
510 memset(&ti, 0, sizeof(ti));
511 ti.tm_year = year - 1900; /* From 1900 */
512 ti.tm_mon = mon - 1; /* 0 to 11 */
513 ti.tm_mday = mday;
514 ti.tm_hour = hour;
515 ti.tm_min = min;
516 ti.tm_sec = sec;
517
518 ep_sec = bt_timegm(&ti);
519 if (ep_sec != (time_t) -1) {
520 ts = (uint64_t) ep_sec * NSEC_PER_SEC
521 + (uint64_t) msec * NSEC_PER_MSEC;
522 }
523
524 has_timestamp = true;
525 }
526
527 if (has_timestamp) {
528 /* Set new start for the message portion of the line */
529 *new_start = strchr(line, ']');
f6ccaed9 530 BT_ASSERT(*new_start);
33ec5dfa
PP
531 (*new_start)++;
532
533 if ((*new_start)[0] == ' ') {
534 (*new_start)++;
535 }
536 }
537
707b323a 538skip_ts:
33ec5dfa
PP
539 /*
540 * At this point, we know if the stream class's event header
5cd6d0e5 541 * field class should have a timestamp or not, so we can lazily
33ec5dfa
PP
542 * create the metadata, stream, and packet objects.
543 */
544 ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp);
545 if (ret) {
546 /* try_create_meta_stream_packet() logs errors */
547 goto error;
548 }
549
2c091c04
PP
550 if (dmesg_comp->clock_class) {
551 msg = bt_message_event_create_with_default_clock_snapshot(
552 msg_iter->pc_msg_iter,
553 dmesg_comp->event_class, dmesg_comp->packet, ts);
554 msg_iter->last_clock_value = ts;
555 } else {
556 msg = bt_message_event_create(msg_iter->pc_msg_iter,
557 dmesg_comp->event_class, dmesg_comp->packet);
558 }
559
d6e69534 560 if (!msg) {
9c8a5044 561 BT_COMP_LOGE_STR("Cannot create event message.");
312c056a
PP
562 goto error;
563 }
33ec5dfa 564
d6e69534 565 event = bt_message_event_borrow_event(msg);
312c056a 566 BT_ASSERT(event);
33ec5dfa
PP
567 goto end;
568
569error:
d6e69534 570 BT_MESSAGE_PUT_REF_AND_RESET(msg);
33ec5dfa
PP
571
572end:
d6e69534 573 return msg;
33ec5dfa
PP
574}
575
576static
caedbbee
PP
577int fill_event_payload_from_line(struct dmesg_component *dmesg_comp,
578 const char *line, bt_event *event)
33ec5dfa 579{
b19ff26f
PP
580 bt_field *ep_field = NULL;
581 bt_field *str_field = NULL;
33ec5dfa
PP
582 size_t len;
583 int ret;
584
40f4ba76 585 ep_field = bt_event_borrow_payload_field(event);
312c056a 586 BT_ASSERT(ep_field);
40f4ba76 587 str_field = bt_field_structure_borrow_member_field_by_index(
e5be10ef 588 ep_field, 0);
33ec5dfa 589 if (!str_field) {
9c8a5044 590 BT_COMP_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field.");
33ec5dfa
PP
591 goto error;
592 }
593
594 len = strlen(line);
595 if (line[len - 1] == '\n') {
596 /* Do not include the newline character in the payload */
597 len--;
598 }
599
d24d5663 600 bt_field_string_clear(str_field);
40f4ba76 601 ret = bt_field_string_append_with_length(str_field, line, len);
33ec5dfa 602 if (ret) {
9c8a5044 603 BT_COMP_LOGE("Cannot append value to string field object: "
33ec5dfa
PP
604 "len=%zu", len);
605 goto error;
606 }
607
33ec5dfa
PP
608 goto end;
609
610error:
611 ret = -1;
612
613end:
33ec5dfa
PP
614 return ret;
615}
616
617static
d6e69534
PP
618bt_message *create_msg_from_line(
619 struct dmesg_msg_iter *dmesg_msg_iter, const char *line)
33ec5dfa 620{
caedbbee 621 struct dmesg_component *dmesg_comp = dmesg_msg_iter->dmesg_comp;
b19ff26f 622 bt_event *event = NULL;
d6e69534 623 bt_message *msg = NULL;
33ec5dfa
PP
624 const char *new_start;
625 int ret;
626
d6e69534 627 msg = create_init_event_msg_from_line(dmesg_msg_iter,
f0010051 628 line, &new_start);
d6e69534 629 if (!msg) {
9c8a5044 630 BT_COMP_LOGE_STR("Cannot create and initialize event message from line.");
33ec5dfa
PP
631 goto error;
632 }
633
d6e69534 634 event = bt_message_event_borrow_event(msg);
312c056a 635 BT_ASSERT(event);
caedbbee 636 ret = fill_event_payload_from_line(dmesg_comp, new_start, event);
33ec5dfa 637 if (ret) {
9c8a5044 638 BT_COMP_LOGE("Cannot fill event payload field from line: "
33ec5dfa
PP
639 "ret=%d", ret);
640 goto error;
641 }
642
33ec5dfa
PP
643 goto end;
644
645error:
d6e69534 646 BT_MESSAGE_PUT_REF_AND_RESET(msg);
33ec5dfa
PP
647
648end:
d6e69534 649 return msg;
33ec5dfa
PP
650}
651
652static
d6e69534 653void destroy_dmesg_msg_iter(struct dmesg_msg_iter *dmesg_msg_iter)
33ec5dfa 654{
caedbbee
PP
655 struct dmesg_component *dmesg_comp = dmesg_msg_iter->dmesg_comp;
656
d6e69534 657 if (!dmesg_msg_iter) {
33ec5dfa
PP
658 return;
659 }
660
d6e69534
PP
661 if (dmesg_msg_iter->fp && dmesg_msg_iter->fp != stdin) {
662 if (fclose(dmesg_msg_iter->fp)) {
9c8a5044 663 BT_COMP_LOGE_ERRNO("Cannot close input file", ".");
33ec5dfa
PP
664 }
665 }
d8866baa 666
d6e69534
PP
667 bt_message_put_ref(dmesg_msg_iter->tmp_event_msg);
668 free(dmesg_msg_iter->linebuf);
669 g_free(dmesg_msg_iter);
d8866baa
PP
670}
671
2d6bab5c
PP
672
673
d8866baa 674BT_HIDDEN
d24d5663 675bt_component_class_message_iterator_init_method_status dmesg_msg_iter_init(
d6e69534 676 bt_self_message_iterator *self_msg_iter,
b19ff26f
PP
677 bt_self_component_source *self_comp,
678 bt_self_component_port_output *self_port)
d8866baa 679{
caedbbee
PP
680 struct dmesg_component *dmesg_comp = bt_self_component_get_data(
681 bt_self_component_source_as_self_component(self_comp));
d6e69534
PP
682 struct dmesg_msg_iter *dmesg_msg_iter =
683 g_new0(struct dmesg_msg_iter, 1);
d24d5663
PP
684 bt_component_class_message_iterator_init_method_status status =
685 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
33ec5dfa 686
d6e69534 687 if (!dmesg_msg_iter) {
9c8a5044 688 BT_COMP_LOGE_STR("Failed to allocate on dmesg message iterator structure.");
33ec5dfa
PP
689 goto error;
690 }
691
f6ccaed9 692 BT_ASSERT(dmesg_comp);
d6e69534
PP
693 dmesg_msg_iter->dmesg_comp = dmesg_comp;
694 dmesg_msg_iter->pc_msg_iter = self_msg_iter;
d8866baa 695
33ec5dfa 696 if (dmesg_comp->params.read_from_stdin) {
d6e69534 697 dmesg_msg_iter->fp = stdin;
33ec5dfa 698 } else {
d6e69534
PP
699 dmesg_msg_iter->fp = fopen(dmesg_comp->params.path->str, "r");
700 if (!dmesg_msg_iter->fp) {
9c8a5044 701 BT_COMP_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"",
33ec5dfa
PP
702 dmesg_comp->params.path->str);
703 goto error;
704 }
705 }
706
d6e69534
PP
707 bt_self_message_iterator_set_data(self_msg_iter,
708 dmesg_msg_iter);
33ec5dfa
PP
709 goto end;
710
711error:
d6e69534
PP
712 destroy_dmesg_msg_iter(dmesg_msg_iter);
713 bt_self_message_iterator_set_data(self_msg_iter, NULL);
33ec5dfa 714 if (status >= 0) {
d24d5663 715 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
33ec5dfa
PP
716 }
717
718end:
33ec5dfa 719 return status;
d8866baa
PP
720}
721
722BT_HIDDEN
d6e69534
PP
723void dmesg_msg_iter_finalize(
724 bt_self_message_iterator *priv_msg_iter)
d8866baa 725{
d6e69534
PP
726 destroy_dmesg_msg_iter(bt_self_message_iterator_get_data(
727 priv_msg_iter));
d8866baa
PP
728}
729
d4393e08 730static
d24d5663 731bt_component_class_message_iterator_next_method_status dmesg_msg_iter_next_one(
d6e69534
PP
732 struct dmesg_msg_iter *dmesg_msg_iter,
733 bt_message **msg)
d8866baa 734{
33ec5dfa 735 ssize_t len;
33ec5dfa 736 struct dmesg_component *dmesg_comp;
d24d5663
PP
737 bt_component_class_message_iterator_next_method_status status =
738 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
d8866baa 739
d6e69534
PP
740 BT_ASSERT(dmesg_msg_iter);
741 dmesg_comp = dmesg_msg_iter->dmesg_comp;
f6ccaed9 742 BT_ASSERT(dmesg_comp);
33ec5dfa 743
d6e69534 744 if (dmesg_msg_iter->state == STATE_DONE) {
d24d5663 745 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
312c056a
PP
746 goto end;
747 }
748
d6e69534
PP
749 if (dmesg_msg_iter->tmp_event_msg ||
750 dmesg_msg_iter->state == STATE_EMIT_PACKET_END ||
751 dmesg_msg_iter->state == STATE_EMIT_STREAM_END) {
312c056a
PP
752 goto handle_state;
753 }
754
33ec5dfa
PP
755 while (true) {
756 const char *ch;
757 bool only_spaces = true;
758
d6e69534
PP
759 len = bt_getline(&dmesg_msg_iter->linebuf,
760 &dmesg_msg_iter->linebuf_len, dmesg_msg_iter->fp);
33ec5dfa
PP
761 if (len < 0) {
762 if (errno == EINVAL) {
d24d5663 763 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
33ec5dfa 764 } else if (errno == ENOMEM) {
d24d5663 765 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR;
33ec5dfa 766 } else {
d6e69534 767 if (dmesg_msg_iter->state == STATE_EMIT_STREAM_BEGINNING) {
312c056a 768 /* Stream did not even begin */
d24d5663 769 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
312c056a
PP
770 goto end;
771 } else {
772 /* End current packet now */
d6e69534 773 dmesg_msg_iter->state =
312c056a
PP
774 STATE_EMIT_PACKET_END;
775 goto handle_state;
776 }
33ec5dfa
PP
777 }
778
779 goto end;
780 }
781
d6e69534 782 BT_ASSERT(dmesg_msg_iter->linebuf);
33ec5dfa
PP
783
784 /* Ignore empty lines, once trimmed */
d6e69534 785 for (ch = dmesg_msg_iter->linebuf; *ch != '\0'; ch++) {
33ec5dfa
PP
786 if (!isspace(*ch)) {
787 only_spaces = false;
788 break;
789 }
790 }
791
792 if (!only_spaces) {
793 break;
794 }
795 }
796
d6e69534
PP
797 dmesg_msg_iter->tmp_event_msg = create_msg_from_line(
798 dmesg_msg_iter, dmesg_msg_iter->linebuf);
799 if (!dmesg_msg_iter->tmp_event_msg) {
9c8a5044 800 BT_COMP_LOGE("Cannot create event message from line: "
33ec5dfa 801 "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp,
d6e69534 802 dmesg_msg_iter->linebuf);
312c056a
PP
803 goto end;
804 }
805
806handle_state:
807 BT_ASSERT(dmesg_comp->trace);
808
d6e69534 809 switch (dmesg_msg_iter->state) {
312c056a 810 case STATE_EMIT_STREAM_BEGINNING:
d6e69534
PP
811 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
812 *msg = bt_message_stream_beginning_create(
813 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
814 dmesg_msg_iter->state = STATE_EMIT_PACKET_BEGINNING;
312c056a
PP
815 break;
816 case STATE_EMIT_PACKET_BEGINNING:
d6e69534 817 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
a6d85d2f
PP
818
819 if (dmesg_comp->clock_class) {
820 *msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
821 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet,
822 dmesg_msg_iter->last_clock_value);
823 } else {
824 *msg = bt_message_packet_beginning_create(
825 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet);
826 }
827
d6e69534 828 dmesg_msg_iter->state = STATE_EMIT_EVENT;
312c056a
PP
829 break;
830 case STATE_EMIT_EVENT:
d6e69534
PP
831 BT_ASSERT(dmesg_msg_iter->tmp_event_msg);
832 *msg = dmesg_msg_iter->tmp_event_msg;
833 dmesg_msg_iter->tmp_event_msg = NULL;
312c056a
PP
834 break;
835 case STATE_EMIT_PACKET_END:
a6d85d2f
PP
836 if (dmesg_comp->clock_class) {
837 *msg = bt_message_packet_end_create_with_default_clock_snapshot(
838 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet,
839 dmesg_msg_iter->last_clock_value);
840 } else {
841 *msg = bt_message_packet_end_create(
842 dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet);
843 }
844
d6e69534 845 dmesg_msg_iter->state = STATE_EMIT_STREAM_END;
312c056a
PP
846 break;
847 case STATE_EMIT_STREAM_END:
d6e69534
PP
848 *msg = bt_message_stream_end_create(
849 dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream);
850 dmesg_msg_iter->state = STATE_DONE;
312c056a
PP
851 break;
852 default:
853 break;
854 }
855
d6e69534 856 if (!*msg) {
9c8a5044 857 BT_COMP_LOGE("Cannot create message: dmesg-comp-addr=%p",
312c056a 858 dmesg_comp);
d24d5663 859 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
33ec5dfa
PP
860 }
861
862end:
d4393e08
PP
863 return status;
864}
865
866BT_HIDDEN
d24d5663 867bt_component_class_message_iterator_next_method_status dmesg_msg_iter_next(
d6e69534
PP
868 bt_self_message_iterator *self_msg_iter,
869 bt_message_array_const msgs, uint64_t capacity,
d4393e08
PP
870 uint64_t *count)
871{
d6e69534
PP
872 struct dmesg_msg_iter *dmesg_msg_iter =
873 bt_self_message_iterator_get_data(
874 self_msg_iter);
d24d5663
PP
875 bt_component_class_message_iterator_next_method_status status =
876 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
d4393e08
PP
877 uint64_t i = 0;
878
d94d92ac 879 while (i < capacity &&
d24d5663 880 status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
d6e69534 881 bt_message *priv_msg = NULL;
e5be10ef 882
d6e69534
PP
883 status = dmesg_msg_iter_next_one(dmesg_msg_iter,
884 &priv_msg);
885 msgs[i] = priv_msg;
d24d5663 886 if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) {
d4393e08
PP
887 i++;
888 }
889 }
890
891 if (i > 0) {
892 /*
d6e69534 893 * Even if dmesg_msg_iter_next_one() returned
d4393e08 894 * something else than
d6e69534
PP
895 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we
896 * accumulated message objects in the output
897 * message array, so we need to return
898 * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they
d94d92ac 899 * are transfered to downstream. This other status
d6e69534 900 * occurs again the next time muxer_msg_iter_do_next()
d94d92ac 901 * is called, possibly without any accumulated
d6e69534 902 * message, in which case we'll return it.
d4393e08
PP
903 */
904 *count = i;
d24d5663 905 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
d4393e08
PP
906 }
907
908 return status;
d8866baa 909}
2d6bab5c
PP
910
911BT_HIDDEN
912bt_bool dmesg_msg_iter_can_seek_beginning(
913 bt_self_message_iterator *self_msg_iter)
914{
915 struct dmesg_msg_iter *dmesg_msg_iter =
916 bt_self_message_iterator_get_data(self_msg_iter);
917
918 /* Can't seek the beginning of the standard input stream */
919 return !dmesg_msg_iter->dmesg_comp->params.read_from_stdin;
920}
921
922BT_HIDDEN
d24d5663
PP
923bt_component_class_message_iterator_seek_beginning_method_status
924dmesg_msg_iter_seek_beginning(
2d6bab5c
PP
925 bt_self_message_iterator *self_msg_iter)
926{
927 struct dmesg_msg_iter *dmesg_msg_iter =
928 bt_self_message_iterator_get_data(self_msg_iter);
929
930 BT_ASSERT(!dmesg_msg_iter->dmesg_comp->params.read_from_stdin);
931
932 BT_MESSAGE_PUT_REF_AND_RESET(dmesg_msg_iter->tmp_event_msg);
933 dmesg_msg_iter->last_clock_value = 0;
934 dmesg_msg_iter->state = STATE_EMIT_STREAM_BEGINNING;
d24d5663 935 return BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
2d6bab5c 936}
This page took 0.091943 seconds and 4 git commands to generate.