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