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