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