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> |
b2e0c907 | 43 | #include <babeltrace/graph/notification-schema.h> |
bc37ae52 | 44 | #include <babeltrace/compiler.h> |
dac5c838 | 45 | #include <babeltrace/values.h> |
83509119 | 46 | #include <babeltrace/ref.h> |
a0b720b2 | 47 | #include <babeltrace/endian.h> |
dc3fffef | 48 | #include <inttypes.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 | ||
92 | bt_ctf_trace_set_byte_order(trace, 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 | ||
e6fa2160 JG |
326 | int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
327 | { | |
328 | int ret = 0; | |
329 | ||
330 | if (!trace) { | |
331 | ret = -1; | |
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 * | |
342 | bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace, | |
343 | int index) | |
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 | ||
dac5c838 | 357 | struct bt_value *bt_ctf_trace_get_environment_field_value( |
7f800dc7 | 358 | struct bt_ctf_trace *trace, int 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; | |
ac0c6bdd | 392 | struct search_query query = { .value = clock_class, .found = 0 }; |
bc37ae52 | 393 | |
ac0c6bdd | 394 | if (!trace || !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 */ |
bc37ae52 JG |
400 | g_ptr_array_foreach(trace->clocks, value_exists, &query); |
401 | if (query.found) { | |
402 | ret = -1; | |
403 | goto end; | |
404 | } | |
405 | ||
ac0c6bdd PP |
406 | bt_get(clock_class); |
407 | g_ptr_array_add(trace->clocks, clock_class); | |
cfeb617e | 408 | |
6474e016 | 409 | if (trace->frozen) { |
ac0c6bdd | 410 | bt_ctf_clock_class_freeze(clock_class); |
6474e016 | 411 | } |
bc37ae52 JG |
412 | end: |
413 | return ret; | |
414 | } | |
415 | ||
ac0c6bdd | 416 | int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace) |
884cd6c3 JG |
417 | { |
418 | int ret = -1; | |
419 | ||
420 | if (!trace) { | |
421 | goto end; | |
422 | } | |
423 | ||
424 | ret = trace->clocks->len; | |
425 | end: | |
426 | return ret; | |
427 | } | |
428 | ||
ac0c6bdd PP |
429 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class( |
430 | struct bt_ctf_trace *trace, int index) | |
884cd6c3 | 431 | { |
ac0c6bdd | 432 | struct bt_ctf_clock_class *clock_class = NULL; |
884cd6c3 JG |
433 | |
434 | if (!trace || index < 0 || index >= trace->clocks->len) { | |
435 | goto end; | |
436 | } | |
437 | ||
ac0c6bdd PP |
438 | clock_class = g_ptr_array_index(trace->clocks, index); |
439 | bt_get(clock_class); | |
884cd6c3 | 440 | end: |
ac0c6bdd | 441 | return clock_class; |
884cd6c3 JG |
442 | } |
443 | ||
ef0c4a15 JG |
444 | int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, |
445 | struct bt_ctf_stream_class *stream_class) | |
446 | { | |
447 | int ret, i; | |
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; | |
460 | int event_class_count; | |
2789e5d9 | 461 | struct bt_ctf_trace *current_parent_trace = NULL; |
ef0c4a15 JG |
462 | |
463 | if (!trace || !stream_class) { | |
464 | ret = -1; | |
465 | goto end; | |
466 | } | |
467 | ||
2789e5d9 JG |
468 | current_parent_trace = bt_ctf_stream_class_get_trace(stream_class); |
469 | if (current_parent_trace) { | |
470 | /* Stream class is already associated to a trace, abort. */ | |
471 | ret = -1; | |
472 | goto end; | |
473 | } | |
474 | ||
09840de5 PP |
475 | event_class_count = |
476 | bt_ctf_stream_class_get_event_class_count(stream_class); | |
477 | assert(event_class_count >= 0); | |
478 | ||
479 | /* Check for duplicate stream classes */ | |
ef0c4a15 JG |
480 | for (i = 0; i < trace->stream_classes->len; i++) { |
481 | if (trace->stream_classes->pdata[i] == stream_class) { | |
09840de5 | 482 | /* Stream class already registered to the trace */ |
ef0c4a15 JG |
483 | ret = -1; |
484 | goto end; | |
485 | } | |
486 | } | |
487 | ||
fe13b5c0 | 488 | if (stream_class->clock) { |
ac0c6bdd PP |
489 | struct bt_ctf_clock_class *stream_clock_class = |
490 | stream_class->clock->clock_class; | |
491 | ||
492 | if (trace->is_created_by_writer) { | |
493 | /* | |
494 | * Make sure this clock was also added to the | |
495 | * trace (potentially through its CTF writer | |
496 | * owner). | |
497 | */ | |
498 | size_t i; | |
499 | ||
500 | for (i = 0; i < trace->clocks->len; i++) { | |
501 | if (trace->clocks->pdata[i] == | |
502 | stream_clock_class) { | |
503 | /* Found! */ | |
504 | break; | |
505 | } | |
506 | } | |
507 | ||
508 | if (i == trace->clocks->len) { | |
509 | /* Not found */ | |
fe13b5c0 PP |
510 | ret = -1; |
511 | goto end; | |
512 | } | |
513 | } else { | |
ac0c6bdd PP |
514 | /* |
515 | * This trace was NOT created by a CTF writer, | |
516 | * thus do not allow the stream class to add to | |
517 | * have a clock at all. Those are two | |
518 | * independent APIs (non-writer and writer | |
519 | * APIs), and isolating them simplifies things. | |
520 | */ | |
521 | ret = -1; | |
522 | goto end; | |
fe13b5c0 PP |
523 | } |
524 | } | |
525 | ||
09840de5 PP |
526 | /* |
527 | * We're about to freeze both the trace and the stream class. | |
528 | * Also, each event class contained in this stream class are | |
529 | * already frozen. | |
530 | * | |
531 | * This trace, this stream class, and all its event classes | |
532 | * should be valid at this point. | |
533 | * | |
534 | * Validate trace and stream class first, then each event | |
535 | * class of this stream class can be validated individually. | |
536 | */ | |
537 | packet_header_type = | |
538 | bt_ctf_trace_get_packet_header_type(trace); | |
539 | packet_context_type = | |
540 | bt_ctf_stream_class_get_packet_context_type(stream_class); | |
541 | event_header_type = | |
542 | bt_ctf_stream_class_get_event_header_type(stream_class); | |
543 | stream_event_ctx_type = | |
544 | bt_ctf_stream_class_get_event_context_type(stream_class); | |
545 | ret = bt_ctf_validate_class_types(trace->environment, | |
546 | packet_header_type, packet_context_type, event_header_type, | |
547 | stream_event_ctx_type, NULL, NULL, trace->valid, | |
548 | stream_class->valid, 1, &trace_sc_validation_output, | |
549 | trace_sc_validation_flags); | |
550 | BT_PUT(packet_header_type); | |
551 | BT_PUT(packet_context_type); | |
552 | BT_PUT(event_header_type); | |
553 | BT_PUT(stream_event_ctx_type); | |
554 | ||
2bd7f864 | 555 | if (ret) { |
09840de5 PP |
556 | /* |
557 | * This means something went wrong during the validation | |
558 | * process, not that the objects are invalid. | |
559 | */ | |
2bd7f864 JG |
560 | goto end; |
561 | } | |
562 | ||
09840de5 PP |
563 | if ((trace_sc_validation_output.valid_flags & |
564 | trace_sc_validation_flags) != | |
565 | trace_sc_validation_flags) { | |
566 | /* Invalid trace/stream class */ | |
567 | ret = -1; | |
568 | goto end; | |
569 | } | |
570 | ||
571 | if (event_class_count > 0) { | |
572 | ec_validation_outputs = g_new0(struct bt_ctf_validation_output, | |
573 | event_class_count); | |
574 | if (!ec_validation_outputs) { | |
575 | ret = -1; | |
576 | goto end; | |
577 | } | |
578 | } | |
579 | ||
580 | /* Validate each event class individually */ | |
6474e016 | 581 | for (i = 0; i < event_class_count; i++) { |
09840de5 PP |
582 | struct bt_ctf_event_class *event_class = |
583 | bt_ctf_stream_class_get_event_class(stream_class, i); | |
584 | struct bt_ctf_field_type *event_context_type = NULL; | |
585 | struct bt_ctf_field_type *event_payload_type = NULL; | |
586 | ||
587 | event_context_type = | |
588 | bt_ctf_event_class_get_context_type(event_class); | |
589 | event_payload_type = | |
590 | bt_ctf_event_class_get_payload_type(event_class); | |
591 | ||
592 | /* | |
593 | * It is important to use the field types returned by | |
594 | * the previous trace and stream class validation here | |
595 | * because copies could have been made. | |
596 | */ | |
597 | ret = bt_ctf_validate_class_types(trace->environment, | |
598 | trace_sc_validation_output.packet_header_type, | |
599 | trace_sc_validation_output.packet_context_type, | |
600 | trace_sc_validation_output.event_header_type, | |
601 | trace_sc_validation_output.stream_event_ctx_type, | |
602 | event_context_type, event_payload_type, | |
603 | 1, 1, event_class->valid, &ec_validation_outputs[i], | |
604 | ec_validation_flags); | |
605 | BT_PUT(event_context_type); | |
606 | BT_PUT(event_payload_type); | |
607 | BT_PUT(event_class); | |
608 | ||
609 | if (ret) { | |
610 | goto end; | |
611 | } | |
612 | ||
613 | if ((ec_validation_outputs[i].valid_flags & | |
614 | ec_validation_flags) != ec_validation_flags) { | |
615 | /* Invalid event class */ | |
616 | ret = -1; | |
617 | goto end; | |
618 | } | |
619 | } | |
620 | ||
ef0c4a15 JG |
621 | stream_id = bt_ctf_stream_class_get_id(stream_class); |
622 | if (stream_id < 0) { | |
623 | stream_id = trace->next_stream_id++; | |
624 | ||
625 | /* Try to assign a new stream id */ | |
626 | for (i = 0; i < trace->stream_classes->len; i++) { | |
627 | if (stream_id == bt_ctf_stream_class_get_id( | |
628 | trace->stream_classes->pdata[i])) { | |
629 | /* Duplicate stream id found */ | |
630 | ret = -1; | |
631 | goto end; | |
632 | } | |
633 | } | |
634 | ||
0ddffae5 JG |
635 | if (bt_ctf_stream_class_set_id_no_check(stream_class, |
636 | stream_id)) { | |
ef0c4a15 JG |
637 | /* TODO Should retry with a different stream id */ |
638 | ret = -1; | |
639 | goto end; | |
640 | } | |
641 | } | |
642 | ||
e6a8e8e4 | 643 | bt_object_set_parent(stream_class, trace); |
ef0c4a15 JG |
644 | g_ptr_array_add(trace->stream_classes, stream_class); |
645 | ||
646 | /* | |
09840de5 PP |
647 | * At this point we know that the function will be successful. |
648 | * Therefore we can replace the trace and stream class field | |
649 | * types with what's in their validation output structure and | |
650 | * mark them as valid. We can also replace the field types of | |
651 | * all the event classes of the stream class and mark them as | |
652 | * valid. | |
653 | */ | |
654 | bt_ctf_validation_replace_types(trace, stream_class, NULL, | |
655 | &trace_sc_validation_output, trace_sc_validation_flags); | |
656 | trace->valid = 1; | |
657 | stream_class->valid = 1; | |
658 | ||
659 | /* | |
660 | * Put what was not moved in bt_ctf_validation_replace_types(). | |
661 | */ | |
662 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
663 | ||
6474e016 | 664 | for (i = 0; i < event_class_count; i++) { |
09840de5 PP |
665 | struct bt_ctf_event_class *event_class = |
666 | bt_ctf_stream_class_get_event_class(stream_class, i); | |
667 | ||
668 | bt_ctf_validation_replace_types(NULL, NULL, event_class, | |
669 | &ec_validation_outputs[i], ec_validation_flags); | |
670 | event_class->valid = 1; | |
671 | BT_PUT(event_class); | |
672 | ||
673 | /* | |
674 | * Put what was not moved in | |
675 | * bt_ctf_validation_replace_types(). | |
676 | */ | |
677 | bt_ctf_validation_output_put_types(&ec_validation_outputs[i]); | |
678 | } | |
679 | ||
09840de5 PP |
680 | /* |
681 | * Freeze the trace and the stream class. | |
682 | */ | |
f67f3a6e | 683 | bt_ctf_stream_class_freeze(stream_class); |
09840de5 PP |
684 | bt_ctf_trace_freeze(trace); |
685 | ||
9b888ff3 JG |
686 | /* Notifiy listeners of the trace's schema modification. */ |
687 | bt_ctf_stream_class_visit(stream_class, | |
d9a13d86 | 688 | bt_ctf_trace_object_modification, trace); |
ef0c4a15 | 689 | end: |
d3814b54 | 690 | if (ret) { |
e6a8e8e4 | 691 | bt_object_set_parent(stream_class, NULL); |
09840de5 PP |
692 | |
693 | if (ec_validation_outputs) { | |
6474e016 | 694 | for (i = 0; i < event_class_count; i++) { |
09840de5 PP |
695 | bt_ctf_validation_output_put_types( |
696 | &ec_validation_outputs[i]); | |
697 | } | |
698 | } | |
d3814b54 | 699 | } |
09840de5 PP |
700 | |
701 | g_free(ec_validation_outputs); | |
702 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
2789e5d9 | 703 | bt_put(current_parent_trace); |
09840de5 PP |
704 | assert(!packet_header_type); |
705 | assert(!packet_context_type); | |
706 | assert(!event_header_type); | |
707 | assert(!stream_event_ctx_type); | |
708 | ||
ef0c4a15 JG |
709 | return ret; |
710 | } | |
711 | ||
712 | int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) | |
713 | { | |
714 | int ret; | |
715 | ||
716 | if (!trace) { | |
717 | ret = -1; | |
718 | goto end; | |
719 | } | |
720 | ||
721 | ret = trace->stream_classes->len; | |
722 | end: | |
723 | return ret; | |
724 | } | |
725 | ||
726 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class( | |
727 | struct bt_ctf_trace *trace, int index) | |
728 | { | |
729 | struct bt_ctf_stream_class *stream_class = NULL; | |
730 | ||
731 | if (!trace || index < 0 || index >= trace->stream_classes->len) { | |
732 | goto end; | |
733 | } | |
734 | ||
735 | stream_class = g_ptr_array_index(trace->stream_classes, index); | |
83509119 | 736 | bt_get(stream_class); |
ef0c4a15 JG |
737 | end: |
738 | return stream_class; | |
739 | } | |
740 | ||
4841ccc1 PP |
741 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id( |
742 | struct bt_ctf_trace *trace, uint32_t id) | |
743 | { | |
744 | int i; | |
745 | struct bt_ctf_stream_class *stream_class = NULL; | |
746 | ||
747 | if (!trace) { | |
748 | goto end; | |
749 | } | |
750 | ||
6474e016 | 751 | for (i = 0; i < trace->stream_classes->len; i++) { |
4841ccc1 PP |
752 | struct bt_ctf_stream_class *stream_class_candidate; |
753 | ||
754 | stream_class_candidate = | |
755 | g_ptr_array_index(trace->stream_classes, i); | |
756 | ||
757 | if (bt_ctf_stream_class_get_id(stream_class_candidate) == | |
758 | (int64_t) id) { | |
759 | stream_class = stream_class_candidate; | |
83509119 | 760 | bt_get(stream_class); |
4841ccc1 PP |
761 | goto end; |
762 | } | |
763 | } | |
764 | ||
765 | end: | |
766 | return stream_class; | |
767 | } | |
768 | ||
ac0c6bdd | 769 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name( |
7e4347a3 PP |
770 | struct bt_ctf_trace *trace, const char *name) |
771 | { | |
772 | size_t i; | |
ac0c6bdd | 773 | struct bt_ctf_clock_class *clock_class = NULL; |
7e4347a3 PP |
774 | |
775 | if (!trace || !name) { | |
776 | goto end; | |
777 | } | |
778 | ||
6474e016 | 779 | for (i = 0; i < trace->clocks->len; i++) { |
ac0c6bdd | 780 | struct bt_ctf_clock_class *cur_clk = |
7e4347a3 | 781 | g_ptr_array_index(trace->clocks, i); |
ac0c6bdd | 782 | const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk); |
7e4347a3 PP |
783 | |
784 | if (!cur_clk_name) { | |
785 | goto end; | |
786 | } | |
787 | ||
788 | if (!strcmp(cur_clk_name, name)) { | |
ac0c6bdd PP |
789 | clock_class = cur_clk; |
790 | bt_get(clock_class); | |
7e4347a3 PP |
791 | goto end; |
792 | } | |
793 | } | |
794 | ||
795 | end: | |
ac0c6bdd | 796 | return clock_class; |
7e4347a3 PP |
797 | } |
798 | ||
bc37ae52 | 799 | BT_HIDDEN |
dc3fffef | 800 | const char *get_byte_order_string(enum bt_ctf_byte_order byte_order) |
bc37ae52 JG |
801 | { |
802 | const char *string; | |
803 | ||
804 | switch (byte_order) { | |
dc3fffef | 805 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
bc37ae52 JG |
806 | string = "le"; |
807 | break; | |
dc3fffef | 808 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
bc37ae52 JG |
809 | string = "be"; |
810 | break; | |
dc3fffef PP |
811 | case BT_CTF_BYTE_ORDER_NATIVE: |
812 | string = "native"; | |
bc37ae52 | 813 | break; |
dc3fffef PP |
814 | default: |
815 | assert(false); | |
bc37ae52 JG |
816 | } |
817 | ||
818 | return string; | |
819 | } | |
820 | ||
821 | static | |
822 | int append_trace_metadata(struct bt_ctf_trace *trace, | |
823 | struct metadata_context *context) | |
824 | { | |
825 | unsigned char *uuid = trace->uuid; | |
826 | int ret; | |
827 | ||
828 | g_string_append(context->string, "trace {\n"); | |
829 | ||
830 | g_string_append(context->string, "\tmajor = 1;\n"); | |
831 | g_string_append(context->string, "\tminor = 8;\n"); | |
dc3fffef PP |
832 | assert(trace->native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN || |
833 | trace->native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN || | |
834 | trace->native_byte_order == BT_CTF_BYTE_ORDER_NETWORK); | |
bc37ae52 JG |
835 | g_string_append_printf(context->string, |
836 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
837 | uuid[0], uuid[1], uuid[2], uuid[3], | |
838 | uuid[4], uuid[5], uuid[6], uuid[7], | |
839 | uuid[8], uuid[9], uuid[10], uuid[11], | |
840 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
841 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", | |
dc3fffef | 842 | get_byte_order_string(trace->native_byte_order)); |
bc37ae52 JG |
843 | |
844 | g_string_append(context->string, "\tpacket.header := "); | |
845 | context->current_indentation_level++; | |
846 | g_string_assign(context->field_name, ""); | |
d246b111 | 847 | ret = bt_ctf_field_type_serialize(trace->packet_header_type, |
bc37ae52 JG |
848 | context); |
849 | if (ret) { | |
850 | goto end; | |
851 | } | |
852 | context->current_indentation_level--; | |
853 | ||
854 | g_string_append(context->string, ";\n};\n\n"); | |
855 | end: | |
856 | return ret; | |
857 | } | |
858 | ||
bc37ae52 JG |
859 | static |
860 | void append_env_metadata(struct bt_ctf_trace *trace, | |
861 | struct metadata_context *context) | |
862 | { | |
7f800dc7 PP |
863 | int i; |
864 | int env_size; | |
865 | ||
866 | env_size = bt_ctf_attributes_get_count(trace->environment); | |
867 | ||
868 | if (env_size <= 0) { | |
bc37ae52 JG |
869 | return; |
870 | } | |
871 | ||
872 | g_string_append(context->string, "env {\n"); | |
7f800dc7 | 873 | |
6474e016 | 874 | for (i = 0; i < env_size; i++) { |
dac5c838 | 875 | struct bt_value *env_field_value_obj = NULL; |
7f800dc7 | 876 | const char *entry_name; |
7f800dc7 PP |
877 | |
878 | entry_name = bt_ctf_attributes_get_field_name( | |
879 | trace->environment, i); | |
880 | env_field_value_obj = bt_ctf_attributes_get_field_value( | |
881 | trace->environment, i); | |
882 | ||
883 | if (!entry_name || !env_field_value_obj) { | |
884 | goto loop_next; | |
885 | } | |
886 | ||
dac5c838 PP |
887 | switch (bt_value_get_type(env_field_value_obj)) { |
888 | case BT_VALUE_TYPE_INTEGER: | |
b8248cc0 PP |
889 | { |
890 | int ret; | |
891 | int64_t int_value; | |
892 | ||
dac5c838 | 893 | ret = bt_value_integer_get(env_field_value_obj, |
7f800dc7 PP |
894 | &int_value); |
895 | ||
896 | if (ret) { | |
897 | goto loop_next; | |
898 | } | |
899 | ||
900 | g_string_append_printf(context->string, | |
901 | "\t%s = %" PRId64 ";\n", entry_name, | |
902 | int_value); | |
903 | break; | |
b8248cc0 | 904 | } |
dac5c838 | 905 | case BT_VALUE_TYPE_STRING: |
7f800dc7 PP |
906 | { |
907 | int ret; | |
908 | const char *str_value; | |
909 | char *escaped_str = NULL; | |
910 | ||
dac5c838 | 911 | ret = bt_value_string_get(env_field_value_obj, |
7f800dc7 PP |
912 | &str_value); |
913 | ||
914 | if (ret) { | |
915 | goto loop_next; | |
916 | } | |
917 | ||
918 | escaped_str = g_strescape(str_value, NULL); | |
919 | ||
920 | if (!escaped_str) { | |
921 | goto loop_next; | |
922 | } | |
923 | ||
924 | g_string_append_printf(context->string, | |
925 | "\t%s = \"%s\";\n", entry_name, escaped_str); | |
926 | free(escaped_str); | |
927 | break; | |
928 | } | |
929 | ||
930 | default: | |
931 | goto loop_next; | |
932 | } | |
933 | ||
934 | loop_next: | |
83509119 | 935 | BT_PUT(env_field_value_obj); |
7f800dc7 PP |
936 | } |
937 | ||
bc37ae52 JG |
938 | g_string_append(context->string, "};\n\n"); |
939 | } | |
940 | ||
941 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) | |
942 | { | |
943 | char *metadata = NULL; | |
944 | struct metadata_context *context = NULL; | |
945 | int err = 0; | |
946 | size_t i; | |
947 | ||
948 | if (!trace) { | |
949 | goto end; | |
950 | } | |
951 | ||
952 | context = g_new0(struct metadata_context, 1); | |
953 | if (!context) { | |
954 | goto end; | |
955 | } | |
956 | ||
957 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); | |
958 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); | |
959 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); | |
960 | if (append_trace_metadata(trace, context)) { | |
961 | goto error; | |
962 | } | |
963 | append_env_metadata(trace, context); | |
964 | g_ptr_array_foreach(trace->clocks, | |
ac0c6bdd | 965 | (GFunc)bt_ctf_clock_class_serialize, context); |
bc37ae52 JG |
966 | |
967 | for (i = 0; i < trace->stream_classes->len; i++) { | |
968 | err = bt_ctf_stream_class_serialize( | |
969 | trace->stream_classes->pdata[i], context); | |
970 | if (err) { | |
971 | goto error; | |
972 | } | |
973 | } | |
974 | ||
975 | metadata = context->string->str; | |
976 | error: | |
977 | g_string_free(context->string, err ? TRUE : FALSE); | |
978 | g_string_free(context->field_name, TRUE); | |
979 | g_free(context); | |
980 | end: | |
981 | return metadata; | |
982 | } | |
983 | ||
4ed90fb3 JG |
984 | enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace) |
985 | { | |
986 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; | |
987 | ||
988 | if (!trace) { | |
989 | goto end; | |
990 | } | |
991 | ||
dc3fffef PP |
992 | ret = trace->native_byte_order; |
993 | ||
4ed90fb3 JG |
994 | end: |
995 | return ret; | |
996 | } | |
997 | ||
bc37ae52 JG |
998 | int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, |
999 | enum bt_ctf_byte_order byte_order) | |
1000 | { | |
1001 | int ret = 0; | |
bc37ae52 JG |
1002 | |
1003 | if (!trace || trace->frozen) { | |
1004 | ret = -1; | |
1005 | goto end; | |
1006 | } | |
1007 | ||
dc3fffef PP |
1008 | if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN && |
1009 | byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN && | |
1010 | byte_order != BT_CTF_BYTE_ORDER_NETWORK) { | |
bc37ae52 JG |
1011 | ret = -1; |
1012 | goto end; | |
1013 | } | |
1014 | ||
dc3fffef PP |
1015 | trace->native_byte_order = byte_order; |
1016 | ||
bc37ae52 JG |
1017 | end: |
1018 | return ret; | |
1019 | } | |
1020 | ||
d246b111 JG |
1021 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( |
1022 | struct bt_ctf_trace *trace) | |
1023 | { | |
1024 | struct bt_ctf_field_type *field_type = NULL; | |
1025 | ||
1026 | if (!trace) { | |
1027 | goto end; | |
1028 | } | |
1029 | ||
83509119 | 1030 | bt_get(trace->packet_header_type); |
d246b111 JG |
1031 | field_type = trace->packet_header_type; |
1032 | end: | |
1033 | return field_type; | |
1034 | } | |
1035 | ||
1036 | int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, | |
1037 | struct bt_ctf_field_type *packet_header_type) | |
1038 | { | |
1039 | int ret = 0; | |
1040 | ||
835b2d10 | 1041 | if (!trace || trace->frozen) { |
d246b111 JG |
1042 | ret = -1; |
1043 | goto end; | |
1044 | } | |
1045 | ||
835b2d10 JG |
1046 | /* packet_header_type must be a structure. */ |
1047 | if (packet_header_type && | |
1048 | bt_ctf_field_type_get_type_id(packet_header_type) != | |
1049 | BT_CTF_TYPE_ID_STRUCT) { | |
d246b111 JG |
1050 | ret = -1; |
1051 | goto end; | |
1052 | } | |
1053 | ||
83509119 | 1054 | bt_put(trace->packet_header_type); |
835b2d10 | 1055 | trace->packet_header_type = bt_get(packet_header_type); |
d246b111 JG |
1056 | end: |
1057 | return ret; | |
1058 | } | |
1059 | ||
8bf65fbd JG |
1060 | static |
1061 | int get_stream_class_count(void *element) | |
1062 | { | |
1063 | return bt_ctf_trace_get_stream_class_count( | |
1064 | (struct bt_ctf_trace *) element); | |
1065 | } | |
1066 | ||
1067 | static | |
1068 | void *get_stream_class(void *element, int i) | |
1069 | { | |
1070 | return bt_ctf_trace_get_stream_class( | |
1071 | (struct bt_ctf_trace *) element, i); | |
1072 | } | |
1073 | ||
1074 | static | |
d9a13d86 | 1075 | int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data) |
8bf65fbd | 1076 | { |
d9a13d86 | 1077 | return bt_ctf_stream_class_visit(object, visitor, data); |
8bf65fbd JG |
1078 | } |
1079 | ||
1080 | int bt_ctf_trace_visit(struct bt_ctf_trace *trace, | |
d9a13d86 | 1081 | bt_ctf_visitor visitor, void *data) |
8bf65fbd JG |
1082 | { |
1083 | int ret; | |
d9a13d86 PP |
1084 | struct bt_ctf_object obj = |
1085 | { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE }; | |
8bf65fbd JG |
1086 | |
1087 | if (!trace || !visitor) { | |
1088 | ret = -1; | |
1089 | goto end; | |
1090 | } | |
1091 | ||
d9a13d86 | 1092 | ret = visitor_helper(&obj, get_stream_class_count, |
8bf65fbd JG |
1093 | get_stream_class, visit_stream_class, visitor, data); |
1094 | end: | |
1095 | return ret; | |
1096 | } | |
1097 | ||
1098 | static | |
d9a13d86 | 1099 | int invoke_listener(struct bt_ctf_object *object, void *data) |
8bf65fbd | 1100 | { |
9b888ff3 | 1101 | struct listener_wrapper *listener_wrapper = data; |
8bf65fbd | 1102 | |
d9a13d86 | 1103 | listener_wrapper->listener(object, listener_wrapper->data); |
9b888ff3 | 1104 | return 0; |
8bf65fbd JG |
1105 | } |
1106 | ||
50ad4244 JG |
1107 | int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace, |
1108 | bt_ctf_listener_cb listener, void *listener_data) | |
2204c381 JG |
1109 | { |
1110 | int ret = 0; | |
50ad4244 JG |
1111 | struct listener_wrapper *listener_wrapper = |
1112 | g_new0(struct listener_wrapper, 1); | |
2204c381 | 1113 | |
50ad4244 | 1114 | if (!trace || !listener || !listener_wrapper) { |
2204c381 JG |
1115 | ret = -1; |
1116 | goto error; | |
1117 | } | |
1118 | ||
50ad4244 JG |
1119 | listener_wrapper->listener = listener; |
1120 | listener_wrapper->data = listener_data; | |
8bf65fbd | 1121 | |
50ad4244 | 1122 | /* Visit the current schema. */ |
9b888ff3 | 1123 | ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper); |
8bf65fbd JG |
1124 | if (ret) { |
1125 | goto error; | |
1126 | } | |
1127 | ||
50ad4244 JG |
1128 | /* |
1129 | * Add listener to the array of callbacks which will be invoked on | |
1130 | * schema changes. | |
1131 | */ | |
1132 | g_ptr_array_add(trace->listeners, listener_wrapper); | |
2204c381 JG |
1133 | return ret; |
1134 | error: | |
50ad4244 | 1135 | g_free(listener_wrapper); |
2204c381 JG |
1136 | return ret; |
1137 | } | |
1138 | ||
bc37ae52 | 1139 | BT_HIDDEN |
d9a13d86 | 1140 | int bt_ctf_trace_object_modification(struct bt_ctf_object *object, |
9b888ff3 JG |
1141 | void *trace_ptr) |
1142 | { | |
1143 | size_t i; | |
1144 | struct bt_ctf_trace *trace = trace_ptr; | |
1145 | ||
1146 | assert(trace); | |
d9a13d86 | 1147 | assert(object); |
9b888ff3 JG |
1148 | |
1149 | if (trace->listeners->len == 0) { | |
1150 | goto end; | |
1151 | } | |
1152 | ||
1153 | for (i = 0; i < trace->listeners->len; i++) { | |
1154 | struct listener_wrapper *listener = | |
1155 | g_ptr_array_index(trace->listeners, i); | |
1156 | ||
d9a13d86 | 1157 | listener->listener(object, listener->data); |
9b888ff3 JG |
1158 | } |
1159 | end: | |
1160 | return 0; | |
1161 | } | |
1162 | ||
1163 | BT_HIDDEN | |
bc37ae52 JG |
1164 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) |
1165 | { | |
a25e0d14 | 1166 | int ret; |
bc37ae52 | 1167 | unsigned int alignment, size; |
8bdcb82e | 1168 | struct bt_ctf_field_type *field_type = NULL; |
bc37ae52 JG |
1169 | |
1170 | if (alias >= NR_FIELD_TYPE_ALIAS) { | |
8bdcb82e | 1171 | goto end; |
bc37ae52 JG |
1172 | } |
1173 | ||
1174 | alignment = field_type_aliases_alignments[alias]; | |
1175 | size = field_type_aliases_sizes[alias]; | |
1176 | field_type = bt_ctf_field_type_integer_create(size); | |
a25e0d14 JG |
1177 | ret = bt_ctf_field_type_set_alignment(field_type, alignment); |
1178 | if (ret) { | |
1179 | BT_PUT(field_type); | |
1180 | } | |
8bdcb82e | 1181 | end: |
bc37ae52 JG |
1182 | return field_type; |
1183 | } | |
1184 | ||
f67f3a6e | 1185 | static |
09840de5 | 1186 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace) |
f67f3a6e | 1187 | { |
6474e016 PP |
1188 | int i; |
1189 | ||
09840de5 | 1190 | bt_ctf_field_type_freeze(trace->packet_header_type); |
f67f3a6e | 1191 | bt_ctf_attributes_freeze(trace->environment); |
6474e016 PP |
1192 | |
1193 | for (i = 0; i < trace->clocks->len; i++) { | |
ac0c6bdd | 1194 | struct bt_ctf_clock_class *clock_class = |
6474e016 PP |
1195 | g_ptr_array_index(trace->clocks, i); |
1196 | ||
ac0c6bdd | 1197 | bt_ctf_clock_class_freeze(clock_class); |
6474e016 PP |
1198 | } |
1199 | ||
f67f3a6e JG |
1200 | trace->frozen = 1; |
1201 | } | |
1202 | ||
bc37ae52 JG |
1203 | static |
1204 | int init_trace_packet_header(struct bt_ctf_trace *trace) | |
1205 | { | |
bc37ae52 | 1206 | int ret = 0; |
d246b111 | 1207 | struct bt_ctf_field *magic = NULL, *uuid_array = NULL; |
bc37ae52 JG |
1208 | struct bt_ctf_field_type *_uint32_t = |
1209 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
1210 | struct bt_ctf_field_type *_uint8_t = | |
1211 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); | |
1212 | struct bt_ctf_field_type *trace_packet_header_type = | |
1213 | bt_ctf_field_type_structure_create(); | |
1214 | struct bt_ctf_field_type *uuid_array_type = | |
1215 | bt_ctf_field_type_array_create(_uint8_t, 16); | |
1216 | ||
1217 | if (!trace_packet_header_type || !uuid_array_type) { | |
1218 | ret = -1; | |
1219 | goto end; | |
1220 | } | |
1221 | ||
bc37ae52 JG |
1222 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
1223 | _uint32_t, "magic"); | |
1224 | if (ret) { | |
1225 | goto end; | |
1226 | } | |
1227 | ||
1228 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
1229 | uuid_array_type, "uuid"); | |
1230 | if (ret) { | |
1231 | goto end; | |
1232 | } | |
1233 | ||
1234 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
1235 | _uint32_t, "stream_id"); | |
1236 | if (ret) { | |
1237 | goto end; | |
1238 | } | |
1239 | ||
662e778c JG |
1240 | ret = bt_ctf_trace_set_packet_header_type(trace, |
1241 | trace_packet_header_type); | |
1242 | if (ret) { | |
1243 | goto end; | |
1244 | } | |
bc37ae52 | 1245 | end: |
83509119 JG |
1246 | bt_put(uuid_array_type); |
1247 | bt_put(_uint32_t); | |
1248 | bt_put(_uint8_t); | |
1249 | bt_put(magic); | |
1250 | bt_put(uuid_array); | |
1251 | bt_put(trace_packet_header_type); | |
bc37ae52 JG |
1252 | return ret; |
1253 | } |