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