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> | |
33 | #include <babeltrace/ctf-writer/functor-internal.h> | |
34 | #include <babeltrace/ctf-ir/event-types-internal.h> | |
654c1444 | 35 | #include <babeltrace/ctf-ir/utils.h> |
bc37ae52 JG |
36 | #include <babeltrace/compiler.h> |
37 | ||
38 | #define DEFAULT_IDENTIFIER_SIZE 128 | |
39 | #define DEFAULT_METADATA_STRING_SIZE 4096 | |
40 | ||
41 | static | |
42 | void environment_variable_destroy(struct environment_variable *var); | |
43 | static | |
44 | void bt_ctf_trace_destroy(struct bt_ctf_ref *ref); | |
45 | static | |
46 | int init_trace_packet_header(struct bt_ctf_trace *trace); | |
47 | ||
bc37ae52 JG |
48 | static |
49 | const unsigned int field_type_aliases_alignments[] = { | |
50 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, | |
51 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, | |
52 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, | |
53 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, | |
54 | }; | |
55 | ||
56 | static | |
57 | const unsigned int field_type_aliases_sizes[] = { | |
58 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, | |
59 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, | |
60 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, | |
61 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, | |
62 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, | |
63 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, | |
64 | }; | |
65 | ||
bc37ae52 JG |
66 | struct bt_ctf_trace *bt_ctf_trace_create(void) |
67 | { | |
68 | struct bt_ctf_trace *trace = NULL; | |
69 | ||
70 | trace = g_new0(struct bt_ctf_trace, 1); | |
71 | if (!trace) { | |
72 | goto error; | |
73 | } | |
74 | ||
75 | bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE); | |
76 | bt_ctf_ref_init(&trace->ref_count); | |
77 | trace->environment = g_ptr_array_new_with_free_func( | |
78 | (GDestroyNotify)environment_variable_destroy); | |
79 | trace->clocks = g_ptr_array_new_with_free_func( | |
80 | (GDestroyNotify)bt_ctf_clock_put); | |
81 | trace->streams = g_ptr_array_new_with_free_func( | |
82 | (GDestroyNotify)bt_ctf_stream_put); | |
83 | trace->stream_classes = g_ptr_array_new_with_free_func( | |
84 | (GDestroyNotify)bt_ctf_stream_class_put); | |
85 | if (!trace->environment || !trace->clocks || | |
86 | !trace->stream_classes || !trace->streams) { | |
87 | goto error_destroy; | |
88 | } | |
89 | ||
90 | /* Generate a trace UUID */ | |
91 | uuid_generate(trace->uuid); | |
92 | if (init_trace_packet_header(trace)) { | |
93 | goto error_destroy; | |
94 | } | |
95 | ||
96 | return trace; | |
97 | ||
98 | error_destroy: | |
99 | bt_ctf_trace_destroy(&trace->ref_count); | |
100 | trace = NULL; | |
101 | error: | |
102 | return trace; | |
103 | } | |
104 | ||
105 | void bt_ctf_trace_destroy(struct bt_ctf_ref *ref) | |
106 | { | |
107 | struct bt_ctf_trace *trace; | |
108 | ||
109 | if (!ref) { | |
110 | return; | |
111 | } | |
112 | ||
113 | trace = container_of(ref, struct bt_ctf_trace, ref_count); | |
114 | if (trace->environment) { | |
115 | g_ptr_array_free(trace->environment, TRUE); | |
116 | } | |
117 | ||
118 | if (trace->clocks) { | |
119 | g_ptr_array_free(trace->clocks, TRUE); | |
120 | } | |
121 | ||
122 | if (trace->streams) { | |
123 | g_ptr_array_free(trace->streams, TRUE); | |
124 | } | |
125 | ||
126 | if (trace->stream_classes) { | |
127 | g_ptr_array_free(trace->stream_classes, TRUE); | |
128 | } | |
129 | ||
d246b111 | 130 | bt_ctf_field_type_put(trace->packet_header_type); |
bc37ae52 JG |
131 | g_free(trace); |
132 | } | |
133 | ||
134 | struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace, | |
135 | struct bt_ctf_stream_class *stream_class) | |
136 | { | |
137 | int ret; | |
138 | int stream_class_found = 0; | |
139 | size_t i; | |
140 | struct bt_ctf_stream *stream = NULL; | |
141 | ||
142 | if (!trace || !stream_class) { | |
143 | goto error; | |
144 | } | |
145 | ||
d246b111 | 146 | stream = bt_ctf_stream_create(stream_class, trace); |
bc37ae52 JG |
147 | if (!stream) { |
148 | goto error; | |
149 | } | |
150 | ||
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) { | |
158 | int64_t stream_id = bt_ctf_stream_class_get_id(stream_class); | |
159 | ||
160 | if (stream_id < 0) { | |
161 | /* Try to assign a new stream id */ | |
5ca83563 | 162 | if (_bt_ctf_stream_class_set_id(stream->stream_class, |
bc37ae52 JG |
163 | trace->next_stream_id++)) { |
164 | goto error; | |
165 | } | |
166 | } | |
167 | ||
168 | for (i = 0; i < trace->stream_classes->len; i++) { | |
169 | if (stream_id == bt_ctf_stream_class_get_id( | |
170 | trace->stream_classes->pdata[i])) { | |
171 | /* Duplicate stream id found */ | |
172 | goto error; | |
173 | } | |
174 | } | |
175 | bt_ctf_stream_class_get(stream->stream_class); | |
176 | g_ptr_array_add(trace->stream_classes, stream->stream_class); | |
177 | } | |
178 | ||
179 | bt_ctf_stream_get(stream); | |
180 | g_ptr_array_add(trace->streams, stream); | |
c35a1669 JG |
181 | |
182 | /* | |
183 | * Freeze the trace and its packet header. | |
184 | * | |
185 | * All field type byte orders set as "native" byte ordering can now be | |
186 | * safely set to trace's own endianness, including the stream class'. | |
187 | */ | |
188 | bt_ctf_field_type_set_native_byte_order(trace->packet_header_type, | |
189 | trace->byte_order); | |
190 | ret = bt_ctf_stream_class_set_byte_order(stream_class, | |
191 | trace->byte_order == LITTLE_ENDIAN ? | |
192 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : BT_CTF_BYTE_ORDER_BIG_ENDIAN); | |
193 | if (ret) { | |
194 | goto error; | |
195 | } | |
196 | ||
29d9d76c | 197 | bt_ctf_stream_class_freeze(stream_class); |
bc37ae52 JG |
198 | trace->frozen = 1; |
199 | return stream; | |
bc37ae52 JG |
200 | error: |
201 | bt_ctf_stream_put(stream); | |
202 | return NULL; | |
203 | } | |
204 | ||
205 | int bt_ctf_trace_add_environment_field(struct bt_ctf_trace *trace, | |
206 | const char *name, | |
207 | const char *value) | |
208 | { | |
209 | struct environment_variable *var = NULL; | |
210 | char *escaped_value = NULL; | |
211 | int ret = 0; | |
212 | ||
654c1444 | 213 | if (!trace || !name || !value || bt_ctf_validate_identifier(name)) { |
bc37ae52 JG |
214 | ret = -1; |
215 | goto error; | |
216 | } | |
217 | ||
218 | if (strchr(name, ' ')) { | |
219 | ret = -1; | |
220 | goto error; | |
221 | } | |
222 | ||
223 | var = g_new0(struct environment_variable, 1); | |
224 | if (!var) { | |
225 | ret = -1; | |
226 | goto error; | |
227 | } | |
228 | ||
3487c9f3 | 229 | var->type = BT_ENVIRONMENT_FIELD_TYPE_STRING; |
bc37ae52 JG |
230 | escaped_value = g_strescape(value, NULL); |
231 | if (!escaped_value) { | |
232 | ret = -1; | |
233 | goto error; | |
234 | } | |
235 | ||
236 | var->name = g_string_new(name); | |
3487c9f3 | 237 | var->value.string = g_string_new(escaped_value); |
bc37ae52 | 238 | g_free(escaped_value); |
3487c9f3 | 239 | if (!var->name || !var->value.string) { |
bc37ae52 JG |
240 | ret = -1; |
241 | goto error; | |
242 | } | |
243 | ||
244 | g_ptr_array_add(trace->environment, var); | |
245 | return ret; | |
246 | ||
247 | error: | |
248 | if (var && var->name) { | |
249 | g_string_free(var->name, TRUE); | |
250 | } | |
251 | ||
3487c9f3 JG |
252 | if (var && var->value.string) { |
253 | g_string_free(var->value.string, TRUE); | |
254 | } | |
255 | ||
256 | g_free(var); | |
257 | return ret; | |
258 | } | |
259 | ||
260 | int bt_ctf_trace_add_environment_field_integer(struct bt_ctf_trace *trace, | |
261 | const char *name, | |
e6fa2160 | 262 | int64_t value) |
3487c9f3 JG |
263 | { |
264 | struct environment_variable *var = NULL; | |
265 | int ret = 0; | |
266 | ||
267 | if (!trace || !name) { | |
268 | ret = -1; | |
269 | goto error; | |
270 | } | |
271 | ||
272 | var = g_new0(struct environment_variable, 1); | |
273 | if (!var) { | |
274 | ret = -1; | |
275 | goto error; | |
276 | } | |
277 | ||
278 | var->type = BT_ENVIRONMENT_FIELD_TYPE_INTEGER; | |
279 | var->name = g_string_new(name); | |
280 | var->value.integer = value; | |
281 | if (!var->name) { | |
282 | ret = -1; | |
283 | goto error; | |
284 | } | |
285 | ||
286 | g_ptr_array_add(trace->environment, var); | |
287 | return ret; | |
288 | ||
289 | error: | |
290 | if (var && var->name) { | |
291 | g_string_free(var->name, TRUE); | |
bc37ae52 JG |
292 | } |
293 | ||
294 | g_free(var); | |
295 | return ret; | |
296 | } | |
297 | ||
e6fa2160 JG |
298 | int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
299 | { | |
300 | int ret = 0; | |
301 | ||
302 | if (!trace) { | |
303 | ret = -1; | |
304 | goto end; | |
305 | } | |
306 | ||
307 | ret = trace->environment->len; | |
308 | end: | |
309 | return ret; | |
310 | } | |
311 | ||
312 | enum bt_environment_field_type | |
313 | bt_ctf_trace_get_environment_field_type(struct bt_ctf_trace *trace, int index) | |
314 | { | |
315 | struct environment_variable *var; | |
316 | enum bt_environment_field_type type = BT_ENVIRONMENT_FIELD_TYPE_UNKNOWN; | |
317 | ||
318 | if (!trace || index < 0 || index >= trace->environment->len) { | |
319 | goto end; | |
320 | } | |
321 | ||
322 | var = g_ptr_array_index(trace->environment, index); | |
323 | type = var->type; | |
324 | end: | |
325 | return type; | |
326 | } | |
327 | ||
328 | const char * | |
329 | bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace, | |
330 | int index) | |
331 | { | |
332 | struct environment_variable *var; | |
333 | const char *ret = NULL; | |
334 | ||
335 | if (!trace || index < 0 || index >= trace->environment->len) { | |
336 | goto end; | |
337 | } | |
338 | ||
339 | var = g_ptr_array_index(trace->environment, index); | |
340 | ret = var->name->str; | |
341 | end: | |
342 | return ret; | |
343 | } | |
344 | ||
345 | const char * | |
346 | bt_ctf_trace_get_environment_field_value_string(struct bt_ctf_trace *trace, | |
347 | int index) | |
348 | { | |
349 | struct environment_variable *var; | |
350 | const char *ret = NULL; | |
351 | ||
352 | if (!trace || index < 0 || index >= trace->environment->len) { | |
353 | goto end; | |
354 | } | |
355 | ||
356 | var = g_ptr_array_index(trace->environment, index); | |
357 | if (var->type != BT_ENVIRONMENT_FIELD_TYPE_STRING) { | |
358 | goto end; | |
359 | } | |
360 | ret = var->value.string->str; | |
361 | end: | |
362 | return ret; | |
363 | } | |
364 | ||
365 | int | |
366 | bt_ctf_trace_get_environment_field_value_integer(struct bt_ctf_trace *trace, | |
367 | int index, int64_t *value) | |
368 | { | |
369 | struct environment_variable *var; | |
370 | int ret = 0; | |
371 | ||
372 | if (!trace || !value || index < 0 || index >= trace->environment->len) { | |
373 | ret = -1; | |
374 | goto end; | |
375 | } | |
376 | ||
377 | var = g_ptr_array_index(trace->environment, index); | |
378 | if (var->type != BT_ENVIRONMENT_FIELD_TYPE_INTEGER) { | |
379 | ret = -1; | |
380 | goto end; | |
381 | } | |
382 | *value = var->value.integer; | |
383 | end: | |
384 | return ret; | |
385 | } | |
386 | ||
bc37ae52 JG |
387 | int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace, |
388 | struct bt_ctf_clock *clock) | |
389 | { | |
390 | int ret = 0; | |
391 | struct search_query query = { .value = clock, .found = 0 }; | |
392 | ||
393 | if (!trace || !clock) { | |
394 | ret = -1; | |
395 | goto end; | |
396 | } | |
397 | ||
398 | /* Check for duplicate clocks */ | |
399 | g_ptr_array_foreach(trace->clocks, value_exists, &query); | |
400 | if (query.found) { | |
401 | ret = -1; | |
402 | goto end; | |
403 | } | |
404 | ||
405 | bt_ctf_clock_get(clock); | |
406 | g_ptr_array_add(trace->clocks, clock); | |
407 | end: | |
408 | return ret; | |
409 | } | |
410 | ||
884cd6c3 JG |
411 | int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace) |
412 | { | |
413 | int ret = -1; | |
414 | ||
415 | if (!trace) { | |
416 | goto end; | |
417 | } | |
418 | ||
419 | ret = trace->clocks->len; | |
420 | end: | |
421 | return ret; | |
422 | } | |
423 | ||
424 | struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace, | |
425 | int index) | |
426 | { | |
427 | struct bt_ctf_clock *clock = NULL; | |
428 | ||
429 | if (!trace || index < 0 || index >= trace->clocks->len) { | |
430 | goto end; | |
431 | } | |
432 | ||
433 | clock = g_ptr_array_index(trace->clocks, index); | |
434 | bt_ctf_clock_get(clock); | |
435 | end: | |
436 | return clock; | |
437 | } | |
438 | ||
bc37ae52 JG |
439 | BT_HIDDEN |
440 | const char *get_byte_order_string(int byte_order) | |
441 | { | |
442 | const char *string; | |
443 | ||
444 | switch (byte_order) { | |
445 | case LITTLE_ENDIAN: | |
446 | string = "le"; | |
447 | break; | |
448 | case BIG_ENDIAN: | |
449 | string = "be"; | |
450 | break; | |
451 | default: | |
452 | string = "unknown"; | |
453 | break; | |
454 | } | |
455 | ||
456 | return string; | |
457 | } | |
458 | ||
459 | static | |
460 | int append_trace_metadata(struct bt_ctf_trace *trace, | |
461 | struct metadata_context *context) | |
462 | { | |
463 | unsigned char *uuid = trace->uuid; | |
464 | int ret; | |
465 | ||
466 | g_string_append(context->string, "trace {\n"); | |
467 | ||
468 | g_string_append(context->string, "\tmajor = 1;\n"); | |
469 | g_string_append(context->string, "\tminor = 8;\n"); | |
470 | ||
471 | g_string_append_printf(context->string, | |
472 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
473 | uuid[0], uuid[1], uuid[2], uuid[3], | |
474 | uuid[4], uuid[5], uuid[6], uuid[7], | |
475 | uuid[8], uuid[9], uuid[10], uuid[11], | |
476 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
477 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", | |
478 | get_byte_order_string(trace->byte_order)); | |
479 | ||
480 | g_string_append(context->string, "\tpacket.header := "); | |
481 | context->current_indentation_level++; | |
482 | g_string_assign(context->field_name, ""); | |
d246b111 | 483 | ret = bt_ctf_field_type_serialize(trace->packet_header_type, |
bc37ae52 JG |
484 | context); |
485 | if (ret) { | |
486 | goto end; | |
487 | } | |
488 | context->current_indentation_level--; | |
489 | ||
490 | g_string_append(context->string, ";\n};\n\n"); | |
491 | end: | |
492 | return ret; | |
493 | } | |
494 | ||
495 | static | |
496 | void append_env_field_metadata(struct environment_variable *var, | |
497 | struct metadata_context *context) | |
498 | { | |
3487c9f3 JG |
499 | switch (var->type) { |
500 | case BT_ENVIRONMENT_FIELD_TYPE_STRING: | |
501 | g_string_append_printf(context->string, "\t%s = \"%s\";\n", | |
502 | var->name->str, var->value.string->str); | |
503 | break; | |
504 | case BT_ENVIRONMENT_FIELD_TYPE_INTEGER: | |
505 | g_string_append_printf(context->string, "\t%s = %" PRId64 ";\n", | |
506 | var->name->str, var->value.integer); | |
507 | break; | |
508 | default: | |
509 | assert(0); | |
510 | } | |
bc37ae52 JG |
511 | } |
512 | ||
513 | static | |
514 | void append_env_metadata(struct bt_ctf_trace *trace, | |
515 | struct metadata_context *context) | |
516 | { | |
517 | if (trace->environment->len == 0) { | |
518 | return; | |
519 | } | |
520 | ||
521 | g_string_append(context->string, "env {\n"); | |
522 | g_ptr_array_foreach(trace->environment, | |
523 | (GFunc)append_env_field_metadata, context); | |
524 | g_string_append(context->string, "};\n\n"); | |
525 | } | |
526 | ||
527 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) | |
528 | { | |
529 | char *metadata = NULL; | |
530 | struct metadata_context *context = NULL; | |
531 | int err = 0; | |
532 | size_t i; | |
533 | ||
534 | if (!trace) { | |
535 | goto end; | |
536 | } | |
537 | ||
538 | context = g_new0(struct metadata_context, 1); | |
539 | if (!context) { | |
540 | goto end; | |
541 | } | |
542 | ||
543 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); | |
544 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); | |
545 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); | |
546 | if (append_trace_metadata(trace, context)) { | |
547 | goto error; | |
548 | } | |
549 | append_env_metadata(trace, context); | |
550 | g_ptr_array_foreach(trace->clocks, | |
551 | (GFunc)bt_ctf_clock_serialize, context); | |
552 | ||
553 | for (i = 0; i < trace->stream_classes->len; i++) { | |
554 | err = bt_ctf_stream_class_serialize( | |
555 | trace->stream_classes->pdata[i], context); | |
556 | if (err) { | |
557 | goto error; | |
558 | } | |
559 | } | |
560 | ||
561 | metadata = context->string->str; | |
562 | error: | |
563 | g_string_free(context->string, err ? TRUE : FALSE); | |
564 | g_string_free(context->field_name, TRUE); | |
565 | g_free(context); | |
566 | end: | |
567 | return metadata; | |
568 | } | |
569 | ||
4ed90fb3 JG |
570 | enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace) |
571 | { | |
572 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; | |
573 | ||
574 | if (!trace) { | |
575 | goto end; | |
576 | } | |
577 | ||
578 | switch (trace->byte_order) { | |
579 | case BIG_ENDIAN: | |
580 | ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; | |
581 | break; | |
582 | case LITTLE_ENDIAN: | |
583 | ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; | |
584 | break; | |
585 | default: | |
586 | break; | |
587 | } | |
588 | end: | |
589 | return ret; | |
590 | } | |
591 | ||
bc37ae52 JG |
592 | int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, |
593 | enum bt_ctf_byte_order byte_order) | |
594 | { | |
595 | int ret = 0; | |
596 | int internal_byte_order; | |
597 | ||
598 | if (!trace || trace->frozen) { | |
599 | ret = -1; | |
600 | goto end; | |
601 | } | |
602 | ||
603 | switch (byte_order) { | |
604 | case BT_CTF_BYTE_ORDER_NATIVE: | |
c35a1669 JG |
605 | /* |
606 | * This doesn't make sense since the CTF specification defines | |
607 | * the "native" byte order as "the byte order described in the | |
608 | * trace description". However, this behavior had been | |
609 | * implemented as part of v1.2 and is kept to maintain | |
610 | * compatibility. | |
611 | * | |
612 | * This may be changed on a major version bump only. | |
613 | */ | |
614 | internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? | |
bc37ae52 JG |
615 | LITTLE_ENDIAN : BIG_ENDIAN; |
616 | break; | |
617 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: | |
618 | internal_byte_order = LITTLE_ENDIAN; | |
619 | break; | |
620 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: | |
621 | case BT_CTF_BYTE_ORDER_NETWORK: | |
622 | internal_byte_order = BIG_ENDIAN; | |
623 | break; | |
624 | default: | |
625 | ret = -1; | |
626 | goto end; | |
627 | } | |
628 | ||
629 | trace->byte_order = internal_byte_order; | |
bc37ae52 JG |
630 | end: |
631 | return ret; | |
632 | } | |
633 | ||
d246b111 JG |
634 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( |
635 | struct bt_ctf_trace *trace) | |
636 | { | |
637 | struct bt_ctf_field_type *field_type = NULL; | |
638 | ||
639 | if (!trace) { | |
640 | goto end; | |
641 | } | |
642 | ||
643 | bt_ctf_field_type_get(trace->packet_header_type); | |
644 | field_type = trace->packet_header_type; | |
645 | end: | |
646 | return field_type; | |
647 | } | |
648 | ||
649 | int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, | |
650 | struct bt_ctf_field_type *packet_header_type) | |
651 | { | |
652 | int ret = 0; | |
653 | ||
654 | if (!trace || !packet_header_type || trace->frozen) { | |
655 | ret = -1; | |
656 | goto end; | |
657 | } | |
658 | ||
659 | /* packet_header_type must be a structure */ | |
660 | if (bt_ctf_field_type_get_type_id(packet_header_type) != | |
661 | CTF_TYPE_STRUCT) { | |
662 | ret = -1; | |
663 | goto end; | |
664 | } | |
665 | ||
666 | bt_ctf_field_type_get(packet_header_type); | |
667 | bt_ctf_field_type_put(trace->packet_header_type); | |
668 | trace->packet_header_type = packet_header_type; | |
669 | end: | |
670 | return ret; | |
671 | } | |
672 | ||
bc37ae52 JG |
673 | void bt_ctf_trace_get(struct bt_ctf_trace *trace) |
674 | { | |
675 | if (!trace) { | |
676 | return; | |
677 | } | |
678 | ||
679 | bt_ctf_ref_get(&trace->ref_count); | |
680 | } | |
681 | ||
682 | void bt_ctf_trace_put(struct bt_ctf_trace *trace) | |
683 | { | |
684 | if (!trace) { | |
685 | return; | |
686 | } | |
687 | ||
688 | bt_ctf_ref_put(&trace->ref_count, bt_ctf_trace_destroy); | |
689 | } | |
690 | ||
bc37ae52 JG |
691 | BT_HIDDEN |
692 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) | |
693 | { | |
694 | unsigned int alignment, size; | |
695 | struct bt_ctf_field_type *field_type; | |
696 | ||
697 | if (alias >= NR_FIELD_TYPE_ALIAS) { | |
698 | return NULL; | |
699 | } | |
700 | ||
701 | alignment = field_type_aliases_alignments[alias]; | |
702 | size = field_type_aliases_sizes[alias]; | |
703 | field_type = bt_ctf_field_type_integer_create(size); | |
704 | bt_ctf_field_type_set_alignment(field_type, alignment); | |
705 | return field_type; | |
706 | } | |
707 | ||
708 | static | |
709 | int init_trace_packet_header(struct bt_ctf_trace *trace) | |
710 | { | |
bc37ae52 | 711 | int ret = 0; |
d246b111 | 712 | struct bt_ctf_field *magic = NULL, *uuid_array = NULL; |
bc37ae52 JG |
713 | struct bt_ctf_field_type *_uint32_t = |
714 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
715 | struct bt_ctf_field_type *_uint8_t = | |
716 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); | |
717 | struct bt_ctf_field_type *trace_packet_header_type = | |
718 | bt_ctf_field_type_structure_create(); | |
719 | struct bt_ctf_field_type *uuid_array_type = | |
720 | bt_ctf_field_type_array_create(_uint8_t, 16); | |
721 | ||
722 | if (!trace_packet_header_type || !uuid_array_type) { | |
723 | ret = -1; | |
724 | goto end; | |
725 | } | |
726 | ||
bc37ae52 JG |
727 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, |
728 | _uint32_t, "magic"); | |
729 | if (ret) { | |
730 | goto end; | |
731 | } | |
732 | ||
733 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
734 | uuid_array_type, "uuid"); | |
735 | if (ret) { | |
736 | goto end; | |
737 | } | |
738 | ||
739 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
740 | _uint32_t, "stream_id"); | |
741 | if (ret) { | |
742 | goto end; | |
743 | } | |
744 | ||
662e778c JG |
745 | ret = bt_ctf_trace_set_packet_header_type(trace, |
746 | trace_packet_header_type); | |
747 | if (ret) { | |
748 | goto end; | |
749 | } | |
bc37ae52 JG |
750 | end: |
751 | bt_ctf_field_type_put(uuid_array_type); | |
752 | bt_ctf_field_type_put(_uint32_t); | |
753 | bt_ctf_field_type_put(_uint8_t); | |
754 | bt_ctf_field_put(magic); | |
755 | bt_ctf_field_put(uuid_array); | |
662e778c | 756 | bt_ctf_field_type_put(trace_packet_header_type); |
bc37ae52 JG |
757 | |
758 | return ret; | |
759 | } | |
760 | ||
761 | static | |
762 | void environment_variable_destroy(struct environment_variable *var) | |
763 | { | |
764 | g_string_free(var->name, TRUE); | |
3487c9f3 JG |
765 | if (var->type == BT_ENVIRONMENT_FIELD_TYPE_STRING) { |
766 | g_string_free(var->value.string, TRUE); | |
767 | } | |
bc37ae52 JG |
768 | g_free(var); |
769 | } |