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