Fix: add stricter checks on packet boundaries
[babeltrace.git] / formats / ctf / writer / stream.c
1 /*
2 * stream.c
3 *
4 * Babeltrace CTF Writer
5 *
6 * Copyright 2013 EfficiOS Inc.
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/clock.h>
30 #include <babeltrace/ctf-writer/clock-internal.h>
31 #include <babeltrace/ctf-writer/event.h>
32 #include <babeltrace/ctf-writer/event-internal.h>
33 #include <babeltrace/ctf-writer/event-types-internal.h>
34 #include <babeltrace/ctf-writer/event-fields-internal.h>
35 #include <babeltrace/ctf-writer/stream.h>
36 #include <babeltrace/ctf-writer/stream-internal.h>
37 #include <babeltrace/ctf-writer/functor-internal.h>
38 #include <babeltrace/compiler.h>
39 #include <babeltrace/align.h>
40
41 static
42 void bt_ctf_stream_destroy(struct bt_ctf_ref *ref);
43 static
44 void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
45 static
46 int init_event_header(struct bt_ctf_stream_class *stream_class,
47 enum bt_ctf_byte_order byte_order);
48 static
49 int init_packet_context(struct bt_ctf_stream_class *stream_class,
50 enum bt_ctf_byte_order byte_order);
51 static
52 int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
53
54 struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
55 {
56 struct bt_ctf_stream_class *stream_class = NULL;
57
58 if (!name || !strlen(name)) {
59 goto error;
60 }
61
62 stream_class = g_new0(struct bt_ctf_stream_class, 1);
63 if (!stream_class) {
64 goto error;
65 }
66
67 stream_class->name = g_string_new(name);
68 stream_class->event_classes = g_ptr_array_new_with_free_func(
69 (GDestroyNotify)bt_ctf_event_class_put);
70 if (!stream_class->event_classes) {
71 goto error_destroy;
72 }
73
74 bt_ctf_ref_init(&stream_class->ref_count);
75 return stream_class;
76
77 error_destroy:
78 bt_ctf_stream_class_destroy(&stream_class->ref_count);
79 stream_class = NULL;
80 error:
81 return stream_class;
82 }
83
84 int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
85 struct bt_ctf_clock *clock)
86 {
87 int ret = 0;
88
89 if (!stream_class || !clock || stream_class->frozen) {
90 ret = -1;
91 goto end;
92 }
93
94 if (stream_class->clock) {
95 bt_ctf_clock_put(stream_class->clock);
96 }
97
98 stream_class->clock = clock;
99 bt_ctf_clock_get(clock);
100 end:
101 return ret;
102 }
103
104 int bt_ctf_stream_class_add_event_class(
105 struct bt_ctf_stream_class *stream_class,
106 struct bt_ctf_event_class *event_class)
107 {
108 int ret = 0;
109
110 if (!stream_class || !event_class) {
111 ret = -1;
112 goto end;
113 }
114
115 /* Check for duplicate event classes */
116 struct search_query query = { .value = event_class, .found = 0 };
117 g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
118 if (query.found) {
119 ret = -1;
120 goto end;
121 }
122
123 if (bt_ctf_event_class_set_id(event_class,
124 stream_class->next_event_id++)) {
125 /* The event is already associated to a stream class */
126 ret = -1;
127 goto end;
128 }
129
130 bt_ctf_event_class_get(event_class);
131 g_ptr_array_add(stream_class->event_classes, event_class);
132 end:
133 return ret;
134 }
135
136 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
137 {
138 if (!stream_class) {
139 return;
140 }
141
142 bt_ctf_ref_get(&stream_class->ref_count);
143 }
144
145 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
146 {
147 if (!stream_class) {
148 return;
149 }
150
151 bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
152 }
153
154 BT_HIDDEN
155 void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
156 {
157 if (!stream_class) {
158 return;
159 }
160
161 stream_class->frozen = 1;
162 bt_ctf_clock_freeze(stream_class->clock);
163 g_ptr_array_foreach(stream_class->event_classes,
164 (GFunc)bt_ctf_event_class_freeze, NULL);
165 }
166
167 BT_HIDDEN
168 int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
169 uint32_t id)
170 {
171 int ret = 0;
172
173 if (!stream_class ||
174 (stream_class->id_set && (id != stream_class->id))) {
175 ret = -1;
176 goto end;
177 }
178
179 stream_class->id = id;
180 stream_class->id_set = 1;
181 end:
182 return ret;
183 }
184
185 BT_HIDDEN
186 int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
187 enum bt_ctf_byte_order byte_order)
188 {
189 int ret = 0;
190
191 ret = init_packet_context(stream_class, byte_order);
192 if (ret) {
193 goto end;
194 }
195
196 ret = init_event_header(stream_class, byte_order);
197 if (ret) {
198 goto end;
199 }
200 end:
201 return ret;
202 }
203
204 BT_HIDDEN
205 int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
206 struct metadata_context *context)
207 {
208 int ret = 0;
209 size_t i;
210
211 g_string_assign(context->field_name, "");
212 context->current_indentation_level = 1;
213 if (!stream_class->id_set) {
214 ret = -1;
215 goto end;
216 }
217
218 g_string_append_printf(context->string,
219 "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
220 stream_class->id);
221 ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
222 context);
223 if (ret) {
224 goto end;
225 }
226
227 g_string_append(context->string, ";\n\n\tpacket.context := ");
228 ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
229 context);
230 if (ret) {
231 goto end;
232 }
233
234 if (stream_class->event_context_type) {
235 g_string_append(context->string, ";\n\n\tevent.context := ");
236 ret = bt_ctf_field_type_serialize(
237 stream_class->event_context_type, context);
238 if (ret) {
239 goto end;
240 }
241 }
242
243 g_string_append(context->string, ";\n};\n\n");
244
245 /* Assign this stream's ID to every event and serialize them */
246 g_ptr_array_foreach(stream_class->event_classes,
247 (GFunc) bt_ctf_event_class_set_stream_id,
248 GUINT_TO_POINTER(stream_class->id));
249 for (i = 0; i < stream_class->event_classes->len; i++) {
250 struct bt_ctf_event_class *event_class =
251 stream_class->event_classes->pdata[i];
252
253 ret = bt_ctf_event_class_set_stream_id(event_class,
254 stream_class->id);
255 if (ret) {
256 goto end;
257 }
258
259 ret = bt_ctf_event_class_serialize(event_class, context);
260 if (ret) {
261 goto end;
262 }
263 }
264 end:
265 context->current_indentation_level = 0;
266 return ret;
267 }
268
269 BT_HIDDEN
270 struct bt_ctf_stream *bt_ctf_stream_create(
271 struct bt_ctf_stream_class *stream_class)
272 {
273 struct bt_ctf_stream *stream = NULL;
274
275 if (!stream_class) {
276 goto end;
277 }
278
279 stream = g_new0(struct bt_ctf_stream, 1);
280 if (!stream) {
281 goto end;
282 }
283
284 bt_ctf_ref_init(&stream->ref_count);
285 stream->pos.fd = -1;
286 stream->id = stream_class->next_stream_id++;
287 stream->stream_class = stream_class;
288 bt_ctf_stream_class_get(stream_class);
289 bt_ctf_stream_class_freeze(stream_class);
290 stream->events = g_ptr_array_new_with_free_func(
291 (GDestroyNotify)bt_ctf_event_put);
292 end:
293 return stream;
294 }
295
296 BT_HIDDEN
297 int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream *stream,
298 flush_func callback, void *data)
299 {
300 int ret = stream ? 0 : -1;
301
302 if (!stream) {
303 goto end;
304 }
305
306 stream->flush.func = callback;
307 stream->flush.data = data;
308 end:
309 return ret;
310 }
311
312 BT_HIDDEN
313 int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd)
314 {
315 int ret = 0;
316
317 if (stream->pos.fd != -1) {
318 ret = -1;
319 goto end;
320 }
321
322 ctf_init_pos(&stream->pos, NULL, fd, O_RDWR);
323 stream->pos.fd = fd;
324 end:
325 return ret;
326 }
327
328 void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
329 uint64_t event_count)
330 {
331 if (!stream) {
332 return;
333 }
334
335 stream->events_discarded += event_count;
336 }
337
338 int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
339 struct bt_ctf_event *event)
340 {
341 int ret = 0;
342 uint64_t timestamp;
343
344 if (!stream || !event) {
345 ret = -1;
346 goto end;
347 }
348
349 ret = bt_ctf_event_validate(event);
350 if (ret) {
351 goto end;
352 }
353
354 timestamp = bt_ctf_clock_get_time(stream->stream_class->clock);
355 ret = bt_ctf_event_set_timestamp(event, timestamp);
356 if (ret) {
357 goto end;
358 }
359
360 bt_ctf_event_get(event);
361 g_ptr_array_add(stream->events, event);
362 end:
363 return ret;
364 }
365
366 int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
367 {
368 int ret = 0;
369 size_t i;
370 uint64_t timestamp_begin, timestamp_end;
371 struct bt_ctf_stream_class *stream_class;
372 struct bt_ctf_field *integer = NULL;
373 struct ctf_stream_pos packet_context_pos;
374
375 if (!stream) {
376 ret = -1;
377 goto end;
378 }
379
380 if (!stream->events->len) {
381 goto end;
382 }
383
384 if (stream->flush.func) {
385 stream->flush.func(stream, stream->flush.data);
386 }
387
388 stream_class = stream->stream_class;
389 timestamp_begin = ((struct bt_ctf_event *) g_ptr_array_index(
390 stream->events, 0))->timestamp;
391 timestamp_end = ((struct bt_ctf_event *) g_ptr_array_index(
392 stream->events, stream->events->len - 1))->timestamp;
393 ret = set_structure_field_integer(stream_class->packet_context,
394 "timestamp_begin", timestamp_begin);
395 if (ret) {
396 goto end;
397 }
398
399 ret = set_structure_field_integer(stream_class->packet_context,
400 "timestamp_end", timestamp_end);
401 if (ret) {
402 goto end;
403 }
404
405 ret = set_structure_field_integer(stream_class->packet_context,
406 "events_discarded", stream->events_discarded);
407 if (ret) {
408 goto end;
409 }
410
411 ret = set_structure_field_integer(stream_class->packet_context,
412 "content_size", UINT64_MAX);
413 if (ret) {
414 goto end;
415 }
416
417 ret = set_structure_field_integer(stream_class->packet_context,
418 "packet_size", UINT64_MAX);
419 if (ret) {
420 goto end;
421 }
422
423 /* Write packet context */
424 memcpy(&packet_context_pos, &stream->pos,
425 sizeof(struct ctf_stream_pos));
426 ret = bt_ctf_field_serialize(stream_class->packet_context,
427 &stream->pos);
428 if (ret) {
429 goto end;
430 }
431
432 for (i = 0; i < stream->events->len; i++) {
433 struct bt_ctf_event *event = g_ptr_array_index(
434 stream->events, i);
435 uint32_t event_id = bt_ctf_event_class_get_id(
436 event->event_class);
437 uint64_t timestamp = bt_ctf_event_get_timestamp(event);
438
439 ret = set_structure_field_integer(stream_class->event_header,
440 "id", event_id);
441 if (ret) {
442 goto end;
443 }
444 ret = set_structure_field_integer(stream_class->event_header,
445 "timestamp", timestamp);
446 if (ret) {
447 goto end;
448 }
449
450 /* Write event header */
451 ret = bt_ctf_field_serialize(stream_class->event_header,
452 &stream->pos);
453 if (ret) {
454 goto end;
455 }
456
457 /* Write event content */
458 ret = bt_ctf_event_serialize(event, &stream->pos);
459 if (ret) {
460 goto end;
461 }
462 }
463
464 /*
465 * Update the packet total size and content size and overwrite the
466 * packet context.
467 */
468 ret = set_structure_field_integer(stream_class->packet_context,
469 "content_size", stream->pos.offset);
470 if (ret) {
471 goto end;
472 }
473
474 ret = set_structure_field_integer(stream_class->packet_context,
475 "packet_size", stream->pos.packet_size);
476 if (ret) {
477 goto end;
478 }
479
480 ret = bt_ctf_field_serialize(stream_class->packet_context,
481 &packet_context_pos);
482 if (ret) {
483 goto end;
484 }
485
486 g_ptr_array_set_size(stream->events, 0);
487 stream->flushed_packet_count++;
488 end:
489 bt_ctf_field_put(integer);
490 return ret;
491 }
492
493 void bt_ctf_stream_get(struct bt_ctf_stream *stream)
494 {
495 if (!stream) {
496 return;
497 }
498
499 bt_ctf_ref_get(&stream->ref_count);
500 }
501
502 void bt_ctf_stream_put(struct bt_ctf_stream *stream)
503 {
504 if (!stream) {
505 return;
506 }
507
508 bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy);
509 }
510
511 static
512 void bt_ctf_stream_destroy(struct bt_ctf_ref *ref)
513 {
514 struct bt_ctf_stream *stream;
515
516 if (!ref) {
517 return;
518 }
519
520 stream = container_of(ref, struct bt_ctf_stream, ref_count);
521 ctf_fini_pos(&stream->pos);
522 if (close(stream->pos.fd)) {
523 perror("close");
524 }
525 bt_ctf_stream_class_put(stream->stream_class);
526 g_ptr_array_free(stream->events, TRUE);
527 g_free(stream);
528 }
529
530 static
531 void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
532 {
533 struct bt_ctf_stream_class *stream_class;
534
535 if (!ref) {
536 return;
537 }
538
539 stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
540 bt_ctf_clock_put(stream_class->clock);
541
542 if (stream_class->event_classes) {
543 g_ptr_array_free(stream_class->event_classes, TRUE);
544 }
545
546 if (stream_class->name) {
547 g_string_free(stream_class->name, TRUE);
548 }
549
550 bt_ctf_field_type_put(stream_class->event_header_type);
551 bt_ctf_field_put(stream_class->event_header);
552 bt_ctf_field_type_put(stream_class->packet_context_type);
553 bt_ctf_field_put(stream_class->packet_context);
554 bt_ctf_field_type_put(stream_class->event_context_type);
555 bt_ctf_field_put(stream_class->event_context);
556 g_free(stream_class);
557 }
558
559 static
560 int init_event_header(struct bt_ctf_stream_class *stream_class,
561 enum bt_ctf_byte_order byte_order)
562 {
563 int ret = 0;
564 struct bt_ctf_field_type *event_header_type =
565 bt_ctf_field_type_structure_create();
566 struct bt_ctf_field_type *_uint32_t =
567 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
568 struct bt_ctf_field_type *_uint64_t =
569 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
570
571 if (!event_header_type) {
572 ret = -1;
573 goto end;
574 }
575
576 ret = bt_ctf_field_type_set_byte_order(_uint32_t, byte_order);
577 if (ret) {
578 goto end;
579 }
580
581 ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
582 if (ret) {
583 goto end;
584 }
585
586 ret = bt_ctf_field_type_structure_add_field(event_header_type,
587 _uint32_t, "id");
588 if (ret) {
589 goto end;
590 }
591
592 ret = bt_ctf_field_type_structure_add_field(event_header_type,
593 _uint64_t, "timestamp");
594 if (ret) {
595 goto end;
596 }
597
598 stream_class->event_header_type = event_header_type;
599 stream_class->event_header = bt_ctf_field_create(
600 stream_class->event_header_type);
601 if (!stream_class->event_header) {
602 ret = -1;
603 }
604 end:
605 if (ret) {
606 bt_ctf_field_type_put(event_header_type);
607 }
608
609 bt_ctf_field_type_put(_uint32_t);
610 bt_ctf_field_type_put(_uint64_t);
611 return ret;
612 }
613
614 static
615 int init_packet_context(struct bt_ctf_stream_class *stream_class,
616 enum bt_ctf_byte_order byte_order)
617 {
618 int ret = 0;
619 struct bt_ctf_field_type *packet_context_type =
620 bt_ctf_field_type_structure_create();
621 struct bt_ctf_field_type *_uint64_t =
622 get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
623
624 if (!packet_context_type) {
625 ret = -1;
626 goto end;
627 }
628
629 /*
630 * We create a stream packet context as proposed in the CTF
631 * specification.
632 */
633 ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
634 if (ret) {
635 goto end;
636 }
637
638 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
639 _uint64_t, "timestamp_begin");
640 if (ret) {
641 goto end;
642 }
643
644 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
645 _uint64_t, "timestamp_end");
646 if (ret) {
647 goto end;
648 }
649
650 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
651 _uint64_t, "content_size");
652 if (ret) {
653 goto end;
654 }
655
656 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
657 _uint64_t, "packet_size");
658 if (ret) {
659 goto end;
660 }
661
662 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
663 _uint64_t, "events_discarded");
664 if (ret) {
665 goto end;
666 }
667
668 stream_class->packet_context_type = packet_context_type;
669 stream_class->packet_context = bt_ctf_field_create(packet_context_type);
670 if (!stream_class->packet_context) {
671 ret = -1;
672 }
673 end:
674 if (ret) {
675 bt_ctf_field_type_put(packet_context_type);
676 goto end;
677 }
678
679 bt_ctf_field_type_put(_uint64_t);
680 return ret;
681 }
682
683 static
684 int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
685 uint64_t value)
686 {
687 int ret = 0;
688
689 struct bt_ctf_field *integer =
690 bt_ctf_field_structure_get_field(structure, name);
691 if (!integer) {
692 ret = -1;
693 goto end;
694 }
695
696 ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
697 end:
698 bt_ctf_field_put(integer);
699 return ret;
700 }
This page took 0.04431 seconds and 4 git commands to generate.