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