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