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