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