Commit | Line | Data |
---|---|---|
bc37ae52 JG |
1 | /* |
2 | * trace.c | |
3 | * | |
4 | * Babeltrace CTF IR - Trace | |
5 | * | |
6 | * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@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/trace-internal.h> | |
ac0c6bdd | 30 | #include <babeltrace/ctf-ir/clock-class-internal.h> |
bc37ae52 JG |
31 | #include <babeltrace/ctf-ir/stream-internal.h> |
32 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
09840de5 | 33 | #include <babeltrace/ctf-ir/event-internal.h> |
272df73e PP |
34 | #include <babeltrace/ctf-ir/event-class.h> |
35 | #include <babeltrace/ctf-ir/event-class-internal.h> | |
bc37ae52 | 36 | #include <babeltrace/ctf-writer/functor-internal.h> |
ac0c6bdd | 37 | #include <babeltrace/ctf-writer/clock-internal.h> |
2e33ac5a | 38 | #include <babeltrace/ctf-ir/field-types-internal.h> |
44e0a4f5 | 39 | #include <babeltrace/ctf-ir/attributes-internal.h> |
09840de5 | 40 | #include <babeltrace/ctf-ir/validation-internal.h> |
8bf65fbd | 41 | #include <babeltrace/ctf-ir/visitor-internal.h> |
654c1444 | 42 | #include <babeltrace/ctf-ir/utils.h> |
3d9990ac | 43 | #include <babeltrace/compiler-internal.h> |
dac5c838 | 44 | #include <babeltrace/values.h> |
83509119 | 45 | #include <babeltrace/ref.h> |
3d9990ac | 46 | #include <babeltrace/endian-internal.h> |
dc3fffef | 47 | #include <inttypes.h> |
544d0515 | 48 | #include <stdint.h> |
4a32fda0 | 49 | #include <string.h> |
bc37ae52 JG |
50 | |
51 | #define DEFAULT_IDENTIFIER_SIZE 128 | |
52 | #define DEFAULT_METADATA_STRING_SIZE 4096 | |
53 | ||
50ad4244 JG |
54 | struct listener_wrapper { |
55 | bt_ctf_listener_cb listener; | |
2204c381 JG |
56 | void *data; |
57 | }; | |
58 | ||
bc37ae52 | 59 | static |
83509119 | 60 | void bt_ctf_trace_destroy(struct bt_object *obj); |
bc37ae52 | 61 | static |
09840de5 | 62 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace); |
bc37ae52 | 63 | |
bc37ae52 JG |
64 | static |
65 | const unsigned int field_type_aliases_alignments[] = { | |
66 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, | |
67 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, | |
68 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, | |
69 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, | |
70 | }; | |
71 | ||
72 | static | |
73 | const unsigned int field_type_aliases_sizes[] = { | |
74 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, | |
75 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, | |
76 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, | |
77 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, | |
78 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, | |
79 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, | |
80 | }; | |
81 | ||
bc37ae52 JG |
82 | struct bt_ctf_trace *bt_ctf_trace_create(void) |
83 | { | |
84 | struct bt_ctf_trace *trace = NULL; | |
488e09a7 | 85 | struct bt_ctf_field_type *packet_header_type = NULL; |
bc37ae52 JG |
86 | |
87 | trace = g_new0(struct bt_ctf_trace, 1); | |
88 | if (!trace) { | |
89 | goto error; | |
90 | } | |
91 | ||
391c8f0d | 92 | trace->native_byte_order = BT_CTF_BYTE_ORDER_NATIVE; |
83509119 | 93 | bt_object_init(trace, bt_ctf_trace_destroy); |
bc37ae52 | 94 | trace->clocks = g_ptr_array_new_with_free_func( |
83509119 | 95 | (GDestroyNotify) bt_put); |
bc37ae52 | 96 | trace->streams = g_ptr_array_new_with_free_func( |
e6a8e8e4 | 97 | (GDestroyNotify) bt_object_release); |
bc37ae52 | 98 | trace->stream_classes = g_ptr_array_new_with_free_func( |
e6a8e8e4 | 99 | (GDestroyNotify) bt_object_release); |
7f800dc7 | 100 | if (!trace->clocks || !trace->stream_classes || !trace->streams) { |
83509119 | 101 | goto error; |
bc37ae52 JG |
102 | } |
103 | ||
488e09a7 PP |
104 | packet_header_type = bt_ctf_field_type_structure_create(); |
105 | if (!packet_header_type) { | |
83509119 | 106 | goto error; |
bc37ae52 JG |
107 | } |
108 | ||
488e09a7 PP |
109 | BT_MOVE(trace->packet_header_type, packet_header_type); |
110 | ||
7f800dc7 PP |
111 | /* Create the environment array object */ |
112 | trace->environment = bt_ctf_attributes_create(); | |
113 | if (!trace->environment) { | |
83509119 | 114 | goto error; |
7f800dc7 PP |
115 | } |
116 | ||
9b888ff3 JG |
117 | trace->listeners = g_ptr_array_new_with_free_func( |
118 | (GDestroyNotify) g_free); | |
119 | if (!trace->listeners) { | |
120 | goto error; | |
121 | } | |
122 | ||
bc37ae52 JG |
123 | return trace; |
124 | ||
bc37ae52 | 125 | error: |
83509119 | 126 | BT_PUT(trace); |
488e09a7 | 127 | bt_put(packet_header_type); |
bc37ae52 JG |
128 | return trace; |
129 | } | |
130 | ||
e96045d4 JG |
131 | const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace) |
132 | { | |
133 | const char *name = NULL; | |
134 | ||
135 | if (!trace || !trace->name) { | |
136 | goto end; | |
137 | } | |
138 | ||
139 | name = trace->name->str; | |
140 | end: | |
141 | return name; | |
142 | } | |
143 | ||
144 | int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name) | |
145 | { | |
146 | int ret = 0; | |
147 | ||
148 | if (!trace || !name || trace->frozen) { | |
149 | ret = -1; | |
150 | goto end; | |
151 | } | |
152 | ||
153 | trace->name = trace->name ? g_string_assign(trace->name, name) : | |
154 | g_string_new(name); | |
155 | if (!trace->name) { | |
156 | ret = -1; | |
157 | goto end; | |
158 | } | |
159 | end: | |
160 | return ret; | |
161 | } | |
162 | ||
4a32fda0 PP |
163 | const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace) |
164 | { | |
165 | return trace && trace->uuid_set ? trace->uuid : NULL; | |
166 | } | |
167 | ||
168 | int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace, const unsigned char *uuid) | |
169 | { | |
170 | int ret = 0; | |
171 | ||
172 | if (!trace || !uuid || trace->frozen) { | |
173 | ret = -1; | |
174 | goto end; | |
175 | } | |
176 | ||
177 | memcpy(trace->uuid, uuid, sizeof(uuid_t)); | |
178 | trace->uuid_set = true; | |
179 | ||
180 | end: | |
181 | return ret; | |
182 | } | |
183 | ||
83509119 | 184 | void bt_ctf_trace_destroy(struct bt_object *obj) |
bc37ae52 JG |
185 | { |
186 | struct bt_ctf_trace *trace; | |
bc37ae52 | 187 | |
83509119 | 188 | trace = container_of(obj, struct bt_ctf_trace, base); |
bc37ae52 | 189 | if (trace->environment) { |
7f800dc7 | 190 | bt_ctf_attributes_destroy(trace->environment); |
bc37ae52 JG |
191 | } |
192 | ||
e96045d4 JG |
193 | if (trace->name) { |
194 | g_string_free(trace->name, TRUE); | |
195 | } | |
196 | ||
bc37ae52 JG |
197 | if (trace->clocks) { |
198 | g_ptr_array_free(trace->clocks, TRUE); | |
199 | } | |
200 | ||
201 | if (trace->streams) { | |
202 | g_ptr_array_free(trace->streams, TRUE); | |
203 | } | |
204 | ||
205 | if (trace->stream_classes) { | |
206 | g_ptr_array_free(trace->stream_classes, TRUE); | |
207 | } | |
208 | ||
9b888ff3 JG |
209 | if (trace->listeners) { |
210 | g_ptr_array_free(trace->listeners, TRUE); | |
211 | } | |
212 | ||
83509119 | 213 | bt_put(trace->packet_header_type); |
bc37ae52 JG |
214 | g_free(trace); |
215 | } | |
216 | ||
7f800dc7 | 217 | int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, |
dac5c838 | 218 | const char *name, struct bt_value *value) |
bc37ae52 | 219 | { |
bc37ae52 JG |
220 | int ret = 0; |
221 | ||
a0d12916 | 222 | if (!trace || !name || !value || |
7f800dc7 | 223 | bt_ctf_validate_identifier(name) || |
dac5c838 | 224 | !(bt_value_is_integer(value) || bt_value_is_string(value))) { |
bc37ae52 | 225 | ret = -1; |
7f800dc7 | 226 | goto end; |
bc37ae52 JG |
227 | } |
228 | ||
229 | if (strchr(name, ' ')) { | |
230 | ret = -1; | |
7f800dc7 | 231 | goto end; |
bc37ae52 JG |
232 | } |
233 | ||
a0d12916 JG |
234 | if (trace->frozen) { |
235 | /* | |
236 | * New environment fields may be added to a frozen trace, | |
237 | * but existing fields may not be changed. | |
238 | * | |
239 | * The object passed is frozen like all other attributes. | |
240 | */ | |
dac5c838 | 241 | struct bt_value *attribute = |
a0d12916 JG |
242 | bt_ctf_attributes_get_field_value_by_name( |
243 | trace->environment, name); | |
244 | ||
245 | if (attribute) { | |
83509119 | 246 | BT_PUT(attribute); |
a0d12916 JG |
247 | ret = -1; |
248 | goto end; | |
249 | } | |
250 | ||
dac5c838 | 251 | bt_value_freeze(value); |
a0d12916 JG |
252 | } |
253 | ||
7f800dc7 PP |
254 | ret = bt_ctf_attributes_set_field_value(trace->environment, name, |
255 | value); | |
bc37ae52 | 256 | |
7f800dc7 PP |
257 | end: |
258 | return ret; | |
259 | } | |
bc37ae52 | 260 | |
7f800dc7 PP |
261 | int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, |
262 | const char *name, const char *value) | |
263 | { | |
264 | int ret = 0; | |
dac5c838 | 265 | struct bt_value *env_value_string_obj = NULL; |
7f800dc7 PP |
266 | |
267 | if (!trace || !name || !value) { | |
bc37ae52 | 268 | ret = -1; |
7f800dc7 | 269 | goto end; |
bc37ae52 JG |
270 | } |
271 | ||
a0d12916 JG |
272 | if (trace->frozen) { |
273 | /* | |
274 | * New environment fields may be added to a frozen trace, | |
275 | * but existing fields may not be changed. | |
276 | */ | |
dac5c838 | 277 | struct bt_value *attribute = |
a0d12916 JG |
278 | bt_ctf_attributes_get_field_value_by_name( |
279 | trace->environment, name); | |
280 | ||
281 | if (attribute) { | |
83509119 | 282 | BT_PUT(attribute); |
a0d12916 JG |
283 | ret = -1; |
284 | goto end; | |
285 | } | |
286 | } | |
287 | ||
dac5c838 | 288 | env_value_string_obj = bt_value_string_create_init(value); |
bc37ae52 | 289 | |
7f800dc7 PP |
290 | if (!env_value_string_obj) { |
291 | ret = -1; | |
292 | goto end; | |
bc37ae52 JG |
293 | } |
294 | ||
a0d12916 | 295 | if (trace->frozen) { |
dac5c838 | 296 | bt_value_freeze(env_value_string_obj); |
a0d12916 | 297 | } |
7f800dc7 PP |
298 | ret = bt_ctf_trace_set_environment_field(trace, name, |
299 | env_value_string_obj); | |
300 | ||
301 | end: | |
83509119 | 302 | BT_PUT(env_value_string_obj); |
3487c9f3 JG |
303 | return ret; |
304 | } | |
305 | ||
7f800dc7 PP |
306 | int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace, |
307 | const char *name, int64_t value) | |
3487c9f3 | 308 | { |
3487c9f3 | 309 | int ret = 0; |
dac5c838 | 310 | struct bt_value *env_value_integer_obj = NULL; |
3487c9f3 JG |
311 | |
312 | if (!trace || !name) { | |
313 | ret = -1; | |
7f800dc7 | 314 | goto end; |
3487c9f3 JG |
315 | } |
316 | ||
a0d12916 JG |
317 | if (trace->frozen) { |
318 | /* | |
319 | * New environment fields may be added to a frozen trace, | |
320 | * but existing fields may not be changed. | |
321 | */ | |
dac5c838 | 322 | struct bt_value *attribute = |
a0d12916 JG |
323 | bt_ctf_attributes_get_field_value_by_name( |
324 | trace->environment, name); | |
325 | ||
326 | if (attribute) { | |
83509119 | 327 | BT_PUT(attribute); |
a0d12916 JG |
328 | ret = -1; |
329 | goto end; | |
330 | } | |
331 | } | |
332 | ||
dac5c838 | 333 | env_value_integer_obj = bt_value_integer_create_init(value); |
7f800dc7 | 334 | if (!env_value_integer_obj) { |
3487c9f3 | 335 | ret = -1; |
7f800dc7 | 336 | goto end; |
3487c9f3 JG |
337 | } |
338 | ||
7f800dc7 PP |
339 | ret = bt_ctf_trace_set_environment_field(trace, name, |
340 | env_value_integer_obj); | |
a0d12916 | 341 | if (trace->frozen) { |
dac5c838 | 342 | bt_value_freeze(env_value_integer_obj); |
a0d12916 | 343 | } |
7f800dc7 | 344 | end: |
83509119 | 345 | BT_PUT(env_value_integer_obj); |
bc37ae52 JG |
346 | return ret; |
347 | } | |
348 | ||
544d0515 | 349 | int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
e6fa2160 | 350 | { |
544d0515 | 351 | int64_t ret = 0; |
e6fa2160 JG |
352 | |
353 | if (!trace) { | |
9ac68eb1 | 354 | ret = (int64_t) -1; |
e6fa2160 JG |
355 | goto end; |
356 | } | |
357 | ||
7f800dc7 | 358 | ret = bt_ctf_attributes_get_count(trace->environment); |
e6fa2160 | 359 | |
e6fa2160 | 360 | end: |
7f800dc7 | 361 | return ret; |
e6fa2160 JG |
362 | } |
363 | ||
364 | const char * | |
9ac68eb1 PP |
365 | bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace, |
366 | uint64_t index) | |
e6fa2160 | 367 | { |
e6fa2160 JG |
368 | const char *ret = NULL; |
369 | ||
7f800dc7 | 370 | if (!trace) { |
e6fa2160 JG |
371 | goto end; |
372 | } | |
373 | ||
7f800dc7 PP |
374 | ret = bt_ctf_attributes_get_field_name(trace->environment, index); |
375 | ||
e6fa2160 JG |
376 | end: |
377 | return ret; | |
378 | } | |
379 | ||
9ac68eb1 PP |
380 | struct bt_value *bt_ctf_trace_get_environment_field_value_by_index( |
381 | struct bt_ctf_trace *trace, uint64_t index) | |
e6fa2160 | 382 | { |
dac5c838 | 383 | struct bt_value *ret = NULL; |
e6fa2160 | 384 | |
7f800dc7 | 385 | if (!trace) { |
e6fa2160 JG |
386 | goto end; |
387 | } | |
388 | ||
7f800dc7 PP |
389 | ret = bt_ctf_attributes_get_field_value(trace->environment, index); |
390 | ||
e6fa2160 JG |
391 | end: |
392 | return ret; | |
393 | } | |
394 | ||
dac5c838 | 395 | struct bt_value *bt_ctf_trace_get_environment_field_value_by_name( |
7f800dc7 | 396 | struct bt_ctf_trace *trace, const char *name) |
e6fa2160 | 397 | { |
dac5c838 | 398 | struct bt_value *ret = NULL; |
e6fa2160 | 399 | |
7f800dc7 | 400 | if (!trace || !name) { |
e6fa2160 JG |
401 | goto end; |
402 | } | |
403 | ||
7f800dc7 PP |
404 | ret = bt_ctf_attributes_get_field_value_by_name(trace->environment, |
405 | name); | |
406 | ||
e6fa2160 JG |
407 | end: |
408 | return ret; | |
409 | } | |
410 | ||
ac0c6bdd PP |
411 | int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace, |
412 | struct bt_ctf_clock_class *clock_class) | |
bc37ae52 JG |
413 | { |
414 | int ret = 0; | |
bc37ae52 | 415 | |
5acf2ae6 PP |
416 | if (!trace || trace->is_static || |
417 | !bt_ctf_clock_class_is_valid(clock_class)) { | |
bc37ae52 JG |
418 | ret = -1; |
419 | goto end; | |
420 | } | |
421 | ||
ac0c6bdd | 422 | /* Check for duplicate clock classes */ |
c9d90a34 | 423 | if (bt_ctf_trace_has_clock_class(trace, clock_class)) { |
bc37ae52 JG |
424 | ret = -1; |
425 | goto end; | |
426 | } | |
427 | ||
ac0c6bdd PP |
428 | bt_get(clock_class); |
429 | g_ptr_array_add(trace->clocks, clock_class); | |
cfeb617e | 430 | |
6474e016 | 431 | if (trace->frozen) { |
ac0c6bdd | 432 | bt_ctf_clock_class_freeze(clock_class); |
6474e016 | 433 | } |
bc37ae52 JG |
434 | end: |
435 | return ret; | |
436 | } | |
437 | ||
544d0515 | 438 | int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace) |
884cd6c3 | 439 | { |
9ac68eb1 | 440 | int64_t ret = (int64_t) -1; |
884cd6c3 JG |
441 | |
442 | if (!trace) { | |
443 | goto end; | |
444 | } | |
445 | ||
446 | ret = trace->clocks->len; | |
447 | end: | |
448 | return ret; | |
449 | } | |
450 | ||
9ac68eb1 PP |
451 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index( |
452 | struct bt_ctf_trace *trace, uint64_t index) | |
884cd6c3 | 453 | { |
ac0c6bdd | 454 | struct bt_ctf_clock_class *clock_class = NULL; |
884cd6c3 | 455 | |
9ac68eb1 | 456 | if (!trace || index >= trace->clocks->len) { |
884cd6c3 JG |
457 | goto end; |
458 | } | |
459 | ||
ac0c6bdd PP |
460 | clock_class = g_ptr_array_index(trace->clocks, index); |
461 | bt_get(clock_class); | |
884cd6c3 | 462 | end: |
ac0c6bdd | 463 | return clock_class; |
884cd6c3 JG |
464 | } |
465 | ||
ef0c4a15 JG |
466 | int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, |
467 | struct bt_ctf_stream_class *stream_class) | |
468 | { | |
544d0515 PP |
469 | int ret; |
470 | int64_t i; | |
ef0c4a15 | 471 | int64_t stream_id; |
09840de5 PP |
472 | struct bt_ctf_validation_output trace_sc_validation_output = { 0 }; |
473 | struct bt_ctf_validation_output *ec_validation_outputs = NULL; | |
474 | const enum bt_ctf_validation_flag trace_sc_validation_flags = | |
475 | BT_CTF_VALIDATION_FLAG_TRACE | | |
476 | BT_CTF_VALIDATION_FLAG_STREAM; | |
477 | const enum bt_ctf_validation_flag ec_validation_flags = | |
478 | BT_CTF_VALIDATION_FLAG_EVENT; | |
479 | struct bt_ctf_field_type *packet_header_type = NULL; | |
480 | struct bt_ctf_field_type *packet_context_type = NULL; | |
481 | struct bt_ctf_field_type *event_header_type = NULL; | |
482 | struct bt_ctf_field_type *stream_event_ctx_type = NULL; | |
544d0515 | 483 | int64_t event_class_count; |
2789e5d9 | 484 | struct bt_ctf_trace *current_parent_trace = NULL; |
ef0c4a15 | 485 | |
5acf2ae6 | 486 | if (!trace || !stream_class || trace->is_static) { |
ef0c4a15 JG |
487 | ret = -1; |
488 | goto end; | |
489 | } | |
490 | ||
d490fcbe PP |
491 | /* |
492 | * At the end of this function we freeze the trace, so its | |
493 | * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE. | |
494 | */ | |
495 | if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) { | |
496 | ret = -1; | |
497 | goto end; | |
498 | } | |
499 | ||
2789e5d9 JG |
500 | current_parent_trace = bt_ctf_stream_class_get_trace(stream_class); |
501 | if (current_parent_trace) { | |
502 | /* Stream class is already associated to a trace, abort. */ | |
503 | ret = -1; | |
504 | goto end; | |
505 | } | |
506 | ||
09840de5 PP |
507 | event_class_count = |
508 | bt_ctf_stream_class_get_event_class_count(stream_class); | |
509 | assert(event_class_count >= 0); | |
510 | ||
511 | /* Check for duplicate stream classes */ | |
ef0c4a15 JG |
512 | for (i = 0; i < trace->stream_classes->len; i++) { |
513 | if (trace->stream_classes->pdata[i] == stream_class) { | |
09840de5 | 514 | /* Stream class already registered to the trace */ |
ef0c4a15 JG |
515 | ret = -1; |
516 | goto end; | |
517 | } | |
518 | } | |
519 | ||
fe13b5c0 | 520 | if (stream_class->clock) { |
ac0c6bdd PP |
521 | struct bt_ctf_clock_class *stream_clock_class = |
522 | stream_class->clock->clock_class; | |
523 | ||
524 | if (trace->is_created_by_writer) { | |
525 | /* | |
526 | * Make sure this clock was also added to the | |
527 | * trace (potentially through its CTF writer | |
528 | * owner). | |
529 | */ | |
530 | size_t i; | |
531 | ||
532 | for (i = 0; i < trace->clocks->len; i++) { | |
533 | if (trace->clocks->pdata[i] == | |
534 | stream_clock_class) { | |
535 | /* Found! */ | |
536 | break; | |
537 | } | |
538 | } | |
539 | ||
540 | if (i == trace->clocks->len) { | |
541 | /* Not found */ | |
fe13b5c0 PP |
542 | ret = -1; |
543 | goto end; | |
544 | } | |
545 | } else { | |
ac0c6bdd PP |
546 | /* |
547 | * This trace was NOT created by a CTF writer, | |
548 | * thus do not allow the stream class to add to | |
549 | * have a clock at all. Those are two | |
550 | * independent APIs (non-writer and writer | |
551 | * APIs), and isolating them simplifies things. | |
552 | */ | |
553 | ret = -1; | |
554 | goto end; | |
fe13b5c0 PP |
555 | } |
556 | } | |
557 | ||
09840de5 PP |
558 | /* |
559 | * We're about to freeze both the trace and the stream class. | |
560 | * Also, each event class contained in this stream class are | |
561 | * already frozen. | |
562 | * | |
563 | * This trace, this stream class, and all its event classes | |
564 | * should be valid at this point. | |
565 | * | |
566 | * Validate trace and stream class first, then each event | |
567 | * class of this stream class can be validated individually. | |
568 | */ | |
569 | packet_header_type = | |
570 | bt_ctf_trace_get_packet_header_type(trace); | |
571 | packet_context_type = | |
572 | bt_ctf_stream_class_get_packet_context_type(stream_class); | |
573 | event_header_type = | |
574 | bt_ctf_stream_class_get_event_header_type(stream_class); | |
575 | stream_event_ctx_type = | |
576 | bt_ctf_stream_class_get_event_context_type(stream_class); | |
577 | ret = bt_ctf_validate_class_types(trace->environment, | |
578 | packet_header_type, packet_context_type, event_header_type, | |
579 | stream_event_ctx_type, NULL, NULL, trace->valid, | |
580 | stream_class->valid, 1, &trace_sc_validation_output, | |
581 | trace_sc_validation_flags); | |
582 | BT_PUT(packet_header_type); | |
583 | BT_PUT(packet_context_type); | |
584 | BT_PUT(event_header_type); | |
585 | BT_PUT(stream_event_ctx_type); | |
586 | ||
2bd7f864 | 587 | if (ret) { |
09840de5 PP |
588 | /* |
589 | * This means something went wrong during the validation | |
590 | * process, not that the objects are invalid. | |
591 | */ | |
2bd7f864 JG |
592 | goto end; |
593 | } | |
594 | ||
09840de5 PP |
595 | if ((trace_sc_validation_output.valid_flags & |
596 | trace_sc_validation_flags) != | |
597 | trace_sc_validation_flags) { | |
598 | /* Invalid trace/stream class */ | |
599 | ret = -1; | |
600 | goto end; | |
601 | } | |
602 | ||
603 | if (event_class_count > 0) { | |
604 | ec_validation_outputs = g_new0(struct bt_ctf_validation_output, | |
605 | event_class_count); | |
606 | if (!ec_validation_outputs) { | |
607 | ret = -1; | |
608 | goto end; | |
609 | } | |
610 | } | |
611 | ||
612 | /* Validate each event class individually */ | |
6474e016 | 613 | for (i = 0; i < event_class_count; i++) { |
09840de5 | 614 | struct bt_ctf_event_class *event_class = |
9ac68eb1 PP |
615 | bt_ctf_stream_class_get_event_class_by_index( |
616 | stream_class, i); | |
09840de5 PP |
617 | struct bt_ctf_field_type *event_context_type = NULL; |
618 | struct bt_ctf_field_type *event_payload_type = NULL; | |
619 | ||
620 | event_context_type = | |
621 | bt_ctf_event_class_get_context_type(event_class); | |
622 | event_payload_type = | |
623 | bt_ctf_event_class_get_payload_type(event_class); | |
624 | ||
625 | /* | |
626 | * It is important to use the field types returned by | |
627 | * the previous trace and stream class validation here | |
628 | * because copies could have been made. | |
629 | */ | |
630 | ret = bt_ctf_validate_class_types(trace->environment, | |
631 | trace_sc_validation_output.packet_header_type, | |
632 | trace_sc_validation_output.packet_context_type, | |
633 | trace_sc_validation_output.event_header_type, | |
634 | trace_sc_validation_output.stream_event_ctx_type, | |
635 | event_context_type, event_payload_type, | |
636 | 1, 1, event_class->valid, &ec_validation_outputs[i], | |
637 | ec_validation_flags); | |
638 | BT_PUT(event_context_type); | |
639 | BT_PUT(event_payload_type); | |
640 | BT_PUT(event_class); | |
641 | ||
642 | if (ret) { | |
643 | goto end; | |
644 | } | |
645 | ||
646 | if ((ec_validation_outputs[i].valid_flags & | |
647 | ec_validation_flags) != ec_validation_flags) { | |
648 | /* Invalid event class */ | |
649 | ret = -1; | |
650 | goto end; | |
651 | } | |
652 | } | |
653 | ||
ef0c4a15 JG |
654 | stream_id = bt_ctf_stream_class_get_id(stream_class); |
655 | if (stream_id < 0) { | |
656 | stream_id = trace->next_stream_id++; | |
9ac68eb1 PP |
657 | if (stream_id < 0) { |
658 | ret = -1; | |
659 | goto end; | |
660 | } | |
ef0c4a15 JG |
661 | |
662 | /* Try to assign a new stream id */ | |
663 | for (i = 0; i < trace->stream_classes->len; i++) { | |
664 | if (stream_id == bt_ctf_stream_class_get_id( | |
665 | trace->stream_classes->pdata[i])) { | |
666 | /* Duplicate stream id found */ | |
667 | ret = -1; | |
668 | goto end; | |
669 | } | |
670 | } | |
671 | ||
0ddffae5 JG |
672 | if (bt_ctf_stream_class_set_id_no_check(stream_class, |
673 | stream_id)) { | |
ef0c4a15 JG |
674 | /* TODO Should retry with a different stream id */ |
675 | ret = -1; | |
676 | goto end; | |
677 | } | |
678 | } | |
679 | ||
e6a8e8e4 | 680 | bt_object_set_parent(stream_class, trace); |
ef0c4a15 JG |
681 | g_ptr_array_add(trace->stream_classes, stream_class); |
682 | ||
683 | /* | |
09840de5 PP |
684 | * At this point we know that the function will be successful. |
685 | * Therefore we can replace the trace and stream class field | |
686 | * types with what's in their validation output structure and | |
687 | * mark them as valid. We can also replace the field types of | |
688 | * all the event classes of the stream class and mark them as | |
689 | * valid. | |
690 | */ | |
691 | bt_ctf_validation_replace_types(trace, stream_class, NULL, | |
692 | &trace_sc_validation_output, trace_sc_validation_flags); | |
693 | trace->valid = 1; | |
694 | stream_class->valid = 1; | |
695 | ||
696 | /* | |
697 | * Put what was not moved in bt_ctf_validation_replace_types(). | |
698 | */ | |
699 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
700 | ||
6474e016 | 701 | for (i = 0; i < event_class_count; i++) { |
09840de5 | 702 | struct bt_ctf_event_class *event_class = |
9ac68eb1 PP |
703 | bt_ctf_stream_class_get_event_class_by_index( |
704 | stream_class, i); | |
09840de5 PP |
705 | |
706 | bt_ctf_validation_replace_types(NULL, NULL, event_class, | |
707 | &ec_validation_outputs[i], ec_validation_flags); | |
708 | event_class->valid = 1; | |
709 | BT_PUT(event_class); | |
710 | ||
711 | /* | |
712 | * Put what was not moved in | |
713 | * bt_ctf_validation_replace_types(). | |
714 | */ | |
715 | bt_ctf_validation_output_put_types(&ec_validation_outputs[i]); | |
716 | } | |
717 | ||
09840de5 PP |
718 | /* |
719 | * Freeze the trace and the stream class. | |
720 | */ | |
f67f3a6e | 721 | bt_ctf_stream_class_freeze(stream_class); |
09840de5 PP |
722 | bt_ctf_trace_freeze(trace); |
723 | ||
9b888ff3 JG |
724 | /* Notifiy listeners of the trace's schema modification. */ |
725 | bt_ctf_stream_class_visit(stream_class, | |
d9a13d86 | 726 | bt_ctf_trace_object_modification, trace); |
ef0c4a15 | 727 | end: |
d3814b54 | 728 | if (ret) { |
e6a8e8e4 | 729 | bt_object_set_parent(stream_class, NULL); |
09840de5 PP |
730 | |
731 | if (ec_validation_outputs) { | |
6474e016 | 732 | for (i = 0; i < event_class_count; i++) { |
09840de5 PP |
733 | bt_ctf_validation_output_put_types( |
734 | &ec_validation_outputs[i]); | |
735 | } | |
736 | } | |
d3814b54 | 737 | } |
09840de5 PP |
738 | |
739 | g_free(ec_validation_outputs); | |
740 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
2789e5d9 | 741 | bt_put(current_parent_trace); |
09840de5 PP |
742 | assert(!packet_header_type); |
743 | assert(!packet_context_type); | |
744 | assert(!event_header_type); | |
745 | assert(!stream_event_ctx_type); | |
746 | ||
ef0c4a15 JG |
747 | return ret; |
748 | } | |
749 | ||
544d0515 | 750 | int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace) |
c1e730fe | 751 | { |
544d0515 | 752 | int64_t ret; |
c1e730fe PP |
753 | |
754 | if (!trace) { | |
9ac68eb1 | 755 | ret = (int64_t) -1; |
c1e730fe PP |
756 | goto end; |
757 | } | |
758 | ||
9ac68eb1 | 759 | ret = (int64_t) trace->streams->len; |
c1e730fe PP |
760 | |
761 | end: | |
762 | return ret; | |
763 | } | |
764 | ||
9ac68eb1 PP |
765 | struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index( |
766 | struct bt_ctf_trace *trace, | |
767 | uint64_t index) | |
c1e730fe PP |
768 | { |
769 | struct bt_ctf_stream *stream = NULL; | |
770 | ||
771 | if (!trace || index >= trace->streams->len) { | |
772 | goto end; | |
773 | } | |
774 | ||
775 | stream = bt_get(g_ptr_array_index(trace->streams, index)); | |
776 | ||
777 | end: | |
778 | return stream; | |
779 | } | |
780 | ||
544d0515 | 781 | int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) |
ef0c4a15 | 782 | { |
544d0515 | 783 | int64_t ret; |
ef0c4a15 JG |
784 | |
785 | if (!trace) { | |
9ac68eb1 | 786 | ret = (int64_t) -1; |
ef0c4a15 JG |
787 | goto end; |
788 | } | |
789 | ||
9ac68eb1 | 790 | ret = (int64_t) trace->stream_classes->len; |
ef0c4a15 JG |
791 | end: |
792 | return ret; | |
793 | } | |
794 | ||
9ac68eb1 PP |
795 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index( |
796 | struct bt_ctf_trace *trace, uint64_t index) | |
ef0c4a15 JG |
797 | { |
798 | struct bt_ctf_stream_class *stream_class = NULL; | |
799 | ||
9ac68eb1 | 800 | if (!trace || index >= trace->stream_classes->len) { |
ef0c4a15 JG |
801 | goto end; |
802 | } | |
803 | ||
804 | stream_class = g_ptr_array_index(trace->stream_classes, index); | |
83509119 | 805 | bt_get(stream_class); |
ef0c4a15 JG |
806 | end: |
807 | return stream_class; | |
808 | } | |
809 | ||
4841ccc1 | 810 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id( |
9ac68eb1 | 811 | struct bt_ctf_trace *trace, uint64_t id_param) |
4841ccc1 PP |
812 | { |
813 | int i; | |
814 | struct bt_ctf_stream_class *stream_class = NULL; | |
9ac68eb1 | 815 | int64_t id = (int64_t) id_param; |
4841ccc1 | 816 | |
9ac68eb1 | 817 | if (!trace || id < 0) { |
4841ccc1 PP |
818 | goto end; |
819 | } | |
820 | ||
6474e016 | 821 | for (i = 0; i < trace->stream_classes->len; i++) { |
4841ccc1 PP |
822 | struct bt_ctf_stream_class *stream_class_candidate; |
823 | ||
824 | stream_class_candidate = | |
825 | g_ptr_array_index(trace->stream_classes, i); | |
826 | ||
827 | if (bt_ctf_stream_class_get_id(stream_class_candidate) == | |
828 | (int64_t) id) { | |
829 | stream_class = stream_class_candidate; | |
83509119 | 830 | bt_get(stream_class); |
4841ccc1 PP |
831 | goto end; |
832 | } | |
833 | } | |
834 | ||
835 | end: | |
836 | return stream_class; | |
837 | } | |
838 | ||
ac0c6bdd | 839 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name( |
7e4347a3 PP |
840 | struct bt_ctf_trace *trace, const char *name) |
841 | { | |
842 | size_t i; | |
ac0c6bdd | 843 | struct bt_ctf_clock_class *clock_class = NULL; |
7e4347a3 PP |
844 | |
845 | if (!trace || !name) { | |
846 | goto end; | |
847 | } | |
848 | ||
6474e016 | 849 | for (i = 0; i < trace->clocks->len; i++) { |
ac0c6bdd | 850 | struct bt_ctf_clock_class *cur_clk = |
7e4347a3 | 851 | g_ptr_array_index(trace->clocks, i); |
ac0c6bdd | 852 | const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk); |
7e4347a3 PP |
853 | |
854 | if (!cur_clk_name) { | |
855 | goto end; | |
856 | } | |
857 | ||
858 | if (!strcmp(cur_clk_name, name)) { | |
ac0c6bdd PP |
859 | clock_class = cur_clk; |
860 | bt_get(clock_class); | |
7e4347a3 PP |
861 | goto end; |
862 | } | |
863 | } | |
864 | ||
865 | end: | |
ac0c6bdd | 866 | return clock_class; |
7e4347a3 PP |
867 | } |
868 | ||
c9d90a34 PP |
869 | BT_HIDDEN |
870 | bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace *trace, | |
871 | struct bt_ctf_clock_class *clock_class) | |
872 | { | |
873 | struct search_query query = { .value = clock_class, .found = 0 }; | |
874 | ||
875 | assert(trace); | |
876 | assert(clock_class); | |
877 | ||
878 | g_ptr_array_foreach(trace->clocks, value_exists, &query); | |
879 | return query.found; | |
880 | } | |
881 | ||
bc37ae52 | 882 | BT_HIDDEN |
dc3fffef | 883 | const char *get_byte_order_string(enum bt_ctf_byte_order byte_order) |
bc37ae52 JG |
884 | { |
885 | const char *string; | |
886 | ||
887 | switch (byte_order) { | |
dc3fffef | 888 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
bc37ae52 JG |
889 | string = "le"; |
890 | break; | |
dc3fffef | 891 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
bc37ae52 JG |
892 | string = "be"; |
893 | break; | |
dc3fffef PP |
894 | case BT_CTF_BYTE_ORDER_NATIVE: |
895 | string = "native"; | |
bc37ae52 | 896 | break; |
dc3fffef PP |
897 | default: |
898 | assert(false); | |
bc37ae52 JG |
899 | } |
900 | ||
901 | return string; | |
902 | } | |
903 | ||
904 | static | |
905 | int append_trace_metadata(struct bt_ctf_trace *trace, | |
906 | struct metadata_context *context) | |
907 | { | |
908 | unsigned char *uuid = trace->uuid; | |
909 | int ret; | |
910 | ||
4a32fda0 PP |
911 | if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
912 | ret = -1; | |
913 | goto end; | |
914 | } | |
bc37ae52 | 915 | |
4a32fda0 | 916 | g_string_append(context->string, "trace {\n"); |
bc37ae52 JG |
917 | g_string_append(context->string, "\tmajor = 1;\n"); |
918 | g_string_append(context->string, "\tminor = 8;\n"); | |
dc3fffef PP |
919 | assert(trace->native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN || |
920 | trace->native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN || | |
921 | trace->native_byte_order == BT_CTF_BYTE_ORDER_NETWORK); | |
4a32fda0 PP |
922 | |
923 | if (trace->uuid_set) { | |
924 | g_string_append_printf(context->string, | |
925 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
926 | uuid[0], uuid[1], uuid[2], uuid[3], | |
927 | uuid[4], uuid[5], uuid[6], uuid[7], | |
928 | uuid[8], uuid[9], uuid[10], uuid[11], | |
929 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
930 | } | |
931 | ||
bc37ae52 | 932 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", |
dc3fffef | 933 | get_byte_order_string(trace->native_byte_order)); |
bc37ae52 JG |
934 | |
935 | g_string_append(context->string, "\tpacket.header := "); | |
936 | context->current_indentation_level++; | |
937 | g_string_assign(context->field_name, ""); | |
d246b111 | 938 | ret = bt_ctf_field_type_serialize(trace->packet_header_type, |
bc37ae52 JG |
939 | context); |
940 | if (ret) { | |
941 | goto end; | |
942 | } | |
943 | context->current_indentation_level--; | |
944 | ||
945 | g_string_append(context->string, ";\n};\n\n"); | |
946 | end: | |
947 | return ret; | |
948 | } | |
949 | ||
bc37ae52 JG |
950 | static |
951 | void append_env_metadata(struct bt_ctf_trace *trace, | |
952 | struct metadata_context *context) | |
953 | { | |
544d0515 PP |
954 | int64_t i; |
955 | int64_t env_size; | |
7f800dc7 PP |
956 | |
957 | env_size = bt_ctf_attributes_get_count(trace->environment); | |
958 | ||
959 | if (env_size <= 0) { | |
bc37ae52 JG |
960 | return; |
961 | } | |
962 | ||
963 | g_string_append(context->string, "env {\n"); | |
7f800dc7 | 964 | |
6474e016 | 965 | for (i = 0; i < env_size; i++) { |
dac5c838 | 966 | struct bt_value *env_field_value_obj = NULL; |
7f800dc7 | 967 | const char *entry_name; |
7f800dc7 PP |
968 | |
969 | entry_name = bt_ctf_attributes_get_field_name( | |
970 | trace->environment, i); | |
971 | env_field_value_obj = bt_ctf_attributes_get_field_value( | |
972 | trace->environment, i); | |
973 | ||
974 | if (!entry_name || !env_field_value_obj) { | |
975 | goto loop_next; | |
976 | } | |
977 | ||
dac5c838 PP |
978 | switch (bt_value_get_type(env_field_value_obj)) { |
979 | case BT_VALUE_TYPE_INTEGER: | |
b8248cc0 PP |
980 | { |
981 | int ret; | |
982 | int64_t int_value; | |
983 | ||
dac5c838 | 984 | ret = bt_value_integer_get(env_field_value_obj, |
7f800dc7 PP |
985 | &int_value); |
986 | ||
987 | if (ret) { | |
988 | goto loop_next; | |
989 | } | |
990 | ||
991 | g_string_append_printf(context->string, | |
992 | "\t%s = %" PRId64 ";\n", entry_name, | |
993 | int_value); | |
994 | break; | |
b8248cc0 | 995 | } |
dac5c838 | 996 | case BT_VALUE_TYPE_STRING: |
7f800dc7 PP |
997 | { |
998 | int ret; | |
999 | const char *str_value; | |
1000 | char *escaped_str = NULL; | |
1001 | ||
dac5c838 | 1002 | ret = bt_value_string_get(env_field_value_obj, |
7f800dc7 PP |
1003 | &str_value); |
1004 | ||
1005 | if (ret) { | |
1006 | goto loop_next; | |
1007 | } | |
1008 | ||
1009 | escaped_str = g_strescape(str_value, NULL); | |
1010 | ||
1011 | if (!escaped_str) { | |
1012 | goto loop_next; | |
1013 | } | |
1014 | ||
1015 | g_string_append_printf(context->string, | |
1016 | "\t%s = \"%s\";\n", entry_name, escaped_str); | |
1017 | free(escaped_str); | |
1018 | break; | |
1019 | } | |
1020 | ||
1021 | default: | |
1022 | goto loop_next; | |
1023 | } | |
1024 | ||
1025 | loop_next: | |
83509119 | 1026 | BT_PUT(env_field_value_obj); |
7f800dc7 PP |
1027 | } |
1028 | ||
bc37ae52 JG |
1029 | g_string_append(context->string, "};\n\n"); |
1030 | } | |
1031 | ||
1032 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) | |
1033 | { | |
1034 | char *metadata = NULL; | |
1035 | struct metadata_context *context = NULL; | |
1036 | int err = 0; | |
1037 | size_t i; | |
1038 | ||
1039 | if (!trace) { | |
1040 | goto end; | |
1041 | } | |
1042 | ||
1043 | context = g_new0(struct metadata_context, 1); | |
1044 | if (!context) { | |
1045 | goto end; | |
1046 | } | |
1047 | ||
1048 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); | |
1049 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); | |
1050 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); | |
1051 | if (append_trace_metadata(trace, context)) { | |
1052 | goto error; | |
1053 | } | |
1054 | append_env_metadata(trace, context); | |
1055 | g_ptr_array_foreach(trace->clocks, | |
ac0c6bdd | 1056 | (GFunc)bt_ctf_clock_class_serialize, context); |
bc37ae52 JG |
1057 | |
1058 | for (i = 0; i < trace->stream_classes->len; i++) { | |
1059 | err = bt_ctf_stream_class_serialize( | |
1060 | trace->stream_classes->pdata[i], context); | |
1061 | if (err) { | |
1062 | goto error; | |
1063 | } | |
1064 | } | |
1065 | ||
1066 | metadata = context->string->str; | |
1067 | error: | |
1068 | g_string_free(context->string, err ? TRUE : FALSE); | |
1069 | g_string_free(context->field_name, TRUE); | |
1070 | g_free(context); | |
1071 | end: | |
1072 | return metadata; | |
1073 | } | |
1074 | ||
8a716c8d PP |
1075 | enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order( |
1076 | struct bt_ctf_trace *trace) | |
4ed90fb3 JG |
1077 | { |
1078 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; | |
1079 | ||
1080 | if (!trace) { | |
1081 | goto end; | |
1082 | } | |
1083 | ||
dc3fffef PP |
1084 | ret = trace->native_byte_order; |
1085 | ||
4ed90fb3 JG |
1086 | end: |
1087 | return ret; | |
1088 | } | |
1089 | ||
391c8f0d | 1090 | int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace, |
bc37ae52 JG |
1091 | enum bt_ctf_byte_order byte_order) |
1092 | { | |
1093 | int ret = 0; | |
bc37ae52 JG |
1094 | |
1095 | if (!trace || trace->frozen) { | |
1096 | ret = -1; | |
1097 | goto end; | |
1098 | } | |
1099 | ||
dc3fffef PP |
1100 | if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN && |
1101 | byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN && | |
1102 | byte_order != BT_CTF_BYTE_ORDER_NETWORK) { | |
bc37ae52 JG |
1103 | ret = -1; |
1104 | goto end; | |
1105 | } | |
1106 | ||
dc3fffef PP |
1107 | trace->native_byte_order = byte_order; |
1108 | ||
bc37ae52 JG |
1109 | end: |
1110 | return ret; | |
1111 | } | |
1112 | ||
d246b111 JG |
1113 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( |
1114 | struct bt_ctf_trace *trace) | |
1115 | { | |
1116 | struct bt_ctf_field_type *field_type = NULL; | |
1117 | ||
1118 | if (!trace) { | |
1119 | goto end; | |
1120 | } | |
1121 | ||
83509119 | 1122 | bt_get(trace->packet_header_type); |
d246b111 JG |
1123 | field_type = trace->packet_header_type; |
1124 | end: | |
1125 | return field_type; | |
1126 | } | |
1127 | ||
1128 | int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, | |
1129 | struct bt_ctf_field_type *packet_header_type) | |
1130 | { | |
1131 | int ret = 0; | |
1132 | ||
835b2d10 | 1133 | if (!trace || trace->frozen) { |
d246b111 JG |
1134 | ret = -1; |
1135 | goto end; | |
1136 | } | |
1137 | ||
835b2d10 JG |
1138 | /* packet_header_type must be a structure. */ |
1139 | if (packet_header_type && | |
1140 | bt_ctf_field_type_get_type_id(packet_header_type) != | |
1487a16a | 1141 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
d246b111 JG |
1142 | ret = -1; |
1143 | goto end; | |
1144 | } | |
1145 | ||
83509119 | 1146 | bt_put(trace->packet_header_type); |
835b2d10 | 1147 | trace->packet_header_type = bt_get(packet_header_type); |
d246b111 JG |
1148 | end: |
1149 | return ret; | |
1150 | } | |
1151 | ||
8bf65fbd | 1152 | static |
544d0515 | 1153 | int64_t get_stream_class_count(void *element) |
8bf65fbd JG |
1154 | { |
1155 | return bt_ctf_trace_get_stream_class_count( | |
1156 | (struct bt_ctf_trace *) element); | |
1157 | } | |
1158 | ||
1159 | static | |
1160 | void *get_stream_class(void *element, int i) | |
1161 | { | |
9ac68eb1 | 1162 | return bt_ctf_trace_get_stream_class_by_index( |
8bf65fbd JG |
1163 | (struct bt_ctf_trace *) element, i); |
1164 | } | |
1165 | ||
1166 | static | |
d9a13d86 | 1167 | int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data) |
8bf65fbd | 1168 | { |
d9a13d86 | 1169 | return bt_ctf_stream_class_visit(object, visitor, data); |
8bf65fbd JG |
1170 | } |
1171 | ||
1172 | int bt_ctf_trace_visit(struct bt_ctf_trace *trace, | |
d9a13d86 | 1173 | bt_ctf_visitor visitor, void *data) |
8bf65fbd JG |
1174 | { |
1175 | int ret; | |
d9a13d86 PP |
1176 | struct bt_ctf_object obj = |
1177 | { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE }; | |
8bf65fbd JG |
1178 | |
1179 | if (!trace || !visitor) { | |
1180 | ret = -1; | |
1181 | goto end; | |
1182 | } | |
1183 | ||
d9a13d86 | 1184 | ret = visitor_helper(&obj, get_stream_class_count, |
8bf65fbd JG |
1185 | get_stream_class, visit_stream_class, visitor, data); |
1186 | end: | |
1187 | return ret; | |
1188 | } | |
1189 | ||
1190 | static | |
d9a13d86 | 1191 | int invoke_listener(struct bt_ctf_object *object, void *data) |
8bf65fbd | 1192 | { |
9b888ff3 | 1193 | struct listener_wrapper *listener_wrapper = data; |
8bf65fbd | 1194 | |
d9a13d86 | 1195 | listener_wrapper->listener(object, listener_wrapper->data); |
9b888ff3 | 1196 | return 0; |
8bf65fbd JG |
1197 | } |
1198 | ||
50ad4244 JG |
1199 | int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace, |
1200 | bt_ctf_listener_cb listener, void *listener_data) | |
2204c381 JG |
1201 | { |
1202 | int ret = 0; | |
50ad4244 JG |
1203 | struct listener_wrapper *listener_wrapper = |
1204 | g_new0(struct listener_wrapper, 1); | |
2204c381 | 1205 | |
50ad4244 | 1206 | if (!trace || !listener || !listener_wrapper) { |
2204c381 JG |
1207 | ret = -1; |
1208 | goto error; | |
1209 | } | |
1210 | ||
50ad4244 JG |
1211 | listener_wrapper->listener = listener; |
1212 | listener_wrapper->data = listener_data; | |
8bf65fbd | 1213 | |
50ad4244 | 1214 | /* Visit the current schema. */ |
9b888ff3 | 1215 | ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper); |
8bf65fbd JG |
1216 | if (ret) { |
1217 | goto error; | |
1218 | } | |
1219 | ||
50ad4244 JG |
1220 | /* |
1221 | * Add listener to the array of callbacks which will be invoked on | |
1222 | * schema changes. | |
1223 | */ | |
1224 | g_ptr_array_add(trace->listeners, listener_wrapper); | |
2204c381 JG |
1225 | return ret; |
1226 | error: | |
50ad4244 | 1227 | g_free(listener_wrapper); |
2204c381 JG |
1228 | return ret; |
1229 | } | |
1230 | ||
bc37ae52 | 1231 | BT_HIDDEN |
d9a13d86 | 1232 | int bt_ctf_trace_object_modification(struct bt_ctf_object *object, |
9b888ff3 JG |
1233 | void *trace_ptr) |
1234 | { | |
1235 | size_t i; | |
1236 | struct bt_ctf_trace *trace = trace_ptr; | |
1237 | ||
1238 | assert(trace); | |
d9a13d86 | 1239 | assert(object); |
9b888ff3 JG |
1240 | |
1241 | if (trace->listeners->len == 0) { | |
1242 | goto end; | |
1243 | } | |
1244 | ||
1245 | for (i = 0; i < trace->listeners->len; i++) { | |
1246 | struct listener_wrapper *listener = | |
1247 | g_ptr_array_index(trace->listeners, i); | |
1248 | ||
d9a13d86 | 1249 | listener->listener(object, listener->data); |
9b888ff3 JG |
1250 | } |
1251 | end: | |
1252 | return 0; | |
1253 | } | |
1254 | ||
1255 | BT_HIDDEN | |
bc37ae52 JG |
1256 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) |
1257 | { | |
a25e0d14 | 1258 | int ret; |
bc37ae52 | 1259 | unsigned int alignment, size; |
8bdcb82e | 1260 | struct bt_ctf_field_type *field_type = NULL; |
bc37ae52 JG |
1261 | |
1262 | if (alias >= NR_FIELD_TYPE_ALIAS) { | |
8bdcb82e | 1263 | goto end; |
bc37ae52 JG |
1264 | } |
1265 | ||
1266 | alignment = field_type_aliases_alignments[alias]; | |
1267 | size = field_type_aliases_sizes[alias]; | |
1268 | field_type = bt_ctf_field_type_integer_create(size); | |
a25e0d14 JG |
1269 | ret = bt_ctf_field_type_set_alignment(field_type, alignment); |
1270 | if (ret) { | |
1271 | BT_PUT(field_type); | |
1272 | } | |
8bdcb82e | 1273 | end: |
bc37ae52 JG |
1274 | return field_type; |
1275 | } | |
1276 | ||
f67f3a6e | 1277 | static |
09840de5 | 1278 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace) |
f67f3a6e | 1279 | { |
6474e016 PP |
1280 | int i; |
1281 | ||
09840de5 | 1282 | bt_ctf_field_type_freeze(trace->packet_header_type); |
f67f3a6e | 1283 | bt_ctf_attributes_freeze(trace->environment); |
6474e016 PP |
1284 | |
1285 | for (i = 0; i < trace->clocks->len; i++) { | |
ac0c6bdd | 1286 | struct bt_ctf_clock_class *clock_class = |
6474e016 PP |
1287 | g_ptr_array_index(trace->clocks, i); |
1288 | ||
ac0c6bdd | 1289 | bt_ctf_clock_class_freeze(clock_class); |
6474e016 PP |
1290 | } |
1291 | ||
f67f3a6e JG |
1292 | trace->frozen = 1; |
1293 | } | |
1294 | ||
5acf2ae6 PP |
1295 | bool bt_ctf_trace_is_static(struct bt_ctf_trace *trace) |
1296 | { | |
1297 | bool is_static = false; | |
1298 | ||
1299 | if (!trace) { | |
1300 | goto end; | |
1301 | } | |
1302 | ||
1303 | is_static = trace->is_static; | |
1304 | ||
1305 | end: | |
1306 | return is_static; | |
1307 | } | |
1308 | ||
1309 | int bt_ctf_trace_set_is_static(struct bt_ctf_trace *trace) | |
1310 | { | |
1311 | int ret = 0; | |
1312 | ||
1313 | if (!trace) { | |
1314 | ret = -1; | |
1315 | goto end; | |
1316 | } | |
1317 | ||
1318 | trace->is_static = true; | |
1319 | bt_ctf_trace_freeze(trace); | |
1320 | ||
1321 | end: | |
1322 | return ret; | |
1323 | } |