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