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