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