Commit | Line | Data |
---|---|---|
273b65be JG |
1 | /* |
2 | * event.c | |
3 | * | |
d2dc44b6 | 4 | * Babeltrace CTF IR - Event |
273b65be | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be 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/event.h> | |
30 | #include <babeltrace/ctf-writer/event-types.h> | |
31 | #include <babeltrace/ctf-writer/event-fields.h> | |
adc315b8 JG |
32 | #include <babeltrace/ctf-ir/event-fields-internal.h> |
33 | #include <babeltrace/ctf-ir/event-types-internal.h> | |
34 | #include <babeltrace/ctf-ir/event-internal.h> | |
2f100782 | 35 | #include <babeltrace/ctf-ir/stream-class.h> |
c35a1669 | 36 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
bc37ae52 | 37 | #include <babeltrace/ctf-ir/trace-internal.h> |
654c1444 | 38 | #include <babeltrace/ctf-ir/utils.h> |
273b65be JG |
39 | #include <babeltrace/compiler.h> |
40 | ||
41 | static | |
42 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref); | |
43 | static | |
44 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref); | |
662e778c JG |
45 | static |
46 | int set_integer_field_value(struct bt_ctf_field *field, uint64_t value); | |
273b65be JG |
47 | |
48 | struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name) | |
49 | { | |
50 | struct bt_ctf_event_class *event_class = NULL; | |
51 | ||
654c1444 | 52 | if (bt_ctf_validate_identifier(name)) { |
273b65be JG |
53 | goto end; |
54 | } | |
55 | ||
56 | event_class = g_new0(struct bt_ctf_event_class, 1); | |
57 | if (!event_class) { | |
58 | goto end; | |
59 | } | |
60 | ||
61 | bt_ctf_ref_init(&event_class->ref_count); | |
62 | event_class->name = g_quark_from_string(name); | |
63 | end: | |
64 | return event_class; | |
65 | } | |
66 | ||
2f100782 JG |
67 | const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class) |
68 | { | |
69 | const char *name = NULL; | |
70 | ||
71 | if (!event_class) { | |
72 | goto end; | |
73 | } | |
74 | ||
75 | name = g_quark_to_string(event_class->name); | |
76 | end: | |
77 | return name; | |
78 | } | |
79 | ||
80 | int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class) | |
81 | { | |
82 | int64_t ret; | |
83 | ||
84 | if (!event_class || !event_class->id_set) { | |
85 | ret = -1; | |
86 | goto end; | |
87 | } | |
88 | ||
89 | ret = (int64_t) event_class->id; | |
90 | end: | |
91 | return ret; | |
92 | } | |
93 | ||
94 | int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, | |
95 | uint32_t id) | |
96 | { | |
97 | int ret = 0; | |
98 | ||
99 | if (!event_class) { | |
100 | ret = -1; | |
101 | goto end; | |
102 | } | |
103 | ||
104 | if (event_class->stream_class) { | |
105 | /* | |
106 | * We don't allow changing the id if the event class has already | |
107 | * been added to a stream class. | |
108 | */ | |
109 | ret = -1; | |
110 | goto end; | |
111 | } | |
112 | ||
113 | event_class->id = id; | |
114 | event_class->id_set = 1; | |
115 | end: | |
116 | return ret; | |
117 | } | |
118 | ||
119 | struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class( | |
120 | struct bt_ctf_event_class *event_class) | |
121 | { | |
122 | struct bt_ctf_stream_class *stream_class = NULL; | |
123 | ||
124 | if (!event_class) { | |
125 | goto end; | |
126 | } | |
127 | ||
128 | stream_class = event_class->stream_class; | |
129 | bt_ctf_stream_class_get(stream_class); | |
130 | end: | |
131 | return stream_class; | |
132 | } | |
133 | ||
273b65be JG |
134 | int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, |
135 | struct bt_ctf_field_type *type, | |
136 | const char *name) | |
137 | { | |
138 | int ret = 0; | |
139 | ||
654c1444 | 140 | if (!event_class || !type || bt_ctf_validate_identifier(name) || |
273b65be JG |
141 | event_class->frozen) { |
142 | ret = -1; | |
143 | goto end; | |
144 | } | |
145 | ||
146 | if (!event_class->fields) { | |
147 | event_class->fields = bt_ctf_field_type_structure_create(); | |
148 | if (!event_class->fields) { | |
149 | ret = -1; | |
150 | goto end; | |
151 | } | |
152 | } | |
153 | ||
154 | ret = bt_ctf_field_type_structure_add_field(event_class->fields, | |
155 | type, name); | |
156 | end: | |
157 | return ret; | |
158 | } | |
159 | ||
074ee56d | 160 | int bt_ctf_event_class_get_field_count( |
2f100782 JG |
161 | struct bt_ctf_event_class *event_class) |
162 | { | |
074ee56d | 163 | int ret; |
2f100782 JG |
164 | |
165 | if (!event_class) { | |
166 | ret = -1; | |
167 | goto end; | |
168 | } | |
169 | ||
170 | ret = bt_ctf_field_type_structure_get_field_count(event_class->fields); | |
171 | end: | |
172 | return ret; | |
173 | } | |
174 | ||
175 | int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class, | |
176 | const char **field_name, struct bt_ctf_field_type **field_type, | |
074ee56d | 177 | int index) |
2f100782 JG |
178 | { |
179 | int ret; | |
180 | ||
074ee56d | 181 | if (!event_class || index < 0) { |
2f100782 JG |
182 | ret = -1; |
183 | goto end; | |
184 | } | |
185 | ||
186 | ret = bt_ctf_field_type_structure_get_field(event_class->fields, | |
187 | field_name, field_type, index); | |
188 | end: | |
189 | return ret; | |
190 | } | |
191 | ||
192 | struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name( | |
193 | struct bt_ctf_event_class *event_class, const char *name) | |
194 | { | |
195 | GQuark name_quark; | |
196 | struct bt_ctf_field_type *field_type = NULL; | |
197 | ||
198 | if (!event_class || !name) { | |
199 | goto end; | |
200 | } | |
201 | ||
202 | name_quark = g_quark_try_string(name); | |
203 | if (!name_quark) { | |
204 | goto end; | |
205 | } | |
206 | ||
207 | /* | |
208 | * No need to increment field_type's reference count since getting it | |
209 | * from the structure already does. | |
210 | */ | |
211 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
212 | event_class->fields, name); | |
213 | end: | |
214 | return field_type; | |
215 | } | |
216 | ||
f655a84d JG |
217 | struct bt_ctf_field_type *bt_ctf_event_class_get_context_type( |
218 | struct bt_ctf_event_class *event_class) | |
219 | { | |
220 | struct bt_ctf_field_type *context_type = NULL; | |
221 | ||
222 | if (!event_class || !event_class->context) { | |
223 | goto end; | |
224 | } | |
225 | ||
226 | bt_ctf_field_type_get(event_class->context); | |
227 | context_type = event_class->context; | |
228 | end: | |
229 | return context_type; | |
230 | } | |
231 | ||
232 | int bt_ctf_event_class_set_context_type( | |
233 | struct bt_ctf_event_class *event_class, | |
234 | struct bt_ctf_field_type *context) | |
235 | { | |
236 | int ret = 0; | |
237 | ||
238 | if (!event_class || !context || event_class->frozen) { | |
239 | ret = -1; | |
240 | goto end; | |
241 | } | |
242 | ||
243 | if (bt_ctf_field_type_get_type_id(context) != CTF_TYPE_STRUCT) { | |
244 | ret = -1; | |
245 | goto end; | |
246 | } | |
247 | ||
248 | bt_ctf_field_type_get(context); | |
249 | bt_ctf_field_type_put(event_class->context); | |
250 | event_class->context = context; | |
251 | end: | |
252 | return ret; | |
253 | ||
254 | } | |
255 | ||
273b65be JG |
256 | void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class) |
257 | { | |
258 | if (!event_class) { | |
259 | return; | |
260 | } | |
261 | ||
262 | bt_ctf_ref_get(&event_class->ref_count); | |
263 | } | |
264 | ||
265 | void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class) | |
266 | { | |
267 | if (!event_class) { | |
268 | return; | |
269 | } | |
270 | ||
271 | bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy); | |
272 | } | |
273 | ||
274 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) | |
275 | { | |
276 | struct bt_ctf_event *event = NULL; | |
277 | ||
278 | if (!event_class) { | |
279 | goto end; | |
280 | } | |
281 | ||
282 | event = g_new0(struct bt_ctf_event, 1); | |
283 | if (!event) { | |
284 | goto end; | |
285 | } | |
286 | ||
287 | bt_ctf_ref_init(&event->ref_count); | |
288 | bt_ctf_event_class_get(event_class); | |
289 | bt_ctf_event_class_freeze(event_class); | |
290 | event->event_class = event_class; | |
662e778c JG |
291 | |
292 | /* | |
293 | * The event class does not keep ownership of the stream class to | |
294 | * which it as been added. Therefore, it can't assume it has been | |
295 | * set. However, we disallow the creation of an event if its | |
296 | * associated stream class has been reclaimed. | |
297 | */ | |
298 | if (!event_class->stream_class) { | |
299 | goto error_destroy; | |
300 | } | |
301 | assert(event_class->stream_class->event_header_type); | |
302 | ||
303 | event->event_header = bt_ctf_field_create( | |
304 | event_class->stream_class->event_header_type); | |
305 | if (!event->event_header) { | |
306 | goto error_destroy; | |
307 | } | |
f655a84d JG |
308 | if (event_class->context) { |
309 | event->context_payload = bt_ctf_field_create( | |
310 | event_class->context); | |
662e778c JG |
311 | if (!event->context_payload) { |
312 | goto error_destroy; | |
313 | } | |
f655a84d | 314 | } |
273b65be | 315 | event->fields_payload = bt_ctf_field_create(event_class->fields); |
662e778c JG |
316 | if (!event->fields_payload) { |
317 | goto error_destroy; | |
318 | } | |
319 | ||
320 | /* | |
321 | * Freeze the stream class since the event header must not be changed | |
322 | * anymore. | |
323 | */ | |
324 | bt_ctf_stream_class_freeze(event_class->stream_class); | |
273b65be JG |
325 | end: |
326 | return event; | |
662e778c JG |
327 | error_destroy: |
328 | bt_ctf_event_destroy(&event->ref_count); | |
329 | return NULL; | |
273b65be JG |
330 | } |
331 | ||
2f100782 JG |
332 | struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event) |
333 | { | |
334 | struct bt_ctf_event_class *event_class = NULL; | |
335 | ||
336 | if (!event) { | |
337 | goto end; | |
338 | } | |
339 | ||
340 | event_class = event->event_class; | |
341 | bt_ctf_event_class_get(event_class); | |
342 | end: | |
343 | return event_class; | |
344 | } | |
345 | ||
346 | struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event) | |
347 | { | |
348 | struct bt_ctf_clock *clock = NULL; | |
349 | struct bt_ctf_event_class *event_class; | |
350 | struct bt_ctf_stream_class *stream_class; | |
351 | ||
352 | if (!event) { | |
353 | goto end; | |
354 | } | |
355 | ||
356 | event_class = bt_ctf_event_get_class(event); | |
357 | if (!event_class) { | |
358 | goto end; | |
359 | } | |
360 | ||
361 | stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
362 | if (!stream_class) { | |
363 | goto error_put_event_class; | |
364 | } | |
365 | ||
366 | clock = bt_ctf_stream_class_get_clock(stream_class); | |
367 | if (!clock) { | |
368 | goto error_put_stream_class; | |
369 | } | |
370 | ||
371 | error_put_stream_class: | |
372 | bt_ctf_stream_class_put(stream_class); | |
373 | error_put_event_class: | |
374 | bt_ctf_event_class_put(event_class); | |
375 | end: | |
376 | return clock; | |
377 | } | |
378 | ||
273b65be JG |
379 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, |
380 | const char *name, | |
381 | struct bt_ctf_field *value) | |
382 | { | |
383 | int ret = 0; | |
384 | ||
654c1444 | 385 | if (!event || !value || bt_ctf_validate_identifier(name)) { |
273b65be JG |
386 | ret = -1; |
387 | goto end; | |
388 | } | |
389 | ||
390 | ret = bt_ctf_field_structure_set_field(event->fields_payload, | |
391 | name, value); | |
392 | end: | |
393 | return ret; | |
394 | } | |
395 | ||
396 | ||
397 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, | |
398 | const char *name) | |
399 | { | |
400 | struct bt_ctf_field *field = NULL; | |
401 | ||
402 | if (!event || !name) { | |
403 | goto end; | |
404 | } | |
405 | ||
406 | field = bt_ctf_field_structure_get_field(event->fields_payload, name); | |
407 | end: | |
408 | return field; | |
409 | } | |
410 | ||
2f100782 | 411 | struct bt_ctf_field *bt_ctf_event_get_payload_by_index( |
074ee56d | 412 | struct bt_ctf_event *event, int index) |
2f100782 JG |
413 | { |
414 | struct bt_ctf_field *field = NULL; | |
415 | ||
074ee56d | 416 | if (!event || index < 0) { |
2f100782 JG |
417 | goto end; |
418 | } | |
419 | ||
420 | field = bt_ctf_field_structure_get_field_by_index(event->fields_payload, | |
421 | index); | |
422 | end: | |
423 | return field; | |
424 | } | |
425 | ||
662e778c JG |
426 | struct bt_ctf_field *bt_ctf_event_get_event_header( |
427 | struct bt_ctf_event *event) | |
428 | { | |
429 | struct bt_ctf_field *header = NULL; | |
430 | ||
431 | if (!event || !event->event_header) { | |
432 | goto end; | |
433 | } | |
434 | ||
435 | header = event->event_header; | |
436 | bt_ctf_field_get(header); | |
437 | end: | |
438 | return header; | |
439 | } | |
440 | ||
441 | int bt_ctf_event_set_event_header(struct bt_ctf_event *event, | |
442 | struct bt_ctf_field *header) | |
443 | { | |
444 | int ret = 0; | |
445 | struct bt_ctf_field_type *field_type = NULL; | |
446 | ||
447 | if (!event || !header) { | |
448 | ret = -1; | |
449 | goto end; | |
450 | } | |
451 | ||
452 | /* Could be NULL since an event class doesn't own a stream class */ | |
453 | if (!event->event_class->stream_class) { | |
454 | ret = -1; | |
455 | goto end; | |
456 | } | |
457 | ||
458 | /* | |
459 | * Ensure the provided header's type matches the one registered to the | |
460 | * stream class. | |
461 | */ | |
462 | field_type = bt_ctf_field_get_type(header); | |
463 | if (field_type != event->event_class->stream_class->event_header_type) { | |
464 | ret = -1; | |
465 | goto end; | |
466 | } | |
467 | ||
468 | bt_ctf_field_get(header); | |
469 | bt_ctf_field_put(event->event_header); | |
470 | event->event_header = header; | |
471 | end: | |
472 | if (field_type) { | |
473 | bt_ctf_field_type_put(field_type); | |
474 | } | |
475 | return ret; | |
476 | } | |
477 | ||
f655a84d JG |
478 | struct bt_ctf_field *bt_ctf_event_get_event_context( |
479 | struct bt_ctf_event *event) | |
480 | { | |
481 | struct bt_ctf_field *context = NULL; | |
482 | ||
483 | if (!event || !event->context_payload) { | |
484 | goto end; | |
485 | } | |
486 | ||
487 | context = event->context_payload; | |
488 | bt_ctf_field_get(context); | |
489 | end: | |
490 | return context; | |
491 | } | |
492 | ||
493 | int bt_ctf_event_set_event_context(struct bt_ctf_event *event, | |
494 | struct bt_ctf_field *context) | |
495 | { | |
496 | int ret = 0; | |
497 | struct bt_ctf_field_type *field_type = NULL; | |
498 | ||
499 | if (!event || !context) { | |
500 | ret = -1; | |
501 | goto end; | |
502 | } | |
503 | ||
504 | field_type = bt_ctf_field_get_type(context); | |
505 | if (field_type != event->event_class->context) { | |
506 | ret = -1; | |
507 | goto end; | |
508 | } | |
509 | ||
510 | bt_ctf_field_get(context); | |
511 | bt_ctf_field_put(event->context_payload); | |
512 | event->context_payload = context; | |
513 | end: | |
514 | if (field_type) { | |
515 | bt_ctf_field_type_put(field_type); | |
516 | } | |
517 | return ret; | |
518 | } | |
519 | ||
273b65be JG |
520 | void bt_ctf_event_get(struct bt_ctf_event *event) |
521 | { | |
522 | if (!event) { | |
523 | return; | |
524 | } | |
525 | ||
526 | bt_ctf_ref_get(&event->ref_count); | |
527 | } | |
528 | ||
529 | void bt_ctf_event_put(struct bt_ctf_event *event) | |
530 | { | |
531 | if (!event) { | |
532 | return; | |
533 | } | |
534 | ||
535 | bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy); | |
536 | } | |
537 | ||
538 | static | |
539 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref) | |
540 | { | |
541 | struct bt_ctf_event_class *event_class; | |
542 | ||
543 | if (!ref) { | |
544 | return; | |
545 | } | |
546 | ||
2f100782 JG |
547 | /* |
548 | * Don't call put() on the stream class. See comment in | |
549 | * bt_ctf_event_class_set_stream_class for explanation. | |
550 | */ | |
273b65be | 551 | event_class = container_of(ref, struct bt_ctf_event_class, ref_count); |
f655a84d JG |
552 | if (event_class->context) { |
553 | bt_ctf_field_type_put(event_class->context); | |
554 | } | |
555 | if (event_class->fields) { | |
556 | bt_ctf_field_type_put(event_class->fields); | |
557 | } | |
273b65be JG |
558 | g_free(event_class); |
559 | } | |
560 | ||
561 | static | |
562 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref) | |
563 | { | |
564 | struct bt_ctf_event *event; | |
565 | ||
566 | if (!ref) { | |
567 | return; | |
568 | } | |
569 | ||
570 | event = container_of(ref, struct bt_ctf_event, | |
571 | ref_count); | |
f655a84d JG |
572 | if (event->event_class) { |
573 | bt_ctf_event_class_put(event->event_class); | |
574 | } | |
662e778c JG |
575 | if (event->event_header) { |
576 | bt_ctf_field_put(event->event_header); | |
577 | } | |
f655a84d JG |
578 | if (event->context_payload) { |
579 | bt_ctf_field_put(event->context_payload); | |
580 | } | |
581 | if (event->fields_payload) { | |
582 | bt_ctf_field_put(event->fields_payload); | |
583 | } | |
273b65be JG |
584 | g_free(event); |
585 | } | |
586 | ||
662e778c JG |
587 | static |
588 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) | |
589 | { | |
590 | int ret = 0; | |
591 | struct bt_ctf_field_type *field_type = NULL; | |
592 | ||
593 | if (!field) { | |
594 | ret = -1; | |
595 | goto end; | |
596 | } | |
597 | ||
598 | if (!bt_ctf_field_validate(field)) { | |
599 | /* Payload already set, skip! (not an error) */ | |
600 | goto end; | |
601 | } | |
602 | ||
603 | field_type = bt_ctf_field_get_type(field); | |
604 | assert(field_type); | |
605 | ||
606 | if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) { | |
607 | /* Not an integer and the value is unset, error. */ | |
608 | ret = -1; | |
609 | goto end; | |
610 | } | |
611 | ||
612 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
613 | ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value); | |
614 | if (ret) { | |
615 | /* Value is out of range, error. */ | |
616 | goto end; | |
617 | } | |
618 | } else { | |
619 | ret = bt_ctf_field_unsigned_integer_set_value(field, value); | |
620 | if (ret) { | |
621 | /* Value is out of range, error. */ | |
622 | goto end; | |
623 | } | |
624 | } | |
625 | end: | |
626 | bt_ctf_field_type_put(field_type); | |
627 | return ret; | |
628 | } | |
629 | ||
273b65be JG |
630 | BT_HIDDEN |
631 | void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class) | |
632 | { | |
633 | assert(event_class); | |
634 | event_class->frozen = 1; | |
635 | bt_ctf_field_type_freeze(event_class->context); | |
636 | bt_ctf_field_type_freeze(event_class->fields); | |
637 | } | |
638 | ||
639 | BT_HIDDEN | |
2f100782 JG |
640 | int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class *event_class, |
641 | struct bt_ctf_stream_class *stream_class) | |
273b65be JG |
642 | { |
643 | int ret = 0; | |
644 | ||
2f100782 | 645 | if (!event_class) { |
273b65be JG |
646 | ret = -1; |
647 | goto end; | |
648 | } | |
649 | ||
2f100782 JG |
650 | /* Allow a NULL stream_class to unset the current stream_class */ |
651 | if (stream_class && event_class->stream_class) { | |
273b65be JG |
652 | ret = -1; |
653 | goto end; | |
654 | } | |
655 | ||
2f100782 JG |
656 | event_class->stream_class = stream_class; |
657 | /* | |
658 | * We don't get() the stream_class since doing so would introduce | |
659 | * a circular ownership between event classes and stream classes. | |
660 | * | |
661 | * A stream class will always unset itself from its events before | |
662 | * being destroyed. This ensures that a user won't get a pointer | |
663 | * to a stale stream class instance from an event class. | |
664 | */ | |
273b65be JG |
665 | end: |
666 | return ret; | |
667 | } | |
668 | ||
669 | BT_HIDDEN | |
670 | int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class, | |
671 | struct metadata_context *context) | |
672 | { | |
673 | int ret = 0; | |
2f100782 | 674 | int64_t stream_id; |
273b65be JG |
675 | |
676 | assert(event_class); | |
677 | assert(context); | |
2f100782 JG |
678 | stream_id = bt_ctf_stream_class_get_id(event_class->stream_class); |
679 | if (stream_id < 0) { | |
680 | ret = -1; | |
681 | goto end; | |
682 | } | |
683 | ||
273b65be JG |
684 | context->current_indentation_level = 1; |
685 | g_string_assign(context->field_name, ""); | |
b08e48bc JG |
686 | g_string_append_printf(context->string, |
687 | "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64 ";\n", | |
273b65be JG |
688 | g_quark_to_string(event_class->name), |
689 | event_class->id, | |
2f100782 | 690 | stream_id); |
273b65be JG |
691 | |
692 | if (event_class->context) { | |
693 | g_string_append(context->string, "\tcontext := "); | |
694 | ret = bt_ctf_field_type_serialize(event_class->context, | |
695 | context); | |
696 | if (ret) { | |
697 | goto end; | |
698 | } | |
699 | g_string_append(context->string, ";\n"); | |
700 | } | |
701 | ||
702 | if (event_class->fields) { | |
703 | g_string_append(context->string, "\tfields := "); | |
704 | ret = bt_ctf_field_type_serialize(event_class->fields, context); | |
705 | if (ret) { | |
706 | goto end; | |
707 | } | |
708 | g_string_append(context->string, ";\n"); | |
709 | } | |
710 | ||
711 | g_string_append(context->string, "};\n\n"); | |
712 | end: | |
713 | context->current_indentation_level = 0; | |
714 | return ret; | |
715 | } | |
716 | ||
c35a1669 JG |
717 | void bt_ctf_event_class_set_native_byte_order( |
718 | struct bt_ctf_event_class *event_class, | |
719 | int byte_order) | |
720 | { | |
721 | if (!event_class) { | |
722 | return; | |
723 | } | |
724 | ||
725 | bt_ctf_field_type_set_native_byte_order(event_class->context, | |
726 | byte_order); | |
727 | bt_ctf_field_type_set_native_byte_order(event_class->fields, | |
728 | byte_order); | |
729 | } | |
730 | ||
273b65be JG |
731 | BT_HIDDEN |
732 | int bt_ctf_event_validate(struct bt_ctf_event *event) | |
733 | { | |
734 | /* Make sure each field's payload has been set */ | |
735 | int ret; | |
736 | ||
737 | assert(event); | |
662e778c JG |
738 | ret = bt_ctf_field_validate(event->event_header); |
739 | if (ret) { | |
740 | goto end; | |
741 | } | |
742 | ||
273b65be JG |
743 | ret = bt_ctf_field_validate(event->fields_payload); |
744 | if (ret) { | |
745 | goto end; | |
746 | } | |
747 | ||
748 | if (event->event_class->context) { | |
749 | ret = bt_ctf_field_validate(event->context_payload); | |
750 | } | |
751 | end: | |
752 | return ret; | |
753 | } | |
754 | ||
755 | BT_HIDDEN | |
756 | int bt_ctf_event_serialize(struct bt_ctf_event *event, | |
757 | struct ctf_stream_pos *pos) | |
758 | { | |
759 | int ret = 0; | |
760 | ||
761 | assert(event); | |
762 | assert(pos); | |
763 | if (event->context_payload) { | |
764 | ret = bt_ctf_field_serialize(event->context_payload, pos); | |
765 | if (ret) { | |
766 | goto end; | |
767 | } | |
768 | } | |
769 | ||
770 | if (event->fields_payload) { | |
771 | ret = bt_ctf_field_serialize(event->fields_payload, pos); | |
772 | if (ret) { | |
773 | goto end; | |
774 | } | |
775 | } | |
776 | end: | |
777 | return ret; | |
778 | } | |
779 | ||
662e778c JG |
780 | BT_HIDDEN |
781 | int bt_ctf_event_populate_event_header(struct bt_ctf_event *event) | |
782 | { | |
783 | int ret = 0; | |
784 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; | |
785 | ||
786 | if (!event) { | |
787 | ret = -1; | |
788 | goto end; | |
789 | } | |
790 | ||
791 | id_field = bt_ctf_field_structure_get_field(event->event_header, "id"); | |
792 | if (id_field) { | |
793 | ret = set_integer_field_value(id_field, | |
794 | (uint64_t) event->event_class->id); | |
795 | if (ret) { | |
796 | goto end; | |
797 | } | |
798 | } | |
799 | ||
800 | timestamp_field = bt_ctf_field_structure_get_field(event->event_header, | |
801 | "timestamp"); | |
802 | if (timestamp_field) { | |
9a220c32 JG |
803 | struct bt_ctf_field_type *timestamp_field_type = |
804 | bt_ctf_field_get_type(timestamp_field); | |
805 | struct bt_ctf_clock *mapped_clock; | |
806 | ||
807 | assert(timestamp_field_type); | |
808 | mapped_clock = bt_ctf_field_type_integer_get_mapped_clock( | |
809 | timestamp_field_type); | |
810 | bt_ctf_field_type_put(timestamp_field_type); | |
811 | if (mapped_clock) { | |
812 | uint64_t timestamp = bt_ctf_clock_get_time( | |
813 | mapped_clock); | |
814 | ||
815 | bt_ctf_clock_put(mapped_clock); | |
816 | if (timestamp == (uint64_t) -1ULL) { | |
817 | goto end; | |
818 | } | |
819 | ||
820 | ret = set_integer_field_value(timestamp_field, | |
821 | timestamp); | |
822 | if (ret) { | |
823 | goto end; | |
824 | } | |
662e778c JG |
825 | } |
826 | } | |
827 | end: | |
828 | if (id_field) { | |
829 | bt_ctf_field_put(id_field); | |
830 | } | |
831 | if (timestamp_field) { | |
832 | bt_ctf_field_put(timestamp_field); | |
833 | } | |
834 | return ret; | |
835 | } |