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