Commit | Line | Data |
---|---|---|
1c822dfb 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-ir/event-internal.h> | |
34 | #include <babeltrace/ctf-ir/event-class.h> | |
35 | #include <babeltrace/ctf-ir/event-class-internal.h> | |
36 | #include <babeltrace/ctf-writer/functor-internal.h> | |
37 | #include <babeltrace/ctf-ir/field-types-internal.h> | |
38 | #include <babeltrace/ctf-ir/attributes-internal.h> | |
39 | #include <babeltrace/ctf-ir/validation-internal.h> | |
40 | #include <babeltrace/ctf-ir/utils.h> | |
41 | #include <babeltrace/compiler.h> | |
42 | #include <babeltrace/values.h> | |
43 | #include <babeltrace/ref.h> | |
44 | #include <babeltrace/endian.h> | |
45 | ||
46 | #define DEFAULT_IDENTIFIER_SIZE 128 | |
47 | #define DEFAULT_METADATA_STRING_SIZE 4096 | |
48 | ||
49 | static | |
50 | void bt_ctf_trace_destroy(struct bt_object *obj); | |
51 | static | |
52 | int init_trace_packet_header(struct bt_ctf_trace *trace); | |
53 | static | |
54 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace); | |
55 | ||
56 | static | |
57 | const unsigned int field_type_aliases_alignments[] = { | |
58 | [FIELD_TYPE_ALIAS_UINT5_T] = 1, | |
59 | [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8, | |
60 | [FIELD_TYPE_ALIAS_UINT27_T] = 1, | |
61 | [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8, | |
62 | }; | |
63 | ||
64 | static | |
65 | const unsigned int field_type_aliases_sizes[] = { | |
66 | [FIELD_TYPE_ALIAS_UINT5_T] = 5, | |
67 | [FIELD_TYPE_ALIAS_UINT8_T] = 8, | |
68 | [FIELD_TYPE_ALIAS_UINT16_T] = 16, | |
69 | [FIELD_TYPE_ALIAS_UINT27_T] = 27, | |
70 | [FIELD_TYPE_ALIAS_UINT32_T] = 32, | |
71 | [FIELD_TYPE_ALIAS_UINT64_T] = 64, | |
72 | }; | |
73 | ||
d14fb122 | 74 | BT_HIDDEN |
1c822dfb JG |
75 | struct bt_ctf_trace *bt_ctf_trace_create(void) |
76 | { | |
77 | struct bt_ctf_trace *trace = NULL; | |
78 | ||
79 | trace = g_new0(struct bt_ctf_trace, 1); | |
80 | if (!trace) { | |
81 | goto error; | |
82 | } | |
83 | ||
84 | bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE); | |
85 | bt_object_init(trace, bt_ctf_trace_destroy); | |
86 | trace->clocks = g_ptr_array_new_with_free_func( | |
87 | (GDestroyNotify) bt_put); | |
88 | trace->streams = g_ptr_array_new_with_free_func( | |
89 | (GDestroyNotify) bt_object_release); | |
90 | trace->stream_classes = g_ptr_array_new_with_free_func( | |
91 | (GDestroyNotify) bt_object_release); | |
92 | if (!trace->clocks || !trace->stream_classes || !trace->streams) { | |
93 | goto error; | |
94 | } | |
95 | ||
96 | /* Generate a trace UUID */ | |
97 | uuid_generate(trace->uuid); | |
98 | if (init_trace_packet_header(trace)) { | |
99 | goto error; | |
100 | } | |
101 | ||
102 | /* Create the environment array object */ | |
103 | trace->environment = bt_ctf_attributes_create(); | |
104 | if (!trace->environment) { | |
105 | goto error; | |
106 | } | |
107 | ||
108 | return trace; | |
109 | ||
110 | error: | |
111 | BT_PUT(trace); | |
112 | return trace; | |
113 | } | |
114 | ||
d14fb122 | 115 | BT_HIDDEN |
1c822dfb JG |
116 | void bt_ctf_trace_destroy(struct bt_object *obj) |
117 | { | |
118 | struct bt_ctf_trace *trace; | |
119 | ||
120 | trace = container_of(obj, struct bt_ctf_trace, base); | |
121 | if (trace->environment) { | |
122 | bt_ctf_attributes_destroy(trace->environment); | |
123 | } | |
124 | ||
125 | if (trace->clocks) { | |
126 | g_ptr_array_free(trace->clocks, TRUE); | |
127 | } | |
128 | ||
129 | if (trace->streams) { | |
130 | g_ptr_array_free(trace->streams, TRUE); | |
131 | } | |
132 | ||
133 | if (trace->stream_classes) { | |
134 | g_ptr_array_free(trace->stream_classes, TRUE); | |
135 | } | |
136 | ||
137 | bt_put(trace->packet_header_type); | |
138 | g_free(trace); | |
139 | } | |
140 | ||
d14fb122 | 141 | BT_HIDDEN |
1c822dfb JG |
142 | int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace, |
143 | const char *name, struct bt_value *value) | |
144 | { | |
145 | int ret = 0; | |
146 | ||
147 | if (!trace || !name || !value || | |
148 | bt_ctf_validate_identifier(name) || | |
149 | !(bt_value_is_integer(value) || bt_value_is_string(value))) { | |
150 | ret = -1; | |
151 | goto end; | |
152 | } | |
153 | ||
154 | if (strchr(name, ' ')) { | |
155 | ret = -1; | |
156 | goto end; | |
157 | } | |
158 | ||
159 | if (trace->frozen) { | |
160 | /* | |
161 | * New environment fields may be added to a frozen trace, | |
162 | * but existing fields may not be changed. | |
163 | * | |
164 | * The object passed is frozen like all other attributes. | |
165 | */ | |
166 | struct bt_value *attribute = | |
167 | bt_ctf_attributes_get_field_value_by_name( | |
168 | trace->environment, name); | |
169 | ||
170 | if (attribute) { | |
171 | BT_PUT(attribute); | |
172 | ret = -1; | |
173 | goto end; | |
174 | } | |
175 | ||
176 | bt_value_freeze(value); | |
177 | } | |
178 | ||
179 | ret = bt_ctf_attributes_set_field_value(trace->environment, name, | |
180 | value); | |
181 | ||
182 | end: | |
183 | return ret; | |
184 | } | |
185 | ||
d14fb122 | 186 | BT_HIDDEN |
1c822dfb JG |
187 | int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace, |
188 | const char *name, const char *value) | |
189 | { | |
190 | int ret = 0; | |
191 | struct bt_value *env_value_string_obj = NULL; | |
192 | ||
193 | if (!trace || !name || !value) { | |
194 | ret = -1; | |
195 | goto end; | |
196 | } | |
197 | ||
198 | if (trace->frozen) { | |
199 | /* | |
200 | * New environment fields may be added to a frozen trace, | |
201 | * but existing fields may not be changed. | |
202 | */ | |
203 | struct bt_value *attribute = | |
204 | bt_ctf_attributes_get_field_value_by_name( | |
205 | trace->environment, name); | |
206 | ||
207 | if (attribute) { | |
208 | BT_PUT(attribute); | |
209 | ret = -1; | |
210 | goto end; | |
211 | } | |
212 | } | |
213 | ||
214 | env_value_string_obj = bt_value_string_create_init(value); | |
215 | ||
216 | if (!env_value_string_obj) { | |
217 | ret = -1; | |
218 | goto end; | |
219 | } | |
220 | ||
221 | if (trace->frozen) { | |
222 | bt_value_freeze(env_value_string_obj); | |
223 | } | |
224 | ret = bt_ctf_trace_set_environment_field(trace, name, | |
225 | env_value_string_obj); | |
226 | ||
227 | end: | |
228 | BT_PUT(env_value_string_obj); | |
229 | return ret; | |
230 | } | |
231 | ||
d14fb122 | 232 | BT_HIDDEN |
1c822dfb JG |
233 | int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace, |
234 | const char *name, int64_t value) | |
235 | { | |
236 | int ret = 0; | |
237 | struct bt_value *env_value_integer_obj = NULL; | |
238 | ||
239 | if (!trace || !name) { | |
240 | ret = -1; | |
241 | goto end; | |
242 | } | |
243 | ||
244 | if (trace->frozen) { | |
245 | /* | |
246 | * New environment fields may be added to a frozen trace, | |
247 | * but existing fields may not be changed. | |
248 | */ | |
249 | struct bt_value *attribute = | |
250 | bt_ctf_attributes_get_field_value_by_name( | |
251 | trace->environment, name); | |
252 | ||
253 | if (attribute) { | |
254 | BT_PUT(attribute); | |
255 | ret = -1; | |
256 | goto end; | |
257 | } | |
258 | } | |
259 | ||
260 | env_value_integer_obj = bt_value_integer_create_init(value); | |
261 | if (!env_value_integer_obj) { | |
262 | ret = -1; | |
263 | goto end; | |
264 | } | |
265 | ||
266 | ret = bt_ctf_trace_set_environment_field(trace, name, | |
267 | env_value_integer_obj); | |
268 | if (trace->frozen) { | |
269 | bt_value_freeze(env_value_integer_obj); | |
270 | } | |
271 | end: | |
272 | BT_PUT(env_value_integer_obj); | |
273 | return ret; | |
274 | } | |
275 | ||
d14fb122 | 276 | BT_HIDDEN |
1c822dfb JG |
277 | int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace) |
278 | { | |
279 | int ret = 0; | |
280 | ||
281 | if (!trace) { | |
282 | ret = -1; | |
283 | goto end; | |
284 | } | |
285 | ||
286 | ret = bt_ctf_attributes_get_count(trace->environment); | |
287 | ||
288 | end: | |
289 | return ret; | |
290 | } | |
291 | ||
d14fb122 | 292 | BT_HIDDEN |
1c822dfb JG |
293 | const char * |
294 | bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace, | |
295 | int index) | |
296 | { | |
297 | const char *ret = NULL; | |
298 | ||
299 | if (!trace) { | |
300 | goto end; | |
301 | } | |
302 | ||
303 | ret = bt_ctf_attributes_get_field_name(trace->environment, index); | |
304 | ||
305 | end: | |
306 | return ret; | |
307 | } | |
308 | ||
d14fb122 | 309 | BT_HIDDEN |
1c822dfb JG |
310 | struct bt_value *bt_ctf_trace_get_environment_field_value( |
311 | struct bt_ctf_trace *trace, int index) | |
312 | { | |
313 | struct bt_value *ret = NULL; | |
314 | ||
315 | if (!trace) { | |
316 | goto end; | |
317 | } | |
318 | ||
319 | ret = bt_ctf_attributes_get_field_value(trace->environment, index); | |
320 | ||
321 | end: | |
322 | return ret; | |
323 | } | |
324 | ||
d14fb122 | 325 | BT_HIDDEN |
1c822dfb JG |
326 | struct bt_value *bt_ctf_trace_get_environment_field_value_by_name( |
327 | struct bt_ctf_trace *trace, const char *name) | |
328 | { | |
329 | struct bt_value *ret = NULL; | |
330 | ||
331 | if (!trace || !name) { | |
332 | goto end; | |
333 | } | |
334 | ||
335 | ret = bt_ctf_attributes_get_field_value_by_name(trace->environment, | |
336 | name); | |
337 | ||
338 | end: | |
339 | return ret; | |
340 | } | |
341 | ||
d14fb122 | 342 | BT_HIDDEN |
1c822dfb JG |
343 | int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace, |
344 | struct bt_ctf_clock *clock) | |
345 | { | |
346 | int ret = 0; | |
347 | struct search_query query = { .value = clock, .found = 0 }; | |
348 | ||
349 | if (!trace || !clock) { | |
350 | ret = -1; | |
351 | goto end; | |
352 | } | |
353 | ||
354 | /* Check for duplicate clocks */ | |
355 | g_ptr_array_foreach(trace->clocks, value_exists, &query); | |
356 | if (query.found) { | |
357 | ret = -1; | |
358 | goto end; | |
359 | } | |
360 | ||
361 | bt_get(clock); | |
362 | g_ptr_array_add(trace->clocks, clock); | |
363 | ||
364 | if (!trace->is_created_by_writer) { | |
365 | /* | |
366 | * Non-writer mode trace: disable clock value functions | |
367 | * because clock values are per-stream in that | |
368 | * situation. | |
369 | */ | |
370 | clock->has_value = 0; | |
371 | } | |
372 | ||
373 | if (trace->frozen) { | |
374 | bt_ctf_clock_freeze(clock); | |
375 | } | |
376 | end: | |
377 | return ret; | |
378 | } | |
379 | ||
d14fb122 | 380 | BT_HIDDEN |
1c822dfb JG |
381 | int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace) |
382 | { | |
383 | int ret = -1; | |
384 | ||
385 | if (!trace) { | |
386 | goto end; | |
387 | } | |
388 | ||
389 | ret = trace->clocks->len; | |
390 | end: | |
391 | return ret; | |
392 | } | |
393 | ||
d14fb122 | 394 | BT_HIDDEN |
1c822dfb JG |
395 | struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace, |
396 | int index) | |
397 | { | |
398 | struct bt_ctf_clock *clock = NULL; | |
399 | ||
400 | if (!trace || index < 0 || index >= trace->clocks->len) { | |
401 | goto end; | |
402 | } | |
403 | ||
404 | clock = g_ptr_array_index(trace->clocks, index); | |
405 | bt_get(clock); | |
406 | end: | |
407 | return clock; | |
408 | } | |
409 | ||
d14fb122 | 410 | BT_HIDDEN |
1c822dfb JG |
411 | int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace, |
412 | struct bt_ctf_stream_class *stream_class) | |
413 | { | |
414 | int ret, i; | |
415 | int64_t stream_id; | |
416 | struct bt_ctf_validation_output trace_sc_validation_output = { 0 }; | |
417 | struct bt_ctf_validation_output *ec_validation_outputs = NULL; | |
418 | const enum bt_ctf_validation_flag trace_sc_validation_flags = | |
419 | BT_CTF_VALIDATION_FLAG_TRACE | | |
420 | BT_CTF_VALIDATION_FLAG_STREAM; | |
421 | const enum bt_ctf_validation_flag ec_validation_flags = | |
422 | BT_CTF_VALIDATION_FLAG_EVENT; | |
423 | struct bt_ctf_field_type *packet_header_type = NULL; | |
424 | struct bt_ctf_field_type *packet_context_type = NULL; | |
425 | struct bt_ctf_field_type *event_header_type = NULL; | |
426 | struct bt_ctf_field_type *stream_event_ctx_type = NULL; | |
427 | int event_class_count; | |
428 | struct bt_ctf_clock *clock_to_add_to_trace = NULL; | |
429 | ||
430 | if (!trace || !stream_class) { | |
431 | ret = -1; | |
432 | goto end; | |
433 | } | |
434 | ||
435 | event_class_count = | |
436 | bt_ctf_stream_class_get_event_class_count(stream_class); | |
437 | assert(event_class_count >= 0); | |
438 | ||
439 | /* Check for duplicate stream classes */ | |
440 | for (i = 0; i < trace->stream_classes->len; i++) { | |
441 | if (trace->stream_classes->pdata[i] == stream_class) { | |
442 | /* Stream class already registered to the trace */ | |
443 | ret = -1; | |
444 | goto end; | |
445 | } | |
446 | } | |
447 | ||
448 | /* | |
449 | * If the stream class has a clock, register this clock to this | |
450 | * trace if not already done. | |
451 | */ | |
452 | if (stream_class->clock) { | |
453 | const char *clock_name = | |
454 | bt_ctf_clock_get_name(stream_class->clock); | |
455 | struct bt_ctf_clock *trace_clock; | |
456 | ||
457 | assert(clock_name); | |
458 | trace_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name); | |
459 | bt_put(trace_clock); | |
460 | if (trace_clock) { | |
461 | if (trace_clock != stream_class->clock) { | |
462 | /* | |
463 | * Error: two different clocks in the | |
464 | * trace would share the same name. | |
465 | */ | |
466 | ret = -1; | |
467 | goto end; | |
468 | } | |
469 | } else { | |
470 | clock_to_add_to_trace = bt_get(stream_class->clock); | |
471 | } | |
472 | } | |
473 | ||
474 | /* | |
475 | * We're about to freeze both the trace and the stream class. | |
476 | * Also, each event class contained in this stream class are | |
477 | * already frozen. | |
478 | * | |
479 | * This trace, this stream class, and all its event classes | |
480 | * should be valid at this point. | |
481 | * | |
482 | * Validate trace and stream class first, then each event | |
483 | * class of this stream class can be validated individually. | |
484 | */ | |
485 | packet_header_type = | |
486 | bt_ctf_trace_get_packet_header_type(trace); | |
487 | packet_context_type = | |
488 | bt_ctf_stream_class_get_packet_context_type(stream_class); | |
489 | event_header_type = | |
490 | bt_ctf_stream_class_get_event_header_type(stream_class); | |
491 | stream_event_ctx_type = | |
492 | bt_ctf_stream_class_get_event_context_type(stream_class); | |
493 | ret = bt_ctf_validate_class_types(trace->environment, | |
494 | packet_header_type, packet_context_type, event_header_type, | |
495 | stream_event_ctx_type, NULL, NULL, trace->valid, | |
496 | stream_class->valid, 1, &trace_sc_validation_output, | |
497 | trace_sc_validation_flags); | |
498 | BT_PUT(packet_header_type); | |
499 | BT_PUT(packet_context_type); | |
500 | BT_PUT(event_header_type); | |
501 | BT_PUT(stream_event_ctx_type); | |
502 | ||
503 | if (ret) { | |
504 | /* | |
505 | * This means something went wrong during the validation | |
506 | * process, not that the objects are invalid. | |
507 | */ | |
508 | goto end; | |
509 | } | |
510 | ||
511 | if ((trace_sc_validation_output.valid_flags & | |
512 | trace_sc_validation_flags) != | |
513 | trace_sc_validation_flags) { | |
514 | /* Invalid trace/stream class */ | |
515 | ret = -1; | |
516 | goto end; | |
517 | } | |
518 | ||
519 | if (event_class_count > 0) { | |
520 | ec_validation_outputs = g_new0(struct bt_ctf_validation_output, | |
521 | event_class_count); | |
522 | if (!ec_validation_outputs) { | |
523 | ret = -1; | |
524 | goto end; | |
525 | } | |
526 | } | |
527 | ||
528 | /* Validate each event class individually */ | |
529 | for (i = 0; i < event_class_count; i++) { | |
530 | struct bt_ctf_event_class *event_class = | |
531 | bt_ctf_stream_class_get_event_class(stream_class, i); | |
532 | struct bt_ctf_field_type *event_context_type = NULL; | |
533 | struct bt_ctf_field_type *event_payload_type = NULL; | |
534 | ||
535 | event_context_type = | |
536 | bt_ctf_event_class_get_context_type(event_class); | |
537 | event_payload_type = | |
538 | bt_ctf_event_class_get_payload_type(event_class); | |
539 | ||
540 | /* | |
541 | * It is important to use the field types returned by | |
542 | * the previous trace and stream class validation here | |
543 | * because copies could have been made. | |
544 | */ | |
545 | ret = bt_ctf_validate_class_types(trace->environment, | |
546 | trace_sc_validation_output.packet_header_type, | |
547 | trace_sc_validation_output.packet_context_type, | |
548 | trace_sc_validation_output.event_header_type, | |
549 | trace_sc_validation_output.stream_event_ctx_type, | |
550 | event_context_type, event_payload_type, | |
551 | 1, 1, event_class->valid, &ec_validation_outputs[i], | |
552 | ec_validation_flags); | |
553 | BT_PUT(event_context_type); | |
554 | BT_PUT(event_payload_type); | |
555 | BT_PUT(event_class); | |
556 | ||
557 | if (ret) { | |
558 | goto end; | |
559 | } | |
560 | ||
561 | if ((ec_validation_outputs[i].valid_flags & | |
562 | ec_validation_flags) != ec_validation_flags) { | |
563 | /* Invalid event class */ | |
564 | ret = -1; | |
565 | goto end; | |
566 | } | |
567 | } | |
568 | ||
569 | stream_id = bt_ctf_stream_class_get_id(stream_class); | |
570 | if (stream_id < 0) { | |
571 | stream_id = trace->next_stream_id++; | |
572 | ||
573 | /* Try to assign a new stream id */ | |
574 | for (i = 0; i < trace->stream_classes->len; i++) { | |
575 | if (stream_id == bt_ctf_stream_class_get_id( | |
576 | trace->stream_classes->pdata[i])) { | |
577 | /* Duplicate stream id found */ | |
578 | ret = -1; | |
579 | goto end; | |
580 | } | |
581 | } | |
582 | ||
583 | if (bt_ctf_stream_class_set_id_no_check(stream_class, | |
584 | stream_id)) { | |
585 | /* TODO Should retry with a different stream id */ | |
586 | ret = -1; | |
587 | goto end; | |
588 | } | |
589 | } | |
590 | ||
591 | bt_object_set_parent(stream_class, trace); | |
592 | g_ptr_array_add(trace->stream_classes, stream_class); | |
593 | ||
594 | /* | |
595 | * At this point we know that the function will be successful. | |
596 | * Therefore we can replace the trace and stream class field | |
597 | * types with what's in their validation output structure and | |
598 | * mark them as valid. We can also replace the field types of | |
599 | * all the event classes of the stream class and mark them as | |
600 | * valid. | |
601 | */ | |
602 | bt_ctf_validation_replace_types(trace, stream_class, NULL, | |
603 | &trace_sc_validation_output, trace_sc_validation_flags); | |
604 | trace->valid = 1; | |
605 | stream_class->valid = 1; | |
606 | ||
607 | /* | |
608 | * Put what was not moved in bt_ctf_validation_replace_types(). | |
609 | */ | |
610 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
611 | ||
612 | for (i = 0; i < event_class_count; i++) { | |
613 | struct bt_ctf_event_class *event_class = | |
614 | bt_ctf_stream_class_get_event_class(stream_class, i); | |
615 | ||
616 | bt_ctf_validation_replace_types(NULL, NULL, event_class, | |
617 | &ec_validation_outputs[i], ec_validation_flags); | |
618 | event_class->valid = 1; | |
619 | BT_PUT(event_class); | |
620 | ||
621 | /* | |
622 | * Put what was not moved in | |
623 | * bt_ctf_validation_replace_types(). | |
624 | */ | |
625 | bt_ctf_validation_output_put_types(&ec_validation_outputs[i]); | |
626 | } | |
627 | ||
628 | /* | |
629 | * All field type byte orders set as "native" byte ordering can now be | |
630 | * safely set to trace's own endianness, including the stream class'. | |
631 | */ | |
632 | bt_ctf_field_type_set_native_byte_order(trace->packet_header_type, | |
633 | trace->byte_order); | |
634 | bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order); | |
635 | ||
636 | /* Add stream class's clock if it exists */ | |
637 | if (clock_to_add_to_trace) { | |
638 | int add_clock_ret = | |
639 | bt_ctf_trace_add_clock(trace, clock_to_add_to_trace); | |
640 | assert(add_clock_ret == 0); | |
641 | } | |
642 | ||
643 | /* | |
644 | * Freeze the trace and the stream class. | |
645 | */ | |
646 | bt_ctf_stream_class_freeze(stream_class); | |
647 | bt_ctf_trace_freeze(trace); | |
648 | ||
649 | end: | |
650 | if (ret) { | |
651 | bt_object_set_parent(stream_class, NULL); | |
652 | ||
653 | if (ec_validation_outputs) { | |
654 | for (i = 0; i < event_class_count; i++) { | |
655 | bt_ctf_validation_output_put_types( | |
656 | &ec_validation_outputs[i]); | |
657 | } | |
658 | } | |
659 | } | |
660 | ||
661 | g_free(ec_validation_outputs); | |
662 | bt_ctf_validation_output_put_types(&trace_sc_validation_output); | |
663 | BT_PUT(clock_to_add_to_trace); | |
664 | assert(!packet_header_type); | |
665 | assert(!packet_context_type); | |
666 | assert(!event_header_type); | |
667 | assert(!stream_event_ctx_type); | |
668 | ||
669 | return ret; | |
670 | } | |
671 | ||
d14fb122 | 672 | BT_HIDDEN |
1c822dfb JG |
673 | int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) |
674 | { | |
675 | int ret; | |
676 | ||
677 | if (!trace) { | |
678 | ret = -1; | |
679 | goto end; | |
680 | } | |
681 | ||
682 | ret = trace->stream_classes->len; | |
683 | end: | |
684 | return ret; | |
685 | } | |
686 | ||
d14fb122 | 687 | BT_HIDDEN |
1c822dfb JG |
688 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class( |
689 | struct bt_ctf_trace *trace, int index) | |
690 | { | |
691 | struct bt_ctf_stream_class *stream_class = NULL; | |
692 | ||
693 | if (!trace || index < 0 || index >= trace->stream_classes->len) { | |
694 | goto end; | |
695 | } | |
696 | ||
697 | stream_class = g_ptr_array_index(trace->stream_classes, index); | |
698 | bt_get(stream_class); | |
699 | end: | |
700 | return stream_class; | |
701 | } | |
702 | ||
d14fb122 | 703 | BT_HIDDEN |
1c822dfb JG |
704 | struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id( |
705 | struct bt_ctf_trace *trace, uint32_t id) | |
706 | { | |
707 | int i; | |
708 | struct bt_ctf_stream_class *stream_class = NULL; | |
709 | ||
710 | if (!trace) { | |
711 | goto end; | |
712 | } | |
713 | ||
714 | for (i = 0; i < trace->stream_classes->len; i++) { | |
715 | struct bt_ctf_stream_class *stream_class_candidate; | |
716 | ||
717 | stream_class_candidate = | |
718 | g_ptr_array_index(trace->stream_classes, i); | |
719 | ||
720 | if (bt_ctf_stream_class_get_id(stream_class_candidate) == | |
721 | (int64_t) id) { | |
722 | stream_class = stream_class_candidate; | |
723 | bt_get(stream_class); | |
724 | goto end; | |
725 | } | |
726 | } | |
727 | ||
728 | end: | |
729 | return stream_class; | |
730 | } | |
731 | ||
d14fb122 | 732 | BT_HIDDEN |
1c822dfb JG |
733 | struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name( |
734 | struct bt_ctf_trace *trace, const char *name) | |
735 | { | |
736 | size_t i; | |
737 | struct bt_ctf_clock *clock = NULL; | |
738 | ||
739 | if (!trace || !name) { | |
740 | goto end; | |
741 | } | |
742 | ||
743 | for (i = 0; i < trace->clocks->len; i++) { | |
744 | struct bt_ctf_clock *cur_clk = | |
745 | g_ptr_array_index(trace->clocks, i); | |
746 | const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk); | |
747 | ||
748 | if (!cur_clk_name) { | |
749 | goto end; | |
750 | } | |
751 | ||
752 | if (!strcmp(cur_clk_name, name)) { | |
753 | clock = cur_clk; | |
754 | bt_get(clock); | |
755 | goto end; | |
756 | } | |
757 | } | |
758 | ||
759 | end: | |
760 | return clock; | |
761 | } | |
762 | ||
763 | BT_HIDDEN | |
764 | const char *get_byte_order_string(int byte_order) | |
765 | { | |
766 | const char *string; | |
767 | ||
768 | switch (byte_order) { | |
769 | case LITTLE_ENDIAN: | |
770 | string = "le"; | |
771 | break; | |
772 | case BIG_ENDIAN: | |
773 | string = "be"; | |
774 | break; | |
775 | default: | |
776 | string = "unknown"; | |
777 | break; | |
778 | } | |
779 | ||
780 | return string; | |
781 | } | |
782 | ||
783 | static | |
784 | int append_trace_metadata(struct bt_ctf_trace *trace, | |
785 | struct metadata_context *context) | |
786 | { | |
787 | unsigned char *uuid = trace->uuid; | |
788 | int ret; | |
789 | ||
790 | g_string_append(context->string, "trace {\n"); | |
791 | ||
792 | g_string_append(context->string, "\tmajor = 1;\n"); | |
793 | g_string_append(context->string, "\tminor = 8;\n"); | |
794 | ||
795 | g_string_append_printf(context->string, | |
796 | "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n", | |
797 | uuid[0], uuid[1], uuid[2], uuid[3], | |
798 | uuid[4], uuid[5], uuid[6], uuid[7], | |
799 | uuid[8], uuid[9], uuid[10], uuid[11], | |
800 | uuid[12], uuid[13], uuid[14], uuid[15]); | |
801 | g_string_append_printf(context->string, "\tbyte_order = %s;\n", | |
802 | get_byte_order_string(trace->byte_order)); | |
803 | ||
804 | g_string_append(context->string, "\tpacket.header := "); | |
805 | context->current_indentation_level++; | |
806 | g_string_assign(context->field_name, ""); | |
807 | ret = bt_ctf_field_type_serialize(trace->packet_header_type, | |
808 | context); | |
809 | if (ret) { | |
810 | goto end; | |
811 | } | |
812 | context->current_indentation_level--; | |
813 | ||
814 | g_string_append(context->string, ";\n};\n\n"); | |
815 | end: | |
816 | return ret; | |
817 | } | |
818 | ||
819 | static | |
820 | void append_env_metadata(struct bt_ctf_trace *trace, | |
821 | struct metadata_context *context) | |
822 | { | |
823 | int i; | |
824 | int env_size; | |
825 | ||
826 | env_size = bt_ctf_attributes_get_count(trace->environment); | |
827 | ||
828 | if (env_size <= 0) { | |
829 | return; | |
830 | } | |
831 | ||
832 | g_string_append(context->string, "env {\n"); | |
833 | ||
834 | for (i = 0; i < env_size; i++) { | |
835 | struct bt_value *env_field_value_obj = NULL; | |
836 | const char *entry_name; | |
837 | ||
838 | entry_name = bt_ctf_attributes_get_field_name( | |
839 | trace->environment, i); | |
840 | env_field_value_obj = bt_ctf_attributes_get_field_value( | |
841 | trace->environment, i); | |
842 | ||
843 | if (!entry_name || !env_field_value_obj) { | |
844 | goto loop_next; | |
845 | } | |
846 | ||
847 | switch (bt_value_get_type(env_field_value_obj)) { | |
848 | case BT_VALUE_TYPE_INTEGER: | |
849 | { | |
850 | int ret; | |
851 | int64_t int_value; | |
852 | ||
853 | ret = bt_value_integer_get(env_field_value_obj, | |
854 | &int_value); | |
855 | ||
856 | if (ret) { | |
857 | goto loop_next; | |
858 | } | |
859 | ||
860 | g_string_append_printf(context->string, | |
861 | "\t%s = %" PRId64 ";\n", entry_name, | |
862 | int_value); | |
863 | break; | |
864 | } | |
865 | case BT_VALUE_TYPE_STRING: | |
866 | { | |
867 | int ret; | |
868 | const char *str_value; | |
869 | char *escaped_str = NULL; | |
870 | ||
871 | ret = bt_value_string_get(env_field_value_obj, | |
872 | &str_value); | |
873 | ||
874 | if (ret) { | |
875 | goto loop_next; | |
876 | } | |
877 | ||
878 | escaped_str = g_strescape(str_value, NULL); | |
879 | ||
880 | if (!escaped_str) { | |
881 | goto loop_next; | |
882 | } | |
883 | ||
884 | g_string_append_printf(context->string, | |
885 | "\t%s = \"%s\";\n", entry_name, escaped_str); | |
886 | free(escaped_str); | |
887 | break; | |
888 | } | |
889 | ||
890 | default: | |
891 | goto loop_next; | |
892 | } | |
893 | ||
894 | loop_next: | |
895 | BT_PUT(env_field_value_obj); | |
896 | } | |
897 | ||
898 | g_string_append(context->string, "};\n\n"); | |
899 | } | |
900 | ||
d14fb122 | 901 | BT_HIDDEN |
1c822dfb JG |
902 | char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace) |
903 | { | |
904 | char *metadata = NULL; | |
905 | struct metadata_context *context = NULL; | |
906 | int err = 0; | |
907 | size_t i; | |
908 | ||
909 | if (!trace) { | |
910 | goto end; | |
911 | } | |
912 | ||
913 | context = g_new0(struct metadata_context, 1); | |
914 | if (!context) { | |
915 | goto end; | |
916 | } | |
917 | ||
918 | context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE); | |
919 | context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE); | |
920 | g_string_append(context->string, "/* CTF 1.8 */\n\n"); | |
921 | if (append_trace_metadata(trace, context)) { | |
922 | goto error; | |
923 | } | |
924 | append_env_metadata(trace, context); | |
925 | g_ptr_array_foreach(trace->clocks, | |
926 | (GFunc)bt_ctf_clock_serialize, context); | |
927 | ||
928 | for (i = 0; i < trace->stream_classes->len; i++) { | |
929 | err = bt_ctf_stream_class_serialize( | |
930 | trace->stream_classes->pdata[i], context); | |
931 | if (err) { | |
932 | goto error; | |
933 | } | |
934 | } | |
935 | ||
936 | metadata = context->string->str; | |
937 | error: | |
938 | g_string_free(context->string, err ? TRUE : FALSE); | |
939 | g_string_free(context->field_name, TRUE); | |
940 | g_free(context); | |
941 | end: | |
942 | return metadata; | |
943 | } | |
944 | ||
d14fb122 | 945 | BT_HIDDEN |
1c822dfb JG |
946 | enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace) |
947 | { | |
948 | enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN; | |
949 | ||
950 | if (!trace) { | |
951 | goto end; | |
952 | } | |
953 | ||
954 | switch (trace->byte_order) { | |
955 | case BIG_ENDIAN: | |
956 | ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN; | |
957 | break; | |
958 | case LITTLE_ENDIAN: | |
959 | ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN; | |
960 | break; | |
961 | default: | |
962 | break; | |
963 | } | |
964 | end: | |
965 | return ret; | |
966 | } | |
967 | ||
d14fb122 | 968 | BT_HIDDEN |
1c822dfb JG |
969 | int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace, |
970 | enum bt_ctf_byte_order byte_order) | |
971 | { | |
972 | int ret = 0; | |
973 | int internal_byte_order; | |
974 | ||
975 | if (!trace || trace->frozen) { | |
976 | ret = -1; | |
977 | goto end; | |
978 | } | |
979 | ||
980 | switch (byte_order) { | |
981 | case BT_CTF_BYTE_ORDER_NATIVE: | |
982 | /* | |
983 | * This doesn't make sense since the CTF specification defines | |
984 | * the "native" byte order as "the byte order described in the | |
985 | * trace description". However, this behavior had been | |
986 | * implemented as part of v1.2 and is kept to maintain | |
987 | * compatibility. | |
988 | * | |
989 | * This may be changed on a major version bump only. | |
990 | */ | |
991 | internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ? | |
992 | LITTLE_ENDIAN : BIG_ENDIAN; | |
993 | break; | |
994 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: | |
995 | internal_byte_order = LITTLE_ENDIAN; | |
996 | break; | |
997 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: | |
998 | case BT_CTF_BYTE_ORDER_NETWORK: | |
999 | internal_byte_order = BIG_ENDIAN; | |
1000 | break; | |
1001 | default: | |
1002 | ret = -1; | |
1003 | goto end; | |
1004 | } | |
1005 | ||
1006 | trace->byte_order = internal_byte_order; | |
1007 | end: | |
1008 | return ret; | |
1009 | } | |
1010 | ||
d14fb122 | 1011 | BT_HIDDEN |
1c822dfb JG |
1012 | struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type( |
1013 | struct bt_ctf_trace *trace) | |
1014 | { | |
1015 | struct bt_ctf_field_type *field_type = NULL; | |
1016 | ||
1017 | if (!trace) { | |
1018 | goto end; | |
1019 | } | |
1020 | ||
1021 | bt_get(trace->packet_header_type); | |
1022 | field_type = trace->packet_header_type; | |
1023 | end: | |
1024 | return field_type; | |
1025 | } | |
1026 | ||
d14fb122 | 1027 | BT_HIDDEN |
1c822dfb JG |
1028 | int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace, |
1029 | struct bt_ctf_field_type *packet_header_type) | |
1030 | { | |
1031 | int ret = 0; | |
1032 | ||
1033 | if (!trace || !packet_header_type || trace->frozen) { | |
1034 | ret = -1; | |
1035 | goto end; | |
1036 | } | |
1037 | ||
1038 | /* packet_header_type must be a structure */ | |
1039 | if (bt_ctf_field_type_get_type_id(packet_header_type) != | |
1040 | BT_CTF_TYPE_ID_STRUCT) { | |
1041 | ret = -1; | |
1042 | goto end; | |
1043 | } | |
1044 | ||
1045 | bt_get(packet_header_type); | |
1046 | bt_put(trace->packet_header_type); | |
1047 | trace->packet_header_type = packet_header_type; | |
1048 | end: | |
1049 | return ret; | |
1050 | } | |
1051 | ||
1052 | BT_HIDDEN | |
1053 | struct bt_ctf_field_type *get_field_type(enum field_type_alias alias) | |
1054 | { | |
1055 | int ret; | |
1056 | unsigned int alignment, size; | |
1057 | struct bt_ctf_field_type *field_type = NULL; | |
1058 | ||
1059 | if (alias >= NR_FIELD_TYPE_ALIAS) { | |
1060 | goto end; | |
1061 | } | |
1062 | ||
1063 | alignment = field_type_aliases_alignments[alias]; | |
1064 | size = field_type_aliases_sizes[alias]; | |
1065 | field_type = bt_ctf_field_type_integer_create(size); | |
1066 | ret = bt_ctf_field_type_set_alignment(field_type, alignment); | |
1067 | if (ret) { | |
1068 | BT_PUT(field_type); | |
1069 | } | |
1070 | end: | |
1071 | return field_type; | |
1072 | } | |
1073 | ||
1074 | static | |
1075 | void bt_ctf_trace_freeze(struct bt_ctf_trace *trace) | |
1076 | { | |
1077 | int i; | |
1078 | ||
1079 | bt_ctf_field_type_freeze(trace->packet_header_type); | |
1080 | bt_ctf_attributes_freeze(trace->environment); | |
1081 | ||
1082 | for (i = 0; i < trace->clocks->len; i++) { | |
1083 | struct bt_ctf_clock *clock = | |
1084 | g_ptr_array_index(trace->clocks, i); | |
1085 | ||
1086 | bt_ctf_clock_freeze(clock); | |
1087 | } | |
1088 | ||
1089 | trace->frozen = 1; | |
1090 | } | |
1091 | ||
1092 | static | |
1093 | int init_trace_packet_header(struct bt_ctf_trace *trace) | |
1094 | { | |
1095 | int ret = 0; | |
1096 | struct bt_ctf_field *magic = NULL, *uuid_array = NULL; | |
1097 | struct bt_ctf_field_type *_uint32_t = | |
1098 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
1099 | struct bt_ctf_field_type *_uint8_t = | |
1100 | get_field_type(FIELD_TYPE_ALIAS_UINT8_T); | |
1101 | struct bt_ctf_field_type *trace_packet_header_type = | |
1102 | bt_ctf_field_type_structure_create(); | |
1103 | struct bt_ctf_field_type *uuid_array_type = | |
1104 | bt_ctf_field_type_array_create(_uint8_t, 16); | |
1105 | ||
1106 | if (!trace_packet_header_type || !uuid_array_type) { | |
1107 | ret = -1; | |
1108 | goto end; | |
1109 | } | |
1110 | ||
1111 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
1112 | _uint32_t, "magic"); | |
1113 | if (ret) { | |
1114 | goto end; | |
1115 | } | |
1116 | ||
1117 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
1118 | uuid_array_type, "uuid"); | |
1119 | if (ret) { | |
1120 | goto end; | |
1121 | } | |
1122 | ||
1123 | ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type, | |
1124 | _uint32_t, "stream_id"); | |
1125 | if (ret) { | |
1126 | goto end; | |
1127 | } | |
1128 | ||
1129 | ret = bt_ctf_trace_set_packet_header_type(trace, | |
1130 | trace_packet_header_type); | |
1131 | if (ret) { | |
1132 | goto end; | |
1133 | } | |
1134 | end: | |
1135 | bt_put(uuid_array_type); | |
1136 | bt_put(_uint32_t); | |
1137 | bt_put(_uint8_t); | |
1138 | bt_put(magic); | |
1139 | bt_put(uuid_array); | |
1140 | bt_put(trace_packet_header_type); | |
1141 | return ret; | |
1142 | } |