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 | 38 | #include <glib.h> |
430a5ccb | 39 | #include "plugins/common/param-validation/param-validation.h" |
d8866baa | 40 | |
33ec5dfa PP |
41 | #define NSEC_PER_USEC 1000UL |
42 | #define NSEC_PER_MSEC 1000000UL | |
43 | #define NSEC_PER_SEC 1000000000ULL | |
44 | #define USEC_PER_SEC 1000000UL | |
45 | ||
d8866baa PP |
46 | struct dmesg_component; |
47 | ||
d6e69534 | 48 | struct dmesg_msg_iter { |
d8866baa | 49 | struct dmesg_component *dmesg_comp; |
d6e69534 | 50 | bt_self_message_iterator *pc_msg_iter; /* Weak */ |
33ec5dfa PP |
51 | char *linebuf; |
52 | size_t linebuf_len; | |
d8866baa | 53 | FILE *fp; |
d6e69534 | 54 | bt_message *tmp_event_msg; |
a6d85d2f | 55 | uint64_t last_clock_value; |
312c056a PP |
56 | |
57 | enum { | |
58 | STATE_EMIT_STREAM_BEGINNING, | |
312c056a | 59 | STATE_EMIT_EVENT, |
312c056a PP |
60 | STATE_EMIT_STREAM_END, |
61 | STATE_DONE, | |
62 | } state; | |
d8866baa PP |
63 | }; |
64 | ||
65 | struct dmesg_component { | |
caedbbee PP |
66 | bt_logging_level log_level; |
67 | ||
d8866baa PP |
68 | struct { |
69 | GString *path; | |
33ec5dfa | 70 | bt_bool read_from_stdin; |
707b323a | 71 | bt_bool no_timestamp; |
d8866baa PP |
72 | } params; |
73 | ||
9c8a5044 PP |
74 | bt_self_component_source *self_comp_src; |
75 | bt_self_component *self_comp; | |
b19ff26f PP |
76 | bt_trace_class *trace_class; |
77 | bt_stream_class *stream_class; | |
78 | bt_event_class *event_class; | |
79 | bt_trace *trace; | |
80 | bt_stream *stream; | |
b19ff26f | 81 | bt_clock_class *clock_class; |
d8866baa PP |
82 | }; |
83 | ||
33ec5dfa | 84 | static |
caedbbee PP |
85 | bt_field_class *create_event_payload_fc(struct dmesg_component *dmesg_comp, |
86 | bt_trace_class *trace_class) | |
33ec5dfa | 87 | { |
b19ff26f PP |
88 | bt_field_class *root_fc = NULL; |
89 | bt_field_class *fc = NULL; | |
33ec5dfa PP |
90 | int ret; |
91 | ||
1122a43a | 92 | root_fc = bt_field_class_structure_create(trace_class); |
5cd6d0e5 | 93 | if (!root_fc) { |
9c8a5044 | 94 | BT_COMP_LOGE_STR("Cannot create an empty structure field class object."); |
33ec5dfa PP |
95 | goto error; |
96 | } | |
97 | ||
1122a43a | 98 | fc = bt_field_class_string_create(trace_class); |
5cd6d0e5 | 99 | if (!fc) { |
9c8a5044 | 100 | BT_COMP_LOGE_STR("Cannot create a string field class object."); |
33ec5dfa PP |
101 | goto error; |
102 | } | |
103 | ||
40f4ba76 | 104 | ret = bt_field_class_structure_append_member(root_fc, |
e5be10ef | 105 | "str", fc); |
33ec5dfa | 106 | if (ret) { |
9c8a5044 | 107 | BT_COMP_LOGE("Cannot add `str` member to structure field class: " |
33ec5dfa PP |
108 | "ret=%d", ret); |
109 | goto error; | |
110 | } | |
111 | ||
112 | goto end; | |
113 | ||
114 | error: | |
c5b9b441 | 115 | BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc); |
33ec5dfa PP |
116 | |
117 | end: | |
c5b9b441 | 118 | bt_field_class_put_ref(fc); |
5cd6d0e5 | 119 | return root_fc; |
33ec5dfa PP |
120 | } |
121 | ||
33ec5dfa PP |
122 | static |
123 | int create_meta(struct dmesg_component *dmesg_comp, bool has_ts) | |
124 | { | |
b19ff26f | 125 | bt_field_class *fc = NULL; |
d8866baa PP |
126 | int ret = 0; |
127 | ||
9c8a5044 | 128 | dmesg_comp->trace_class = bt_trace_class_create(dmesg_comp->self_comp); |
862ca4ed | 129 | if (!dmesg_comp->trace_class) { |
9c8a5044 | 130 | BT_COMP_LOGE_STR("Cannot create an empty trace class object."); |
33ec5dfa PP |
131 | goto error; |
132 | } | |
133 | ||
40f4ba76 | 134 | dmesg_comp->stream_class = bt_stream_class_create( |
862ca4ed | 135 | dmesg_comp->trace_class); |
33ec5dfa | 136 | if (!dmesg_comp->stream_class) { |
9c8a5044 | 137 | BT_COMP_LOGE_STR("Cannot create a stream class object."); |
33ec5dfa PP |
138 | goto error; |
139 | } | |
140 | ||
33ec5dfa | 141 | if (has_ts) { |
0f2d58c9 | 142 | dmesg_comp->clock_class = bt_clock_class_create( |
9c8a5044 | 143 | dmesg_comp->self_comp); |
33ec5dfa | 144 | if (!dmesg_comp->clock_class) { |
9c8a5044 | 145 | BT_COMP_LOGE_STR("Cannot create clock class."); |
33ec5dfa PP |
146 | goto error; |
147 | } | |
148 | ||
21029722 | 149 | /* |
5552377a PP |
150 | * The `dmesg` timestamp's origin is not the Unix epoch, |
151 | * it's the boot time. | |
21029722 | 152 | */ |
5552377a | 153 | bt_clock_class_set_origin_is_unix_epoch(dmesg_comp->clock_class, |
21029722 PP |
154 | BT_FALSE); |
155 | ||
40f4ba76 PP |
156 | ret = bt_stream_class_set_default_clock_class( |
157 | dmesg_comp->stream_class, dmesg_comp->clock_class); | |
33ec5dfa | 158 | if (ret) { |
9c8a5044 | 159 | BT_COMP_LOGE_STR("Cannot set stream class's default clock class."); |
33ec5dfa PP |
160 | goto error; |
161 | } | |
162 | } | |
163 | ||
40f4ba76 | 164 | dmesg_comp->event_class = bt_event_class_create( |
44c440bc | 165 | dmesg_comp->stream_class); |
33ec5dfa | 166 | if (!dmesg_comp->event_class) { |
9c8a5044 | 167 | BT_COMP_LOGE_STR("Cannot create an event class object."); |
44c440bc PP |
168 | goto error; |
169 | } | |
170 | ||
40f4ba76 | 171 | ret = bt_event_class_set_name(dmesg_comp->event_class, "string"); |
44c440bc | 172 | if (ret) { |
9c8a5044 | 173 | BT_COMP_LOGE_STR("Cannot set event class's name."); |
33ec5dfa PP |
174 | goto error; |
175 | } | |
176 | ||
caedbbee | 177 | fc = create_event_payload_fc(dmesg_comp, dmesg_comp->trace_class); |
5cd6d0e5 | 178 | if (!fc) { |
9c8a5044 | 179 | BT_COMP_LOGE_STR("Cannot create event payload field class."); |
d8866baa PP |
180 | goto error; |
181 | } | |
182 | ||
40f4ba76 | 183 | ret = bt_event_class_set_payload_field_class(dmesg_comp->event_class, fc); |
33ec5dfa | 184 | if (ret) { |
9c8a5044 | 185 | BT_COMP_LOGE_STR("Cannot set event class's event payload field class."); |
33ec5dfa PP |
186 | goto error; |
187 | } | |
188 | ||
33ec5dfa PP |
189 | goto end; |
190 | ||
191 | error: | |
192 | ret = -1; | |
193 | ||
194 | end: | |
c5b9b441 | 195 | bt_field_class_put_ref(fc); |
33ec5dfa PP |
196 | return ret; |
197 | } | |
198 | ||
430a5ccb SM |
199 | struct bt_param_validation_map_value_entry_descr dmesg_params[] = { |
200 | { "no-extract-timestamp", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_BOOL } }, | |
201 | { "path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL, { .type = BT_VALUE_TYPE_STRING } }, | |
202 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END | |
203 | }; | |
204 | ||
33ec5dfa | 205 | static |
430a5ccb SM |
206 | bt_component_class_initialize_method_status handle_params( |
207 | struct dmesg_component *dmesg_comp, | |
b19ff26f | 208 | const bt_value *params) |
33ec5dfa | 209 | { |
b19ff26f PP |
210 | const bt_value *no_timestamp = NULL; |
211 | const bt_value *path = NULL; | |
430a5ccb SM |
212 | bt_component_class_initialize_method_status status; |
213 | enum bt_param_validation_status validation_status; | |
214 | gchar *validate_error = NULL; | |
215 | ||
216 | validation_status = bt_param_validation_validate(params, | |
217 | dmesg_params, &validate_error); | |
218 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
219 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; | |
220 | goto end; | |
221 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
222 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; | |
223 | BT_COMP_LOGE_APPEND_CAUSE(dmesg_comp->self_comp, | |
224 | "%s", validate_error); | |
225 | goto end; | |
226 | } | |
33ec5dfa | 227 | |
05e21286 | 228 | no_timestamp = bt_value_map_borrow_entry_value_const(params, |
07208d85 | 229 | "no-extract-timestamp"); |
707b323a | 230 | if (no_timestamp) { |
601b0d3c PP |
231 | dmesg_comp->params.no_timestamp = |
232 | bt_value_bool_get(no_timestamp); | |
707b323a PP |
233 | } |
234 | ||
05e21286 | 235 | path = bt_value_map_borrow_entry_value_const(params, "path"); |
d8866baa | 236 | if (path) { |
430a5ccb | 237 | const char *path_str = bt_value_string_get(path); |
d8866baa | 238 | |
d8866baa PP |
239 | g_string_assign(dmesg_comp->params.path, path_str); |
240 | } else { | |
4da23e31 | 241 | dmesg_comp->params.read_from_stdin = true; |
d8866baa PP |
242 | } |
243 | ||
430a5ccb | 244 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
d8866baa | 245 | end: |
430a5ccb SM |
246 | g_free(validate_error); |
247 | ||
248 | return status; | |
d8866baa PP |
249 | } |
250 | ||
33ec5dfa | 251 | static |
26fc5aed | 252 | int create_stream_and_trace(struct dmesg_component *dmesg_comp) |
33ec5dfa PP |
253 | { |
254 | int ret = 0; | |
e54986cf | 255 | const char *trace_name = NULL; |
862ca4ed PP |
256 | gchar *basename = NULL; |
257 | ||
258 | dmesg_comp->trace = bt_trace_create(dmesg_comp->trace_class); | |
259 | if (!dmesg_comp->trace) { | |
9c8a5044 | 260 | BT_COMP_LOGE_STR("Cannot create trace object."); |
862ca4ed PP |
261 | goto error; |
262 | } | |
33ec5dfa | 263 | |
862ca4ed PP |
264 | if (dmesg_comp->params.read_from_stdin) { |
265 | trace_name = "STDIN"; | |
266 | } else { | |
267 | basename = g_path_get_basename(dmesg_comp->params.path->str); | |
268 | BT_ASSERT(basename); | |
269 | ||
270 | if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 && | |
271 | strcmp(basename, ".") != 0) { | |
272 | trace_name = basename; | |
273 | } | |
274 | } | |
275 | ||
276 | if (trace_name) { | |
277 | ret = bt_trace_set_name(dmesg_comp->trace, trace_name); | |
278 | if (ret) { | |
9c8a5044 | 279 | BT_COMP_LOGE("Cannot set trace's name: name=\"%s\"", |
862ca4ed PP |
280 | trace_name); |
281 | goto error; | |
282 | } | |
283 | } | |
284 | ||
285 | dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class, | |
286 | dmesg_comp->trace); | |
33ec5dfa | 287 | if (!dmesg_comp->stream) { |
9c8a5044 | 288 | BT_COMP_LOGE_STR("Cannot create stream object."); |
33ec5dfa PP |
289 | goto error; |
290 | } | |
291 | ||
33ec5dfa PP |
292 | goto end; |
293 | ||
294 | error: | |
295 | ret = -1; | |
296 | ||
297 | end: | |
19bbdc9b | 298 | g_free(basename); |
862ca4ed | 299 | |
33ec5dfa PP |
300 | return ret; |
301 | } | |
302 | ||
303 | static | |
26fc5aed | 304 | int try_create_meta_stream(struct dmesg_component *dmesg_comp, bool has_ts) |
33ec5dfa PP |
305 | { |
306 | int ret = 0; | |
307 | ||
308 | if (dmesg_comp->trace) { | |
309 | /* Already created */ | |
310 | goto end; | |
311 | } | |
312 | ||
313 | ret = create_meta(dmesg_comp, has_ts); | |
314 | if (ret) { | |
9c8a5044 | 315 | BT_COMP_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p", |
33ec5dfa PP |
316 | dmesg_comp); |
317 | goto error; | |
318 | } | |
319 | ||
26fc5aed | 320 | ret = create_stream_and_trace(dmesg_comp); |
33ec5dfa | 321 | if (ret) { |
26fc5aed | 322 | BT_COMP_LOGE("Cannot create stream object: " |
33ec5dfa PP |
323 | "dmesg-comp-addr=%p", dmesg_comp); |
324 | goto error; | |
325 | } | |
326 | ||
327 | goto end; | |
328 | ||
329 | error: | |
330 | ret = -1; | |
331 | ||
332 | end: | |
333 | return ret; | |
334 | } | |
d8866baa PP |
335 | |
336 | static | |
337 | void destroy_dmesg_component(struct dmesg_component *dmesg_comp) | |
338 | { | |
339 | if (!dmesg_comp) { | |
340 | return; | |
341 | } | |
342 | ||
343 | if (dmesg_comp->params.path) { | |
344 | g_string_free(dmesg_comp->params.path, TRUE); | |
345 | } | |
346 | ||
c5b9b441 PP |
347 | bt_trace_put_ref(dmesg_comp->trace); |
348 | bt_stream_class_put_ref(dmesg_comp->stream_class); | |
349 | bt_event_class_put_ref(dmesg_comp->event_class); | |
350 | bt_stream_put_ref(dmesg_comp->stream); | |
351 | bt_clock_class_put_ref(dmesg_comp->clock_class); | |
8b5bd70c | 352 | bt_trace_class_put_ref(dmesg_comp->trace_class); |
d8866baa PP |
353 | g_free(dmesg_comp); |
354 | } | |
355 | ||
33ec5dfa | 356 | static |
21a9f056 | 357 | bt_component_class_initialize_method_status create_port( |
b19ff26f | 358 | bt_self_component_source *self_comp) |
33ec5dfa | 359 | { |
21a9f056 | 360 | bt_component_class_initialize_method_status status; |
d24d5663 PP |
361 | bt_self_component_add_port_status add_port_status; |
362 | ||
363 | add_port_status = bt_self_component_source_add_output_port(self_comp, | |
33ec5dfa | 364 | "out", NULL, NULL); |
d24d5663 PP |
365 | switch (add_port_status) { |
366 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK: | |
21a9f056 | 367 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
d24d5663 PP |
368 | break; |
369 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR: | |
21a9f056 | 370 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
d24d5663 PP |
371 | break; |
372 | case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR: | |
21a9f056 | 373 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
d24d5663 PP |
374 | break; |
375 | default: | |
376 | abort(); | |
377 | } | |
378 | ||
379 | return status; | |
33ec5dfa PP |
380 | } |
381 | ||
d8866baa | 382 | BT_HIDDEN |
21a9f056 | 383 | bt_component_class_initialize_method_status dmesg_init( |
9c8a5044 | 384 | bt_self_component_source *self_comp_src, |
59225a3e | 385 | bt_self_component_source_configuration *config, |
430a5ccb | 386 | const bt_value *params, void *init_method_data) |
d8866baa | 387 | { |
d8866baa | 388 | struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1); |
430a5ccb | 389 | bt_component_class_initialize_method_status status; |
9c8a5044 PP |
390 | bt_self_component *self_comp = |
391 | bt_self_component_source_as_self_component(self_comp_src); | |
392 | const bt_component *comp = bt_self_component_as_component(self_comp); | |
caedbbee | 393 | bt_logging_level log_level = bt_component_get_logging_level(comp); |
d8866baa PP |
394 | |
395 | if (!dmesg_comp) { | |
caedbbee | 396 | /* Implicit log level is not available here */ |
9c8a5044 | 397 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp, |
caedbbee | 398 | "Failed to allocate one dmesg component structure."); |
430a5ccb | 399 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
d8866baa PP |
400 | goto error; |
401 | } | |
402 | ||
caedbbee | 403 | dmesg_comp->log_level = log_level; |
41693723 | 404 | dmesg_comp->self_comp = self_comp; |
9c8a5044 | 405 | dmesg_comp->self_comp_src = self_comp_src; |
d8866baa PP |
406 | dmesg_comp->params.path = g_string_new(NULL); |
407 | if (!dmesg_comp->params.path) { | |
9c8a5044 | 408 | BT_COMP_LOGE_STR("Failed to allocate a GString."); |
430a5ccb | 409 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
d8866baa PP |
410 | goto error; |
411 | } | |
412 | ||
430a5ccb SM |
413 | status = handle_params(dmesg_comp, params); |
414 | if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { | |
9c8a5044 | 415 | BT_COMP_LOGE("Invalid parameters: comp-addr=%p", self_comp); |
430a5ccb | 416 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
d8866baa PP |
417 | goto error; |
418 | } | |
419 | ||
33ec5dfa PP |
420 | if (!dmesg_comp->params.read_from_stdin && |
421 | !g_file_test(dmesg_comp->params.path->str, | |
422 | G_FILE_TEST_IS_REGULAR)) { | |
9c8a5044 | 423 | BT_COMP_LOGE("Input path is not a regular file: " |
d94d92ac | 424 | "comp-addr=%p, path=\"%s\"", self_comp, |
33ec5dfa | 425 | dmesg_comp->params.path->str); |
430a5ccb | 426 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
33ec5dfa PP |
427 | goto error; |
428 | } | |
d8866baa | 429 | |
9c8a5044 | 430 | status = create_port(self_comp_src); |
21a9f056 | 431 | if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) { |
33ec5dfa PP |
432 | goto error; |
433 | } | |
d8866baa | 434 | |
9c8a5044 PP |
435 | bt_self_component_set_data(self_comp, dmesg_comp); |
436 | BT_COMP_LOGI_STR("Component initialized."); | |
430a5ccb SM |
437 | |
438 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; | |
d8866baa PP |
439 | goto end; |
440 | ||
441 | error: | |
442 | destroy_dmesg_component(dmesg_comp); | |
9c8a5044 | 443 | bt_self_component_set_data(self_comp, NULL); |
33ec5dfa | 444 | |
d8866baa PP |
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, ']'); | |
98b15851 | 512 | BT_ASSERT_DBG(*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); |
98b15851 | 548 | BT_ASSERT_DBG(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); |
98b15851 | 568 | BT_ASSERT_DBG(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); |
98b15851 | 617 | BT_ASSERT_DBG(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 |
21a9f056 | 659 | bt_component_class_message_iterator_initialize_method_status dmesg_msg_iter_init( |
d6e69534 | 660 | bt_self_message_iterator *self_msg_iter, |
8d8b141d | 661 | bt_self_message_iterator_configuration *config, |
b19ff26f PP |
662 | bt_self_component_source *self_comp, |
663 | bt_self_component_port_output *self_port) | |
d8866baa | 664 | { |
caedbbee PP |
665 | struct dmesg_component *dmesg_comp = bt_self_component_get_data( |
666 | bt_self_component_source_as_self_component(self_comp)); | |
d6e69534 PP |
667 | struct dmesg_msg_iter *dmesg_msg_iter = |
668 | g_new0(struct dmesg_msg_iter, 1); | |
21a9f056 FD |
669 | bt_component_class_message_iterator_initialize_method_status status = |
670 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK; | |
33ec5dfa | 671 | |
d6e69534 | 672 | if (!dmesg_msg_iter) { |
9c8a5044 | 673 | BT_COMP_LOGE_STR("Failed to allocate on dmesg message iterator structure."); |
33ec5dfa PP |
674 | goto error; |
675 | } | |
676 | ||
f6ccaed9 | 677 | BT_ASSERT(dmesg_comp); |
d6e69534 PP |
678 | dmesg_msg_iter->dmesg_comp = dmesg_comp; |
679 | dmesg_msg_iter->pc_msg_iter = self_msg_iter; | |
d8866baa | 680 | |
33ec5dfa | 681 | if (dmesg_comp->params.read_from_stdin) { |
d6e69534 | 682 | dmesg_msg_iter->fp = stdin; |
33ec5dfa | 683 | } else { |
d6e69534 PP |
684 | dmesg_msg_iter->fp = fopen(dmesg_comp->params.path->str, "r"); |
685 | if (!dmesg_msg_iter->fp) { | |
9c8a5044 | 686 | BT_COMP_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"", |
33ec5dfa PP |
687 | dmesg_comp->params.path->str); |
688 | goto error; | |
689 | } | |
690 | } | |
691 | ||
d6e69534 PP |
692 | bt_self_message_iterator_set_data(self_msg_iter, |
693 | dmesg_msg_iter); | |
33ec5dfa PP |
694 | goto end; |
695 | ||
696 | error: | |
d6e69534 PP |
697 | destroy_dmesg_msg_iter(dmesg_msg_iter); |
698 | bt_self_message_iterator_set_data(self_msg_iter, NULL); | |
33ec5dfa | 699 | if (status >= 0) { |
21a9f056 | 700 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR; |
33ec5dfa PP |
701 | } |
702 | ||
703 | end: | |
33ec5dfa | 704 | return status; |
d8866baa PP |
705 | } |
706 | ||
707 | BT_HIDDEN | |
d6e69534 PP |
708 | void dmesg_msg_iter_finalize( |
709 | bt_self_message_iterator *priv_msg_iter) | |
d8866baa | 710 | { |
d6e69534 PP |
711 | destroy_dmesg_msg_iter(bt_self_message_iterator_get_data( |
712 | priv_msg_iter)); | |
d8866baa PP |
713 | } |
714 | ||
d4393e08 | 715 | static |
d24d5663 | 716 | bt_component_class_message_iterator_next_method_status dmesg_msg_iter_next_one( |
d6e69534 PP |
717 | struct dmesg_msg_iter *dmesg_msg_iter, |
718 | bt_message **msg) | |
d8866baa | 719 | { |
33ec5dfa | 720 | ssize_t len; |
33ec5dfa | 721 | struct dmesg_component *dmesg_comp; |
d24d5663 PP |
722 | bt_component_class_message_iterator_next_method_status status = |
723 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
d8866baa | 724 | |
98b15851 | 725 | BT_ASSERT_DBG(dmesg_msg_iter); |
d6e69534 | 726 | dmesg_comp = dmesg_msg_iter->dmesg_comp; |
98b15851 | 727 | BT_ASSERT_DBG(dmesg_comp); |
33ec5dfa | 728 | |
d6e69534 | 729 | if (dmesg_msg_iter->state == STATE_DONE) { |
d24d5663 | 730 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END; |
312c056a PP |
731 | goto end; |
732 | } | |
733 | ||
d6e69534 | 734 | if (dmesg_msg_iter->tmp_event_msg || |
d6e69534 | 735 | dmesg_msg_iter->state == STATE_EMIT_STREAM_END) { |
312c056a PP |
736 | goto handle_state; |
737 | } | |
738 | ||
33ec5dfa PP |
739 | while (true) { |
740 | const char *ch; | |
741 | bool only_spaces = true; | |
742 | ||
d6e69534 PP |
743 | len = bt_getline(&dmesg_msg_iter->linebuf, |
744 | &dmesg_msg_iter->linebuf_len, dmesg_msg_iter->fp); | |
33ec5dfa PP |
745 | if (len < 0) { |
746 | if (errno == EINVAL) { | |
d24d5663 | 747 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
33ec5dfa | 748 | } else if (errno == ENOMEM) { |
d24d5663 | 749 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR; |
33ec5dfa | 750 | } else { |
d6e69534 | 751 | if (dmesg_msg_iter->state == STATE_EMIT_STREAM_BEGINNING) { |
312c056a | 752 | /* Stream did not even begin */ |
d24d5663 | 753 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END; |
312c056a PP |
754 | goto end; |
755 | } else { | |
26fc5aed | 756 | /* End stream now */ |
d6e69534 | 757 | dmesg_msg_iter->state = |
26fc5aed | 758 | STATE_EMIT_STREAM_END; |
312c056a PP |
759 | goto handle_state; |
760 | } | |
33ec5dfa PP |
761 | } |
762 | ||
763 | goto end; | |
764 | } | |
765 | ||
98b15851 | 766 | BT_ASSERT_DBG(dmesg_msg_iter->linebuf); |
33ec5dfa PP |
767 | |
768 | /* Ignore empty lines, once trimmed */ | |
d6e69534 | 769 | for (ch = dmesg_msg_iter->linebuf; *ch != '\0'; ch++) { |
33ec5dfa PP |
770 | if (!isspace(*ch)) { |
771 | only_spaces = false; | |
772 | break; | |
773 | } | |
774 | } | |
775 | ||
776 | if (!only_spaces) { | |
777 | break; | |
778 | } | |
779 | } | |
780 | ||
d6e69534 PP |
781 | dmesg_msg_iter->tmp_event_msg = create_msg_from_line( |
782 | dmesg_msg_iter, dmesg_msg_iter->linebuf); | |
783 | if (!dmesg_msg_iter->tmp_event_msg) { | |
9c8a5044 | 784 | BT_COMP_LOGE("Cannot create event message from line: " |
33ec5dfa | 785 | "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp, |
d6e69534 | 786 | dmesg_msg_iter->linebuf); |
312c056a PP |
787 | goto end; |
788 | } | |
789 | ||
790 | handle_state: | |
98b15851 | 791 | BT_ASSERT_DBG(dmesg_comp->trace); |
312c056a | 792 | |
d6e69534 | 793 | switch (dmesg_msg_iter->state) { |
312c056a | 794 | case STATE_EMIT_STREAM_BEGINNING: |
98b15851 | 795 | BT_ASSERT_DBG(dmesg_msg_iter->tmp_event_msg); |
d6e69534 PP |
796 | *msg = bt_message_stream_beginning_create( |
797 | dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream); | |
d6e69534 | 798 | dmesg_msg_iter->state = STATE_EMIT_EVENT; |
312c056a PP |
799 | break; |
800 | case STATE_EMIT_EVENT: | |
98b15851 | 801 | BT_ASSERT_DBG(dmesg_msg_iter->tmp_event_msg); |
d6e69534 PP |
802 | *msg = dmesg_msg_iter->tmp_event_msg; |
803 | dmesg_msg_iter->tmp_event_msg = NULL; | |
312c056a | 804 | break; |
312c056a | 805 | case STATE_EMIT_STREAM_END: |
d6e69534 PP |
806 | *msg = bt_message_stream_end_create( |
807 | dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream); | |
808 | dmesg_msg_iter->state = STATE_DONE; | |
312c056a PP |
809 | break; |
810 | default: | |
811 | break; | |
812 | } | |
813 | ||
d6e69534 | 814 | if (!*msg) { |
9c8a5044 | 815 | BT_COMP_LOGE("Cannot create message: dmesg-comp-addr=%p", |
312c056a | 816 | dmesg_comp); |
d24d5663 | 817 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR; |
33ec5dfa PP |
818 | } |
819 | ||
820 | end: | |
d4393e08 PP |
821 | return status; |
822 | } | |
823 | ||
824 | BT_HIDDEN | |
d24d5663 | 825 | bt_component_class_message_iterator_next_method_status dmesg_msg_iter_next( |
d6e69534 PP |
826 | bt_self_message_iterator *self_msg_iter, |
827 | bt_message_array_const msgs, uint64_t capacity, | |
d4393e08 PP |
828 | uint64_t *count) |
829 | { | |
d6e69534 PP |
830 | struct dmesg_msg_iter *dmesg_msg_iter = |
831 | bt_self_message_iterator_get_data( | |
832 | self_msg_iter); | |
d24d5663 PP |
833 | bt_component_class_message_iterator_next_method_status status = |
834 | BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; | |
d4393e08 PP |
835 | uint64_t i = 0; |
836 | ||
d94d92ac | 837 | while (i < capacity && |
d24d5663 | 838 | status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
d6e69534 | 839 | bt_message *priv_msg = NULL; |
e5be10ef | 840 | |
d6e69534 PP |
841 | status = dmesg_msg_iter_next_one(dmesg_msg_iter, |
842 | &priv_msg); | |
843 | msgs[i] = priv_msg; | |
d24d5663 | 844 | if (status == BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK) { |
d4393e08 PP |
845 | i++; |
846 | } | |
847 | } | |
848 | ||
849 | if (i > 0) { | |
850 | /* | |
d6e69534 | 851 | * Even if dmesg_msg_iter_next_one() returned |
d4393e08 | 852 | * something else than |
d6e69534 PP |
853 | * BT_SELF_MESSAGE_ITERATOR_STATUS_OK, we |
854 | * accumulated message objects in the output | |
855 | * message array, so we need to return | |
856 | * BT_SELF_MESSAGE_ITERATOR_STATUS_OK so that they | |
d94d92ac | 857 | * are transfered to downstream. This other status |
d6e69534 | 858 | * occurs again the next time muxer_msg_iter_do_next() |
d94d92ac | 859 | * is called, possibly without any accumulated |
d6e69534 | 860 | * message, in which case we'll return it. |
d4393e08 PP |
861 | */ |
862 | *count = i; | |
d24d5663 | 863 | status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK; |
d4393e08 PP |
864 | } |
865 | ||
866 | return status; | |
d8866baa | 867 | } |
2d6bab5c PP |
868 | |
869 | BT_HIDDEN | |
f2fb1b32 SM |
870 | bt_component_class_message_iterator_can_seek_beginning_method_status |
871 | dmesg_msg_iter_can_seek_beginning( | |
872 | bt_self_message_iterator *self_msg_iter, bt_bool *can_seek) | |
2d6bab5c PP |
873 | { |
874 | struct dmesg_msg_iter *dmesg_msg_iter = | |
875 | bt_self_message_iterator_get_data(self_msg_iter); | |
876 | ||
877 | /* Can't seek the beginning of the standard input stream */ | |
f2fb1b32 SM |
878 | *can_seek = !dmesg_msg_iter->dmesg_comp->params.read_from_stdin; |
879 | ||
880 | return BT_COMPONENT_CLASS_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_METHOD_STATUS_OK; | |
2d6bab5c PP |
881 | } |
882 | ||
883 | BT_HIDDEN | |
d24d5663 PP |
884 | bt_component_class_message_iterator_seek_beginning_method_status |
885 | dmesg_msg_iter_seek_beginning( | |
2d6bab5c PP |
886 | bt_self_message_iterator *self_msg_iter) |
887 | { | |
888 | struct dmesg_msg_iter *dmesg_msg_iter = | |
889 | bt_self_message_iterator_get_data(self_msg_iter); | |
890 | ||
891 | BT_ASSERT(!dmesg_msg_iter->dmesg_comp->params.read_from_stdin); | |
892 | ||
893 | BT_MESSAGE_PUT_REF_AND_RESET(dmesg_msg_iter->tmp_event_msg); | |
894 | dmesg_msg_iter->last_clock_value = 0; | |
895 | dmesg_msg_iter->state = STATE_EMIT_STREAM_BEGINNING; | |
d24d5663 | 896 | return BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK; |
2d6bab5c | 897 | } |