Commit | Line | Data |
---|---|---|
8deee039 PP |
1 | /* |
2 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
25 | #define BT_LOG_TAG "CTF-WRITER-FIELDS" | |
26 | #include <babeltrace/lib-logging-internal.h> | |
27 | ||
28 | #include <babeltrace/compat/fcntl-internal.h> | |
29 | #include <babeltrace/ctf-writer/fields-internal.h> | |
30 | #include <babeltrace/ctf-writer/field-types-internal.h> | |
31 | #include <babeltrace/ctf-writer/serialize-internal.h> | |
32 | #include <babeltrace/ctf-ir/fields-internal.h> | |
33 | #include <babeltrace/ctf-ir/field-types-internal.h> | |
34 | #include <babeltrace/assert-pre-internal.h> | |
35 | #include <babeltrace/object-internal.h> | |
36 | #include <babeltrace/ref.h> | |
37 | #include <babeltrace/compiler-internal.h> | |
38 | #include <babeltrace/endian-internal.h> | |
39 | #include <babeltrace/assert-internal.h> | |
40 | #include <float.h> | |
41 | #include <inttypes.h> | |
42 | #include <stdlib.h> | |
43 | ||
44 | static struct bt_field_common_methods bt_ctf_field_integer_methods = { | |
45 | .freeze = bt_field_common_generic_freeze, | |
46 | .validate = bt_field_common_generic_validate, | |
47 | .copy = NULL, | |
48 | .is_set = bt_field_common_generic_is_set, | |
49 | .reset = bt_field_common_generic_reset, | |
50 | }; | |
51 | ||
52 | static struct bt_field_common_methods bt_ctf_field_floating_point_methods = { | |
53 | .freeze = bt_field_common_generic_freeze, | |
54 | .validate = bt_field_common_generic_validate, | |
55 | .copy = NULL, | |
56 | .is_set = bt_field_common_generic_is_set, | |
57 | .reset = bt_field_common_generic_reset, | |
58 | }; | |
59 | ||
60 | static struct bt_field_common_methods bt_ctf_field_enumeration_methods = { | |
61 | .freeze = bt_field_common_enumeration_freeze_recursive, | |
62 | .validate = bt_field_common_enumeration_validate_recursive, | |
63 | .copy = NULL, | |
64 | .is_set = bt_field_common_enumeration_is_set_recursive, | |
65 | .reset = bt_field_common_enumeration_reset_recursive, | |
66 | }; | |
67 | ||
68 | static struct bt_field_common_methods bt_ctf_field_string_methods = { | |
69 | .freeze = bt_field_common_generic_freeze, | |
70 | .validate = bt_field_common_generic_validate, | |
71 | .copy = NULL, | |
72 | .is_set = bt_field_common_generic_is_set, | |
73 | .reset = bt_field_common_generic_reset, | |
74 | }; | |
75 | ||
76 | static struct bt_field_common_methods bt_ctf_field_structure_methods = { | |
77 | .freeze = bt_field_common_structure_freeze_recursive, | |
78 | .validate = bt_field_common_structure_validate_recursive, | |
79 | .copy = NULL, | |
80 | .is_set = bt_field_common_structure_is_set_recursive, | |
81 | .reset = bt_field_common_structure_reset_recursive, | |
82 | }; | |
83 | ||
84 | static struct bt_field_common_methods bt_ctf_field_sequence_methods = { | |
85 | .freeze = bt_field_common_sequence_freeze_recursive, | |
86 | .validate = bt_field_common_sequence_validate_recursive, | |
87 | .copy = NULL, | |
88 | .is_set = bt_field_common_sequence_is_set_recursive, | |
89 | .reset = bt_field_common_sequence_reset_recursive, | |
90 | }; | |
91 | ||
92 | static struct bt_field_common_methods bt_ctf_field_array_methods = { | |
93 | .freeze = bt_field_common_array_freeze_recursive, | |
94 | .validate = bt_field_common_array_validate_recursive, | |
95 | .copy = NULL, | |
96 | .is_set = bt_field_common_array_is_set_recursive, | |
97 | .reset = bt_field_common_array_reset_recursive, | |
98 | }; | |
99 | ||
100 | static struct bt_field_common_methods bt_ctf_field_variant_methods = { | |
101 | .freeze = bt_field_common_variant_freeze_recursive, | |
102 | .validate = bt_field_common_variant_validate_recursive, | |
103 | .copy = NULL, | |
104 | .is_set = bt_field_common_variant_is_set_recursive, | |
105 | .reset = bt_field_common_variant_reset_recursive, | |
106 | }; | |
107 | ||
108 | static | |
109 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *); | |
110 | ||
111 | static | |
112 | struct bt_ctf_field *bt_ctf_field_enumeration_create(struct bt_ctf_field_type *); | |
113 | ||
114 | static | |
115 | struct bt_ctf_field *bt_ctf_field_floating_point_create(struct bt_ctf_field_type *); | |
116 | ||
117 | static | |
118 | struct bt_ctf_field *bt_ctf_field_structure_create(struct bt_ctf_field_type *); | |
119 | ||
120 | static | |
121 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *); | |
122 | ||
123 | static | |
124 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *); | |
125 | ||
126 | static | |
127 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *); | |
128 | ||
129 | static | |
130 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *); | |
131 | ||
132 | static | |
133 | struct bt_ctf_field *(* const field_create_funcs[])(struct bt_ctf_field_type *) = { | |
134 | [BT_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_create, | |
135 | [BT_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_create, | |
136 | [BT_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_floating_point_create, | |
137 | [BT_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_create, | |
138 | [BT_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_create, | |
139 | [BT_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_create, | |
140 | [BT_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_create, | |
141 | [BT_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_create, | |
142 | }; | |
143 | ||
144 | typedef int (*bt_ctf_field_serialize_recursive_func)( | |
145 | struct bt_field_common *, struct bt_ctf_stream_pos *, | |
146 | enum bt_ctf_byte_order); | |
147 | ||
148 | BT_HIDDEN | |
149 | int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field, | |
150 | struct bt_ctf_stream_pos *pos, | |
151 | enum bt_ctf_byte_order native_byte_order) | |
152 | { | |
153 | struct bt_field_common *field_common = (void *) field; | |
154 | bt_ctf_field_serialize_recursive_func serialize_func; | |
155 | ||
156 | BT_ASSERT(pos); | |
157 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
158 | BT_ASSERT(field_common->spec.writer.serialize_func); | |
159 | serialize_func = field_common->spec.writer.serialize_func; | |
160 | return serialize_func(field_common, pos, | |
161 | native_byte_order); | |
162 | } | |
163 | ||
164 | static | |
165 | int increase_packet_size(struct bt_ctf_stream_pos *pos) | |
166 | { | |
167 | int ret; | |
168 | ||
169 | BT_ASSERT(pos); | |
170 | BT_LOGV("Increasing packet size: pos-offset=%" PRId64 ", " | |
171 | "cur-packet-size=%" PRIu64, | |
172 | pos->offset, pos->packet_size); | |
173 | ret = munmap_align(pos->base_mma); | |
174 | if (ret) { | |
175 | BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping", | |
176 | ": ret=%d", ret); | |
177 | goto end; | |
178 | } | |
179 | ||
180 | pos->packet_size += PACKET_LEN_INCREMENT; | |
181 | do { | |
182 | ret = bt_posix_fallocate(pos->fd, pos->mmap_offset, | |
183 | pos->packet_size / CHAR_BIT); | |
184 | } while (ret == EINTR); | |
185 | if (ret) { | |
186 | BT_LOGE_ERRNO("Failed to preallocate memory space", | |
187 | ": ret=%d", ret); | |
188 | errno = EINTR; | |
189 | ret = -1; | |
190 | goto end; | |
191 | } | |
192 | ||
193 | pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot, | |
194 | pos->flags, pos->fd, pos->mmap_offset); | |
195 | if (pos->base_mma == MAP_FAILED) { | |
196 | BT_LOGE_ERRNO("Failed to perform an aligned memory mapping", | |
197 | ": ret=%d", ret); | |
198 | ret = -1; | |
199 | } | |
200 | ||
201 | BT_LOGV("Increased packet size: pos-offset=%" PRId64 ", " | |
202 | "new-packet-size=%" PRIu64, | |
203 | pos->offset, pos->packet_size); | |
204 | BT_ASSERT(pos->packet_size % 8 == 0); | |
205 | ||
206 | end: | |
207 | return ret; | |
208 | } | |
209 | ||
210 | static | |
211 | int bt_ctf_field_integer_serialize(struct bt_field_common *field, | |
212 | struct bt_ctf_stream_pos *pos, | |
213 | enum bt_ctf_byte_order native_byte_order) | |
214 | { | |
215 | int ret = 0; | |
216 | ||
217 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "Integer field"); | |
218 | BT_LOGV("Serializing CTF writer integer field: addr=%p, pos-offset=%" PRId64 ", " | |
219 | "native-bo=%s", field, pos->offset, | |
220 | bt_common_byte_order_string((int) native_byte_order)); | |
221 | ||
222 | retry: | |
223 | ret = bt_ctf_field_integer_write(field, pos, native_byte_order); | |
224 | if (ret == -EFAULT) { | |
225 | /* | |
226 | * The field is too large to fit in the current packet's | |
227 | * remaining space. Bump the packet size and retry. | |
228 | */ | |
229 | ret = increase_packet_size(pos); | |
230 | if (ret) { | |
231 | BT_LOGE("Cannot increase packet size: ret=%d", ret); | |
232 | goto end; | |
233 | } | |
234 | goto retry; | |
235 | } | |
236 | ||
237 | end: | |
238 | return ret; | |
239 | } | |
240 | ||
241 | static | |
242 | int bt_ctf_field_enumeration_serialize_recursive(struct bt_field_common *field, | |
243 | struct bt_ctf_stream_pos *pos, | |
244 | enum bt_ctf_byte_order native_byte_order) | |
245 | { | |
246 | struct bt_field_common_enumeration *enumeration = BT_FROM_COMMON(field); | |
247 | ||
248 | BT_LOGV("Serializing enumeration field: addr=%p, pos-offset=%" PRId64 ", " | |
249 | "native-bo=%s", field, pos->offset, | |
250 | bt_common_byte_order_string((int) native_byte_order)); | |
251 | BT_LOGV_STR("Serializing enumeration field's payload field."); | |
252 | return bt_ctf_field_serialize_recursive((void *) enumeration->payload, | |
253 | pos, native_byte_order); | |
254 | } | |
255 | ||
256 | static | |
257 | int bt_ctf_field_floating_point_serialize(struct bt_field_common *field, | |
258 | struct bt_ctf_stream_pos *pos, | |
259 | enum bt_ctf_byte_order native_byte_order) | |
260 | { | |
261 | int ret = 0; | |
262 | ||
263 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "Floating point number field"); | |
264 | BT_LOGV("Serializing floating point number field: addr=%p, pos-offset=%" PRId64 ", " | |
265 | "native-bo=%s", field, pos->offset, | |
266 | bt_common_byte_order_string((int) native_byte_order)); | |
267 | ||
268 | retry: | |
269 | ret = bt_ctf_field_floating_point_write(field, pos, | |
270 | native_byte_order); | |
271 | if (ret == -EFAULT) { | |
272 | /* | |
273 | * The field is too large to fit in the current packet's | |
274 | * remaining space. Bump the packet size and retry. | |
275 | */ | |
276 | ret = increase_packet_size(pos); | |
277 | if (ret) { | |
278 | BT_LOGE("Cannot increase packet size: ret=%d", ret); | |
279 | goto end; | |
280 | } | |
281 | goto retry; | |
282 | } | |
283 | ||
284 | end: | |
285 | return ret; | |
286 | } | |
287 | ||
288 | static | |
289 | int bt_ctf_field_structure_serialize_recursive(struct bt_field_common *field, | |
290 | struct bt_ctf_stream_pos *pos, | |
291 | enum bt_ctf_byte_order native_byte_order) | |
292 | { | |
293 | int64_t i; | |
294 | int ret = 0; | |
295 | struct bt_field_common_structure *structure = BT_FROM_COMMON(field); | |
296 | ||
297 | BT_LOGV("Serializing structure field: addr=%p, pos-offset=%" PRId64 ", " | |
298 | "native-bo=%s", field, pos->offset, | |
299 | bt_common_byte_order_string((int) native_byte_order)); | |
300 | ||
301 | while (!bt_ctf_stream_pos_access_ok(pos, | |
302 | offset_align(pos->offset, field->type->alignment))) { | |
303 | ret = increase_packet_size(pos); | |
304 | if (ret) { | |
305 | BT_LOGE("Cannot increase packet size: ret=%d", ret); | |
306 | goto end; | |
307 | } | |
308 | } | |
309 | ||
310 | if (!bt_ctf_stream_pos_align(pos, field->type->alignment)) { | |
311 | BT_LOGE("Cannot align packet's position: pos-offset=%" PRId64 ", " | |
312 | "align=%u", pos->offset, field->type->alignment); | |
313 | ret = -1; | |
314 | goto end; | |
315 | } | |
316 | ||
317 | for (i = 0; i < structure->fields->len; i++) { | |
318 | struct bt_field_common *member = g_ptr_array_index( | |
319 | structure->fields, i); | |
320 | const char *field_name = NULL; | |
321 | ||
322 | BT_LOGV("Serializing structure field's field: pos-offset=%" PRId64 ", " | |
323 | "field-addr=%p, index=%" PRId64, | |
324 | pos->offset, member, i); | |
325 | ||
326 | if (!member) { | |
5fe68922 | 327 | ret = bt_field_type_common_structure_borrow_field_by_index( |
8deee039 PP |
328 | field->type, &field_name, NULL, i); |
329 | BT_ASSERT(ret == 0); | |
330 | BT_LOGW("Cannot serialize structure field's field: field is not set: " | |
331 | "struct-field-addr=%p, " | |
332 | "field-name=\"%s\", index=%" PRId64, | |
333 | field, field_name, i); | |
334 | ret = -1; | |
335 | goto end; | |
336 | } | |
337 | ||
338 | ret = bt_ctf_field_serialize_recursive((void *) member, pos, | |
339 | native_byte_order); | |
340 | if (ret) { | |
5fe68922 | 341 | ret = bt_field_type_common_structure_borrow_field_by_index( |
8deee039 PP |
342 | field->type, &field_name, NULL, i); |
343 | BT_ASSERT(ret == 0); | |
344 | BT_LOGW("Cannot serialize structure field's field: " | |
345 | "struct-field-addr=%p, field-addr=%p, " | |
346 | "field-name=\"%s\", index=%" PRId64, | |
347 | field->type, member, field_name, i); | |
348 | break; | |
349 | } | |
350 | } | |
351 | ||
352 | end: | |
353 | return ret; | |
354 | } | |
355 | ||
356 | static | |
357 | int bt_ctf_field_variant_serialize_recursive(struct bt_field_common *field, | |
358 | struct bt_ctf_stream_pos *pos, | |
359 | enum bt_ctf_byte_order native_byte_order) | |
360 | { | |
361 | struct bt_field_common_variant *variant = BT_FROM_COMMON(field); | |
362 | ||
363 | BT_LOGV("Serializing variant field: addr=%p, pos-offset=%" PRId64 ", " | |
364 | "native-bo=%s", field, pos->offset, | |
365 | bt_common_byte_order_string((int) native_byte_order)); | |
366 | BT_LOGV_STR("Serializing variant field's payload field."); | |
367 | return bt_ctf_field_serialize_recursive( | |
368 | (void *) variant->payload, pos, native_byte_order); | |
369 | } | |
370 | ||
371 | static | |
372 | int bt_ctf_field_array_serialize_recursive(struct bt_field_common *field, | |
373 | struct bt_ctf_stream_pos *pos, | |
374 | enum bt_ctf_byte_order native_byte_order) | |
375 | { | |
376 | int64_t i; | |
377 | int ret = 0; | |
378 | struct bt_field_common_array *array = BT_FROM_COMMON(field); | |
379 | ||
380 | BT_LOGV("Serializing array field: addr=%p, pos-offset=%" PRId64 ", " | |
381 | "native-bo=%s", field, pos->offset, | |
382 | bt_common_byte_order_string((int) native_byte_order)); | |
383 | ||
384 | for (i = 0; i < array->elements->len; i++) { | |
385 | struct bt_field_common *elem_field = | |
386 | g_ptr_array_index(array->elements, i); | |
387 | ||
388 | BT_LOGV("Serializing array field's element field: " | |
389 | "pos-offset=%" PRId64 ", field-addr=%p, index=%" PRId64, | |
390 | pos->offset, elem_field, i); | |
391 | ret = bt_ctf_field_serialize_recursive( | |
392 | (void *) elem_field, pos, native_byte_order); | |
393 | if (ret) { | |
394 | BT_LOGW("Cannot serialize array field's element field: " | |
395 | "array-field-addr=%p, field-addr=%p, " | |
396 | "index=%" PRId64, field, elem_field, i); | |
397 | goto end; | |
398 | } | |
399 | } | |
400 | ||
401 | end: | |
402 | return ret; | |
403 | } | |
404 | ||
405 | static | |
406 | int bt_ctf_field_sequence_serialize_recursive(struct bt_field_common *field, | |
407 | struct bt_ctf_stream_pos *pos, | |
408 | enum bt_ctf_byte_order native_byte_order) | |
409 | { | |
410 | int64_t i; | |
411 | int ret = 0; | |
412 | struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field); | |
413 | ||
414 | BT_LOGV("Serializing sequence field: addr=%p, pos-offset=%" PRId64 ", " | |
415 | "native-bo=%s", field, pos->offset, | |
416 | bt_common_byte_order_string((int) native_byte_order)); | |
417 | ||
418 | for (i = 0; i < sequence->elements->len; i++) { | |
419 | struct bt_field_common *elem_field = | |
420 | g_ptr_array_index(sequence->elements, i); | |
421 | ||
422 | BT_LOGV("Serializing sequence field's element field: " | |
423 | "pos-offset=%" PRId64 ", field-addr=%p, index=%" PRId64, | |
424 | pos->offset, elem_field, i); | |
425 | ret = bt_ctf_field_serialize_recursive( | |
426 | (void *) elem_field, pos, native_byte_order); | |
427 | if (ret) { | |
428 | BT_LOGW("Cannot serialize sequence field's element field: " | |
429 | "sequence-field-addr=%p, field-addr=%p, " | |
430 | "index=%" PRId64, field, elem_field, i); | |
431 | goto end; | |
432 | } | |
433 | } | |
434 | ||
435 | end: | |
436 | return ret; | |
437 | } | |
438 | ||
439 | static | |
440 | int bt_ctf_field_string_serialize(struct bt_field_common *field, | |
441 | struct bt_ctf_stream_pos *pos, | |
442 | enum bt_ctf_byte_order native_byte_order) | |
443 | { | |
444 | int64_t i; | |
445 | int ret = 0; | |
446 | struct bt_field_common_string *string = BT_FROM_COMMON(field); | |
447 | struct bt_ctf_field_type *character_type = | |
448 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); | |
449 | struct bt_ctf_field *character; | |
450 | ||
451 | BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field, "String field"); | |
452 | BT_LOGV("Serializing string field: addr=%p, pos-offset=%" PRId64 ", " | |
453 | "native-bo=%s", field, pos->offset, | |
454 | bt_common_byte_order_string((int) native_byte_order)); | |
455 | ||
456 | BT_LOGV_STR("Creating character field from string field's character field type."); | |
457 | character = bt_ctf_field_create(character_type); | |
458 | ||
459 | for (i = 0; i < string->payload->len + 1; i++) { | |
460 | const uint64_t chr = (uint64_t) string->payload->str[i]; | |
461 | ||
462 | ret = bt_ctf_field_integer_unsigned_set_value(character, chr); | |
463 | BT_ASSERT(ret == 0); | |
464 | BT_LOGV("Serializing string field's character field: " | |
465 | "pos-offset=%" PRId64 ", field-addr=%p, " | |
466 | "index=%" PRId64 ", char-int=%" PRIu64, | |
467 | pos->offset, character, i, chr); | |
468 | ret = bt_ctf_field_integer_serialize( | |
469 | (void *) character, pos, native_byte_order); | |
470 | if (ret) { | |
471 | BT_LOGW_STR("Cannot serialize character field."); | |
472 | goto end; | |
473 | } | |
474 | } | |
475 | ||
476 | end: | |
477 | bt_put(character); | |
478 | bt_put(character_type); | |
479 | return ret; | |
480 | } | |
481 | ||
482 | struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type) | |
483 | { | |
484 | struct bt_ctf_field *field = NULL; | |
485 | enum bt_ctf_field_type_id type_id; | |
486 | ||
487 | BT_ASSERT_PRE_NON_NULL(type, "Field type"); | |
488 | BT_ASSERT(field_type_common_has_known_id((void *) type)); | |
489 | BT_ASSERT_PRE(bt_field_type_common_validate((void *) type) == 0, | |
490 | "Field type is invalid: %!+wF", type); | |
491 | type_id = bt_ctf_field_type_get_type_id(type); | |
492 | field = field_create_funcs[type_id](type); | |
493 | if (!field) { | |
494 | goto end; | |
495 | } | |
496 | ||
497 | bt_field_type_common_freeze((void *) type); | |
498 | ||
499 | end: | |
500 | return field; | |
501 | } | |
502 | ||
503 | struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field) | |
504 | { | |
5fe68922 | 505 | return bt_get(bt_field_common_borrow_type((void *) field)); |
8deee039 PP |
506 | } |
507 | ||
508 | enum bt_ctf_field_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field) | |
509 | { | |
510 | struct bt_field_common *field_common = (void *) field; | |
511 | ||
512 | BT_ASSERT_PRE_NON_NULL(field, "Field"); | |
513 | return (int) field_common->type->id; | |
514 | } | |
515 | ||
516 | struct bt_ctf_field *bt_ctf_field_sequence_get_length( | |
517 | struct bt_ctf_field *field) | |
518 | { | |
5fe68922 | 519 | return bt_get(bt_field_common_sequence_borrow_length((void *) field)); |
8deee039 PP |
520 | } |
521 | ||
522 | int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field, | |
523 | struct bt_ctf_field *length_field) | |
524 | { | |
525 | return bt_field_common_sequence_set_length((void *) field, | |
526 | (void *) length_field); | |
527 | } | |
528 | ||
529 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index( | |
530 | struct bt_ctf_field *field, uint64_t index) | |
531 | { | |
5fe68922 PP |
532 | return bt_get(bt_field_common_structure_borrow_field_by_index( |
533 | (void *) field, index)); | |
8deee039 PP |
534 | } |
535 | ||
536 | struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name( | |
537 | struct bt_ctf_field *field, const char *name) | |
538 | { | |
5fe68922 PP |
539 | return bt_get(bt_field_common_structure_borrow_field_by_name( |
540 | (void *) field, name)); | |
8deee039 PP |
541 | } |
542 | ||
543 | int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field, | |
544 | const char *name, struct bt_ctf_field *value) | |
545 | { | |
546 | return bt_field_common_structure_set_field_by_name((void *) field, | |
547 | name, (void *) value); | |
548 | } | |
549 | ||
550 | struct bt_ctf_field *bt_ctf_field_array_get_field( | |
551 | struct bt_ctf_field *field, uint64_t index) | |
552 | { | |
5fe68922 PP |
553 | return bt_get(bt_field_common_array_borrow_field((void *) field, index, |
554 | (bt_field_common_create_func) bt_ctf_field_create)); | |
8deee039 PP |
555 | } |
556 | ||
557 | struct bt_ctf_field *bt_ctf_field_sequence_get_field( | |
558 | struct bt_ctf_field *field, uint64_t index) | |
559 | { | |
5fe68922 PP |
560 | return bt_get(bt_field_common_sequence_borrow_field((void *) field, |
561 | index, (bt_field_common_create_func) bt_ctf_field_create)); | |
8deee039 PP |
562 | } |
563 | ||
564 | struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field, | |
565 | struct bt_ctf_field *tag_field) | |
566 | { | |
5fe68922 | 567 | return bt_get(bt_field_common_variant_borrow_field((void *) field, |
8deee039 | 568 | (void *) tag_field, |
5fe68922 | 569 | (bt_field_common_create_func) bt_ctf_field_create)); |
8deee039 PP |
570 | } |
571 | ||
572 | struct bt_ctf_field *bt_ctf_field_variant_get_current_field( | |
573 | struct bt_ctf_field *variant_field) | |
574 | { | |
5fe68922 PP |
575 | return bt_get(bt_field_common_variant_borrow_current_field( |
576 | (void *) variant_field)); | |
8deee039 PP |
577 | } |
578 | ||
579 | struct bt_ctf_field *bt_ctf_field_variant_get_tag( | |
580 | struct bt_ctf_field *variant_field) | |
581 | { | |
5fe68922 PP |
582 | return bt_get(bt_field_common_variant_borrow_tag( |
583 | (void *) variant_field)); | |
8deee039 PP |
584 | } |
585 | ||
586 | struct bt_ctf_field *bt_ctf_field_enumeration_get_container(struct bt_ctf_field *field) | |
587 | { | |
5fe68922 PP |
588 | return bt_get(bt_field_common_enumeration_borrow_container( |
589 | (void *) field, | |
590 | (bt_field_common_create_func) bt_ctf_field_create)); | |
8deee039 PP |
591 | } |
592 | ||
593 | int bt_ctf_field_integer_signed_get_value(struct bt_ctf_field *field, int64_t *value) | |
594 | { | |
595 | return bt_field_common_integer_signed_get_value((void *) field, value); | |
596 | } | |
597 | ||
598 | int bt_ctf_field_integer_signed_set_value(struct bt_ctf_field *field, | |
599 | int64_t value) | |
600 | { | |
601 | return bt_field_common_integer_signed_set_value((void *) field, value); | |
602 | } | |
603 | ||
604 | int bt_ctf_field_integer_unsigned_get_value(struct bt_ctf_field *field, | |
605 | uint64_t *value) | |
606 | { | |
607 | return bt_field_common_integer_unsigned_get_value((void *) field, | |
608 | value); | |
609 | } | |
610 | ||
611 | int bt_ctf_field_integer_unsigned_set_value(struct bt_ctf_field *field, uint64_t value) | |
612 | { | |
613 | return bt_field_common_integer_unsigned_set_value((void *) field, value); | |
614 | } | |
615 | ||
616 | int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field, | |
617 | double *value) | |
618 | { | |
619 | return bt_field_common_floating_point_get_value((void *) field, value); | |
620 | } | |
621 | ||
622 | int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field, | |
623 | double value) | |
624 | { | |
625 | return bt_field_common_floating_point_set_value((void *) field, value); | |
626 | } | |
627 | ||
628 | const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field) | |
629 | { | |
630 | return bt_field_common_string_get_value((void *) field); | |
631 | } | |
632 | ||
633 | int bt_ctf_field_string_set_value(struct bt_ctf_field *field, const char *value) | |
634 | { | |
635 | return bt_field_common_string_set_value((void *) field, value); | |
636 | } | |
637 | ||
638 | int bt_ctf_field_string_append(struct bt_ctf_field *field, const char *value) | |
639 | { | |
640 | return bt_field_common_string_append((void *) field, value); | |
641 | } | |
642 | ||
643 | int bt_ctf_field_string_append_len(struct bt_ctf_field *field, | |
644 | const char *value, unsigned int length) | |
645 | { | |
646 | return bt_field_common_string_append_len((void *) field, value, length); | |
647 | } | |
648 | ||
649 | struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field) | |
650 | { | |
651 | return (void *) bt_field_common_copy((void *) field); | |
652 | } | |
653 | ||
654 | static | |
655 | struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type) | |
656 | { | |
657 | struct bt_field_common_integer *integer = | |
658 | g_new0(struct bt_field_common_integer, 1); | |
659 | ||
660 | BT_LOGD("Creating CTF writer integer field object: ft-addr=%p", type); | |
661 | ||
662 | if (integer) { | |
663 | bt_field_common_initialize(BT_TO_COMMON(integer), (void *) type, | |
664 | bt_field_common_integer_destroy, | |
665 | &bt_ctf_field_integer_methods); | |
666 | integer->common.spec.writer.serialize_func = | |
667 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_integer_serialize; | |
668 | BT_LOGD("Created CTF writer integer field object: addr=%p, ft-addr=%p", | |
669 | integer, type); | |
670 | } else { | |
671 | BT_LOGE_STR("Failed to allocate one integer field."); | |
672 | } | |
673 | ||
674 | return (void *) integer; | |
675 | } | |
676 | ||
677 | static | |
678 | struct bt_ctf_field *bt_ctf_field_enumeration_create( | |
679 | struct bt_ctf_field_type *type) | |
680 | { | |
681 | struct bt_field_common_enumeration *enumeration = g_new0( | |
682 | struct bt_field_common_enumeration, 1); | |
683 | ||
684 | BT_LOGD("Creating CTF writer enumeration field object: ft-addr=%p", type); | |
685 | ||
686 | if (enumeration) { | |
687 | bt_field_common_initialize(BT_TO_COMMON(enumeration), | |
688 | (void *) type, | |
689 | bt_field_common_enumeration_destroy_recursive, | |
690 | &bt_ctf_field_enumeration_methods); | |
691 | enumeration->common.spec.writer.serialize_func = | |
692 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_enumeration_serialize_recursive; | |
693 | BT_LOGD("Created CTF writer enumeration field object: addr=%p, ft-addr=%p", | |
694 | enumeration, type); | |
695 | } else { | |
696 | BT_LOGE_STR("Failed to allocate one enumeration field."); | |
697 | } | |
698 | ||
699 | return (void *) enumeration; | |
700 | } | |
701 | ||
702 | static | |
703 | struct bt_ctf_field *bt_ctf_field_floating_point_create( | |
704 | struct bt_ctf_field_type *type) | |
705 | { | |
706 | struct bt_field_common_floating_point *floating_point; | |
707 | ||
708 | BT_LOGD("Creating CTF writer floating point number field object: ft-addr=%p", type); | |
709 | floating_point = g_new0(struct bt_field_common_floating_point, 1); | |
710 | ||
711 | if (floating_point) { | |
712 | bt_field_common_initialize(BT_TO_COMMON(floating_point), | |
713 | (void *) type, | |
714 | bt_field_common_floating_point_destroy, | |
715 | &bt_ctf_field_floating_point_methods); | |
716 | floating_point->common.spec.writer.serialize_func = | |
717 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_floating_point_serialize; | |
718 | BT_LOGD("Created CTF writer floating point number field object: addr=%p, ft-addr=%p", | |
719 | floating_point, type); | |
720 | } else { | |
721 | BT_LOGE_STR("Failed to allocate one floating point number field."); | |
722 | } | |
723 | ||
724 | return (void *) floating_point; | |
725 | } | |
726 | ||
727 | static | |
728 | struct bt_ctf_field *bt_ctf_field_structure_create( | |
729 | struct bt_ctf_field_type *type) | |
730 | { | |
731 | struct bt_field_common_structure *structure = g_new0( | |
732 | struct bt_field_common_structure, 1); | |
733 | int iret; | |
734 | ||
735 | BT_LOGD("Creating CTF writer structure field object: ft-addr=%p", type); | |
736 | ||
737 | if (!structure) { | |
738 | BT_LOGE_STR("Failed to allocate one structure field."); | |
739 | goto end; | |
740 | } | |
741 | ||
742 | iret = bt_field_common_structure_initialize(BT_TO_COMMON(structure), | |
743 | (void *) type, bt_field_common_structure_destroy_recursive, | |
744 | &bt_ctf_field_structure_methods, | |
745 | (bt_field_common_create_func) bt_ctf_field_create); | |
746 | structure->common.spec.writer.serialize_func = | |
747 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_structure_serialize_recursive; | |
748 | if (iret) { | |
749 | BT_PUT(structure); | |
750 | goto end; | |
751 | } | |
752 | ||
753 | BT_LOGD("Created CTF writer structure field object: addr=%p, ft-addr=%p", | |
754 | structure, type); | |
755 | ||
756 | end: | |
757 | return (void *) structure; | |
758 | } | |
759 | ||
760 | static | |
761 | struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type) | |
762 | { | |
763 | struct bt_field_common_variant *variant = g_new0( | |
764 | struct bt_field_common_variant, 1); | |
765 | ||
766 | BT_LOGD("Creating CTF writer variant field object: ft-addr=%p", type); | |
767 | ||
768 | if (variant) { | |
769 | bt_field_common_initialize(BT_TO_COMMON(variant), | |
770 | (void *) type, | |
771 | bt_field_common_variant_destroy_recursive, | |
772 | &bt_ctf_field_variant_methods); | |
773 | variant->common.spec.writer.serialize_func = | |
774 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_variant_serialize_recursive; | |
775 | BT_LOGD("Created CTF writer variant field object: addr=%p, ft-addr=%p", | |
776 | variant, type); | |
777 | } else { | |
778 | BT_LOGE_STR("Failed to allocate one variant field."); | |
779 | } | |
780 | ||
781 | return (void *) variant; | |
782 | } | |
783 | ||
784 | static | |
785 | struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type) | |
786 | { | |
787 | struct bt_field_common_array *array = | |
788 | g_new0(struct bt_field_common_array, 1); | |
789 | int ret; | |
790 | ||
791 | BT_LOGD("Creating CTF writer array field object: ft-addr=%p", type); | |
792 | BT_ASSERT(type); | |
793 | ||
794 | if (!array) { | |
795 | BT_LOGE_STR("Failed to allocate one array field."); | |
796 | goto end; | |
797 | } | |
798 | ||
799 | ret = bt_field_common_array_initialize(BT_TO_COMMON(array), | |
800 | (void *) type, | |
801 | bt_field_common_array_destroy_recursive, | |
802 | &bt_ctf_field_array_methods); | |
803 | array->common.spec.writer.serialize_func = | |
804 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_array_serialize_recursive; | |
805 | if (ret) { | |
806 | BT_PUT(array); | |
807 | goto end; | |
808 | } | |
809 | ||
810 | BT_LOGD("Created CTF writer array field object: addr=%p, ft-addr=%p", | |
811 | array, type); | |
812 | ||
813 | end: | |
814 | return (void *) array; | |
815 | } | |
816 | ||
817 | static | |
818 | struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *type) | |
819 | { | |
820 | struct bt_field_common_sequence *sequence = g_new0( | |
821 | struct bt_field_common_sequence, 1); | |
822 | ||
823 | BT_LOGD("Creating CTF writer sequence field object: ft-addr=%p", type); | |
824 | ||
825 | if (sequence) { | |
826 | bt_field_common_initialize(BT_TO_COMMON(sequence), | |
827 | (void *) type, | |
828 | bt_field_common_sequence_destroy_recursive, | |
829 | &bt_ctf_field_sequence_methods); | |
830 | sequence->common.spec.writer.serialize_func = | |
831 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_sequence_serialize_recursive; | |
832 | BT_LOGD("Created CTF writer sequence field object: addr=%p, ft-addr=%p", | |
833 | sequence, type); | |
834 | } else { | |
835 | BT_LOGE_STR("Failed to allocate one sequence field."); | |
836 | } | |
837 | ||
838 | return (void *) sequence; | |
839 | } | |
840 | ||
841 | static | |
842 | struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type) | |
843 | { | |
844 | struct bt_field_common_string *string = g_new0( | |
845 | struct bt_field_common_string, 1); | |
846 | ||
847 | BT_LOGD("Creating CTF writer string field object: ft-addr=%p", type); | |
848 | ||
849 | if (string) { | |
850 | bt_field_common_initialize(BT_TO_COMMON(string), | |
851 | (void *) type, | |
852 | bt_field_common_string_destroy, | |
853 | &bt_ctf_field_string_methods); | |
854 | string->common.spec.writer.serialize_func = | |
855 | (bt_ctf_field_serialize_recursive_func) bt_ctf_field_string_serialize; | |
856 | BT_LOGD("Created CTF writer string field object: addr=%p, ft-addr=%p", | |
857 | string, type); | |
858 | } else { | |
859 | BT_LOGE_STR("Failed to allocate one string field."); | |
860 | } | |
861 | ||
862 | return (void *) string; | |
863 | } |