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