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