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