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