Commit | Line | Data |
---|---|---|
46bdd3e0 PP |
1 | /* |
2 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
23 | #define BT_LOG_TAG "PLUGIN-CTF-FS-SINK-TRANSLATE-TRACE-IR-TO-CTF-IR" | |
24 | #include "logging.h" | |
25 | ||
26 | #include <babeltrace/babeltrace.h> | |
27 | #include <babeltrace/babeltrace-internal.h> | |
28 | #include <babeltrace/common-internal.h> | |
29 | #include <babeltrace/assert-internal.h> | |
30 | #include <stdio.h> | |
31 | #include <stdbool.h> | |
32 | #include <string.h> | |
33 | #include <glib.h> | |
34 | ||
35 | #include "fs-sink-ctf-meta.h" | |
36 | ||
37 | struct field_path_elem { | |
38 | uint64_t index_in_parent; | |
39 | GString *name; | |
40 | ||
41 | /* Weak */ | |
42 | const bt_field_class *ir_fc; | |
43 | ||
44 | /* Weak */ | |
45 | struct fs_sink_ctf_field_class *parent_fc; | |
46 | }; | |
47 | ||
48 | struct ctx { | |
49 | /* Weak */ | |
50 | struct fs_sink_ctf_stream_class *cur_sc; | |
51 | ||
52 | /* Weak */ | |
53 | struct fs_sink_ctf_event_class *cur_ec; | |
54 | ||
55 | bt_scope cur_scope; | |
56 | ||
57 | /* | |
58 | * Array of `struct field_path_elem` */ | |
59 | GArray *cur_path; | |
60 | }; | |
61 | ||
62 | static inline | |
63 | struct field_path_elem *cur_path_stack_at(struct ctx *ctx, uint64_t i) | |
64 | { | |
65 | BT_ASSERT(i < ctx->cur_path->len); | |
66 | return &g_array_index(ctx->cur_path, struct field_path_elem, i); | |
67 | } | |
68 | ||
69 | static inline | |
70 | struct field_path_elem *cur_path_stack_top(struct ctx *ctx) | |
71 | { | |
72 | BT_ASSERT(ctx->cur_path->len > 0); | |
73 | return cur_path_stack_at(ctx, ctx->cur_path->len - 1); | |
74 | } | |
75 | ||
76 | static inline | |
77 | bool is_reserved_member_name(const char *name, const char *reserved_name) | |
78 | { | |
79 | bool is_reserved = false; | |
80 | ||
81 | if (strcmp(name, reserved_name) == 0) { | |
82 | is_reserved = true; | |
83 | goto end; | |
84 | } | |
85 | ||
86 | if (name[0] == '_' && strcmp(&name[1], reserved_name) == 0) { | |
87 | is_reserved = true; | |
88 | goto end; | |
89 | } | |
90 | ||
91 | end: | |
92 | return is_reserved; | |
93 | } | |
94 | ||
95 | static inline | |
96 | int cur_path_stack_push(struct ctx *ctx, | |
97 | uint64_t index_in_parent, const char *ir_name, | |
98 | const bt_field_class *ir_fc, | |
99 | struct fs_sink_ctf_field_class *parent_fc) | |
100 | { | |
101 | int ret = 0; | |
102 | struct field_path_elem *field_path_elem; | |
103 | ||
104 | g_array_set_size(ctx->cur_path, ctx->cur_path->len + 1); | |
105 | field_path_elem = cur_path_stack_top(ctx); | |
106 | field_path_elem->index_in_parent = index_in_parent; | |
107 | field_path_elem->name = g_string_new(ir_name); | |
108 | ||
109 | if (ir_name) { | |
110 | if (ctx->cur_scope == BT_SCOPE_PACKET_CONTEXT) { | |
111 | if (is_reserved_member_name(ir_name, "packet_size") || | |
112 | is_reserved_member_name(ir_name, "content_size") || | |
113 | is_reserved_member_name(ir_name, "timestamp_begin") || | |
114 | is_reserved_member_name(ir_name, "timestamp_end") || | |
115 | is_reserved_member_name(ir_name, "events_discarded") || | |
116 | is_reserved_member_name(ir_name, "packet_seq_num")) { | |
117 | BT_LOGE("Unsupported reserved TSDL structure field class member " | |
118 | "or variant field class option name: name=\"%s\"", | |
119 | ir_name); | |
120 | ret = -1; | |
121 | goto end; | |
122 | } | |
123 | } | |
124 | ||
125 | ret = fs_sink_ctf_protect_name(field_path_elem->name); | |
126 | if (ret) { | |
127 | BT_LOGE("Unsupported non-TSDL structure field class member " | |
128 | "or variant field class option name: name=\"%s\"", | |
129 | ir_name); | |
130 | goto end; | |
131 | } | |
132 | } | |
133 | ||
134 | field_path_elem->ir_fc = ir_fc; | |
135 | field_path_elem->parent_fc = parent_fc; | |
136 | ||
137 | end: | |
138 | return ret; | |
139 | } | |
140 | ||
141 | static inline | |
142 | void cur_path_stack_pop(struct ctx *ctx) | |
143 | { | |
144 | struct field_path_elem *field_path_elem; | |
145 | ||
146 | BT_ASSERT(ctx->cur_path->len > 0); | |
147 | field_path_elem = cur_path_stack_top(ctx); | |
148 | ||
149 | if (field_path_elem->name) { | |
150 | g_string_free(field_path_elem->name, TRUE); | |
151 | field_path_elem->name = NULL; | |
152 | } | |
153 | ||
154 | g_array_set_size(ctx->cur_path, ctx->cur_path->len - 1); | |
155 | } | |
156 | ||
46bdd3e0 PP |
157 | /* |
158 | * Creates a relative field ref (a single name) from IR field path | |
159 | * `tgt_ir_field_path`. | |
160 | * | |
161 | * This function tries to locate the target field class recursively from | |
162 | * the top to the bottom of the context's current path using only the | |
163 | * target field class's own name. This is because many CTF reading tools | |
164 | * do not support a relative field ref with more than one element, for | |
165 | * example `prev_struct.len`. | |
166 | * | |
167 | * Returns a negative value if this resolving operation failed. | |
168 | */ | |
169 | static | |
170 | int create_relative_field_ref(struct ctx *ctx, | |
171 | const bt_field_path *tgt_ir_field_path, GString *tgt_field_ref) | |
172 | { | |
173 | int ret = 0; | |
174 | struct fs_sink_ctf_field_class *tgt_fc = NULL; | |
175 | uint64_t i; | |
176 | int64_t si; | |
08cdcf01 | 177 | const char *tgt_fc_name = NULL; |
46bdd3e0 PP |
178 | struct field_path_elem *field_path_elem; |
179 | ||
180 | /* Get target field class's name */ | |
181 | switch (bt_field_path_get_root_scope(tgt_ir_field_path)) { | |
182 | case BT_SCOPE_PACKET_CONTEXT: | |
183 | BT_ASSERT(ctx->cur_sc); | |
184 | tgt_fc = ctx->cur_sc->packet_context_fc; | |
185 | break; | |
186 | case BT_SCOPE_EVENT_COMMON_CONTEXT: | |
187 | BT_ASSERT(ctx->cur_sc); | |
188 | tgt_fc = ctx->cur_sc->event_common_context_fc; | |
189 | break; | |
190 | case BT_SCOPE_EVENT_SPECIFIC_CONTEXT: | |
191 | BT_ASSERT(ctx->cur_ec); | |
192 | tgt_fc = ctx->cur_ec->spec_context_fc; | |
193 | break; | |
194 | case BT_SCOPE_EVENT_PAYLOAD: | |
195 | BT_ASSERT(ctx->cur_ec); | |
196 | tgt_fc = ctx->cur_ec->payload_fc; | |
197 | break; | |
198 | default: | |
199 | abort(); | |
200 | } | |
201 | ||
202 | i = 0; | |
203 | ||
1d4afa5f PP |
204 | while (i < bt_field_path_get_item_count(tgt_ir_field_path)) { |
205 | const bt_field_path_item *fp_item = | |
206 | bt_field_path_borrow_item_by_index_const( | |
207 | tgt_ir_field_path, i); | |
46bdd3e0 PP |
208 | struct fs_sink_ctf_named_field_class *named_fc = NULL; |
209 | ||
210 | BT_ASSERT(tgt_fc); | |
1d4afa5f | 211 | BT_ASSERT(fp_item); |
46bdd3e0 PP |
212 | |
213 | switch (tgt_fc->type) { | |
214 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
1d4afa5f PP |
215 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
216 | BT_FIELD_PATH_ITEM_TYPE_INDEX); | |
46bdd3e0 | 217 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
1d4afa5f PP |
218 | (void *) tgt_fc, |
219 | bt_field_path_item_index_get_index(fp_item)); | |
46bdd3e0 PP |
220 | break; |
221 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: | |
1d4afa5f PP |
222 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
223 | BT_FIELD_PATH_ITEM_TYPE_INDEX); | |
46bdd3e0 | 224 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
1d4afa5f PP |
225 | (void *) tgt_fc, |
226 | bt_field_path_item_index_get_index(fp_item)); | |
46bdd3e0 PP |
227 | break; |
228 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
229 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
230 | { | |
231 | struct fs_sink_ctf_field_class_array_base *array_base_fc = | |
232 | (void *) tgt_fc; | |
233 | ||
1d4afa5f PP |
234 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
235 | BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT); | |
46bdd3e0 PP |
236 | tgt_fc = array_base_fc->elem_fc; |
237 | break; | |
238 | } | |
239 | default: | |
240 | abort(); | |
241 | } | |
242 | ||
243 | if (named_fc) { | |
244 | tgt_fc = named_fc->fc; | |
245 | tgt_fc_name = named_fc->name->str; | |
246 | i++; | |
247 | } | |
248 | } | |
249 | ||
250 | BT_ASSERT(tgt_fc); | |
251 | BT_ASSERT(tgt_fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_INT); | |
252 | BT_ASSERT(tgt_fc_name); | |
253 | ||
254 | /* Find target field class having this name in current context */ | |
255 | for (si = ctx->cur_path->len - 1; si >= 0; si--) { | |
256 | struct fs_sink_ctf_field_class *fc; | |
257 | struct fs_sink_ctf_field_class_struct *struct_fc; | |
258 | struct fs_sink_ctf_field_class_variant *var_fc; | |
259 | struct fs_sink_ctf_named_field_class *named_fc; | |
260 | uint64_t len; | |
261 | ||
262 | field_path_elem = cur_path_stack_at(ctx, (uint64_t) si); | |
263 | fc = field_path_elem->parent_fc; | |
264 | if (!fc) { | |
265 | /* Reached stack's bottom */ | |
266 | ret = -1; | |
267 | goto end; | |
268 | } | |
269 | ||
270 | switch (fc->type) { | |
271 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
272 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: | |
273 | break; | |
274 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
275 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
276 | continue; | |
277 | default: | |
278 | /* Not supported by TSDL 1.8 */ | |
279 | ret = -1; | |
280 | goto end; | |
281 | } | |
282 | ||
283 | if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { | |
284 | struct_fc = (void *) fc; | |
285 | len = struct_fc->members->len; | |
286 | } else { | |
287 | var_fc = (void *) fc; | |
288 | len = var_fc->options->len; | |
289 | } | |
290 | ||
291 | for (i = 0; i < len; i++) { | |
292 | if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { | |
293 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( | |
294 | struct_fc, i); | |
295 | } else { | |
296 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( | |
297 | var_fc, i); | |
298 | } | |
299 | ||
300 | if (strcmp(named_fc->name->str, tgt_fc_name) == 0) { | |
301 | if (named_fc->fc == tgt_fc) { | |
302 | g_string_assign(tgt_field_ref, | |
303 | tgt_fc_name); | |
304 | } else { | |
305 | /* | |
306 | * Using only the target field | |
307 | * class's name, we're not | |
308 | * reaching the target field | |
309 | * class. This is not supported | |
310 | * by TSDL 1.8. | |
311 | */ | |
312 | ret = -1; | |
313 | } | |
314 | ||
315 | goto end; | |
316 | } | |
317 | } | |
318 | } | |
319 | ||
320 | end: | |
321 | return ret; | |
322 | } | |
323 | ||
324 | /* | |
325 | * Creates an absolute field ref from IR field path `tgt_ir_field_path`. | |
326 | * | |
327 | * Returns a negative value if this resolving operation failed. | |
328 | */ | |
329 | static | |
330 | int create_absolute_field_ref(struct ctx *ctx, | |
331 | const bt_field_path *tgt_ir_field_path, GString *tgt_field_ref) | |
332 | { | |
333 | int ret = 0; | |
334 | struct fs_sink_ctf_field_class *fc = NULL; | |
335 | uint64_t i; | |
336 | ||
337 | switch (bt_field_path_get_root_scope(tgt_ir_field_path)) { | |
338 | case BT_SCOPE_PACKET_CONTEXT: | |
339 | BT_ASSERT(ctx->cur_sc); | |
340 | fc = ctx->cur_sc->packet_context_fc; | |
341 | g_string_assign(tgt_field_ref, "stream.packet.context"); | |
342 | break; | |
343 | case BT_SCOPE_EVENT_COMMON_CONTEXT: | |
344 | BT_ASSERT(ctx->cur_sc); | |
345 | fc = ctx->cur_sc->event_common_context_fc; | |
346 | g_string_assign(tgt_field_ref, "stream.event.context"); | |
347 | break; | |
348 | case BT_SCOPE_EVENT_SPECIFIC_CONTEXT: | |
349 | BT_ASSERT(ctx->cur_ec); | |
350 | fc = ctx->cur_ec->spec_context_fc; | |
351 | g_string_assign(tgt_field_ref, "event.context"); | |
352 | break; | |
353 | case BT_SCOPE_EVENT_PAYLOAD: | |
354 | BT_ASSERT(ctx->cur_ec); | |
355 | fc = ctx->cur_ec->payload_fc; | |
356 | g_string_assign(tgt_field_ref, "event.fields"); | |
357 | break; | |
358 | default: | |
359 | abort(); | |
360 | } | |
361 | ||
362 | BT_ASSERT(fc); | |
363 | ||
1d4afa5f PP |
364 | for (i = 0; i < bt_field_path_get_item_count(tgt_ir_field_path); i++) { |
365 | const bt_field_path_item *fp_item = | |
366 | bt_field_path_borrow_item_by_index_const( | |
367 | tgt_ir_field_path, i); | |
46bdd3e0 PP |
368 | struct fs_sink_ctf_named_field_class *named_fc = NULL; |
369 | ||
1d4afa5f PP |
370 | if (bt_field_path_item_get_type(fp_item) != |
371 | BT_FIELD_PATH_ITEM_TYPE_INDEX) { | |
372 | /* Not supported by TSDL 1.8 */ | |
373 | ret = -1; | |
374 | goto end; | |
375 | } | |
376 | ||
46bdd3e0 PP |
377 | switch (fc->type) { |
378 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
1d4afa5f PP |
379 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
380 | BT_FIELD_PATH_ITEM_TYPE_INDEX); | |
46bdd3e0 | 381 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( |
1d4afa5f PP |
382 | (void *) fc, |
383 | bt_field_path_item_index_get_index(fp_item)); | |
46bdd3e0 PP |
384 | break; |
385 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: | |
1d4afa5f PP |
386 | BT_ASSERT(bt_field_path_item_get_type(fp_item) == |
387 | BT_FIELD_PATH_ITEM_TYPE_INDEX); | |
46bdd3e0 | 388 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( |
1d4afa5f PP |
389 | (void *) fc, |
390 | bt_field_path_item_index_get_index(fp_item)); | |
46bdd3e0 | 391 | break; |
46bdd3e0 PP |
392 | default: |
393 | abort(); | |
394 | } | |
395 | ||
396 | BT_ASSERT(named_fc); | |
397 | g_string_append_c(tgt_field_ref, '.'); | |
398 | g_string_append(tgt_field_ref, named_fc->name->str); | |
399 | fc = named_fc->fc; | |
400 | } | |
401 | ||
402 | end: | |
403 | return ret; | |
404 | } | |
405 | ||
406 | /* | |
407 | * Resolves a target field class located at `tgt_ir_field_path`, writing | |
408 | * the resolved field ref to `tgt_field_ref` and setting | |
409 | * `*create_before` according to whether or not the target field must be | |
410 | * created immediately before (in which case `tgt_field_ref` is | |
411 | * irrelevant). | |
412 | */ | |
413 | static | |
414 | void resolve_field_class(struct ctx *ctx, | |
415 | const bt_field_path *tgt_ir_field_path, | |
416 | GString *tgt_field_ref, bool *create_before) | |
417 | { | |
418 | int ret; | |
419 | bt_scope tgt_scope; | |
420 | ||
421 | *create_before = false; | |
422 | ||
423 | if (!tgt_ir_field_path) { | |
424 | *create_before = true; | |
425 | goto end; | |
426 | } | |
427 | ||
428 | tgt_scope = bt_field_path_get_root_scope(tgt_ir_field_path); | |
429 | ||
430 | if (tgt_scope == ctx->cur_scope) { | |
431 | /* | |
432 | * Try, in this order: | |
433 | * | |
434 | * 1. Use a relative path, using only the target field | |
435 | * class's name. This is what is the most commonly | |
436 | * supported by popular CTF reading tools. | |
437 | * | |
438 | * 2. Use an absolute path. This could fail if there's | |
439 | * an array field class from the current root's field | |
440 | * class to the target field class. | |
441 | * | |
442 | * 3. Create the target field class before the | |
443 | * requesting field class (fallback). | |
444 | */ | |
445 | ret = create_relative_field_ref(ctx, tgt_ir_field_path, | |
446 | tgt_field_ref); | |
447 | if (ret) { | |
448 | ret = create_absolute_field_ref(ctx, tgt_ir_field_path, | |
449 | tgt_field_ref); | |
450 | if (ret) { | |
451 | *create_before = true; | |
452 | ret = 0; | |
453 | goto end; | |
454 | } | |
455 | } | |
456 | } else { | |
457 | ret = create_absolute_field_ref(ctx, tgt_ir_field_path, | |
458 | tgt_field_ref); | |
459 | ||
460 | /* It must always work in previous scopes */ | |
461 | BT_ASSERT(ret == 0); | |
462 | } | |
463 | ||
464 | end: | |
465 | return; | |
466 | } | |
467 | ||
468 | static | |
469 | int translate_field_class(struct ctx *ctx); | |
470 | ||
471 | static inline | |
472 | void append_to_parent_field_class(struct ctx *ctx, | |
473 | struct fs_sink_ctf_field_class *fc) | |
474 | { | |
475 | struct fs_sink_ctf_field_class *parent_fc = | |
476 | cur_path_stack_top(ctx)->parent_fc; | |
477 | ||
478 | switch (parent_fc->type) { | |
479 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
480 | fs_sink_ctf_field_class_struct_append_member((void *) parent_fc, | |
481 | cur_path_stack_top(ctx)->name->str, fc); | |
482 | break; | |
483 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: | |
484 | fs_sink_ctf_field_class_variant_append_option((void *) parent_fc, | |
485 | cur_path_stack_top(ctx)->name->str, fc); | |
486 | break; | |
487 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
488 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
489 | { | |
490 | struct fs_sink_ctf_field_class_array_base *array_base_fc = | |
491 | (void *) parent_fc; | |
492 | ||
493 | BT_ASSERT(!array_base_fc->elem_fc); | |
494 | array_base_fc->elem_fc = fc; | |
495 | array_base_fc->base.alignment = fc->alignment; | |
496 | break; | |
497 | } | |
498 | default: | |
499 | abort(); | |
500 | } | |
501 | } | |
502 | ||
503 | static inline | |
504 | void update_parent_field_class_alignment(struct ctx *ctx, | |
505 | unsigned int alignment) | |
506 | { | |
507 | struct fs_sink_ctf_field_class *parent_fc = | |
508 | cur_path_stack_top(ctx)->parent_fc; | |
509 | ||
510 | switch (parent_fc->type) { | |
511 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
512 | fs_sink_ctf_field_class_struct_align_at_least( | |
513 | (void *) parent_fc, alignment); | |
514 | break; | |
515 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
516 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
517 | { | |
518 | struct fs_sink_ctf_field_class_array_base *array_base_fc = | |
519 | (void *) parent_fc; | |
520 | ||
521 | array_base_fc->base.alignment = alignment; | |
522 | break; | |
523 | } | |
524 | default: | |
525 | break; | |
526 | } | |
527 | } | |
528 | ||
529 | static inline | |
530 | int translate_structure_field_class_members(struct ctx *ctx, | |
531 | struct fs_sink_ctf_field_class_struct *struct_fc, | |
532 | const bt_field_class *ir_fc) | |
533 | { | |
534 | int ret = 0; | |
535 | uint64_t i; | |
536 | ||
537 | for (i = 0; i < bt_field_class_structure_get_member_count(ir_fc); i++) { | |
538 | const bt_field_class_structure_member *member; | |
539 | const char *name; | |
540 | const bt_field_class *memb_ir_fc; | |
541 | ||
542 | member = | |
543 | bt_field_class_structure_borrow_member_by_index_const( | |
544 | ir_fc, i); | |
545 | name = bt_field_class_structure_member_get_name(member); | |
546 | memb_ir_fc = bt_field_class_structure_member_borrow_field_class_const( | |
547 | member); | |
548 | ret = cur_path_stack_push(ctx, i, name, memb_ir_fc, | |
549 | (void *) struct_fc); | |
550 | if (ret) { | |
551 | BT_LOGE("Cannot translate structure field class member: " | |
552 | "name=\"%s\"", name); | |
553 | goto end; | |
554 | } | |
555 | ||
556 | ret = translate_field_class(ctx); | |
557 | if (ret) { | |
558 | BT_LOGE("Cannot translate structure field class member: " | |
559 | "name=\"%s\"", name); | |
560 | goto end; | |
561 | } | |
562 | ||
563 | cur_path_stack_pop(ctx); | |
564 | } | |
565 | ||
566 | end: | |
567 | return ret; | |
568 | } | |
569 | ||
570 | static inline | |
571 | int translate_structure_field_class(struct ctx *ctx) | |
572 | { | |
573 | int ret; | |
574 | struct fs_sink_ctf_field_class_struct *fc = | |
575 | fs_sink_ctf_field_class_struct_create_empty( | |
576 | cur_path_stack_top(ctx)->ir_fc, | |
577 | cur_path_stack_top(ctx)->index_in_parent); | |
578 | ||
579 | BT_ASSERT(fc); | |
580 | append_to_parent_field_class(ctx, (void *) fc); | |
581 | ret = translate_structure_field_class_members(ctx, fc, fc->base.ir_fc); | |
582 | if (ret) { | |
583 | goto end; | |
584 | } | |
585 | ||
586 | update_parent_field_class_alignment(ctx, fc->base.alignment); | |
587 | ||
588 | end: | |
589 | return ret; | |
590 | } | |
591 | ||
592 | static inline | |
593 | int translate_variant_field_class(struct ctx *ctx) | |
594 | { | |
595 | int ret = 0; | |
596 | uint64_t i; | |
597 | struct fs_sink_ctf_field_class_variant *fc = | |
598 | fs_sink_ctf_field_class_variant_create_empty( | |
599 | cur_path_stack_top(ctx)->ir_fc, | |
600 | cur_path_stack_top(ctx)->index_in_parent); | |
601 | ||
602 | BT_ASSERT(fc); | |
603 | ||
604 | /* Resolve tag field class before appending to parent */ | |
605 | resolve_field_class(ctx, | |
606 | bt_field_class_variant_borrow_selector_field_path_const( | |
607 | fc->base.ir_fc), fc->tag_ref, &fc->tag_is_before); | |
608 | ||
609 | append_to_parent_field_class(ctx, (void *) fc); | |
610 | ||
611 | for (i = 0; i < bt_field_class_variant_get_option_count(fc->base.ir_fc); | |
612 | i++) { | |
613 | const bt_field_class_variant_option *opt; | |
614 | const char *name; | |
615 | const bt_field_class *opt_ir_fc; | |
616 | ||
617 | opt = bt_field_class_variant_borrow_option_by_index_const( | |
618 | fc->base.ir_fc, i); | |
619 | name = bt_field_class_variant_option_get_name(opt); | |
620 | opt_ir_fc = bt_field_class_variant_option_borrow_field_class_const( | |
621 | opt); | |
622 | ret = cur_path_stack_push(ctx, i, name, opt_ir_fc, (void *) fc); | |
623 | if (ret) { | |
624 | BT_LOGE("Cannot translate variant field class option: " | |
625 | "name=\"%s\"", name); | |
626 | goto end; | |
627 | } | |
628 | ||
629 | ret = translate_field_class(ctx); | |
630 | if (ret) { | |
631 | BT_LOGE("Cannot translate variant field class option: " | |
632 | "name=\"%s\"", name); | |
633 | goto end; | |
634 | } | |
635 | ||
636 | cur_path_stack_pop(ctx); | |
637 | } | |
638 | ||
639 | end: | |
640 | return ret; | |
641 | } | |
642 | ||
643 | static inline | |
644 | int translate_static_array_field_class(struct ctx *ctx) | |
645 | { | |
646 | struct fs_sink_ctf_field_class_array *fc = | |
647 | fs_sink_ctf_field_class_array_create_empty( | |
648 | cur_path_stack_top(ctx)->ir_fc, | |
649 | cur_path_stack_top(ctx)->index_in_parent); | |
650 | const bt_field_class *elem_ir_fc = | |
651 | bt_field_class_array_borrow_element_field_class_const( | |
652 | fc->base.base.ir_fc); | |
653 | int ret; | |
654 | ||
655 | BT_ASSERT(fc); | |
656 | append_to_parent_field_class(ctx, (void *) fc); | |
657 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, elem_ir_fc, | |
658 | (void *) fc); | |
659 | if (ret) { | |
660 | BT_LOGE_STR("Cannot translate static array field class element."); | |
661 | goto end; | |
662 | } | |
663 | ||
664 | ret = translate_field_class(ctx); | |
665 | if (ret) { | |
666 | BT_LOGE_STR("Cannot translate static array field class element."); | |
667 | goto end; | |
668 | } | |
669 | ||
670 | cur_path_stack_pop(ctx); | |
671 | update_parent_field_class_alignment(ctx, fc->base.base.alignment); | |
672 | ||
673 | end: | |
674 | return ret; | |
675 | } | |
676 | ||
677 | static inline | |
678 | int translate_dynamic_array_field_class(struct ctx *ctx) | |
679 | { | |
680 | struct fs_sink_ctf_field_class_sequence *fc = | |
681 | fs_sink_ctf_field_class_sequence_create_empty( | |
682 | cur_path_stack_top(ctx)->ir_fc, | |
683 | cur_path_stack_top(ctx)->index_in_parent); | |
684 | const bt_field_class *elem_ir_fc = | |
685 | bt_field_class_array_borrow_element_field_class_const( | |
686 | fc->base.base.ir_fc); | |
687 | int ret; | |
688 | ||
689 | BT_ASSERT(fc); | |
690 | ||
691 | /* Resolve length field class before appending to parent */ | |
692 | resolve_field_class(ctx, | |
693 | bt_field_class_dynamic_array_borrow_length_field_path_const( | |
694 | fc->base.base.ir_fc), | |
695 | fc->length_ref, &fc->length_is_before); | |
696 | ||
697 | append_to_parent_field_class(ctx, (void *) fc); | |
698 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, elem_ir_fc, | |
699 | (void *) fc); | |
700 | if (ret) { | |
701 | BT_LOGE_STR("Cannot translate dynamic array field class element."); | |
702 | goto end; | |
703 | } | |
704 | ||
705 | ret = translate_field_class(ctx); | |
706 | if (ret) { | |
707 | BT_LOGE_STR("Cannot translate dynamic array field class element."); | |
708 | goto end; | |
709 | } | |
710 | ||
711 | cur_path_stack_pop(ctx); | |
712 | update_parent_field_class_alignment(ctx, fc->base.base.alignment); | |
713 | ||
714 | end: | |
715 | return ret; | |
716 | } | |
717 | ||
718 | static inline | |
719 | int translate_integer_field_class(struct ctx *ctx) | |
720 | { | |
721 | struct fs_sink_ctf_field_class_int *fc = | |
722 | fs_sink_ctf_field_class_int_create( | |
723 | cur_path_stack_top(ctx)->ir_fc, | |
724 | cur_path_stack_top(ctx)->index_in_parent); | |
725 | ||
726 | BT_ASSERT(fc); | |
727 | append_to_parent_field_class(ctx, (void *) fc); | |
728 | return 0; | |
729 | } | |
730 | ||
731 | static inline | |
732 | int translate_real_field_class(struct ctx *ctx) | |
733 | { | |
734 | struct fs_sink_ctf_field_class_float *fc = | |
735 | fs_sink_ctf_field_class_float_create( | |
736 | cur_path_stack_top(ctx)->ir_fc, | |
737 | cur_path_stack_top(ctx)->index_in_parent); | |
738 | ||
739 | BT_ASSERT(fc); | |
740 | append_to_parent_field_class(ctx, (void *) fc); | |
741 | return 0; | |
742 | } | |
743 | ||
744 | static inline | |
745 | int translate_string_field_class(struct ctx *ctx) | |
746 | { | |
747 | struct fs_sink_ctf_field_class_string *fc = | |
748 | fs_sink_ctf_field_class_string_create( | |
749 | cur_path_stack_top(ctx)->ir_fc, | |
750 | cur_path_stack_top(ctx)->index_in_parent); | |
751 | ||
752 | BT_ASSERT(fc); | |
753 | append_to_parent_field_class(ctx, (void *) fc); | |
754 | return 0; | |
755 | } | |
756 | ||
757 | /* | |
758 | * Translates a field class, recursively. | |
759 | * | |
760 | * The field class's IR field class, parent field class, and index | |
761 | * within its parent are in the context's current path's top element | |
762 | * (cur_path_stack_top()). | |
763 | */ | |
764 | static | |
765 | int translate_field_class(struct ctx *ctx) | |
766 | { | |
767 | int ret; | |
768 | ||
769 | switch (bt_field_class_get_type(cur_path_stack_top(ctx)->ir_fc)) { | |
770 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
771 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
772 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
773 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
774 | ret = translate_integer_field_class(ctx); | |
775 | break; | |
776 | case BT_FIELD_CLASS_TYPE_REAL: | |
777 | ret = translate_real_field_class(ctx); | |
778 | break; | |
779 | case BT_FIELD_CLASS_TYPE_STRING: | |
780 | ret = translate_string_field_class(ctx); | |
781 | break; | |
782 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
783 | ret = translate_structure_field_class(ctx); | |
784 | break; | |
785 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
786 | ret = translate_static_array_field_class(ctx); | |
787 | break; | |
788 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
789 | ret = translate_dynamic_array_field_class(ctx); | |
790 | break; | |
791 | case BT_FIELD_CLASS_TYPE_VARIANT: | |
792 | ret = translate_variant_field_class(ctx); | |
793 | break; | |
794 | default: | |
795 | abort(); | |
796 | } | |
797 | ||
798 | return ret; | |
799 | } | |
800 | ||
801 | static | |
802 | int set_field_ref(struct fs_sink_ctf_field_class *fc, const char *fc_name, | |
803 | struct fs_sink_ctf_field_class *parent_fc) | |
804 | { | |
805 | int ret = 0; | |
806 | GString *field_ref = NULL; | |
807 | bool is_before; | |
808 | const char *tgt_type; | |
809 | struct fs_sink_ctf_field_class_struct *parent_struct_fc = | |
810 | (void *) parent_fc; | |
811 | uint64_t i; | |
812 | unsigned int suffix = 0; | |
813 | ||
814 | if (!fc_name || !parent_fc || | |
815 | parent_fc->type != FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { | |
816 | /* Not supported */ | |
817 | ret = -1; | |
818 | goto end; | |
819 | } | |
820 | ||
821 | switch (fc->type) { | |
822 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
823 | { | |
824 | struct fs_sink_ctf_field_class_sequence *seq_fc = (void *) fc; | |
825 | ||
826 | field_ref = seq_fc->length_ref; | |
827 | is_before = seq_fc->length_is_before; | |
828 | tgt_type = "len"; | |
829 | break; | |
830 | } | |
831 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: | |
832 | { | |
833 | struct fs_sink_ctf_field_class_variant *var_fc = (void *) fc; | |
834 | ||
835 | field_ref = var_fc->tag_ref; | |
836 | is_before = var_fc->tag_is_before; | |
837 | tgt_type = "tag"; | |
838 | break; | |
839 | } | |
840 | default: | |
841 | abort(); | |
842 | } | |
843 | ||
844 | BT_ASSERT(field_ref); | |
845 | ||
846 | if (!is_before) { | |
847 | goto end; | |
848 | } | |
849 | ||
850 | /* Initial field ref */ | |
851 | g_string_printf(field_ref, "__%s_%s", fc_name, tgt_type); | |
852 | ||
853 | /* | |
854 | * Make sure field ref does not clash with an existing field | |
855 | * class name within the same parent structure field class. | |
856 | */ | |
857 | while (true) { | |
858 | bool name_ok = true; | |
859 | ||
860 | for (i = 0; i < parent_struct_fc->members->len; i++) { | |
861 | struct fs_sink_ctf_named_field_class *named_fc = | |
862 | fs_sink_ctf_field_class_struct_borrow_member_by_index( | |
863 | parent_struct_fc, i); | |
864 | ||
865 | if (strcmp(field_ref->str, named_fc->name->str) == 0) { | |
866 | /* Name clash */ | |
867 | name_ok = false; | |
868 | break; | |
869 | } | |
870 | } | |
871 | ||
872 | if (name_ok) { | |
873 | /* No clash: we're done */ | |
874 | break; | |
875 | } | |
876 | ||
877 | /* Append suffix and try again */ | |
878 | g_string_printf(field_ref, "__%s_%s_%u", fc_name, tgt_type, | |
879 | suffix); | |
880 | suffix++; | |
881 | } | |
882 | ||
883 | end: | |
884 | return ret; | |
885 | } | |
886 | ||
887 | /* | |
888 | * This function recursively sets field refs of sequence and variant | |
889 | * field classes when they are immediately before, avoiding name clashes | |
890 | * with existing field class names. | |
891 | * | |
892 | * It can fail at this point if, for example, a sequence field class of | |
893 | * which to set the length's field ref has something else than a | |
894 | * structure field class as its parent: in this case, there's no | |
895 | * location to place the length field class immediately before the | |
896 | * sequence field class. | |
897 | */ | |
898 | static | |
899 | int set_field_refs(struct fs_sink_ctf_field_class *fc, const char *fc_name, | |
900 | struct fs_sink_ctf_field_class *parent_fc) | |
901 | { | |
902 | int ret = 0; | |
903 | BT_ASSERT(fc); | |
904 | ||
905 | switch (fc->type) { | |
906 | case FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT: | |
907 | case FS_SINK_CTF_FIELD_CLASS_TYPE_VARIANT: | |
908 | { | |
909 | uint64_t i; | |
910 | uint64_t len; | |
911 | struct fs_sink_ctf_field_class_struct *struct_fc; | |
912 | struct fs_sink_ctf_field_class_variant *var_fc; | |
913 | struct fs_sink_ctf_named_field_class *named_fc; | |
914 | ||
915 | if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { | |
916 | struct_fc = (void *) fc; | |
917 | len = struct_fc->members->len; | |
918 | } else { | |
919 | var_fc = (void *) fc; | |
920 | len = var_fc->options->len; | |
921 | ret = set_field_ref(fc, fc_name, parent_fc); | |
922 | if (ret) { | |
923 | goto end; | |
924 | } | |
925 | } | |
926 | ||
927 | for (i = 0; i < len; i++) { | |
928 | if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_STRUCT) { | |
929 | named_fc = fs_sink_ctf_field_class_struct_borrow_member_by_index( | |
930 | struct_fc, i); | |
931 | } else { | |
932 | named_fc = fs_sink_ctf_field_class_variant_borrow_option_by_index( | |
933 | var_fc, i); | |
934 | } | |
935 | ||
936 | ret = set_field_refs(named_fc->fc, named_fc->name->str, | |
937 | fc); | |
938 | if (ret) { | |
939 | goto end; | |
940 | } | |
941 | } | |
942 | ||
943 | break; | |
944 | } | |
945 | case FS_SINK_CTF_FIELD_CLASS_TYPE_ARRAY: | |
946 | case FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE: | |
947 | { | |
948 | struct fs_sink_ctf_field_class_array_base *array_base_fc = | |
949 | (void *) fc; | |
950 | ||
951 | if (fc->type == FS_SINK_CTF_FIELD_CLASS_TYPE_SEQUENCE) { | |
952 | ret = set_field_ref(fc, fc_name, parent_fc); | |
953 | if (ret) { | |
954 | goto end; | |
955 | } | |
956 | } | |
957 | ||
958 | ret = set_field_refs(array_base_fc->elem_fc, NULL, fc); | |
959 | if (ret) { | |
960 | goto end; | |
961 | } | |
962 | ||
963 | break; | |
964 | } | |
965 | default: | |
966 | break; | |
967 | } | |
968 | ||
969 | end: | |
970 | return ret; | |
971 | } | |
972 | ||
973 | /* | |
974 | * This function translates a root scope trace IR field class to | |
975 | * a CTF IR field class. | |
976 | * | |
977 | * The resulting CTF IR field class is written to `*fc` so that it | |
978 | * exists as the parent object's (stream class or event class) true root | |
979 | * field class during the recursive translation for resolving purposes. | |
980 | * This is also why this function creates the empty structure field | |
981 | * class and then calls translate_structure_field_class_members() to | |
982 | * fill it. | |
983 | */ | |
984 | static | |
985 | int translate_scope_field_class(struct ctx *ctx, bt_scope scope, | |
986 | struct fs_sink_ctf_field_class **fc, | |
987 | const bt_field_class *ir_fc) | |
988 | { | |
989 | int ret = 0; | |
990 | ||
991 | if (!ir_fc) { | |
992 | goto end; | |
993 | } | |
994 | ||
995 | BT_ASSERT(bt_field_class_get_type(ir_fc) == | |
996 | BT_FIELD_CLASS_TYPE_STRUCTURE); | |
997 | BT_ASSERT(fc); | |
998 | *fc = (void *) fs_sink_ctf_field_class_struct_create_empty( | |
999 | ir_fc, UINT64_C(-1)); | |
1000 | BT_ASSERT(*fc); | |
1001 | ctx->cur_scope = scope; | |
1002 | BT_ASSERT(ctx->cur_path->len == 0); | |
1003 | ret = cur_path_stack_push(ctx, UINT64_C(-1), NULL, ir_fc, NULL); | |
1004 | if (ret) { | |
1005 | BT_LOGE("Cannot translate scope structure field class: " | |
1006 | "scope=%d", scope); | |
1007 | goto end; | |
1008 | } | |
1009 | ||
1010 | ret = translate_structure_field_class_members(ctx, (void *) *fc, ir_fc); | |
1011 | if (ret) { | |
1012 | BT_LOGE("Cannot translate scope structure field class: " | |
1013 | "scope=%d", scope); | |
1014 | goto end; | |
1015 | } | |
1016 | ||
1017 | cur_path_stack_pop(ctx); | |
1018 | ||
1019 | /* Set field refs for preceding targets */ | |
1020 | ret = set_field_refs(*fc, NULL, NULL); | |
1021 | ||
1022 | end: | |
1023 | return ret; | |
1024 | } | |
1025 | ||
1026 | static inline | |
1027 | void ctx_init(struct ctx *ctx) | |
1028 | { | |
1029 | memset(ctx, 0, sizeof(struct ctx)); | |
1030 | ctx->cur_path = g_array_new(FALSE, TRUE, | |
1031 | sizeof(struct field_path_elem)); | |
1032 | BT_ASSERT(ctx->cur_path); | |
1033 | } | |
1034 | ||
1035 | static inline | |
1036 | void ctx_fini(struct ctx *ctx) | |
1037 | { | |
1038 | if (ctx->cur_path) { | |
1039 | g_array_free(ctx->cur_path, TRUE); | |
1040 | ctx->cur_path = NULL; | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | static | |
1045 | int translate_event_class(struct fs_sink_ctf_stream_class *sc, | |
1046 | const bt_event_class *ir_ec, | |
1047 | struct fs_sink_ctf_event_class **out_ec) | |
1048 | { | |
1049 | int ret = 0; | |
1050 | struct ctx ctx; | |
1051 | struct fs_sink_ctf_event_class *ec; | |
1052 | ||
1053 | BT_ASSERT(sc); | |
1054 | BT_ASSERT(ir_ec); | |
1055 | ||
1056 | ctx_init(&ctx); | |
1057 | ec = fs_sink_ctf_event_class_create(sc, ir_ec); | |
1058 | BT_ASSERT(ec); | |
1059 | ctx.cur_sc = sc; | |
1060 | ctx.cur_ec = ec; | |
1061 | ret = translate_scope_field_class(&ctx, BT_SCOPE_EVENT_SPECIFIC_CONTEXT, | |
1062 | &ec->spec_context_fc, | |
1063 | bt_event_class_borrow_specific_context_field_class_const( | |
1064 | ir_ec)); | |
1065 | if (ret) { | |
1066 | goto end; | |
1067 | } | |
1068 | ||
1069 | ret = translate_scope_field_class(&ctx, BT_SCOPE_EVENT_PAYLOAD, | |
1070 | &ec->payload_fc, | |
1071 | bt_event_class_borrow_payload_field_class_const(ir_ec)); | |
1072 | if (ret) { | |
1073 | goto end; | |
1074 | } | |
1075 | ||
1076 | end: | |
1077 | ctx_fini(&ctx); | |
1078 | *out_ec = ec; | |
1079 | return ret; | |
1080 | } | |
1081 | ||
1082 | BT_HIDDEN | |
1083 | int try_translate_event_class_trace_ir_to_ctf_ir( | |
1084 | struct fs_sink_ctf_stream_class *sc, | |
1085 | const bt_event_class *ir_ec, | |
1086 | struct fs_sink_ctf_event_class **out_ec) | |
1087 | { | |
1088 | int ret = 0; | |
1089 | ||
1090 | BT_ASSERT(sc); | |
1091 | BT_ASSERT(ir_ec); | |
1092 | ||
1093 | /* Check in hash table first */ | |
1094 | *out_ec = g_hash_table_lookup(sc->event_classes_from_ir, ir_ec); | |
1095 | if (likely(*out_ec)) { | |
1096 | goto end; | |
1097 | } | |
1098 | ||
1099 | ret = translate_event_class(sc, ir_ec, out_ec); | |
1100 | ||
1101 | end: | |
1102 | return ret; | |
1103 | } | |
1104 | ||
1105 | bool default_clock_class_name_exists(struct fs_sink_ctf_trace_class *tc, | |
1106 | const char *name) | |
1107 | { | |
1108 | bool exists = false; | |
1109 | uint64_t i; | |
1110 | ||
1111 | for (i = 0; i < tc->stream_classes->len; i++) { | |
1112 | struct fs_sink_ctf_stream_class *sc = | |
1113 | tc->stream_classes->pdata[i]; | |
1114 | ||
1115 | if (sc->default_clock_class_name->len == 0) { | |
1116 | /* No default clock class */ | |
1117 | continue; | |
1118 | } | |
1119 | ||
1120 | if (strcmp(sc->default_clock_class_name->str, name) == 0) { | |
1121 | exists = true; | |
1122 | goto end; | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | end: | |
1127 | return exists; | |
1128 | } | |
1129 | ||
1130 | static | |
1131 | void make_unique_default_clock_class_name(struct fs_sink_ctf_stream_class *sc) | |
1132 | { | |
1133 | unsigned int suffix = 0; | |
1134 | char buf[16]; | |
1135 | ||
1136 | g_string_assign(sc->default_clock_class_name, ""); | |
1137 | sprintf(buf, "default"); | |
1138 | ||
1139 | while (default_clock_class_name_exists(sc->tc, buf)) { | |
1140 | sprintf(buf, "default%u", suffix); | |
1141 | suffix++; | |
1142 | } | |
1143 | ||
1144 | g_string_assign(sc->default_clock_class_name, buf); | |
1145 | } | |
1146 | ||
1147 | static | |
1148 | int translate_stream_class(struct fs_sink_ctf_trace_class *tc, | |
1149 | const bt_stream_class *ir_sc, | |
1150 | struct fs_sink_ctf_stream_class **out_sc) | |
1151 | { | |
1152 | int ret = 0; | |
1153 | struct ctx ctx; | |
1154 | ||
1155 | BT_ASSERT(tc); | |
1156 | BT_ASSERT(ir_sc); | |
1157 | ctx_init(&ctx); | |
1158 | *out_sc = fs_sink_ctf_stream_class_create(tc, ir_sc); | |
1159 | BT_ASSERT(*out_sc); | |
1160 | ||
1161 | /* Set default clock class's protected name, if any */ | |
1162 | if ((*out_sc)->default_clock_class) { | |
1163 | const char *name = bt_clock_class_get_name( | |
1164 | (*out_sc)->default_clock_class); | |
1165 | ||
1166 | if (!bt_stream_class_default_clock_is_always_known(ir_sc)) { | |
1167 | BT_LOGE("Unsupported stream clock which can have an unknown value: " | |
1168 | "sc-name=\"%s\"", | |
1169 | bt_stream_class_get_name(ir_sc)); | |
1170 | goto error; | |
1171 | } | |
1172 | ||
1173 | if (name) { | |
1174 | /* Try original name, protected */ | |
1175 | g_string_assign((*out_sc)->default_clock_class_name, | |
1176 | name); | |
1177 | ret = fs_sink_ctf_protect_name( | |
1178 | (*out_sc)->default_clock_class_name); | |
1179 | if (ret) { | |
1180 | /* Invalid: create a new name */ | |
1181 | make_unique_default_clock_class_name(*out_sc); | |
1182 | ret = 0; | |
1183 | } | |
1184 | } else { | |
1185 | /* No name: create a name */ | |
1186 | make_unique_default_clock_class_name(*out_sc); | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | ctx.cur_sc = *out_sc; | |
1191 | ret = translate_scope_field_class(&ctx, BT_SCOPE_PACKET_CONTEXT, | |
1192 | &(*out_sc)->packet_context_fc, | |
1193 | bt_stream_class_borrow_packet_context_field_class_const(ir_sc)); | |
1194 | if (ret) { | |
1195 | goto error; | |
1196 | } | |
1197 | ||
1198 | if ((*out_sc)->packet_context_fc) { | |
1199 | /* | |
1200 | * Make sure the structure field class's alignment is | |
1201 | * enough: 8 is what we use for our own special members | |
1202 | * in the packet context. | |
1203 | */ | |
1204 | fs_sink_ctf_field_class_struct_align_at_least( | |
1205 | (void *) (*out_sc)->packet_context_fc, 8); | |
1206 | } | |
1207 | ||
1208 | ret = translate_scope_field_class(&ctx, BT_SCOPE_EVENT_COMMON_CONTEXT, | |
1209 | &(*out_sc)->event_common_context_fc, | |
1210 | bt_stream_class_borrow_event_common_context_field_class_const( | |
1211 | ir_sc)); | |
1212 | if (ret) { | |
1213 | goto error; | |
1214 | } | |
1215 | ||
1216 | goto end; | |
1217 | ||
1218 | error: | |
1219 | fs_sink_ctf_stream_class_destroy(*out_sc); | |
1220 | *out_sc = NULL; | |
1221 | ||
1222 | end: | |
1223 | ctx_fini(&ctx); | |
1224 | return ret; | |
1225 | } | |
1226 | ||
1227 | BT_HIDDEN | |
1228 | int try_translate_stream_class_trace_ir_to_ctf_ir( | |
1229 | struct fs_sink_ctf_trace_class *tc, | |
1230 | const bt_stream_class *ir_sc, | |
1231 | struct fs_sink_ctf_stream_class **out_sc) | |
1232 | { | |
1233 | int ret = 0; | |
1234 | uint64_t i; | |
1235 | ||
1236 | BT_ASSERT(tc); | |
1237 | BT_ASSERT(ir_sc); | |
1238 | ||
1239 | for (i = 0; i < tc->stream_classes->len; i++) { | |
1240 | *out_sc = tc->stream_classes->pdata[i]; | |
1241 | ||
1242 | if ((*out_sc)->ir_sc == ir_sc) { | |
1243 | goto end; | |
1244 | } | |
1245 | } | |
1246 | ||
1247 | ret = translate_stream_class(tc, ir_sc, out_sc); | |
1248 | ||
1249 | end: | |
1250 | return ret; | |
1251 | } | |
1252 | ||
1253 | BT_HIDDEN | |
1254 | struct fs_sink_ctf_trace_class *translate_trace_class_trace_ir_to_ctf_ir( | |
1255 | const bt_trace_class *ir_tc) | |
1256 | { | |
1257 | uint64_t count; | |
1258 | uint64_t i; | |
1259 | struct fs_sink_ctf_trace_class *tc = NULL; | |
1260 | ||
1261 | /* Check that trace class's environment is TSDL-compatible */ | |
1262 | count = bt_trace_class_get_environment_entry_count(ir_tc); | |
1263 | for (i = 0; i < count; i++) { | |
1264 | const char *name; | |
1265 | const bt_value *val; | |
1266 | ||
1267 | bt_trace_class_borrow_environment_entry_by_index_const( | |
1268 | ir_tc, i, &name, &val); | |
1269 | ||
1270 | if (!fs_sink_ctf_ist_valid_identifier(name)) { | |
1271 | BT_LOGE("Unsupported trace class's environment entry name: " | |
1272 | "name=\"%s\"", name); | |
1273 | goto end; | |
1274 | } | |
1275 | ||
1276 | switch (bt_value_get_type(val)) { | |
1277 | case BT_VALUE_TYPE_INTEGER: | |
1278 | case BT_VALUE_TYPE_STRING: | |
1279 | break; | |
1280 | default: | |
1281 | BT_LOGE("Unsupported trace class's environment entry value type: " | |
1282 | "type=%s", | |
1283 | bt_common_value_type_string( | |
1284 | bt_value_get_type(val))); | |
1285 | goto end; | |
1286 | } | |
1287 | } | |
1288 | ||
1289 | tc = fs_sink_ctf_trace_class_create(ir_tc); | |
1290 | BT_ASSERT(tc); | |
1291 | ||
1292 | end: | |
1293 | return tc; | |
1294 | } |