Commit | Line | Data |
---|---|---|
11b0cdc8 | 1 | /* |
3f043b05 | 2 | * stream-class.c |
11b0cdc8 | 3 | * |
d2dc44b6 | 4 | * Babeltrace CTF IR - Stream Class |
11b0cdc8 | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
11b0cdc8 JG |
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-writer/clock.h> | |
30 | #include <babeltrace/ctf-ir/clock-internal.h> | |
31 | #include <babeltrace/ctf-writer/event.h> | |
32 | #include <babeltrace/ctf-ir/event-internal.h> | |
33 | #include <babeltrace/ctf-ir/event-types-internal.h> | |
34 | #include <babeltrace/ctf-ir/event-fields-internal.h> | |
35 | #include <babeltrace/ctf-writer/stream.h> | |
36 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
26079216 | 37 | #include <babeltrace/ctf-ir/visitor-internal.h> |
11b0cdc8 | 38 | #include <babeltrace/ctf-writer/functor-internal.h> |
654c1444 | 39 | #include <babeltrace/ctf-ir/utils.h> |
83509119 | 40 | #include <babeltrace/ref.h> |
11b0cdc8 JG |
41 | #include <babeltrace/compiler.h> |
42 | #include <babeltrace/align.h> | |
a0b720b2 | 43 | #include <babeltrace/endian.h> |
11b0cdc8 JG |
44 | |
45 | static | |
83509119 | 46 | void bt_ctf_stream_class_destroy(struct bt_object *obj); |
11b0cdc8 | 47 | static |
662e778c | 48 | int init_event_header(struct bt_ctf_stream_class *stream_class); |
11b0cdc8 | 49 | static |
662e778c | 50 | int init_packet_context(struct bt_ctf_stream_class *stream_class); |
11b0cdc8 JG |
51 | |
52 | struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name) | |
53 | { | |
12c8a1a3 | 54 | int ret; |
11b0cdc8 JG |
55 | struct bt_ctf_stream_class *stream_class = NULL; |
56 | ||
3ea33115 | 57 | if (name && bt_ctf_validate_identifier(name)) { |
11b0cdc8 JG |
58 | goto error; |
59 | } | |
60 | ||
61 | stream_class = g_new0(struct bt_ctf_stream_class, 1); | |
62 | if (!stream_class) { | |
63 | goto error; | |
64 | } | |
65 | ||
66 | stream_class->name = g_string_new(name); | |
67 | stream_class->event_classes = g_ptr_array_new_with_free_func( | |
83509119 | 68 | (GDestroyNotify) bt_put); |
11b0cdc8 | 69 | if (!stream_class->event_classes) { |
83509119 | 70 | goto error; |
11b0cdc8 JG |
71 | } |
72 | ||
662e778c JG |
73 | ret = init_event_header(stream_class); |
74 | if (ret) { | |
83509119 | 75 | goto error; |
662e778c JG |
76 | } |
77 | ||
78 | ret = init_packet_context(stream_class); | |
12c8a1a3 | 79 | if (ret) { |
83509119 | 80 | goto error; |
12c8a1a3 JG |
81 | } |
82 | ||
83509119 | 83 | bt_object_init(stream_class, bt_ctf_stream_class_destroy); |
11b0cdc8 JG |
84 | return stream_class; |
85 | ||
11b0cdc8 | 86 | error: |
83509119 | 87 | BT_PUT(stream_class); |
11b0cdc8 JG |
88 | return stream_class; |
89 | } | |
90 | ||
142c5610 JG |
91 | struct bt_ctf_trace *bt_ctf_stream_class_get_trace( |
92 | struct bt_ctf_stream_class *stream_class) | |
93 | { | |
94 | struct bt_ctf_trace *trace = NULL; | |
95 | ||
96 | if (!stream_class) { | |
97 | goto end; | |
98 | } | |
99 | ||
100 | trace = stream_class->trace; | |
101 | if (trace) { | |
83509119 | 102 | bt_get(trace); |
142c5610 JG |
103 | } |
104 | end: | |
105 | return trace; | |
106 | } | |
107 | ||
69dc4535 JG |
108 | const char *bt_ctf_stream_class_get_name( |
109 | struct bt_ctf_stream_class *stream_class) | |
110 | { | |
111 | const char *name = NULL; | |
112 | ||
113 | if (!stream_class) { | |
114 | goto end; | |
115 | } | |
116 | ||
117 | name = stream_class->name->str; | |
118 | end: | |
119 | return name; | |
120 | } | |
121 | ||
3ea33115 JG |
122 | int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class, |
123 | const char *name) | |
124 | { | |
125 | int ret = 0; | |
126 | ||
127 | if (!stream_class || stream_class->frozen) { | |
128 | ret = -1; | |
129 | goto end; | |
130 | } | |
131 | ||
132 | g_string_assign(stream_class->name, name); | |
133 | end: | |
134 | return ret; | |
135 | } | |
136 | ||
2f100782 JG |
137 | struct bt_ctf_clock *bt_ctf_stream_class_get_clock( |
138 | struct bt_ctf_stream_class *stream_class) | |
139 | { | |
140 | struct bt_ctf_clock *clock = NULL; | |
141 | ||
142 | if (!stream_class || !stream_class->clock) { | |
143 | goto end; | |
144 | } | |
145 | ||
146 | clock = stream_class->clock; | |
83509119 | 147 | bt_get(clock); |
2f100782 JG |
148 | end: |
149 | return clock; | |
150 | } | |
151 | ||
11b0cdc8 JG |
152 | int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, |
153 | struct bt_ctf_clock *clock) | |
154 | { | |
155 | int ret = 0; | |
eee752e5 | 156 | struct bt_ctf_field_type *timestamp_field = NULL; |
11b0cdc8 JG |
157 | |
158 | if (!stream_class || !clock || stream_class->frozen) { | |
159 | ret = -1; | |
160 | goto end; | |
161 | } | |
162 | ||
eee752e5 JG |
163 | /* |
164 | * Look for a "timestamp" field in the stream class' event header type | |
165 | * and map the stream's clock to that field if no current mapping is | |
166 | * currently set. | |
167 | */ | |
168 | timestamp_field = bt_ctf_field_type_structure_get_field_type_by_name( | |
169 | stream_class->event_header_type, "timestamp"); | |
170 | if (timestamp_field) { | |
171 | struct bt_ctf_clock *mapped_clock; | |
172 | ||
173 | mapped_clock = bt_ctf_field_type_integer_get_mapped_clock( | |
174 | timestamp_field); | |
175 | if (mapped_clock) { | |
83509119 | 176 | bt_put(mapped_clock); |
eee752e5 JG |
177 | goto end; |
178 | } | |
179 | ||
180 | ret = bt_ctf_field_type_integer_set_mapped_clock( | |
181 | timestamp_field, clock); | |
182 | if (ret) { | |
183 | goto end; | |
184 | } | |
185 | } | |
186 | ||
11b0cdc8 | 187 | if (stream_class->clock) { |
83509119 | 188 | bt_put(stream_class->clock); |
11b0cdc8 JG |
189 | } |
190 | ||
191 | stream_class->clock = clock; | |
83509119 | 192 | bt_get(clock); |
11b0cdc8 | 193 | end: |
eee752e5 | 194 | if (timestamp_field) { |
83509119 | 195 | bt_put(timestamp_field); |
eee752e5 | 196 | } |
11b0cdc8 JG |
197 | return ret; |
198 | } | |
199 | ||
2f100782 JG |
200 | int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class) |
201 | { | |
202 | int64_t ret; | |
203 | ||
204 | if (!stream_class || !stream_class->id_set) { | |
205 | ret = -1; | |
206 | goto end; | |
207 | } | |
208 | ||
209 | ret = (int64_t) stream_class->id; | |
210 | end: | |
211 | return ret; | |
212 | } | |
213 | ||
5ca83563 JG |
214 | BT_HIDDEN |
215 | int _bt_ctf_stream_class_set_id( | |
216 | struct bt_ctf_stream_class *stream_class, uint32_t id) | |
217 | { | |
218 | stream_class->id = id; | |
219 | stream_class->id_set = 1; | |
220 | return 0; | |
221 | } | |
222 | ||
29664b2a PP |
223 | struct event_class_set_stream_id_data { |
224 | uint32_t stream_id; | |
225 | int ret; | |
226 | }; | |
227 | ||
228 | static | |
229 | void event_class_set_stream_id(gpointer event_class, gpointer data) | |
230 | { | |
231 | struct event_class_set_stream_id_data *typed_data = data; | |
232 | ||
233 | typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class, | |
234 | typed_data->stream_id); | |
235 | } | |
236 | ||
237 | BT_HIDDEN | |
238 | int bt_ctf_stream_class_set_id_no_check( | |
239 | struct bt_ctf_stream_class *stream_class, uint32_t id) | |
2f100782 JG |
240 | { |
241 | int ret = 0; | |
29664b2a PP |
242 | struct event_class_set_stream_id_data data = |
243 | { .stream_id = id, .ret = 0 }; | |
2f100782 | 244 | |
29664b2a PP |
245 | /* |
246 | * Make sure all event classes have their "stream_id" attribute | |
247 | * set to this value. | |
248 | */ | |
249 | g_ptr_array_foreach(stream_class->event_classes, | |
250 | event_class_set_stream_id, &data); | |
251 | ret = data.ret; | |
252 | if (ret) { | |
2f100782 JG |
253 | goto end; |
254 | } | |
255 | ||
5ca83563 JG |
256 | ret = _bt_ctf_stream_class_set_id(stream_class, id); |
257 | if (ret) { | |
258 | goto end; | |
259 | } | |
2f100782 JG |
260 | end: |
261 | return ret; | |
262 | } | |
263 | ||
29664b2a PP |
264 | int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, |
265 | uint32_t id) | |
266 | { | |
267 | int ret = 0; | |
268 | ||
269 | if (!stream_class || stream_class->frozen) { | |
270 | ret = -1; | |
271 | goto end; | |
272 | } | |
273 | ||
274 | ret = bt_ctf_stream_class_set_id_no_check(stream_class, id); | |
275 | end: | |
276 | return ret; | |
277 | } | |
278 | ||
0d23acbe PP |
279 | static |
280 | void event_class_exists(gpointer element, gpointer query) | |
281 | { | |
282 | struct bt_ctf_event_class *event_class_a = element; | |
283 | struct search_query *search_query = query; | |
284 | struct bt_ctf_event_class *event_class_b = search_query->value; | |
285 | int64_t id_a, id_b; | |
286 | ||
287 | if (search_query->value == element) { | |
288 | search_query->found = 1; | |
289 | goto end; | |
290 | } | |
291 | ||
292 | /* | |
293 | * Two event classes cannot share the same name in a given | |
294 | * stream class. | |
295 | */ | |
296 | if (!strcmp(bt_ctf_event_class_get_name(event_class_a), | |
297 | bt_ctf_event_class_get_name(event_class_b))) { | |
298 | search_query->found = 1; | |
299 | goto end; | |
300 | } | |
301 | ||
302 | /* | |
303 | * Two event classes cannot share the same ID in a given | |
304 | * stream class. | |
305 | */ | |
306 | id_a = bt_ctf_event_class_get_id(event_class_a); | |
307 | id_b = bt_ctf_event_class_get_id(event_class_b); | |
308 | ||
309 | if (id_a < 0 || id_b < 0) { | |
310 | /* at least one ID is not set: will be automatically set later */ | |
311 | goto end; | |
312 | } | |
313 | ||
314 | if (id_a == id_b) { | |
315 | search_query->found = 1; | |
316 | goto end; | |
317 | } | |
318 | ||
319 | end: | |
320 | return; | |
321 | } | |
322 | ||
11b0cdc8 JG |
323 | int bt_ctf_stream_class_add_event_class( |
324 | struct bt_ctf_stream_class *stream_class, | |
325 | struct bt_ctf_event_class *event_class) | |
326 | { | |
327 | int ret = 0; | |
2f100782 | 328 | int64_t event_id; |
11b0cdc8 JG |
329 | |
330 | if (!stream_class || !event_class) { | |
331 | ret = -1; | |
332 | goto end; | |
333 | } | |
334 | ||
335 | /* Check for duplicate event classes */ | |
336 | struct search_query query = { .value = event_class, .found = 0 }; | |
0d23acbe PP |
337 | g_ptr_array_foreach(stream_class->event_classes, event_class_exists, |
338 | &query); | |
11b0cdc8 JG |
339 | if (query.found) { |
340 | ret = -1; | |
341 | goto end; | |
342 | } | |
343 | ||
26079216 JG |
344 | /* |
345 | * Resolve the event's sequence length and variant tags if the | |
346 | * stream is already associated with a trace. Otherwise, this | |
347 | * validation will be performed once the stream is registered | |
348 | * to a trace. | |
349 | */ | |
350 | if (stream_class->trace) { | |
351 | ret = bt_ctf_event_class_resolve_types(event_class, | |
352 | stream_class->trace, stream_class); | |
353 | if (ret) { | |
354 | goto end; | |
355 | } | |
356 | } | |
357 | ||
2f100782 JG |
358 | /* Only set an event id if none was explicitly set before */ |
359 | event_id = bt_ctf_event_class_get_id(event_class); | |
360 | if (event_id < 0) { | |
361 | if (bt_ctf_event_class_set_id(event_class, | |
362 | stream_class->next_event_id++)) { | |
363 | ret = -1; | |
364 | goto end; | |
365 | } | |
366 | } | |
367 | ||
368 | ret = bt_ctf_event_class_set_stream_class(event_class, stream_class); | |
369 | if (ret) { | |
11b0cdc8 JG |
370 | goto end; |
371 | } | |
372 | ||
29664b2a PP |
373 | ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id); |
374 | if (ret) { | |
375 | goto end; | |
376 | } | |
377 | ||
83509119 | 378 | bt_get(event_class); |
11b0cdc8 | 379 | g_ptr_array_add(stream_class->event_classes, event_class); |
58203827 | 380 | bt_ctf_event_class_freeze(event_class); |
5ca83563 JG |
381 | |
382 | if (stream_class->byte_order) { | |
383 | /* | |
384 | * Only set native byte order if it has been initialized | |
385 | * when the stream class was added to a trace. | |
386 | * | |
387 | * If not set here, this will be set when the stream | |
388 | * classe will be added to a trace. | |
389 | */ | |
390 | bt_ctf_event_class_set_native_byte_order(event_class, | |
391 | stream_class->byte_order); | |
392 | } | |
11b0cdc8 JG |
393 | end: |
394 | return ret; | |
395 | } | |
396 | ||
074ee56d | 397 | int bt_ctf_stream_class_get_event_class_count( |
69dc4535 JG |
398 | struct bt_ctf_stream_class *stream_class) |
399 | { | |
074ee56d | 400 | int ret; |
69dc4535 JG |
401 | |
402 | if (!stream_class) { | |
403 | ret = -1; | |
404 | goto end; | |
405 | } | |
406 | ||
074ee56d | 407 | ret = (int) stream_class->event_classes->len; |
69dc4535 JG |
408 | end: |
409 | return ret; | |
410 | } | |
411 | ||
412 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class( | |
074ee56d | 413 | struct bt_ctf_stream_class *stream_class, int index) |
69dc4535 JG |
414 | { |
415 | struct bt_ctf_event_class *event_class = NULL; | |
416 | ||
074ee56d JG |
417 | if (!stream_class || index < 0 || |
418 | index >= stream_class->event_classes->len) { | |
69dc4535 JG |
419 | goto end; |
420 | } | |
421 | ||
422 | event_class = g_ptr_array_index(stream_class->event_classes, index); | |
83509119 | 423 | bt_get(event_class); |
69dc4535 JG |
424 | end: |
425 | return event_class; | |
426 | } | |
427 | ||
428 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name( | |
429 | struct bt_ctf_stream_class *stream_class, const char *name) | |
430 | { | |
431 | size_t i; | |
69dc4535 JG |
432 | struct bt_ctf_event_class *event_class = NULL; |
433 | ||
434 | if (!stream_class || !name) { | |
435 | goto end; | |
436 | } | |
437 | ||
69dc4535 | 438 | for (i = 0; i < stream_class->event_classes->len; i++) { |
b8248cc0 | 439 | struct bt_ctf_event_class *cur_event_class = |
69dc4535 | 440 | g_ptr_array_index(stream_class->event_classes, i); |
b8248cc0 PP |
441 | const char *cur_event_class_name = |
442 | bt_ctf_event_class_get_name(cur_event_class); | |
69dc4535 | 443 | |
b8248cc0 PP |
444 | if (!strcmp(name, cur_event_class_name)) { |
445 | event_class = cur_event_class; | |
83509119 | 446 | bt_get(event_class); |
69dc4535 JG |
447 | goto end; |
448 | } | |
449 | } | |
450 | end: | |
451 | return event_class; | |
452 | } | |
453 | ||
0863f950 PP |
454 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id( |
455 | struct bt_ctf_stream_class *stream_class, uint32_t id) | |
456 | { | |
457 | size_t i; | |
458 | struct bt_ctf_event_class *event_class = NULL; | |
459 | ||
460 | if (!stream_class) { | |
461 | goto end; | |
462 | } | |
463 | ||
464 | for (i = 0; i < stream_class->event_classes->len; i++) { | |
465 | struct bt_ctf_event_class *current_event_class = | |
466 | g_ptr_array_index(stream_class->event_classes, i); | |
467 | ||
468 | if (bt_ctf_event_class_get_id(current_event_class) == id) { | |
469 | event_class = current_event_class; | |
83509119 | 470 | bt_get(event_class); |
0863f950 PP |
471 | goto end; |
472 | } | |
473 | } | |
474 | end: | |
475 | return event_class; | |
476 | } | |
477 | ||
12c8a1a3 JG |
478 | struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type( |
479 | struct bt_ctf_stream_class *stream_class) | |
480 | { | |
481 | struct bt_ctf_field_type *ret = NULL; | |
482 | ||
483 | if (!stream_class) { | |
484 | goto end; | |
485 | } | |
486 | ||
487 | assert(stream_class->packet_context_type); | |
83509119 | 488 | bt_get(stream_class->packet_context_type); |
12c8a1a3 JG |
489 | ret = stream_class->packet_context_type; |
490 | end: | |
491 | return ret; | |
492 | } | |
493 | ||
494 | int bt_ctf_stream_class_set_packet_context_type( | |
495 | struct bt_ctf_stream_class *stream_class, | |
496 | struct bt_ctf_field_type *packet_context_type) | |
497 | { | |
498 | int ret = 0; | |
499 | ||
0a00bfa1 | 500 | if (!stream_class || !packet_context_type || stream_class->frozen) { |
12c8a1a3 JG |
501 | ret = -1; |
502 | goto end; | |
503 | } | |
504 | ||
505 | assert(stream_class->packet_context_type); | |
662e778c JG |
506 | if (stream_class->packet_context_type == packet_context_type) { |
507 | goto end; | |
508 | } | |
b34f4d90 | 509 | if (bt_ctf_field_type_get_type_id(packet_context_type) != |
12c8a1a3 JG |
510 | CTF_TYPE_STRUCT) { |
511 | /* A packet context must be a structure */ | |
512 | ret = -1; | |
513 | goto end; | |
514 | } | |
515 | ||
83509119 JG |
516 | bt_put(stream_class->packet_context_type); |
517 | bt_get(packet_context_type); | |
12c8a1a3 JG |
518 | stream_class->packet_context_type = packet_context_type; |
519 | end: | |
520 | return ret; | |
521 | } | |
522 | ||
662e778c JG |
523 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type( |
524 | struct bt_ctf_stream_class *stream_class) | |
525 | { | |
526 | struct bt_ctf_field_type *ret = NULL; | |
527 | ||
528 | if (!stream_class || !stream_class->event_header_type) { | |
529 | goto end; | |
530 | } | |
531 | ||
532 | assert(stream_class->event_header_type); | |
83509119 | 533 | bt_get(stream_class->event_header_type); |
662e778c JG |
534 | ret = stream_class->event_header_type; |
535 | end: | |
536 | return ret; | |
537 | } | |
538 | ||
539 | int bt_ctf_stream_class_set_event_header_type( | |
540 | struct bt_ctf_stream_class *stream_class, | |
541 | struct bt_ctf_field_type *event_header_type) | |
542 | { | |
543 | int ret = 0; | |
544 | ||
545 | if (!stream_class || !event_header_type || stream_class->frozen) { | |
546 | ret = -1; | |
547 | goto end; | |
548 | } | |
549 | ||
550 | assert(stream_class->event_header_type); | |
551 | if (stream_class->event_header_type == event_header_type) { | |
552 | goto end; | |
553 | } | |
554 | if (bt_ctf_field_type_get_type_id(event_header_type) != | |
555 | CTF_TYPE_STRUCT) { | |
556 | /* An event header must be a structure */ | |
557 | ret = -1; | |
558 | goto end; | |
559 | } | |
560 | ||
83509119 JG |
561 | bt_put(stream_class->event_header_type); |
562 | bt_get(event_header_type); | |
662e778c JG |
563 | stream_class->event_header_type = event_header_type; |
564 | end: | |
565 | return ret; | |
566 | } | |
567 | ||
af181248 JG |
568 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type( |
569 | struct bt_ctf_stream_class *stream_class) | |
570 | { | |
571 | struct bt_ctf_field_type *ret = NULL; | |
572 | ||
573 | if (!stream_class || !stream_class->event_context_type) { | |
574 | goto end; | |
575 | } | |
576 | ||
577 | assert(stream_class->event_context_type); | |
83509119 | 578 | bt_get(stream_class->event_context_type); |
af181248 JG |
579 | ret = stream_class->event_context_type; |
580 | end: | |
581 | return ret; | |
582 | } | |
583 | ||
584 | int bt_ctf_stream_class_set_event_context_type( | |
585 | struct bt_ctf_stream_class *stream_class, | |
586 | struct bt_ctf_field_type *event_context_type) | |
587 | { | |
588 | int ret = 0; | |
589 | ||
590 | if (!stream_class || !event_context_type || stream_class->frozen) { | |
591 | ret = -1; | |
592 | goto end; | |
593 | } | |
594 | ||
595 | if (bt_ctf_field_type_get_type_id(event_context_type) != | |
596 | CTF_TYPE_STRUCT) { | |
597 | /* A packet context must be a structure */ | |
598 | ret = -1; | |
599 | goto end; | |
600 | } | |
601 | ||
83509119 JG |
602 | bt_put(stream_class->event_context_type); |
603 | bt_get(event_context_type); | |
af181248 JG |
604 | stream_class->event_context_type = event_context_type; |
605 | end: | |
606 | return ret; | |
607 | } | |
608 | ||
11b0cdc8 JG |
609 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) |
610 | { | |
83509119 | 611 | bt_get(stream_class); |
11b0cdc8 JG |
612 | } |
613 | ||
614 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) | |
615 | { | |
83509119 | 616 | bt_put(stream_class); |
11b0cdc8 JG |
617 | } |
618 | ||
619 | BT_HIDDEN | |
620 | void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class) | |
621 | { | |
622 | if (!stream_class) { | |
623 | return; | |
624 | } | |
625 | ||
626 | stream_class->frozen = 1; | |
662e778c | 627 | bt_ctf_field_type_freeze(stream_class->event_header_type); |
12c8a1a3 | 628 | bt_ctf_field_type_freeze(stream_class->packet_context_type); |
af181248 | 629 | bt_ctf_field_type_freeze(stream_class->event_context_type); |
11b0cdc8 | 630 | bt_ctf_clock_freeze(stream_class->clock); |
11b0cdc8 JG |
631 | } |
632 | ||
11b0cdc8 JG |
633 | BT_HIDDEN |
634 | int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class, | |
635 | enum bt_ctf_byte_order byte_order) | |
636 | { | |
5ca83563 | 637 | int i, ret = 0; |
c35a1669 | 638 | int internal_byte_order; |
11b0cdc8 | 639 | |
c35a1669 JG |
640 | /* Note that "NATIVE" means the trace's endianness, not the host's. */ |
641 | if (!stream_class || byte_order <= BT_CTF_BYTE_ORDER_UNKNOWN || | |
5ca83563 | 642 | byte_order > BT_CTF_BYTE_ORDER_NETWORK) { |
c35a1669 JG |
643 | ret = -1; |
644 | goto end; | |
645 | } | |
646 | ||
647 | switch (byte_order) { | |
648 | case BT_CTF_BYTE_ORDER_NETWORK: | |
649 | case BT_CTF_BYTE_ORDER_BIG_ENDIAN: | |
650 | internal_byte_order = BIG_ENDIAN; | |
651 | break; | |
652 | case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN: | |
653 | internal_byte_order = LITTLE_ENDIAN; | |
654 | break; | |
655 | default: | |
656 | ret = -1; | |
11b0cdc8 JG |
657 | goto end; |
658 | } | |
c35a1669 JG |
659 | |
660 | stream_class->byte_order = internal_byte_order; | |
5ca83563 JG |
661 | |
662 | /* Set native byte order to little or big endian */ | |
663 | bt_ctf_field_type_set_native_byte_order( | |
664 | stream_class->event_header_type, stream_class->byte_order); | |
665 | bt_ctf_field_type_set_native_byte_order( | |
666 | stream_class->packet_context_type, stream_class->byte_order); | |
667 | bt_ctf_field_type_set_native_byte_order( | |
668 | stream_class->event_context_type, stream_class->byte_order); | |
669 | ||
670 | /* Set all events' native byte order */ | |
671 | for (i = 0; i < stream_class->event_classes->len; i++) { | |
672 | bt_ctf_event_class_set_native_byte_order( | |
673 | g_ptr_array_index(stream_class->event_classes, i), | |
674 | stream_class->byte_order); | |
675 | bt_ctf_event_class_freeze( | |
676 | g_ptr_array_index(stream_class->event_classes, i)); | |
677 | } | |
11b0cdc8 JG |
678 | end: |
679 | return ret; | |
680 | } | |
681 | ||
682 | BT_HIDDEN | |
683 | int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class, | |
684 | struct metadata_context *context) | |
685 | { | |
2f100782 | 686 | int64_t ret = 0; |
11b0cdc8 JG |
687 | size_t i; |
688 | ||
689 | g_string_assign(context->field_name, ""); | |
690 | context->current_indentation_level = 1; | |
691 | if (!stream_class->id_set) { | |
692 | ret = -1; | |
693 | goto end; | |
694 | } | |
695 | ||
696 | g_string_append_printf(context->string, | |
697 | "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ", | |
698 | stream_class->id); | |
699 | ret = bt_ctf_field_type_serialize(stream_class->event_header_type, | |
700 | context); | |
701 | if (ret) { | |
702 | goto end; | |
703 | } | |
704 | ||
705 | g_string_append(context->string, ";\n\n\tpacket.context := "); | |
706 | ret = bt_ctf_field_type_serialize(stream_class->packet_context_type, | |
707 | context); | |
708 | if (ret) { | |
709 | goto end; | |
710 | } | |
711 | ||
712 | if (stream_class->event_context_type) { | |
713 | g_string_append(context->string, ";\n\n\tevent.context := "); | |
714 | ret = bt_ctf_field_type_serialize( | |
715 | stream_class->event_context_type, context); | |
716 | if (ret) { | |
717 | goto end; | |
718 | } | |
719 | } | |
720 | ||
721 | g_string_append(context->string, ";\n};\n\n"); | |
11b0cdc8 JG |
722 | for (i = 0; i < stream_class->event_classes->len; i++) { |
723 | struct bt_ctf_event_class *event_class = | |
724 | stream_class->event_classes->pdata[i]; | |
725 | ||
11b0cdc8 JG |
726 | ret = bt_ctf_event_class_serialize(event_class, context); |
727 | if (ret) { | |
728 | goto end; | |
729 | } | |
730 | } | |
731 | end: | |
732 | context->current_indentation_level = 0; | |
733 | return ret; | |
734 | } | |
735 | ||
d3814b54 JG |
736 | BT_HIDDEN |
737 | int bt_ctf_stream_class_set_trace(struct bt_ctf_stream_class *stream_class, | |
738 | struct bt_ctf_trace *trace) | |
739 | { | |
740 | int ret = 0; | |
741 | ||
742 | if (!stream_class) { | |
743 | ret = -1; | |
744 | goto end; | |
745 | } | |
746 | ||
747 | if (stream_class->trace && trace) { | |
748 | /* Already attached to a trace */ | |
749 | ret = -1; | |
750 | goto end; | |
751 | } | |
752 | ||
753 | stream_class->trace = trace; | |
754 | end: | |
755 | return ret; | |
756 | } | |
757 | ||
11b0cdc8 | 758 | static |
83509119 | 759 | void bt_ctf_stream_class_destroy(struct bt_object *obj) |
11b0cdc8 JG |
760 | { |
761 | struct bt_ctf_stream_class *stream_class; | |
762 | ||
83509119 JG |
763 | stream_class = container_of(obj, struct bt_ctf_stream_class, base); |
764 | bt_put(stream_class->clock); | |
11b0cdc8 JG |
765 | |
766 | if (stream_class->event_classes) { | |
2f100782 JG |
767 | size_t i; |
768 | ||
769 | /* Unregister this stream class from the event classes */ | |
770 | for (i = 0; i < stream_class->event_classes->len; i++) { | |
771 | struct bt_ctf_event_class *event_class = | |
772 | g_ptr_array_index(stream_class->event_classes, | |
773 | i); | |
774 | ||
775 | bt_ctf_event_class_set_stream_class(event_class, NULL); | |
776 | } | |
777 | ||
11b0cdc8 JG |
778 | g_ptr_array_free(stream_class->event_classes, TRUE); |
779 | } | |
780 | ||
781 | if (stream_class->name) { | |
782 | g_string_free(stream_class->name, TRUE); | |
783 | } | |
784 | ||
83509119 JG |
785 | bt_put(stream_class->event_header_type); |
786 | bt_put(stream_class->packet_context_type); | |
787 | bt_put(stream_class->event_context_type); | |
11b0cdc8 JG |
788 | g_free(stream_class); |
789 | } | |
790 | ||
791 | static | |
662e778c | 792 | int init_event_header(struct bt_ctf_stream_class *stream_class) |
11b0cdc8 JG |
793 | { |
794 | int ret = 0; | |
795 | struct bt_ctf_field_type *event_header_type = | |
796 | bt_ctf_field_type_structure_create(); | |
797 | struct bt_ctf_field_type *_uint32_t = | |
798 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
799 | struct bt_ctf_field_type *_uint64_t = | |
800 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); | |
801 | ||
802 | if (!event_header_type) { | |
803 | ret = -1; | |
804 | goto end; | |
805 | } | |
806 | ||
11b0cdc8 JG |
807 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
808 | _uint32_t, "id"); | |
809 | if (ret) { | |
810 | goto end; | |
811 | } | |
812 | ||
813 | ret = bt_ctf_field_type_structure_add_field(event_header_type, | |
814 | _uint64_t, "timestamp"); | |
815 | if (ret) { | |
816 | goto end; | |
817 | } | |
818 | ||
662e778c | 819 | if (stream_class->event_header_type) { |
83509119 | 820 | bt_put(stream_class->event_header_type); |
de876b7f | 821 | } |
662e778c | 822 | stream_class->event_header_type = event_header_type; |
11b0cdc8 JG |
823 | end: |
824 | if (ret) { | |
83509119 | 825 | bt_put(event_header_type); |
11b0cdc8 JG |
826 | } |
827 | ||
83509119 JG |
828 | bt_put(_uint32_t); |
829 | bt_put(_uint64_t); | |
11b0cdc8 JG |
830 | return ret; |
831 | } | |
832 | ||
833 | static | |
662e778c | 834 | int init_packet_context(struct bt_ctf_stream_class *stream_class) |
11b0cdc8 JG |
835 | { |
836 | int ret = 0; | |
837 | struct bt_ctf_field_type *packet_context_type = | |
838 | bt_ctf_field_type_structure_create(); | |
839 | struct bt_ctf_field_type *_uint64_t = | |
840 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); | |
841 | ||
842 | if (!packet_context_type) { | |
843 | ret = -1; | |
844 | goto end; | |
845 | } | |
846 | ||
847 | /* | |
848 | * We create a stream packet context as proposed in the CTF | |
849 | * specification. | |
850 | */ | |
11b0cdc8 JG |
851 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
852 | _uint64_t, "timestamp_begin"); | |
853 | if (ret) { | |
854 | goto end; | |
855 | } | |
856 | ||
857 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
858 | _uint64_t, "timestamp_end"); | |
859 | if (ret) { | |
860 | goto end; | |
861 | } | |
862 | ||
863 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
864 | _uint64_t, "content_size"); | |
865 | if (ret) { | |
866 | goto end; | |
867 | } | |
868 | ||
869 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
870 | _uint64_t, "packet_size"); | |
871 | if (ret) { | |
872 | goto end; | |
873 | } | |
874 | ||
875 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
876 | _uint64_t, "events_discarded"); | |
877 | if (ret) { | |
878 | goto end; | |
879 | } | |
880 | ||
83509119 | 881 | bt_put(stream_class->packet_context_type); |
11b0cdc8 | 882 | stream_class->packet_context_type = packet_context_type; |
11b0cdc8 JG |
883 | end: |
884 | if (ret) { | |
83509119 | 885 | bt_put(packet_context_type); |
11b0cdc8 JG |
886 | goto end; |
887 | } | |
888 | ||
83509119 | 889 | bt_put(_uint64_t); |
11b0cdc8 JG |
890 | return ret; |
891 | } |