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