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