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