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 | ||
29 | #include <babeltrace/ctf-writer/event.h> | |
30 | #include <babeltrace/ctf-writer/event-types.h> | |
31 | #include <babeltrace/ctf-writer/event-fields.h> | |
adc315b8 JG |
32 | #include <babeltrace/ctf-ir/event-fields-internal.h> |
33 | #include <babeltrace/ctf-ir/event-types-internal.h> | |
34 | #include <babeltrace/ctf-ir/event-internal.h> | |
2f100782 | 35 | #include <babeltrace/ctf-ir/stream-class.h> |
bc37ae52 | 36 | #include <babeltrace/ctf-ir/trace-internal.h> |
273b65be JG |
37 | #include <babeltrace/compiler.h> |
38 | ||
39 | static | |
40 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref); | |
41 | static | |
42 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref); | |
43 | ||
44 | struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name) | |
45 | { | |
46 | struct bt_ctf_event_class *event_class = NULL; | |
47 | ||
48 | if (validate_identifier(name)) { | |
49 | goto end; | |
50 | } | |
51 | ||
52 | event_class = g_new0(struct bt_ctf_event_class, 1); | |
53 | if (!event_class) { | |
54 | goto end; | |
55 | } | |
56 | ||
57 | bt_ctf_ref_init(&event_class->ref_count); | |
58 | event_class->name = g_quark_from_string(name); | |
59 | end: | |
60 | return event_class; | |
61 | } | |
62 | ||
2f100782 JG |
63 | const char *bt_ctf_event_class_get_name(struct bt_ctf_event_class *event_class) |
64 | { | |
65 | const char *name = NULL; | |
66 | ||
67 | if (!event_class) { | |
68 | goto end; | |
69 | } | |
70 | ||
71 | name = g_quark_to_string(event_class->name); | |
72 | end: | |
73 | return name; | |
74 | } | |
75 | ||
76 | int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class) | |
77 | { | |
78 | int64_t ret; | |
79 | ||
80 | if (!event_class || !event_class->id_set) { | |
81 | ret = -1; | |
82 | goto end; | |
83 | } | |
84 | ||
85 | ret = (int64_t) event_class->id; | |
86 | end: | |
87 | return ret; | |
88 | } | |
89 | ||
90 | int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class, | |
91 | uint32_t id) | |
92 | { | |
93 | int ret = 0; | |
94 | ||
95 | if (!event_class) { | |
96 | ret = -1; | |
97 | goto end; | |
98 | } | |
99 | ||
100 | if (event_class->stream_class) { | |
101 | /* | |
102 | * We don't allow changing the id if the event class has already | |
103 | * been added to a stream class. | |
104 | */ | |
105 | ret = -1; | |
106 | goto end; | |
107 | } | |
108 | ||
109 | event_class->id = id; | |
110 | event_class->id_set = 1; | |
111 | end: | |
112 | return ret; | |
113 | } | |
114 | ||
115 | struct bt_ctf_stream_class *bt_ctf_event_class_get_stream_class( | |
116 | struct bt_ctf_event_class *event_class) | |
117 | { | |
118 | struct bt_ctf_stream_class *stream_class = NULL; | |
119 | ||
120 | if (!event_class) { | |
121 | goto end; | |
122 | } | |
123 | ||
124 | stream_class = event_class->stream_class; | |
125 | bt_ctf_stream_class_get(stream_class); | |
126 | end: | |
127 | return stream_class; | |
128 | } | |
129 | ||
273b65be JG |
130 | int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class, |
131 | struct bt_ctf_field_type *type, | |
132 | const char *name) | |
133 | { | |
134 | int ret = 0; | |
135 | ||
136 | if (!event_class || !type || validate_identifier(name) || | |
137 | event_class->frozen) { | |
138 | ret = -1; | |
139 | goto end; | |
140 | } | |
141 | ||
142 | if (!event_class->fields) { | |
143 | event_class->fields = bt_ctf_field_type_structure_create(); | |
144 | if (!event_class->fields) { | |
145 | ret = -1; | |
146 | goto end; | |
147 | } | |
148 | } | |
149 | ||
150 | ret = bt_ctf_field_type_structure_add_field(event_class->fields, | |
151 | type, name); | |
152 | end: | |
153 | return ret; | |
154 | } | |
155 | ||
074ee56d | 156 | int bt_ctf_event_class_get_field_count( |
2f100782 JG |
157 | struct bt_ctf_event_class *event_class) |
158 | { | |
074ee56d | 159 | int ret; |
2f100782 JG |
160 | |
161 | if (!event_class) { | |
162 | ret = -1; | |
163 | goto end; | |
164 | } | |
165 | ||
166 | ret = bt_ctf_field_type_structure_get_field_count(event_class->fields); | |
167 | end: | |
168 | return ret; | |
169 | } | |
170 | ||
171 | int bt_ctf_event_class_get_field(struct bt_ctf_event_class *event_class, | |
172 | const char **field_name, struct bt_ctf_field_type **field_type, | |
074ee56d | 173 | int index) |
2f100782 JG |
174 | { |
175 | int ret; | |
176 | ||
074ee56d | 177 | if (!event_class || index < 0) { |
2f100782 JG |
178 | ret = -1; |
179 | goto end; | |
180 | } | |
181 | ||
182 | ret = bt_ctf_field_type_structure_get_field(event_class->fields, | |
183 | field_name, field_type, index); | |
184 | end: | |
185 | return ret; | |
186 | } | |
187 | ||
188 | struct bt_ctf_field_type *bt_ctf_event_class_get_field_by_name( | |
189 | struct bt_ctf_event_class *event_class, const char *name) | |
190 | { | |
191 | GQuark name_quark; | |
192 | struct bt_ctf_field_type *field_type = NULL; | |
193 | ||
194 | if (!event_class || !name) { | |
195 | goto end; | |
196 | } | |
197 | ||
198 | name_quark = g_quark_try_string(name); | |
199 | if (!name_quark) { | |
200 | goto end; | |
201 | } | |
202 | ||
203 | /* | |
204 | * No need to increment field_type's reference count since getting it | |
205 | * from the structure already does. | |
206 | */ | |
207 | field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
208 | event_class->fields, name); | |
209 | end: | |
210 | return field_type; | |
211 | } | |
212 | ||
f655a84d JG |
213 | struct bt_ctf_field_type *bt_ctf_event_class_get_context_type( |
214 | struct bt_ctf_event_class *event_class) | |
215 | { | |
216 | struct bt_ctf_field_type *context_type = NULL; | |
217 | ||
218 | if (!event_class || !event_class->context) { | |
219 | goto end; | |
220 | } | |
221 | ||
222 | bt_ctf_field_type_get(event_class->context); | |
223 | context_type = event_class->context; | |
224 | end: | |
225 | return context_type; | |
226 | } | |
227 | ||
228 | int bt_ctf_event_class_set_context_type( | |
229 | struct bt_ctf_event_class *event_class, | |
230 | struct bt_ctf_field_type *context) | |
231 | { | |
232 | int ret = 0; | |
233 | ||
234 | if (!event_class || !context || event_class->frozen) { | |
235 | ret = -1; | |
236 | goto end; | |
237 | } | |
238 | ||
239 | if (bt_ctf_field_type_get_type_id(context) != CTF_TYPE_STRUCT) { | |
240 | ret = -1; | |
241 | goto end; | |
242 | } | |
243 | ||
244 | bt_ctf_field_type_get(context); | |
245 | bt_ctf_field_type_put(event_class->context); | |
246 | event_class->context = context; | |
247 | end: | |
248 | return ret; | |
249 | ||
250 | } | |
251 | ||
273b65be JG |
252 | void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class) |
253 | { | |
254 | if (!event_class) { | |
255 | return; | |
256 | } | |
257 | ||
258 | bt_ctf_ref_get(&event_class->ref_count); | |
259 | } | |
260 | ||
261 | void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class) | |
262 | { | |
263 | if (!event_class) { | |
264 | return; | |
265 | } | |
266 | ||
267 | bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy); | |
268 | } | |
269 | ||
270 | struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class) | |
271 | { | |
272 | struct bt_ctf_event *event = NULL; | |
273 | ||
274 | if (!event_class) { | |
275 | goto end; | |
276 | } | |
277 | ||
278 | event = g_new0(struct bt_ctf_event, 1); | |
279 | if (!event) { | |
280 | goto end; | |
281 | } | |
282 | ||
283 | bt_ctf_ref_init(&event->ref_count); | |
284 | bt_ctf_event_class_get(event_class); | |
285 | bt_ctf_event_class_freeze(event_class); | |
286 | event->event_class = event_class; | |
f655a84d JG |
287 | if (event_class->context) { |
288 | event->context_payload = bt_ctf_field_create( | |
289 | event_class->context); | |
290 | } | |
273b65be JG |
291 | event->fields_payload = bt_ctf_field_create(event_class->fields); |
292 | end: | |
293 | return event; | |
294 | } | |
295 | ||
2f100782 JG |
296 | struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event) |
297 | { | |
298 | struct bt_ctf_event_class *event_class = NULL; | |
299 | ||
300 | if (!event) { | |
301 | goto end; | |
302 | } | |
303 | ||
304 | event_class = event->event_class; | |
305 | bt_ctf_event_class_get(event_class); | |
306 | end: | |
307 | return event_class; | |
308 | } | |
309 | ||
310 | struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event) | |
311 | { | |
312 | struct bt_ctf_clock *clock = NULL; | |
313 | struct bt_ctf_event_class *event_class; | |
314 | struct bt_ctf_stream_class *stream_class; | |
315 | ||
316 | if (!event) { | |
317 | goto end; | |
318 | } | |
319 | ||
320 | event_class = bt_ctf_event_get_class(event); | |
321 | if (!event_class) { | |
322 | goto end; | |
323 | } | |
324 | ||
325 | stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
326 | if (!stream_class) { | |
327 | goto error_put_event_class; | |
328 | } | |
329 | ||
330 | clock = bt_ctf_stream_class_get_clock(stream_class); | |
331 | if (!clock) { | |
332 | goto error_put_stream_class; | |
333 | } | |
334 | ||
335 | error_put_stream_class: | |
336 | bt_ctf_stream_class_put(stream_class); | |
337 | error_put_event_class: | |
338 | bt_ctf_event_class_put(event_class); | |
339 | end: | |
340 | return clock; | |
341 | } | |
342 | ||
273b65be JG |
343 | int bt_ctf_event_set_payload(struct bt_ctf_event *event, |
344 | const char *name, | |
345 | struct bt_ctf_field *value) | |
346 | { | |
347 | int ret = 0; | |
348 | ||
349 | if (!event || !value || validate_identifier(name)) { | |
350 | ret = -1; | |
351 | goto end; | |
352 | } | |
353 | ||
354 | ret = bt_ctf_field_structure_set_field(event->fields_payload, | |
355 | name, value); | |
356 | end: | |
357 | return ret; | |
358 | } | |
359 | ||
360 | ||
361 | struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event, | |
362 | const char *name) | |
363 | { | |
364 | struct bt_ctf_field *field = NULL; | |
365 | ||
366 | if (!event || !name) { | |
367 | goto end; | |
368 | } | |
369 | ||
370 | field = bt_ctf_field_structure_get_field(event->fields_payload, name); | |
371 | end: | |
372 | return field; | |
373 | } | |
374 | ||
2f100782 | 375 | struct bt_ctf_field *bt_ctf_event_get_payload_by_index( |
074ee56d | 376 | struct bt_ctf_event *event, int index) |
2f100782 JG |
377 | { |
378 | struct bt_ctf_field *field = NULL; | |
379 | ||
074ee56d | 380 | if (!event || index < 0) { |
2f100782 JG |
381 | goto end; |
382 | } | |
383 | ||
384 | field = bt_ctf_field_structure_get_field_by_index(event->fields_payload, | |
385 | index); | |
386 | end: | |
387 | return field; | |
388 | } | |
389 | ||
f655a84d JG |
390 | struct bt_ctf_field *bt_ctf_event_get_event_context( |
391 | struct bt_ctf_event *event) | |
392 | { | |
393 | struct bt_ctf_field *context = NULL; | |
394 | ||
395 | if (!event || !event->context_payload) { | |
396 | goto end; | |
397 | } | |
398 | ||
399 | context = event->context_payload; | |
400 | bt_ctf_field_get(context); | |
401 | end: | |
402 | return context; | |
403 | } | |
404 | ||
405 | int bt_ctf_event_set_event_context(struct bt_ctf_event *event, | |
406 | struct bt_ctf_field *context) | |
407 | { | |
408 | int ret = 0; | |
409 | struct bt_ctf_field_type *field_type = NULL; | |
410 | ||
411 | if (!event || !context) { | |
412 | ret = -1; | |
413 | goto end; | |
414 | } | |
415 | ||
416 | field_type = bt_ctf_field_get_type(context); | |
417 | if (field_type != event->event_class->context) { | |
418 | ret = -1; | |
419 | goto end; | |
420 | } | |
421 | ||
422 | bt_ctf_field_get(context); | |
423 | bt_ctf_field_put(event->context_payload); | |
424 | event->context_payload = context; | |
425 | end: | |
426 | if (field_type) { | |
427 | bt_ctf_field_type_put(field_type); | |
428 | } | |
429 | return ret; | |
430 | } | |
431 | ||
273b65be JG |
432 | void bt_ctf_event_get(struct bt_ctf_event *event) |
433 | { | |
434 | if (!event) { | |
435 | return; | |
436 | } | |
437 | ||
438 | bt_ctf_ref_get(&event->ref_count); | |
439 | } | |
440 | ||
441 | void bt_ctf_event_put(struct bt_ctf_event *event) | |
442 | { | |
443 | if (!event) { | |
444 | return; | |
445 | } | |
446 | ||
447 | bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy); | |
448 | } | |
449 | ||
450 | static | |
451 | void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref) | |
452 | { | |
453 | struct bt_ctf_event_class *event_class; | |
454 | ||
455 | if (!ref) { | |
456 | return; | |
457 | } | |
458 | ||
2f100782 JG |
459 | /* |
460 | * Don't call put() on the stream class. See comment in | |
461 | * bt_ctf_event_class_set_stream_class for explanation. | |
462 | */ | |
273b65be | 463 | event_class = container_of(ref, struct bt_ctf_event_class, ref_count); |
f655a84d JG |
464 | if (event_class->context) { |
465 | bt_ctf_field_type_put(event_class->context); | |
466 | } | |
467 | if (event_class->fields) { | |
468 | bt_ctf_field_type_put(event_class->fields); | |
469 | } | |
273b65be JG |
470 | g_free(event_class); |
471 | } | |
472 | ||
473 | static | |
474 | void bt_ctf_event_destroy(struct bt_ctf_ref *ref) | |
475 | { | |
476 | struct bt_ctf_event *event; | |
477 | ||
478 | if (!ref) { | |
479 | return; | |
480 | } | |
481 | ||
482 | event = container_of(ref, struct bt_ctf_event, | |
483 | ref_count); | |
f655a84d JG |
484 | if (event->event_class) { |
485 | bt_ctf_event_class_put(event->event_class); | |
486 | } | |
487 | if (event->context_payload) { | |
488 | bt_ctf_field_put(event->context_payload); | |
489 | } | |
490 | if (event->fields_payload) { | |
491 | bt_ctf_field_put(event->fields_payload); | |
492 | } | |
273b65be JG |
493 | g_free(event); |
494 | } | |
495 | ||
496 | BT_HIDDEN | |
497 | void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class) | |
498 | { | |
499 | assert(event_class); | |
500 | event_class->frozen = 1; | |
501 | bt_ctf_field_type_freeze(event_class->context); | |
502 | bt_ctf_field_type_freeze(event_class->fields); | |
503 | } | |
504 | ||
505 | BT_HIDDEN | |
2f100782 JG |
506 | int bt_ctf_event_class_set_stream_class(struct bt_ctf_event_class *event_class, |
507 | struct bt_ctf_stream_class *stream_class) | |
273b65be JG |
508 | { |
509 | int ret = 0; | |
510 | ||
2f100782 | 511 | if (!event_class) { |
273b65be JG |
512 | ret = -1; |
513 | goto end; | |
514 | } | |
515 | ||
2f100782 JG |
516 | /* Allow a NULL stream_class to unset the current stream_class */ |
517 | if (stream_class && event_class->stream_class) { | |
273b65be JG |
518 | ret = -1; |
519 | goto end; | |
520 | } | |
521 | ||
2f100782 JG |
522 | event_class->stream_class = stream_class; |
523 | /* | |
524 | * We don't get() the stream_class since doing so would introduce | |
525 | * a circular ownership between event classes and stream classes. | |
526 | * | |
527 | * A stream class will always unset itself from its events before | |
528 | * being destroyed. This ensures that a user won't get a pointer | |
529 | * to a stale stream class instance from an event class. | |
530 | */ | |
273b65be JG |
531 | end: |
532 | return ret; | |
533 | } | |
534 | ||
535 | BT_HIDDEN | |
536 | int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class, | |
537 | struct metadata_context *context) | |
538 | { | |
539 | int ret = 0; | |
2f100782 | 540 | int64_t stream_id; |
273b65be JG |
541 | |
542 | assert(event_class); | |
543 | assert(context); | |
2f100782 JG |
544 | stream_id = bt_ctf_stream_class_get_id(event_class->stream_class); |
545 | if (stream_id < 0) { | |
546 | ret = -1; | |
547 | goto end; | |
548 | } | |
549 | ||
273b65be JG |
550 | context->current_indentation_level = 1; |
551 | g_string_assign(context->field_name, ""); | |
de876b7f | 552 | g_string_append_printf(context->string, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRId64 ";\n", |
273b65be JG |
553 | g_quark_to_string(event_class->name), |
554 | event_class->id, | |
2f100782 | 555 | stream_id); |
273b65be JG |
556 | |
557 | if (event_class->context) { | |
558 | g_string_append(context->string, "\tcontext := "); | |
559 | ret = bt_ctf_field_type_serialize(event_class->context, | |
560 | context); | |
561 | if (ret) { | |
562 | goto end; | |
563 | } | |
564 | g_string_append(context->string, ";\n"); | |
565 | } | |
566 | ||
567 | if (event_class->fields) { | |
568 | g_string_append(context->string, "\tfields := "); | |
569 | ret = bt_ctf_field_type_serialize(event_class->fields, context); | |
570 | if (ret) { | |
571 | goto end; | |
572 | } | |
573 | g_string_append(context->string, ";\n"); | |
574 | } | |
575 | ||
576 | g_string_append(context->string, "};\n\n"); | |
577 | end: | |
578 | context->current_indentation_level = 0; | |
579 | return ret; | |
580 | } | |
581 | ||
582 | BT_HIDDEN | |
583 | int bt_ctf_event_validate(struct bt_ctf_event *event) | |
584 | { | |
585 | /* Make sure each field's payload has been set */ | |
586 | int ret; | |
587 | ||
588 | assert(event); | |
589 | ret = bt_ctf_field_validate(event->fields_payload); | |
590 | if (ret) { | |
591 | goto end; | |
592 | } | |
593 | ||
594 | if (event->event_class->context) { | |
595 | ret = bt_ctf_field_validate(event->context_payload); | |
596 | } | |
597 | end: | |
598 | return ret; | |
599 | } | |
600 | ||
601 | BT_HIDDEN | |
602 | int bt_ctf_event_serialize(struct bt_ctf_event *event, | |
603 | struct ctf_stream_pos *pos) | |
604 | { | |
605 | int ret = 0; | |
606 | ||
607 | assert(event); | |
608 | assert(pos); | |
609 | if (event->context_payload) { | |
610 | ret = bt_ctf_field_serialize(event->context_payload, pos); | |
611 | if (ret) { | |
612 | goto end; | |
613 | } | |
614 | } | |
615 | ||
616 | if (event->fields_payload) { | |
617 | ret = bt_ctf_field_serialize(event->fields_payload, pos); | |
618 | if (ret) { | |
619 | goto end; | |
620 | } | |
621 | } | |
622 | end: | |
623 | return ret; | |
624 | } | |
625 | ||
626 | BT_HIDDEN | |
627 | int bt_ctf_event_set_timestamp(struct bt_ctf_event *event, | |
628 | uint64_t timestamp) | |
629 | { | |
630 | int ret = 0; | |
631 | ||
632 | assert(event); | |
633 | if (event->timestamp) { | |
634 | ret = -1; | |
635 | goto end; | |
636 | } | |
637 | ||
638 | event->timestamp = timestamp; | |
639 | end: | |
640 | return ret; | |
641 | } | |
642 | ||
643 | BT_HIDDEN | |
644 | uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event *event) | |
645 | { | |
646 | assert(event); | |
647 | return event->timestamp; | |
648 | } |