Commit | Line | Data |
---|---|---|
b2f1f465 JD |
1 | /* |
2 | * clock-fields.c | |
3 | * | |
4 | * Babeltrace - Update clock fields to write uint64 values | |
5 | * | |
6 | * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com> | |
7 | * | |
8 | * Author: Julien Desfossez <jdesfossez@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <babeltrace/ctf-ir/event.h> | |
30 | #include <babeltrace/ctf-ir/packet.h> | |
31 | #include <babeltrace/ctf-ir/event-class.h> | |
32 | #include <babeltrace/ctf-ir/stream.h> | |
33 | #include <babeltrace/ctf-ir/stream-class.h> | |
34 | #include <babeltrace/ctf-ir/clock-class.h> | |
35 | #include <babeltrace/ctf-ir/fields.h> | |
36 | #include <babeltrace/ctf-writer/stream.h> | |
37 | #include <babeltrace/ctf-ir/field-types.h> | |
38 | #include <assert.h> | |
39 | #include <stdio.h> | |
40 | ||
41 | #include "clock-fields.h" | |
42 | ||
43 | static | |
44 | int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
45 | struct bt_ctf_clock_class *writer_clock_class); | |
46 | static | |
47 | int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
48 | struct bt_ctf_clock_class *writer_clock_class); | |
49 | static | |
50 | int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
51 | struct bt_ctf_clock_class *writer_clock_class); | |
52 | static | |
53 | int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
54 | struct bt_ctf_clock_class *writer_clock_class); | |
55 | static | |
56 | int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
57 | struct bt_ctf_clock_class *writer_clock_class); | |
58 | ||
59 | static | |
60 | int copy_find_clock_int_field(FILE *err, | |
61 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
62 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
63 | struct bt_ctf_field *copy_field); | |
64 | static | |
65 | int copy_find_clock_struct_field(FILE *err, | |
66 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
67 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
68 | struct bt_ctf_field *copy_field); | |
69 | static | |
70 | int copy_find_clock_array_field(FILE *err, | |
71 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
72 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
73 | struct bt_ctf_field *copy_field); | |
74 | static | |
75 | int copy_find_clock_sequence_field(FILE *err, | |
76 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
77 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
78 | struct bt_ctf_field *copy_field); | |
79 | static | |
80 | int copy_find_clock_variant_field(FILE *err, struct bt_ctf_event *event, | |
81 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
82 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field); | |
83 | static | |
84 | int copy_find_clock_enum_field(FILE *err, struct bt_ctf_event *event, | |
85 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
86 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field); | |
87 | ||
88 | static | |
89 | int update_header_clock_int_field_type(FILE *err, struct bt_ctf_field_type *type, | |
90 | struct bt_ctf_clock_class *writer_clock_class) | |
91 | { | |
9ae49d3d | 92 | struct bt_ctf_clock_class *clock = NULL; |
b2f1f465 JD |
93 | int ret; |
94 | ||
95 | clock = bt_ctf_field_type_integer_get_mapped_clock_class(type); | |
96 | if (!clock) { | |
97 | return 0; | |
98 | } | |
9ae49d3d | 99 | BT_PUT(clock); |
b2f1f465 JD |
100 | |
101 | ret = bt_ctf_field_type_integer_set_size(type, 64); | |
102 | if (ret) { | |
103 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
104 | __LINE__); | |
105 | goto end; | |
106 | } | |
107 | ||
108 | ret = bt_ctf_field_type_integer_set_mapped_clock_class(type, | |
109 | writer_clock_class); | |
110 | if (ret) { | |
111 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
112 | __LINE__); | |
113 | goto end; | |
114 | } | |
115 | ||
116 | end: | |
117 | return ret; | |
118 | } | |
119 | ||
120 | static | |
121 | int find_update_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
122 | struct bt_ctf_clock_class *writer_clock_class) | |
123 | { | |
124 | int ret; | |
125 | ||
126 | switch (bt_ctf_field_type_get_type_id(type)) { | |
1487a16a | 127 | case BT_CTF_FIELD_TYPE_ID_INTEGER: |
b2f1f465 JD |
128 | return update_header_clock_int_field_type(err, type, |
129 | writer_clock_class); | |
1487a16a | 130 | case BT_CTF_FIELD_TYPE_ID_STRUCT: |
b2f1f465 JD |
131 | return find_update_struct_clock_fields(err, type, |
132 | writer_clock_class); | |
1487a16a | 133 | case BT_CTF_FIELD_TYPE_ID_ARRAY: |
b2f1f465 JD |
134 | return find_update_array_clock_fields(err, type, |
135 | writer_clock_class); | |
1487a16a | 136 | case BT_CTF_FIELD_TYPE_ID_SEQUENCE: |
b2f1f465 JD |
137 | return find_update_sequence_clock_fields(err, type, |
138 | writer_clock_class); | |
1487a16a | 139 | case BT_CTF_FIELD_TYPE_ID_VARIANT: |
b2f1f465 JD |
140 | return find_update_variant_clock_fields(err, type, |
141 | writer_clock_class); | |
1487a16a | 142 | case BT_CTF_FIELD_TYPE_ID_ENUM: |
b2f1f465 JD |
143 | return find_update_enum_clock_fields(err, type, |
144 | writer_clock_class); | |
145 | break; | |
146 | default: | |
147 | break; | |
148 | } | |
149 | ||
150 | ret = 0; | |
151 | ||
152 | return ret; | |
153 | } | |
154 | ||
155 | static | |
156 | int find_update_variant_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
157 | struct bt_ctf_clock_class *writer_clock_class) | |
158 | { | |
159 | int count, i, ret; | |
9ae49d3d | 160 | struct bt_ctf_field_type *entry_type = NULL; |
b2f1f465 JD |
161 | |
162 | count = bt_ctf_field_type_variant_get_field_count(type); | |
163 | for (i = 0; i < count; i++) { | |
b2f1f465 JD |
164 | const char *entry_name; |
165 | ||
166 | ret = bt_ctf_field_type_variant_get_field(type, | |
167 | &entry_name, &entry_type, i); | |
168 | if (ret) { | |
169 | fprintf(err, "[error] %s in %s:%d\n", | |
170 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 171 | goto error; |
b2f1f465 JD |
172 | } |
173 | ret = find_update_clock_fields(err, entry_type, | |
174 | writer_clock_class); | |
b2f1f465 JD |
175 | if (ret) { |
176 | fprintf(err, "[error] %s in %s:%d\n", | |
177 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 178 | goto error; |
b2f1f465 | 179 | } |
9ae49d3d | 180 | BT_PUT(entry_type); |
b2f1f465 JD |
181 | } |
182 | ||
183 | ret = 0; | |
9ae49d3d | 184 | goto end; |
b2f1f465 | 185 | |
9ae49d3d JD |
186 | error: |
187 | ret = -1; | |
b2f1f465 | 188 | end: |
9ae49d3d | 189 | bt_put(entry_type); |
b2f1f465 JD |
190 | return ret; |
191 | } | |
192 | ||
193 | static | |
194 | int find_update_struct_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
195 | struct bt_ctf_clock_class *writer_clock_class) | |
196 | { | |
197 | int count, i, ret; | |
9ae49d3d | 198 | struct bt_ctf_field_type *entry_type = NULL; |
b2f1f465 JD |
199 | |
200 | count = bt_ctf_field_type_structure_get_field_count(type); | |
201 | for (i = 0; i < count; i++) { | |
b2f1f465 JD |
202 | const char *entry_name; |
203 | ||
204 | ret = bt_ctf_field_type_structure_get_field(type, | |
205 | &entry_name, &entry_type, i); | |
206 | if (ret) { | |
207 | fprintf(err, "[error] %s in %s:%d\n", | |
208 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 209 | goto error; |
b2f1f465 JD |
210 | } |
211 | ret = find_update_clock_fields(err, entry_type, | |
212 | writer_clock_class); | |
b2f1f465 JD |
213 | if (ret) { |
214 | fprintf(err, "[error] %s in %s:%d\n", | |
215 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 216 | goto error; |
b2f1f465 | 217 | } |
9ae49d3d | 218 | BT_PUT(entry_type); |
b2f1f465 JD |
219 | } |
220 | ||
221 | ret = 0; | |
9ae49d3d | 222 | goto end; |
b2f1f465 | 223 | |
9ae49d3d JD |
224 | error: |
225 | bt_put(entry_type); | |
b2f1f465 JD |
226 | end: |
227 | return ret; | |
228 | } | |
229 | ||
230 | static | |
231 | int find_update_sequence_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
232 | struct bt_ctf_clock_class *writer_clock_class) | |
233 | { | |
234 | int ret; | |
9ae49d3d | 235 | struct bt_ctf_field_type *entry_type = NULL; |
b2f1f465 JD |
236 | |
237 | entry_type = bt_ctf_field_type_sequence_get_element_type(type); | |
9ae49d3d JD |
238 | if (!entry_type) { |
239 | fprintf(err, "[error] %s in %s:%d\n", | |
240 | __func__, __FILE__, __LINE__); | |
241 | goto error; | |
242 | } | |
b2f1f465 | 243 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
9ae49d3d | 244 | BT_PUT(entry_type); |
b2f1f465 JD |
245 | if (ret) { |
246 | fprintf(err, "[error] %s in %s:%d\n", | |
247 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 248 | goto error; |
b2f1f465 JD |
249 | } |
250 | ||
251 | ret = 0; | |
9ae49d3d | 252 | goto end; |
b2f1f465 | 253 | |
9ae49d3d JD |
254 | error: |
255 | ret = -1; | |
b2f1f465 JD |
256 | end: |
257 | return ret; | |
258 | } | |
259 | ||
260 | static | |
261 | int find_update_array_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
262 | struct bt_ctf_clock_class *writer_clock_class) | |
263 | { | |
264 | int ret; | |
9ae49d3d | 265 | struct bt_ctf_field_type *entry_type = NULL; |
b2f1f465 JD |
266 | |
267 | entry_type = bt_ctf_field_type_array_get_element_type(type); | |
9ae49d3d JD |
268 | if (!entry_type) { |
269 | fprintf(err, "[error] %s in %s:%d\n", | |
270 | __func__, __FILE__, __LINE__); | |
271 | goto error; | |
272 | } | |
b2f1f465 | 273 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
9ae49d3d | 274 | BT_PUT(entry_type); |
b2f1f465 JD |
275 | if (ret) { |
276 | fprintf(err, "[error] %s in %s:%d\n", | |
277 | __func__, __FILE__, __LINE__); | |
278 | ret = -1; | |
279 | goto end; | |
280 | } | |
281 | ||
282 | ret = 0; | |
9ae49d3d | 283 | goto end; |
b2f1f465 | 284 | |
9ae49d3d JD |
285 | error: |
286 | ret = -1; | |
b2f1f465 JD |
287 | end: |
288 | return ret; | |
289 | } | |
290 | ||
291 | static | |
292 | int find_update_enum_clock_fields(FILE *err, struct bt_ctf_field_type *type, | |
293 | struct bt_ctf_clock_class *writer_clock_class) | |
294 | { | |
295 | int ret; | |
9ae49d3d | 296 | struct bt_ctf_field_type *entry_type = NULL; |
b2f1f465 JD |
297 | |
298 | entry_type = bt_ctf_field_type_enumeration_get_container_type(type); | |
9ae49d3d JD |
299 | if (!entry_type) { |
300 | fprintf(err, "[error] %s in %s:%d\n", | |
301 | __func__, __FILE__, __LINE__); | |
302 | goto error; | |
303 | } | |
b2f1f465 | 304 | ret = find_update_clock_fields(err, entry_type, writer_clock_class); |
9ae49d3d | 305 | BT_PUT(entry_type); |
b2f1f465 JD |
306 | if (ret) { |
307 | fprintf(err, "[error] %s in %s:%d\n", | |
308 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 309 | goto error; |
b2f1f465 JD |
310 | } |
311 | ||
312 | ret = 0; | |
9ae49d3d | 313 | goto end; |
b2f1f465 | 314 | |
9ae49d3d JD |
315 | error: |
316 | ret = -1; | |
b2f1f465 JD |
317 | end: |
318 | return ret; | |
319 | } | |
320 | ||
321 | BT_HIDDEN | |
322 | struct bt_ctf_field_type *override_header_type(FILE *err, | |
323 | struct bt_ctf_field_type *type, | |
324 | struct bt_ctf_trace *writer_trace) | |
325 | { | |
326 | struct bt_ctf_field_type *new_type = NULL; | |
9ae49d3d | 327 | struct bt_ctf_clock_class *writer_clock_class = NULL; |
b2f1f465 | 328 | int ret; |
b2f1f465 JD |
329 | |
330 | /* FIXME multi-clock? */ | |
9ac68eb1 | 331 | writer_clock_class = bt_ctf_trace_get_clock_class_by_index(writer_trace, 0); |
b2f1f465 JD |
332 | if (!writer_clock_class) { |
333 | fprintf(err, "[error] %s in %s:%d\n", | |
334 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 335 | goto error; |
b2f1f465 JD |
336 | } |
337 | ||
338 | new_type = bt_ctf_field_type_copy(type); | |
339 | if (!new_type) { | |
340 | fprintf(err, "[error] %s in %s:%d\n", | |
341 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 342 | goto error; |
b2f1f465 JD |
343 | } |
344 | ||
1487a16a | 345 | if (bt_ctf_field_type_get_type_id(new_type) != BT_CTF_FIELD_TYPE_ID_STRUCT) { |
b2f1f465 JD |
346 | fprintf(err, "[error] Unexpected header field type\n"); |
347 | goto error; | |
348 | } | |
349 | ||
350 | ret = find_update_struct_clock_fields(err, new_type, writer_clock_class); | |
351 | if (ret) { | |
352 | fprintf(err, "[error] %s in %s:%d\n", | |
353 | __func__, __FILE__, __LINE__); | |
354 | goto error; | |
355 | } | |
9ae49d3d | 356 | BT_PUT(writer_clock_class); |
b2f1f465 | 357 | |
9ae49d3d | 358 | goto end; |
b2f1f465 JD |
359 | |
360 | error: | |
b2f1f465 | 361 | bt_put(writer_clock_class); |
9ae49d3d | 362 | BT_PUT(new_type); |
b2f1f465 JD |
363 | end: |
364 | return new_type; | |
365 | } | |
366 | ||
367 | static | |
368 | int copy_float_field(FILE *err, struct bt_ctf_field *field, | |
369 | struct bt_ctf_field_type *type, | |
370 | struct bt_ctf_field *copy_field) | |
371 | { | |
372 | double value; | |
373 | int ret; | |
374 | ||
375 | ret = bt_ctf_field_floating_point_get_value(field, &value); | |
376 | if (ret) { | |
377 | ret = -1; | |
378 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
379 | __FILE__, __LINE__); | |
380 | goto end; | |
381 | } | |
382 | ret = bt_ctf_field_floating_point_set_value(copy_field, value); | |
383 | if (ret) { | |
384 | ret = -1; | |
385 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
386 | __FILE__, __LINE__); | |
387 | goto end; | |
388 | } | |
389 | ||
390 | ret = 0; | |
391 | ||
392 | end: | |
393 | return ret; | |
394 | } | |
395 | ||
396 | static | |
397 | int copy_string_field(FILE *err, struct bt_ctf_field *field, | |
398 | struct bt_ctf_field_type *type, | |
399 | struct bt_ctf_field *copy_field) | |
400 | { | |
401 | const char *value; | |
402 | int ret; | |
403 | ||
404 | value = bt_ctf_field_string_get_value(field); | |
405 | if (!value) { | |
406 | ret = -1; | |
407 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
408 | __FILE__, __LINE__); | |
409 | goto end; | |
410 | } | |
411 | ret = bt_ctf_field_string_set_value(copy_field, value); | |
412 | if (ret) { | |
413 | ret = -1; | |
414 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
415 | __FILE__, __LINE__); | |
416 | goto end; | |
417 | } | |
418 | ||
419 | ret = 0; | |
420 | ||
421 | end: | |
422 | return ret; | |
423 | } | |
424 | ||
425 | BT_HIDDEN | |
426 | int copy_override_field(FILE *err, struct bt_ctf_event *event, | |
427 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
428 | struct bt_ctf_field *copy_field) | |
429 | { | |
9ae49d3d | 430 | struct bt_ctf_field_type *type = NULL; |
b2f1f465 JD |
431 | int ret; |
432 | ||
433 | type = bt_ctf_field_get_type(field); | |
434 | if (!type) { | |
435 | ret = -1; | |
436 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
437 | __LINE__); | |
438 | goto end; | |
439 | } | |
440 | ||
441 | switch (bt_ctf_field_type_get_type_id(type)) { | |
1487a16a | 442 | case BT_CTF_FIELD_TYPE_ID_INTEGER: |
b2f1f465 JD |
443 | ret = copy_find_clock_int_field(err, event, writer_event, |
444 | field, type, copy_field); | |
445 | break; | |
1487a16a | 446 | case BT_CTF_FIELD_TYPE_ID_STRUCT: |
b2f1f465 JD |
447 | ret = copy_find_clock_struct_field(err, event, writer_event, |
448 | field, type, copy_field); | |
449 | break; | |
1487a16a | 450 | case BT_CTF_FIELD_TYPE_ID_FLOAT: |
b2f1f465 JD |
451 | ret = copy_float_field(err, field, type, copy_field); |
452 | break; | |
1487a16a | 453 | case BT_CTF_FIELD_TYPE_ID_ENUM: |
b2f1f465 JD |
454 | ret = copy_find_clock_enum_field(err, event, writer_event, |
455 | field, type, copy_field); | |
456 | break; | |
1487a16a | 457 | case BT_CTF_FIELD_TYPE_ID_STRING: |
b2f1f465 JD |
458 | ret = copy_string_field(err, field, type, copy_field); |
459 | break; | |
1487a16a | 460 | case BT_CTF_FIELD_TYPE_ID_ARRAY: |
b2f1f465 JD |
461 | ret = copy_find_clock_array_field(err, event, writer_event, |
462 | field, type, copy_field); | |
463 | break; | |
1487a16a | 464 | case BT_CTF_FIELD_TYPE_ID_SEQUENCE: |
b2f1f465 JD |
465 | ret = copy_find_clock_sequence_field(err, event, writer_event, |
466 | field, type, copy_field); | |
467 | break; | |
1487a16a | 468 | case BT_CTF_FIELD_TYPE_ID_VARIANT: |
b2f1f465 JD |
469 | ret = copy_find_clock_variant_field(err, event, writer_event, |
470 | field, type, copy_field); | |
471 | break; | |
472 | /* No default, we want to catch missing field types. */ | |
1487a16a | 473 | case BT_CTF_FIELD_TYPE_ID_UNKNOWN: |
b2f1f465 JD |
474 | case BT_CTF_NR_TYPE_IDS: |
475 | break; | |
476 | } | |
477 | ||
478 | ret = 0; | |
9ae49d3d | 479 | BT_PUT(type); |
b2f1f465 JD |
480 | |
481 | end: | |
482 | return ret; | |
483 | } | |
484 | ||
485 | static | |
486 | int copy_find_clock_enum_field(FILE *err, struct bt_ctf_event *event, | |
487 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
488 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field) | |
489 | { | |
490 | int ret; | |
9ae49d3d | 491 | struct bt_ctf_field *container = NULL, *copy_container = NULL; |
b2f1f465 JD |
492 | |
493 | container = bt_ctf_field_enumeration_get_container(field); | |
494 | if (!container) { | |
495 | ret = -1; | |
496 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
497 | __LINE__); | |
498 | goto end; | |
499 | } | |
500 | ||
501 | copy_container = bt_ctf_field_enumeration_get_container(copy_field); | |
502 | if (!copy_container) { | |
503 | ret = -1; | |
504 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
505 | __LINE__); | |
9ae49d3d | 506 | goto error; |
b2f1f465 JD |
507 | } |
508 | ||
509 | ret = copy_override_field(err, event, writer_event, container, | |
510 | copy_container); | |
511 | if (ret) { | |
512 | ret = -1; | |
513 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
514 | __LINE__); | |
9ae49d3d | 515 | goto error; |
b2f1f465 | 516 | } |
9ac68eb1 | 517 | |
9ae49d3d JD |
518 | ret = 0; |
519 | goto end; | |
b2f1f465 | 520 | |
9ae49d3d JD |
521 | error: |
522 | ret = -1; | |
523 | end: | |
b2f1f465 | 524 | bt_put(copy_container); |
b2f1f465 | 525 | bt_put(container); |
b2f1f465 JD |
526 | return ret; |
527 | } | |
528 | ||
529 | static | |
530 | int copy_find_clock_variant_field(FILE *err, struct bt_ctf_event *event, | |
531 | struct bt_ctf_event *writer_event, struct bt_ctf_field *field, | |
532 | struct bt_ctf_field_type *type, struct bt_ctf_field *copy_field) | |
533 | { | |
534 | int ret; | |
9ae49d3d JD |
535 | struct bt_ctf_field *tag = NULL; |
536 | struct bt_ctf_field *variant_field = NULL, *copy_variant_field = NULL; | |
b2f1f465 JD |
537 | |
538 | tag = bt_ctf_field_variant_get_tag(field); | |
539 | if (!tag) { | |
b2f1f465 JD |
540 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
541 | __LINE__); | |
9ae49d3d | 542 | goto error; |
b2f1f465 JD |
543 | } |
544 | ||
545 | variant_field = bt_ctf_field_variant_get_field(field, tag); | |
546 | if (!variant_field) { | |
b2f1f465 JD |
547 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
548 | __LINE__); | |
9ae49d3d | 549 | goto error; |
b2f1f465 JD |
550 | } |
551 | ||
552 | copy_variant_field = bt_ctf_field_variant_get_field(copy_field, tag); | |
553 | if (!copy_variant_field) { | |
b2f1f465 JD |
554 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
555 | __LINE__); | |
9ae49d3d | 556 | goto error; |
b2f1f465 JD |
557 | } |
558 | ||
559 | ret = copy_override_field(err, event, writer_event, variant_field, | |
560 | copy_variant_field); | |
561 | if (ret) { | |
b2f1f465 JD |
562 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
563 | __LINE__); | |
9ae49d3d | 564 | goto error; |
b2f1f465 JD |
565 | } |
566 | ||
567 | ret = 0; | |
9ae49d3d | 568 | goto end; |
b2f1f465 | 569 | |
9ae49d3d JD |
570 | error: |
571 | ret = -1; | |
572 | end: | |
b2f1f465 | 573 | bt_put(copy_variant_field); |
b2f1f465 | 574 | bt_put(variant_field); |
b2f1f465 | 575 | bt_put(tag); |
b2f1f465 JD |
576 | return ret; |
577 | } | |
578 | ||
579 | static | |
580 | int copy_find_clock_sequence_field(FILE *err, | |
581 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
582 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
583 | struct bt_ctf_field *copy_field) | |
584 | { | |
585 | int ret; | |
586 | uint64_t i, count; | |
9ae49d3d JD |
587 | struct bt_ctf_field *length_field = NULL; |
588 | struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL; | |
b2f1f465 JD |
589 | |
590 | length_field = bt_ctf_field_sequence_get_length(field); | |
591 | if (!length_field) { | |
b2f1f465 JD |
592 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
593 | __LINE__); | |
9ae49d3d | 594 | goto error; |
b2f1f465 JD |
595 | } |
596 | ||
597 | ret = bt_ctf_field_unsigned_integer_get_value(length_field, &count); | |
598 | if (ret) { | |
b2f1f465 JD |
599 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
600 | __LINE__); | |
9ae49d3d | 601 | goto error; |
b2f1f465 JD |
602 | } |
603 | ||
604 | ret = bt_ctf_field_sequence_set_length(copy_field, length_field); | |
b2f1f465 | 605 | if (ret) { |
b2f1f465 JD |
606 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
607 | __LINE__); | |
9ae49d3d | 608 | goto error; |
b2f1f465 | 609 | } |
9ae49d3d | 610 | BT_PUT(length_field); |
b2f1f465 JD |
611 | |
612 | for (i = 0; i < count; i++) { | |
b2f1f465 JD |
613 | entry_field = bt_ctf_field_sequence_get_field(field, i); |
614 | if (!entry_field) { | |
b2f1f465 JD |
615 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
616 | __LINE__); | |
9ae49d3d | 617 | goto error; |
b2f1f465 JD |
618 | } |
619 | ||
620 | entry_copy = bt_ctf_field_sequence_get_field(copy_field, i); | |
621 | if (!entry_copy) { | |
b2f1f465 JD |
622 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
623 | __LINE__); | |
9ae49d3d | 624 | goto error; |
b2f1f465 JD |
625 | } |
626 | ||
627 | ret = copy_override_field(err, event, writer_event, entry_field, | |
628 | entry_copy); | |
b2f1f465 | 629 | if (ret) { |
b2f1f465 JD |
630 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
631 | __LINE__); | |
9ae49d3d | 632 | goto error; |
b2f1f465 | 633 | } |
9ae49d3d JD |
634 | BT_PUT(entry_field); |
635 | BT_PUT(entry_copy); | |
b2f1f465 JD |
636 | } |
637 | ||
b2f1f465 | 638 | ret = 0; |
9ae49d3d | 639 | goto end; |
b2f1f465 | 640 | |
9ae49d3d JD |
641 | error: |
642 | bt_put(length_field); | |
643 | bt_put(entry_field); | |
644 | bt_put(entry_copy); | |
645 | ret = -1; | |
b2f1f465 JD |
646 | end: |
647 | return ret; | |
648 | } | |
649 | ||
650 | static | |
651 | int copy_find_clock_array_field(FILE *err, | |
652 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
653 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
654 | struct bt_ctf_field *copy_field) | |
655 | { | |
656 | int ret, count, i; | |
9ae49d3d | 657 | struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL; |
b2f1f465 JD |
658 | |
659 | count = bt_ctf_field_type_array_get_length(type); | |
b2f1f465 | 660 | for (i = 0; i < count; i++) { |
b2f1f465 JD |
661 | entry_field = bt_ctf_field_array_get_field(field, i); |
662 | if (!entry_field) { | |
53296853 | 663 | ret = -1; |
b2f1f465 JD |
664 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
665 | __LINE__); | |
9ae49d3d | 666 | goto error; |
b2f1f465 JD |
667 | } |
668 | ||
669 | entry_copy = bt_ctf_field_array_get_field(copy_field, i); | |
670 | if (!entry_copy) { | |
53296853 | 671 | ret = -1; |
b2f1f465 JD |
672 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
673 | __LINE__); | |
9ae49d3d | 674 | goto error; |
b2f1f465 JD |
675 | } |
676 | ||
677 | ret = copy_override_field(err, event, writer_event, entry_field, | |
678 | entry_copy); | |
b2f1f465 JD |
679 | if (ret) { |
680 | ret = -1; | |
681 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
682 | __LINE__); | |
9ae49d3d | 683 | goto error; |
b2f1f465 | 684 | } |
9ae49d3d JD |
685 | BT_PUT(entry_field); |
686 | BT_PUT(entry_copy); | |
b2f1f465 JD |
687 | } |
688 | ||
b2f1f465 | 689 | ret = 0; |
9ae49d3d JD |
690 | goto end; |
691 | ||
692 | error: | |
693 | bt_put(entry_field); | |
694 | bt_put(entry_copy); | |
b2f1f465 JD |
695 | |
696 | end: | |
697 | return ret; | |
698 | } | |
699 | ||
700 | static | |
701 | int copy_find_clock_struct_field(FILE *err, | |
702 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
703 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
704 | struct bt_ctf_field *copy_field) | |
705 | { | |
706 | int count, i, ret; | |
9ae49d3d JD |
707 | struct bt_ctf_field_type *entry_type = NULL; |
708 | struct bt_ctf_field *entry_field = NULL, *entry_copy = NULL; | |
b2f1f465 JD |
709 | |
710 | count = bt_ctf_field_type_structure_get_field_count(type); | |
711 | for (i = 0; i < count; i++) { | |
b2f1f465 | 712 | const char *entry_name; |
b2f1f465 JD |
713 | |
714 | entry_field = bt_ctf_field_structure_get_field_by_index(field, i); | |
715 | if (!entry_field) { | |
b2f1f465 JD |
716 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
717 | __LINE__); | |
9ae49d3d | 718 | goto error; |
b2f1f465 JD |
719 | } |
720 | ||
721 | ret = bt_ctf_field_type_structure_get_field(type, &entry_name, | |
722 | &entry_type, i); | |
723 | if (ret) { | |
b2f1f465 JD |
724 | fprintf(err, "[error] %s in %s:%d\n", |
725 | __func__, __FILE__, __LINE__); | |
9ae49d3d | 726 | goto error; |
b2f1f465 JD |
727 | } |
728 | ||
729 | entry_copy = bt_ctf_field_structure_get_field_by_index(copy_field, i); | |
730 | if (!entry_copy) { | |
b2f1f465 JD |
731 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
732 | __LINE__); | |
9ae49d3d | 733 | goto error; |
b2f1f465 JD |
734 | } |
735 | ||
736 | ret = copy_override_field(err, event, writer_event, entry_field, | |
737 | entry_copy); | |
b2f1f465 | 738 | if (ret) { |
b2f1f465 JD |
739 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, |
740 | __LINE__); | |
9ae49d3d | 741 | goto error; |
b2f1f465 | 742 | } |
9ae49d3d JD |
743 | BT_PUT(entry_copy); |
744 | BT_PUT(entry_field); | |
745 | BT_PUT(entry_type); | |
b2f1f465 JD |
746 | } |
747 | ||
748 | ret = 0; | |
9ae49d3d | 749 | goto end; |
b2f1f465 | 750 | |
9ae49d3d JD |
751 | error: |
752 | bt_put(entry_type); | |
753 | bt_put(entry_field); | |
754 | bt_put(entry_copy); | |
755 | ret = -1; | |
b2f1f465 JD |
756 | end: |
757 | return ret; | |
758 | } | |
759 | ||
760 | static | |
761 | int set_int_value(FILE *err, struct bt_ctf_field *field, | |
762 | struct bt_ctf_field *copy_field, | |
763 | struct bt_ctf_field_type *type) | |
764 | { | |
765 | uint64_t uvalue; | |
766 | int64_t value; | |
767 | int ret; | |
768 | ||
769 | if (bt_ctf_field_type_integer_get_signed(type)) { | |
770 | ret = bt_ctf_field_signed_integer_get_value(field, &value); | |
771 | if (ret) { | |
772 | ret = -1; | |
773 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
774 | __FILE__, __LINE__); | |
775 | goto end; | |
776 | } | |
b2f1f465 JD |
777 | ret = bt_ctf_field_signed_integer_set_value(copy_field, value); |
778 | if (ret) { | |
779 | ret = -1; | |
780 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
781 | __FILE__, __LINE__); | |
782 | goto end; | |
783 | } | |
784 | } else { | |
785 | ret = bt_ctf_field_unsigned_integer_get_value(field, | |
786 | &uvalue); | |
787 | if (ret) { | |
788 | ret = -1; | |
789 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
790 | __FILE__, __LINE__); | |
791 | goto end; | |
792 | } | |
793 | ret = bt_ctf_field_unsigned_integer_set_value(copy_field, uvalue); | |
794 | if (ret) { | |
795 | ret = -1; | |
796 | fprintf(err, "[error] %s in %s:%d\n", __func__, | |
797 | __FILE__, __LINE__); | |
798 | goto end; | |
799 | } | |
800 | } | |
801 | ret = 0; | |
802 | ||
803 | end: | |
804 | return ret; | |
805 | } | |
806 | ||
807 | struct bt_ctf_clock_class *stream_class_get_clock_class(FILE *err, | |
808 | struct bt_ctf_stream_class *stream_class) | |
809 | { | |
9ae49d3d | 810 | struct bt_ctf_trace *trace = NULL; |
b2f1f465 JD |
811 | struct bt_ctf_clock_class *clock_class = NULL; |
812 | ||
813 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
814 | if (!trace) { | |
815 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
816 | __LINE__); | |
817 | goto end; | |
818 | } | |
819 | ||
820 | /* FIXME multi-clock? */ | |
9ac68eb1 | 821 | clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0); |
b2f1f465 JD |
822 | |
823 | bt_put(trace); | |
824 | end: | |
825 | return clock_class; | |
826 | } | |
827 | ||
828 | struct bt_ctf_clock_class *event_get_clock_class(FILE *err, struct bt_ctf_event *event) | |
829 | { | |
9ae49d3d JD |
830 | struct bt_ctf_event_class *event_class = NULL; |
831 | struct bt_ctf_stream_class *stream_class = NULL; | |
b2f1f465 JD |
832 | struct bt_ctf_clock_class *clock_class = NULL; |
833 | ||
834 | event_class = bt_ctf_event_get_class(event); | |
835 | if (!event_class) { | |
836 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
837 | __LINE__); | |
838 | goto end; | |
839 | } | |
840 | ||
841 | stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
9ae49d3d | 842 | BT_PUT(event_class); |
b2f1f465 JD |
843 | if (!stream_class) { |
844 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
845 | __LINE__); | |
9ae49d3d | 846 | goto end; |
b2f1f465 JD |
847 | } |
848 | ||
849 | clock_class = stream_class_get_clock_class(err, stream_class); | |
850 | bt_put(stream_class); | |
851 | ||
b2f1f465 JD |
852 | end: |
853 | return clock_class; | |
854 | } | |
855 | ||
856 | static | |
857 | int copy_find_clock_int_field(FILE *err, | |
858 | struct bt_ctf_event *event, struct bt_ctf_event *writer_event, | |
859 | struct bt_ctf_field *field, struct bt_ctf_field_type *type, | |
860 | struct bt_ctf_field *copy_field) | |
861 | { | |
9ae49d3d JD |
862 | struct bt_ctf_clock_class *clock_class = NULL, *writer_clock_class = NULL; |
863 | struct bt_ctf_clock_value *clock_value = NULL, *writer_clock_value = NULL; | |
b2f1f465 JD |
864 | uint64_t value; |
865 | int ret; | |
866 | ||
867 | clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(type); | |
868 | if (!clock_class) { | |
869 | return set_int_value(err, field, copy_field, type); | |
870 | } | |
871 | ||
872 | clock_value = bt_ctf_event_get_clock_value(event, clock_class); | |
9ae49d3d | 873 | BT_PUT(clock_class); |
b2f1f465 JD |
874 | if (!clock_value) { |
875 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
876 | __LINE__); | |
9ae49d3d | 877 | goto error; |
b2f1f465 JD |
878 | } |
879 | ||
880 | ret = bt_ctf_clock_value_get_value(clock_value, &value); | |
9ae49d3d | 881 | BT_PUT(clock_value); |
b2f1f465 JD |
882 | if (ret) { |
883 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
884 | __LINE__); | |
9ae49d3d | 885 | goto error; |
b2f1f465 JD |
886 | } |
887 | ||
888 | ret = bt_ctf_field_unsigned_integer_set_value(copy_field, value); | |
889 | if (ret) { | |
890 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
891 | __LINE__); | |
9ae49d3d | 892 | goto error; |
b2f1f465 JD |
893 | } |
894 | ||
895 | writer_clock_class = event_get_clock_class(err, writer_event); | |
896 | if (!writer_clock_class) { | |
897 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
898 | __LINE__); | |
9ae49d3d | 899 | goto error; |
b2f1f465 JD |
900 | } |
901 | ||
902 | writer_clock_value = bt_ctf_clock_value_create(writer_clock_class, value); | |
9ae49d3d | 903 | BT_PUT(writer_clock_class); |
b2f1f465 JD |
904 | if (!writer_clock_value) { |
905 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
906 | __LINE__); | |
9ae49d3d | 907 | goto error; |
b2f1f465 JD |
908 | } |
909 | ||
910 | ret = bt_ctf_event_set_clock_value(writer_event, writer_clock_value); | |
9ae49d3d | 911 | BT_PUT(writer_clock_value); |
b2f1f465 JD |
912 | if (ret) { |
913 | fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, | |
914 | __LINE__); | |
9ae49d3d | 915 | goto error; |
b2f1f465 JD |
916 | } |
917 | ||
918 | ret = 0; | |
9ae49d3d | 919 | goto end; |
b2f1f465 | 920 | |
9ae49d3d JD |
921 | error: |
922 | ret = -1; | |
b2f1f465 JD |
923 | end: |
924 | return ret; | |
925 | } | |
926 |