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