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