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