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