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