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 | ||
e2531d3b PP |
24 | #define BT_LOG_TAG "PLUGIN-TEXT-DMESG-SRC" |
25 | #include "logging.h" | |
26 | ||
d8866baa | 27 | #include <stdbool.h> |
e2531d3b PP |
28 | #include <string.h> |
29 | #include <ctype.h> | |
d8866baa | 30 | #include <stdio.h> |
8b45963b | 31 | #include <babeltrace/assert-internal.h> |
17582c6d | 32 | #include <babeltrace/common-internal.h> |
e2531d3b | 33 | #include <babeltrace/babeltrace.h> |
0f15f666 | 34 | #include <babeltrace/value-internal.h> |
e2531d3b PP |
35 | #include <babeltrace/compat/utc-internal.h> |
36 | #include <babeltrace/compat/stdio-internal.h> | |
d8866baa PP |
37 | #include <glib.h> |
38 | ||
e2531d3b PP |
39 | #define NSEC_PER_USEC 1000UL |
40 | #define NSEC_PER_MSEC 1000000UL | |
41 | #define NSEC_PER_SEC 1000000000ULL | |
42 | #define USEC_PER_SEC 1000000UL | |
43 | ||
d8866baa PP |
44 | struct dmesg_component; |
45 | ||
b09a5592 | 46 | struct dmesg_msg_iter { |
d8866baa | 47 | struct dmesg_component *dmesg_comp; |
b09a5592 | 48 | bt_self_message_iterator *pc_msg_iter; /* Weak */ |
e2531d3b PP |
49 | char *linebuf; |
50 | size_t linebuf_len; | |
d8866baa | 51 | FILE *fp; |
b09a5592 | 52 | bt_message *tmp_event_msg; |
a6918753 PP |
53 | |
54 | enum { | |
55 | STATE_EMIT_STREAM_BEGINNING, | |
66ff4085 | 56 | STATE_EMIT_STREAM_ACTIVITY_BEGINNING, |
a6918753 PP |
57 | STATE_EMIT_PACKET_BEGINNING, |
58 | STATE_EMIT_EVENT, | |
59 | STATE_EMIT_PACKET_END, | |
66ff4085 | 60 | STATE_EMIT_STREAM_ACTIVITY_END, |
a6918753 PP |
61 | STATE_EMIT_STREAM_END, |
62 | STATE_DONE, | |
63 | } state; | |
d8866baa PP |
64 | }; |
65 | ||
66 | struct dmesg_component { | |
67 | struct { | |
68 | GString *path; | |
e2531d3b | 69 | bt_bool read_from_stdin; |
8743317e | 70 | bt_bool no_timestamp; |
d8866baa PP |
71 | } params; |
72 | ||
e5e71bf8 | 73 | bt_self_component_source *self_comp; |
8eee8ea2 PP |
74 | bt_trace_class *trace_class; |
75 | bt_stream_class *stream_class; | |
76 | bt_event_class *event_class; | |
77 | bt_trace *trace; | |
78 | bt_stream *stream; | |
79 | bt_packet *packet; | |
80 | bt_clock_class *clock_class; | |
d8866baa PP |
81 | }; |
82 | ||
e2531d3b | 83 | static |
b7396828 | 84 | bt_field_class *create_event_payload_fc(bt_trace_class *trace_class) |
e2531d3b | 85 | { |
8eee8ea2 PP |
86 | bt_field_class *root_fc = NULL; |
87 | bt_field_class *fc = NULL; | |
e2531d3b PP |
88 | int ret; |
89 | ||
b7396828 | 90 | root_fc = bt_field_class_structure_create(trace_class); |
939190b3 PP |
91 | if (!root_fc) { |
92 | BT_LOGE_STR("Cannot create an empty structure field class object."); | |
e2531d3b PP |
93 | goto error; |
94 | } | |
95 | ||
b7396828 | 96 | fc = bt_field_class_string_create(trace_class); |
939190b3 PP |
97 | if (!fc) { |
98 | BT_LOGE_STR("Cannot create a string field class object."); | |
e2531d3b PP |
99 | goto error; |
100 | } | |
101 | ||
78cf9df6 | 102 | ret = bt_field_class_structure_append_member(root_fc, |
9e550e5f | 103 | "str", fc); |
e2531d3b | 104 | if (ret) { |
939190b3 | 105 | BT_LOGE("Cannot add `str` member to structure field class: " |
e2531d3b PP |
106 | "ret=%d", ret); |
107 | goto error; | |
108 | } | |
109 | ||
110 | goto end; | |
111 | ||
112 | error: | |
8c6884d9 | 113 | BT_FIELD_CLASS_PUT_REF_AND_RESET(root_fc); |
e2531d3b PP |
114 | |
115 | end: | |
8c6884d9 | 116 | bt_field_class_put_ref(fc); |
939190b3 | 117 | return root_fc; |
e2531d3b PP |
118 | } |
119 | ||
e2531d3b PP |
120 | static |
121 | int create_meta(struct dmesg_component *dmesg_comp, bool has_ts) | |
122 | { | |
8eee8ea2 | 123 | bt_field_class *fc = NULL; |
d8866baa PP |
124 | int ret = 0; |
125 | ||
e5e71bf8 PP |
126 | dmesg_comp->trace_class = bt_trace_class_create( |
127 | bt_self_component_source_as_self_component( | |
128 | dmesg_comp->self_comp)); | |
10b7a2e4 PP |
129 | if (!dmesg_comp->trace_class) { |
130 | BT_LOGE_STR("Cannot create an empty trace class object."); | |
e2531d3b PP |
131 | goto error; |
132 | } | |
133 | ||
78cf9df6 | 134 | dmesg_comp->stream_class = bt_stream_class_create( |
10b7a2e4 | 135 | dmesg_comp->trace_class); |
e2531d3b | 136 | if (!dmesg_comp->stream_class) { |
7b33a0e0 | 137 | BT_LOGE_STR("Cannot create a stream class object."); |
e2531d3b PP |
138 | goto error; |
139 | } | |
140 | ||
e2531d3b | 141 | if (has_ts) { |
35fa110e PP |
142 | dmesg_comp->clock_class = bt_clock_class_create( |
143 | dmesg_comp->trace_class); | |
e2531d3b PP |
144 | if (!dmesg_comp->clock_class) { |
145 | BT_LOGE_STR("Cannot create clock class."); | |
146 | goto error; | |
147 | } | |
148 | ||
78cf9df6 PP |
149 | ret = bt_stream_class_set_default_clock_class( |
150 | dmesg_comp->stream_class, dmesg_comp->clock_class); | |
e2531d3b | 151 | if (ret) { |
7b33a0e0 | 152 | BT_LOGE_STR("Cannot set stream class's default clock class."); |
e2531d3b PP |
153 | goto error; |
154 | } | |
155 | } | |
156 | ||
78cf9df6 | 157 | dmesg_comp->event_class = bt_event_class_create( |
7b33a0e0 | 158 | dmesg_comp->stream_class); |
e2531d3b | 159 | if (!dmesg_comp->event_class) { |
7b33a0e0 PP |
160 | BT_LOGE_STR("Cannot create an event class object."); |
161 | goto error; | |
162 | } | |
163 | ||
78cf9df6 | 164 | ret = bt_event_class_set_name(dmesg_comp->event_class, "string"); |
7b33a0e0 PP |
165 | if (ret) { |
166 | BT_LOGE_STR("Cannot set event class's name."); | |
e2531d3b PP |
167 | goto error; |
168 | } | |
169 | ||
b7396828 | 170 | fc = create_event_payload_fc(dmesg_comp->trace_class); |
939190b3 PP |
171 | if (!fc) { |
172 | BT_LOGE_STR("Cannot create event payload field class."); | |
d8866baa PP |
173 | goto error; |
174 | } | |
175 | ||
78cf9df6 | 176 | ret = bt_event_class_set_payload_field_class(dmesg_comp->event_class, fc); |
e2531d3b | 177 | if (ret) { |
939190b3 | 178 | BT_LOGE_STR("Cannot set event class's event payload field class."); |
e2531d3b PP |
179 | goto error; |
180 | } | |
181 | ||
e2531d3b PP |
182 | goto end; |
183 | ||
184 | error: | |
185 | ret = -1; | |
186 | ||
187 | end: | |
8c6884d9 | 188 | bt_field_class_put_ref(fc); |
e2531d3b PP |
189 | return ret; |
190 | } | |
191 | ||
192 | static | |
ce141536 | 193 | int handle_params(struct dmesg_component *dmesg_comp, |
8eee8ea2 | 194 | const bt_value *params) |
e2531d3b | 195 | { |
8eee8ea2 PP |
196 | const bt_value *no_timestamp = NULL; |
197 | const bt_value *path = NULL; | |
e2531d3b PP |
198 | const char *path_str; |
199 | int ret = 0; | |
200 | ||
ce141536 | 201 | no_timestamp = bt_value_map_borrow_entry_value_const(params, |
44514773 | 202 | "no-extract-timestamp"); |
8743317e PP |
203 | if (no_timestamp) { |
204 | if (!bt_value_is_bool(no_timestamp)) { | |
205 | BT_LOGE("Expecting a boolean value for the `no-extract-timestamp` parameter: " | |
206 | "type=%s", | |
17582c6d | 207 | bt_common_value_type_string( |
8743317e PP |
208 | bt_value_get_type(no_timestamp))); |
209 | goto error; | |
210 | } | |
211 | ||
b5cdc106 PP |
212 | dmesg_comp->params.no_timestamp = |
213 | bt_value_bool_get(no_timestamp); | |
8743317e PP |
214 | } |
215 | ||
ce141536 | 216 | path = bt_value_map_borrow_entry_value_const(params, "path"); |
d8866baa PP |
217 | if (path) { |
218 | if (dmesg_comp->params.read_from_stdin) { | |
e2531d3b | 219 | BT_LOGE_STR("Cannot specify both `read-from-stdin` and `path` parameters."); |
d8866baa PP |
220 | goto error; |
221 | } | |
222 | ||
223 | if (!bt_value_is_string(path)) { | |
e2531d3b PP |
224 | BT_LOGE("Expecting a string value for the `path` parameter: " |
225 | "type=%s", | |
17582c6d | 226 | bt_common_value_type_string( |
e2531d3b | 227 | bt_value_get_type(path))); |
d8866baa PP |
228 | goto error; |
229 | } | |
230 | ||
b5cdc106 | 231 | path_str = bt_value_string_get(path); |
d8866baa PP |
232 | g_string_assign(dmesg_comp->params.path, path_str); |
233 | } else { | |
2a2f36a2 | 234 | dmesg_comp->params.read_from_stdin = true; |
d8866baa PP |
235 | } |
236 | ||
237 | goto end; | |
238 | ||
239 | error: | |
240 | ret = -1; | |
241 | ||
242 | end: | |
d8866baa PP |
243 | return ret; |
244 | } | |
245 | ||
e2531d3b | 246 | static |
10b7a2e4 | 247 | int create_packet_and_stream_and_trace(struct dmesg_component *dmesg_comp) |
e2531d3b PP |
248 | { |
249 | int ret = 0; | |
a260020d | 250 | const char *trace_name = NULL; |
10b7a2e4 PP |
251 | gchar *basename = NULL; |
252 | ||
253 | dmesg_comp->trace = bt_trace_create(dmesg_comp->trace_class); | |
254 | if (!dmesg_comp->trace) { | |
255 | BT_LOGE_STR("Cannot create trace object."); | |
256 | goto error; | |
257 | } | |
e2531d3b | 258 | |
10b7a2e4 PP |
259 | if (dmesg_comp->params.read_from_stdin) { |
260 | trace_name = "STDIN"; | |
261 | } else { | |
262 | basename = g_path_get_basename(dmesg_comp->params.path->str); | |
263 | BT_ASSERT(basename); | |
264 | ||
265 | if (strcmp(basename, G_DIR_SEPARATOR_S) != 0 && | |
266 | strcmp(basename, ".") != 0) { | |
267 | trace_name = basename; | |
268 | } | |
269 | } | |
270 | ||
271 | if (trace_name) { | |
272 | ret = bt_trace_set_name(dmesg_comp->trace, trace_name); | |
273 | if (ret) { | |
274 | BT_LOGE("Cannot set trace's name: name=\"%s\"", | |
275 | trace_name); | |
276 | goto error; | |
277 | } | |
278 | } | |
279 | ||
280 | dmesg_comp->stream = bt_stream_create(dmesg_comp->stream_class, | |
281 | dmesg_comp->trace); | |
e2531d3b PP |
282 | if (!dmesg_comp->stream) { |
283 | BT_LOGE_STR("Cannot create stream object."); | |
284 | goto error; | |
285 | } | |
286 | ||
78cf9df6 | 287 | dmesg_comp->packet = bt_packet_create(dmesg_comp->stream); |
e2531d3b PP |
288 | if (!dmesg_comp->packet) { |
289 | BT_LOGE_STR("Cannot create packet object."); | |
290 | goto error; | |
291 | } | |
292 | ||
78cf9df6 | 293 | ret = bt_trace_make_static(dmesg_comp->trace); |
e2531d3b PP |
294 | if (ret) { |
295 | BT_LOGE_STR("Cannot make trace static."); | |
296 | goto error; | |
297 | } | |
298 | ||
299 | goto end; | |
300 | ||
301 | error: | |
302 | ret = -1; | |
303 | ||
304 | end: | |
10b7a2e4 PP |
305 | if (basename) { |
306 | g_free(basename); | |
307 | } | |
308 | ||
e2531d3b PP |
309 | return ret; |
310 | } | |
311 | ||
312 | static | |
313 | int try_create_meta_stream_packet(struct dmesg_component *dmesg_comp, | |
314 | bool has_ts) | |
315 | { | |
316 | int ret = 0; | |
317 | ||
318 | if (dmesg_comp->trace) { | |
319 | /* Already created */ | |
320 | goto end; | |
321 | } | |
322 | ||
323 | ret = create_meta(dmesg_comp, has_ts); | |
324 | if (ret) { | |
325 | BT_LOGE("Cannot create metadata objects: dmesg-comp-addr=%p", | |
326 | dmesg_comp); | |
327 | goto error; | |
328 | } | |
329 | ||
10b7a2e4 | 330 | ret = create_packet_and_stream_and_trace(dmesg_comp); |
e2531d3b PP |
331 | if (ret) { |
332 | BT_LOGE("Cannot create packet and stream objects: " | |
333 | "dmesg-comp-addr=%p", dmesg_comp); | |
334 | goto error; | |
335 | } | |
336 | ||
337 | goto end; | |
338 | ||
339 | error: | |
340 | ret = -1; | |
341 | ||
342 | end: | |
343 | return ret; | |
344 | } | |
d8866baa PP |
345 | |
346 | static | |
347 | void destroy_dmesg_component(struct dmesg_component *dmesg_comp) | |
348 | { | |
349 | if (!dmesg_comp) { | |
350 | return; | |
351 | } | |
352 | ||
353 | if (dmesg_comp->params.path) { | |
354 | g_string_free(dmesg_comp->params.path, TRUE); | |
355 | } | |
356 | ||
8c6884d9 PP |
357 | bt_packet_put_ref(dmesg_comp->packet); |
358 | bt_trace_put_ref(dmesg_comp->trace); | |
359 | bt_stream_class_put_ref(dmesg_comp->stream_class); | |
360 | bt_event_class_put_ref(dmesg_comp->event_class); | |
361 | bt_stream_put_ref(dmesg_comp->stream); | |
362 | bt_clock_class_put_ref(dmesg_comp->clock_class); | |
d8866baa PP |
363 | g_free(dmesg_comp); |
364 | } | |
365 | ||
e2531d3b | 366 | static |
ee78f405 | 367 | bt_self_component_status create_port( |
8eee8ea2 | 368 | bt_self_component_source *self_comp) |
e2531d3b | 369 | { |
834e9996 | 370 | return bt_self_component_source_add_output_port(self_comp, |
e2531d3b PP |
371 | "out", NULL, NULL); |
372 | } | |
373 | ||
d8866baa | 374 | BT_HIDDEN |
ee78f405 | 375 | bt_self_component_status dmesg_init( |
8eee8ea2 PP |
376 | bt_self_component_source *self_comp, |
377 | bt_value *params, void *init_method_data) | |
d8866baa PP |
378 | { |
379 | int ret = 0; | |
380 | struct dmesg_component *dmesg_comp = g_new0(struct dmesg_component, 1); | |
ee78f405 | 381 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
d8866baa PP |
382 | |
383 | if (!dmesg_comp) { | |
e2531d3b | 384 | BT_LOGE_STR("Failed to allocate one dmesg component structure."); |
d8866baa PP |
385 | goto error; |
386 | } | |
387 | ||
e5e71bf8 | 388 | dmesg_comp->self_comp = self_comp; |
d8866baa PP |
389 | dmesg_comp->params.path = g_string_new(NULL); |
390 | if (!dmesg_comp->params.path) { | |
e2531d3b | 391 | BT_LOGE_STR("Failed to allocate a GString."); |
d8866baa PP |
392 | goto error; |
393 | } | |
394 | ||
e2531d3b | 395 | ret = handle_params(dmesg_comp, params); |
d8866baa | 396 | if (ret) { |
834e9996 | 397 | BT_LOGE("Invalid parameters: comp-addr=%p", self_comp); |
d8866baa PP |
398 | goto error; |
399 | } | |
400 | ||
e2531d3b PP |
401 | if (!dmesg_comp->params.read_from_stdin && |
402 | !g_file_test(dmesg_comp->params.path->str, | |
403 | G_FILE_TEST_IS_REGULAR)) { | |
404 | BT_LOGE("Input path is not a regular file: " | |
834e9996 | 405 | "comp-addr=%p, path=\"%s\"", self_comp, |
e2531d3b PP |
406 | dmesg_comp->params.path->str); |
407 | goto error; | |
408 | } | |
d8866baa | 409 | |
834e9996 PP |
410 | status = create_port(self_comp); |
411 | if (status != BT_SELF_COMPONENT_STATUS_OK) { | |
e2531d3b PP |
412 | goto error; |
413 | } | |
d8866baa | 414 | |
834e9996 | 415 | bt_self_component_set_data( |
bb61965b | 416 | bt_self_component_source_as_self_component(self_comp), |
834e9996 | 417 | dmesg_comp); |
d8866baa PP |
418 | goto end; |
419 | ||
420 | error: | |
421 | destroy_dmesg_component(dmesg_comp); | |
834e9996 | 422 | bt_self_component_set_data( |
bb61965b | 423 | bt_self_component_source_as_self_component(self_comp), |
834e9996 | 424 | NULL); |
e2531d3b PP |
425 | |
426 | if (status >= 0) { | |
834e9996 | 427 | status = BT_SELF_COMPONENT_STATUS_ERROR; |
e2531d3b | 428 | } |
d8866baa PP |
429 | |
430 | end: | |
431 | return status; | |
432 | } | |
433 | ||
434 | BT_HIDDEN | |
8eee8ea2 | 435 | void dmesg_finalize(bt_self_component_source *self_comp) |
d8866baa | 436 | { |
834e9996 | 437 | destroy_dmesg_component(bt_self_component_get_data( |
bb61965b | 438 | bt_self_component_source_as_self_component(self_comp))); |
e2531d3b PP |
439 | } |
440 | ||
441 | static | |
b09a5592 PP |
442 | bt_message *create_init_event_msg_from_line( |
443 | struct dmesg_msg_iter *msg_iter, | |
a6918753 | 444 | const char *line, const char **new_start) |
e2531d3b | 445 | { |
8eee8ea2 | 446 | bt_event *event; |
b09a5592 | 447 | bt_message *msg = NULL; |
e2531d3b PP |
448 | bool has_timestamp = false; |
449 | unsigned long sec, usec, msec; | |
450 | unsigned int year, mon, mday, hour, min; | |
451 | uint64_t ts = 0; | |
e2531d3b | 452 | int ret = 0; |
b09a5592 | 453 | struct dmesg_component *dmesg_comp = msg_iter->dmesg_comp; |
e2531d3b | 454 | |
e2531d3b PP |
455 | *new_start = line; |
456 | ||
8743317e PP |
457 | if (dmesg_comp->params.no_timestamp) { |
458 | goto skip_ts; | |
459 | } | |
460 | ||
e2531d3b PP |
461 | /* Extract time from input line */ |
462 | if (sscanf(line, "[%lu.%lu] ", &sec, &usec) == 2) { | |
463 | ts = (uint64_t) sec * USEC_PER_SEC + (uint64_t) usec; | |
464 | ||
465 | /* | |
466 | * The clock class we use has a 1 GHz frequency: convert | |
467 | * from µs to ns. | |
468 | */ | |
469 | ts *= NSEC_PER_USEC; | |
470 | has_timestamp = true; | |
471 | } else if (sscanf(line, "[%u-%u-%u %u:%u:%lu.%lu] ", | |
472 | &year, &mon, &mday, &hour, &min, | |
473 | &sec, &msec) == 7) { | |
474 | time_t ep_sec; | |
475 | struct tm ti; | |
476 | ||
477 | memset(&ti, 0, sizeof(ti)); | |
478 | ti.tm_year = year - 1900; /* From 1900 */ | |
479 | ti.tm_mon = mon - 1; /* 0 to 11 */ | |
480 | ti.tm_mday = mday; | |
481 | ti.tm_hour = hour; | |
482 | ti.tm_min = min; | |
483 | ti.tm_sec = sec; | |
484 | ||
485 | ep_sec = bt_timegm(&ti); | |
486 | if (ep_sec != (time_t) -1) { | |
487 | ts = (uint64_t) ep_sec * NSEC_PER_SEC | |
488 | + (uint64_t) msec * NSEC_PER_MSEC; | |
489 | } | |
490 | ||
491 | has_timestamp = true; | |
492 | } | |
493 | ||
494 | if (has_timestamp) { | |
495 | /* Set new start for the message portion of the line */ | |
496 | *new_start = strchr(line, ']'); | |
8b45963b | 497 | BT_ASSERT(*new_start); |
e2531d3b PP |
498 | (*new_start)++; |
499 | ||
500 | if ((*new_start)[0] == ' ') { | |
501 | (*new_start)++; | |
502 | } | |
503 | } | |
504 | ||
8743317e | 505 | skip_ts: |
e2531d3b PP |
506 | /* |
507 | * At this point, we know if the stream class's event header | |
939190b3 | 508 | * field class should have a timestamp or not, so we can lazily |
e2531d3b PP |
509 | * create the metadata, stream, and packet objects. |
510 | */ | |
511 | ret = try_create_meta_stream_packet(dmesg_comp, has_timestamp); | |
512 | if (ret) { | |
513 | /* try_create_meta_stream_packet() logs errors */ | |
514 | goto error; | |
515 | } | |
516 | ||
b09a5592 | 517 | msg = bt_message_event_create(msg_iter->pc_msg_iter, |
8fc063a2 | 518 | dmesg_comp->event_class, dmesg_comp->packet); |
b09a5592 PP |
519 | if (!msg) { |
520 | BT_LOGE_STR("Cannot create event message."); | |
a6918753 PP |
521 | goto error; |
522 | } | |
e2531d3b | 523 | |
b09a5592 | 524 | event = bt_message_event_borrow_event(msg); |
a6918753 | 525 | BT_ASSERT(event); |
e2531d3b | 526 | |
a6918753 | 527 | if (dmesg_comp->clock_class) { |
ecbb78c0 | 528 | bt_event_set_default_clock_snapshot(event, ts); |
e2531d3b PP |
529 | } |
530 | ||
531 | goto end; | |
532 | ||
533 | error: | |
b09a5592 | 534 | BT_MESSAGE_PUT_REF_AND_RESET(msg); |
e2531d3b PP |
535 | |
536 | end: | |
b09a5592 | 537 | return msg; |
e2531d3b PP |
538 | } |
539 | ||
540 | static | |
9e550e5f | 541 | int fill_event_payload_from_line(const char *line, |
8eee8ea2 | 542 | bt_event *event) |
e2531d3b | 543 | { |
8eee8ea2 PP |
544 | bt_field *ep_field = NULL; |
545 | bt_field *str_field = NULL; | |
e2531d3b PP |
546 | size_t len; |
547 | int ret; | |
548 | ||
78cf9df6 | 549 | ep_field = bt_event_borrow_payload_field(event); |
a6918753 | 550 | BT_ASSERT(ep_field); |
78cf9df6 | 551 | str_field = bt_field_structure_borrow_member_field_by_index( |
9e550e5f | 552 | ep_field, 0); |
e2531d3b | 553 | if (!str_field) { |
a6918753 | 554 | BT_LOGE_STR("Cannot borrow `timestamp` field from event payload structure field."); |
e2531d3b PP |
555 | goto error; |
556 | } | |
557 | ||
558 | len = strlen(line); | |
559 | if (line[len - 1] == '\n') { | |
560 | /* Do not include the newline character in the payload */ | |
561 | len--; | |
562 | } | |
563 | ||
78cf9df6 | 564 | ret = bt_field_string_clear(str_field); |
a6918753 PP |
565 | if (ret) { |
566 | BT_LOGE_STR("Cannot clear string field object."); | |
567 | goto error; | |
568 | } | |
569 | ||
78cf9df6 | 570 | ret = bt_field_string_append_with_length(str_field, line, len); |
e2531d3b PP |
571 | if (ret) { |
572 | BT_LOGE("Cannot append value to string field object: " | |
573 | "len=%zu", len); | |
574 | goto error; | |
575 | } | |
576 | ||
e2531d3b PP |
577 | goto end; |
578 | ||
579 | error: | |
580 | ret = -1; | |
581 | ||
582 | end: | |
e2531d3b PP |
583 | return ret; |
584 | } | |
585 | ||
586 | static | |
b09a5592 PP |
587 | bt_message *create_msg_from_line( |
588 | struct dmesg_msg_iter *dmesg_msg_iter, const char *line) | |
e2531d3b | 589 | { |
8eee8ea2 | 590 | bt_event *event = NULL; |
b09a5592 | 591 | bt_message *msg = NULL; |
e2531d3b PP |
592 | const char *new_start; |
593 | int ret; | |
594 | ||
b09a5592 | 595 | msg = create_init_event_msg_from_line(dmesg_msg_iter, |
03e18d5d | 596 | line, &new_start); |
b09a5592 PP |
597 | if (!msg) { |
598 | BT_LOGE_STR("Cannot create and initialize event message from line."); | |
e2531d3b PP |
599 | goto error; |
600 | } | |
601 | ||
b09a5592 | 602 | event = bt_message_event_borrow_event(msg); |
a6918753 | 603 | BT_ASSERT(event); |
03e18d5d | 604 | ret = fill_event_payload_from_line(new_start, event); |
e2531d3b | 605 | if (ret) { |
a6918753 | 606 | BT_LOGE("Cannot fill event payload field from line: " |
e2531d3b PP |
607 | "ret=%d", ret); |
608 | goto error; | |
609 | } | |
610 | ||
e2531d3b PP |
611 | goto end; |
612 | ||
613 | error: | |
b09a5592 | 614 | BT_MESSAGE_PUT_REF_AND_RESET(msg); |
e2531d3b PP |
615 | |
616 | end: | |
b09a5592 | 617 | return msg; |
e2531d3b PP |
618 | } |
619 | ||
620 | static | |
b09a5592 | 621 | void destroy_dmesg_msg_iter(struct dmesg_msg_iter *dmesg_msg_iter) |
e2531d3b | 622 | { |
b09a5592 | 623 | if (!dmesg_msg_iter) { |
e2531d3b PP |
624 | return; |
625 | } | |
626 | ||
b09a5592 PP |
627 | if (dmesg_msg_iter->fp && dmesg_msg_iter->fp != stdin) { |
628 | if (fclose(dmesg_msg_iter->fp)) { | |
e2531d3b PP |
629 | BT_LOGE_ERRNO("Cannot close input file", "."); |
630 | } | |
631 | } | |
d8866baa | 632 | |
b09a5592 PP |
633 | bt_message_put_ref(dmesg_msg_iter->tmp_event_msg); |
634 | free(dmesg_msg_iter->linebuf); | |
635 | g_free(dmesg_msg_iter); | |
d8866baa PP |
636 | } |
637 | ||
638 | BT_HIDDEN | |
ee78f405 | 639 | bt_self_message_iterator_status dmesg_msg_iter_init( |
b09a5592 | 640 | bt_self_message_iterator *self_msg_iter, |
8eee8ea2 PP |
641 | bt_self_component_source *self_comp, |
642 | bt_self_component_port_output *self_port) | |
d8866baa | 643 | { |
e2531d3b | 644 | struct dmesg_component *dmesg_comp; |
b09a5592 PP |
645 | struct dmesg_msg_iter *dmesg_msg_iter = |
646 | g_new0(struct dmesg_msg_iter, 1); | |
ee78f405 | 647 | bt_self_message_iterator_status status = |
b09a5592 | 648 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
e2531d3b | 649 | |
b09a5592 PP |
650 | if (!dmesg_msg_iter) { |
651 | BT_LOGE_STR("Failed to allocate on dmesg message iterator structure."); | |
e2531d3b PP |
652 | goto error; |
653 | } | |
654 | ||
834e9996 | 655 | dmesg_comp = bt_self_component_get_data( |
bb61965b | 656 | bt_self_component_source_as_self_component(self_comp)); |
8b45963b | 657 | BT_ASSERT(dmesg_comp); |
b09a5592 PP |
658 | dmesg_msg_iter->dmesg_comp = dmesg_comp; |
659 | dmesg_msg_iter->pc_msg_iter = self_msg_iter; | |
d8866baa | 660 | |
e2531d3b | 661 | if (dmesg_comp->params.read_from_stdin) { |
b09a5592 | 662 | dmesg_msg_iter->fp = stdin; |
e2531d3b | 663 | } else { |
b09a5592 PP |
664 | dmesg_msg_iter->fp = fopen(dmesg_comp->params.path->str, "r"); |
665 | if (!dmesg_msg_iter->fp) { | |
e2531d3b PP |
666 | BT_LOGE_ERRNO("Cannot open input file in read mode", ": path=\"%s\"", |
667 | dmesg_comp->params.path->str); | |
668 | goto error; | |
669 | } | |
670 | } | |
671 | ||
b09a5592 PP |
672 | bt_self_message_iterator_set_data(self_msg_iter, |
673 | dmesg_msg_iter); | |
e2531d3b PP |
674 | goto end; |
675 | ||
676 | error: | |
b09a5592 PP |
677 | destroy_dmesg_msg_iter(dmesg_msg_iter); |
678 | bt_self_message_iterator_set_data(self_msg_iter, NULL); | |
e2531d3b | 679 | if (status >= 0) { |
b09a5592 | 680 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
e2531d3b PP |
681 | } |
682 | ||
683 | end: | |
e2531d3b | 684 | return status; |
d8866baa PP |
685 | } |
686 | ||
687 | BT_HIDDEN | |
b09a5592 PP |
688 | void dmesg_msg_iter_finalize( |
689 | bt_self_message_iterator *priv_msg_iter) | |
d8866baa | 690 | { |
b09a5592 PP |
691 | destroy_dmesg_msg_iter(bt_self_message_iterator_get_data( |
692 | priv_msg_iter)); | |
d8866baa PP |
693 | } |
694 | ||
3fd7b79d | 695 | static |
ee78f405 | 696 | bt_self_message_iterator_status dmesg_msg_iter_next_one( |
b09a5592 PP |
697 | struct dmesg_msg_iter *dmesg_msg_iter, |
698 | bt_message **msg) | |
d8866baa | 699 | { |
e2531d3b | 700 | ssize_t len; |
e2531d3b | 701 | struct dmesg_component *dmesg_comp; |
ee78f405 | 702 | bt_self_message_iterator_status status = |
b09a5592 | 703 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
d8866baa | 704 | |
b09a5592 PP |
705 | BT_ASSERT(dmesg_msg_iter); |
706 | dmesg_comp = dmesg_msg_iter->dmesg_comp; | |
8b45963b | 707 | BT_ASSERT(dmesg_comp); |
e2531d3b | 708 | |
b09a5592 PP |
709 | if (dmesg_msg_iter->state == STATE_DONE) { |
710 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; | |
a6918753 PP |
711 | goto end; |
712 | } | |
713 | ||
b09a5592 PP |
714 | if (dmesg_msg_iter->tmp_event_msg || |
715 | dmesg_msg_iter->state == STATE_EMIT_PACKET_END || | |
66ff4085 | 716 | dmesg_msg_iter->state == STATE_EMIT_STREAM_ACTIVITY_END || |
b09a5592 | 717 | dmesg_msg_iter->state == STATE_EMIT_STREAM_END) { |
a6918753 PP |
718 | goto handle_state; |
719 | } | |
720 | ||
e2531d3b PP |
721 | while (true) { |
722 | const char *ch; | |
723 | bool only_spaces = true; | |
724 | ||
b09a5592 PP |
725 | len = bt_getline(&dmesg_msg_iter->linebuf, |
726 | &dmesg_msg_iter->linebuf_len, dmesg_msg_iter->fp); | |
e2531d3b PP |
727 | if (len < 0) { |
728 | if (errno == EINVAL) { | |
b09a5592 | 729 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
e2531d3b | 730 | } else if (errno == ENOMEM) { |
3fd7b79d | 731 | status = |
b09a5592 | 732 | BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM; |
e2531d3b | 733 | } else { |
b09a5592 | 734 | if (dmesg_msg_iter->state == STATE_EMIT_STREAM_BEGINNING) { |
a6918753 | 735 | /* Stream did not even begin */ |
b09a5592 | 736 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; |
a6918753 PP |
737 | goto end; |
738 | } else { | |
739 | /* End current packet now */ | |
b09a5592 | 740 | dmesg_msg_iter->state = |
a6918753 PP |
741 | STATE_EMIT_PACKET_END; |
742 | goto handle_state; | |
743 | } | |
e2531d3b PP |
744 | } |
745 | ||
746 | goto end; | |
747 | } | |
748 | ||
b09a5592 | 749 | BT_ASSERT(dmesg_msg_iter->linebuf); |
e2531d3b PP |
750 | |
751 | /* Ignore empty lines, once trimmed */ | |
b09a5592 | 752 | for (ch = dmesg_msg_iter->linebuf; *ch != '\0'; ch++) { |
e2531d3b PP |
753 | if (!isspace(*ch)) { |
754 | only_spaces = false; | |
755 | break; | |
756 | } | |
757 | } | |
758 | ||
759 | if (!only_spaces) { | |
760 | break; | |
761 | } | |
762 | } | |
763 | ||
b09a5592 PP |
764 | dmesg_msg_iter->tmp_event_msg = create_msg_from_line( |
765 | dmesg_msg_iter, dmesg_msg_iter->linebuf); | |
766 | if (!dmesg_msg_iter->tmp_event_msg) { | |
767 | BT_LOGE("Cannot create event message from line: " | |
e2531d3b | 768 | "dmesg-comp-addr=%p, line=\"%s\"", dmesg_comp, |
b09a5592 | 769 | dmesg_msg_iter->linebuf); |
a6918753 PP |
770 | goto end; |
771 | } | |
772 | ||
773 | handle_state: | |
774 | BT_ASSERT(dmesg_comp->trace); | |
775 | ||
b09a5592 | 776 | switch (dmesg_msg_iter->state) { |
a6918753 | 777 | case STATE_EMIT_STREAM_BEGINNING: |
b09a5592 PP |
778 | BT_ASSERT(dmesg_msg_iter->tmp_event_msg); |
779 | *msg = bt_message_stream_beginning_create( | |
780 | dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream); | |
66ff4085 PP |
781 | dmesg_msg_iter->state = STATE_EMIT_STREAM_ACTIVITY_BEGINNING; |
782 | break; | |
783 | case STATE_EMIT_STREAM_ACTIVITY_BEGINNING: | |
784 | BT_ASSERT(dmesg_msg_iter->tmp_event_msg); | |
785 | *msg = bt_message_stream_activity_beginning_create( | |
786 | dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream); | |
b09a5592 | 787 | dmesg_msg_iter->state = STATE_EMIT_PACKET_BEGINNING; |
a6918753 PP |
788 | break; |
789 | case STATE_EMIT_PACKET_BEGINNING: | |
b09a5592 PP |
790 | BT_ASSERT(dmesg_msg_iter->tmp_event_msg); |
791 | *msg = bt_message_packet_beginning_create( | |
792 | dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet); | |
793 | dmesg_msg_iter->state = STATE_EMIT_EVENT; | |
a6918753 PP |
794 | break; |
795 | case STATE_EMIT_EVENT: | |
b09a5592 PP |
796 | BT_ASSERT(dmesg_msg_iter->tmp_event_msg); |
797 | *msg = dmesg_msg_iter->tmp_event_msg; | |
798 | dmesg_msg_iter->tmp_event_msg = NULL; | |
a6918753 PP |
799 | break; |
800 | case STATE_EMIT_PACKET_END: | |
b09a5592 PP |
801 | *msg = bt_message_packet_end_create( |
802 | dmesg_msg_iter->pc_msg_iter, dmesg_comp->packet); | |
66ff4085 PP |
803 | dmesg_msg_iter->state = STATE_EMIT_STREAM_ACTIVITY_END; |
804 | break; | |
805 | case STATE_EMIT_STREAM_ACTIVITY_END: | |
806 | *msg = bt_message_stream_activity_end_create( | |
807 | dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream); | |
b09a5592 | 808 | dmesg_msg_iter->state = STATE_EMIT_STREAM_END; |
a6918753 PP |
809 | break; |
810 | case STATE_EMIT_STREAM_END: | |
b09a5592 PP |
811 | *msg = bt_message_stream_end_create( |
812 | dmesg_msg_iter->pc_msg_iter, dmesg_comp->stream); | |
813 | dmesg_msg_iter->state = STATE_DONE; | |
a6918753 PP |
814 | break; |
815 | default: | |
816 | break; | |
817 | } | |
818 | ||
b09a5592 PP |
819 | if (!*msg) { |
820 | BT_LOGE("Cannot create message: dmesg-comp-addr=%p", | |
a6918753 | 821 | dmesg_comp); |
b09a5592 | 822 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
e2531d3b PP |
823 | } |
824 | ||
825 | end: | |
3fd7b79d PP |
826 | return status; |
827 | } | |
828 | ||
829 | BT_HIDDEN | |
ee78f405 | 830 | bt_self_message_iterator_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); | |
ee78f405 | 838 | bt_self_message_iterator_status status = |
b09a5592 | 839 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
3fd7b79d PP |
840 | uint64_t i = 0; |
841 | ||
834e9996 | 842 | while (i < capacity && |
b09a5592 PP |
843 | status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
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; | |
849 | if (status == BT_SELF_MESSAGE_ITERATOR_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; | |
b09a5592 | 868 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
3fd7b79d PP |
869 | } |
870 | ||
871 | return status; | |
d8866baa | 872 | } |