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 | ||
e011d2c1 PP |
29 | #define BT_LOG_TAG "TRACE" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
bc37ae52 | 32 | #include <babeltrace/ctf-ir/trace-internal.h> |
ac0c6bdd | 33 | #include <babeltrace/ctf-ir/clock-class-internal.h> |
bc37ae52 JG |
34 | #include <babeltrace/ctf-ir/stream-internal.h> |
35 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
09840de5 | 36 | #include <babeltrace/ctf-ir/event-internal.h> |
272df73e PP |
37 | #include <babeltrace/ctf-ir/event-class.h> |
38 | #include <babeltrace/ctf-ir/event-class-internal.h> | |
bc37ae52 | 39 | #include <babeltrace/ctf-writer/functor-internal.h> |
ac0c6bdd | 40 | #include <babeltrace/ctf-writer/clock-internal.h> |
2e33ac5a | 41 | #include <babeltrace/ctf-ir/field-types-internal.h> |
44e0a4f5 | 42 | #include <babeltrace/ctf-ir/attributes-internal.h> |
09840de5 | 43 | #include <babeltrace/ctf-ir/validation-internal.h> |
8bf65fbd | 44 | #include <babeltrace/ctf-ir/visitor-internal.h> |
654c1444 | 45 | #include <babeltrace/ctf-ir/utils.h> |
3d9990ac | 46 | #include <babeltrace/compiler-internal.h> |
dac5c838 | 47 | #include <babeltrace/values.h> |
83509119 | 48 | #include <babeltrace/ref.h> |
c55a9f58 | 49 | #include <babeltrace/types.h> |
3d9990ac | 50 | #include <babeltrace/endian-internal.h> |
dc3fffef | 51 | #include <inttypes.h> |
544d0515 | 52 | #include <stdint.h> |
4a32fda0 | 53 | #include <string.h> |
bc37ae52 JG |
54 | |
55 | #define DEFAULT_IDENTIFIER_SIZE 128 | |
56 | #define DEFAULT_METADATA_STRING_SIZE 4096 | |
57 | ||
50ad4244 JG |
58 | struct listener_wrapper { |
59 | bt_ctf_listener_cb listener; | |
2204c381 JG |
60 | void *data; |
61 | }; | |
62 | ||
bc37ae52 | 63 | static |
83509119 | 64 | void bt_ctf_trace_destroy(struct bt_object *obj); |
bc37ae52 | 65 | static |
09840de5 | 66 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace); |
bc37ae52 | 67 | |
bc37ae52 JG |
68 | static |
69 | const unsigned int field_type_aliases_alignments[] = { | |
70 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, | |
71 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, | |
72 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, | |
73 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, | |
74 | }; | |
75 | ||
76 | static | |
77 | const unsigned int field_type_aliases_sizes[] = { | |
78 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, | |
79 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, | |
80 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, | |
81 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, | |
82 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, | |
83 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, | |
84 | }; | |
85 | ||
bc37ae52 JG |
86 | struct bt_ctf_trace *bt_ctf_trace_create(void) |
87 | { | |
88 | struct bt_ctf_trace *trace = NULL; | |
488e09a7 | 89 | struct bt_ctf_field_type *packet_header_type = NULL; |
bc37ae52 JG |
90 | |
91 | trace = g_new0(struct bt_ctf_trace, 1); | |
92 | if (!trace) { | |
93 | goto error; | |
94 | } | |
95 | ||
391c8f0d | 96 | trace->native_byte_order = BT_CTF_BYTE_ORDER_NATIVE; |
83509119 | 97 | bt_object_init(trace, bt_ctf_trace_destroy); |
bc37ae52 | 98 | trace->clocks = g_ptr_array_new_with_free_func( |
83509119 | 99 | (GDestroyNotify) bt_put); |
bc37ae52 | 100 | trace->streams = g_ptr_array_new_with_free_func( |
e6a8e8e4 | 101 | (GDestroyNotify) bt_object_release); |
bc37ae52 | 102 | trace->stream_classes = g_ptr_array_new_with_free_func( |
e6a8e8e4 | 103 | (GDestroyNotify) bt_object_release); |
7f800dc7 | 104 | if (!trace->clocks || !trace->stream_classes || !trace->streams) { |
83509119 | 105 | goto error; |
bc37ae52 JG |
106 | } |
107 | ||
488e09a7 PP |
108 | packet_header_type = bt_ctf_field_type_structure_create(); |
109 | if (!packet_header_type) { | |
83509119 | 110 | goto error; |
bc37ae52 JG |
111 | } |
112 | ||
488e09a7 PP |
113 | BT_MOVE(trace->packet_header_type, packet_header_type); |
114 | ||
7f800dc7 PP |
115 | /* Create the environment array object */ |
116 | trace->environment = bt_ctf_attributes_create(); | |
117 | if (!trace->environment) { | |
83509119 | 118 | goto error; |
7f800dc7 PP |
119 | } |
120 | ||
9b888ff3 JG |
121 | trace->listeners = g_ptr_array_new_with_free_func( |
122 | (GDestroyNotify) g_free); | |
123 | if (!trace->listeners) { | |
124 | goto error; | |
125 | } | |
126 | ||
bc37ae52 JG |
127 | return trace; |
128 | ||
bc37ae52 | 129 | error: |
83509119 | 130 | BT_PUT(trace); |
488e09a7 | 131 | bt_put(packet_header_type); |
bc37ae52 JG |
132 | return trace; |
133 | } | |
134 | ||
e96045d4 JG |
135 | const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace) |
136 | { | |
137 | const char *name = NULL; | |
138 | ||
139 | if (!trace || !trace->name) { | |
140 | goto end; | |
141 | } | |
142 | ||
143 | name = trace->name->str; | |
144 | end: | |
145 | return name; | |
146 | } | |
147 | ||
148 | int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name) | |
149 | { | |
150 | int ret = 0; | |
151 | ||
152 | if (!trace || !name || trace->frozen) { | |
153 | ret = -1; | |
154 | goto end; | |
155 | } | |
156 | ||
157 | trace->name = trace->name ? g_string_assign(trace->name, name) : | |
158 | g_string_new(name); | |
159 | if (!trace->name) { | |
160 | ret = -1; | |
161 | goto end; | |
162 | } | |
163 | end: | |
164 | return ret; | |
165 | } | |
166 | ||
4a32fda0 PP |
167 | const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace) |
168 | { | |
169 | return trace && trace->uuid_set ? trace->uuid : NULL; | |
170 | } | |
171 | ||
172 | int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace, const unsigned char *uuid) | |
173 | { | |
174 | int ret = 0; | |
175 | ||
176 | if (!trace || !uuid || trace->frozen) { | |
177 | ret = -1; | |
178 | goto end; | |
179 | } | |
180 | ||
181 | memcpy(trace->uuid, uuid, sizeof(uuid_t)); | |
c55a9f58 | 182 | trace->uuid_set = BT_TRUE; |
4a32fda0 PP |
183 | |
184 | end: | |
185 | return ret; | |
186 | } | |
187 | ||
83509119 | 188 | void bt_ctf_trace_destroy(struct bt_object *obj) |
bc37ae52 JG |
189 | { |
190 | struct bt_ctf_trace *trace; | |
bc37ae52 | 191 | |
83509119 | 192 | trace = container_of(obj, struct bt_ctf_trace, base); |
bc37ae52 | 193 | if (trace->environment) { |
7f800dc7 | 194 | bt_ctf_attributes_destroy(trace->environment); |
bc37ae52 JG |
195 | } |
196 | ||
e96045d4 JG |
197 | if (trace->name) { |
198 | g_string_free(trace->name, TRUE); | |
199 | } | |
200 | ||
bc37ae52 JG |
201 | if (trace->clocks) { |
202 | g_ptr_array_free(trace->clocks, TRUE); | |
203 | } | |
204 | ||
205 | if (trace->streams) { | |
206 | g_ptr_array_free(trace->streams, TRUE); | |
207 | } | |
208 | ||
209 | if (trace->stream_classes) { | |
210 | g_ptr_array_free(trace->stream_classes, TRUE); | |
211 | } | |
212 | ||
9b888ff3 JG |
213 | if (trace->listeners) { |
214 | g_ptr_array_free(trace->listeners, TRUE); | |
215 | } | |
216 | ||
83509119 | 217 | bt_put(trace->packet_header_type); |
bc37ae52 JG |
218 | g_free(trace); |
219 | } | |
220 | ||
7f800dc7 | 221 | int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, |
dac5c838 | 222 | const char *name, struct bt_value *value) |
bc37ae52 | 223 | { |
bc37ae52 JG |
224 | int ret = 0; |
225 | ||
a0d12916 | 226 | if (!trace || !name || !value || |
7f800dc7 | 227 | bt_ctf_validate_identifier(name) || |
dac5c838 | 228 | !(bt_value_is_integer(value) || bt_value_is_string(value))) { |
bc37ae52 | 229 | ret = -1; |
7f800dc7 | 230 | goto end; |
bc37ae52 JG |
231 | } |
232 | ||
233 | if (strchr(name, ' ')) { | |
234 | ret = -1; | |
7f800dc7 | 235 | goto end; |
bc37ae52 JG |
236 | } |
237 | ||
a0d12916 JG |
238 | if (trace->frozen) { |
239 | /* | |
240 | * New environment fields may be added to a frozen trace, | |
241 | * but existing fields may not be changed. | |
242 | * | |
243 | * The object passed is frozen like all other attributes. | |
244 | */ | |
dac5c838 | 245 | struct bt_value *attribute = |
a0d12916 JG |
246 | bt_ctf_attributes_get_field_value_by_name( |
247 | trace->environment, name); | |
248 | ||
249 | if (attribute) { | |
83509119 | 250 | BT_PUT(attribute); |
a0d12916 JG |
251 | ret = -1; |
252 | goto end; | |
253 | } | |
254 | ||
dac5c838 | 255 | bt_value_freeze(value); |
a0d12916 JG |
256 | } |
257 | ||
7f800dc7 PP |
258 | ret = bt_ctf_attributes_set_field_value(trace->environment, name, |
259 | value); | |
bc37ae52 | 260 | |
7f800dc7 PP |
261 | end: |
262 | return ret; | |
263 | } | |
bc37ae52 | 264 | |
7f800dc7 PP |
265 | int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, |
266 | const char *name, const char *value) | |
267 | { | |
268 | int ret = 0; | |
dac5c838 | 269 | struct bt_value *env_value_string_obj = NULL; |
7f800dc7 PP |
270 | |
271 | if (!trace || !name || !value) { | |
bc37ae52 | 272 | ret = -1; |
7f800dc7 | 273 | goto end; |
bc37ae52 JG |
274 | } |
275 | ||
a0d12916 JG |
276 | if (trace->frozen) { |
277 | /* | |
278 | * New environment fields may be added to a frozen trace, | |
279 | * but existing fields may not be changed. | |
280 | */ | |
dac5c838 | 281 | struct bt_value *attribute = |
a0d12916 JG |
282 | bt_ctf_attributes_get_field_value_by_name( |
283 | trace->environment, name); | |
284 | ||
285 | if (attribute) { | |
83509119 | 286 | BT_PUT(attribute); |
a0d12916 JG |
287 | ret = -1; |
288 | goto end; | |
289 | } | |
290 | } | |
291 | ||
dac5c838 | 292 | env_value_string_obj = bt_value_string_create_init(value); |
bc37ae52 | 293 | |
7f800dc7 PP |
294 | if (!env_value_string_obj) { |
295 | ret = -1; | |
296 | goto end; | |
bc37ae52 JG |
297 | } |
298 | ||
a0d12916 | 299 | if (trace->frozen) { |
dac5c838 | 300 | bt_value_freeze(env_value_string_obj); |
a0d12916 | 301 | } |
7f800dc7 PP |
302 | ret = bt_ctf_trace_set_environment_field(trace, name, |
303 | env_value_string_obj); | |
304 | ||
305 | end: | |
83509119 | 306 | BT_PUT(env_value_string_obj); |
3487c9f3 JG |
307 | return ret; |
308 | } | |
309 | ||
7f800dc7 PP |
310 | int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace, |
311 | const char *name, int64_t value) | |
3487c9f3 | 312 | { |
3487c9f3 | 313 | int ret = 0; |
dac5c838 | 314 | struct bt_value *env_value_integer_obj = NULL; |
3487c9f3 JG |
315 | |
316 | if (!trace || !name) { | |
317 | ret = -1; | |
7f800dc7 | 318 | goto end; |
3487c9f3 JG |
319 | } |
320 | ||
a0d12916 JG |
321 | if (trace->frozen) { |
322 | /* | |
323 | * New environment fields may be added to a frozen trace, | |
324 | * but existing fields may not be changed. | |
325 | */ | |
dac5c838 | 326 | struct bt_value *attribute = |
a0d12916 JG |
327 | bt_ctf_attributes_get_field_value_by_name( |
328 | trace->environment, name); | |
329 | ||
330 | if (attribute) { | |
83509119 | 331 | BT_PUT(attribute); |
a0d12916 JG |
332 | ret = -1; |
333 | goto end; | |
334 | } | |
335 | } | |
336 | ||
dac5c838 | 337 | env_value_integer_obj = bt_value_integer_create_init(value); |
7f800dc7 | 338 | if (!env_value_integer_obj) { |
3487c9f3 | 339 | ret = -1; |
7f800dc7 | 340 | goto end; |
3487c9f3 JG |
341 | } |
342 | ||
7f800dc7 PP |
343 | ret = bt_ctf_trace_set_environment_field(trace, name, |
344 | env_value_integer_obj); | |
a0d12916 | 345 | if (trace->frozen) { |
dac5c838 | 346 | bt_value_freeze(env_value_integer_obj); |
a0d12916 | 347 | } |
7f800dc7 | 348 | end: |
83509119 | 349 | BT_PUT(env_value_integer_obj); |
bc37ae52 JG |
350 | return ret; |
351 | } | |
352 | ||
544d0515 | 353 | int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
e6fa2160 | 354 | { |
544d0515 | 355 | int64_t ret = 0; |
e6fa2160 JG |
356 | |
357 | if (!trace) { | |
9ac68eb1 | 358 | ret = (int64_t) -1; |
e6fa2160 JG |
359 | goto end; |
360 | } | |
361 | ||
7f800dc7 | 362 | ret = bt_ctf_attributes_get_count(trace->environment); |
e6fa2160 | 363 | |
e6fa2160 | 364 | end: |
7f800dc7 | 365 | return ret; |
e6fa2160 JG |
366 | } |
367 | ||
368 | const char * | |
9ac68eb1 PP |
369 | bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace, |
370 | uint64_t index) | |
e6fa2160 | 371 | { |
e6fa2160 JG |
372 | const char *ret = NULL; |
373 | ||
7f800dc7 | 374 | if (!trace) { |
e6fa2160 JG |
375 | goto end; |
376 | } | |
377 | ||
7f800dc7 PP |
378 | ret = bt_ctf_attributes_get_field_name(trace->environment, index); |
379 | ||
e6fa2160 JG |
380 | end: |
381 | return ret; | |
382 | } | |
383 | ||
9ac68eb1 PP |
384 | struct bt_value *bt_ctf_trace_get_environment_field_value_by_index( |
385 | struct bt_ctf_trace *trace, uint64_t index) | |
e6fa2160 | 386 | { |
dac5c838 | 387 | struct bt_value *ret = NULL; |
e6fa2160 | 388 | |
7f800dc7 | 389 | if (!trace) { |
e6fa2160 JG |
390 | goto end; |
391 | } | |
392 | ||
7f800dc7 PP |
393 | ret = bt_ctf_attributes_get_field_value(trace->environment, index); |
394 | ||
e6fa2160 JG |
395 | end: |
396 | return ret; | |
397 | } | |
398 | ||
dac5c838 | 399 | struct bt_value *bt_ctf_trace_get_environment_field_value_by_name( |
7f800dc7 | 400 | struct bt_ctf_trace *trace, const char *name) |
e6fa2160 | 401 | { |
dac5c838 | 402 | struct bt_value *ret = NULL; |
e6fa2160 | 403 | |
7f800dc7 | 404 | if (!trace || !name) { |
e6fa2160 JG |
405 | goto end; |
406 | } | |
407 | ||
7f800dc7 PP |
408 | ret = bt_ctf_attributes_get_field_value_by_name(trace->environment, |
409 | name); | |
410 | ||
e6fa2160 JG |
411 | end: |
412 | return ret; | |
413 | } | |
414 | ||
ac0c6bdd PP |
415 | int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace, |
416 | struct bt_ctf_clock_class *clock_class) | |
bc37ae52 JG |
417 | { |
418 | int ret = 0; | |
bc37ae52 | 419 | |
5acf2ae6 PP |
420 | if (!trace || trace->is_static || |
421 | !bt_ctf_clock_class_is_valid(clock_class)) { | |
bc37ae52 JG |
422 | ret = -1; |
423 | goto end; | |
424 | } | |
425 | ||
ac0c6bdd | 426 | /* Check for duplicate clock classes */ |
c9d90a34 | 427 | if (bt_ctf_trace_has_clock_class(trace, clock_class)) { |
bc37ae52 JG |
428 | ret = -1; |
429 | goto end; | |
430 | } | |
431 | ||
ac0c6bdd PP |
432 | bt_get(clock_class); |
433 | g_ptr_array_add(trace->clocks, clock_class); | |
cfeb617e | 434 | |
6474e016 | 435 | if (trace->frozen) { |
ac0c6bdd | 436 | bt_ctf_clock_class_freeze(clock_class); |
6474e016 | 437 | } |
bc37ae52 JG |
438 | end: |
439 | return ret; | |
440 | } | |
441 | ||
544d0515 | 442 | int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace) |
884cd6c3 | 443 | { |
9ac68eb1 | 444 | int64_t ret = (int64_t) -1; |
884cd6c3 JG |
445 | |
446 | if (!trace) { | |
447 | goto end; | |
448 | } | |
449 | ||
450 | ret = trace->clocks->len; | |
451 | end: | |
452 | return ret; | |
453 | } | |
454 | ||
9ac68eb1 PP |
455 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index( |
456 | struct bt_ctf_trace *trace, uint64_t index) | |
884cd6c3 | 457 | { |
ac0c6bdd | 458 | struct bt_ctf_clock_class *clock_class = NULL; |
884cd6c3 | 459 | |
9ac68eb1 | 460 | if (!trace || index >= trace->clocks->len) { |
884cd6c3 JG |
461 | goto end; |
462 | } | |
463 | ||
ac0c6bdd PP |
464 | clock_class = g_ptr_array_index(trace->clocks, index); |
465 | bt_get(clock_class); | |
884cd6c3 | 466 | end: |
ac0c6bdd | 467 | return clock_class; |
884cd6c3 JG |
468 | } |
469 | ||
e011d2c1 PP |
470 | static |
471 | bool packet_header_field_type_is_valid(struct bt_ctf_trace *trace, | |
472 | struct bt_ctf_field_type *packet_header_type) | |
473 | { | |
474 | int ret; | |
475 | bool is_valid = true; | |
476 | struct bt_ctf_field_type *field_type = NULL; | |
477 | ||
478 | if (!packet_header_type) { | |
479 | /* | |
480 | * No packet header field type: trace must have only | |
481 | * one stream. At this point the stream class being | |
482 | * added is not part of the trace yet, so we validate | |
483 | * that the trace contains no stream classes yet. | |
484 | */ | |
485 | if (trace->stream_classes->len >= 1) { | |
486 | BT_LOGW_STR("Invalid packet header field type: " | |
487 | "packet header field type does not exist but there's more than one stream class in the trace."); | |
488 | goto invalid; | |
489 | } | |
490 | ||
491 | /* No packet header field type: valid at this point */ | |
492 | goto end; | |
493 | } | |
494 | ||
495 | /* Packet header field type, if it exists, must be a structure */ | |
496 | if (!bt_ctf_field_type_is_structure(packet_header_type)) { | |
497 | BT_LOGW("Invalid packet header field type: must be a structure field type if it exists: " | |
498 | "ft-addr=%p, ft-id=%s", | |
499 | packet_header_type, | |
500 | bt_ctf_field_type_id_string(packet_header_type->id)); | |
501 | goto invalid; | |
502 | } | |
503 | ||
504 | /* | |
505 | * If there's a `magic` field, it must be a 32-bit unsigned | |
506 | * integer field type. Also it must be the first field of the | |
507 | * packet header field type. | |
508 | */ | |
509 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
510 | packet_header_type, "magic"); | |
511 | if (field_type) { | |
512 | const char *field_name; | |
513 | ||
514 | if (!bt_ctf_field_type_is_integer(field_type)) { | |
515 | BT_LOGW("Invalid packet header field type: `magic` field must be an integer field type: " | |
516 | "magic-ft-addr=%p, magic-ft-id=%s", | |
517 | field_type, | |
518 | bt_ctf_field_type_id_string(field_type->id)); | |
519 | goto invalid; | |
520 | } | |
521 | ||
522 | if (bt_ctf_field_type_integer_is_signed(field_type)) { | |
523 | BT_LOGW("Invalid packet header field type: `magic` field must be an unsigned integer field type: " | |
524 | "magic-ft-addr=%p", field_type); | |
525 | goto invalid; | |
526 | } | |
527 | ||
528 | if (bt_ctf_field_type_integer_get_size(field_type) != 32) { | |
529 | BT_LOGW("Invalid packet header field type: `magic` field must be a 32-bit unsigned integer field type: " | |
530 | "magic-ft-addr=%p, magic-ft-size=%u", | |
531 | field_type, | |
532 | bt_ctf_field_type_integer_get_size(field_type)); | |
533 | goto invalid; | |
534 | } | |
535 | ||
536 | ret = bt_ctf_field_type_structure_get_field_by_index( | |
537 | packet_header_type, &field_name, NULL, 0); | |
538 | assert(ret == 0); | |
539 | ||
540 | if (strcmp(field_name, "magic") != 0) { | |
541 | BT_LOGW("Invalid packet header field type: `magic` field must be the first field: " | |
542 | "magic-ft-addr=%p, first-field-name=\"%s\"", | |
543 | field_type, field_name); | |
544 | goto invalid; | |
545 | } | |
546 | ||
547 | BT_PUT(field_type); | |
548 | } | |
549 | ||
550 | /* | |
551 | * If there's a `uuid` field, it must be an array field type of | |
552 | * length 16 with an 8-bit unsigned integer element field type. | |
553 | */ | |
554 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
555 | packet_header_type, "uuid"); | |
556 | if (field_type) { | |
557 | struct bt_ctf_field_type *elem_ft; | |
558 | ||
559 | if (!bt_ctf_field_type_is_array(field_type)) { | |
560 | BT_LOGW("Invalid packet header field type: `uuid` field must be an array field type: " | |
561 | "uuid-ft-addr=%p, uuid-ft-id=%s", | |
562 | field_type, | |
563 | bt_ctf_field_type_id_string(field_type->id)); | |
564 | goto invalid; | |
565 | } | |
566 | ||
567 | if (bt_ctf_field_type_array_get_length(field_type) != 16) { | |
568 | BT_LOGW("Invalid packet header field type: `uuid` array field type's length must be 16: " | |
569 | "uuid-ft-addr=%p, uuid-ft-length=%" PRId64, | |
570 | field_type, | |
571 | bt_ctf_field_type_array_get_length(field_type)); | |
572 | goto invalid; | |
573 | } | |
574 | ||
575 | elem_ft = bt_ctf_field_type_array_get_element_type(field_type); | |
576 | assert(elem_ft); | |
577 | ||
578 | if (!bt_ctf_field_type_is_integer(elem_ft)) { | |
579 | BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an integer field type: " | |
580 | "elem-ft-addr=%p, elem-ft-id=%s", | |
581 | elem_ft, | |
582 | bt_ctf_field_type_id_string(elem_ft->id)); | |
583 | bt_put(elem_ft); | |
584 | goto invalid; | |
585 | } | |
586 | ||
587 | if (bt_ctf_field_type_integer_is_signed(elem_ft)) { | |
588 | BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an unsigned integer field type: " | |
589 | "elem-ft-addr=%p", elem_ft); | |
590 | bt_put(elem_ft); | |
591 | goto invalid; | |
592 | } | |
593 | ||
594 | if (bt_ctf_field_type_integer_get_size(elem_ft) != 8) { | |
595 | BT_LOGW("Invalid packet header field type: `uuid` field's element field type must be an 8-bit unsigned integer field type: " | |
596 | "elem-ft-addr=%p, elem-ft-size=%u", | |
597 | elem_ft, | |
598 | bt_ctf_field_type_integer_get_size(elem_ft)); | |
599 | bt_put(elem_ft); | |
600 | goto invalid; | |
601 | } | |
602 | ||
603 | bt_put(elem_ft); | |
604 | BT_PUT(field_type); | |
605 | } | |
606 | ||
607 | /* | |
608 | * The `stream_id` field must exist if there's more than one | |
609 | * stream classes in the trace. | |
610 | */ | |
611 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
612 | packet_header_type, "stream_id"); | |
613 | ||
614 | if (!field_type && trace->stream_classes->len >= 1) { | |
615 | BT_LOGW_STR("Invalid packet header field type: " | |
616 | "`stream_id` field does not exist but there's more than one stream class in the trace."); | |
617 | goto invalid; | |
618 | } | |
619 | ||
620 | /* | |
621 | * If there's a `stream_id` field, it must be an unsigned | |
622 | * integer field type. | |
623 | */ | |
624 | if (field_type) { | |
625 | if (!bt_ctf_field_type_is_integer(field_type)) { | |
626 | BT_LOGW("Invalid packet header field type: `stream_id` field must be an integer field type: " | |
627 | "stream-id-ft-addr=%p, stream-id-ft-id=%s", | |
628 | field_type, | |
629 | bt_ctf_field_type_id_string(field_type->id)); | |
630 | goto invalid; | |
631 | } | |
632 | ||
633 | if (bt_ctf_field_type_integer_is_signed(field_type)) { | |
634 | BT_LOGW("Invalid packet header field type: `stream_id` field must be an unsigned integer field type: " | |
635 | "stream-id-ft-addr=%p", field_type); | |
636 | goto invalid; | |
637 | } | |
638 | ||
639 | BT_PUT(field_type); | |
640 | } | |
641 | ||
642 | goto end; | |
643 | ||
644 | invalid: | |
645 | is_valid = false; | |
646 | ||
647 | end: | |
648 | bt_put(field_type); | |
649 | return is_valid; | |
650 | } | |
651 | ||
652 | static | |
653 | bool packet_context_field_type_is_valid(struct bt_ctf_trace *trace, | |
654 | struct bt_ctf_stream_class *stream_class, | |
655 | struct bt_ctf_field_type *packet_context_type) | |
656 | { | |
657 | bool is_valid = true; | |
658 | struct bt_ctf_field_type *field_type = NULL; | |
659 | ||
660 | if (!packet_context_type) { | |
661 | /* No packet context field type: valid at this point */ | |
662 | goto end; | |
663 | } | |
664 | ||
665 | /* Packet context field type, if it exists, must be a structure */ | |
666 | if (!bt_ctf_field_type_is_structure(packet_context_type)) { | |
667 | BT_LOGW("Invalid packet context field type: must be a structure field type if it exists: " | |
668 | "ft-addr=%p, ft-id=%s", | |
669 | packet_context_type, | |
670 | bt_ctf_field_type_id_string(packet_context_type->id)); | |
671 | goto invalid; | |
672 | } | |
673 | ||
674 | /* | |
675 | * If there's a `packet_size` field, it must be an unsigned | |
676 | * integer field type. | |
677 | */ | |
678 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
679 | packet_context_type, "packet_size"); | |
680 | if (field_type) { | |
681 | if (!bt_ctf_field_type_is_integer(field_type)) { | |
682 | BT_LOGW("Invalid packet context field type: `packet_size` field must be an integer field type: " | |
683 | "packet-size-ft-addr=%p, packet-size-ft-id=%s", | |
684 | field_type, | |
685 | bt_ctf_field_type_id_string(field_type->id)); | |
686 | goto invalid; | |
687 | } | |
688 | ||
689 | if (bt_ctf_field_type_integer_is_signed(field_type)) { | |
690 | BT_LOGW("Invalid packet context field type: `packet_size` field must be an unsigned integer field type: " | |
691 | "packet-size-ft-addr=%p", field_type); | |
692 | goto invalid; | |
693 | } | |
694 | ||
695 | BT_PUT(field_type); | |
696 | } | |
697 | ||
698 | /* | |
699 | * If there's a `content_size` field, it must be an unsigned | |
700 | * integer field type. | |
701 | */ | |
702 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
703 | packet_context_type, "content_size"); | |
704 | if (field_type) { | |
705 | if (!bt_ctf_field_type_is_integer(field_type)) { | |
706 | BT_LOGW("Invalid packet context field type: `content_size` field must be an integer field type: " | |
707 | "content-size-ft-addr=%p, content-size-ft-id=%s", | |
708 | field_type, | |
709 | bt_ctf_field_type_id_string(field_type->id)); | |
710 | goto invalid; | |
711 | } | |
712 | ||
713 | if (bt_ctf_field_type_integer_is_signed(field_type)) { | |
714 | BT_LOGW("Invalid packet context field type: `content_size` field must be an unsigned integer field type: " | |
715 | "content-size-ft-addr=%p", field_type); | |
716 | goto invalid; | |
717 | } | |
718 | ||
719 | BT_PUT(field_type); | |
720 | } | |
721 | ||
722 | /* | |
723 | * If there's a `events_discarded` field, it must be an unsigned | |
724 | * integer field type. | |
725 | */ | |
726 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
727 | packet_context_type, "events_discarded"); | |
728 | if (field_type) { | |
729 | if (!bt_ctf_field_type_is_integer(field_type)) { | |
730 | BT_LOGW("Invalid packet context field type: `events_discarded` field must be an integer field type: " | |
731 | "events-discarded-ft-addr=%p, events-discarded-ft-id=%s", | |
732 | field_type, | |
733 | bt_ctf_field_type_id_string(field_type->id)); | |
734 | goto invalid; | |
735 | } | |
736 | ||
737 | if (bt_ctf_field_type_integer_is_signed(field_type)) { | |
738 | BT_LOGW("Invalid packet context field type: `events_discarded` field must be an unsigned integer field type: " | |
739 | "events-discarded-ft-addr=%p", field_type); | |
740 | goto invalid; | |
741 | } | |
742 | ||
743 | BT_PUT(field_type); | |
744 | } | |
745 | ||
746 | /* | |
747 | * If there's a `timestamp_begin` field, it must be an unsigned | |
748 | * integer field type. Also, if the trace is not a CTF writer's | |
749 | * trace, then we cannot automatically set the mapped clock | |
750 | * class of this field, so it must have a mapped clock class. | |
751 | */ | |
752 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
753 | packet_context_type, "timestamp_begin"); | |
754 | if (field_type) { | |
755 | if (!bt_ctf_field_type_is_integer(field_type)) { | |
756 | BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an integer field type: " | |
757 | "timestamp-begin-ft-addr=%p, timestamp-begin-ft-id=%s", | |
758 | field_type, | |
759 | bt_ctf_field_type_id_string(field_type->id)); | |
760 | goto invalid; | |
761 | } | |
762 | ||
763 | if (bt_ctf_field_type_integer_is_signed(field_type)) { | |
764 | BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be an unsigned integer field type: " | |
765 | "timestamp-begin-ft-addr=%p", field_type); | |
766 | goto invalid; | |
767 | } | |
768 | ||
769 | if (!trace->is_created_by_writer) { | |
770 | struct bt_ctf_clock_class *clock_class = | |
771 | bt_ctf_field_type_integer_get_mapped_clock_class( | |
772 | field_type); | |
773 | ||
774 | bt_put(clock_class); | |
775 | if (!clock_class) { | |
776 | BT_LOGW("Invalid packet context field type: `timestamp_begin` field must be mapped to a clock class: " | |
777 | "timestamp-begin-ft-addr=%p", field_type); | |
778 | goto invalid; | |
779 | } | |
780 | } | |
781 | ||
782 | BT_PUT(field_type); | |
783 | } | |
784 | ||
785 | /* | |
786 | * If there's a `timestamp_end` field, it must be an unsigned | |
787 | * integer field type. Also, if the trace is not a CTF writer's | |
788 | * trace, then we cannot automatically set the mapped clock | |
789 | * class of this field, so it must have a mapped clock class. | |
790 | */ | |
791 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
792 | packet_context_type, "timestamp_end"); | |
793 | if (field_type) { | |
794 | if (!bt_ctf_field_type_is_integer(field_type)) { | |
795 | BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an integer field type: " | |
796 | "timestamp-end-ft-addr=%p, timestamp-end-ft-id=%s", | |
797 | field_type, | |
798 | bt_ctf_field_type_id_string(field_type->id)); | |
799 | goto invalid; | |
800 | } | |
801 | ||
802 | if (bt_ctf_field_type_integer_is_signed(field_type)) { | |
803 | BT_LOGW("Invalid packet context field type: `timestamp_end` field must be an unsigned integer field type: " | |
804 | "timestamp-end-ft-addr=%p", field_type); | |
805 | goto invalid; | |
806 | } | |
807 | ||
808 | if (!trace->is_created_by_writer) { | |
809 | struct bt_ctf_clock_class *clock_class = | |
810 | bt_ctf_field_type_integer_get_mapped_clock_class( | |
811 | field_type); | |
812 | ||
813 | bt_put(clock_class); | |
814 | if (!clock_class) { | |
815 | BT_LOGW("Invalid packet context field type: `timestamp_end` field must be mapped to a clock class: " | |
816 | "timestamp-end-ft-addr=%p", field_type); | |
817 | goto invalid; | |
818 | } | |
819 | } | |
820 | ||
821 | BT_PUT(field_type); | |
822 | } | |
823 | ||
824 | goto end; | |
825 | ||
826 | invalid: | |
827 | is_valid = false; | |
828 | ||
829 | end: | |
830 | bt_put(field_type); | |
831 | return is_valid; | |
832 | } | |
833 | ||
834 | static | |
835 | bool event_header_field_type_is_valid(struct bt_ctf_trace *trace, | |
836 | struct bt_ctf_stream_class *stream_class, | |
837 | struct bt_ctf_field_type *event_header_type) | |
838 | { | |
839 | bool is_valid = true; | |
840 | struct bt_ctf_field_type *field_type = NULL; | |
841 | ||
842 | /* | |
843 | * We do not validate that the `timestamp` field exists here | |
844 | * because CTF does not require this exact name to be mapped to | |
845 | * a clock class. | |
846 | */ | |
847 | ||
848 | if (!event_header_type) { | |
849 | /* | |
850 | * No event header field type: stream class must have | |
851 | * only one event class. | |
852 | */ | |
853 | if (bt_ctf_stream_class_get_event_class_count(stream_class) > 1) { | |
854 | BT_LOGW_STR("Invalid event header field type: " | |
855 | "event header field type does not exist but there's more than one event class in the stream class."); | |
856 | goto invalid; | |
857 | } | |
858 | ||
859 | /* No event header field type: valid at this point */ | |
860 | goto end; | |
861 | } | |
862 | ||
863 | /* Event header field type, if it exists, must be a structure */ | |
864 | if (!bt_ctf_field_type_is_structure(event_header_type)) { | |
865 | BT_LOGW("Invalid event header field type: must be a structure field type if it exists: " | |
866 | "ft-addr=%p, ft-id=%s", | |
867 | event_header_type, | |
868 | bt_ctf_field_type_id_string(event_header_type->id)); | |
869 | goto invalid; | |
870 | } | |
871 | ||
872 | /* | |
873 | * If there's an `id` field, it must be an unsigned integer | |
874 | * field type or an enumeration field type with an unsigned | |
875 | * integer container field type. | |
876 | */ | |
877 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
878 | event_header_type, "id"); | |
879 | if (field_type) { | |
880 | struct bt_ctf_field_type *int_ft; | |
881 | ||
882 | if (bt_ctf_field_type_is_integer(field_type)) { | |
883 | int_ft = bt_get(field_type); | |
884 | } else if (bt_ctf_field_type_is_enumeration(field_type)) { | |
885 | int_ft = bt_ctf_field_type_enumeration_get_container_type( | |
886 | field_type); | |
887 | } else { | |
888 | BT_LOGW("Invalid event header field type: `id` field must be an integer or enumeration field type: " | |
889 | "id-ft-addr=%p, id-ft-id=%s", | |
890 | field_type, | |
891 | bt_ctf_field_type_id_string(field_type->id)); | |
892 | goto invalid; | |
893 | } | |
894 | ||
895 | assert(int_ft); | |
896 | if (bt_ctf_field_type_integer_is_signed(int_ft)) { | |
897 | BT_LOGW("Invalid event header field type: `id` field must be an unsigned integer or enumeration field type: " | |
898 | "id-ft-addr=%p", int_ft); | |
899 | goto invalid; | |
900 | } | |
901 | ||
902 | bt_put(int_ft); | |
903 | BT_PUT(field_type); | |
904 | } | |
905 | ||
906 | goto end; | |
907 | ||
908 | invalid: | |
909 | is_valid = false; | |
910 | ||
911 | end: | |
912 | bt_put(field_type); | |
913 | return is_valid; | |
914 | } | |
915 | ||
ef0c4a15 JG |
916 | int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, |
917 | struct bt_ctf_stream_class *stream_class) | |
918 | { | |
544d0515 PP |
919 | int ret; |
920 | int64_t i; | |
ef0c4a15 | 921 | int64_t stream_id; |
09840de5 PP |
922 | struct bt_ctf_validation_output trace_sc_validation_output = { 0 }; |
923 | struct bt_ctf_validation_output *ec_validation_outputs = NULL; | |
924 | const enum bt_ctf_validation_flag trace_sc_validation_flags = | |
925 | BT_CTF_VALIDATION_FLAG_TRACE | | |
926 | BT_CTF_VALIDATION_FLAG_STREAM; | |
927 | const enum bt_ctf_validation_flag ec_validation_flags = | |
928 | BT_CTF_VALIDATION_FLAG_EVENT; | |
929 | struct bt_ctf_field_type *packet_header_type = NULL; | |
930 | struct bt_ctf_field_type *packet_context_type = NULL; | |
931 | struct bt_ctf_field_type *event_header_type = NULL; | |
932 | struct bt_ctf_field_type *stream_event_ctx_type = NULL; | |
544d0515 | 933 | int64_t event_class_count; |
2789e5d9 | 934 | struct bt_ctf_trace *current_parent_trace = NULL; |
ef0c4a15 | 935 | |
5acf2ae6 | 936 | if (!trace || !stream_class || trace->is_static) { |
ef0c4a15 JG |
937 | ret = -1; |
938 | goto end; | |
939 | } | |
940 | ||
d490fcbe PP |
941 | /* |
942 | * At the end of this function we freeze the trace, so its | |
943 | * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE. | |
944 | */ | |
945 | if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) { | |
946 | ret = -1; | |
947 | goto end; | |
948 | } | |
949 | ||
2789e5d9 JG |
950 | current_parent_trace = bt_ctf_stream_class_get_trace(stream_class); |
951 | if (current_parent_trace) { | |
952 | /* Stream class is already associated to a trace, abort. */ | |
953 | ret = -1; | |
954 | goto end; | |
955 | } | |
956 | ||
09840de5 PP |
957 | event_class_count = |
958 | bt_ctf_stream_class_get_event_class_count(stream_class); | |
959 | assert(event_class_count >= 0); | |
960 | ||
961 | /* Check for duplicate stream classes */ | |
ef0c4a15 JG |
962 | for (i = 0; i < trace->stream_classes->len; i++) { |
963 | if (trace->stream_classes->pdata[i] == stream_class) { | |
09840de5 | 964 | /* Stream class already registered to the trace */ |
ef0c4a15 JG |
965 | ret = -1; |
966 | goto end; | |
967 | } | |
968 | } | |
969 | ||
fe13b5c0 | 970 | if (stream_class->clock) { |
ac0c6bdd PP |
971 | struct bt_ctf_clock_class *stream_clock_class = |
972 | stream_class->clock->clock_class; | |
973 | ||
974 | if (trace->is_created_by_writer) { | |
975 | /* | |
976 | * Make sure this clock was also added to the | |
977 | * trace (potentially through its CTF writer | |
978 | * owner). | |
979 | */ | |
980 | size_t i; | |
981 | ||
982 | for (i = 0; i < trace->clocks->len; i++) { | |
983 | if (trace->clocks->pdata[i] == | |
984 | stream_clock_class) { | |
985 | /* Found! */ | |
986 | break; | |
987 | } | |
988 | } | |
989 | ||
990 | if (i == trace->clocks->len) { | |
991 | /* Not found */ | |
fe13b5c0 PP |
992 | ret = -1; |
993 | goto end; | |
994 | } | |
995 | } else { | |
ac0c6bdd PP |
996 | /* |
997 | * This trace was NOT created by a CTF writer, | |
998 | * thus do not allow the stream class to add to | |
999 | * have a clock at all. Those are two | |
1000 | * independent APIs (non-writer and writer | |
1001 | * APIs), and isolating them simplifies things. | |
1002 | */ | |
1003 | ret = -1; | |
1004 | goto end; | |
fe13b5c0 PP |
1005 | } |
1006 | } | |
1007 | ||
09840de5 PP |
1008 | /* |
1009 | * We're about to freeze both the trace and the stream class. | |
1010 | * Also, each event class contained in this stream class are | |
1011 | * already frozen. | |
1012 | * | |
1013 | * This trace, this stream class, and all its event classes | |
1014 | * should be valid at this point. | |
1015 | * | |
1016 | * Validate trace and stream class first, then each event | |
1017 | * class of this stream class can be validated individually. | |
1018 | */ | |
1019 | packet_header_type = | |
1020 | bt_ctf_trace_get_packet_header_type(trace); | |
1021 | packet_context_type = | |
1022 | bt_ctf_stream_class_get_packet_context_type(stream_class); | |
1023 | event_header_type = | |
1024 | bt_ctf_stream_class_get_event_header_type(stream_class); | |
1025 | stream_event_ctx_type = | |
1026 | bt_ctf_stream_class_get_event_context_type(stream_class); | |
1027 | ret = bt_ctf_validate_class_types(trace->environment, | |
1028 | packet_header_type, packet_context_type, event_header_type, | |
1029 | stream_event_ctx_type, NULL, NULL, trace->valid, | |
1030 | stream_class->valid, 1, &trace_sc_validation_output, | |
1031 | trace_sc_validation_flags); | |
1032 | BT_PUT(packet_header_type); | |
1033 | BT_PUT(packet_context_type); | |
1034 | BT_PUT(event_header_type); | |
1035 | BT_PUT(stream_event_ctx_type); | |
1036 | ||
2bd7f864 | 1037 | if (ret) { |
09840de5 PP |
1038 | /* |
1039 | * This means something went wrong during the validation | |
1040 | * process, not that the objects are invalid. | |
1041 | */ | |
2bd7f864 JG |
1042 | goto end; |
1043 | } | |
1044 | ||
09840de5 PP |
1045 | if ((trace_sc_validation_output.valid_flags & |
1046 | trace_sc_validation_flags) != | |
1047 | trace_sc_validation_flags) { | |
1048 | /* Invalid trace/stream class */ | |
1049 | ret = -1; | |
1050 | goto end; | |
1051 | } | |
1052 | ||
1053 | if (event_class_count > 0) { | |
1054 | ec_validation_outputs = g_new0(struct bt_ctf_validation_output, | |
1055 | event_class_count); | |
1056 | if (!ec_validation_outputs) { | |
1057 | ret = -1; | |
1058 | goto end; | |
1059 | } | |
1060 | } | |
1061 | ||
1062 | /* Validate each event class individually */ | |
6474e016 | 1063 | for (i = 0; i < event_class_count; i++) { |
09840de5 | 1064 | struct bt_ctf_event_class *event_class = |
9ac68eb1 PP |
1065 | bt_ctf_stream_class_get_event_class_by_index( |
1066 | stream_class, i); | |
09840de5 PP |
1067 | struct bt_ctf_field_type *event_context_type = NULL; |
1068 | struct bt_ctf_field_type *event_payload_type = NULL; | |
1069 | ||
1070 | event_context_type = | |
1071 | bt_ctf_event_class_get_context_type(event_class); | |
1072 | event_payload_type = | |
1073 | bt_ctf_event_class_get_payload_type(event_class); | |
1074 | ||
1075 | /* | |
1076 | * It is important to use the field types returned by | |
1077 | * the previous trace and stream class validation here | |
1078 | * because copies could have been made. | |
1079 | */ | |
1080 | ret = bt_ctf_validate_class_types(trace->environment, | |
1081 | trace_sc_validation_output.packet_header_type, | |
1082 | trace_sc_validation_output.packet_context_type, | |
1083 | trace_sc_validation_output.event_header_type, | |
1084 | trace_sc_validation_output.stream_event_ctx_type, | |
1085 | event_context_type, event_payload_type, | |
1086 | 1, 1, event_class->valid, &ec_validation_outputs[i], | |
1087 | ec_validation_flags); | |
1088 | BT_PUT(event_context_type); | |
1089 | BT_PUT(event_payload_type); | |
1090 | BT_PUT(event_class); | |
1091 | ||
1092 | if (ret) { | |
1093 | goto end; | |
1094 | } | |
1095 | ||
1096 | if ((ec_validation_outputs[i].valid_flags & | |
1097 | ec_validation_flags) != ec_validation_flags) { | |
1098 | /* Invalid event class */ | |
1099 | ret = -1; | |
1100 | goto end; | |
1101 | } | |
1102 | } | |
1103 | ||
ef0c4a15 JG |
1104 | stream_id = bt_ctf_stream_class_get_id(stream_class); |
1105 | if (stream_id < 0) { | |
1106 | stream_id = trace->next_stream_id++; | |
9ac68eb1 PP |
1107 | if (stream_id < 0) { |
1108 | ret = -1; | |
1109 | goto end; | |
1110 | } | |
ef0c4a15 JG |
1111 | |
1112 | /* Try to assign a new stream id */ | |
1113 | for (i = 0; i < trace->stream_classes->len; i++) { | |
1114 | if (stream_id == bt_ctf_stream_class_get_id( | |
1115 | trace->stream_classes->pdata[i])) { | |
1116 | /* Duplicate stream id found */ | |
1117 | ret = -1; | |
1118 | goto end; | |
1119 | } | |
1120 | } | |
1121 | ||
0ddffae5 JG |
1122 | if (bt_ctf_stream_class_set_id_no_check(stream_class, |
1123 | stream_id)) { | |
ef0c4a15 JG |
1124 | /* TODO Should retry with a different stream id */ |
1125 | ret = -1; | |
1126 | goto end; | |
1127 | } | |
1128 | } | |
1129 | ||
e011d2c1 PP |
1130 | /* |
1131 | * At this point all the field types in the validation output | |
1132 | * are valid. Validate the semantics of some scopes according to | |
1133 | * the CTF specification. | |
1134 | */ | |
1135 | if (!packet_header_field_type_is_valid(trace, | |
1136 | trace_sc_validation_output.packet_header_type)) { | |
1137 | ret = -1; | |
1138 | goto end; | |
1139 | } | |
1140 | ||
1141 | if (!packet_context_field_type_is_valid(trace, | |
1142 | stream_class, | |
1143 | trace_sc_validation_output.packet_context_type)) { | |
1144 | ret = -1; | |
1145 | goto end; | |
1146 | } | |
1147 | ||
1148 | if (!event_header_field_type_is_valid(trace, | |
1149 | stream_class, | |
1150 | trace_sc_validation_output.event_header_type)) { | |
1151 | ret = -1; | |
1152 | goto end; | |
1153 | } | |
1154 | ||
1155 | /* | |
1156 | * Now is the time to automatically map specific field types of | |
1157 | * the stream class's packet context and event header field | |
1158 | * types to the stream class's clock's class if they are not | |
1159 | * mapped to a clock class yet. We do it here because we know | |
1160 | * that after this point, everything is frozen so it won't be | |
1161 | * possible for the user to modify the stream class's clock, or | |
1162 | * to map those field types to other clock classes. | |
1163 | */ | |
1164 | if (trace->is_created_by_writer) { | |
1165 | if (bt_ctf_stream_class_map_clock_class(stream_class, | |
1166 | trace_sc_validation_output.packet_context_type, | |
1167 | trace_sc_validation_output.event_header_type)) { | |
1168 | ret = -1; | |
1169 | goto end; | |
1170 | } | |
1171 | } | |
1172 | ||
e6a8e8e4 | 1173 | bt_object_set_parent(stream_class, trace); |
ef0c4a15 JG |
1174 | g_ptr_array_add(trace->stream_classes, stream_class); |
1175 | ||
1176 | /* | |
09840de5 PP |
1177 | * At this point we know that the function will be successful. |
1178 | * Therefore we can replace the trace and stream class field | |
1179 | * types with what's in their validation output structure and | |
1180 | * mark them as valid. We can also replace the field types of | |
1181 | * all the event classes of the stream class and mark them as | |
1182 | * valid. | |
1183 | */ | |
1184 | bt_ctf_validation_replace_types(trace, stream_class, NULL, | |
1185 | &trace_sc_validation_output, trace_sc_validation_flags); | |
1186 | trace->valid = 1; | |
1187 | stream_class->valid = 1; | |
1188 | ||
1189 | /* | |
1190 | * Put what was not moved in bt_ctf_validation_replace_types(). | |
1191 | */ | |
1192 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
1193 | ||
6474e016 | 1194 | for (i = 0; i < event_class_count; i++) { |
09840de5 | 1195 | struct bt_ctf_event_class *event_class = |
9ac68eb1 PP |
1196 | bt_ctf_stream_class_get_event_class_by_index( |
1197 | stream_class, i); | |
09840de5 PP |
1198 | |
1199 | bt_ctf_validation_replace_types(NULL, NULL, event_class, | |
1200 | &ec_validation_outputs[i], ec_validation_flags); | |
1201 | event_class->valid = 1; | |
1202 | BT_PUT(event_class); | |
1203 | ||
1204 | /* | |
1205 | * Put what was not moved in | |
1206 | * bt_ctf_validation_replace_types(). | |
1207 | */ | |
1208 | bt_ctf_validation_output_put_types(&ec_validation_outputs[i]); | |
1209 | } | |
1210 | ||
09840de5 PP |
1211 | /* |
1212 | * Freeze the trace and the stream class. | |
1213 | */ | |
f67f3a6e | 1214 | bt_ctf_stream_class_freeze(stream_class); |
09840de5 PP |
1215 | bt_ctf_trace_freeze(trace); |
1216 | ||
9b888ff3 JG |
1217 | /* Notifiy listeners of the trace's schema modification. */ |
1218 | bt_ctf_stream_class_visit(stream_class, | |
d9a13d86 | 1219 | bt_ctf_trace_object_modification, trace); |
ef0c4a15 | 1220 | end: |
d3814b54 | 1221 | if (ret) { |
e6a8e8e4 | 1222 | bt_object_set_parent(stream_class, NULL); |
09840de5 PP |
1223 | |
1224 | if (ec_validation_outputs) { | |
6474e016 | 1225 | for (i = 0; i < event_class_count; i++) { |
09840de5 PP |
1226 | bt_ctf_validation_output_put_types( |
1227 | &ec_validation_outputs[i]); | |
1228 | } | |
1229 | } | |
d3814b54 | 1230 | } |
09840de5 PP |
1231 | |
1232 | g_free(ec_validation_outputs); | |
1233 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
2789e5d9 | 1234 | bt_put(current_parent_trace); |
09840de5 PP |
1235 | assert(!packet_header_type); |
1236 | assert(!packet_context_type); | |
1237 | assert(!event_header_type); | |
1238 | assert(!stream_event_ctx_type); | |
1239 | ||
ef0c4a15 JG |
1240 | return ret; |
1241 | } | |
1242 | ||
544d0515 | 1243 | int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace) |
c1e730fe | 1244 | { |
544d0515 | 1245 | int64_t ret; |
c1e730fe PP |
1246 | |
1247 | if (!trace) { | |
9ac68eb1 | 1248 | ret = (int64_t) -1; |
c1e730fe PP |
1249 | goto end; |
1250 | } | |
1251 | ||
9ac68eb1 | 1252 | ret = (int64_t) trace->streams->len; |
c1e730fe PP |
1253 | |
1254 | end: | |
1255 | return ret; | |
1256 | } | |
1257 | ||
9ac68eb1 PP |
1258 | struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index( |
1259 | struct bt_ctf_trace *trace, | |
1260 | uint64_t index) | |
c1e730fe PP |
1261 | { |
1262 | struct bt_ctf_stream *stream = NULL; | |
1263 | ||
1264 | if (!trace || index >= trace->streams->len) { | |
1265 | goto end; | |
1266 | } | |
1267 | ||
1268 | stream = bt_get(g_ptr_array_index(trace->streams, index)); | |
1269 | ||
1270 | end: | |
1271 | return stream; | |
1272 | } | |
1273 | ||
544d0515 | 1274 | int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) |
ef0c4a15 | 1275 | { |
544d0515 | 1276 | int64_t ret; |
ef0c4a15 JG |
1277 | |
1278 | if (!trace) { | |
9ac68eb1 | 1279 | ret = (int64_t) -1; |
ef0c4a15 JG |
1280 | goto end; |
1281 | } | |
1282 | ||
9ac68eb1 | 1283 | ret = (int64_t) trace->stream_classes->len; |
ef0c4a15 JG |
1284 | end: |
1285 | return ret; | |
1286 | } | |
1287 | ||
9ac68eb1 PP |
1288 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index( |
1289 | struct bt_ctf_trace *trace, uint64_t index) | |
ef0c4a15 JG |
1290 | { |
1291 | struct bt_ctf_stream_class *stream_class = NULL; | |
1292 | ||
9ac68eb1 | 1293 | if (!trace || index >= trace->stream_classes->len) { |
ef0c4a15 JG |
1294 | goto end; |
1295 | } | |
1296 | ||
1297 | stream_class = g_ptr_array_index(trace->stream_classes, index); | |
83509119 | 1298 | bt_get(stream_class); |
ef0c4a15 JG |
1299 | end: |
1300 | return stream_class; | |
1301 | } | |
1302 | ||
4841ccc1 | 1303 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id( |
9ac68eb1 | 1304 | struct bt_ctf_trace *trace, uint64_t id_param) |
4841ccc1 PP |
1305 | { |
1306 | int i; | |
1307 | struct bt_ctf_stream_class *stream_class = NULL; | |
9ac68eb1 | 1308 | int64_t id = (int64_t) id_param; |
4841ccc1 | 1309 | |
9ac68eb1 | 1310 | if (!trace || id < 0) { |
4841ccc1 PP |
1311 | goto end; |
1312 | } | |
1313 | ||
6474e016 | 1314 | for (i = 0; i < trace->stream_classes->len; i++) { |
4841ccc1 PP |
1315 | struct bt_ctf_stream_class *stream_class_candidate; |
1316 | ||
1317 | stream_class_candidate = | |
1318 | g_ptr_array_index(trace->stream_classes, i); | |
1319 | ||
1320 | if (bt_ctf_stream_class_get_id(stream_class_candidate) == | |
1321 | (int64_t) id) { | |
1322 | stream_class = stream_class_candidate; | |
83509119 | 1323 | bt_get(stream_class); |
4841ccc1 PP |
1324 | goto end; |
1325 | } | |
1326 | } | |
1327 | ||
1328 | end: | |
1329 | return stream_class; | |
1330 | } | |
1331 | ||
ac0c6bdd | 1332 | struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name( |
7e4347a3 PP |
1333 | struct bt_ctf_trace *trace, const char *name) |
1334 | { | |
1335 | size_t i; | |
ac0c6bdd | 1336 | struct bt_ctf_clock_class *clock_class = NULL; |
7e4347a3 PP |
1337 | |
1338 | if (!trace || !name) { | |
1339 | goto end; | |
1340 | } | |
1341 | ||
6474e016 | 1342 | for (i = 0; i < trace->clocks->len; i++) { |
ac0c6bdd | 1343 | struct bt_ctf_clock_class *cur_clk = |
7e4347a3 | 1344 | g_ptr_array_index(trace->clocks, i); |
ac0c6bdd | 1345 | const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk); |
7e4347a3 PP |
1346 | |
1347 | if (!cur_clk_name) { | |
1348 | goto end; | |
1349 | } | |
1350 | ||
1351 | if (!strcmp(cur_clk_name, name)) { | |
ac0c6bdd PP |
1352 | clock_class = cur_clk; |
1353 | bt_get(clock_class); | |
7e4347a3 PP |
1354 | goto end; |
1355 | } | |
1356 | } | |
1357 | ||
1358 | end: | |
ac0c6bdd | 1359 | return clock_class; |
7e4347a3 PP |
1360 | } |
1361 | ||
c9d90a34 | 1362 | BT_HIDDEN |
c55a9f58 | 1363 | bt_bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace *trace, |
c9d90a34 PP |
1364 | struct bt_ctf_clock_class *clock_class) |
1365 | { | |
1366 | struct search_query query = { .value = clock_class, .found = 0 }; | |
1367 | ||
1368 | assert(trace); | |
1369 | assert(clock_class); | |
1370 | ||
1371 | g_ptr_array_foreach(trace->clocks, value_exists, &query); | |
1372 | return query.found; | |
1373 | } | |
1374 | ||
bc37ae52 | 1375 | BT_HIDDEN |
dc3fffef | 1376 | const char *get_byte_order_string(enum bt_ctf_byte_order byte_order) |
bc37ae52 JG |
1377 | { |
1378 | const char *string; | |
1379 | ||
1380 | switch (byte_order) { | |
dc3fffef | 1381 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: |
bc37ae52 JG |
1382 | string = "le"; |
1383 | break; | |
dc3fffef | 1384 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: |
bc37ae52 JG |
1385 | string = "be"; |
1386 | break; | |
dc3fffef PP |
1387 | case BT_CTF_BYTE_ORDER_NATIVE: |
1388 | string = "native"; | |
bc37ae52 | 1389 | break; |
dc3fffef | 1390 | default: |
c55a9f58 | 1391 | assert(BT_FALSE); |
bc37ae52 JG |
1392 | } |
1393 | ||
1394 | return string; | |
1395 | } | |
1396 | ||
1397 | static | |
1398 | int append_trace_metadata(struct bt_ctf_trace *trace, | |
1399 | struct metadata_context *context) | |
1400 | { | |
1401 | unsigned char *uuid = trace->uuid; | |
e011d2c1 | 1402 | int ret = 0; |
bc37ae52 | 1403 | |
4a32fda0 PP |
1404 | if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) { |
1405 | ret = -1; | |
1406 | goto end; | |
1407 | } | |
bc37ae52 | 1408 | |
4a32fda0 | 1409 | g_string_append(context->string, "trace {\n"); |
bc37ae52 JG |
1410 | g_string_append(context->string, "\tmajor = 1;\n"); |
1411 | g_string_append(context->string, "\tminor = 8;\n"); | |
dc3fffef PP |
1412 | assert(trace->native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN || |
1413 | trace->native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN || | |
1414 | trace->native_byte_order == BT_CTF_BYTE_ORDER_NETWORK); | |
4a32fda0 PP |
1415 | |
1416 | if (trace->uuid_set) { | |
1417 | g_string_append_printf(context->string, | |
1418 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
1419 | uuid[0], uuid[1], uuid[2], uuid[3], | |
1420 | uuid[4], uuid[5], uuid[6], uuid[7], | |
1421 | uuid[8], uuid[9], uuid[10], uuid[11], | |
1422 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
1423 | } | |
1424 | ||
bc37ae52 | 1425 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", |
dc3fffef | 1426 | get_byte_order_string(trace->native_byte_order)); |
bc37ae52 | 1427 | |
e011d2c1 PP |
1428 | if (trace->packet_header_type) { |
1429 | g_string_append(context->string, "\tpacket.header := "); | |
1430 | context->current_indentation_level++; | |
1431 | g_string_assign(context->field_name, ""); | |
1432 | ret = bt_ctf_field_type_serialize(trace->packet_header_type, | |
1433 | context); | |
1434 | if (ret) { | |
1435 | goto end; | |
1436 | } | |
1437 | context->current_indentation_level--; | |
bc37ae52 | 1438 | } |
bc37ae52 JG |
1439 | |
1440 | g_string_append(context->string, ";\n};\n\n"); | |
1441 | end: | |
1442 | return ret; | |
1443 | } | |
1444 | ||
bc37ae52 JG |
1445 | static |
1446 | void append_env_metadata(struct bt_ctf_trace *trace, | |
1447 | struct metadata_context *context) | |
1448 | { | |
544d0515 PP |
1449 | int64_t i; |
1450 | int64_t env_size; | |
7f800dc7 PP |
1451 | |
1452 | env_size = bt_ctf_attributes_get_count(trace->environment); | |
1453 | ||
1454 | if (env_size <= 0) { | |
bc37ae52 JG |
1455 | return; |
1456 | } | |
1457 | ||
1458 | g_string_append(context->string, "env {\n"); | |
7f800dc7 | 1459 | |
6474e016 | 1460 | for (i = 0; i < env_size; i++) { |
dac5c838 | 1461 | struct bt_value *env_field_value_obj = NULL; |
7f800dc7 | 1462 | const char *entry_name; |
7f800dc7 PP |
1463 | |
1464 | entry_name = bt_ctf_attributes_get_field_name( | |
1465 | trace->environment, i); | |
1466 | env_field_value_obj = bt_ctf_attributes_get_field_value( | |
1467 | trace->environment, i); | |
1468 | ||
1469 | if (!entry_name || !env_field_value_obj) { | |
1470 | goto loop_next; | |
1471 | } | |
1472 | ||
dac5c838 PP |
1473 | switch (bt_value_get_type(env_field_value_obj)) { |
1474 | case BT_VALUE_TYPE_INTEGER: | |
b8248cc0 PP |
1475 | { |
1476 | int ret; | |
1477 | int64_t int_value; | |
1478 | ||
dac5c838 | 1479 | ret = bt_value_integer_get(env_field_value_obj, |
7f800dc7 PP |
1480 | &int_value); |
1481 | ||
1482 | if (ret) { | |
1483 | goto loop_next; | |
1484 | } | |
1485 | ||
1486 | g_string_append_printf(context->string, | |
1487 | "\t%s = %" PRId64 ";\n", entry_name, | |
1488 | int_value); | |
1489 | break; | |
b8248cc0 | 1490 | } |
dac5c838 | 1491 | case BT_VALUE_TYPE_STRING: |
7f800dc7 PP |
1492 | { |
1493 | int ret; | |
1494 | const char *str_value; | |
1495 | char *escaped_str = NULL; | |
1496 | ||
dac5c838 | 1497 | ret = bt_value_string_get(env_field_value_obj, |
7f800dc7 PP |
1498 | &str_value); |
1499 | ||
1500 | if (ret) { | |
1501 | goto loop_next; | |
1502 | } | |
1503 | ||
1504 | escaped_str = g_strescape(str_value, NULL); | |
1505 | ||
1506 | if (!escaped_str) { | |
1507 | goto loop_next; | |
1508 | } | |
1509 | ||
1510 | g_string_append_printf(context->string, | |
1511 | "\t%s = \"%s\";\n", entry_name, escaped_str); | |
1512 | free(escaped_str); | |
1513 | break; | |
1514 | } | |
1515 | ||
1516 | default: | |
1517 | goto loop_next; | |
1518 | } | |
1519 | ||
1520 | loop_next: | |
83509119 | 1521 | BT_PUT(env_field_value_obj); |
7f800dc7 PP |
1522 | } |
1523 | ||
bc37ae52 JG |
1524 | g_string_append(context->string, "};\n\n"); |
1525 | } | |
1526 | ||
1527 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) | |
1528 | { | |
1529 | char *metadata = NULL; | |
1530 | struct metadata_context *context = NULL; | |
1531 | int err = 0; | |
1532 | size_t i; | |
1533 | ||
1534 | if (!trace) { | |
1535 | goto end; | |
1536 | } | |
1537 | ||
1538 | context = g_new0(struct metadata_context, 1); | |
1539 | if (!context) { | |
1540 | goto end; | |
1541 | } | |
1542 | ||
1543 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); | |
1544 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); | |
1545 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); | |
1546 | if (append_trace_metadata(trace, context)) { | |
1547 | goto error; | |
1548 | } | |
1549 | append_env_metadata(trace, context); | |
1550 | g_ptr_array_foreach(trace->clocks, | |
ac0c6bdd | 1551 | (GFunc)bt_ctf_clock_class_serialize, context); |
bc37ae52 JG |
1552 | |
1553 | for (i = 0; i < trace->stream_classes->len; i++) { | |
1554 | err = bt_ctf_stream_class_serialize( | |
1555 | trace->stream_classes->pdata[i], context); | |
1556 | if (err) { | |
1557 | goto error; | |
1558 | } | |
1559 | } | |
1560 | ||
1561 | metadata = context->string->str; | |
1562 | error: | |
1563 | g_string_free(context->string, err ? TRUE : FALSE); | |
1564 | g_string_free(context->field_name, TRUE); | |
1565 | g_free(context); | |
1566 | end: | |
1567 | return metadata; | |
1568 | } | |
1569 | ||
8a716c8d PP |
1570 | enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order( |
1571 | struct bt_ctf_trace *trace) | |
4ed90fb3 JG |
1572 | { |
1573 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; | |
1574 | ||
1575 | if (!trace) { | |
1576 | goto end; | |
1577 | } | |
1578 | ||
dc3fffef PP |
1579 | ret = trace->native_byte_order; |
1580 | ||
4ed90fb3 JG |
1581 | end: |
1582 | return ret; | |
1583 | } | |
1584 | ||
391c8f0d | 1585 | int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace, |
bc37ae52 JG |
1586 | enum bt_ctf_byte_order byte_order) |
1587 | { | |
1588 | int ret = 0; | |
bc37ae52 JG |
1589 | |
1590 | if (!trace || trace->frozen) { | |
1591 | ret = -1; | |
1592 | goto end; | |
1593 | } | |
1594 | ||
dc3fffef PP |
1595 | if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN && |
1596 | byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN && | |
1597 | byte_order != BT_CTF_BYTE_ORDER_NETWORK) { | |
bc37ae52 JG |
1598 | ret = -1; |
1599 | goto end; | |
1600 | } | |
1601 | ||
dc3fffef PP |
1602 | trace->native_byte_order = byte_order; |
1603 | ||
bc37ae52 JG |
1604 | end: |
1605 | return ret; | |
1606 | } | |
1607 | ||
d246b111 JG |
1608 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( |
1609 | struct bt_ctf_trace *trace) | |
1610 | { | |
1611 | struct bt_ctf_field_type *field_type = NULL; | |
1612 | ||
1613 | if (!trace) { | |
1614 | goto end; | |
1615 | } | |
1616 | ||
83509119 | 1617 | bt_get(trace->packet_header_type); |
d246b111 JG |
1618 | field_type = trace->packet_header_type; |
1619 | end: | |
1620 | return field_type; | |
1621 | } | |
1622 | ||
1623 | int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, | |
1624 | struct bt_ctf_field_type *packet_header_type) | |
1625 | { | |
1626 | int ret = 0; | |
1627 | ||
835b2d10 | 1628 | if (!trace || trace->frozen) { |
d246b111 JG |
1629 | ret = -1; |
1630 | goto end; | |
1631 | } | |
1632 | ||
835b2d10 JG |
1633 | /* packet_header_type must be a structure. */ |
1634 | if (packet_header_type && | |
1635 | bt_ctf_field_type_get_type_id(packet_header_type) != | |
1487a16a | 1636 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
d246b111 JG |
1637 | ret = -1; |
1638 | goto end; | |
1639 | } | |
1640 | ||
83509119 | 1641 | bt_put(trace->packet_header_type); |
835b2d10 | 1642 | trace->packet_header_type = bt_get(packet_header_type); |
d246b111 JG |
1643 | end: |
1644 | return ret; | |
1645 | } | |
1646 | ||
8bf65fbd | 1647 | static |
544d0515 | 1648 | int64_t get_stream_class_count(void *element) |
8bf65fbd JG |
1649 | { |
1650 | return bt_ctf_trace_get_stream_class_count( | |
1651 | (struct bt_ctf_trace *) element); | |
1652 | } | |
1653 | ||
1654 | static | |
1655 | void *get_stream_class(void *element, int i) | |
1656 | { | |
9ac68eb1 | 1657 | return bt_ctf_trace_get_stream_class_by_index( |
8bf65fbd JG |
1658 | (struct bt_ctf_trace *) element, i); |
1659 | } | |
1660 | ||
1661 | static | |
d9a13d86 | 1662 | int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data) |
8bf65fbd | 1663 | { |
d9a13d86 | 1664 | return bt_ctf_stream_class_visit(object, visitor, data); |
8bf65fbd JG |
1665 | } |
1666 | ||
1667 | int bt_ctf_trace_visit(struct bt_ctf_trace *trace, | |
d9a13d86 | 1668 | bt_ctf_visitor visitor, void *data) |
8bf65fbd JG |
1669 | { |
1670 | int ret; | |
d9a13d86 PP |
1671 | struct bt_ctf_object obj = |
1672 | { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE }; | |
8bf65fbd JG |
1673 | |
1674 | if (!trace || !visitor) { | |
1675 | ret = -1; | |
1676 | goto end; | |
1677 | } | |
1678 | ||
d9a13d86 | 1679 | ret = visitor_helper(&obj, get_stream_class_count, |
8bf65fbd JG |
1680 | get_stream_class, visit_stream_class, visitor, data); |
1681 | end: | |
1682 | return ret; | |
1683 | } | |
1684 | ||
1685 | static | |
d9a13d86 | 1686 | int invoke_listener(struct bt_ctf_object *object, void *data) |
8bf65fbd | 1687 | { |
9b888ff3 | 1688 | struct listener_wrapper *listener_wrapper = data; |
8bf65fbd | 1689 | |
d9a13d86 | 1690 | listener_wrapper->listener(object, listener_wrapper->data); |
9b888ff3 | 1691 | return 0; |
8bf65fbd JG |
1692 | } |
1693 | ||
50ad4244 JG |
1694 | int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace, |
1695 | bt_ctf_listener_cb listener, void *listener_data) | |
2204c381 JG |
1696 | { |
1697 | int ret = 0; | |
50ad4244 JG |
1698 | struct listener_wrapper *listener_wrapper = |
1699 | g_new0(struct listener_wrapper, 1); | |
2204c381 | 1700 | |
50ad4244 | 1701 | if (!trace || !listener || !listener_wrapper) { |
2204c381 JG |
1702 | ret = -1; |
1703 | goto error; | |
1704 | } | |
1705 | ||
50ad4244 JG |
1706 | listener_wrapper->listener = listener; |
1707 | listener_wrapper->data = listener_data; | |
8bf65fbd | 1708 | |
50ad4244 | 1709 | /* Visit the current schema. */ |
9b888ff3 | 1710 | ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper); |
8bf65fbd JG |
1711 | if (ret) { |
1712 | goto error; | |
1713 | } | |
1714 | ||
50ad4244 JG |
1715 | /* |
1716 | * Add listener to the array of callbacks which will be invoked on | |
1717 | * schema changes. | |
1718 | */ | |
1719 | g_ptr_array_add(trace->listeners, listener_wrapper); | |
2204c381 JG |
1720 | return ret; |
1721 | error: | |
50ad4244 | 1722 | g_free(listener_wrapper); |
2204c381 JG |
1723 | return ret; |
1724 | } | |
1725 | ||
bc37ae52 | 1726 | BT_HIDDEN |
d9a13d86 | 1727 | int bt_ctf_trace_object_modification(struct bt_ctf_object *object, |
9b888ff3 JG |
1728 | void *trace_ptr) |
1729 | { | |
1730 | size_t i; | |
1731 | struct bt_ctf_trace *trace = trace_ptr; | |
1732 | ||
1733 | assert(trace); | |
d9a13d86 | 1734 | assert(object); |
9b888ff3 JG |
1735 | |
1736 | if (trace->listeners->len == 0) { | |
1737 | goto end; | |
1738 | } | |
1739 | ||
1740 | for (i = 0; i < trace->listeners->len; i++) { | |
1741 | struct listener_wrapper *listener = | |
1742 | g_ptr_array_index(trace->listeners, i); | |
1743 | ||
d9a13d86 | 1744 | listener->listener(object, listener->data); |
9b888ff3 JG |
1745 | } |
1746 | end: | |
1747 | return 0; | |
1748 | } | |
1749 | ||
1750 | BT_HIDDEN | |
bc37ae52 JG |
1751 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) |
1752 | { | |
a25e0d14 | 1753 | int ret; |
bc37ae52 | 1754 | unsigned int alignment, size; |
8bdcb82e | 1755 | struct bt_ctf_field_type *field_type = NULL; |
bc37ae52 JG |
1756 | |
1757 | if (alias >= NR_FIELD_TYPE_ALIAS) { | |
8bdcb82e | 1758 | goto end; |
bc37ae52 JG |
1759 | } |
1760 | ||
1761 | alignment = field_type_aliases_alignments[alias]; | |
1762 | size = field_type_aliases_sizes[alias]; | |
1763 | field_type = bt_ctf_field_type_integer_create(size); | |
a25e0d14 JG |
1764 | ret = bt_ctf_field_type_set_alignment(field_type, alignment); |
1765 | if (ret) { | |
1766 | BT_PUT(field_type); | |
1767 | } | |
8bdcb82e | 1768 | end: |
bc37ae52 JG |
1769 | return field_type; |
1770 | } | |
1771 | ||
f67f3a6e | 1772 | static |
09840de5 | 1773 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace) |
f67f3a6e | 1774 | { |
6474e016 PP |
1775 | int i; |
1776 | ||
09840de5 | 1777 | bt_ctf_field_type_freeze(trace->packet_header_type); |
f67f3a6e | 1778 | bt_ctf_attributes_freeze(trace->environment); |
6474e016 PP |
1779 | |
1780 | for (i = 0; i < trace->clocks->len; i++) { | |
ac0c6bdd | 1781 | struct bt_ctf_clock_class *clock_class = |
6474e016 PP |
1782 | g_ptr_array_index(trace->clocks, i); |
1783 | ||
ac0c6bdd | 1784 | bt_ctf_clock_class_freeze(clock_class); |
6474e016 PP |
1785 | } |
1786 | ||
f67f3a6e JG |
1787 | trace->frozen = 1; |
1788 | } | |
1789 | ||
c55a9f58 | 1790 | bt_bool bt_ctf_trace_is_static(struct bt_ctf_trace *trace) |
5acf2ae6 | 1791 | { |
c55a9f58 | 1792 | bt_bool is_static = BT_FALSE; |
5acf2ae6 PP |
1793 | |
1794 | if (!trace) { | |
1795 | goto end; | |
1796 | } | |
1797 | ||
1798 | is_static = trace->is_static; | |
1799 | ||
1800 | end: | |
1801 | return is_static; | |
1802 | } | |
1803 | ||
1804 | int bt_ctf_trace_set_is_static(struct bt_ctf_trace *trace) | |
1805 | { | |
1806 | int ret = 0; | |
1807 | ||
1808 | if (!trace) { | |
1809 | ret = -1; | |
1810 | goto end; | |
1811 | } | |
1812 | ||
c55a9f58 | 1813 | trace->is_static = BT_TRUE; |
5acf2ae6 PP |
1814 | bt_ctf_trace_freeze(trace); |
1815 | ||
1816 | end: | |
1817 | return ret; | |
1818 | } |