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