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> | |
30 | #include <babeltrace/ctf-ir/clock-internal.h> | |
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> |
2e33ac5a | 37 | #include <babeltrace/ctf-ir/field-types-internal.h> |
44e0a4f5 | 38 | #include <babeltrace/ctf-ir/attributes-internal.h> |
09840de5 | 39 | #include <babeltrace/ctf-ir/validation-internal.h> |
654c1444 | 40 | #include <babeltrace/ctf-ir/utils.h> |
bc37ae52 | 41 | #include <babeltrace/compiler.h> |
dac5c838 | 42 | #include <babeltrace/values.h> |
83509119 | 43 | #include <babeltrace/ref.h> |
a0b720b2 | 44 | #include <babeltrace/endian.h> |
bc37ae52 JG |
45 | |
46 | #define DEFAULT_IDENTIFIER_SIZE 128 | |
47 | #define DEFAULT_METADATA_STRING_SIZE 4096 | |
48 | ||
bc37ae52 | 49 | static |
83509119 | 50 | void bt_ctf_trace_destroy(struct bt_object *obj); |
bc37ae52 JG |
51 | static |
52 | int init_trace_packet_header(struct bt_ctf_trace *trace); | |
f67f3a6e | 53 | static |
09840de5 | 54 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace); |
bc37ae52 | 55 | |
bc37ae52 JG |
56 | static |
57 | const unsigned int field_type_aliases_alignments[] = { | |
58 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, | |
59 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, | |
60 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, | |
61 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, | |
62 | }; | |
63 | ||
64 | static | |
65 | const unsigned int field_type_aliases_sizes[] = { | |
66 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, | |
67 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, | |
68 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, | |
69 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, | |
70 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, | |
71 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, | |
72 | }; | |
73 | ||
bc37ae52 JG |
74 | struct bt_ctf_trace *bt_ctf_trace_create(void) |
75 | { | |
76 | struct bt_ctf_trace *trace = NULL; | |
77 | ||
78 | trace = g_new0(struct bt_ctf_trace, 1); | |
79 | if (!trace) { | |
80 | goto error; | |
81 | } | |
82 | ||
83 | bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE); | |
83509119 | 84 | bt_object_init(trace, bt_ctf_trace_destroy); |
bc37ae52 | 85 | trace->clocks = g_ptr_array_new_with_free_func( |
83509119 | 86 | (GDestroyNotify) bt_put); |
bc37ae52 | 87 | trace->streams = g_ptr_array_new_with_free_func( |
e6a8e8e4 | 88 | (GDestroyNotify) bt_object_release); |
bc37ae52 | 89 | trace->stream_classes = g_ptr_array_new_with_free_func( |
e6a8e8e4 | 90 | (GDestroyNotify) bt_object_release); |
7f800dc7 | 91 | if (!trace->clocks || !trace->stream_classes || !trace->streams) { |
83509119 | 92 | goto error; |
bc37ae52 JG |
93 | } |
94 | ||
95 | /* Generate a trace UUID */ | |
96 | uuid_generate(trace->uuid); | |
97 | if (init_trace_packet_header(trace)) { | |
83509119 | 98 | goto error; |
bc37ae52 JG |
99 | } |
100 | ||
7f800dc7 PP |
101 | /* Create the environment array object */ |
102 | trace->environment = bt_ctf_attributes_create(); | |
103 | if (!trace->environment) { | |
83509119 | 104 | goto error; |
7f800dc7 PP |
105 | } |
106 | ||
bc37ae52 JG |
107 | return trace; |
108 | ||
bc37ae52 | 109 | error: |
83509119 | 110 | BT_PUT(trace); |
bc37ae52 JG |
111 | return trace; |
112 | } | |
113 | ||
83509119 | 114 | void bt_ctf_trace_destroy(struct bt_object *obj) |
bc37ae52 JG |
115 | { |
116 | struct bt_ctf_trace *trace; | |
bc37ae52 | 117 | |
83509119 | 118 | trace = container_of(obj, struct bt_ctf_trace, base); |
bc37ae52 | 119 | if (trace->environment) { |
7f800dc7 | 120 | bt_ctf_attributes_destroy(trace->environment); |
bc37ae52 JG |
121 | } |
122 | ||
123 | if (trace->clocks) { | |
124 | g_ptr_array_free(trace->clocks, TRUE); | |
125 | } | |
126 | ||
127 | if (trace->streams) { | |
128 | g_ptr_array_free(trace->streams, TRUE); | |
129 | } | |
130 | ||
131 | if (trace->stream_classes) { | |
132 | g_ptr_array_free(trace->stream_classes, TRUE); | |
133 | } | |
134 | ||
83509119 | 135 | bt_put(trace->packet_header_type); |
bc37ae52 JG |
136 | g_free(trace); |
137 | } | |
138 | ||
139 | struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace, | |
140 | struct bt_ctf_stream_class *stream_class) | |
141 | { | |
142 | int ret; | |
143 | int stream_class_found = 0; | |
144 | size_t i; | |
145 | struct bt_ctf_stream *stream = NULL; | |
146 | ||
147 | if (!trace || !stream_class) { | |
148 | goto error; | |
149 | } | |
150 | ||
bc37ae52 JG |
151 | for (i = 0; i < trace->stream_classes->len; i++) { |
152 | if (trace->stream_classes->pdata[i] == stream_class) { | |
153 | stream_class_found = 1; | |
154 | } | |
155 | } | |
156 | ||
157 | if (!stream_class_found) { | |
ef0c4a15 JG |
158 | ret = bt_ctf_trace_add_stream_class(trace, stream_class); |
159 | if (ret) { | |
160 | goto error; | |
bc37ae52 | 161 | } |
bc37ae52 JG |
162 | } |
163 | ||
ae294457 JG |
164 | stream = bt_ctf_stream_create(stream_class, trace); |
165 | if (!stream) { | |
166 | goto error; | |
167 | } | |
168 | ||
bc37ae52 | 169 | g_ptr_array_add(trace->streams, stream); |
bc37ae52 | 170 | return stream; |
bc37ae52 | 171 | error: |
83509119 JG |
172 | BT_PUT(stream); |
173 | return stream; | |
bc37ae52 JG |
174 | } |
175 | ||
7f800dc7 | 176 | int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, |
dac5c838 | 177 | const char *name, struct bt_value *value) |
bc37ae52 | 178 | { |
bc37ae52 JG |
179 | int ret = 0; |
180 | ||
a0d12916 | 181 | if (!trace || !name || !value || |
7f800dc7 | 182 | bt_ctf_validate_identifier(name) || |
dac5c838 | 183 | !(bt_value_is_integer(value) || bt_value_is_string(value))) { |
bc37ae52 | 184 | ret = -1; |
7f800dc7 | 185 | goto end; |
bc37ae52 JG |
186 | } |
187 | ||
188 | if (strchr(name, ' ')) { | |
189 | ret = -1; | |
7f800dc7 | 190 | goto end; |
bc37ae52 JG |
191 | } |
192 | ||
a0d12916 JG |
193 | if (trace->frozen) { |
194 | /* | |
195 | * New environment fields may be added to a frozen trace, | |
196 | * but existing fields may not be changed. | |
197 | * | |
198 | * The object passed is frozen like all other attributes. | |
199 | */ | |
dac5c838 | 200 | struct bt_value *attribute = |
a0d12916 JG |
201 | bt_ctf_attributes_get_field_value_by_name( |
202 | trace->environment, name); | |
203 | ||
204 | if (attribute) { | |
83509119 | 205 | BT_PUT(attribute); |
a0d12916 JG |
206 | ret = -1; |
207 | goto end; | |
208 | } | |
209 | ||
dac5c838 | 210 | bt_value_freeze(value); |
a0d12916 JG |
211 | } |
212 | ||
7f800dc7 PP |
213 | ret = bt_ctf_attributes_set_field_value(trace->environment, name, |
214 | value); | |
bc37ae52 | 215 | |
7f800dc7 PP |
216 | end: |
217 | return ret; | |
218 | } | |
bc37ae52 | 219 | |
7f800dc7 PP |
220 | int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, |
221 | const char *name, const char *value) | |
222 | { | |
223 | int ret = 0; | |
dac5c838 | 224 | struct bt_value *env_value_string_obj = NULL; |
7f800dc7 PP |
225 | |
226 | if (!trace || !name || !value) { | |
bc37ae52 | 227 | ret = -1; |
7f800dc7 | 228 | goto end; |
bc37ae52 JG |
229 | } |
230 | ||
a0d12916 JG |
231 | if (trace->frozen) { |
232 | /* | |
233 | * New environment fields may be added to a frozen trace, | |
234 | * but existing fields may not be changed. | |
235 | */ | |
dac5c838 | 236 | struct bt_value *attribute = |
a0d12916 JG |
237 | bt_ctf_attributes_get_field_value_by_name( |
238 | trace->environment, name); | |
239 | ||
240 | if (attribute) { | |
83509119 | 241 | BT_PUT(attribute); |
a0d12916 JG |
242 | ret = -1; |
243 | goto end; | |
244 | } | |
245 | } | |
246 | ||
dac5c838 | 247 | env_value_string_obj = bt_value_string_create_init(value); |
bc37ae52 | 248 | |
7f800dc7 PP |
249 | if (!env_value_string_obj) { |
250 | ret = -1; | |
251 | goto end; | |
bc37ae52 JG |
252 | } |
253 | ||
a0d12916 | 254 | if (trace->frozen) { |
dac5c838 | 255 | bt_value_freeze(env_value_string_obj); |
a0d12916 | 256 | } |
7f800dc7 PP |
257 | ret = bt_ctf_trace_set_environment_field(trace, name, |
258 | env_value_string_obj); | |
259 | ||
260 | end: | |
83509119 | 261 | BT_PUT(env_value_string_obj); |
3487c9f3 JG |
262 | return ret; |
263 | } | |
264 | ||
7f800dc7 PP |
265 | int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace, |
266 | const char *name, int64_t value) | |
3487c9f3 | 267 | { |
3487c9f3 | 268 | int ret = 0; |
dac5c838 | 269 | struct bt_value *env_value_integer_obj = NULL; |
3487c9f3 JG |
270 | |
271 | if (!trace || !name) { | |
272 | ret = -1; | |
7f800dc7 | 273 | goto end; |
3487c9f3 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_integer_obj = bt_value_integer_create_init(value); |
7f800dc7 | 293 | if (!env_value_integer_obj) { |
3487c9f3 | 294 | ret = -1; |
7f800dc7 | 295 | goto end; |
3487c9f3 JG |
296 | } |
297 | ||
7f800dc7 PP |
298 | ret = bt_ctf_trace_set_environment_field(trace, name, |
299 | env_value_integer_obj); | |
a0d12916 | 300 | if (trace->frozen) { |
dac5c838 | 301 | bt_value_freeze(env_value_integer_obj); |
a0d12916 | 302 | } |
7f800dc7 | 303 | end: |
83509119 | 304 | BT_PUT(env_value_integer_obj); |
bc37ae52 JG |
305 | return ret; |
306 | } | |
307 | ||
e6fa2160 JG |
308 | int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
309 | { | |
310 | int ret = 0; | |
311 | ||
312 | if (!trace) { | |
313 | ret = -1; | |
314 | goto end; | |
315 | } | |
316 | ||
7f800dc7 | 317 | ret = bt_ctf_attributes_get_count(trace->environment); |
e6fa2160 | 318 | |
e6fa2160 | 319 | end: |
7f800dc7 | 320 | return ret; |
e6fa2160 JG |
321 | } |
322 | ||
323 | const char * | |
324 | bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace, | |
325 | int index) | |
326 | { | |
e6fa2160 JG |
327 | const char *ret = NULL; |
328 | ||
7f800dc7 | 329 | if (!trace) { |
e6fa2160 JG |
330 | goto end; |
331 | } | |
332 | ||
7f800dc7 PP |
333 | ret = bt_ctf_attributes_get_field_name(trace->environment, index); |
334 | ||
e6fa2160 JG |
335 | end: |
336 | return ret; | |
337 | } | |
338 | ||
dac5c838 | 339 | struct bt_value *bt_ctf_trace_get_environment_field_value( |
7f800dc7 | 340 | struct bt_ctf_trace *trace, int index) |
e6fa2160 | 341 | { |
dac5c838 | 342 | struct bt_value *ret = NULL; |
e6fa2160 | 343 | |
7f800dc7 | 344 | if (!trace) { |
e6fa2160 JG |
345 | goto end; |
346 | } | |
347 | ||
7f800dc7 PP |
348 | ret = bt_ctf_attributes_get_field_value(trace->environment, index); |
349 | ||
e6fa2160 JG |
350 | end: |
351 | return ret; | |
352 | } | |
353 | ||
dac5c838 | 354 | struct bt_value *bt_ctf_trace_get_environment_field_value_by_name( |
7f800dc7 | 355 | struct bt_ctf_trace *trace, const char *name) |
e6fa2160 | 356 | { |
dac5c838 | 357 | struct bt_value *ret = NULL; |
e6fa2160 | 358 | |
7f800dc7 | 359 | if (!trace || !name) { |
e6fa2160 JG |
360 | goto end; |
361 | } | |
362 | ||
7f800dc7 PP |
363 | ret = bt_ctf_attributes_get_field_value_by_name(trace->environment, |
364 | name); | |
365 | ||
e6fa2160 JG |
366 | end: |
367 | return ret; | |
368 | } | |
369 | ||
bc37ae52 JG |
370 | int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace, |
371 | struct bt_ctf_clock *clock) | |
372 | { | |
373 | int ret = 0; | |
374 | struct search_query query = { .value = clock, .found = 0 }; | |
375 | ||
376 | if (!trace || !clock) { | |
377 | ret = -1; | |
378 | goto end; | |
379 | } | |
380 | ||
381 | /* Check for duplicate clocks */ | |
382 | g_ptr_array_foreach(trace->clocks, value_exists, &query); | |
383 | if (query.found) { | |
384 | ret = -1; | |
385 | goto end; | |
386 | } | |
387 | ||
83509119 | 388 | bt_get(clock); |
bc37ae52 | 389 | g_ptr_array_add(trace->clocks, clock); |
6474e016 PP |
390 | |
391 | if (trace->frozen) { | |
392 | bt_ctf_clock_freeze(clock); | |
393 | } | |
bc37ae52 JG |
394 | end: |
395 | return ret; | |
396 | } | |
397 | ||
884cd6c3 JG |
398 | int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace) |
399 | { | |
400 | int ret = -1; | |
401 | ||
402 | if (!trace) { | |
403 | goto end; | |
404 | } | |
405 | ||
406 | ret = trace->clocks->len; | |
407 | end: | |
408 | return ret; | |
409 | } | |
410 | ||
411 | struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace, | |
412 | int index) | |
413 | { | |
414 | struct bt_ctf_clock *clock = NULL; | |
415 | ||
416 | if (!trace || index < 0 || index >= trace->clocks->len) { | |
417 | goto end; | |
418 | } | |
419 | ||
420 | clock = g_ptr_array_index(trace->clocks, index); | |
83509119 | 421 | bt_get(clock); |
884cd6c3 JG |
422 | end: |
423 | return clock; | |
424 | } | |
425 | ||
ef0c4a15 JG |
426 | int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, |
427 | struct bt_ctf_stream_class *stream_class) | |
428 | { | |
429 | int ret, i; | |
430 | int64_t stream_id; | |
09840de5 PP |
431 | struct bt_ctf_validation_output trace_sc_validation_output = { 0 }; |
432 | struct bt_ctf_validation_output *ec_validation_outputs = NULL; | |
433 | const enum bt_ctf_validation_flag trace_sc_validation_flags = | |
434 | BT_CTF_VALIDATION_FLAG_TRACE | | |
435 | BT_CTF_VALIDATION_FLAG_STREAM; | |
436 | const enum bt_ctf_validation_flag ec_validation_flags = | |
437 | BT_CTF_VALIDATION_FLAG_EVENT; | |
438 | struct bt_ctf_field_type *packet_header_type = NULL; | |
439 | struct bt_ctf_field_type *packet_context_type = NULL; | |
440 | struct bt_ctf_field_type *event_header_type = NULL; | |
441 | struct bt_ctf_field_type *stream_event_ctx_type = NULL; | |
442 | int event_class_count; | |
ef0c4a15 JG |
443 | |
444 | if (!trace || !stream_class) { | |
445 | ret = -1; | |
446 | goto end; | |
447 | } | |
448 | ||
09840de5 PP |
449 | event_class_count = |
450 | bt_ctf_stream_class_get_event_class_count(stream_class); | |
451 | assert(event_class_count >= 0); | |
452 | ||
453 | /* Check for duplicate stream classes */ | |
ef0c4a15 JG |
454 | for (i = 0; i < trace->stream_classes->len; i++) { |
455 | if (trace->stream_classes->pdata[i] == stream_class) { | |
09840de5 | 456 | /* Stream class already registered to the trace */ |
ef0c4a15 JG |
457 | ret = -1; |
458 | goto end; | |
459 | } | |
460 | } | |
461 | ||
09840de5 PP |
462 | /* |
463 | * We're about to freeze both the trace and the stream class. | |
464 | * Also, each event class contained in this stream class are | |
465 | * already frozen. | |
466 | * | |
467 | * This trace, this stream class, and all its event classes | |
468 | * should be valid at this point. | |
469 | * | |
470 | * Validate trace and stream class first, then each event | |
471 | * class of this stream class can be validated individually. | |
472 | */ | |
473 | packet_header_type = | |
474 | bt_ctf_trace_get_packet_header_type(trace); | |
475 | packet_context_type = | |
476 | bt_ctf_stream_class_get_packet_context_type(stream_class); | |
477 | event_header_type = | |
478 | bt_ctf_stream_class_get_event_header_type(stream_class); | |
479 | stream_event_ctx_type = | |
480 | bt_ctf_stream_class_get_event_context_type(stream_class); | |
481 | ret = bt_ctf_validate_class_types(trace->environment, | |
482 | packet_header_type, packet_context_type, event_header_type, | |
483 | stream_event_ctx_type, NULL, NULL, trace->valid, | |
484 | stream_class->valid, 1, &trace_sc_validation_output, | |
485 | trace_sc_validation_flags); | |
486 | BT_PUT(packet_header_type); | |
487 | BT_PUT(packet_context_type); | |
488 | BT_PUT(event_header_type); | |
489 | BT_PUT(stream_event_ctx_type); | |
490 | ||
2bd7f864 | 491 | if (ret) { |
09840de5 PP |
492 | /* |
493 | * This means something went wrong during the validation | |
494 | * process, not that the objects are invalid. | |
495 | */ | |
2bd7f864 JG |
496 | goto end; |
497 | } | |
498 | ||
09840de5 PP |
499 | if ((trace_sc_validation_output.valid_flags & |
500 | trace_sc_validation_flags) != | |
501 | trace_sc_validation_flags) { | |
502 | /* Invalid trace/stream class */ | |
503 | ret = -1; | |
504 | goto end; | |
505 | } | |
506 | ||
507 | if (event_class_count > 0) { | |
508 | ec_validation_outputs = g_new0(struct bt_ctf_validation_output, | |
509 | event_class_count); | |
510 | if (!ec_validation_outputs) { | |
511 | ret = -1; | |
512 | goto end; | |
513 | } | |
514 | } | |
515 | ||
516 | /* Validate each event class individually */ | |
6474e016 | 517 | for (i = 0; i < event_class_count; i++) { |
09840de5 PP |
518 | struct bt_ctf_event_class *event_class = |
519 | bt_ctf_stream_class_get_event_class(stream_class, i); | |
520 | struct bt_ctf_field_type *event_context_type = NULL; | |
521 | struct bt_ctf_field_type *event_payload_type = NULL; | |
522 | ||
523 | event_context_type = | |
524 | bt_ctf_event_class_get_context_type(event_class); | |
525 | event_payload_type = | |
526 | bt_ctf_event_class_get_payload_type(event_class); | |
527 | ||
528 | /* | |
529 | * It is important to use the field types returned by | |
530 | * the previous trace and stream class validation here | |
531 | * because copies could have been made. | |
532 | */ | |
533 | ret = bt_ctf_validate_class_types(trace->environment, | |
534 | trace_sc_validation_output.packet_header_type, | |
535 | trace_sc_validation_output.packet_context_type, | |
536 | trace_sc_validation_output.event_header_type, | |
537 | trace_sc_validation_output.stream_event_ctx_type, | |
538 | event_context_type, event_payload_type, | |
539 | 1, 1, event_class->valid, &ec_validation_outputs[i], | |
540 | ec_validation_flags); | |
541 | BT_PUT(event_context_type); | |
542 | BT_PUT(event_payload_type); | |
543 | BT_PUT(event_class); | |
544 | ||
545 | if (ret) { | |
546 | goto end; | |
547 | } | |
548 | ||
549 | if ((ec_validation_outputs[i].valid_flags & | |
550 | ec_validation_flags) != ec_validation_flags) { | |
551 | /* Invalid event class */ | |
552 | ret = -1; | |
553 | goto end; | |
554 | } | |
555 | } | |
556 | ||
ef0c4a15 JG |
557 | stream_id = bt_ctf_stream_class_get_id(stream_class); |
558 | if (stream_id < 0) { | |
559 | stream_id = trace->next_stream_id++; | |
560 | ||
561 | /* Try to assign a new stream id */ | |
562 | for (i = 0; i < trace->stream_classes->len; i++) { | |
563 | if (stream_id == bt_ctf_stream_class_get_id( | |
564 | trace->stream_classes->pdata[i])) { | |
565 | /* Duplicate stream id found */ | |
566 | ret = -1; | |
567 | goto end; | |
568 | } | |
569 | } | |
570 | ||
0ddffae5 JG |
571 | if (bt_ctf_stream_class_set_id_no_check(stream_class, |
572 | stream_id)) { | |
ef0c4a15 JG |
573 | /* TODO Should retry with a different stream id */ |
574 | ret = -1; | |
575 | goto end; | |
576 | } | |
577 | } | |
578 | ||
e6a8e8e4 | 579 | bt_object_set_parent(stream_class, trace); |
ef0c4a15 JG |
580 | g_ptr_array_add(trace->stream_classes, stream_class); |
581 | ||
582 | /* | |
09840de5 PP |
583 | * At this point we know that the function will be successful. |
584 | * Therefore we can replace the trace and stream class field | |
585 | * types with what's in their validation output structure and | |
586 | * mark them as valid. We can also replace the field types of | |
587 | * all the event classes of the stream class and mark them as | |
588 | * valid. | |
589 | */ | |
590 | bt_ctf_validation_replace_types(trace, stream_class, NULL, | |
591 | &trace_sc_validation_output, trace_sc_validation_flags); | |
592 | trace->valid = 1; | |
593 | stream_class->valid = 1; | |
594 | ||
595 | /* | |
596 | * Put what was not moved in bt_ctf_validation_replace_types(). | |
597 | */ | |
598 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
599 | ||
6474e016 | 600 | for (i = 0; i < event_class_count; i++) { |
09840de5 PP |
601 | struct bt_ctf_event_class *event_class = |
602 | bt_ctf_stream_class_get_event_class(stream_class, i); | |
603 | ||
604 | bt_ctf_validation_replace_types(NULL, NULL, event_class, | |
605 | &ec_validation_outputs[i], ec_validation_flags); | |
606 | event_class->valid = 1; | |
607 | BT_PUT(event_class); | |
608 | ||
609 | /* | |
610 | * Put what was not moved in | |
611 | * bt_ctf_validation_replace_types(). | |
612 | */ | |
613 | bt_ctf_validation_output_put_types(&ec_validation_outputs[i]); | |
614 | } | |
615 | ||
616 | /* | |
ef0c4a15 JG |
617 | * All field type byte orders set as "native" byte ordering can now be |
618 | * safely set to trace's own endianness, including the stream class'. | |
619 | */ | |
620 | bt_ctf_field_type_set_native_byte_order(trace->packet_header_type, | |
621 | trace->byte_order); | |
445c3471 | 622 | bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order); |
ef0c4a15 | 623 | |
09840de5 PP |
624 | /* |
625 | * Freeze the trace and the stream class. | |
626 | */ | |
f67f3a6e | 627 | bt_ctf_stream_class_freeze(stream_class); |
09840de5 PP |
628 | bt_ctf_trace_freeze(trace); |
629 | ||
ef0c4a15 | 630 | end: |
d3814b54 | 631 | if (ret) { |
e6a8e8e4 | 632 | bt_object_set_parent(stream_class, NULL); |
09840de5 PP |
633 | |
634 | if (ec_validation_outputs) { | |
6474e016 | 635 | for (i = 0; i < event_class_count; i++) { |
09840de5 PP |
636 | bt_ctf_validation_output_put_types( |
637 | &ec_validation_outputs[i]); | |
638 | } | |
639 | } | |
d3814b54 | 640 | } |
09840de5 PP |
641 | |
642 | g_free(ec_validation_outputs); | |
643 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
644 | assert(!packet_header_type); | |
645 | assert(!packet_context_type); | |
646 | assert(!event_header_type); | |
647 | assert(!stream_event_ctx_type); | |
648 | ||
ef0c4a15 JG |
649 | return ret; |
650 | } | |
651 | ||
652 | int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) | |
653 | { | |
654 | int ret; | |
655 | ||
656 | if (!trace) { | |
657 | ret = -1; | |
658 | goto end; | |
659 | } | |
660 | ||
661 | ret = trace->stream_classes->len; | |
662 | end: | |
663 | return ret; | |
664 | } | |
665 | ||
666 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class( | |
667 | struct bt_ctf_trace *trace, int index) | |
668 | { | |
669 | struct bt_ctf_stream_class *stream_class = NULL; | |
670 | ||
671 | if (!trace || index < 0 || index >= trace->stream_classes->len) { | |
672 | goto end; | |
673 | } | |
674 | ||
675 | stream_class = g_ptr_array_index(trace->stream_classes, index); | |
83509119 | 676 | bt_get(stream_class); |
ef0c4a15 JG |
677 | end: |
678 | return stream_class; | |
679 | } | |
680 | ||
4841ccc1 PP |
681 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id( |
682 | struct bt_ctf_trace *trace, uint32_t id) | |
683 | { | |
684 | int i; | |
685 | struct bt_ctf_stream_class *stream_class = NULL; | |
686 | ||
687 | if (!trace) { | |
688 | goto end; | |
689 | } | |
690 | ||
6474e016 | 691 | for (i = 0; i < trace->stream_classes->len; i++) { |
4841ccc1 PP |
692 | struct bt_ctf_stream_class *stream_class_candidate; |
693 | ||
694 | stream_class_candidate = | |
695 | g_ptr_array_index(trace->stream_classes, i); | |
696 | ||
697 | if (bt_ctf_stream_class_get_id(stream_class_candidate) == | |
698 | (int64_t) id) { | |
699 | stream_class = stream_class_candidate; | |
83509119 | 700 | bt_get(stream_class); |
4841ccc1 PP |
701 | goto end; |
702 | } | |
703 | } | |
704 | ||
705 | end: | |
706 | return stream_class; | |
707 | } | |
708 | ||
7e4347a3 PP |
709 | struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name( |
710 | struct bt_ctf_trace *trace, const char *name) | |
711 | { | |
712 | size_t i; | |
713 | struct bt_ctf_clock *clock = NULL; | |
714 | ||
715 | if (!trace || !name) { | |
716 | goto end; | |
717 | } | |
718 | ||
6474e016 | 719 | for (i = 0; i < trace->clocks->len; i++) { |
7e4347a3 PP |
720 | struct bt_ctf_clock *cur_clk = |
721 | g_ptr_array_index(trace->clocks, i); | |
722 | const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk); | |
723 | ||
724 | if (!cur_clk_name) { | |
725 | goto end; | |
726 | } | |
727 | ||
728 | if (!strcmp(cur_clk_name, name)) { | |
729 | clock = cur_clk; | |
83509119 | 730 | bt_get(clock); |
7e4347a3 PP |
731 | goto end; |
732 | } | |
733 | } | |
734 | ||
735 | end: | |
736 | return clock; | |
737 | } | |
738 | ||
bc37ae52 JG |
739 | BT_HIDDEN |
740 | const char *get_byte_order_string(int byte_order) | |
741 | { | |
742 | const char *string; | |
743 | ||
744 | switch (byte_order) { | |
745 | case LITTLE_ENDIAN: | |
746 | string = "le"; | |
747 | break; | |
748 | case BIG_ENDIAN: | |
749 | string = "be"; | |
750 | break; | |
751 | default: | |
752 | string = "unknown"; | |
753 | break; | |
754 | } | |
755 | ||
756 | return string; | |
757 | } | |
758 | ||
759 | static | |
760 | int append_trace_metadata(struct bt_ctf_trace *trace, | |
761 | struct metadata_context *context) | |
762 | { | |
763 | unsigned char *uuid = trace->uuid; | |
764 | int ret; | |
765 | ||
766 | g_string_append(context->string, "trace {\n"); | |
767 | ||
768 | g_string_append(context->string, "\tmajor = 1;\n"); | |
769 | g_string_append(context->string, "\tminor = 8;\n"); | |
770 | ||
771 | g_string_append_printf(context->string, | |
772 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
773 | uuid[0], uuid[1], uuid[2], uuid[3], | |
774 | uuid[4], uuid[5], uuid[6], uuid[7], | |
775 | uuid[8], uuid[9], uuid[10], uuid[11], | |
776 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
777 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", | |
778 | get_byte_order_string(trace->byte_order)); | |
779 | ||
780 | g_string_append(context->string, "\tpacket.header := "); | |
781 | context->current_indentation_level++; | |
782 | g_string_assign(context->field_name, ""); | |
d246b111 | 783 | ret = bt_ctf_field_type_serialize(trace->packet_header_type, |
bc37ae52 JG |
784 | context); |
785 | if (ret) { | |
786 | goto end; | |
787 | } | |
788 | context->current_indentation_level--; | |
789 | ||
790 | g_string_append(context->string, ";\n};\n\n"); | |
791 | end: | |
792 | return ret; | |
793 | } | |
794 | ||
bc37ae52 JG |
795 | static |
796 | void append_env_metadata(struct bt_ctf_trace *trace, | |
797 | struct metadata_context *context) | |
798 | { | |
7f800dc7 PP |
799 | int i; |
800 | int env_size; | |
801 | ||
802 | env_size = bt_ctf_attributes_get_count(trace->environment); | |
803 | ||
804 | if (env_size <= 0) { | |
bc37ae52 JG |
805 | return; |
806 | } | |
807 | ||
808 | g_string_append(context->string, "env {\n"); | |
7f800dc7 | 809 | |
6474e016 | 810 | for (i = 0; i < env_size; i++) { |
dac5c838 | 811 | struct bt_value *env_field_value_obj = NULL; |
7f800dc7 | 812 | const char *entry_name; |
7f800dc7 PP |
813 | |
814 | entry_name = bt_ctf_attributes_get_field_name( | |
815 | trace->environment, i); | |
816 | env_field_value_obj = bt_ctf_attributes_get_field_value( | |
817 | trace->environment, i); | |
818 | ||
819 | if (!entry_name || !env_field_value_obj) { | |
820 | goto loop_next; | |
821 | } | |
822 | ||
dac5c838 PP |
823 | switch (bt_value_get_type(env_field_value_obj)) { |
824 | case BT_VALUE_TYPE_INTEGER: | |
b8248cc0 PP |
825 | { |
826 | int ret; | |
827 | int64_t int_value; | |
828 | ||
dac5c838 | 829 | ret = bt_value_integer_get(env_field_value_obj, |
7f800dc7 PP |
830 | &int_value); |
831 | ||
832 | if (ret) { | |
833 | goto loop_next; | |
834 | } | |
835 | ||
836 | g_string_append_printf(context->string, | |
837 | "\t%s = %" PRId64 ";\n", entry_name, | |
838 | int_value); | |
839 | break; | |
b8248cc0 | 840 | } |
dac5c838 | 841 | case BT_VALUE_TYPE_STRING: |
7f800dc7 PP |
842 | { |
843 | int ret; | |
844 | const char *str_value; | |
845 | char *escaped_str = NULL; | |
846 | ||
dac5c838 | 847 | ret = bt_value_string_get(env_field_value_obj, |
7f800dc7 PP |
848 | &str_value); |
849 | ||
850 | if (ret) { | |
851 | goto loop_next; | |
852 | } | |
853 | ||
854 | escaped_str = g_strescape(str_value, NULL); | |
855 | ||
856 | if (!escaped_str) { | |
857 | goto loop_next; | |
858 | } | |
859 | ||
860 | g_string_append_printf(context->string, | |
861 | "\t%s = \"%s\";\n", entry_name, escaped_str); | |
862 | free(escaped_str); | |
863 | break; | |
864 | } | |
865 | ||
866 | default: | |
867 | goto loop_next; | |
868 | } | |
869 | ||
870 | loop_next: | |
83509119 | 871 | BT_PUT(env_field_value_obj); |
7f800dc7 PP |
872 | } |
873 | ||
bc37ae52 JG |
874 | g_string_append(context->string, "};\n\n"); |
875 | } | |
876 | ||
877 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) | |
878 | { | |
879 | char *metadata = NULL; | |
880 | struct metadata_context *context = NULL; | |
881 | int err = 0; | |
882 | size_t i; | |
883 | ||
884 | if (!trace) { | |
885 | goto end; | |
886 | } | |
887 | ||
888 | context = g_new0(struct metadata_context, 1); | |
889 | if (!context) { | |
890 | goto end; | |
891 | } | |
892 | ||
893 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); | |
894 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); | |
895 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); | |
896 | if (append_trace_metadata(trace, context)) { | |
897 | goto error; | |
898 | } | |
899 | append_env_metadata(trace, context); | |
900 | g_ptr_array_foreach(trace->clocks, | |
901 | (GFunc)bt_ctf_clock_serialize, context); | |
902 | ||
903 | for (i = 0; i < trace->stream_classes->len; i++) { | |
904 | err = bt_ctf_stream_class_serialize( | |
905 | trace->stream_classes->pdata[i], context); | |
906 | if (err) { | |
907 | goto error; | |
908 | } | |
909 | } | |
910 | ||
911 | metadata = context->string->str; | |
912 | error: | |
913 | g_string_free(context->string, err ? TRUE : FALSE); | |
914 | g_string_free(context->field_name, TRUE); | |
915 | g_free(context); | |
916 | end: | |
917 | return metadata; | |
918 | } | |
919 | ||
4ed90fb3 JG |
920 | enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace) |
921 | { | |
922 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; | |
923 | ||
924 | if (!trace) { | |
925 | goto end; | |
926 | } | |
927 | ||
928 | switch (trace->byte_order) { | |
929 | case BIG_ENDIAN: | |
930 | ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; | |
931 | break; | |
932 | case LITTLE_ENDIAN: | |
933 | ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; | |
934 | break; | |
935 | default: | |
936 | break; | |
937 | } | |
938 | end: | |
939 | return ret; | |
940 | } | |
941 | ||
bc37ae52 JG |
942 | int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, |
943 | enum bt_ctf_byte_order byte_order) | |
944 | { | |
945 | int ret = 0; | |
946 | int internal_byte_order; | |
947 | ||
948 | if (!trace || trace->frozen) { | |
949 | ret = -1; | |
950 | goto end; | |
951 | } | |
952 | ||
953 | switch (byte_order) { | |
954 | case BT_CTF_BYTE_ORDER_NATIVE: | |
c35a1669 JG |
955 | /* |
956 | * This doesn't make sense since the CTF specification defines | |
957 | * the "native" byte order as "the byte order described in the | |
958 | * trace description". However, this behavior had been | |
959 | * implemented as part of v1.2 and is kept to maintain | |
960 | * compatibility. | |
961 | * | |
962 | * This may be changed on a major version bump only. | |
963 | */ | |
964 | internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? | |
bc37ae52 JG |
965 | LITTLE_ENDIAN : BIG_ENDIAN; |
966 | break; | |
967 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: | |
968 | internal_byte_order = LITTLE_ENDIAN; | |
969 | break; | |
970 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: | |
971 | case BT_CTF_BYTE_ORDER_NETWORK: | |
972 | internal_byte_order = BIG_ENDIAN; | |
973 | break; | |
974 | default: | |
975 | ret = -1; | |
976 | goto end; | |
977 | } | |
978 | ||
979 | trace->byte_order = internal_byte_order; | |
bc37ae52 JG |
980 | end: |
981 | return ret; | |
982 | } | |
983 | ||
d246b111 JG |
984 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( |
985 | struct bt_ctf_trace *trace) | |
986 | { | |
987 | struct bt_ctf_field_type *field_type = NULL; | |
988 | ||
989 | if (!trace) { | |
990 | goto end; | |
991 | } | |
992 | ||
83509119 | 993 | bt_get(trace->packet_header_type); |
d246b111 JG |
994 | field_type = trace->packet_header_type; |
995 | end: | |
996 | return field_type; | |
997 | } | |
998 | ||
999 | int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, | |
1000 | struct bt_ctf_field_type *packet_header_type) | |
1001 | { | |
1002 | int ret = 0; | |
1003 | ||
1004 | if (!trace || !packet_header_type || trace->frozen) { | |
1005 | ret = -1; | |
1006 | goto end; | |
1007 | } | |
1008 | ||
1009 | /* packet_header_type must be a structure */ | |
1010 | if (bt_ctf_field_type_get_type_id(packet_header_type) != | |
9a19a512 | 1011 | BT_CTF_TYPE_ID_STRUCT) { |
d246b111 JG |
1012 | ret = -1; |
1013 | goto end; | |
1014 | } | |
1015 | ||
83509119 JG |
1016 | bt_get(packet_header_type); |
1017 | bt_put(trace->packet_header_type); | |
d246b111 JG |
1018 | trace->packet_header_type = packet_header_type; |
1019 | end: | |
1020 | return ret; | |
1021 | } | |
1022 | ||
bc37ae52 JG |
1023 | void bt_ctf_trace_get(struct bt_ctf_trace *trace) |
1024 | { | |
83509119 | 1025 | bt_get(trace); |
bc37ae52 JG |
1026 | } |
1027 | ||
1028 | void bt_ctf_trace_put(struct bt_ctf_trace *trace) | |
1029 | { | |
83509119 | 1030 | bt_put(trace); |
bc37ae52 | 1031 | |
bc37ae52 JG |
1032 | } |
1033 | ||
bc37ae52 JG |
1034 | BT_HIDDEN |
1035 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) | |
1036 | { | |
1037 | unsigned int alignment, size; | |
8bdcb82e | 1038 | struct bt_ctf_field_type *field_type = NULL; |
bc37ae52 JG |
1039 | |
1040 | if (alias >= NR_FIELD_TYPE_ALIAS) { | |
8bdcb82e | 1041 | goto end; |
bc37ae52 JG |
1042 | } |
1043 | ||
1044 | alignment = field_type_aliases_alignments[alias]; | |
1045 | size = field_type_aliases_sizes[alias]; | |
1046 | field_type = bt_ctf_field_type_integer_create(size); | |
1047 | bt_ctf_field_type_set_alignment(field_type, alignment); | |
8bdcb82e | 1048 | end: |
bc37ae52 JG |
1049 | return field_type; |
1050 | } | |
1051 | ||
f67f3a6e | 1052 | static |
09840de5 | 1053 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace) |
f67f3a6e | 1054 | { |
6474e016 PP |
1055 | int i; |
1056 | ||
09840de5 | 1057 | bt_ctf_field_type_freeze(trace->packet_header_type); |
f67f3a6e | 1058 | bt_ctf_attributes_freeze(trace->environment); |
6474e016 PP |
1059 | |
1060 | for (i = 0; i < trace->clocks->len; i++) { | |
1061 | struct bt_ctf_clock *clock = | |
1062 | g_ptr_array_index(trace->clocks, i); | |
1063 | ||
1064 | bt_ctf_clock_freeze(clock); | |
1065 | } | |
1066 | ||
f67f3a6e JG |
1067 | trace->frozen = 1; |
1068 | } | |
1069 | ||
bc37ae52 JG |
1070 | static |
1071 | int init_trace_packet_header(struct bt_ctf_trace *trace) | |
1072 | { | |
bc37ae52 | 1073 | int ret = 0; |
d246b111 | 1074 | struct bt_ctf_field *magic = NULL, *uuid_array = NULL; |
bc37ae52 JG |
1075 | struct bt_ctf_field_type *_uint32_t = |
1076 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
1077 | struct bt_ctf_field_type *_uint8_t = | |
1078 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); | |
1079 | struct bt_ctf_field_type *trace_packet_header_type = | |
1080 | bt_ctf_field_type_structure_create(); | |
1081 | struct bt_ctf_field_type *uuid_array_type = | |
1082 | bt_ctf_field_type_array_create(_uint8_t, 16); | |
1083 | ||
1084 | if (!trace_packet_header_type || !uuid_array_type) { | |
1085 | ret = -1; | |
1086 | goto end; | |
1087 | } | |
1088 | ||
bc37ae52 JG |
1089 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
1090 | _uint32_t, "magic"); | |
1091 | if (ret) { | |
1092 | goto end; | |
1093 | } | |
1094 | ||
1095 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
1096 | uuid_array_type, "uuid"); | |
1097 | if (ret) { | |
1098 | goto end; | |
1099 | } | |
1100 | ||
1101 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
1102 | _uint32_t, "stream_id"); | |
1103 | if (ret) { | |
1104 | goto end; | |
1105 | } | |
1106 | ||
662e778c JG |
1107 | ret = bt_ctf_trace_set_packet_header_type(trace, |
1108 | trace_packet_header_type); | |
1109 | if (ret) { | |
1110 | goto end; | |
1111 | } | |
bc37ae52 | 1112 | end: |
83509119 JG |
1113 | bt_put(uuid_array_type); |
1114 | bt_put(_uint32_t); | |
1115 | bt_put(_uint8_t); | |
1116 | bt_put(magic); | |
1117 | bt_put(uuid_array); | |
1118 | bt_put(trace_packet_header_type); | |
bc37ae52 JG |
1119 | return ret; |
1120 | } |