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