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 | ||
2e33ac5a PP |
29 | #include <babeltrace/ctf-ir/fields-internal.h> |
30 | #include <babeltrace/ctf-ir/field-types-internal.h> | |
adc315b8 | 31 | #include <babeltrace/ctf-ir/event-internal.h> |
272df73e PP |
32 | #include <babeltrace/ctf-ir/event-class.h> |
33 | #include <babeltrace/ctf-ir/event-class-internal.h> | |
2f100782 | 34 | #include <babeltrace/ctf-ir/stream-class.h> |
c35a1669 | 35 | #include <babeltrace/ctf-ir/stream-class-internal.h> |
4ce9f9d0 PP |
36 | #include <babeltrace/ctf-ir/packet.h> |
37 | #include <babeltrace/ctf-ir/packet-internal.h> | |
bc37ae52 | 38 | #include <babeltrace/ctf-ir/trace-internal.h> |
09840de5 | 39 | #include <babeltrace/ctf-ir/validation-internal.h> |
5c3b707d | 40 | #include <babeltrace/ctf-ir/packet-internal.h> |
654c1444 | 41 | #include <babeltrace/ctf-ir/utils.h> |
83509119 | 42 | #include <babeltrace/ref.h> |
44e0a4f5 | 43 | #include <babeltrace/ctf-ir/attributes-internal.h> |
273b65be JG |
44 | #include <babeltrace/compiler.h> |
45 | ||
273b65be | 46 | static |
83509119 | 47 | void bt_ctf_event_destroy(struct bt_object *obj); |
662e778c JG |
48 | static |
49 | int set_integer_field_value(struct bt_ctf_field *field, uint64_t value); | |
273b65be | 50 | |
273b65be JG |
51 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) |
52 | { | |
09840de5 PP |
53 | int ret; |
54 | enum bt_ctf_validation_flag validation_flags = | |
55 | BT_CTF_VALIDATION_FLAG_STREAM | | |
56 | BT_CTF_VALIDATION_FLAG_EVENT; | |
273b65be | 57 | struct bt_ctf_event *event = NULL; |
09840de5 | 58 | struct bt_ctf_trace *trace = NULL; |
e6a8e8e4 | 59 | struct bt_ctf_stream_class *stream_class = NULL; |
09840de5 PP |
60 | struct bt_ctf_field_type *packet_header_type = NULL; |
61 | struct bt_ctf_field_type *packet_context_type = NULL; | |
62 | struct bt_ctf_field_type *event_header_type = NULL; | |
63 | struct bt_ctf_field_type *stream_event_ctx_type = NULL; | |
64 | struct bt_ctf_field_type *event_context_type = NULL; | |
65 | struct bt_ctf_field_type *event_payload_type = NULL; | |
66 | struct bt_ctf_field *event_header = NULL; | |
5fd2e9fd | 67 | struct bt_ctf_field *stream_event_context = NULL; |
09840de5 PP |
68 | struct bt_ctf_field *event_context = NULL; |
69 | struct bt_ctf_field *event_payload = NULL; | |
70 | struct bt_value *environment = NULL; | |
71 | struct bt_ctf_validation_output validation_output = { 0 }; | |
72 | int trace_valid = 0; | |
273b65be JG |
73 | |
74 | if (!event_class) { | |
e6a8e8e4 | 75 | goto error; |
273b65be JG |
76 | } |
77 | ||
e6a8e8e4 | 78 | stream_class = bt_ctf_event_class_get_stream_class(event_class); |
09840de5 | 79 | |
662e778c | 80 | /* |
e6a8e8e4 JG |
81 | * We disallow the creation of an event if its event class has not been |
82 | * associated to a stream class. | |
662e778c | 83 | */ |
e6a8e8e4 JG |
84 | if (!stream_class) { |
85 | goto error; | |
662e778c | 86 | } |
09840de5 PP |
87 | |
88 | /* A stream class should always have an existing event header type */ | |
e6a8e8e4 | 89 | assert(stream_class->event_header_type); |
09840de5 PP |
90 | |
91 | /* The event class was frozen when added to its stream class */ | |
92 | assert(event_class->frozen); | |
93 | ||
94 | /* Validate the trace (if any), the stream class, and the event class */ | |
95 | trace = bt_ctf_stream_class_get_trace(stream_class); | |
96 | if (trace) { | |
97 | packet_header_type = bt_ctf_trace_get_packet_header_type(trace); | |
98 | trace_valid = trace->valid; | |
99 | assert(trace_valid); | |
100 | environment = trace->environment; | |
101 | } | |
102 | ||
103 | packet_context_type = bt_ctf_stream_class_get_packet_context_type( | |
104 | stream_class); | |
105 | event_header_type = bt_ctf_stream_class_get_event_header_type( | |
106 | stream_class); | |
107 | stream_event_ctx_type = bt_ctf_stream_class_get_event_context_type( | |
108 | stream_class); | |
109 | event_context_type = bt_ctf_event_class_get_context_type(event_class); | |
110 | event_payload_type = bt_ctf_event_class_get_payload_type(event_class); | |
111 | ret = bt_ctf_validate_class_types(environment, packet_header_type, | |
112 | packet_context_type, event_header_type, stream_event_ctx_type, | |
113 | event_context_type, event_payload_type, trace_valid, | |
114 | stream_class->valid, event_class->valid, | |
115 | &validation_output, validation_flags); | |
116 | BT_PUT(packet_header_type); | |
117 | BT_PUT(packet_context_type); | |
118 | BT_PUT(event_header_type); | |
119 | BT_PUT(stream_event_ctx_type); | |
120 | BT_PUT(event_context_type); | |
121 | BT_PUT(event_payload_type); | |
09840de5 PP |
122 | if (ret) { |
123 | /* | |
124 | * This means something went wrong during the validation | |
125 | * process, not that the objects are invalid. | |
126 | */ | |
127 | goto error; | |
128 | } | |
129 | ||
130 | if ((validation_output.valid_flags & validation_flags) != | |
131 | validation_flags) { | |
132 | /* Invalid trace/stream class/event class */ | |
133 | goto error; | |
134 | } | |
135 | ||
136 | /* | |
137 | * At this point we know the trace (if associated to the stream | |
138 | * class), the stream class, and the event class, with their | |
139 | * current types, are valid. We may proceed with creating | |
140 | * the event. | |
141 | */ | |
b8248cc0 PP |
142 | event = g_new0(struct bt_ctf_event, 1); |
143 | if (!event) { | |
e6a8e8e4 | 144 | goto error; |
b8248cc0 PP |
145 | } |
146 | ||
83509119 | 147 | bt_object_init(event, bt_ctf_event_destroy); |
09840de5 | 148 | |
e6a8e8e4 JG |
149 | /* |
150 | * event does not share a common ancestor with the event class; it has | |
151 | * to guarantee its existence by holding a reference. This reference | |
152 | * shall be released once the event is associated to a stream since, | |
153 | * from that point, the event and its class will share the same | |
154 | * lifetime. | |
155 | */ | |
156 | event->event_class = bt_get(event_class); | |
09840de5 PP |
157 | event_header = |
158 | bt_ctf_field_create(validation_output.event_header_type); | |
09840de5 | 159 | if (!event_header) { |
83509119 | 160 | goto error; |
662e778c | 161 | } |
09840de5 | 162 | |
5fd2e9fd PP |
163 | if (validation_output.stream_event_ctx_type) { |
164 | stream_event_context = bt_ctf_field_create( | |
165 | validation_output.stream_event_ctx_type); | |
166 | if (!stream_event_context) { | |
167 | goto error; | |
168 | } | |
169 | } | |
170 | ||
09840de5 PP |
171 | if (validation_output.event_context_type) { |
172 | event_context = bt_ctf_field_create( | |
173 | validation_output.event_context_type); | |
174 | if (!event_context) { | |
83509119 | 175 | goto error; |
662e778c | 176 | } |
f655a84d | 177 | } |
09840de5 PP |
178 | |
179 | if (validation_output.event_payload_type) { | |
180 | event_payload = bt_ctf_field_create( | |
181 | validation_output.event_payload_type); | |
182 | if (!event_payload) { | |
183 | goto error; | |
184 | } | |
662e778c JG |
185 | } |
186 | ||
09840de5 PP |
187 | /* |
188 | * At this point all the fields are created, potentially from | |
189 | * validated copies of field types, so that the field types and | |
190 | * fields can be replaced in the trace, stream class, | |
191 | * event class, and created event. | |
192 | */ | |
193 | bt_ctf_validation_replace_types(trace, stream_class, | |
194 | event_class, &validation_output, validation_flags); | |
195 | BT_MOVE(event->event_header, event_header); | |
5fd2e9fd | 196 | BT_MOVE(event->stream_event_context, stream_event_context); |
09840de5 PP |
197 | BT_MOVE(event->context_payload, event_context); |
198 | BT_MOVE(event->fields_payload, event_payload); | |
199 | ||
200 | /* | |
201 | * Put what was not moved in bt_ctf_validation_replace_types(). | |
202 | */ | |
203 | bt_ctf_validation_output_put_types(&validation_output); | |
204 | ||
662e778c JG |
205 | /* |
206 | * Freeze the stream class since the event header must not be changed | |
207 | * anymore. | |
208 | */ | |
e6a8e8e4 | 209 | bt_ctf_stream_class_freeze(stream_class); |
09840de5 PP |
210 | |
211 | /* | |
212 | * Mark stream class, and event class as valid since | |
213 | * they're all frozen now. | |
214 | */ | |
215 | stream_class->valid = 1; | |
216 | event_class->valid = 1; | |
217 | ||
218 | /* Put stuff we borrowed from the event class */ | |
e6a8e8e4 | 219 | BT_PUT(stream_class); |
09840de5 PP |
220 | BT_PUT(trace); |
221 | ||
273b65be | 222 | return event; |
09840de5 | 223 | |
83509119 | 224 | error: |
09840de5 | 225 | bt_ctf_validation_output_put_types(&validation_output); |
83509119 | 226 | BT_PUT(event); |
e6a8e8e4 | 227 | BT_PUT(stream_class); |
09840de5 PP |
228 | BT_PUT(trace); |
229 | BT_PUT(event_header); | |
5fd2e9fd | 230 | BT_PUT(stream_event_context); |
09840de5 PP |
231 | BT_PUT(event_context); |
232 | BT_PUT(event_payload); | |
233 | assert(!packet_header_type); | |
234 | assert(!packet_context_type); | |
235 | assert(!event_header_type); | |
236 | assert(!stream_event_ctx_type); | |
237 | assert(!event_context_type); | |
238 | assert(!event_payload_type); | |
239 | ||
83509119 | 240 | return event; |
273b65be JG |
241 | } |
242 | ||
2f100782 JG |
243 | struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event) |
244 | { | |
245 | struct bt_ctf_event_class *event_class = NULL; | |
246 | ||
247 | if (!event) { | |
248 | goto end; | |
249 | } | |
250 | ||
251 | event_class = event->event_class; | |
83509119 | 252 | bt_get(event_class); |
2f100782 JG |
253 | end: |
254 | return event_class; | |
255 | } | |
256 | ||
8e5003bb JG |
257 | struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event) |
258 | { | |
e6a8e8e4 | 259 | return (struct bt_ctf_stream *) bt_object_get_parent(event); |
8e5003bb JG |
260 | } |
261 | ||
2f100782 JG |
262 | struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event) |
263 | { | |
264 | struct bt_ctf_clock *clock = NULL; | |
265 | struct bt_ctf_event_class *event_class; | |
266 | struct bt_ctf_stream_class *stream_class; | |
267 | ||
268 | if (!event) { | |
269 | goto end; | |
270 | } | |
271 | ||
272 | event_class = bt_ctf_event_get_class(event); | |
273 | if (!event_class) { | |
274 | goto end; | |
275 | } | |
276 | ||
277 | stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
278 | if (!stream_class) { | |
279 | goto error_put_event_class; | |
280 | } | |
281 | ||
282 | clock = bt_ctf_stream_class_get_clock(stream_class); | |
283 | if (!clock) { | |
284 | goto error_put_stream_class; | |
285 | } | |
286 | ||
287 | error_put_stream_class: | |
83509119 | 288 | bt_put(stream_class); |
2f100782 | 289 | error_put_event_class: |
83509119 | 290 | bt_put(event_class); |
2f100782 JG |
291 | end: |
292 | return clock; | |
293 | } | |
294 | ||
273b65be JG |
295 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, |
296 | const char *name, | |
c5a9aa19 | 297 | struct bt_ctf_field *payload) |
273b65be JG |
298 | { |
299 | int ret = 0; | |
300 | ||
4ce9f9d0 | 301 | if (!event || !payload || event->frozen) { |
273b65be JG |
302 | ret = -1; |
303 | goto end; | |
304 | } | |
305 | ||
c5a9aa19 JG |
306 | if (name) { |
307 | ret = bt_ctf_field_structure_set_field(event->fields_payload, | |
308 | name, payload); | |
309 | } else { | |
310 | struct bt_ctf_field_type *payload_type; | |
311 | ||
312 | payload_type = bt_ctf_field_get_type(payload); | |
09840de5 PP |
313 | |
314 | if (bt_ctf_field_type_compare(payload_type, | |
315 | event->event_class->fields) == 0) { | |
83509119 JG |
316 | bt_put(event->fields_payload); |
317 | bt_get(payload); | |
c5a9aa19 JG |
318 | event->fields_payload = payload; |
319 | } else { | |
320 | ret = -1; | |
321 | } | |
322 | ||
83509119 | 323 | bt_put(payload_type); |
c5a9aa19 | 324 | } |
273b65be JG |
325 | end: |
326 | return ret; | |
327 | } | |
328 | ||
71362d53 PP |
329 | struct bt_ctf_field *bt_ctf_event_get_payload_field(struct bt_ctf_event *event) |
330 | { | |
331 | struct bt_ctf_field *payload = NULL; | |
332 | ||
333 | if (!event || !event->fields_payload) { | |
334 | goto end; | |
335 | } | |
336 | ||
337 | payload = event->fields_payload; | |
83509119 | 338 | bt_get(payload); |
71362d53 PP |
339 | end: |
340 | return payload; | |
341 | } | |
273b65be | 342 | |
e5e6eb3a JG |
343 | int bt_ctf_event_set_payload_field(struct bt_ctf_event *event, |
344 | struct bt_ctf_field *payload) | |
345 | { | |
346 | int ret = 0; | |
347 | struct bt_ctf_field_type *payload_type = NULL; | |
348 | ||
4ce9f9d0 | 349 | if (!event || !payload || event->frozen) { |
e5e6eb3a JG |
350 | ret = -1; |
351 | goto end; | |
352 | } | |
353 | ||
354 | payload_type = bt_ctf_field_get_type(payload); | |
355 | if (!payload_type) { | |
356 | ret = -1; | |
357 | goto end; | |
358 | } | |
359 | ||
9a19a512 PP |
360 | if (bt_ctf_field_type_get_type_id(payload_type) != |
361 | BT_CTF_TYPE_ID_STRUCT) { | |
e5e6eb3a JG |
362 | ret = -1; |
363 | goto end; | |
364 | } | |
365 | ||
83509119 JG |
366 | bt_get(payload); |
367 | bt_put(event->fields_payload); | |
e5e6eb3a JG |
368 | event->fields_payload = payload; |
369 | ||
370 | end: | |
83509119 | 371 | bt_put(payload_type); |
e5e6eb3a JG |
372 | return ret; |
373 | } | |
374 | ||
273b65be JG |
375 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, |
376 | const char *name) | |
377 | { | |
378 | struct bt_ctf_field *field = NULL; | |
379 | ||
c5a9aa19 | 380 | if (!event) { |
273b65be JG |
381 | goto end; |
382 | } | |
383 | ||
c5a9aa19 JG |
384 | if (name) { |
385 | field = bt_ctf_field_structure_get_field(event->fields_payload, | |
386 | name); | |
387 | } else { | |
388 | field = event->fields_payload; | |
83509119 | 389 | bt_get(field); |
c5a9aa19 | 390 | } |
273b65be JG |
391 | end: |
392 | return field; | |
393 | } | |
394 | ||
2f100782 | 395 | struct bt_ctf_field *bt_ctf_event_get_payload_by_index( |
074ee56d | 396 | struct bt_ctf_event *event, int index) |
2f100782 JG |
397 | { |
398 | struct bt_ctf_field *field = NULL; | |
399 | ||
074ee56d | 400 | if (!event || index < 0) { |
2f100782 JG |
401 | goto end; |
402 | } | |
403 | ||
404 | field = bt_ctf_field_structure_get_field_by_index(event->fields_payload, | |
405 | index); | |
406 | end: | |
407 | return field; | |
408 | } | |
409 | ||
286a2840 | 410 | struct bt_ctf_field *bt_ctf_event_get_header( |
662e778c JG |
411 | struct bt_ctf_event *event) |
412 | { | |
413 | struct bt_ctf_field *header = NULL; | |
414 | ||
415 | if (!event || !event->event_header) { | |
416 | goto end; | |
417 | } | |
418 | ||
419 | header = event->event_header; | |
83509119 | 420 | bt_get(header); |
662e778c JG |
421 | end: |
422 | return header; | |
423 | } | |
424 | ||
286a2840 | 425 | int bt_ctf_event_set_header(struct bt_ctf_event *event, |
662e778c JG |
426 | struct bt_ctf_field *header) |
427 | { | |
428 | int ret = 0; | |
429 | struct bt_ctf_field_type *field_type = NULL; | |
e6a8e8e4 | 430 | struct bt_ctf_stream_class *stream_class = NULL; |
662e778c | 431 | |
4ce9f9d0 | 432 | if (!event || !header || event->frozen) { |
662e778c JG |
433 | ret = -1; |
434 | goto end; | |
435 | } | |
436 | ||
e6a8e8e4 JG |
437 | stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent( |
438 | event->event_class); | |
662e778c JG |
439 | /* |
440 | * Ensure the provided header's type matches the one registered to the | |
441 | * stream class. | |
442 | */ | |
443 | field_type = bt_ctf_field_get_type(header); | |
09840de5 PP |
444 | if (bt_ctf_field_type_compare(field_type, |
445 | stream_class->event_header_type)) { | |
662e778c JG |
446 | ret = -1; |
447 | goto end; | |
448 | } | |
449 | ||
83509119 JG |
450 | bt_get(header); |
451 | bt_put(event->event_header); | |
662e778c JG |
452 | event->event_header = header; |
453 | end: | |
e6a8e8e4 | 454 | bt_put(stream_class); |
83509119 | 455 | bt_put(field_type); |
662e778c JG |
456 | return ret; |
457 | } | |
458 | ||
f655a84d JG |
459 | struct bt_ctf_field *bt_ctf_event_get_event_context( |
460 | struct bt_ctf_event *event) | |
461 | { | |
462 | struct bt_ctf_field *context = NULL; | |
463 | ||
464 | if (!event || !event->context_payload) { | |
465 | goto end; | |
466 | } | |
467 | ||
468 | context = event->context_payload; | |
83509119 | 469 | bt_get(context); |
f655a84d JG |
470 | end: |
471 | return context; | |
472 | } | |
473 | ||
474 | int bt_ctf_event_set_event_context(struct bt_ctf_event *event, | |
475 | struct bt_ctf_field *context) | |
476 | { | |
477 | int ret = 0; | |
478 | struct bt_ctf_field_type *field_type = NULL; | |
479 | ||
4ce9f9d0 | 480 | if (!event || !context || event->frozen) { |
f655a84d JG |
481 | ret = -1; |
482 | goto end; | |
483 | } | |
484 | ||
485 | field_type = bt_ctf_field_get_type(context); | |
09840de5 PP |
486 | if (bt_ctf_field_type_compare(field_type, |
487 | event->event_class->context)) { | |
f655a84d JG |
488 | ret = -1; |
489 | goto end; | |
490 | } | |
491 | ||
83509119 JG |
492 | bt_get(context); |
493 | bt_put(event->context_payload); | |
f655a84d JG |
494 | event->context_payload = context; |
495 | end: | |
83509119 | 496 | bt_put(field_type); |
f655a84d JG |
497 | return ret; |
498 | } | |
499 | ||
5fd2e9fd PP |
500 | struct bt_ctf_field *bt_ctf_event_get_stream_event_context( |
501 | struct bt_ctf_event *event) | |
502 | { | |
503 | struct bt_ctf_field *stream_event_context = NULL; | |
504 | ||
505 | if (!event || !event->stream_event_context) { | |
506 | goto end; | |
507 | } | |
508 | ||
509 | stream_event_context = event->stream_event_context; | |
510 | end: | |
511 | return bt_get(stream_event_context); | |
512 | } | |
513 | ||
514 | int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event, | |
515 | struct bt_ctf_field *stream_event_context) | |
516 | { | |
517 | int ret = 0; | |
518 | struct bt_ctf_field_type *field_type = NULL; | |
519 | struct bt_ctf_stream_class *stream_class = NULL; | |
520 | ||
4ce9f9d0 | 521 | if (!event || !stream_event_context || event->frozen) { |
5fd2e9fd PP |
522 | ret = -1; |
523 | goto end; | |
524 | } | |
525 | ||
526 | stream_class = bt_ctf_event_class_get_stream_class(event->event_class); | |
527 | /* | |
528 | * We should not have been able to create the event without associating | |
529 | * the event class to a stream class. | |
530 | */ | |
531 | assert(stream_class); | |
532 | ||
533 | field_type = bt_ctf_field_get_type(stream_event_context); | |
534 | if (bt_ctf_field_type_compare(field_type, | |
535 | stream_class->event_context_type)) { | |
536 | ret = -1; | |
537 | goto end; | |
538 | } | |
539 | ||
540 | bt_get(stream_event_context); | |
541 | BT_MOVE(event->stream_event_context, stream_event_context); | |
542 | end: | |
543 | BT_PUT(stream_class); | |
544 | bt_put(field_type); | |
545 | return ret; | |
546 | } | |
547 | ||
273b65be JG |
548 | void bt_ctf_event_get(struct bt_ctf_event *event) |
549 | { | |
83509119 | 550 | bt_get(event); |
273b65be JG |
551 | } |
552 | ||
553 | void bt_ctf_event_put(struct bt_ctf_event *event) | |
554 | { | |
83509119 | 555 | bt_put(event); |
273b65be JG |
556 | } |
557 | ||
83509119 | 558 | void bt_ctf_event_destroy(struct bt_object *obj) |
273b65be JG |
559 | { |
560 | struct bt_ctf_event *event; | |
561 | ||
83509119 | 562 | event = container_of(obj, struct bt_ctf_event, base); |
e6a8e8e4 JG |
563 | if (!event->base.parent) { |
564 | /* | |
565 | * Event was keeping a reference to its class since it shared no | |
566 | * common ancestor with it to guarantee they would both have the | |
567 | * same lifetime. | |
568 | */ | |
569 | bt_put(event->event_class); | |
570 | } | |
83509119 | 571 | bt_put(event->event_header); |
5fd2e9fd | 572 | bt_put(event->stream_event_context); |
83509119 JG |
573 | bt_put(event->context_payload); |
574 | bt_put(event->fields_payload); | |
5c3b707d | 575 | bt_put(event->packet); |
273b65be JG |
576 | g_free(event); |
577 | } | |
578 | ||
662e778c JG |
579 | static |
580 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) | |
581 | { | |
582 | int ret = 0; | |
583 | struct bt_ctf_field_type *field_type = NULL; | |
584 | ||
585 | if (!field) { | |
586 | ret = -1; | |
587 | goto end; | |
588 | } | |
589 | ||
590 | if (!bt_ctf_field_validate(field)) { | |
591 | /* Payload already set, skip! (not an error) */ | |
592 | goto end; | |
593 | } | |
594 | ||
595 | field_type = bt_ctf_field_get_type(field); | |
596 | assert(field_type); | |
597 | ||
9a19a512 PP |
598 | if (bt_ctf_field_type_get_type_id(field_type) != |
599 | BT_CTF_TYPE_ID_INTEGER) { | |
662e778c JG |
600 | /* Not an integer and the value is unset, error. */ |
601 | ret = -1; | |
602 | goto end; | |
603 | } | |
604 | ||
605 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
606 | ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value); | |
607 | if (ret) { | |
608 | /* Value is out of range, error. */ | |
609 | goto end; | |
610 | } | |
611 | } else { | |
612 | ret = bt_ctf_field_unsigned_integer_set_value(field, value); | |
613 | if (ret) { | |
614 | /* Value is out of range, error. */ | |
615 | goto end; | |
616 | } | |
617 | } | |
618 | end: | |
83509119 | 619 | bt_put(field_type); |
662e778c JG |
620 | return ret; |
621 | } | |
622 | ||
273b65be JG |
623 | BT_HIDDEN |
624 | int bt_ctf_event_validate(struct bt_ctf_event *event) | |
625 | { | |
626 | /* Make sure each field's payload has been set */ | |
627 | int ret; | |
5fd2e9fd | 628 | struct bt_ctf_stream_class *stream_class = NULL; |
273b65be JG |
629 | |
630 | assert(event); | |
662e778c JG |
631 | ret = bt_ctf_field_validate(event->event_header); |
632 | if (ret) { | |
633 | goto end; | |
634 | } | |
635 | ||
5fd2e9fd PP |
636 | stream_class = bt_ctf_event_class_get_stream_class(event->event_class); |
637 | /* | |
638 | * We should not have been able to create the event without associating | |
639 | * the event class to a stream class. | |
640 | */ | |
641 | assert(stream_class); | |
642 | if (stream_class->event_context_type) { | |
643 | ret = bt_ctf_field_validate(event->stream_event_context); | |
644 | if (ret) { | |
645 | goto end; | |
646 | } | |
647 | } | |
648 | ||
273b65be JG |
649 | ret = bt_ctf_field_validate(event->fields_payload); |
650 | if (ret) { | |
651 | goto end; | |
652 | } | |
653 | ||
654 | if (event->event_class->context) { | |
655 | ret = bt_ctf_field_validate(event->context_payload); | |
656 | } | |
657 | end: | |
5fd2e9fd | 658 | bt_put(stream_class); |
273b65be JG |
659 | return ret; |
660 | } | |
661 | ||
662 | BT_HIDDEN | |
663 | int bt_ctf_event_serialize(struct bt_ctf_event *event, | |
664 | struct ctf_stream_pos *pos) | |
665 | { | |
666 | int ret = 0; | |
667 | ||
668 | assert(event); | |
669 | assert(pos); | |
670 | if (event->context_payload) { | |
671 | ret = bt_ctf_field_serialize(event->context_payload, pos); | |
672 | if (ret) { | |
673 | goto end; | |
674 | } | |
675 | } | |
676 | ||
677 | if (event->fields_payload) { | |
678 | ret = bt_ctf_field_serialize(event->fields_payload, pos); | |
679 | if (ret) { | |
680 | goto end; | |
681 | } | |
682 | } | |
683 | end: | |
684 | return ret; | |
685 | } | |
686 | ||
662e778c JG |
687 | BT_HIDDEN |
688 | int bt_ctf_event_populate_event_header(struct bt_ctf_event *event) | |
689 | { | |
690 | int ret = 0; | |
691 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; | |
692 | ||
4ce9f9d0 | 693 | if (!event || event->frozen) { |
662e778c JG |
694 | ret = -1; |
695 | goto end; | |
696 | } | |
697 | ||
698 | id_field = bt_ctf_field_structure_get_field(event->event_header, "id"); | |
699 | if (id_field) { | |
700 | ret = set_integer_field_value(id_field, | |
b8248cc0 PP |
701 | (uint64_t) bt_ctf_event_class_get_id( |
702 | event->event_class)); | |
662e778c JG |
703 | if (ret) { |
704 | goto end; | |
705 | } | |
706 | } | |
707 | ||
708 | timestamp_field = bt_ctf_field_structure_get_field(event->event_header, | |
709 | "timestamp"); | |
710 | if (timestamp_field) { | |
9a220c32 JG |
711 | struct bt_ctf_field_type *timestamp_field_type = |
712 | bt_ctf_field_get_type(timestamp_field); | |
713 | struct bt_ctf_clock *mapped_clock; | |
714 | ||
715 | assert(timestamp_field_type); | |
716 | mapped_clock = bt_ctf_field_type_integer_get_mapped_clock( | |
717 | timestamp_field_type); | |
83509119 | 718 | bt_put(timestamp_field_type); |
9a220c32 | 719 | if (mapped_clock) { |
61cf588b | 720 | int64_t timestamp; |
9a220c32 | 721 | |
61cf588b | 722 | ret = bt_ctf_clock_get_time(mapped_clock, ×tamp); |
83509119 | 723 | bt_put(mapped_clock); |
61cf588b | 724 | if (ret) { |
9a220c32 JG |
725 | goto end; |
726 | } | |
727 | ||
728 | ret = set_integer_field_value(timestamp_field, | |
729 | timestamp); | |
730 | if (ret) { | |
731 | goto end; | |
732 | } | |
662e778c JG |
733 | } |
734 | } | |
735 | end: | |
83509119 JG |
736 | bt_put(id_field); |
737 | bt_put(timestamp_field); | |
662e778c JG |
738 | return ret; |
739 | } | |
123fbdec | 740 | |
9c4c8f6e PP |
741 | struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event) |
742 | { | |
743 | struct bt_ctf_event *copy = NULL; | |
744 | ||
745 | if (!event) { | |
746 | goto error; | |
747 | } | |
748 | ||
749 | copy = g_new0(struct bt_ctf_event, 1); | |
750 | if (!copy) { | |
751 | goto error; | |
752 | } | |
753 | ||
83509119 | 754 | bt_object_init(copy, bt_ctf_event_destroy); |
e6a8e8e4 | 755 | copy->event_class = bt_get(event->event_class); |
9c4c8f6e PP |
756 | |
757 | if (event->event_header) { | |
758 | copy->event_header = bt_ctf_field_copy(event->event_header); | |
9c4c8f6e PP |
759 | if (!copy->event_header) { |
760 | goto error; | |
761 | } | |
762 | } | |
763 | ||
5fd2e9fd PP |
764 | if (event->stream_event_context) { |
765 | copy->stream_event_context = | |
766 | bt_ctf_field_copy(event->stream_event_context); | |
767 | if (!copy->stream_event_context) { | |
768 | goto error; | |
769 | } | |
770 | } | |
771 | ||
9c4c8f6e PP |
772 | if (event->context_payload) { |
773 | copy->context_payload = bt_ctf_field_copy( | |
774 | event->context_payload); | |
9c4c8f6e PP |
775 | if (!copy->context_payload) { |
776 | goto error; | |
777 | } | |
778 | } | |
779 | ||
780 | if (event->fields_payload) { | |
781 | copy->fields_payload = bt_ctf_field_copy(event->fields_payload); | |
9c4c8f6e PP |
782 | if (!copy->fields_payload) { |
783 | goto error; | |
784 | } | |
785 | } | |
786 | ||
787 | return copy; | |
788 | ||
789 | error: | |
83509119 JG |
790 | BT_PUT(copy); |
791 | return copy; | |
9c4c8f6e | 792 | } |
5c3b707d PP |
793 | |
794 | int bt_ctf_event_set_packet(struct bt_ctf_event *event, | |
795 | struct bt_ctf_packet *packet) | |
796 | { | |
797 | struct bt_ctf_stream *stream = NULL; | |
798 | int ret = 0; | |
799 | ||
4ce9f9d0 | 800 | if (!event || !packet || event->frozen) { |
5c3b707d PP |
801 | ret = -1; |
802 | goto end; | |
803 | } | |
804 | ||
805 | /* | |
806 | * Make sure the new packet was created by this event's | |
807 | * stream, if it is set. | |
808 | */ | |
809 | stream = bt_ctf_event_get_stream(event); | |
810 | if (stream) { | |
811 | if (packet->stream != stream) { | |
812 | ret = -1; | |
813 | goto end; | |
814 | } | |
815 | } else { | |
816 | /* Set the event's parent to the packet's stream */ | |
817 | bt_object_set_parent(event, packet->stream); | |
818 | } | |
819 | ||
820 | bt_put(event->packet); | |
821 | event->packet = bt_get(packet); | |
822 | ||
823 | end: | |
824 | BT_PUT(stream); | |
825 | ||
826 | return ret; | |
827 | } | |
4ce9f9d0 PP |
828 | |
829 | BT_HIDDEN | |
830 | void bt_ctf_event_freeze(struct bt_ctf_event *event) | |
831 | { | |
832 | assert(event); | |
833 | bt_ctf_packet_freeze(event->packet); | |
834 | bt_ctf_field_freeze(event->event_header); | |
835 | bt_ctf_field_freeze(event->stream_event_context); | |
836 | bt_ctf_field_freeze(event->context_payload); | |
837 | bt_ctf_field_freeze(event->fields_payload); | |
838 | event->frozen = 1; | |
839 | } |