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