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