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> |
44e0a4f5 | 39 | #include <babeltrace/ctf-ir/attributes-internal.h> |
273b65be JG |
40 | #include <babeltrace/compiler.h> |
41 | ||
42 | static | |
43 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref); | |
44 | static | |
45 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref); | |
662e778c JG |
46 | static |
47 | int set_integer_field_value(struct bt_ctf_field *field, uint64_t value); | |
273b65be JG |
48 | |
49 | struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name) | |
50 | { | |
b8248cc0 PP |
51 | int ret; |
52 | struct bt_object *obj = NULL; | |
273b65be JG |
53 | struct bt_ctf_event_class *event_class = NULL; |
54 | ||
654c1444 | 55 | if (bt_ctf_validate_identifier(name)) { |
b8248cc0 | 56 | goto error; |
273b65be JG |
57 | } |
58 | ||
59 | event_class = g_new0(struct bt_ctf_event_class, 1); | |
60 | if (!event_class) { | |
b8248cc0 | 61 | goto error; |
273b65be JG |
62 | } |
63 | ||
64 | bt_ctf_ref_init(&event_class->ref_count); | |
c5a9aa19 JG |
65 | event_class->fields = bt_ctf_field_type_structure_create(); |
66 | if (!event_class->fields) { | |
b8248cc0 | 67 | goto error; |
c5a9aa19 JG |
68 | } |
69 | ||
b8248cc0 PP |
70 | event_class->attributes = bt_ctf_attributes_create(); |
71 | if (!event_class->attributes) { | |
72 | goto error; | |
73 | } | |
74 | ||
75 | obj = bt_object_integer_create_init(-1); | |
76 | if (!obj) { | |
77 | goto error; | |
78 | } | |
79 | ||
80 | ret = bt_ctf_attributes_set_field_value(event_class->attributes, | |
81 | "id", obj); | |
82 | if (ret) { | |
83 | goto error; | |
84 | } | |
85 | ||
86 | BT_OBJECT_PUT(obj); | |
87 | ||
88 | obj = bt_object_string_create_init(name); | |
89 | if (!obj) { | |
90 | goto error; | |
91 | } | |
92 | ||
93 | ret = bt_ctf_attributes_set_field_value(event_class->attributes, | |
94 | "name", obj); | |
95 | if (ret) { | |
96 | goto error; | |
97 | } | |
98 | ||
99 | BT_OBJECT_PUT(obj); | |
100 | ||
273b65be | 101 | return event_class; |
b8248cc0 PP |
102 | |
103 | error: | |
104 | if (event_class) { | |
105 | bt_ctf_event_class_put(event_class); | |
106 | } | |
107 | ||
108 | BT_OBJECT_PUT(obj); | |
109 | ||
110 | return NULL; | |
273b65be JG |
111 | } |
112 | ||
2f100782 JG |
113 | const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class) |
114 | { | |
b8248cc0 | 115 | struct bt_object *obj = NULL; |
2f100782 JG |
116 | const char *name = NULL; |
117 | ||
118 | if (!event_class) { | |
119 | goto end; | |
120 | } | |
121 | ||
b8248cc0 PP |
122 | obj = bt_ctf_attributes_get_field_value(event_class->attributes, |
123 | BT_CTF_EVENT_CLASS_ATTR_NAME_INDEX); | |
124 | if (!obj) { | |
125 | goto end; | |
126 | } | |
127 | ||
128 | if (bt_object_string_get(obj, &name)) { | |
129 | name = NULL; | |
130 | } | |
131 | ||
2f100782 | 132 | end: |
b8248cc0 PP |
133 | BT_OBJECT_PUT(obj); |
134 | ||
2f100782 JG |
135 | return name; |
136 | } | |
137 | ||
138 | int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class) | |
139 | { | |
b8248cc0 | 140 | struct bt_object *obj = NULL; |
9ea90c02 | 141 | int64_t ret = 0; |
2f100782 | 142 | |
b8248cc0 PP |
143 | if (!event_class) { |
144 | ret = -1; | |
145 | goto end; | |
146 | } | |
147 | ||
148 | obj = bt_ctf_attributes_get_field_value(event_class->attributes, | |
149 | BT_CTF_EVENT_CLASS_ATTR_ID_INDEX); | |
150 | if (!obj) { | |
151 | goto end; | |
152 | } | |
153 | ||
154 | if (bt_object_integer_get(obj, &ret)) { | |
155 | ret = -1; | |
156 | } | |
157 | ||
158 | if (ret < 0) { | |
159 | /* means ID is not set */ | |
2f100782 JG |
160 | ret = -1; |
161 | goto end; | |
162 | } | |
163 | ||
2f100782 | 164 | end: |
b8248cc0 PP |
165 | BT_OBJECT_PUT(obj); |
166 | ||
2f100782 JG |
167 | return ret; |
168 | } | |
169 | ||
170 | int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, | |
171 | uint32_t id) | |
172 | { | |
173 | int ret = 0; | |
b8248cc0 | 174 | struct bt_object *obj = NULL; |
2f100782 JG |
175 | |
176 | if (!event_class) { | |
177 | ret = -1; | |
178 | goto end; | |
179 | } | |
180 | ||
181 | if (event_class->stream_class) { | |
182 | /* | |
183 | * We don't allow changing the id if the event class has already | |
184 | * been added to a stream class. | |
185 | */ | |
186 | ret = -1; | |
187 | goto end; | |
188 | } | |
189 | ||
b8248cc0 PP |
190 | obj = bt_ctf_attributes_get_field_value(event_class->attributes, |
191 | BT_CTF_EVENT_CLASS_ATTR_ID_INDEX); | |
192 | if (!obj) { | |
193 | goto end; | |
194 | } | |
195 | ||
196 | if (bt_object_integer_set(obj, id)) { | |
197 | ret = -1; | |
198 | goto end; | |
199 | } | |
200 | ||
201 | end: | |
202 | BT_OBJECT_PUT(obj); | |
203 | ||
204 | return ret; | |
205 | } | |
206 | ||
207 | int bt_ctf_event_class_set_attribute( | |
208 | struct bt_ctf_event_class *event_class, const char *name, | |
209 | struct bt_object *value) | |
210 | { | |
211 | int ret = 0; | |
212 | ||
213 | if (!event_class || !name || !value || event_class->frozen) { | |
214 | ret = -1; | |
215 | goto end; | |
216 | } | |
217 | ||
218 | if (!strcmp(name, "id") || !strcmp(name, "loglevel")) { | |
219 | if (!bt_object_is_integer(value)) { | |
220 | ret = -1; | |
221 | goto end; | |
222 | } | |
223 | } else if (!strcmp(name, "name") || !strcmp(name, "model.emf.uri")) { | |
224 | if (!bt_object_is_string(value)) { | |
225 | ret = -1; | |
226 | goto end; | |
227 | } | |
228 | } else { | |
229 | /* unknown attribute */ | |
230 | ret = -1; | |
231 | goto end; | |
232 | } | |
233 | ||
234 | /* "id" special case: >= 0 */ | |
235 | if (!strcmp(name, "id")) { | |
236 | int64_t val; | |
237 | ||
238 | ret = bt_object_integer_get(value, &val); | |
239 | ||
240 | if (ret) { | |
241 | goto end; | |
242 | } | |
243 | ||
244 | if (val < 0) { | |
245 | ret = -1; | |
246 | goto end; | |
247 | } | |
248 | } | |
249 | ||
250 | ret = bt_ctf_attributes_set_field_value(event_class->attributes, | |
251 | name, value); | |
252 | ||
2f100782 JG |
253 | end: |
254 | return ret; | |
255 | } | |
256 | ||
b8248cc0 PP |
257 | int bt_ctf_event_class_get_attribute_count( |
258 | struct bt_ctf_event_class *event_class) | |
259 | { | |
260 | int ret = 0; | |
261 | ||
262 | if (!event_class) { | |
263 | ret = -1; | |
264 | goto end; | |
265 | } | |
266 | ||
267 | ret = bt_ctf_attributes_get_count(event_class->attributes); | |
268 | ||
269 | end: | |
270 | return ret; | |
271 | } | |
272 | ||
273 | const char * | |
274 | bt_ctf_event_class_get_attribute_name( | |
275 | struct bt_ctf_event_class *event_class, int index) | |
276 | { | |
277 | const char *ret; | |
278 | ||
279 | if (!event_class) { | |
280 | ret = NULL; | |
281 | goto end; | |
282 | } | |
283 | ||
284 | ret = bt_ctf_attributes_get_field_name(event_class->attributes, index); | |
285 | ||
286 | end: | |
287 | return ret; | |
288 | } | |
289 | ||
290 | struct bt_object * | |
291 | bt_ctf_event_class_get_attribute_value(struct bt_ctf_event_class *event_class, | |
292 | int index) | |
293 | { | |
294 | struct bt_object *ret; | |
295 | ||
296 | if (!event_class) { | |
297 | ret = NULL; | |
298 | goto end; | |
299 | } | |
300 | ||
301 | ret = bt_ctf_attributes_get_field_value(event_class->attributes, index); | |
302 | ||
303 | end: | |
304 | return ret; | |
305 | } | |
306 | ||
307 | struct bt_object * | |
308 | bt_ctf_event_class_get_attribute_value_by_name( | |
309 | struct bt_ctf_event_class *event_class, const char *name) | |
310 | { | |
311 | struct bt_object *ret; | |
312 | ||
313 | if (!event_class || !name) { | |
314 | ret = NULL; | |
315 | goto end; | |
316 | } | |
317 | ||
318 | ret = bt_ctf_attributes_get_field_value_by_name(event_class->attributes, | |
319 | name); | |
320 | ||
321 | end: | |
322 | return ret; | |
323 | ||
324 | } | |
325 | ||
2f100782 JG |
326 | struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class( |
327 | struct bt_ctf_event_class *event_class) | |
328 | { | |
329 | struct bt_ctf_stream_class *stream_class = NULL; | |
330 | ||
331 | if (!event_class) { | |
332 | goto end; | |
333 | } | |
334 | ||
335 | stream_class = event_class->stream_class; | |
336 | bt_ctf_stream_class_get(stream_class); | |
337 | end: | |
338 | return stream_class; | |
339 | } | |
340 | ||
c5a9aa19 JG |
341 | struct bt_ctf_field_type *bt_ctf_event_class_get_payload_type( |
342 | struct bt_ctf_event_class *event_class) | |
343 | { | |
344 | struct bt_ctf_field_type *payload = NULL; | |
345 | ||
346 | if (!event_class) { | |
347 | goto end; | |
348 | } | |
349 | ||
350 | bt_ctf_field_type_get(event_class->fields); | |
351 | payload = event_class->fields; | |
352 | end: | |
353 | return payload; | |
354 | } | |
355 | ||
356 | int bt_ctf_event_class_set_payload_type(struct bt_ctf_event_class *event_class, | |
357 | struct bt_ctf_field_type *payload) | |
358 | { | |
359 | int ret = 0; | |
360 | ||
d2127f80 JG |
361 | if (!event_class || !payload || |
362 | bt_ctf_field_type_get_type_id(payload) != CTF_TYPE_STRUCT) { | |
c5a9aa19 JG |
363 | ret = -1; |
364 | goto end; | |
365 | } | |
366 | ||
367 | bt_ctf_field_type_get(payload); | |
368 | bt_ctf_field_type_put(event_class->fields); | |
369 | event_class->fields = payload; | |
370 | end: | |
371 | return ret; | |
372 | } | |
373 | ||
273b65be JG |
374 | int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, |
375 | struct bt_ctf_field_type *type, | |
376 | const char *name) | |
377 | { | |
378 | int ret = 0; | |
379 | ||
654c1444 | 380 | if (!event_class || !type || bt_ctf_validate_identifier(name) || |
273b65be JG |
381 | event_class->frozen) { |
382 | ret = -1; | |
383 | goto end; | |
384 | } | |
385 | ||
c5a9aa19 JG |
386 | if (bt_ctf_field_type_get_type_id(event_class->fields) != |
387 | CTF_TYPE_STRUCT) { | |
388 | ret = -1; | |
389 | goto end; | |
273b65be JG |
390 | } |
391 | ||
392 | ret = bt_ctf_field_type_structure_add_field(event_class->fields, | |
393 | type, name); | |
394 | end: | |
395 | return ret; | |
396 | } | |
397 | ||
074ee56d | 398 | int bt_ctf_event_class_get_field_count( |
2f100782 JG |
399 | struct bt_ctf_event_class *event_class) |
400 | { | |
074ee56d | 401 | int ret; |
2f100782 JG |
402 | |
403 | if (!event_class) { | |
404 | ret = -1; | |
405 | goto end; | |
406 | } | |
407 | ||
c5a9aa19 JG |
408 | if (bt_ctf_field_type_get_type_id(event_class->fields) != |
409 | CTF_TYPE_STRUCT) { | |
410 | ret = -1; | |
411 | goto end; | |
412 | } | |
413 | ||
2f100782 JG |
414 | ret = bt_ctf_field_type_structure_get_field_count(event_class->fields); |
415 | end: | |
416 | return ret; | |
417 | } | |
418 | ||
419 | int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class, | |
420 | const char **field_name, struct bt_ctf_field_type **field_type, | |
074ee56d | 421 | int index) |
2f100782 JG |
422 | { |
423 | int ret; | |
424 | ||
074ee56d | 425 | if (!event_class || index < 0) { |
2f100782 JG |
426 | ret = -1; |
427 | goto end; | |
428 | } | |
429 | ||
c5a9aa19 JG |
430 | if (bt_ctf_field_type_get_type_id(event_class->fields) != |
431 | CTF_TYPE_STRUCT) { | |
432 | ret = -1; | |
433 | goto end; | |
434 | } | |
435 | ||
2f100782 JG |
436 | ret = bt_ctf_field_type_structure_get_field(event_class->fields, |
437 | field_name, field_type, index); | |
438 | end: | |
439 | return ret; | |
440 | } | |
441 | ||
442 | struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name( | |
443 | struct bt_ctf_event_class *event_class, const char *name) | |
444 | { | |
445 | GQuark name_quark; | |
446 | struct bt_ctf_field_type *field_type = NULL; | |
447 | ||
448 | if (!event_class || !name) { | |
449 | goto end; | |
450 | } | |
451 | ||
c5a9aa19 JG |
452 | if (bt_ctf_field_type_get_type_id(event_class->fields) != |
453 | CTF_TYPE_STRUCT) { | |
454 | goto end; | |
455 | } | |
456 | ||
2f100782 JG |
457 | name_quark = g_quark_try_string(name); |
458 | if (!name_quark) { | |
459 | goto end; | |
460 | } | |
461 | ||
462 | /* | |
463 | * No need to increment field_type's reference count since getting it | |
464 | * from the structure already does. | |
465 | */ | |
466 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
467 | event_class->fields, name); | |
468 | end: | |
469 | return field_type; | |
470 | } | |
471 | ||
f655a84d JG |
472 | struct bt_ctf_field_type *bt_ctf_event_class_get_context_type( |
473 | struct bt_ctf_event_class *event_class) | |
474 | { | |
475 | struct bt_ctf_field_type *context_type = NULL; | |
476 | ||
477 | if (!event_class || !event_class->context) { | |
478 | goto end; | |
479 | } | |
480 | ||
481 | bt_ctf_field_type_get(event_class->context); | |
482 | context_type = event_class->context; | |
483 | end: | |
484 | return context_type; | |
485 | } | |
486 | ||
487 | int bt_ctf_event_class_set_context_type( | |
488 | struct bt_ctf_event_class *event_class, | |
489 | struct bt_ctf_field_type *context) | |
490 | { | |
491 | int ret = 0; | |
492 | ||
493 | if (!event_class || !context || event_class->frozen) { | |
494 | ret = -1; | |
495 | goto end; | |
496 | } | |
497 | ||
498 | if (bt_ctf_field_type_get_type_id(context) != CTF_TYPE_STRUCT) { | |
499 | ret = -1; | |
500 | goto end; | |
501 | } | |
502 | ||
503 | bt_ctf_field_type_get(context); | |
504 | bt_ctf_field_type_put(event_class->context); | |
505 | event_class->context = context; | |
506 | end: | |
507 | return ret; | |
508 | ||
509 | } | |
510 | ||
273b65be JG |
511 | void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class) |
512 | { | |
513 | if (!event_class) { | |
514 | return; | |
515 | } | |
516 | ||
517 | bt_ctf_ref_get(&event_class->ref_count); | |
518 | } | |
519 | ||
520 | void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class) | |
521 | { | |
522 | if (!event_class) { | |
523 | return; | |
524 | } | |
525 | ||
526 | bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy); | |
527 | } | |
528 | ||
529 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) | |
530 | { | |
b8248cc0 PP |
531 | int ret; |
532 | struct bt_object *obj = NULL; | |
273b65be JG |
533 | struct bt_ctf_event *event = NULL; |
534 | ||
535 | if (!event_class) { | |
536 | goto end; | |
537 | } | |
538 | ||
662e778c JG |
539 | /* |
540 | * The event class does not keep ownership of the stream class to | |
541 | * which it as been added. Therefore, it can't assume it has been | |
542 | * set. However, we disallow the creation of an event if its | |
543 | * associated stream class has been reclaimed. | |
544 | */ | |
545 | if (!event_class->stream_class) { | |
b8248cc0 | 546 | goto end; |
662e778c JG |
547 | } |
548 | assert(event_class->stream_class->event_header_type); | |
549 | ||
b8248cc0 PP |
550 | /* set "stream_id" attribute now that we know its value */ |
551 | obj = bt_object_integer_create_init(event_class->stream_class->id); | |
552 | if (!obj) { | |
553 | goto end; | |
554 | } | |
555 | ||
556 | ret = bt_ctf_attributes_set_field_value(event_class->attributes, | |
557 | "stream_id", obj); | |
558 | BT_OBJECT_PUT(obj); | |
559 | ||
560 | if (ret) { | |
561 | goto end; | |
562 | } | |
563 | ||
564 | event = g_new0(struct bt_ctf_event, 1); | |
565 | if (!event) { | |
566 | goto end; | |
567 | } | |
568 | ||
569 | bt_ctf_ref_init(&event->ref_count); | |
570 | bt_ctf_event_class_get(event_class); | |
571 | bt_ctf_event_class_freeze(event_class); | |
572 | event->event_class = event_class; | |
573 | ||
662e778c JG |
574 | event->event_header = bt_ctf_field_create( |
575 | event_class->stream_class->event_header_type); | |
576 | if (!event->event_header) { | |
577 | goto error_destroy; | |
578 | } | |
f655a84d JG |
579 | if (event_class->context) { |
580 | event->context_payload = bt_ctf_field_create( | |
581 | event_class->context); | |
662e778c JG |
582 | if (!event->context_payload) { |
583 | goto error_destroy; | |
584 | } | |
f655a84d | 585 | } |
273b65be | 586 | event->fields_payload = bt_ctf_field_create(event_class->fields); |
662e778c JG |
587 | if (!event->fields_payload) { |
588 | goto error_destroy; | |
589 | } | |
590 | ||
591 | /* | |
592 | * Freeze the stream class since the event header must not be changed | |
593 | * anymore. | |
594 | */ | |
595 | bt_ctf_stream_class_freeze(event_class->stream_class); | |
273b65be JG |
596 | end: |
597 | return event; | |
662e778c | 598 | error_destroy: |
b8248cc0 PP |
599 | if (event) { |
600 | bt_ctf_event_destroy(&event->ref_count); | |
601 | } | |
602 | ||
662e778c | 603 | return NULL; |
273b65be JG |
604 | } |
605 | ||
2f100782 JG |
606 | struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event) |
607 | { | |
608 | struct bt_ctf_event_class *event_class = NULL; | |
609 | ||
610 | if (!event) { | |
611 | goto end; | |
612 | } | |
613 | ||
614 | event_class = event->event_class; | |
615 | bt_ctf_event_class_get(event_class); | |
616 | end: | |
617 | return event_class; | |
618 | } | |
619 | ||
8e5003bb JG |
620 | struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event) |
621 | { | |
622 | struct bt_ctf_stream *stream = NULL; | |
623 | ||
624 | if (!event) { | |
625 | goto end; | |
626 | } | |
627 | ||
628 | stream = event->stream; | |
629 | if (stream) { | |
630 | bt_ctf_stream_get(stream); | |
631 | } | |
632 | end: | |
633 | return stream; | |
634 | } | |
635 | ||
2f100782 JG |
636 | struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event) |
637 | { | |
638 | struct bt_ctf_clock *clock = NULL; | |
639 | struct bt_ctf_event_class *event_class; | |
640 | struct bt_ctf_stream_class *stream_class; | |
641 | ||
642 | if (!event) { | |
643 | goto end; | |
644 | } | |
645 | ||
646 | event_class = bt_ctf_event_get_class(event); | |
647 | if (!event_class) { | |
648 | goto end; | |
649 | } | |
650 | ||
651 | stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
652 | if (!stream_class) { | |
653 | goto error_put_event_class; | |
654 | } | |
655 | ||
656 | clock = bt_ctf_stream_class_get_clock(stream_class); | |
657 | if (!clock) { | |
658 | goto error_put_stream_class; | |
659 | } | |
660 | ||
661 | error_put_stream_class: | |
662 | bt_ctf_stream_class_put(stream_class); | |
663 | error_put_event_class: | |
664 | bt_ctf_event_class_put(event_class); | |
665 | end: | |
666 | return clock; | |
667 | } | |
668 | ||
273b65be JG |
669 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, |
670 | const char *name, | |
c5a9aa19 | 671 | struct bt_ctf_field *payload) |
273b65be JG |
672 | { |
673 | int ret = 0; | |
674 | ||
c5a9aa19 | 675 | if (!event || !payload) { |
273b65be JG |
676 | ret = -1; |
677 | goto end; | |
678 | } | |
679 | ||
c5a9aa19 JG |
680 | if (name) { |
681 | ret = bt_ctf_field_structure_set_field(event->fields_payload, | |
682 | name, payload); | |
683 | } else { | |
684 | struct bt_ctf_field_type *payload_type; | |
685 | ||
686 | payload_type = bt_ctf_field_get_type(payload); | |
687 | if (payload_type == event->event_class->fields) { | |
688 | bt_ctf_field_put(event->fields_payload); | |
689 | bt_ctf_field_get(payload); | |
690 | event->fields_payload = payload; | |
691 | } else { | |
692 | ret = -1; | |
693 | } | |
694 | ||
695 | bt_ctf_field_type_put(payload_type); | |
696 | } | |
273b65be JG |
697 | end: |
698 | return ret; | |
699 | } | |
700 | ||
701 | ||
702 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, | |
703 | const char *name) | |
704 | { | |
705 | struct bt_ctf_field *field = NULL; | |
706 | ||
c5a9aa19 | 707 | if (!event) { |
273b65be JG |
708 | goto end; |
709 | } | |
710 | ||
c5a9aa19 JG |
711 | if (name) { |
712 | field = bt_ctf_field_structure_get_field(event->fields_payload, | |
713 | name); | |
714 | } else { | |
715 | field = event->fields_payload; | |
716 | bt_ctf_field_get(field); | |
717 | } | |
273b65be JG |
718 | end: |
719 | return field; | |
720 | } | |
721 | ||
2f100782 | 722 | struct bt_ctf_field *bt_ctf_event_get_payload_by_index( |
074ee56d | 723 | struct bt_ctf_event *event, int index) |
2f100782 JG |
724 | { |
725 | struct bt_ctf_field *field = NULL; | |
726 | ||
074ee56d | 727 | if (!event || index < 0) { |
2f100782 JG |
728 | goto end; |
729 | } | |
730 | ||
731 | field = bt_ctf_field_structure_get_field_by_index(event->fields_payload, | |
732 | index); | |
733 | end: | |
734 | return field; | |
735 | } | |
736 | ||
662e778c JG |
737 | struct bt_ctf_field *bt_ctf_event_get_event_header( |
738 | struct bt_ctf_event *event) | |
739 | { | |
740 | struct bt_ctf_field *header = NULL; | |
741 | ||
742 | if (!event || !event->event_header) { | |
743 | goto end; | |
744 | } | |
745 | ||
746 | header = event->event_header; | |
747 | bt_ctf_field_get(header); | |
748 | end: | |
749 | return header; | |
750 | } | |
751 | ||
752 | int bt_ctf_event_set_event_header(struct bt_ctf_event *event, | |
753 | struct bt_ctf_field *header) | |
754 | { | |
755 | int ret = 0; | |
756 | struct bt_ctf_field_type *field_type = NULL; | |
757 | ||
758 | if (!event || !header) { | |
759 | ret = -1; | |
760 | goto end; | |
761 | } | |
762 | ||
763 | /* Could be NULL since an event class doesn't own a stream class */ | |
764 | if (!event->event_class->stream_class) { | |
765 | ret = -1; | |
766 | goto end; | |
767 | } | |
768 | ||
769 | /* | |
770 | * Ensure the provided header's type matches the one registered to the | |
771 | * stream class. | |
772 | */ | |
773 | field_type = bt_ctf_field_get_type(header); | |
774 | if (field_type != event->event_class->stream_class->event_header_type) { | |
775 | ret = -1; | |
776 | goto end; | |
777 | } | |
778 | ||
779 | bt_ctf_field_get(header); | |
780 | bt_ctf_field_put(event->event_header); | |
781 | event->event_header = header; | |
782 | end: | |
783 | if (field_type) { | |
784 | bt_ctf_field_type_put(field_type); | |
785 | } | |
786 | return ret; | |
787 | } | |
788 | ||
f655a84d JG |
789 | struct bt_ctf_field *bt_ctf_event_get_event_context( |
790 | struct bt_ctf_event *event) | |
791 | { | |
792 | struct bt_ctf_field *context = NULL; | |
793 | ||
794 | if (!event || !event->context_payload) { | |
795 | goto end; | |
796 | } | |
797 | ||
798 | context = event->context_payload; | |
799 | bt_ctf_field_get(context); | |
800 | end: | |
801 | return context; | |
802 | } | |
803 | ||
804 | int bt_ctf_event_set_event_context(struct bt_ctf_event *event, | |
805 | struct bt_ctf_field *context) | |
806 | { | |
807 | int ret = 0; | |
808 | struct bt_ctf_field_type *field_type = NULL; | |
809 | ||
810 | if (!event || !context) { | |
811 | ret = -1; | |
812 | goto end; | |
813 | } | |
814 | ||
815 | field_type = bt_ctf_field_get_type(context); | |
816 | if (field_type != event->event_class->context) { | |
817 | ret = -1; | |
818 | goto end; | |
819 | } | |
820 | ||
821 | bt_ctf_field_get(context); | |
822 | bt_ctf_field_put(event->context_payload); | |
823 | event->context_payload = context; | |
824 | end: | |
825 | if (field_type) { | |
826 | bt_ctf_field_type_put(field_type); | |
827 | } | |
828 | return ret; | |
829 | } | |
830 | ||
273b65be JG |
831 | void bt_ctf_event_get(struct bt_ctf_event *event) |
832 | { | |
833 | if (!event) { | |
834 | return; | |
835 | } | |
836 | ||
837 | bt_ctf_ref_get(&event->ref_count); | |
838 | } | |
839 | ||
840 | void bt_ctf_event_put(struct bt_ctf_event *event) | |
841 | { | |
842 | if (!event) { | |
843 | return; | |
844 | } | |
845 | ||
846 | bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy); | |
847 | } | |
848 | ||
849 | static | |
850 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref) | |
851 | { | |
852 | struct bt_ctf_event_class *event_class; | |
853 | ||
854 | if (!ref) { | |
855 | return; | |
856 | } | |
857 | ||
2f100782 JG |
858 | /* |
859 | * Don't call put() on the stream class. See comment in | |
860 | * bt_ctf_event_class_set_stream_class for explanation. | |
861 | */ | |
273b65be | 862 | event_class = container_of(ref, struct bt_ctf_event_class, ref_count); |
b8248cc0 PP |
863 | if (event_class->attributes) { |
864 | bt_ctf_attributes_destroy(event_class->attributes); | |
865 | } | |
f655a84d JG |
866 | if (event_class->context) { |
867 | bt_ctf_field_type_put(event_class->context); | |
868 | } | |
869 | if (event_class->fields) { | |
870 | bt_ctf_field_type_put(event_class->fields); | |
871 | } | |
273b65be JG |
872 | g_free(event_class); |
873 | } | |
874 | ||
875 | static | |
876 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref) | |
877 | { | |
878 | struct bt_ctf_event *event; | |
879 | ||
880 | if (!ref) { | |
881 | return; | |
882 | } | |
883 | ||
884 | event = container_of(ref, struct bt_ctf_event, | |
885 | ref_count); | |
f655a84d JG |
886 | if (event->event_class) { |
887 | bt_ctf_event_class_put(event->event_class); | |
888 | } | |
662e778c JG |
889 | if (event->event_header) { |
890 | bt_ctf_field_put(event->event_header); | |
891 | } | |
f655a84d JG |
892 | if (event->context_payload) { |
893 | bt_ctf_field_put(event->context_payload); | |
894 | } | |
895 | if (event->fields_payload) { | |
896 | bt_ctf_field_put(event->fields_payload); | |
897 | } | |
273b65be JG |
898 | g_free(event); |
899 | } | |
900 | ||
662e778c JG |
901 | static |
902 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) | |
903 | { | |
904 | int ret = 0; | |
905 | struct bt_ctf_field_type *field_type = NULL; | |
906 | ||
907 | if (!field) { | |
908 | ret = -1; | |
909 | goto end; | |
910 | } | |
911 | ||
912 | if (!bt_ctf_field_validate(field)) { | |
913 | /* Payload already set, skip! (not an error) */ | |
914 | goto end; | |
915 | } | |
916 | ||
917 | field_type = bt_ctf_field_get_type(field); | |
918 | assert(field_type); | |
919 | ||
920 | if (bt_ctf_field_type_get_type_id(field_type) != CTF_TYPE_INTEGER) { | |
921 | /* Not an integer and the value is unset, error. */ | |
922 | ret = -1; | |
923 | goto end; | |
924 | } | |
925 | ||
926 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
927 | ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value); | |
928 | if (ret) { | |
929 | /* Value is out of range, error. */ | |
930 | goto end; | |
931 | } | |
932 | } else { | |
933 | ret = bt_ctf_field_unsigned_integer_set_value(field, value); | |
934 | if (ret) { | |
935 | /* Value is out of range, error. */ | |
936 | goto end; | |
937 | } | |
938 | } | |
939 | end: | |
940 | bt_ctf_field_type_put(field_type); | |
941 | return ret; | |
942 | } | |
943 | ||
273b65be JG |
944 | BT_HIDDEN |
945 | void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class) | |
946 | { | |
947 | assert(event_class); | |
948 | event_class->frozen = 1; | |
949 | bt_ctf_field_type_freeze(event_class->context); | |
950 | bt_ctf_field_type_freeze(event_class->fields); | |
b8248cc0 | 951 | bt_ctf_attributes_freeze(event_class->attributes); |
273b65be JG |
952 | } |
953 | ||
954 | BT_HIDDEN | |
2f100782 JG |
955 | int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class *event_class, |
956 | struct bt_ctf_stream_class *stream_class) | |
273b65be JG |
957 | { |
958 | int ret = 0; | |
959 | ||
2f100782 | 960 | if (!event_class) { |
273b65be JG |
961 | ret = -1; |
962 | goto end; | |
963 | } | |
964 | ||
2f100782 JG |
965 | /* Allow a NULL stream_class to unset the current stream_class */ |
966 | if (stream_class && event_class->stream_class) { | |
273b65be JG |
967 | ret = -1; |
968 | goto end; | |
969 | } | |
970 | ||
2f100782 JG |
971 | event_class->stream_class = stream_class; |
972 | /* | |
973 | * We don't get() the stream_class since doing so would introduce | |
974 | * a circular ownership between event classes and stream classes. | |
975 | * | |
976 | * A stream class will always unset itself from its events before | |
977 | * being destroyed. This ensures that a user won't get a pointer | |
978 | * to a stale stream class instance from an event class. | |
979 | */ | |
273b65be JG |
980 | end: |
981 | return ret; | |
982 | } | |
983 | ||
984 | BT_HIDDEN | |
985 | int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class, | |
986 | struct metadata_context *context) | |
987 | { | |
b8248cc0 PP |
988 | int i; |
989 | int count; | |
273b65be | 990 | int ret = 0; |
b8248cc0 | 991 | struct bt_object *attr_value = NULL; |
273b65be JG |
992 | |
993 | assert(event_class); | |
994 | assert(context); | |
b8248cc0 PP |
995 | |
996 | context->current_indentation_level = 1; | |
997 | g_string_assign(context->field_name, ""); | |
998 | g_string_append(context->string, "event {\n"); | |
999 | count = bt_ctf_event_class_get_attribute_count(event_class); | |
1000 | ||
1001 | if (count < 0) { | |
2f100782 JG |
1002 | ret = -1; |
1003 | goto end; | |
1004 | } | |
1005 | ||
b8248cc0 PP |
1006 | for (i = 0; i < count; ++i) { |
1007 | const char *attr_name = NULL; | |
1008 | ||
1009 | attr_name = bt_ctf_event_class_get_attribute_name( | |
1010 | event_class, i); | |
1011 | attr_value = bt_ctf_event_class_get_attribute_value( | |
1012 | event_class, i); | |
1013 | ||
1014 | if (!attr_name || !attr_value) { | |
1015 | ret = -1; | |
1016 | goto end; | |
1017 | } | |
1018 | ||
1019 | switch (bt_object_get_type(attr_value)) { | |
1020 | case BT_OBJECT_TYPE_INTEGER: | |
1021 | { | |
1022 | int64_t value; | |
1023 | ||
1024 | ret = bt_object_integer_get(attr_value, &value); | |
1025 | ||
1026 | if (ret) { | |
1027 | goto end; | |
1028 | } | |
1029 | ||
1030 | g_string_append_printf(context->string, | |
1031 | "\t%s = %" PRId64 ";\n", attr_name, value); | |
1032 | break; | |
1033 | } | |
1034 | ||
1035 | case BT_OBJECT_TYPE_STRING: | |
1036 | { | |
1037 | const char *value; | |
1038 | ||
1039 | ret = bt_object_string_get(attr_value, &value); | |
1040 | ||
1041 | if (ret) { | |
1042 | goto end; | |
1043 | } | |
1044 | ||
1045 | g_string_append_printf(context->string, | |
1046 | "\t%s = \"%s\";\n", attr_name, value); | |
1047 | break; | |
1048 | } | |
1049 | ||
1050 | default: | |
1051 | /* should never happen */ | |
1052 | assert(false); | |
1053 | break; | |
1054 | } | |
1055 | ||
1056 | BT_OBJECT_PUT(attr_value); | |
1057 | } | |
273b65be JG |
1058 | |
1059 | if (event_class->context) { | |
1060 | g_string_append(context->string, "\tcontext := "); | |
1061 | ret = bt_ctf_field_type_serialize(event_class->context, | |
1062 | context); | |
1063 | if (ret) { | |
1064 | goto end; | |
1065 | } | |
1066 | g_string_append(context->string, ";\n"); | |
1067 | } | |
1068 | ||
1069 | if (event_class->fields) { | |
1070 | g_string_append(context->string, "\tfields := "); | |
1071 | ret = bt_ctf_field_type_serialize(event_class->fields, context); | |
1072 | if (ret) { | |
1073 | goto end; | |
1074 | } | |
1075 | g_string_append(context->string, ";\n"); | |
1076 | } | |
1077 | ||
1078 | g_string_append(context->string, "};\n\n"); | |
1079 | end: | |
1080 | context->current_indentation_level = 0; | |
b8248cc0 | 1081 | BT_OBJECT_PUT(attr_value); |
273b65be JG |
1082 | return ret; |
1083 | } | |
1084 | ||
c35a1669 JG |
1085 | void bt_ctf_event_class_set_native_byte_order( |
1086 | struct bt_ctf_event_class *event_class, | |
1087 | int byte_order) | |
1088 | { | |
1089 | if (!event_class) { | |
1090 | return; | |
1091 | } | |
1092 | ||
1093 | bt_ctf_field_type_set_native_byte_order(event_class->context, | |
1094 | byte_order); | |
1095 | bt_ctf_field_type_set_native_byte_order(event_class->fields, | |
1096 | byte_order); | |
1097 | } | |
1098 | ||
273b65be JG |
1099 | BT_HIDDEN |
1100 | int bt_ctf_event_validate(struct bt_ctf_event *event) | |
1101 | { | |
1102 | /* Make sure each field's payload has been set */ | |
1103 | int ret; | |
1104 | ||
1105 | assert(event); | |
662e778c JG |
1106 | ret = bt_ctf_field_validate(event->event_header); |
1107 | if (ret) { | |
1108 | goto end; | |
1109 | } | |
1110 | ||
273b65be JG |
1111 | ret = bt_ctf_field_validate(event->fields_payload); |
1112 | if (ret) { | |
1113 | goto end; | |
1114 | } | |
1115 | ||
1116 | if (event->event_class->context) { | |
1117 | ret = bt_ctf_field_validate(event->context_payload); | |
1118 | } | |
1119 | end: | |
1120 | return ret; | |
1121 | } | |
1122 | ||
1123 | BT_HIDDEN | |
1124 | int bt_ctf_event_serialize(struct bt_ctf_event *event, | |
1125 | struct ctf_stream_pos *pos) | |
1126 | { | |
1127 | int ret = 0; | |
1128 | ||
1129 | assert(event); | |
1130 | assert(pos); | |
1131 | if (event->context_payload) { | |
1132 | ret = bt_ctf_field_serialize(event->context_payload, pos); | |
1133 | if (ret) { | |
1134 | goto end; | |
1135 | } | |
1136 | } | |
1137 | ||
1138 | if (event->fields_payload) { | |
1139 | ret = bt_ctf_field_serialize(event->fields_payload, pos); | |
1140 | if (ret) { | |
1141 | goto end; | |
1142 | } | |
1143 | } | |
1144 | end: | |
1145 | return ret; | |
1146 | } | |
1147 | ||
662e778c JG |
1148 | BT_HIDDEN |
1149 | int bt_ctf_event_populate_event_header(struct bt_ctf_event *event) | |
1150 | { | |
1151 | int ret = 0; | |
1152 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; | |
1153 | ||
1154 | if (!event) { | |
1155 | ret = -1; | |
1156 | goto end; | |
1157 | } | |
1158 | ||
1159 | id_field = bt_ctf_field_structure_get_field(event->event_header, "id"); | |
1160 | if (id_field) { | |
1161 | ret = set_integer_field_value(id_field, | |
b8248cc0 PP |
1162 | (uint64_t) bt_ctf_event_class_get_id( |
1163 | event->event_class)); | |
662e778c JG |
1164 | if (ret) { |
1165 | goto end; | |
1166 | } | |
1167 | } | |
1168 | ||
1169 | timestamp_field = bt_ctf_field_structure_get_field(event->event_header, | |
1170 | "timestamp"); | |
1171 | if (timestamp_field) { | |
9a220c32 JG |
1172 | struct bt_ctf_field_type *timestamp_field_type = |
1173 | bt_ctf_field_get_type(timestamp_field); | |
1174 | struct bt_ctf_clock *mapped_clock; | |
1175 | ||
1176 | assert(timestamp_field_type); | |
1177 | mapped_clock = bt_ctf_field_type_integer_get_mapped_clock( | |
1178 | timestamp_field_type); | |
1179 | bt_ctf_field_type_put(timestamp_field_type); | |
1180 | if (mapped_clock) { | |
1181 | uint64_t timestamp = bt_ctf_clock_get_time( | |
1182 | mapped_clock); | |
1183 | ||
1184 | bt_ctf_clock_put(mapped_clock); | |
1185 | if (timestamp == (uint64_t) -1ULL) { | |
1186 | goto end; | |
1187 | } | |
1188 | ||
1189 | ret = set_integer_field_value(timestamp_field, | |
1190 | timestamp); | |
1191 | if (ret) { | |
1192 | goto end; | |
1193 | } | |
662e778c JG |
1194 | } |
1195 | } | |
1196 | end: | |
1197 | if (id_field) { | |
1198 | bt_ctf_field_put(id_field); | |
1199 | } | |
1200 | if (timestamp_field) { | |
1201 | bt_ctf_field_put(timestamp_field); | |
1202 | } | |
1203 | return ret; | |
1204 | } | |
123fbdec JG |
1205 | |
1206 | BT_HIDDEN | |
1207 | int bt_ctf_event_set_stream(struct bt_ctf_event *event, | |
1208 | struct bt_ctf_stream *stream) | |
1209 | { | |
1210 | int ret = 0; | |
1211 | ||
1212 | if (!event) { | |
1213 | ret = -1; | |
1214 | goto end; | |
1215 | } | |
1216 | ||
1217 | if (event->stream && stream) { | |
1218 | /* Already attached to a stream */ | |
1219 | ret = -1; | |
1220 | goto end; | |
1221 | } | |
1222 | ||
1223 | event->stream = stream; | |
1224 | end: | |
1225 | return ret; | |
1226 | } |