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 | 158 | event->clock_values = g_hash_table_new_full(g_direct_hash, |
1556a1af | 159 | g_direct_equal, bt_put, bt_put); |
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 | { | |
cacf3147 PP |
262 | struct bt_ctf_stream *stream = NULL; |
263 | ||
264 | if (!event) { | |
265 | goto end; | |
266 | } | |
267 | ||
268 | /* | |
269 | * If the event has a parent, then this is its (writer) stream. | |
270 | * If the event has no parent, then if it has a packet, this | |
271 | * is its (non-writer) stream. | |
272 | */ | |
273 | if (event->base.parent) { | |
274 | stream = (struct bt_ctf_stream *) bt_object_get_parent(event); | |
275 | } else { | |
276 | if (event->packet) { | |
277 | stream = bt_get(event->packet->stream); | |
278 | } | |
279 | } | |
280 | ||
281 | end: | |
282 | return stream; | |
8e5003bb JG |
283 | } |
284 | ||
273b65be JG |
285 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, |
286 | const char *name, | |
c5a9aa19 | 287 | struct bt_ctf_field *payload) |
273b65be JG |
288 | { |
289 | int ret = 0; | |
290 | ||
4ce9f9d0 | 291 | if (!event || !payload || event->frozen) { |
273b65be JG |
292 | ret = -1; |
293 | goto end; | |
294 | } | |
295 | ||
c5a9aa19 JG |
296 | if (name) { |
297 | ret = bt_ctf_field_structure_set_field(event->fields_payload, | |
298 | name, payload); | |
299 | } else { | |
300 | struct bt_ctf_field_type *payload_type; | |
301 | ||
302 | payload_type = bt_ctf_field_get_type(payload); | |
09840de5 PP |
303 | |
304 | if (bt_ctf_field_type_compare(payload_type, | |
305 | event->event_class->fields) == 0) { | |
83509119 JG |
306 | bt_put(event->fields_payload); |
307 | bt_get(payload); | |
c5a9aa19 JG |
308 | event->fields_payload = payload; |
309 | } else { | |
310 | ret = -1; | |
311 | } | |
312 | ||
83509119 | 313 | bt_put(payload_type); |
c5a9aa19 | 314 | } |
273b65be JG |
315 | end: |
316 | return ret; | |
317 | } | |
318 | ||
71362d53 PP |
319 | struct bt_ctf_field *bt_ctf_event_get_payload_field(struct bt_ctf_event *event) |
320 | { | |
321 | struct bt_ctf_field *payload = NULL; | |
322 | ||
323 | if (!event || !event->fields_payload) { | |
324 | goto end; | |
325 | } | |
326 | ||
327 | payload = event->fields_payload; | |
83509119 | 328 | bt_get(payload); |
71362d53 PP |
329 | end: |
330 | return payload; | |
331 | } | |
273b65be | 332 | |
e5e6eb3a JG |
333 | int bt_ctf_event_set_payload_field(struct bt_ctf_event *event, |
334 | struct bt_ctf_field *payload) | |
335 | { | |
336 | int ret = 0; | |
337 | struct bt_ctf_field_type *payload_type = NULL; | |
338 | ||
4ce9f9d0 | 339 | if (!event || !payload || event->frozen) { |
e5e6eb3a JG |
340 | ret = -1; |
341 | goto end; | |
342 | } | |
343 | ||
344 | payload_type = bt_ctf_field_get_type(payload); | |
345 | if (!payload_type) { | |
346 | ret = -1; | |
347 | goto end; | |
348 | } | |
349 | ||
9a19a512 PP |
350 | if (bt_ctf_field_type_get_type_id(payload_type) != |
351 | BT_CTF_TYPE_ID_STRUCT) { | |
e5e6eb3a JG |
352 | ret = -1; |
353 | goto end; | |
354 | } | |
355 | ||
83509119 JG |
356 | bt_get(payload); |
357 | bt_put(event->fields_payload); | |
e5e6eb3a JG |
358 | event->fields_payload = payload; |
359 | ||
360 | end: | |
83509119 | 361 | bt_put(payload_type); |
e5e6eb3a JG |
362 | return ret; |
363 | } | |
364 | ||
273b65be JG |
365 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, |
366 | const char *name) | |
367 | { | |
368 | struct bt_ctf_field *field = NULL; | |
369 | ||
c5a9aa19 | 370 | if (!event) { |
273b65be JG |
371 | goto end; |
372 | } | |
373 | ||
c5a9aa19 JG |
374 | if (name) { |
375 | field = bt_ctf_field_structure_get_field(event->fields_payload, | |
376 | name); | |
377 | } else { | |
378 | field = event->fields_payload; | |
83509119 | 379 | bt_get(field); |
c5a9aa19 | 380 | } |
273b65be JG |
381 | end: |
382 | return field; | |
383 | } | |
384 | ||
2f100782 | 385 | struct bt_ctf_field *bt_ctf_event_get_payload_by_index( |
074ee56d | 386 | struct bt_ctf_event *event, int index) |
2f100782 JG |
387 | { |
388 | struct bt_ctf_field *field = NULL; | |
389 | ||
074ee56d | 390 | if (!event || index < 0) { |
2f100782 JG |
391 | goto end; |
392 | } | |
393 | ||
394 | field = bt_ctf_field_structure_get_field_by_index(event->fields_payload, | |
395 | index); | |
396 | end: | |
397 | return field; | |
398 | } | |
399 | ||
286a2840 | 400 | struct bt_ctf_field *bt_ctf_event_get_header( |
662e778c JG |
401 | struct bt_ctf_event *event) |
402 | { | |
403 | struct bt_ctf_field *header = NULL; | |
404 | ||
405 | if (!event || !event->event_header) { | |
406 | goto end; | |
407 | } | |
408 | ||
409 | header = event->event_header; | |
83509119 | 410 | bt_get(header); |
662e778c JG |
411 | end: |
412 | return header; | |
413 | } | |
414 | ||
286a2840 | 415 | int bt_ctf_event_set_header(struct bt_ctf_event *event, |
662e778c JG |
416 | struct bt_ctf_field *header) |
417 | { | |
418 | int ret = 0; | |
419 | struct bt_ctf_field_type *field_type = NULL; | |
e6a8e8e4 | 420 | struct bt_ctf_stream_class *stream_class = NULL; |
662e778c | 421 | |
4ce9f9d0 | 422 | if (!event || !header || event->frozen) { |
662e778c JG |
423 | ret = -1; |
424 | goto end; | |
425 | } | |
426 | ||
e6a8e8e4 JG |
427 | stream_class = (struct bt_ctf_stream_class *) bt_object_get_parent( |
428 | event->event_class); | |
662e778c JG |
429 | /* |
430 | * Ensure the provided header's type matches the one registered to the | |
431 | * stream class. | |
432 | */ | |
433 | field_type = bt_ctf_field_get_type(header); | |
09840de5 PP |
434 | if (bt_ctf_field_type_compare(field_type, |
435 | stream_class->event_header_type)) { | |
662e778c JG |
436 | ret = -1; |
437 | goto end; | |
438 | } | |
439 | ||
83509119 JG |
440 | bt_get(header); |
441 | bt_put(event->event_header); | |
662e778c JG |
442 | event->event_header = header; |
443 | end: | |
e6a8e8e4 | 444 | bt_put(stream_class); |
83509119 | 445 | bt_put(field_type); |
662e778c JG |
446 | return ret; |
447 | } | |
448 | ||
f655a84d JG |
449 | struct bt_ctf_field *bt_ctf_event_get_event_context( |
450 | struct bt_ctf_event *event) | |
451 | { | |
452 | struct bt_ctf_field *context = NULL; | |
453 | ||
454 | if (!event || !event->context_payload) { | |
455 | goto end; | |
456 | } | |
457 | ||
458 | context = event->context_payload; | |
83509119 | 459 | bt_get(context); |
f655a84d JG |
460 | end: |
461 | return context; | |
462 | } | |
463 | ||
464 | int bt_ctf_event_set_event_context(struct bt_ctf_event *event, | |
465 | struct bt_ctf_field *context) | |
466 | { | |
467 | int ret = 0; | |
468 | struct bt_ctf_field_type *field_type = NULL; | |
469 | ||
4ce9f9d0 | 470 | if (!event || !context || event->frozen) { |
f655a84d JG |
471 | ret = -1; |
472 | goto end; | |
473 | } | |
474 | ||
475 | field_type = bt_ctf_field_get_type(context); | |
09840de5 PP |
476 | if (bt_ctf_field_type_compare(field_type, |
477 | event->event_class->context)) { | |
f655a84d JG |
478 | ret = -1; |
479 | goto end; | |
480 | } | |
481 | ||
83509119 JG |
482 | bt_get(context); |
483 | bt_put(event->context_payload); | |
f655a84d JG |
484 | event->context_payload = context; |
485 | end: | |
83509119 | 486 | bt_put(field_type); |
f655a84d JG |
487 | return ret; |
488 | } | |
489 | ||
5fd2e9fd PP |
490 | struct bt_ctf_field *bt_ctf_event_get_stream_event_context( |
491 | struct bt_ctf_event *event) | |
492 | { | |
493 | struct bt_ctf_field *stream_event_context = NULL; | |
494 | ||
495 | if (!event || !event->stream_event_context) { | |
496 | goto end; | |
497 | } | |
498 | ||
499 | stream_event_context = event->stream_event_context; | |
500 | end: | |
501 | return bt_get(stream_event_context); | |
502 | } | |
503 | ||
504 | int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event, | |
505 | struct bt_ctf_field *stream_event_context) | |
506 | { | |
507 | int ret = 0; | |
508 | struct bt_ctf_field_type *field_type = NULL; | |
509 | struct bt_ctf_stream_class *stream_class = NULL; | |
510 | ||
4ce9f9d0 | 511 | if (!event || !stream_event_context || event->frozen) { |
5fd2e9fd PP |
512 | ret = -1; |
513 | goto end; | |
514 | } | |
515 | ||
516 | stream_class = bt_ctf_event_class_get_stream_class(event->event_class); | |
517 | /* | |
518 | * We should not have been able to create the event without associating | |
519 | * the event class to a stream class. | |
520 | */ | |
521 | assert(stream_class); | |
522 | ||
523 | field_type = bt_ctf_field_get_type(stream_event_context); | |
524 | if (bt_ctf_field_type_compare(field_type, | |
525 | stream_class->event_context_type)) { | |
526 | ret = -1; | |
527 | goto end; | |
528 | } | |
529 | ||
530 | bt_get(stream_event_context); | |
531 | BT_MOVE(event->stream_event_context, stream_event_context); | |
532 | end: | |
533 | BT_PUT(stream_class); | |
534 | bt_put(field_type); | |
535 | return ret; | |
536 | } | |
537 | ||
273b65be JG |
538 | void bt_ctf_event_get(struct bt_ctf_event *event) |
539 | { | |
83509119 | 540 | bt_get(event); |
273b65be JG |
541 | } |
542 | ||
543 | void bt_ctf_event_put(struct bt_ctf_event *event) | |
544 | { | |
83509119 | 545 | bt_put(event); |
273b65be JG |
546 | } |
547 | ||
83509119 | 548 | void bt_ctf_event_destroy(struct bt_object *obj) |
273b65be JG |
549 | { |
550 | struct bt_ctf_event *event; | |
551 | ||
83509119 | 552 | event = container_of(obj, struct bt_ctf_event, base); |
e6a8e8e4 JG |
553 | if (!event->base.parent) { |
554 | /* | |
555 | * Event was keeping a reference to its class since it shared no | |
556 | * common ancestor with it to guarantee they would both have the | |
557 | * same lifetime. | |
558 | */ | |
559 | bt_put(event->event_class); | |
560 | } | |
41ac640a | 561 | g_hash_table_destroy(event->clock_values); |
83509119 | 562 | bt_put(event->event_header); |
5fd2e9fd | 563 | bt_put(event->stream_event_context); |
83509119 JG |
564 | bt_put(event->context_payload); |
565 | bt_put(event->fields_payload); | |
5c3b707d | 566 | bt_put(event->packet); |
273b65be JG |
567 | g_free(event); |
568 | } | |
569 | ||
1556a1af JG |
570 | struct bt_ctf_clock_value *bt_ctf_event_get_clock_value( |
571 | struct bt_ctf_event *event, struct bt_ctf_clock *clock) | |
78586d8a | 572 | { |
1556a1af | 573 | struct bt_ctf_clock_value *clock_value = NULL; |
78586d8a JG |
574 | |
575 | if (!event || !clock) { | |
576 | goto end; | |
577 | } | |
578 | ||
579 | clock_value = g_hash_table_lookup(event->clock_values, clock); | |
580 | if (!clock_value) { | |
581 | goto end; | |
582 | } | |
583 | ||
1556a1af JG |
584 | bt_get(clock_value); |
585 | end: | |
586 | return clock_value; | |
587 | } | |
588 | ||
589 | int bt_ctf_event_set_clock_value(struct bt_ctf_event *event, | |
590 | struct bt_ctf_clock *clock, struct bt_ctf_clock_value *value) | |
591 | { | |
592 | int ret = 0; | |
593 | ||
594 | if (!event || !clock || !value || event->frozen) { | |
595 | ret = -1; | |
596 | goto end; | |
597 | } | |
78586d8a | 598 | |
1556a1af | 599 | g_hash_table_insert(event->clock_values, bt_get(clock), bt_get(value)); |
78586d8a JG |
600 | end: |
601 | return ret; | |
602 | } | |
603 | ||
662e778c JG |
604 | static |
605 | int set_integer_field_value(struct bt_ctf_field* field, uint64_t value) | |
606 | { | |
607 | int ret = 0; | |
608 | struct bt_ctf_field_type *field_type = NULL; | |
609 | ||
610 | if (!field) { | |
611 | ret = -1; | |
612 | goto end; | |
613 | } | |
614 | ||
615 | if (!bt_ctf_field_validate(field)) { | |
616 | /* Payload already set, skip! (not an error) */ | |
617 | goto end; | |
618 | } | |
619 | ||
620 | field_type = bt_ctf_field_get_type(field); | |
621 | assert(field_type); | |
622 | ||
9a19a512 PP |
623 | if (bt_ctf_field_type_get_type_id(field_type) != |
624 | BT_CTF_TYPE_ID_INTEGER) { | |
662e778c JG |
625 | /* Not an integer and the value is unset, error. */ |
626 | ret = -1; | |
627 | goto end; | |
628 | } | |
629 | ||
630 | if (bt_ctf_field_type_integer_get_signed(field_type)) { | |
631 | ret = bt_ctf_field_signed_integer_set_value(field, (int64_t) value); | |
632 | if (ret) { | |
633 | /* Value is out of range, error. */ | |
634 | goto end; | |
635 | } | |
636 | } else { | |
637 | ret = bt_ctf_field_unsigned_integer_set_value(field, value); | |
638 | if (ret) { | |
639 | /* Value is out of range, error. */ | |
640 | goto end; | |
641 | } | |
642 | } | |
643 | end: | |
83509119 | 644 | bt_put(field_type); |
662e778c JG |
645 | return ret; |
646 | } | |
647 | ||
273b65be JG |
648 | BT_HIDDEN |
649 | int bt_ctf_event_validate(struct bt_ctf_event *event) | |
650 | { | |
651 | /* Make sure each field's payload has been set */ | |
652 | int ret; | |
5fd2e9fd | 653 | struct bt_ctf_stream_class *stream_class = NULL; |
273b65be JG |
654 | |
655 | assert(event); | |
662e778c JG |
656 | ret = bt_ctf_field_validate(event->event_header); |
657 | if (ret) { | |
658 | goto end; | |
659 | } | |
660 | ||
5fd2e9fd PP |
661 | stream_class = bt_ctf_event_class_get_stream_class(event->event_class); |
662 | /* | |
663 | * We should not have been able to create the event without associating | |
664 | * the event class to a stream class. | |
665 | */ | |
666 | assert(stream_class); | |
667 | if (stream_class->event_context_type) { | |
668 | ret = bt_ctf_field_validate(event->stream_event_context); | |
669 | if (ret) { | |
670 | goto end; | |
671 | } | |
672 | } | |
673 | ||
273b65be JG |
674 | ret = bt_ctf_field_validate(event->fields_payload); |
675 | if (ret) { | |
676 | goto end; | |
677 | } | |
678 | ||
679 | if (event->event_class->context) { | |
680 | ret = bt_ctf_field_validate(event->context_payload); | |
681 | } | |
682 | end: | |
5fd2e9fd | 683 | bt_put(stream_class); |
273b65be JG |
684 | return ret; |
685 | } | |
686 | ||
687 | BT_HIDDEN | |
688 | int bt_ctf_event_serialize(struct bt_ctf_event *event, | |
689 | struct ctf_stream_pos *pos) | |
690 | { | |
691 | int ret = 0; | |
692 | ||
693 | assert(event); | |
694 | assert(pos); | |
695 | if (event->context_payload) { | |
696 | ret = bt_ctf_field_serialize(event->context_payload, pos); | |
697 | if (ret) { | |
698 | goto end; | |
699 | } | |
700 | } | |
701 | ||
702 | if (event->fields_payload) { | |
703 | ret = bt_ctf_field_serialize(event->fields_payload, pos); | |
704 | if (ret) { | |
705 | goto end; | |
706 | } | |
707 | } | |
708 | end: | |
709 | return ret; | |
710 | } | |
711 | ||
662e778c JG |
712 | BT_HIDDEN |
713 | int bt_ctf_event_populate_event_header(struct bt_ctf_event *event) | |
714 | { | |
715 | int ret = 0; | |
716 | struct bt_ctf_field *id_field = NULL, *timestamp_field = NULL; | |
717 | ||
4ce9f9d0 | 718 | if (!event || event->frozen) { |
662e778c JG |
719 | ret = -1; |
720 | goto end; | |
721 | } | |
722 | ||
723 | id_field = bt_ctf_field_structure_get_field(event->event_header, "id"); | |
724 | if (id_field) { | |
725 | ret = set_integer_field_value(id_field, | |
b8248cc0 PP |
726 | (uint64_t) bt_ctf_event_class_get_id( |
727 | event->event_class)); | |
662e778c JG |
728 | if (ret) { |
729 | goto end; | |
730 | } | |
731 | } | |
732 | ||
733 | timestamp_field = bt_ctf_field_structure_get_field(event->event_header, | |
734 | "timestamp"); | |
735 | if (timestamp_field) { | |
9a220c32 JG |
736 | struct bt_ctf_field_type *timestamp_field_type = |
737 | bt_ctf_field_get_type(timestamp_field); | |
738 | struct bt_ctf_clock *mapped_clock; | |
739 | ||
740 | assert(timestamp_field_type); | |
741 | mapped_clock = bt_ctf_field_type_integer_get_mapped_clock( | |
742 | timestamp_field_type); | |
83509119 | 743 | bt_put(timestamp_field_type); |
9a220c32 | 744 | if (mapped_clock) { |
61ec14e6 | 745 | int64_t timestamp = 0; |
9a220c32 | 746 | |
61ec14e6 JG |
747 | // FIXME - Clock refactoring |
748 | /* | |
61cf588b | 749 | ret = bt_ctf_clock_get_time(mapped_clock, ×tamp); |
83509119 | 750 | bt_put(mapped_clock); |
61cf588b | 751 | if (ret) { |
9a220c32 JG |
752 | goto end; |
753 | } | |
61ec14e6 | 754 | */ |
9a220c32 JG |
755 | |
756 | ret = set_integer_field_value(timestamp_field, | |
757 | timestamp); | |
758 | if (ret) { | |
759 | goto end; | |
760 | } | |
662e778c JG |
761 | } |
762 | } | |
763 | end: | |
83509119 JG |
764 | bt_put(id_field); |
765 | bt_put(timestamp_field); | |
662e778c JG |
766 | return ret; |
767 | } | |
123fbdec | 768 | |
5c0f40f4 JG |
769 | struct bt_ctf_packet *bt_ctf_event_get_packet(struct bt_ctf_event *event) |
770 | { | |
771 | struct bt_ctf_packet *packet = NULL; | |
772 | ||
773 | if (!event || !event->packet) { | |
774 | goto end; | |
775 | } | |
776 | ||
777 | packet = bt_get(event->packet); | |
778 | end: | |
779 | return packet; | |
780 | } | |
781 | ||
5c3b707d PP |
782 | int bt_ctf_event_set_packet(struct bt_ctf_event *event, |
783 | struct bt_ctf_packet *packet) | |
784 | { | |
b2b635e9 PP |
785 | struct bt_ctf_stream_class *event_stream_class = NULL; |
786 | struct bt_ctf_stream_class *packet_stream_class = NULL; | |
5c3b707d PP |
787 | struct bt_ctf_stream *stream = NULL; |
788 | int ret = 0; | |
789 | ||
4ce9f9d0 | 790 | if (!event || !packet || event->frozen) { |
5c3b707d PP |
791 | ret = -1; |
792 | goto end; | |
793 | } | |
794 | ||
795 | /* | |
796 | * Make sure the new packet was created by this event's | |
797 | * stream, if it is set. | |
798 | */ | |
799 | stream = bt_ctf_event_get_stream(event); | |
800 | if (stream) { | |
801 | if (packet->stream != stream) { | |
802 | ret = -1; | |
803 | goto end; | |
804 | } | |
805 | } else { | |
b2b635e9 PP |
806 | event_stream_class = |
807 | bt_ctf_event_class_get_stream_class(event->event_class); | |
808 | packet_stream_class = | |
809 | bt_ctf_stream_get_class(packet->stream); | |
810 | ||
811 | assert(event_stream_class); | |
812 | assert(packet_stream_class); | |
813 | ||
814 | if (event_stream_class != packet_stream_class) { | |
815 | ret = -1; | |
816 | goto end; | |
817 | } | |
5c3b707d PP |
818 | } |
819 | ||
cacf3147 PP |
820 | bt_get(packet); |
821 | BT_MOVE(event->packet, packet); | |
5c3b707d PP |
822 | |
823 | end: | |
824 | BT_PUT(stream); | |
b2b635e9 PP |
825 | BT_PUT(event_stream_class); |
826 | BT_PUT(packet_stream_class); | |
5c3b707d PP |
827 | |
828 | return ret; | |
829 | } | |
4ce9f9d0 PP |
830 | |
831 | BT_HIDDEN | |
832 | void bt_ctf_event_freeze(struct bt_ctf_event *event) | |
833 | { | |
834 | assert(event); | |
835 | bt_ctf_packet_freeze(event->packet); | |
836 | bt_ctf_field_freeze(event->event_header); | |
837 | bt_ctf_field_freeze(event->stream_event_context); | |
838 | bt_ctf_field_freeze(event->context_payload); | |
839 | bt_ctf_field_freeze(event->fields_payload); | |
840 | event->frozen = 1; | |
841 | } |