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 | ||
d2f71f12 PP |
29 | #define BT_LOG_TAG "STREAM-CLASS" |
30 | #include <babeltrace/lib-logging-internal.h> | |
31 | ||
11b0cdc8 | 32 | #include <babeltrace/ctf-writer/clock.h> |
ac0c6bdd PP |
33 | #include <babeltrace/ctf-writer/clock-internal.h> |
34 | #include <babeltrace/ctf-ir/clock-class-internal.h> | |
11b0cdc8 | 35 | #include <babeltrace/ctf-writer/event.h> |
272df73e | 36 | #include <babeltrace/ctf-ir/event-class-internal.h> |
11b0cdc8 | 37 | #include <babeltrace/ctf-ir/event-internal.h> |
2e33ac5a PP |
38 | #include <babeltrace/ctf-ir/field-types-internal.h> |
39 | #include <babeltrace/ctf-ir/fields-internal.h> | |
11b0cdc8 JG |
40 | #include <babeltrace/ctf-writer/stream.h> |
41 | #include <babeltrace/ctf-ir/stream-class-internal.h> | |
09840de5 | 42 | #include <babeltrace/ctf-ir/validation-internal.h> |
8bf65fbd | 43 | #include <babeltrace/ctf-ir/visitor-internal.h> |
11b0cdc8 | 44 | #include <babeltrace/ctf-writer/functor-internal.h> |
654c1444 | 45 | #include <babeltrace/ctf-ir/utils.h> |
83509119 | 46 | #include <babeltrace/ref.h> |
3d9990ac PP |
47 | #include <babeltrace/compiler-internal.h> |
48 | #include <babeltrace/align-internal.h> | |
49 | #include <babeltrace/endian-internal.h> | |
dc3fffef | 50 | #include <inttypes.h> |
544d0515 | 51 | #include <stdint.h> |
e011d2c1 | 52 | #include <stdbool.h> |
11b0cdc8 JG |
53 | |
54 | static | |
83509119 | 55 | void bt_ctf_stream_class_destroy(struct bt_object *obj); |
11b0cdc8 | 56 | static |
662e778c | 57 | int init_event_header(struct bt_ctf_stream_class *stream_class); |
11b0cdc8 | 58 | static |
662e778c | 59 | int init_packet_context(struct bt_ctf_stream_class *stream_class); |
11b0cdc8 JG |
60 | |
61 | struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name) | |
62 | { | |
d2f71f12 | 63 | struct bt_ctf_stream_class *stream_class; |
12c8a1a3 | 64 | int ret; |
e0e2946b | 65 | |
d2f71f12 PP |
66 | BT_LOGD("Creating default stream class object: name=\"%s\"", name); |
67 | stream_class = bt_ctf_stream_class_create_empty(name); | |
e0e2946b | 68 | if (!stream_class) { |
d2f71f12 | 69 | BT_LOGD_STR("Cannot create empty stream class."); |
e0e2946b PP |
70 | goto error; |
71 | } | |
72 | ||
73 | ret = init_event_header(stream_class); | |
74 | if (ret) { | |
d2f71f12 | 75 | BT_LOGE_STR("Cannot initialize stream class's event header field type."); |
e0e2946b PP |
76 | goto error; |
77 | } | |
78 | ||
79 | ret = init_packet_context(stream_class); | |
80 | if (ret) { | |
d2f71f12 | 81 | BT_LOGE_STR("Cannot initialize stream class's packet context field type."); |
e0e2946b PP |
82 | goto error; |
83 | } | |
84 | ||
d2f71f12 PP |
85 | BT_LOGD("Created default stream class object: addr=%p, name=\"%s\"", |
86 | stream_class, name); | |
e0e2946b PP |
87 | return stream_class; |
88 | ||
89 | error: | |
90 | BT_PUT(stream_class); | |
91 | return stream_class; | |
92 | } | |
93 | ||
94 | struct bt_ctf_stream_class *bt_ctf_stream_class_create_empty(const char *name) | |
95 | { | |
11b0cdc8 JG |
96 | struct bt_ctf_stream_class *stream_class = NULL; |
97 | ||
d2f71f12 PP |
98 | BT_LOGD("Creating empty stream class object: name=\"%s\"", name); |
99 | ||
3ea33115 | 100 | if (name && bt_ctf_validate_identifier(name)) { |
d2f71f12 PP |
101 | BT_LOGW("Invalid parameter: stream class's name is not a valid CTF identifier: " |
102 | "name=\"%s\"", name); | |
11b0cdc8 JG |
103 | goto error; |
104 | } | |
105 | ||
106 | stream_class = g_new0(struct bt_ctf_stream_class, 1); | |
107 | if (!stream_class) { | |
d2f71f12 | 108 | BT_LOGE_STR("Failed to allocate one stream class."); |
11b0cdc8 JG |
109 | goto error; |
110 | } | |
111 | ||
112 | stream_class->name = g_string_new(name); | |
113 | stream_class->event_classes = g_ptr_array_new_with_free_func( | |
e6a8e8e4 | 114 | (GDestroyNotify) bt_object_release); |
11b0cdc8 | 115 | if (!stream_class->event_classes) { |
d2f71f12 | 116 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
83509119 | 117 | goto error; |
11b0cdc8 JG |
118 | } |
119 | ||
0b9ce69f JG |
120 | stream_class->event_classes_ht = g_hash_table_new_full(g_int64_hash, |
121 | g_int64_equal, g_free, NULL); | |
d2f71f12 PP |
122 | if (!stream_class->event_classes_ht) { |
123 | BT_LOGE_STR("Failed to allocate a GHashTable."); | |
124 | goto error; | |
125 | } | |
0b9ce69f | 126 | |
e0e2946b PP |
127 | stream_class->packet_context_type = bt_ctf_field_type_structure_create(); |
128 | if (!stream_class->packet_context_type) { | |
40f7101a | 129 | BT_LOGE_STR("Cannot create stream class's initial packet context field type."); |
83509119 | 130 | goto error; |
662e778c JG |
131 | } |
132 | ||
e0e2946b PP |
133 | stream_class->event_header_type = bt_ctf_field_type_structure_create(); |
134 | if (!stream_class->event_header_type) { | |
40f7101a | 135 | BT_LOGE_STR("Cannot create stream class's initial event header field type."); |
e0e2946b PP |
136 | goto error; |
137 | } | |
138 | ||
139 | stream_class->event_context_type = bt_ctf_field_type_structure_create(); | |
140 | if (!stream_class->event_context_type) { | |
40f7101a | 141 | BT_LOGE_STR("Cannot create stream class's initial event context field type."); |
83509119 | 142 | goto error; |
12c8a1a3 JG |
143 | } |
144 | ||
83509119 | 145 | bt_object_init(stream_class, bt_ctf_stream_class_destroy); |
d2f71f12 PP |
146 | BT_LOGD("Created empty stream class object: addr=%p, name=\"%s\"", |
147 | stream_class, name); | |
11b0cdc8 JG |
148 | return stream_class; |
149 | ||
11b0cdc8 | 150 | error: |
e0e2946b | 151 | BT_PUT(stream_class); |
11b0cdc8 JG |
152 | return stream_class; |
153 | } | |
154 | ||
142c5610 JG |
155 | struct bt_ctf_trace *bt_ctf_stream_class_get_trace( |
156 | struct bt_ctf_stream_class *stream_class) | |
157 | { | |
dc3fffef PP |
158 | return stream_class ? |
159 | bt_get(bt_ctf_stream_class_borrow_trace(stream_class)) : | |
160 | NULL; | |
142c5610 JG |
161 | } |
162 | ||
69dc4535 JG |
163 | const char *bt_ctf_stream_class_get_name( |
164 | struct bt_ctf_stream_class *stream_class) | |
165 | { | |
166 | const char *name = NULL; | |
167 | ||
168 | if (!stream_class) { | |
d2f71f12 | 169 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
69dc4535 JG |
170 | goto end; |
171 | } | |
172 | ||
173 | name = stream_class->name->str; | |
174 | end: | |
175 | return name; | |
176 | } | |
177 | ||
3ea33115 JG |
178 | int bt_ctf_stream_class_set_name(struct bt_ctf_stream_class *stream_class, |
179 | const char *name) | |
180 | { | |
181 | int ret = 0; | |
182 | ||
d2f71f12 PP |
183 | if (!stream_class) { |
184 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
185 | ret = -1; | |
186 | goto end; | |
187 | } | |
188 | ||
189 | if (stream_class->frozen) { | |
190 | BT_LOGW("Invalid parameter: stream class is frozen: " | |
191 | "addr=%p, name=\"%s\", id=%" PRId64, | |
192 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
193 | bt_ctf_stream_class_get_id(stream_class)); | |
3ea33115 JG |
194 | ret = -1; |
195 | goto end; | |
196 | } | |
197 | ||
198 | g_string_assign(stream_class->name, name); | |
d2f71f12 PP |
199 | BT_LOGV("Set stream class's name: " |
200 | "addr=%p, name=\"%s\", id=%" PRId64, | |
201 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
202 | bt_ctf_stream_class_get_id(stream_class)); | |
3ea33115 JG |
203 | end: |
204 | return ret; | |
205 | } | |
206 | ||
2f100782 JG |
207 | struct bt_ctf_clock *bt_ctf_stream_class_get_clock( |
208 | struct bt_ctf_stream_class *stream_class) | |
209 | { | |
210 | struct bt_ctf_clock *clock = NULL; | |
211 | ||
d2f71f12 PP |
212 | if (!stream_class) { |
213 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
214 | goto end; | |
215 | } | |
216 | ||
217 | if (!stream_class->clock) { | |
218 | BT_LOGV("Stream class has no clock: " | |
219 | "addr=%p, name=\"%s\", id=%" PRId64, | |
220 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
221 | bt_ctf_stream_class_get_id(stream_class)); | |
2f100782 JG |
222 | goto end; |
223 | } | |
224 | ||
ac0c6bdd | 225 | clock = bt_get(stream_class->clock); |
2f100782 JG |
226 | end: |
227 | return clock; | |
228 | } | |
229 | ||
11b0cdc8 JG |
230 | int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class, |
231 | struct bt_ctf_clock *clock) | |
232 | { | |
233 | int ret = 0; | |
eee752e5 | 234 | struct bt_ctf_field_type *timestamp_field = NULL; |
11b0cdc8 | 235 | |
d2f71f12 PP |
236 | if (!stream_class || !clock) { |
237 | BT_LOGW("Invalid parameter: stream class or clock is NULL: " | |
238 | "stream-class-addr=%p, clock-addr=%p", | |
239 | stream_class, clock); | |
240 | ret = -1; | |
241 | goto end; | |
242 | } | |
243 | ||
244 | if (stream_class->frozen) { | |
245 | BT_LOGW("Invalid parameter: stream class is frozen: " | |
246 | "addr=%p, name=\"%s\", id=%" PRId64, | |
247 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
248 | bt_ctf_stream_class_get_id(stream_class)); | |
11b0cdc8 JG |
249 | ret = -1; |
250 | goto end; | |
251 | } | |
252 | ||
ac0c6bdd PP |
253 | /* Replace the current clock of this stream class. */ |
254 | bt_put(stream_class->clock); | |
255 | stream_class->clock = bt_get(clock); | |
d2f71f12 PP |
256 | BT_LOGV("Set stream class's clock: " |
257 | "addr=%p, name=\"%s\", id=%" PRId64 ", " | |
258 | "clock-addr=%p, clock-name=\"%s\"", | |
259 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
260 | bt_ctf_stream_class_get_id(stream_class), | |
261 | stream_class->clock, | |
262 | bt_ctf_clock_get_name(stream_class->clock)); | |
11b0cdc8 | 263 | |
11b0cdc8 | 264 | end: |
ac0c6bdd | 265 | bt_put(timestamp_field); |
11b0cdc8 JG |
266 | return ret; |
267 | } | |
268 | ||
2f100782 JG |
269 | int64_t bt_ctf_stream_class_get_id(struct bt_ctf_stream_class *stream_class) |
270 | { | |
271 | int64_t ret; | |
272 | ||
d2f71f12 PP |
273 | if (!stream_class) { |
274 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
275 | ret = (int64_t) -1; | |
276 | goto end; | |
277 | } | |
278 | ||
279 | if (!stream_class->id_set) { | |
280 | BT_LOGV("Stream class's ID is not set: addr=%p, name=\"%s\"", | |
281 | stream_class, | |
282 | bt_ctf_stream_class_get_name(stream_class)); | |
9ac68eb1 | 283 | ret = (int64_t) -1; |
2f100782 JG |
284 | goto end; |
285 | } | |
286 | ||
9ac68eb1 | 287 | ret = stream_class->id; |
2f100782 JG |
288 | end: |
289 | return ret; | |
290 | } | |
291 | ||
5ca83563 | 292 | BT_HIDDEN |
d2f71f12 | 293 | void _bt_ctf_stream_class_set_id( |
9ac68eb1 | 294 | struct bt_ctf_stream_class *stream_class, int64_t id) |
5ca83563 | 295 | { |
d2f71f12 | 296 | assert(stream_class); |
5ca83563 JG |
297 | stream_class->id = id; |
298 | stream_class->id_set = 1; | |
d2f71f12 PP |
299 | BT_LOGV("Set stream class's ID (internal): " |
300 | "addr=%p, name=\"%s\", id=%" PRId64, | |
301 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
302 | bt_ctf_stream_class_get_id(stream_class)); | |
5ca83563 JG |
303 | } |
304 | ||
9ac68eb1 PP |
305 | struct event_class_set_stream_class_id_data { |
306 | int64_t stream_class_id; | |
29664b2a PP |
307 | int ret; |
308 | }; | |
309 | ||
310 | static | |
311 | void event_class_set_stream_id(gpointer event_class, gpointer data) | |
312 | { | |
9ac68eb1 | 313 | struct event_class_set_stream_class_id_data *typed_data = data; |
29664b2a PP |
314 | |
315 | typed_data->ret |= bt_ctf_event_class_set_stream_id(event_class, | |
9ac68eb1 | 316 | typed_data->stream_class_id); |
29664b2a PP |
317 | } |
318 | ||
319 | BT_HIDDEN | |
320 | int bt_ctf_stream_class_set_id_no_check( | |
9ac68eb1 | 321 | struct bt_ctf_stream_class *stream_class, int64_t id) |
2f100782 JG |
322 | { |
323 | int ret = 0; | |
9ac68eb1 PP |
324 | struct event_class_set_stream_class_id_data data = |
325 | { .stream_class_id = id, .ret = 0 }; | |
2f100782 | 326 | |
29664b2a PP |
327 | /* |
328 | * Make sure all event classes have their "stream_id" attribute | |
329 | * set to this value. | |
330 | */ | |
331 | g_ptr_array_foreach(stream_class->event_classes, | |
332 | event_class_set_stream_id, &data); | |
333 | ret = data.ret; | |
334 | if (ret) { | |
d2f71f12 PP |
335 | BT_LOGE("Cannot set the IDs of all stream class's event classes: " |
336 | "addr=%p, name=\"%s\", id=%" PRId64, | |
337 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
338 | bt_ctf_stream_class_get_id(stream_class)); | |
2f100782 JG |
339 | goto end; |
340 | } | |
341 | ||
d2f71f12 | 342 | _bt_ctf_stream_class_set_id(stream_class, id); |
2f100782 JG |
343 | end: |
344 | return ret; | |
345 | } | |
346 | ||
29664b2a | 347 | int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class, |
9ac68eb1 | 348 | uint64_t id_param) |
29664b2a PP |
349 | { |
350 | int ret = 0; | |
9ac68eb1 | 351 | int64_t id = (int64_t) id_param; |
29664b2a | 352 | |
d2f71f12 PP |
353 | if (!stream_class) { |
354 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
355 | ret = -1; | |
356 | goto end; | |
357 | } | |
358 | ||
359 | if (stream_class->frozen) { | |
360 | BT_LOGW("Invalid parameter: stream class is frozen: " | |
361 | "addr=%p, name=\"%s\", id=%" PRId64, | |
362 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
363 | bt_ctf_stream_class_get_id(stream_class)); | |
364 | ret = -1; | |
365 | goto end; | |
366 | } | |
367 | ||
368 | if (id < 0) { | |
369 | BT_LOGW("Invalid parameter: invalid stream class's ID: " | |
370 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
371 | "stream-class-id=%" PRId64 ", id=%" PRIu64, | |
372 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
373 | bt_ctf_stream_class_get_id(stream_class), | |
374 | id_param); | |
29664b2a PP |
375 | ret = -1; |
376 | goto end; | |
377 | } | |
378 | ||
379 | ret = bt_ctf_stream_class_set_id_no_check(stream_class, id); | |
d2f71f12 PP |
380 | if (ret == 0) { |
381 | BT_LOGV("Set stream class's ID: " | |
382 | "addr=%p, name=\"%s\", id=%" PRId64, | |
383 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
384 | bt_ctf_stream_class_get_id(stream_class)); | |
385 | } | |
29664b2a PP |
386 | end: |
387 | return ret; | |
388 | } | |
389 | ||
0d23acbe PP |
390 | static |
391 | void event_class_exists(gpointer element, gpointer query) | |
392 | { | |
393 | struct bt_ctf_event_class *event_class_a = element; | |
394 | struct search_query *search_query = query; | |
395 | struct bt_ctf_event_class *event_class_b = search_query->value; | |
396 | int64_t id_a, id_b; | |
397 | ||
398 | if (search_query->value == element) { | |
399 | search_query->found = 1; | |
400 | goto end; | |
401 | } | |
402 | ||
0d23acbe PP |
403 | /* |
404 | * Two event classes cannot share the same ID in a given | |
405 | * stream class. | |
406 | */ | |
407 | id_a = bt_ctf_event_class_get_id(event_class_a); | |
408 | id_b = bt_ctf_event_class_get_id(event_class_b); | |
409 | ||
410 | if (id_a < 0 || id_b < 0) { | |
411 | /* at least one ID is not set: will be automatically set later */ | |
412 | goto end; | |
413 | } | |
414 | ||
415 | if (id_a == id_b) { | |
66871d36 | 416 | BT_LOGW("Event class with this ID already exists in the stream class: " |
d2f71f12 PP |
417 | "id=%" PRId64 ", name=\"%s\"", |
418 | id_a, bt_ctf_event_class_get_name(event_class_a)); | |
0d23acbe PP |
419 | search_query->found = 1; |
420 | goto end; | |
421 | } | |
422 | ||
423 | end: | |
424 | return; | |
425 | } | |
426 | ||
11b0cdc8 JG |
427 | int bt_ctf_stream_class_add_event_class( |
428 | struct bt_ctf_stream_class *stream_class, | |
429 | struct bt_ctf_event_class *event_class) | |
430 | { | |
431 | int ret = 0; | |
0b9ce69f | 432 | int64_t *event_id = NULL; |
e6a8e8e4 JG |
433 | struct bt_ctf_trace *trace = NULL; |
434 | struct bt_ctf_stream_class *old_stream_class = NULL; | |
09840de5 PP |
435 | struct bt_ctf_validation_output validation_output = { 0 }; |
436 | struct bt_ctf_field_type *packet_header_type = NULL; | |
437 | struct bt_ctf_field_type *packet_context_type = NULL; | |
438 | struct bt_ctf_field_type *event_header_type = NULL; | |
439 | struct bt_ctf_field_type *stream_event_ctx_type = NULL; | |
440 | struct bt_ctf_field_type *event_context_type = NULL; | |
441 | struct bt_ctf_field_type *event_payload_type = NULL; | |
442 | const enum bt_ctf_validation_flag validation_flags = | |
443 | BT_CTF_VALIDATION_FLAG_EVENT; | |
11b0cdc8 | 444 | |
e011d2c1 PP |
445 | if (!stream_class || !event_class) { |
446 | BT_LOGW("Invalid parameter: stream class or event class is NULL: " | |
447 | "stream-class-addr=%p, event-class-addr=%p", | |
448 | stream_class, event_class); | |
449 | ret = -1; | |
450 | goto end; | |
451 | } | |
452 | ||
d2f71f12 PP |
453 | BT_LOGD("Adding event class to stream class: " |
454 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
455 | "stream-class-id=%" PRId64 ", event-class-addr=%p, " | |
456 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
457 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
458 | bt_ctf_stream_class_get_id(stream_class), | |
459 | event_class, | |
460 | bt_ctf_event_class_get_name(event_class), | |
461 | bt_ctf_event_class_get_id(event_class)); | |
462 | ||
5acf2ae6 PP |
463 | trace = bt_ctf_stream_class_get_trace(stream_class); |
464 | if (trace && trace->is_static) { | |
465 | ret = -1; | |
466 | goto end; | |
467 | } | |
468 | ||
0b9ce69f JG |
469 | event_id = g_new(int64_t, 1); |
470 | if (!event_id) { | |
d2f71f12 | 471 | BT_LOGE_STR("Failed to allocate one int64_t."); |
0b9ce69f JG |
472 | ret = -1; |
473 | goto end; | |
474 | } | |
475 | ||
11b0cdc8 JG |
476 | /* Check for duplicate event classes */ |
477 | struct search_query query = { .value = event_class, .found = 0 }; | |
0d23acbe PP |
478 | g_ptr_array_foreach(stream_class->event_classes, event_class_exists, |
479 | &query); | |
11b0cdc8 | 480 | if (query.found) { |
d2f71f12 | 481 | BT_LOGW_STR("Another event class part of this stream class has the same ID."); |
11b0cdc8 JG |
482 | ret = -1; |
483 | goto end; | |
484 | } | |
485 | ||
09840de5 | 486 | old_stream_class = bt_ctf_event_class_get_stream_class(event_class); |
e6a8e8e4 JG |
487 | if (old_stream_class) { |
488 | /* Event class is already associated to a stream class. */ | |
d2f71f12 PP |
489 | BT_LOGW("Event class is already part of another stream class: " |
490 | "event-class-stream-class-addr=%p, " | |
491 | "event-class-stream-class-name=\"%s\", " | |
492 | "event-class-stream-class-id=%" PRId64, | |
493 | old_stream_class, | |
494 | bt_ctf_stream_class_get_name(old_stream_class), | |
495 | bt_ctf_stream_class_get_id(old_stream_class)); | |
e6a8e8e4 JG |
496 | ret = -1; |
497 | goto end; | |
498 | } | |
499 | ||
e6a8e8e4 | 500 | if (trace) { |
09840de5 PP |
501 | /* |
502 | * If the stream class is associated with a trace, then | |
503 | * both those objects are frozen. Also, this event class | |
504 | * is about to be frozen. | |
505 | * | |
506 | * Therefore the event class must be validated here. | |
507 | * The trace and stream class should be valid at this | |
508 | * point. | |
509 | */ | |
510 | assert(trace->valid); | |
511 | assert(stream_class->valid); | |
512 | packet_header_type = | |
513 | bt_ctf_trace_get_packet_header_type(trace); | |
514 | packet_context_type = | |
515 | bt_ctf_stream_class_get_packet_context_type( | |
516 | stream_class); | |
517 | event_header_type = | |
518 | bt_ctf_stream_class_get_event_header_type(stream_class); | |
519 | stream_event_ctx_type = | |
520 | bt_ctf_stream_class_get_event_context_type( | |
521 | stream_class); | |
522 | event_context_type = | |
523 | bt_ctf_event_class_get_context_type(event_class); | |
524 | event_payload_type = | |
525 | bt_ctf_event_class_get_payload_type(event_class); | |
526 | ret = bt_ctf_validate_class_types( | |
527 | trace->environment, packet_header_type, | |
528 | packet_context_type, event_header_type, | |
529 | stream_event_ctx_type, event_context_type, | |
530 | event_payload_type, trace->valid, | |
531 | stream_class->valid, event_class->valid, | |
532 | &validation_output, validation_flags); | |
533 | BT_PUT(packet_header_type); | |
534 | BT_PUT(packet_context_type); | |
535 | BT_PUT(event_header_type); | |
536 | BT_PUT(stream_event_ctx_type); | |
537 | BT_PUT(event_context_type); | |
538 | BT_PUT(event_payload_type); | |
539 | ||
26079216 | 540 | if (ret) { |
09840de5 PP |
541 | /* |
542 | * This means something went wrong during the | |
543 | * validation process, not that the objects are | |
544 | * invalid. | |
545 | */ | |
d2f71f12 | 546 | BT_LOGE("Failed to validate event class: ret=%d", ret); |
09840de5 PP |
547 | goto end; |
548 | } | |
549 | ||
550 | if ((validation_output.valid_flags & validation_flags) != | |
551 | validation_flags) { | |
552 | /* Invalid event class */ | |
66871d36 | 553 | BT_LOGW("Invalid trace, stream class, or event class: " |
d2f71f12 PP |
554 | "valid-flags=0x%x", |
555 | validation_output.valid_flags); | |
09840de5 | 556 | ret = -1; |
26079216 JG |
557 | goto end; |
558 | } | |
559 | } | |
560 | ||
09840de5 | 561 | /* Only set an event ID if none was explicitly set before */ |
0b9ce69f | 562 | *event_id = bt_ctf_event_class_get_id(event_class); |
24626e8b | 563 | if (*event_id < 0) { |
d2f71f12 PP |
564 | BT_LOGV("Event class has no ID: automatically setting it: " |
565 | "id=%" PRId64, stream_class->next_event_id); | |
566 | ||
2f100782 | 567 | if (bt_ctf_event_class_set_id(event_class, |
d2f71f12 PP |
568 | stream_class->next_event_id)) { |
569 | BT_LOGE("Cannot set event class's ID: id=%" PRId64, | |
570 | stream_class->next_event_id); | |
2f100782 JG |
571 | ret = -1; |
572 | goto end; | |
573 | } | |
d2f71f12 | 574 | stream_class->next_event_id++; |
0b9ce69f | 575 | *event_id = stream_class->next_event_id; |
2f100782 JG |
576 | } |
577 | ||
29664b2a PP |
578 | ret = bt_ctf_event_class_set_stream_id(event_class, stream_class->id); |
579 | if (ret) { | |
d2f71f12 PP |
580 | BT_LOGE("Cannot set event class's stream class ID attribute: ret=%d", |
581 | ret); | |
29664b2a PP |
582 | goto end; |
583 | } | |
584 | ||
e6a8e8e4 | 585 | bt_object_set_parent(event_class, stream_class); |
09840de5 PP |
586 | |
587 | if (trace) { | |
588 | /* | |
589 | * At this point we know that the function will be | |
590 | * successful. Therefore we can replace the event | |
591 | * class's field types with what's in the validation | |
592 | * output structure and mark this event class as valid. | |
593 | */ | |
594 | bt_ctf_validation_replace_types(NULL, NULL, event_class, | |
595 | &validation_output, validation_flags); | |
596 | event_class->valid = 1; | |
597 | ||
598 | /* | |
599 | * Put what was not moved in | |
600 | * bt_ctf_validation_replace_types(). | |
601 | */ | |
602 | bt_ctf_validation_output_put_types(&validation_output); | |
603 | } | |
604 | ||
605 | /* Add to the event classes of the stream class */ | |
11b0cdc8 | 606 | g_ptr_array_add(stream_class->event_classes, event_class); |
0b9ce69f JG |
607 | g_hash_table_insert(stream_class->event_classes_ht, event_id, |
608 | event_class); | |
609 | event_id = NULL; | |
09840de5 PP |
610 | |
611 | /* Freeze the event class */ | |
58203827 | 612 | bt_ctf_event_class_freeze(event_class); |
5ca83563 | 613 | |
9b888ff3 JG |
614 | /* Notifiy listeners of the trace's schema modification. */ |
615 | if (trace) { | |
d9a13d86 PP |
616 | struct bt_ctf_object obj = { .object = event_class, |
617 | .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS }; | |
9b888ff3 | 618 | |
d9a13d86 | 619 | (void) bt_ctf_trace_object_modification(&obj, trace); |
9b888ff3 | 620 | } |
d2f71f12 PP |
621 | |
622 | BT_LOGD("Added event class to stream class: " | |
623 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
624 | "stream-class-id=%" PRId64 ", event-class-addr=%p, " | |
625 | "event-class-name=\"%s\", event-class-id=%" PRId64, | |
626 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
627 | bt_ctf_stream_class_get_id(stream_class), | |
628 | event_class, | |
629 | bt_ctf_event_class_get_name(event_class), | |
630 | bt_ctf_event_class_get_id(event_class)); | |
631 | ||
11b0cdc8 | 632 | end: |
e6a8e8e4 JG |
633 | BT_PUT(trace); |
634 | BT_PUT(old_stream_class); | |
09840de5 PP |
635 | bt_ctf_validation_output_put_types(&validation_output); |
636 | assert(!packet_header_type); | |
637 | assert(!packet_context_type); | |
638 | assert(!event_header_type); | |
639 | assert(!stream_event_ctx_type); | |
640 | assert(!event_context_type); | |
641 | assert(!event_payload_type); | |
0b9ce69f | 642 | g_free(event_id); |
09840de5 | 643 | |
11b0cdc8 JG |
644 | return ret; |
645 | } | |
646 | ||
544d0515 | 647 | int64_t bt_ctf_stream_class_get_event_class_count( |
69dc4535 JG |
648 | struct bt_ctf_stream_class *stream_class) |
649 | { | |
544d0515 | 650 | int64_t ret; |
69dc4535 JG |
651 | |
652 | if (!stream_class) { | |
d2f71f12 | 653 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
9ac68eb1 | 654 | ret = (int64_t) -1; |
69dc4535 JG |
655 | goto end; |
656 | } | |
657 | ||
9ac68eb1 | 658 | ret = (int64_t) stream_class->event_classes->len; |
69dc4535 JG |
659 | end: |
660 | return ret; | |
661 | } | |
662 | ||
9ac68eb1 PP |
663 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_index( |
664 | struct bt_ctf_stream_class *stream_class, uint64_t index) | |
69dc4535 JG |
665 | { |
666 | struct bt_ctf_event_class *event_class = NULL; | |
667 | ||
d2f71f12 PP |
668 | if (!stream_class) { |
669 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
670 | goto end; | |
671 | } | |
672 | ||
673 | if (index >= stream_class->event_classes->len) { | |
674 | BT_LOGW("Invalid parameter: index is out of bounds: " | |
675 | "addr=%p, name=\"%s\", id=%" PRId64 ", " | |
676 | "index=%" PRIu64 ", count=%u", | |
677 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
678 | bt_ctf_stream_class_get_id(stream_class), | |
679 | index, stream_class->event_classes->len); | |
69dc4535 JG |
680 | goto end; |
681 | } | |
682 | ||
683 | event_class = g_ptr_array_index(stream_class->event_classes, index); | |
83509119 | 684 | bt_get(event_class); |
69dc4535 JG |
685 | end: |
686 | return event_class; | |
687 | } | |
688 | ||
0863f950 | 689 | struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_id( |
9ac68eb1 | 690 | struct bt_ctf_stream_class *stream_class, uint64_t id) |
0863f950 | 691 | { |
9ac68eb1 | 692 | int64_t id_key = (int64_t) id; |
0863f950 PP |
693 | struct bt_ctf_event_class *event_class = NULL; |
694 | ||
d2f71f12 PP |
695 | if (!stream_class) { |
696 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
697 | goto end; | |
698 | } | |
699 | ||
700 | if (id_key < 0) { | |
701 | BT_LOGW("Invalid parameter: invalid event class's ID: " | |
702 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
703 | "stream-class-id=%" PRId64 ", event-class-id=%" PRIu64, | |
704 | stream_class, | |
705 | bt_ctf_stream_class_get_name(stream_class), | |
706 | bt_ctf_stream_class_get_id(stream_class), id); | |
0863f950 PP |
707 | goto end; |
708 | } | |
709 | ||
0b9ce69f JG |
710 | event_class = g_hash_table_lookup(stream_class->event_classes_ht, |
711 | &id_key); | |
712 | bt_get(event_class); | |
0863f950 PP |
713 | end: |
714 | return event_class; | |
715 | } | |
716 | ||
12c8a1a3 JG |
717 | struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type( |
718 | struct bt_ctf_stream_class *stream_class) | |
719 | { | |
720 | struct bt_ctf_field_type *ret = NULL; | |
721 | ||
722 | if (!stream_class) { | |
d2f71f12 | 723 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); |
12c8a1a3 JG |
724 | goto end; |
725 | } | |
726 | ||
83509119 | 727 | bt_get(stream_class->packet_context_type); |
12c8a1a3 JG |
728 | ret = stream_class->packet_context_type; |
729 | end: | |
730 | return ret; | |
731 | } | |
732 | ||
733 | int bt_ctf_stream_class_set_packet_context_type( | |
734 | struct bt_ctf_stream_class *stream_class, | |
735 | struct bt_ctf_field_type *packet_context_type) | |
736 | { | |
737 | int ret = 0; | |
738 | ||
d2f71f12 PP |
739 | if (!stream_class) { |
740 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
741 | ret = -1; | |
742 | goto end; | |
743 | } | |
744 | ||
745 | if (stream_class->frozen) { | |
746 | BT_LOGW("Invalid parameter: stream class is frozen: " | |
747 | "addr=%p, name=\"%s\", id=%" PRId64, | |
748 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
749 | bt_ctf_stream_class_get_id(stream_class)); | |
12c8a1a3 JG |
750 | ret = -1; |
751 | goto end; | |
752 | } | |
753 | ||
835b2d10 JG |
754 | if (packet_context_type && |
755 | bt_ctf_field_type_get_type_id(packet_context_type) != | |
1487a16a | 756 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
835b2d10 | 757 | /* A packet context must be a structure. */ |
d2f71f12 PP |
758 | BT_LOGW("Invalid parameter: stream class's packet context field type must be a structure: " |
759 | "addr=%p, name=\"%s\", id=%" PRId64 ", " | |
760 | "packet-context-ft-addr=%p, packet-context-ft-id=%s", | |
761 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
762 | bt_ctf_stream_class_get_id(stream_class), | |
763 | packet_context_type, | |
764 | bt_ctf_field_type_id_string( | |
765 | bt_ctf_field_type_get_type_id(packet_context_type))); | |
12c8a1a3 JG |
766 | ret = -1; |
767 | goto end; | |
768 | } | |
769 | ||
83509119 JG |
770 | bt_put(stream_class->packet_context_type); |
771 | bt_get(packet_context_type); | |
12c8a1a3 | 772 | stream_class->packet_context_type = packet_context_type; |
d2f71f12 PP |
773 | BT_LOGV("Set stream class's packet context field type: " |
774 | "addr=%p, name=\"%s\", id=%" PRId64 ", " | |
775 | "packet-context-ft-addr=%p", | |
776 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
777 | bt_ctf_stream_class_get_id(stream_class), | |
778 | packet_context_type); | |
779 | ||
12c8a1a3 JG |
780 | end: |
781 | return ret; | |
782 | } | |
783 | ||
662e778c JG |
784 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_header_type( |
785 | struct bt_ctf_stream_class *stream_class) | |
786 | { | |
787 | struct bt_ctf_field_type *ret = NULL; | |
788 | ||
d2f71f12 PP |
789 | if (!stream_class) { |
790 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
791 | goto end; | |
792 | } | |
793 | ||
794 | if (!stream_class->event_header_type) { | |
795 | BT_LOGV("Stream class has no event header field type: " | |
796 | "addr=%p, name=\"%s\", id=%" PRId64, | |
797 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
798 | bt_ctf_stream_class_get_id(stream_class)); | |
662e778c JG |
799 | goto end; |
800 | } | |
801 | ||
83509119 | 802 | bt_get(stream_class->event_header_type); |
662e778c JG |
803 | ret = stream_class->event_header_type; |
804 | end: | |
805 | return ret; | |
806 | } | |
807 | ||
808 | int bt_ctf_stream_class_set_event_header_type( | |
809 | struct bt_ctf_stream_class *stream_class, | |
810 | struct bt_ctf_field_type *event_header_type) | |
811 | { | |
812 | int ret = 0; | |
813 | ||
d2f71f12 PP |
814 | if (!stream_class) { |
815 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
816 | ret = -1; | |
817 | goto end; | |
818 | } | |
819 | ||
820 | if (stream_class->frozen) { | |
821 | BT_LOGW("Invalid parameter: stream class is frozen: " | |
822 | "addr=%p, name=\"%s\", id=%" PRId64, | |
823 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
824 | bt_ctf_stream_class_get_id(stream_class)); | |
662e778c JG |
825 | ret = -1; |
826 | goto end; | |
827 | } | |
828 | ||
835b2d10 JG |
829 | if (event_header_type && |
830 | bt_ctf_field_type_get_type_id(event_header_type) != | |
1487a16a | 831 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
835b2d10 | 832 | /* An event header must be a structure. */ |
d2f71f12 PP |
833 | BT_LOGW("Invalid parameter: stream class's event header field type must be a structure: " |
834 | "addr=%p, name=\"%s\", id=%" PRId64 ", " | |
835 | "event-header-ft-addr=%p, event-header-ft-id=%s", | |
836 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
837 | bt_ctf_stream_class_get_id(stream_class), | |
838 | event_header_type, | |
839 | bt_ctf_field_type_id_string( | |
840 | bt_ctf_field_type_get_type_id(event_header_type))); | |
662e778c JG |
841 | ret = -1; |
842 | goto end; | |
843 | } | |
844 | ||
83509119 | 845 | bt_put(stream_class->event_header_type); |
835b2d10 | 846 | stream_class->event_header_type = bt_get(event_header_type); |
d2f71f12 PP |
847 | BT_LOGV("Set stream class's event header field type: " |
848 | "addr=%p, name=\"%s\", id=%" PRId64 ", " | |
849 | "event-header-ft-addr=%p", | |
850 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
851 | bt_ctf_stream_class_get_id(stream_class), | |
852 | event_header_type); | |
662e778c JG |
853 | end: |
854 | return ret; | |
855 | } | |
856 | ||
af181248 JG |
857 | struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type( |
858 | struct bt_ctf_stream_class *stream_class) | |
859 | { | |
860 | struct bt_ctf_field_type *ret = NULL; | |
861 | ||
d2f71f12 PP |
862 | if (!stream_class) { |
863 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
864 | goto end; | |
865 | } | |
866 | ||
867 | if (!stream_class->event_context_type) { | |
af181248 JG |
868 | goto end; |
869 | } | |
870 | ||
83509119 | 871 | bt_get(stream_class->event_context_type); |
af181248 JG |
872 | ret = stream_class->event_context_type; |
873 | end: | |
874 | return ret; | |
875 | } | |
876 | ||
877 | int bt_ctf_stream_class_set_event_context_type( | |
878 | struct bt_ctf_stream_class *stream_class, | |
879 | struct bt_ctf_field_type *event_context_type) | |
880 | { | |
881 | int ret = 0; | |
882 | ||
d2f71f12 PP |
883 | if (!stream_class) { |
884 | BT_LOGW_STR("Invalid parameter: stream class is NULL."); | |
885 | ret = -1; | |
886 | goto end; | |
887 | } | |
888 | ||
889 | if (stream_class->frozen) { | |
890 | BT_LOGW("Invalid parameter: stream class is frozen: " | |
891 | "addr=%p, name=\"%s\", id=%" PRId64, | |
892 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
893 | bt_ctf_stream_class_get_id(stream_class)); | |
af181248 JG |
894 | ret = -1; |
895 | goto end; | |
896 | } | |
897 | ||
835b2d10 JG |
898 | if (event_context_type && |
899 | bt_ctf_field_type_get_type_id(event_context_type) != | |
1487a16a | 900 | BT_CTF_FIELD_TYPE_ID_STRUCT) { |
835b2d10 | 901 | /* A packet context must be a structure. */ |
d2f71f12 PP |
902 | BT_LOGW("Invalid parameter: stream class's event context field type must be a structure: " |
903 | "addr=%p, name=\"%s\", id=%" PRId64 ", " | |
904 | "event-context-ft-addr=%p, event-context-ft-id=%s", | |
905 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
906 | bt_ctf_stream_class_get_id(stream_class), | |
907 | event_context_type, | |
908 | bt_ctf_field_type_id_string( | |
909 | bt_ctf_field_type_get_type_id(event_context_type))); | |
af181248 JG |
910 | ret = -1; |
911 | goto end; | |
912 | } | |
913 | ||
83509119 | 914 | bt_put(stream_class->event_context_type); |
835b2d10 | 915 | stream_class->event_context_type = bt_get(event_context_type); |
d2f71f12 PP |
916 | BT_LOGV("Set stream class's event context field type: " |
917 | "addr=%p, name=\"%s\", id=%" PRId64 ", " | |
918 | "event-context-ft-addr=%p", | |
919 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
920 | bt_ctf_stream_class_get_id(stream_class), | |
921 | event_context_type); | |
af181248 JG |
922 | end: |
923 | return ret; | |
924 | } | |
925 | ||
d2f71f12 | 926 | /* Pre-2.0 CTF writer backward compatibility */ |
11b0cdc8 JG |
927 | void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class) |
928 | { | |
83509119 | 929 | bt_get(stream_class); |
11b0cdc8 JG |
930 | } |
931 | ||
d2f71f12 | 932 | /* Pre-2.0 CTF writer backward compatibility */ |
11b0cdc8 JG |
933 | void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class) |
934 | { | |
83509119 | 935 | bt_put(stream_class); |
11b0cdc8 JG |
936 | } |
937 | ||
8bf65fbd | 938 | static |
544d0515 | 939 | int64_t get_event_class_count(void *element) |
8bf65fbd JG |
940 | { |
941 | return bt_ctf_stream_class_get_event_class_count( | |
942 | (struct bt_ctf_stream_class *) element); | |
943 | } | |
944 | ||
945 | static | |
946 | void *get_event_class(void *element, int i) | |
947 | { | |
9ac68eb1 | 948 | return bt_ctf_stream_class_get_event_class_by_index( |
8bf65fbd JG |
949 | (struct bt_ctf_stream_class *) element, i); |
950 | } | |
951 | ||
952 | static | |
d9a13d86 | 953 | int visit_event_class(void *object, bt_ctf_visitor visitor,void *data) |
8bf65fbd | 954 | { |
d9a13d86 PP |
955 | struct bt_ctf_object obj = |
956 | { .object = object, | |
957 | .type = BT_CTF_OBJECT_TYPE_EVENT_CLASS }; | |
8bf65fbd | 958 | |
d9a13d86 | 959 | return visitor(&obj, data); |
8bf65fbd JG |
960 | } |
961 | ||
962 | int bt_ctf_stream_class_visit(struct bt_ctf_stream_class *stream_class, | |
d9a13d86 | 963 | bt_ctf_visitor visitor, void *data) |
8bf65fbd JG |
964 | { |
965 | int ret; | |
d9a13d86 PP |
966 | struct bt_ctf_object obj = |
967 | { .object = stream_class, | |
968 | .type = BT_CTF_OBJECT_TYPE_STREAM_CLASS }; | |
8bf65fbd JG |
969 | |
970 | if (!stream_class || !visitor) { | |
d2f71f12 PP |
971 | BT_LOGW("Invalid parameter: stream class or visitor is NULL: " |
972 | "stream-class-addr=%p, visitor=%p", | |
973 | stream_class, visitor); | |
8bf65fbd JG |
974 | ret = -1; |
975 | goto end; | |
976 | } | |
977 | ||
d9a13d86 | 978 | ret = visitor_helper(&obj, get_event_class_count, |
8bf65fbd JG |
979 | get_event_class, |
980 | visit_event_class, visitor, data); | |
d2f71f12 | 981 | BT_LOGV("visitor_helper() returned: ret=%d", ret); |
8bf65fbd JG |
982 | end: |
983 | return ret; | |
984 | } | |
985 | ||
11b0cdc8 JG |
986 | BT_HIDDEN |
987 | void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class) | |
988 | { | |
d2f71f12 | 989 | if (!stream_class || stream_class->frozen) { |
11b0cdc8 JG |
990 | return; |
991 | } | |
992 | ||
d2f71f12 PP |
993 | BT_LOGD("Freezing stream class: addr=%p, name=\"%s\", id=%" PRId64, |
994 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
995 | bt_ctf_stream_class_get_id(stream_class)); | |
11b0cdc8 | 996 | stream_class->frozen = 1; |
662e778c | 997 | bt_ctf_field_type_freeze(stream_class->event_header_type); |
12c8a1a3 | 998 | bt_ctf_field_type_freeze(stream_class->packet_context_type); |
af181248 | 999 | bt_ctf_field_type_freeze(stream_class->event_context_type); |
ac0c6bdd PP |
1000 | |
1001 | if (stream_class->clock) { | |
1002 | bt_ctf_clock_class_freeze(stream_class->clock->clock_class); | |
1003 | } | |
11b0cdc8 JG |
1004 | } |
1005 | ||
11b0cdc8 JG |
1006 | BT_HIDDEN |
1007 | int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class, | |
1008 | struct metadata_context *context) | |
1009 | { | |
9ac68eb1 | 1010 | int ret = 0; |
11b0cdc8 JG |
1011 | size_t i; |
1012 | ||
d2f71f12 PP |
1013 | BT_LOGD("Serializing stream class's metadata: " |
1014 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
1015 | "stream-class-id=%" PRId64 ", metadata-context-addr=%p", | |
1016 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
1017 | bt_ctf_stream_class_get_id(stream_class), context); | |
11b0cdc8 JG |
1018 | g_string_assign(context->field_name, ""); |
1019 | context->current_indentation_level = 1; | |
1020 | if (!stream_class->id_set) { | |
d2f71f12 | 1021 | BT_LOGW_STR("Stream class's ID is not set."); |
11b0cdc8 JG |
1022 | ret = -1; |
1023 | goto end; | |
1024 | } | |
1025 | ||
1026 | g_string_append_printf(context->string, | |
e011d2c1 PP |
1027 | "stream {\n\tid = %" PRId64 ";\n", stream_class->id); |
1028 | if (stream_class->event_header_type) { | |
1029 | BT_LOGD_STR("Serializing stream class's event header field type's metadata."); | |
1030 | g_string_append(context->string, "\tevent.header := "); | |
1031 | ret = bt_ctf_field_type_serialize(stream_class->event_header_type, | |
1032 | context); | |
1033 | if (ret) { | |
1034 | BT_LOGW("Cannot serialize stream class's event header field type's metadata: " | |
1035 | "ret=%d", ret); | |
1036 | goto end; | |
1037 | } | |
1038 | g_string_append(context->string, ";"); | |
11b0cdc8 JG |
1039 | } |
1040 | ||
e011d2c1 | 1041 | |
98edd02c | 1042 | if (stream_class->packet_context_type) { |
e011d2c1 PP |
1043 | BT_LOGD_STR("Serializing stream class's packet context field type's metadata."); |
1044 | g_string_append(context->string, "\n\n\tpacket.context := "); | |
98edd02c JG |
1045 | ret = bt_ctf_field_type_serialize(stream_class->packet_context_type, |
1046 | context); | |
1047 | if (ret) { | |
66871d36 | 1048 | BT_LOGW("Cannot serialize stream class's packet context field type's metadata: " |
d2f71f12 | 1049 | "ret=%d", ret); |
98edd02c JG |
1050 | goto end; |
1051 | } | |
e011d2c1 | 1052 | g_string_append(context->string, ";"); |
11b0cdc8 JG |
1053 | } |
1054 | ||
1055 | if (stream_class->event_context_type) { | |
e011d2c1 PP |
1056 | BT_LOGD_STR("Serializing stream class's event context field type's metadata."); |
1057 | g_string_append(context->string, "\n\n\tevent.context := "); | |
11b0cdc8 JG |
1058 | ret = bt_ctf_field_type_serialize( |
1059 | stream_class->event_context_type, context); | |
1060 | if (ret) { | |
66871d36 | 1061 | BT_LOGW("Cannot serialize stream class's event context field type's metadata: " |
d2f71f12 | 1062 | "ret=%d", ret); |
11b0cdc8 JG |
1063 | goto end; |
1064 | } | |
e011d2c1 | 1065 | g_string_append(context->string, ";"); |
11b0cdc8 JG |
1066 | } |
1067 | ||
e011d2c1 PP |
1068 | g_string_append(context->string, "\n};\n\n"); |
1069 | ||
11b0cdc8 JG |
1070 | for (i = 0; i < stream_class->event_classes->len; i++) { |
1071 | struct bt_ctf_event_class *event_class = | |
1072 | stream_class->event_classes->pdata[i]; | |
1073 | ||
11b0cdc8 JG |
1074 | ret = bt_ctf_event_class_serialize(event_class, context); |
1075 | if (ret) { | |
66871d36 | 1076 | BT_LOGW("Cannot serialize event class's metadata: " |
d2f71f12 PP |
1077 | "event-class-addr=%p, event-class-name=\"%s\", " |
1078 | "event-class-id=%" PRId64, | |
1079 | event_class, | |
1080 | bt_ctf_event_class_get_name(event_class), | |
1081 | bt_ctf_event_class_get_id(event_class)); | |
11b0cdc8 JG |
1082 | goto end; |
1083 | } | |
1084 | } | |
1085 | end: | |
1086 | context->current_indentation_level = 0; | |
1087 | return ret; | |
1088 | } | |
1089 | ||
1090 | static | |
83509119 | 1091 | void bt_ctf_stream_class_destroy(struct bt_object *obj) |
11b0cdc8 JG |
1092 | { |
1093 | struct bt_ctf_stream_class *stream_class; | |
1094 | ||
83509119 | 1095 | stream_class = container_of(obj, struct bt_ctf_stream_class, base); |
d2f71f12 PP |
1096 | BT_LOGD("Destroying stream class: addr=%p, name=\"%s\", id=%" PRId64, |
1097 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
1098 | bt_ctf_stream_class_get_id(stream_class)); | |
83509119 | 1099 | bt_put(stream_class->clock); |
11b0cdc8 | 1100 | |
0b9ce69f JG |
1101 | if (stream_class->event_classes_ht) { |
1102 | g_hash_table_destroy(stream_class->event_classes_ht); | |
1103 | } | |
11b0cdc8 | 1104 | if (stream_class->event_classes) { |
8c8cb0f2 | 1105 | BT_LOGD_STR("Destroying event classes."); |
11b0cdc8 JG |
1106 | g_ptr_array_free(stream_class->event_classes, TRUE); |
1107 | } | |
1108 | ||
1109 | if (stream_class->name) { | |
1110 | g_string_free(stream_class->name, TRUE); | |
1111 | } | |
1112 | ||
8c8cb0f2 | 1113 | BT_LOGD_STR("Putting event header field type."); |
83509119 | 1114 | bt_put(stream_class->event_header_type); |
8c8cb0f2 | 1115 | BT_LOGD_STR("Putting packet context field type."); |
83509119 | 1116 | bt_put(stream_class->packet_context_type); |
8c8cb0f2 | 1117 | BT_LOGD_STR("Putting event context field type."); |
83509119 | 1118 | bt_put(stream_class->event_context_type); |
11b0cdc8 JG |
1119 | g_free(stream_class); |
1120 | } | |
1121 | ||
1122 | static | |
662e778c | 1123 | int init_event_header(struct bt_ctf_stream_class *stream_class) |
11b0cdc8 JG |
1124 | { |
1125 | int ret = 0; | |
1126 | struct bt_ctf_field_type *event_header_type = | |
1127 | bt_ctf_field_type_structure_create(); | |
1128 | struct bt_ctf_field_type *_uint32_t = | |
1129 | get_field_type(FIELD_TYPE_ALIAS_UINT32_T); | |
1130 | struct bt_ctf_field_type *_uint64_t = | |
1131 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); | |
1132 | ||
1133 | if (!event_header_type) { | |
d2f71f12 | 1134 | BT_LOGE_STR("Cannot create empty structure field type."); |
11b0cdc8 JG |
1135 | ret = -1; |
1136 | goto end; | |
1137 | } | |
1138 | ||
11b0cdc8 JG |
1139 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
1140 | _uint32_t, "id"); | |
1141 | if (ret) { | |
d2f71f12 | 1142 | BT_LOGE_STR("Cannot add `id` field to event header field type."); |
11b0cdc8 JG |
1143 | goto end; |
1144 | } | |
1145 | ||
1146 | ret = bt_ctf_field_type_structure_add_field(event_header_type, | |
1147 | _uint64_t, "timestamp"); | |
1148 | if (ret) { | |
d2f71f12 | 1149 | BT_LOGE_STR("Cannot add `timestamp` field to event header field type."); |
11b0cdc8 JG |
1150 | goto end; |
1151 | } | |
1152 | ||
e0e2946b | 1153 | BT_MOVE(stream_class->event_header_type, event_header_type); |
11b0cdc8 JG |
1154 | end: |
1155 | if (ret) { | |
83509119 | 1156 | bt_put(event_header_type); |
11b0cdc8 JG |
1157 | } |
1158 | ||
83509119 JG |
1159 | bt_put(_uint32_t); |
1160 | bt_put(_uint64_t); | |
11b0cdc8 JG |
1161 | return ret; |
1162 | } | |
1163 | ||
1164 | static | |
662e778c | 1165 | int init_packet_context(struct bt_ctf_stream_class *stream_class) |
11b0cdc8 JG |
1166 | { |
1167 | int ret = 0; | |
1168 | struct bt_ctf_field_type *packet_context_type = | |
1169 | bt_ctf_field_type_structure_create(); | |
1170 | struct bt_ctf_field_type *_uint64_t = | |
1171 | get_field_type(FIELD_TYPE_ALIAS_UINT64_T); | |
e011d2c1 | 1172 | struct bt_ctf_field_type *ts_begin_end_uint64_t; |
11b0cdc8 JG |
1173 | |
1174 | if (!packet_context_type) { | |
d2f71f12 | 1175 | BT_LOGE_STR("Cannot create empty structure field type."); |
11b0cdc8 JG |
1176 | ret = -1; |
1177 | goto end; | |
1178 | } | |
1179 | ||
e011d2c1 PP |
1180 | ts_begin_end_uint64_t = bt_ctf_field_type_copy(_uint64_t); |
1181 | if (!ts_begin_end_uint64_t) { | |
1182 | BT_LOGE_STR("Cannot copy integer field type for `timestamp_begin` and `timestamp_end` fields."); | |
1183 | ret = -1; | |
1184 | goto end; | |
1185 | } | |
1186 | ||
11b0cdc8 JG |
1187 | /* |
1188 | * We create a stream packet context as proposed in the CTF | |
1189 | * specification. | |
1190 | */ | |
11b0cdc8 | 1191 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
e011d2c1 | 1192 | ts_begin_end_uint64_t, "timestamp_begin"); |
11b0cdc8 | 1193 | if (ret) { |
d2f71f12 | 1194 | BT_LOGE_STR("Cannot add `timestamp_begin` field to event header field type."); |
11b0cdc8 JG |
1195 | goto end; |
1196 | } | |
1197 | ||
1198 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
e011d2c1 | 1199 | ts_begin_end_uint64_t, "timestamp_end"); |
11b0cdc8 | 1200 | if (ret) { |
d2f71f12 | 1201 | BT_LOGE_STR("Cannot add `timestamp_end` field to event header field type."); |
11b0cdc8 JG |
1202 | goto end; |
1203 | } | |
1204 | ||
1205 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
1206 | _uint64_t, "content_size"); | |
1207 | if (ret) { | |
d2f71f12 | 1208 | BT_LOGE_STR("Cannot add `content_size` field to event header field type."); |
11b0cdc8 JG |
1209 | goto end; |
1210 | } | |
1211 | ||
1212 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
1213 | _uint64_t, "packet_size"); | |
1214 | if (ret) { | |
d2f71f12 | 1215 | BT_LOGE_STR("Cannot add `packet_size` field to event header field type."); |
11b0cdc8 JG |
1216 | goto end; |
1217 | } | |
1218 | ||
1219 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
1220 | _uint64_t, "events_discarded"); | |
1221 | if (ret) { | |
d2f71f12 | 1222 | BT_LOGE_STR("Cannot add `events_discarded` field to event header field type."); |
11b0cdc8 JG |
1223 | goto end; |
1224 | } | |
1225 | ||
e0e2946b | 1226 | BT_MOVE(stream_class->packet_context_type, packet_context_type); |
11b0cdc8 JG |
1227 | end: |
1228 | if (ret) { | |
83509119 | 1229 | bt_put(packet_context_type); |
11b0cdc8 JG |
1230 | goto end; |
1231 | } | |
1232 | ||
83509119 | 1233 | bt_put(_uint64_t); |
e011d2c1 PP |
1234 | bt_put(ts_begin_end_uint64_t); |
1235 | return ret; | |
1236 | } | |
1237 | ||
1238 | static | |
1239 | int try_map_clock_class(struct bt_ctf_stream_class *stream_class, | |
1240 | struct bt_ctf_field_type *ft) | |
1241 | { | |
1242 | struct bt_ctf_clock_class *mapped_clock_class = NULL; | |
1243 | int ret = 0; | |
1244 | ||
1245 | if (!ft) { | |
1246 | /* Field does not exist: not an error */ | |
1247 | goto end; | |
1248 | } | |
1249 | ||
1250 | assert(bt_ctf_field_type_is_integer(ft)); | |
1251 | mapped_clock_class = | |
1252 | bt_ctf_field_type_integer_get_mapped_clock_class(ft); | |
1253 | if (!mapped_clock_class) { | |
1254 | if (!stream_class->clock) { | |
1255 | BT_LOGW("Cannot automatically set field's type mapped clock class: stream class's clock is not set: " | |
1256 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
1257 | "stream-class-id=%" PRId64 ", ft-addr=%p", | |
1258 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
1259 | bt_ctf_stream_class_get_id(stream_class), ft); | |
1260 | ret = -1; | |
1261 | goto end; | |
1262 | } | |
1263 | ||
1264 | ret = bt_ctf_field_type_integer_set_mapped_clock_class_no_check( | |
1265 | ft, stream_class->clock->clock_class); | |
1266 | if (ret) { | |
1267 | BT_LOGW("Cannot set field type's mapped clock class: " | |
1268 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
1269 | "stream-class-id=%" PRId64 ", ft-addr=%p", | |
1270 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
1271 | bt_ctf_stream_class_get_id(stream_class), ft); | |
1272 | goto end; | |
1273 | } | |
1274 | ||
1275 | BT_LOGV("Automatically mapped field type to stream class's clock class: " | |
1276 | "stream-class-addr=%p, stream-class-name=\"%s\", " | |
1277 | "stream-class-id=%" PRId64 ", ft-addr=%p", | |
1278 | stream_class, bt_ctf_stream_class_get_name(stream_class), | |
1279 | bt_ctf_stream_class_get_id(stream_class), ft); | |
1280 | } | |
1281 | ||
1282 | end: | |
1283 | bt_put(mapped_clock_class); | |
1284 | return ret; | |
1285 | } | |
1286 | ||
1287 | BT_HIDDEN | |
1288 | int bt_ctf_stream_class_map_clock_class( | |
1289 | struct bt_ctf_stream_class *stream_class, | |
1290 | struct bt_ctf_field_type *packet_context_type, | |
1291 | struct bt_ctf_field_type *event_header_type) | |
1292 | { | |
1293 | struct bt_ctf_field_type *ft = NULL; | |
1294 | int ret = 0; | |
1295 | ||
1296 | assert(stream_class); | |
1297 | ||
1298 | if (packet_context_type) { | |
1299 | ft = bt_ctf_field_type_structure_get_field_type_by_name( | |
1300 | packet_context_type, "timestamp_begin"); | |
1301 | if (try_map_clock_class(stream_class, ft)) { | |
1302 | BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_begin` field's mapped clock class."); | |
1303 | ret = -1; | |
1304 | goto end; | |
1305 | } | |
1306 | ||
1307 | bt_put(ft); | |
1308 | ft = bt_ctf_field_type_structure_get_field_type_by_name( | |
1309 | packet_context_type, "timestamp_end"); | |
1310 | if (try_map_clock_class(stream_class, ft)) { | |
1311 | BT_LOGE_STR("Cannot automatically set stream class's packet context field type's `timestamp_end` field's mapped clock class."); | |
1312 | ret = -1; | |
1313 | goto end; | |
1314 | } | |
1315 | ||
1316 | BT_PUT(ft); | |
1317 | } | |
1318 | ||
1319 | if (event_header_type) { | |
1320 | ft = bt_ctf_field_type_structure_get_field_type_by_name( | |
1321 | event_header_type, "timestamp"); | |
1322 | if (try_map_clock_class(stream_class, ft)) { | |
1323 | BT_LOGE_STR("Cannot automatically set stream class's event header field type's `timestamp` field's mapped clock class."); | |
1324 | ret = -1; | |
1325 | goto end; | |
1326 | } | |
1327 | ||
1328 | BT_PUT(ft); | |
1329 | } | |
1330 | ||
1331 | end: | |
11b0cdc8 JG |
1332 | return ret; |
1333 | } |