Merge branch 'master' into bindings/python
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 12 Nov 2013 16:23:52 +0000 (11:23 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 12 Nov 2013 16:23:52 +0000 (11:23 -0500)
36 files changed:
configure.ac
formats/ctf/Makefile.am
formats/ctf/ctf.c
formats/ctf/types/float.c
formats/ctf/writer/Makefile.am [new file with mode: 0644]
formats/ctf/writer/clock.c [new file with mode: 0644]
formats/ctf/writer/event-fields.c [new file with mode: 0644]
formats/ctf/writer/event-types.c [new file with mode: 0644]
formats/ctf/writer/event.c [new file with mode: 0644]
formats/ctf/writer/functor.c [new file with mode: 0644]
formats/ctf/writer/stream.c [new file with mode: 0644]
formats/ctf/writer/writer.c [new file with mode: 0644]
include/Makefile.am
include/babeltrace/bitfield.h
include/babeltrace/ctf-writer/clock-internal.h [new file with mode: 0644]
include/babeltrace/ctf-writer/clock.h [new file with mode: 0644]
include/babeltrace/ctf-writer/event-fields-internal.h [new file with mode: 0644]
include/babeltrace/ctf-writer/event-fields.h [new file with mode: 0644]
include/babeltrace/ctf-writer/event-internal.h [new file with mode: 0644]
include/babeltrace/ctf-writer/event-types-internal.h [new file with mode: 0644]
include/babeltrace/ctf-writer/event-types.h [new file with mode: 0644]
include/babeltrace/ctf-writer/event.h [new file with mode: 0644]
include/babeltrace/ctf-writer/functor-internal.h [new file with mode: 0644]
include/babeltrace/ctf-writer/ref-internal.h [new file with mode: 0644]
include/babeltrace/ctf-writer/stream-internal.h [new file with mode: 0644]
include/babeltrace/ctf-writer/stream.h [new file with mode: 0644]
include/babeltrace/ctf-writer/writer-internal.h [new file with mode: 0644]
include/babeltrace/ctf-writer/writer.h [new file with mode: 0644]
include/babeltrace/ctf/types.h
include/babeltrace/iterator.h
lib/context.c
tests/lib/Makefile.am
tests/lib/test_ctf_writer.c [new file with mode: 0644]
tests/lib/test_ctf_writer_complete [new file with mode: 0755]
tests/lib/test_seek.c
tests/tests

index 16679b84ae09dd7527b17cac343d1b7f4c8c1ad5..637daa56761370c9a7c19f5f45e5ada72d396b4a 100644 (file)
@@ -150,6 +150,9 @@ AC_SUBST(babeltraceincludedir)
 babeltracectfincludedir="${includedir}/babeltrace/ctf"
 AC_SUBST(babeltracectfincludedir)
 
+babeltracectfwriterincludedir="${includedir}/babeltrace/ctf-writer"
+AC_SUBST(babeltracectfwriterincludedir)
+
 AC_CONFIG_FILES([
        Makefile
        types/Makefile
@@ -162,6 +165,7 @@ AC_CONFIG_FILES([
        formats/ctf-metadata/Makefile
        formats/bt-dummy/Makefile
        formats/ctf/metadata/Makefile
+       formats/ctf/writer/Makefile
        converter/Makefile
        doc/Makefile
        lib/Makefile
index 6dcc1bb3343cb19f8b6e1a3d1d26708e88fd1f22..d79d9ef38030166725f9f80feafdc81dcb21ce76 100644 (file)
@@ -1,6 +1,6 @@
 AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include
 
-SUBDIRS = types metadata .
+SUBDIRS = types metadata writer .
 
 lib_LTLIBRARIES = libbabeltrace-ctf.la
 
@@ -13,10 +13,11 @@ libbabeltrace_ctf_la_SOURCES = \
 
 # Request that the linker keeps all static libraries objects.
 libbabeltrace_ctf_la_LDFLAGS = \
-       -Wl,--no-as-needed \
+       -Wl,--no-as-needed
+
+libbabeltrace_ctf_la_LIBADD = \
+       $(top_builddir)/lib/libbabeltrace.la \
        types/libctf-types.la \
        metadata/libctf-parser.la \
-       metadata/libctf-ast.la
-       
-libbabeltrace_ctf_la_LIBADD = \
-       $(top_builddir)/lib/libbabeltrace.la
+       metadata/libctf-ast.la \
+       writer/libctf-writer.la
index 947b439d24349d9055459e3339eec03fc27a2f35..60d9c9f9459d159b7627c9e76927409eccd55272 100644 (file)
@@ -712,7 +712,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                switch (whence) {
                case SEEK_CUR:
                        /* The writer will add padding */
-                       pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT;
+                       pos->mmap_offset += pos->packet_size / CHAR_BIT;
                        break;
                case SEEK_SET:
                        assert(index == 0);     /* only seek supported for now */
index 2daadf91e40de1325832d39e867ab6ff38e826f0..689028360d4fe8f258d307e014e54103fb03201e 100644 (file)
@@ -232,7 +232,7 @@ int ctf_float_write(struct bt_stream_pos *ppos, struct bt_definition *definition
        union doubleIEEE754 u;
        struct bt_definition *tmpdef;
        struct definition_float *tmpfloat;
-       struct ctf_stream_pos srcp;
+       struct ctf_stream_pos srcp = { { 0 } };
        struct mmap_align mma;
        int ret;
 
diff --git a/formats/ctf/writer/Makefile.am b/formats/ctf/writer/Makefile.am
new file mode 100644 (file)
index 0000000..84ac03d
--- /dev/null
@@ -0,0 +1,22 @@
+AM_CFLAGS = $(PACKAGE_CFLAGS) -I$(top_srcdir)/include
+
+noinst_LTLIBRARIES = libctf-writer.la
+
+libctf_writer_la_SOURCES = \
+       writer.c \
+       clock.c \
+       stream.c \
+       event-types.c \
+       event-fields.c \
+       event.c \
+       functor.c
+
+libctf_writer_la_LIBADD = \
+       $(top_builddir)/lib/libbabeltrace.la
+
+if BABELTRACE_BUILD_WITH_LIBUUID
+libctf_writer_la_LIBADD += -luuid
+endif
+if BABELTRACE_BUILD_WITH_LIBC_UUID
+libctf_writer_la_LIBADD += -lc
+endif
diff --git a/formats/ctf/writer/clock.c b/formats/ctf/writer/clock.c
new file mode 100644 (file)
index 0000000..1146e00
--- /dev/null
@@ -0,0 +1,264 @@
+/*
+ * clock.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/clock.h>
+#include <babeltrace/ctf-writer/clock-internal.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/compiler.h>
+#include <inttypes.h>
+
+static
+void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
+
+struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
+{
+       struct bt_ctf_clock *clock = NULL;
+
+       if (validate_identifier(name)) {
+               goto error;
+       }
+
+       clock = g_new0(struct bt_ctf_clock, 1);
+       if (!clock) {
+               goto error;
+       }
+
+       clock->name = g_string_new(name);
+       if (!clock->name) {
+               goto error_destroy;
+       }
+
+       clock->description = g_string_new(NULL);
+       if (!clock->description) {
+               goto error_destroy;
+       }
+
+       clock->precision = 1;
+       clock->frequency = 1000000000;
+       uuid_generate(clock->uuid);
+       bt_ctf_ref_init(&clock->ref_count);
+       return clock;
+error_destroy:
+       bt_ctf_clock_destroy(&clock->ref_count);
+error:
+       clock = NULL;
+       return clock;
+}
+
+int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
+{
+       int ret = 0;
+
+       if (!clock || !desc || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->description = g_string_assign(clock->description, desc);
+       ret = clock->description ? 0 : -1;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->frequency = freq;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->precision = precision;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->offset_s = offset_s;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->offset = offset;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
+{
+       int ret = 0;
+
+       if (!clock || clock->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->absolute = !!is_absolute;
+end:
+       return ret;
+}
+
+int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
+{
+       int ret = 0;
+
+       /* Timestamps are strictly monotonic */
+       if (!clock || time < clock->time) {
+               ret = -1;
+               goto end;
+       }
+
+       clock->time = time;
+end:
+       return ret;
+}
+
+void bt_ctf_clock_get(struct bt_ctf_clock *clock)
+{
+       if (!clock) {
+               return;
+       }
+
+       bt_ctf_ref_get(&clock->ref_count);
+}
+
+void bt_ctf_clock_put(struct bt_ctf_clock *clock)
+{
+       if (!clock) {
+               return;
+       }
+
+       bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
+}
+
+BT_HIDDEN
+void bt_ctf_clock_freeze(struct bt_ctf_clock *clock)
+{
+       if (!clock) {
+               return;
+       }
+
+       clock->frozen = 1;
+}
+
+BT_HIDDEN
+void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
+               struct metadata_context *context)
+{
+       unsigned char *uuid;
+
+       if (!clock || !context) {
+               return;
+       }
+
+       uuid = clock->uuid;
+       g_string_append(context->string, "clock {\n");
+       g_string_append_printf(context->string, "\tname = %s;\n",
+               clock->name->str);
+       g_string_append_printf(context->string,
+               "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
+               uuid[0], uuid[1], uuid[2], uuid[3],
+               uuid[4], uuid[5], uuid[6], uuid[7],
+               uuid[8], uuid[9], uuid[10], uuid[11],
+               uuid[12], uuid[13], uuid[14], uuid[15]);
+       if (clock->description->len) {
+               g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
+                       clock->description->str);
+       }
+
+       g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
+               clock->frequency);
+       g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
+               clock->precision);
+       g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
+               clock->offset_s);
+       g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
+               clock->offset);
+       g_string_append_printf(context->string, "\tabsolute = %s;\n",
+               clock->absolute ? "TRUE" : "FALSE");
+       g_string_append(context->string, "};\n\n");
+}
+
+BT_HIDDEN
+uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
+{
+       return clock ? clock->time : 0;
+}
+
+static
+void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_clock *clock;
+
+       if (!ref) {
+               return;
+       }
+
+       clock = container_of(ref, struct bt_ctf_clock, ref_count);
+       if (clock->name) {
+               g_string_free(clock->name, TRUE);
+       }
+
+       if (clock->description) {
+               g_string_free(clock->description, TRUE);
+       }
+
+       g_free(clock);
+}
diff --git a/formats/ctf/writer/event-fields.c b/formats/ctf/writer/event-fields.c
new file mode 100644 (file)
index 0000000..aa389fd
--- /dev/null
@@ -0,0 +1,1249 @@
+/*
+ * event-fields.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/ctf-writer/event-fields-internal.h>
+#include <babeltrace/ctf-writer/event-types-internal.h>
+#include <babeltrace/compiler.h>
+
+#define PACKET_LEN_INCREMENT   (getpagesize() * 8 * CHAR_BIT)
+
+static
+struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *);
+static
+struct bt_ctf_field *bt_ctf_field_enumeration_create(
+               struct bt_ctf_field_type *);
+static
+struct bt_ctf_field *bt_ctf_field_floating_point_create(
+               struct bt_ctf_field_type *);
+static
+struct bt_ctf_field *bt_ctf_field_structure_create(
+               struct bt_ctf_field_type *);
+static
+struct bt_ctf_field *bt_ctf_field_variant_create(
+               struct bt_ctf_field_type *);
+static
+struct bt_ctf_field *bt_ctf_field_array_create(
+               struct bt_ctf_field_type *);
+static
+struct bt_ctf_field *bt_ctf_field_sequence_create(
+               struct bt_ctf_field_type *);
+static
+struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *);
+
+static
+void bt_ctf_field_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_integer_destroy(struct bt_ctf_field *);
+static
+void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *);
+static
+void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *);
+static
+void bt_ctf_field_structure_destroy(struct bt_ctf_field *);
+static
+void bt_ctf_field_variant_destroy(struct bt_ctf_field *);
+static
+void bt_ctf_field_array_destroy(struct bt_ctf_field *);
+static
+void bt_ctf_field_sequence_destroy(struct bt_ctf_field *);
+static
+void bt_ctf_field_string_destroy(struct bt_ctf_field *);
+
+static
+int bt_ctf_field_generic_validate(struct bt_ctf_field *field);
+static
+int bt_ctf_field_structure_validate(struct bt_ctf_field *field);
+static
+int bt_ctf_field_variant_validate(struct bt_ctf_field *field);
+static
+int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field);
+static
+int bt_ctf_field_array_validate(struct bt_ctf_field *field);
+static
+int bt_ctf_field_sequence_validate(struct bt_ctf_field *field);
+
+static
+int bt_ctf_field_integer_serialize(struct bt_ctf_field *,
+               struct ctf_stream_pos *);
+static
+int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *,
+               struct ctf_stream_pos *);
+static
+int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *,
+               struct ctf_stream_pos *);
+static
+int bt_ctf_field_structure_serialize(struct bt_ctf_field *,
+               struct ctf_stream_pos *);
+static
+int bt_ctf_field_variant_serialize(struct bt_ctf_field *,
+               struct ctf_stream_pos *);
+static
+int bt_ctf_field_array_serialize(struct bt_ctf_field *,
+               struct ctf_stream_pos *);
+static
+int bt_ctf_field_sequence_serialize(struct bt_ctf_field *,
+               struct ctf_stream_pos *);
+static
+int bt_ctf_field_string_serialize(struct bt_ctf_field *,
+               struct ctf_stream_pos *);
+
+static
+int increase_packet_size(struct ctf_stream_pos *pos);
+
+static
+struct bt_ctf_field *(*field_create_funcs[])(
+               struct bt_ctf_field_type *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_floating_point_create,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_create,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_create,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_create,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_create,
+       [CTF_TYPE_STRING] = bt_ctf_field_string_create,
+};
+
+static
+void (*field_destroy_funcs[])(struct bt_ctf_field *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_floating_point_destroy,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_destroy,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_destroy,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_destroy,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_destroy,
+       [CTF_TYPE_STRING] = bt_ctf_field_string_destroy,
+};
+
+static
+int (*field_validate_funcs[])(struct bt_ctf_field *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate,
+       [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_validate,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_validate,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_validate,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_validate,
+       [CTF_TYPE_STRING] = bt_ctf_field_generic_validate,
+};
+
+static
+int (*field_serialize_funcs[])(struct bt_ctf_field *,
+               struct ctf_stream_pos *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_floating_point_serialize,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_serialize,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_serialize,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_serialize,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_serialize,
+       [CTF_TYPE_STRING] = bt_ctf_field_string_serialize,
+};
+
+struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field *field = NULL;
+       enum ctf_type_id type_id;
+
+       if (!type) {
+               goto error;
+       }
+
+       type_id = bt_ctf_field_type_get_type_id(type);
+       if (type_id <= CTF_TYPE_UNKNOWN ||
+               type_id >= NR_CTF_TYPES) {
+               goto error;
+       }
+
+       field = field_create_funcs[type_id](type);
+       if (!field) {
+               goto error;
+       }
+
+       /* The type's declaration can't change after this point */
+       bt_ctf_field_type_freeze(type);
+       bt_ctf_field_type_get(type);
+       bt_ctf_ref_init(&field->ref_count);
+       field->type = type;
+error:
+       return field;
+}
+
+void bt_ctf_field_get(struct bt_ctf_field *field)
+{
+       if (field) {
+               bt_ctf_ref_get(&field->ref_count);
+       }
+}
+
+void bt_ctf_field_put(struct bt_ctf_field *field)
+{
+       if (field) {
+               bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy);
+       }
+}
+
+int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field,
+               struct bt_ctf_field *length_field)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_integer *length_type;
+       struct bt_ctf_field_integer *length;
+       struct bt_ctf_field_sequence *sequence;
+       uint64_t sequence_length;
+
+       if (!field || !length_field) {
+               ret = -1;
+               goto end;
+       }
+       if (bt_ctf_field_type_get_type_id(length_field->type) !=
+               CTF_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+
+       length_type = container_of(length_field->type,
+               struct bt_ctf_field_type_integer, parent);
+       if (length_type->declaration.signedness) {
+               ret = -1;
+               goto end;
+       }
+
+       length = container_of(length_field, struct bt_ctf_field_integer,
+               parent);
+       sequence_length = length->definition.value._unsigned;
+       sequence = container_of(field, struct bt_ctf_field_sequence, parent);
+       if (sequence->elements) {
+               g_ptr_array_free(sequence->elements, TRUE);
+               bt_ctf_field_put(sequence->length);
+       }
+
+       sequence->elements = g_ptr_array_new_full((size_t)sequence_length,
+               (GDestroyNotify)bt_ctf_field_put);
+       if (!sequence->elements) {
+               ret = -1;
+               goto end;
+       }
+
+       g_ptr_array_set_size(sequence->elements, (size_t)sequence_length);
+       bt_ctf_field_get(length_field);
+       sequence->length = length_field;
+end:
+       return ret;
+}
+
+struct bt_ctf_field *bt_ctf_field_structure_get_field(
+               struct bt_ctf_field *field, const char *name)
+{
+       struct bt_ctf_field *new_field = NULL;
+       GQuark field_quark;
+       struct bt_ctf_field_structure *structure;
+       struct bt_ctf_field_type_structure *structure_type;
+       struct bt_ctf_field_type *field_type;
+       size_t index;
+
+       if (!field || !name ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_STRUCT) {
+               goto error;
+       }
+
+       field_quark = g_quark_from_string(name);
+       structure = container_of(field, struct bt_ctf_field_structure, parent);
+       structure_type = container_of(field->type,
+               struct bt_ctf_field_type_structure, parent);
+       field_type = bt_ctf_field_type_structure_get_type(structure_type, name);
+       if (!g_hash_table_lookup_extended(structure->field_name_to_index,
+               GUINT_TO_POINTER(field_quark), NULL, (gpointer *)&index)) {
+               goto error;
+       }
+
+       if (structure->fields->pdata[index]) {
+               new_field = structure->fields->pdata[index];
+               goto end;
+       }
+
+       new_field = bt_ctf_field_create(field_type);
+       if (!new_field) {
+               goto error;
+       }
+
+       structure->fields->pdata[index] = new_field;
+end:
+       bt_ctf_field_get(new_field);
+error:
+       return new_field;
+}
+
+BT_HIDDEN
+int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
+               const char *name, struct bt_ctf_field *value)
+{
+       int ret = 0;
+       GQuark field_quark;
+       struct bt_ctf_field_structure *structure;
+       struct bt_ctf_field_type_structure *structure_type;
+       struct bt_ctf_field_type *expected_field_type;
+       size_t index;
+
+       if (!field || !name || !value ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_STRUCT) {
+               ret = -1;
+               goto end;
+       }
+
+       field_quark = g_quark_from_string(name);
+       structure = container_of(field, struct bt_ctf_field_structure, parent);
+       structure_type = container_of(field->type,
+               struct bt_ctf_field_type_structure, parent);
+       expected_field_type = bt_ctf_field_type_structure_get_type(
+               structure_type, name);
+       if (expected_field_type != value->type) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!g_hash_table_lookup_extended(structure->field_name_to_index,
+               GUINT_TO_POINTER(field_quark), NULL, (gpointer *) &index)) {
+               goto end;
+       }
+
+       if (structure->fields->pdata[index]) {
+               bt_ctf_field_put(structure->fields->pdata[index]);
+       }
+
+       structure->fields->pdata[index] = value;
+       bt_ctf_field_get(value);
+end:
+       return ret;
+}
+
+struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field,
+               uint64_t index)
+{
+       struct bt_ctf_field *new_field = NULL;
+       struct bt_ctf_field_array *array;
+       struct bt_ctf_field_type_array *array_type;
+       struct bt_ctf_field_type *field_type;
+
+       if (!field || bt_ctf_field_type_get_type_id(field->type) !=
+               CTF_TYPE_ARRAY) {
+               goto end;
+       }
+
+       array = container_of(field, struct bt_ctf_field_array, parent);
+       if (index >= array->elements->len) {
+               goto end;
+       }
+
+       array_type = container_of(field->type, struct bt_ctf_field_type_array,
+               parent);
+       field_type = bt_ctf_field_type_array_get_element_type(array_type);
+       if (array->elements->pdata[(size_t)index]) {
+               new_field = array->elements->pdata[(size_t)index];
+               goto end;
+       }
+
+       new_field = bt_ctf_field_create(field_type);
+       bt_ctf_field_get(new_field);
+       array->elements->pdata[(size_t)index] = new_field;
+end:
+       return new_field;
+}
+
+struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
+               uint64_t index)
+{
+       struct bt_ctf_field *new_field = NULL;
+       struct bt_ctf_field_sequence *sequence;
+       struct bt_ctf_field_type_sequence *sequence_type;
+       struct bt_ctf_field_type *field_type;
+
+       if (!field || bt_ctf_field_type_get_type_id(field->type) !=
+               CTF_TYPE_SEQUENCE) {
+               goto end;
+       }
+
+       sequence = container_of(field, struct bt_ctf_field_sequence, parent);
+       if (!sequence->elements || sequence->elements->len <= index) {
+               goto end;
+       }
+
+       sequence_type = container_of(field->type,
+               struct bt_ctf_field_type_sequence, parent);
+       field_type = bt_ctf_field_type_sequence_get_element_type(sequence_type);
+       if (sequence->elements->pdata[(size_t)index]) {
+               new_field = sequence->elements->pdata[(size_t)index];
+               goto end;
+       }
+
+       new_field = bt_ctf_field_create(field_type);
+       bt_ctf_field_get(new_field);
+       sequence->elements->pdata[(size_t)index] = new_field;
+end:
+       return new_field;
+}
+
+struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field,
+               struct bt_ctf_field *tag_field)
+{
+       struct bt_ctf_field *new_field = NULL;
+       struct bt_ctf_field_variant *variant;
+       struct bt_ctf_field_type_variant *variant_type;
+       struct bt_ctf_field_type *field_type;
+       struct bt_ctf_field *tag_enum = NULL;
+       struct bt_ctf_field_integer *tag_enum_integer;
+       int64_t tag_enum_value;
+
+       if (!field || !tag_field ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_VARIANT ||
+               bt_ctf_field_type_get_type_id(tag_field->type) !=
+                       CTF_TYPE_ENUM) {
+               goto end;
+       }
+
+       variant = container_of(field, struct bt_ctf_field_variant, parent);
+       variant_type = container_of(field->type,
+               struct bt_ctf_field_type_variant, parent);
+       tag_enum = bt_ctf_field_enumeration_get_container(tag_field);
+       if (!tag_enum) {
+               goto end;
+       }
+
+       tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer,
+               parent);
+
+       if (!bt_ctf_field_validate(variant->tag)) {
+               goto end;
+       }
+
+       tag_enum_value = tag_enum_integer->definition.value._signed;
+       field_type = bt_ctf_field_type_variant_get_field_type(variant_type,
+               tag_enum_value);
+       if (!field_type) {
+               goto end;
+       }
+
+       new_field = bt_ctf_field_create(field_type);
+       if (!new_field) {
+               goto end;
+       }
+
+       bt_ctf_field_put(variant->tag);
+       bt_ctf_field_put(variant->payload);
+       bt_ctf_field_get(new_field);
+       bt_ctf_field_get(tag_field);
+       variant->tag = tag_field;
+       variant->payload = new_field;
+end:
+       bt_ctf_field_put(tag_enum);
+       return new_field;
+}
+
+struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
+       struct bt_ctf_field *field)
+{
+       struct bt_ctf_field *container = NULL;
+       struct bt_ctf_field_enumeration *enumeration;
+
+       if (!field) {
+               goto end;
+       }
+
+       enumeration = container_of(field, struct bt_ctf_field_enumeration,
+               parent);
+       if (!enumeration->payload) {
+               struct bt_ctf_field_type_enumeration *enumeration_type =
+                       container_of(field->type,
+                       struct bt_ctf_field_type_enumeration, parent);
+               enumeration->payload =
+                       bt_ctf_field_create(enumeration_type->container);
+       }
+
+       container = enumeration->payload;
+       bt_ctf_field_get(container);
+end:
+       return container;
+}
+
+int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field,
+               int64_t value)
+{
+       int ret = 0;
+       struct bt_ctf_field_integer *integer;
+       struct bt_ctf_field_type_integer *integer_type;
+       unsigned int size;
+       int64_t min_value, max_value;
+
+       if (!field ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+
+       integer = container_of(field, struct bt_ctf_field_integer, parent);
+       integer_type = container_of(field->type,
+               struct bt_ctf_field_type_integer, parent);
+       if (!integer_type->declaration.signedness) {
+               ret = -1;
+               goto end;
+       }
+
+       size = integer_type->declaration.len;
+       min_value = -((int64_t)1 << (size - 1));
+       max_value = ((int64_t)1 << (size - 1)) - 1;
+       if (value < min_value || value > max_value) {
+               ret = -1;
+               goto end;
+       }
+
+       integer->definition.value._signed = value;
+       integer->parent.payload_set = 1;
+end:
+       return ret;
+}
+
+int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field,
+               uint64_t value)
+{
+       int ret = 0;
+       struct bt_ctf_field_integer *integer;
+       struct bt_ctf_field_type_integer *integer_type;
+       unsigned int size;
+       uint64_t max_value;
+
+       if (!field ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+
+       integer = container_of(field, struct bt_ctf_field_integer, parent);
+       integer_type = container_of(field->type,
+               struct bt_ctf_field_type_integer, parent);
+       if (integer_type->declaration.signedness) {
+               ret = -1;
+               goto end;
+       }
+
+       size = integer_type->declaration.len;
+       max_value = (size == 64) ? UINT64_MAX : ((uint64_t)1 << size) - 1;
+       if (value > max_value) {
+               ret = -1;
+               goto end;
+       }
+
+       integer->definition.value._unsigned = value;
+       integer->parent.payload_set = 1;
+end:
+       return ret;
+}
+
+int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field,
+               double value)
+{
+       int ret = 0;
+       struct bt_ctf_field_floating_point *floating_point;
+
+       if (!field ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_FLOAT) {
+               ret = -1;
+               goto end;
+       }
+       floating_point = container_of(field, struct bt_ctf_field_floating_point,
+               parent);
+       floating_point->definition.value = value;
+       floating_point->parent.payload_set = 1;
+end:
+       return ret;
+}
+
+int bt_ctf_field_string_set_value(struct bt_ctf_field *field,
+               const char *value)
+{
+       int ret = 0;
+       struct bt_ctf_field_string *string;
+
+       if (!field || !value ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_STRING) {
+               ret = -1;
+               goto end;
+       }
+
+       string = container_of(field, struct bt_ctf_field_string, parent);
+       if (string->payload) {
+               g_string_free(string->payload, TRUE);
+       }
+
+       string->payload = g_string_new(value);
+       string->parent.payload_set = 1;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_field_validate(struct bt_ctf_field *field)
+{
+       int ret = 0;
+       enum ctf_type_id type_id;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       type_id = bt_ctf_field_type_get_type_id(field->type);
+       if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = field_validate_funcs[type_id](field);
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_field_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos)
+{
+       int ret = 0;
+       enum ctf_type_id type_id;
+
+       if (!field || !pos) {
+               ret = -1;
+               goto end;
+       }
+
+       type_id = bt_ctf_field_type_get_type_id(field->type);
+       if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = field_serialize_funcs[type_id](field, pos);
+end:
+       return ret;
+}
+
+static
+struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_type_integer *integer_type = container_of(type,
+               struct bt_ctf_field_type_integer, parent);
+       struct bt_ctf_field_integer *integer = g_new0(
+               struct bt_ctf_field_integer, 1);
+
+       if (integer) {
+               integer->definition.declaration = &integer_type->declaration;
+       }
+
+       return integer ? &integer->parent : NULL;
+}
+
+static
+struct bt_ctf_field *bt_ctf_field_enumeration_create(
+       struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_enumeration *enumeration = g_new0(
+               struct bt_ctf_field_enumeration, 1);
+
+       return enumeration ? &enumeration->parent : NULL;
+}
+
+static
+struct bt_ctf_field *bt_ctf_field_floating_point_create(
+       struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_floating_point *floating_point;
+       struct bt_ctf_field_type_floating_point *floating_point_type;
+
+       floating_point = g_new0(struct bt_ctf_field_floating_point, 1);
+       if (!floating_point) {
+               goto end;
+       }
+
+       floating_point_type = container_of(type,
+               struct bt_ctf_field_type_floating_point, parent);
+       floating_point->definition.declaration = container_of(
+               type->declaration, struct declaration_float, p);
+
+
+       floating_point->definition.sign = &floating_point->sign;
+       floating_point->sign.declaration = &floating_point_type->sign;
+       floating_point->definition.sign->p.declaration =
+               &floating_point_type->sign.p;
+
+       floating_point->definition.mantissa = &floating_point->mantissa;
+       floating_point->mantissa.declaration = &floating_point_type->mantissa;
+       floating_point->definition.mantissa->p.declaration =
+               &floating_point_type->mantissa.p;
+
+       floating_point->definition.exp = &floating_point->exp;
+       floating_point->exp.declaration = &floating_point_type->exp;
+       floating_point->definition.exp->p.declaration =
+               &floating_point_type->exp.p;
+
+end:
+       return floating_point ? &floating_point->parent : NULL;
+}
+
+static
+struct bt_ctf_field *bt_ctf_field_structure_create(
+       struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_type_structure *structure_type = container_of(type,
+               struct bt_ctf_field_type_structure, parent);
+       struct bt_ctf_field_structure *structure = g_new0(
+               struct bt_ctf_field_structure, 1);
+       struct bt_ctf_field *field = NULL;
+
+       if (!structure || !structure_type->fields->len) {
+               goto end;
+       }
+
+       structure->field_name_to_index = structure_type->field_name_to_index;
+       structure->fields = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)bt_ctf_field_put);
+       g_ptr_array_set_size(structure->fields,
+               g_hash_table_size(structure->field_name_to_index));
+       field = &structure->parent;
+end:
+       return field;
+}
+
+static
+struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_variant *variant = g_new0(
+               struct bt_ctf_field_variant, 1);
+       return variant ? &variant->parent : NULL;
+}
+
+static
+struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_array *array = g_new0(struct bt_ctf_field_array, 1);
+       struct bt_ctf_field_type_array *array_type;
+       unsigned int array_length;
+
+       if (!array || !type) {
+               goto error;
+       }
+
+       array_type = container_of(type, struct bt_ctf_field_type_array, parent);
+       array_length = array_type->length;
+       array->elements = g_ptr_array_new_full(array_length,
+               (GDestroyNotify)bt_ctf_field_put);
+       if (!array->elements) {
+               goto error;
+       }
+
+       g_ptr_array_set_size(array->elements, array_length);
+       return &array->parent;
+error:
+       g_free(array);
+       return NULL;
+}
+
+static
+struct bt_ctf_field *bt_ctf_field_sequence_create(
+       struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_sequence *sequence = g_new0(
+               struct bt_ctf_field_sequence, 1);
+       return sequence ? &sequence->parent : NULL;
+}
+
+static
+struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_string *string = g_new0(
+               struct bt_ctf_field_string, 1);
+       return string ? &string->parent : NULL;
+}
+
+static
+void bt_ctf_field_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field *field;
+       struct bt_ctf_field_type *type;
+       enum ctf_type_id type_id;
+
+       if (!ref) {
+               return;
+       }
+
+       field = container_of(ref, struct bt_ctf_field, ref_count);
+       type = field->type;
+       type_id = bt_ctf_field_type_get_type_id(type);
+       if (type_id <= CTF_TYPE_UNKNOWN ||
+               type_id >= NR_CTF_TYPES) {
+               return;
+       }
+
+       field_destroy_funcs[type_id](field);
+       if (type) {
+               bt_ctf_field_type_put(type);
+       }
+}
+
+static
+void bt_ctf_field_integer_destroy(struct bt_ctf_field *field)
+{
+       struct bt_ctf_field_integer *integer;
+
+       if (!field) {
+               return;
+       }
+
+       integer = container_of(field, struct bt_ctf_field_integer, parent);
+       g_free(integer);
+}
+
+static
+void bt_ctf_field_enumeration_destroy(struct bt_ctf_field *field)
+{
+       struct bt_ctf_field_enumeration *enumeration;
+
+       if (!field) {
+               return;
+       }
+
+       enumeration = container_of(field, struct bt_ctf_field_enumeration,
+               parent);
+       bt_ctf_field_put(enumeration->payload);
+       g_free(enumeration);
+}
+
+static
+void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field)
+{
+       struct bt_ctf_field_floating_point *floating_point;
+
+       if (!field) {
+               return;
+       }
+
+       floating_point = container_of(field, struct bt_ctf_field_floating_point,
+               parent);
+       g_free(floating_point);
+}
+
+static
+void bt_ctf_field_structure_destroy(struct bt_ctf_field *field)
+{
+       struct bt_ctf_field_structure *structure;
+
+       if (!field) {
+               return;
+       }
+
+       structure = container_of(field, struct bt_ctf_field_structure, parent);
+       g_ptr_array_free(structure->fields, TRUE);
+       g_free(structure);
+}
+
+static
+void bt_ctf_field_variant_destroy(struct bt_ctf_field *field)
+{
+       struct bt_ctf_field_variant *variant;
+
+       if (!field) {
+               return;
+       }
+
+       variant = container_of(field, struct bt_ctf_field_variant, parent);
+       bt_ctf_field_put(variant->tag);
+       bt_ctf_field_put(variant->payload);
+       g_free(variant);
+}
+
+static
+void bt_ctf_field_array_destroy(struct bt_ctf_field *field)
+{
+       struct bt_ctf_field_array *array;
+
+       if (!field) {
+               return;
+       }
+
+       array = container_of(field, struct bt_ctf_field_array, parent);
+       g_ptr_array_free(array->elements, TRUE);
+       g_free(array);
+}
+
+static
+void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field)
+{
+       struct bt_ctf_field_sequence *sequence;
+
+       if (!field) {
+               return;
+       }
+
+       sequence = container_of(field, struct bt_ctf_field_sequence, parent);
+       g_ptr_array_free(sequence->elements, TRUE);
+       bt_ctf_field_put(sequence->length);
+       g_free(sequence);
+}
+
+static
+void bt_ctf_field_string_destroy(struct bt_ctf_field *field)
+{
+       struct bt_ctf_field_string *string;
+       if (!field) {
+               return;
+       }
+
+       string = container_of(field, struct bt_ctf_field_string, parent);
+       g_string_free(string->payload, TRUE);
+       g_free(string);
+}
+
+static
+int bt_ctf_field_generic_validate(struct bt_ctf_field *field)
+{
+       return !(field && field->payload_set);
+}
+
+static
+int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field)
+{
+       int ret;
+       struct bt_ctf_field_enumeration *enumeration;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       enumeration = container_of(field, struct bt_ctf_field_enumeration,
+               parent);
+       if (!enumeration->payload) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_validate(enumeration->payload);
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_structure_validate(struct bt_ctf_field *field)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_structure *structure;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       structure = container_of(field, struct bt_ctf_field_structure, parent);
+       for (i = 0; i < structure->fields->len; i++) {
+               ret = bt_ctf_field_validate(structure->fields->pdata[i]);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_variant_validate(struct bt_ctf_field *field)
+{
+       int ret = 0;
+       struct bt_ctf_field_variant *variant;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       variant = container_of(field, struct bt_ctf_field_variant, parent);
+       ret = bt_ctf_field_validate(variant->payload);
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_array_validate(struct bt_ctf_field *field)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_array *array;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       array = container_of(field, struct bt_ctf_field_array, parent);
+       for (i = 0; i < array->elements->len; i++) {
+               ret = bt_ctf_field_validate(array->elements->pdata[i]);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_sequence_validate(struct bt_ctf_field *field)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_sequence *sequence;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       sequence = container_of(field, struct bt_ctf_field_sequence, parent);
+       for (i = 0; i < sequence->elements->len; i++) {
+               ret = bt_ctf_field_validate(sequence->elements->pdata[i]);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_integer_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos)
+{
+       int ret = 0;
+       struct bt_ctf_field_integer *integer = container_of(field,
+               struct bt_ctf_field_integer, parent);
+
+retry:
+       ret = ctf_integer_write(&pos->parent, &integer->definition.p);
+       if (ret == -EFAULT) {
+               /*
+                * The field is too large to fit in the current packet's
+                * remaining space. Bump the packet size and retry.
+                */
+               ret = increase_packet_size(pos);
+               if (ret) {
+                       goto end;
+               }
+               goto retry;
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_enumeration_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos)
+{
+       struct bt_ctf_field_enumeration *enumeration = container_of(
+               field, struct bt_ctf_field_enumeration, parent);
+
+       return bt_ctf_field_serialize(enumeration->payload, pos);
+}
+
+static
+int bt_ctf_field_floating_point_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos)
+{
+       int ret = 0;
+       struct bt_ctf_field_floating_point *floating_point = container_of(field,
+               struct bt_ctf_field_floating_point, parent);
+
+retry:
+       ret = ctf_float_write(&pos->parent, &floating_point->definition.p);
+       if (ret == -EFAULT) {
+               /*
+                * The field is too large to fit in the current packet's
+                * remaining space. Bump the packet size and retry.
+                */
+               ret = increase_packet_size(pos);
+               if (ret) {
+                       goto end;
+               }
+               goto retry;
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_structure_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_structure *structure = container_of(
+               field, struct bt_ctf_field_structure, parent);
+
+       while (!ctf_pos_access_ok(pos,
+               offset_align(pos->offset,
+                       field->type->declaration->alignment))) {
+               increase_packet_size(pos);
+       }
+
+       ctf_align_pos(pos, field->type->declaration->alignment);
+
+       for (i = 0; i < structure->fields->len; i++) {
+               struct bt_ctf_field *field = g_ptr_array_index(
+                       structure->fields, i);
+
+               ret = bt_ctf_field_serialize(field, pos);
+               if (ret) {
+                       break;
+               }
+       }
+
+       return ret;
+}
+
+static
+int bt_ctf_field_variant_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos)
+{
+       struct bt_ctf_field_variant *variant = container_of(
+               field, struct bt_ctf_field_variant, parent);
+
+       return bt_ctf_field_serialize(variant->payload, pos);
+}
+
+static
+int bt_ctf_field_array_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_array *array = container_of(
+               field, struct bt_ctf_field_array, parent);
+
+       for (i = 0; i < array->elements->len; i++) {
+               ret = bt_ctf_field_serialize(
+                       g_ptr_array_index(array->elements, i), pos);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_sequence_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_sequence *sequence = container_of(
+               field, struct bt_ctf_field_sequence, parent);
+
+       for (i = 0; i < sequence->elements->len; i++) {
+               ret = bt_ctf_field_serialize(
+                       g_ptr_array_index(sequence->elements, i), pos);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_string_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_string *string = container_of(field,
+               struct bt_ctf_field_string, parent);
+       struct bt_ctf_field_type *character_type =
+               get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
+       struct bt_ctf_field *character = bt_ctf_field_create(character_type);
+
+       for (i = 0; i < string->payload->len + 1; i++) {
+               ret = bt_ctf_field_unsigned_integer_set_value(character,
+                       (uint64_t) string->payload->str[i]);
+               if (ret) {
+                       goto end;
+               }
+
+               ret = bt_ctf_field_integer_serialize(character, pos);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       bt_ctf_field_put(character);
+       bt_ctf_field_type_put(character_type);
+       return ret;
+}
+
+static
+int increase_packet_size(struct ctf_stream_pos *pos)
+{
+       int ret;
+
+       assert(pos);
+       ret = munmap_align(pos->base_mma);
+       if (ret) {
+               goto end;
+       }
+
+       pos->packet_size += PACKET_LEN_INCREMENT;
+       ret = posix_fallocate(pos->fd, pos->mmap_offset,
+               pos->packet_size / CHAR_BIT);
+       if (ret) {
+               goto end;
+       }
+
+       pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
+               pos->flags, pos->fd, pos->mmap_offset);
+       if (pos->base_mma == MAP_FAILED) {
+               ret = -1;
+       }
+end:
+       return ret;
+}
diff --git a/formats/ctf/writer/event-types.c b/formats/ctf/writer/event-types.c
new file mode 100644 (file)
index 0000000..091b320
--- /dev/null
@@ -0,0 +1,1404 @@
+/*
+ * event-types.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/event-types.h>
+#include <babeltrace/ctf-writer/event-types-internal.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/compiler.h>
+#include <babeltrace/endian.h>
+#include <float.h>
+#include <inttypes.h>
+
+struct range_overlap_query {
+       int64_t range_start, range_end;
+       int overlaps;
+       GQuark mapping_name;
+};
+
+static
+void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *);
+static
+void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *);
+
+static
+void (* const type_destroy_funcs[])(struct bt_ctf_ref *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_destroy,
+       [CTF_TYPE_ENUM] =
+               bt_ctf_field_type_enumeration_destroy,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_type_floating_point_destroy,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_destroy,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_destroy,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_destroy,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_destroy,
+       [CTF_TYPE_STRING] = bt_ctf_field_type_string_destroy,
+};
+
+static
+void generic_field_type_freeze(struct bt_ctf_field_type *);
+static
+void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *);
+static
+void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *);
+static
+void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *);
+static
+void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *);
+static
+void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *);
+
+static
+type_freeze_func const type_freeze_funcs[] = {
+       [CTF_TYPE_INTEGER] = generic_field_type_freeze,
+       [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_freeze,
+       [CTF_TYPE_FLOAT] = generic_field_type_freeze,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_freeze,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_freeze,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_freeze,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_freeze,
+       [CTF_TYPE_STRING] = generic_field_type_freeze,
+};
+
+static
+int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *,
+               struct metadata_context *);
+static
+int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *,
+               struct metadata_context *);
+static
+int bt_ctf_field_type_floating_point_serialize(
+               struct bt_ctf_field_type *, struct metadata_context *);
+static
+int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *,
+               struct metadata_context *);
+static
+int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *,
+               struct metadata_context *);
+static
+int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *,
+               struct metadata_context *);
+static
+int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *,
+               struct metadata_context *);
+static
+int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *,
+               struct metadata_context *);
+
+static
+type_serialize_func const type_serialize_funcs[] = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_serialize,
+       [CTF_TYPE_ENUM] =
+               bt_ctf_field_type_enumeration_serialize,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_type_floating_point_serialize,
+       [CTF_TYPE_STRUCT] =
+               bt_ctf_field_type_structure_serialize,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_serialize,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_serialize,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_serialize,
+       [CTF_TYPE_STRING] = bt_ctf_field_type_string_serialize,
+};
+
+static
+void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *,
+               int byte_order);
+static
+void bt_ctf_field_type_floating_point_set_byte_order(
+               struct bt_ctf_field_type *, int byte_order);
+
+static
+void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *,
+       int) = {
+       [CTF_TYPE_INTEGER] =
+               bt_ctf_field_type_integer_set_byte_order,
+       [CTF_TYPE_FLOAT] =
+               bt_ctf_field_type_floating_point_set_byte_order,
+       [CTF_TYPE_ENUM ... CTF_TYPE_SEQUENCE] = NULL,
+};
+
+
+static
+void destroy_enumeration_mapping(struct enumeration_mapping *mapping)
+{
+       g_free(mapping);
+}
+
+static
+void destroy_structure_field(struct structure_field *field)
+{
+       if (field->type) {
+               bt_ctf_field_type_put(field->type);
+       }
+
+       g_free(field);
+}
+
+static
+void check_ranges_overlap(gpointer element, gpointer query)
+{
+       struct enumeration_mapping *mapping = element;
+       struct range_overlap_query *overlap_query = query;
+
+       if (mapping->range_start <= overlap_query->range_end
+               && overlap_query->range_start <= mapping->range_end) {
+               overlap_query->overlaps = 1;
+               overlap_query->mapping_name = mapping->string;
+       }
+
+       overlap_query->overlaps |=
+               mapping->string == overlap_query->mapping_name;
+}
+
+static
+void bt_ctf_field_type_init(struct bt_ctf_field_type *type)
+{
+       enum ctf_type_id type_id = type->declaration->id;
+       int ret;
+
+       assert(type && (type_id > CTF_TYPE_UNKNOWN) &&
+               (type_id < NR_CTF_TYPES));
+
+       bt_ctf_ref_init(&type->ref_count);
+       type->freeze = type_freeze_funcs[type_id];
+       type->serialize = type_serialize_funcs[type_id];
+       ret = bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_NATIVE);
+       assert(!ret);
+       type->declaration->alignment = 1;
+}
+
+static
+int add_structure_field(GPtrArray *fields,
+               GHashTable *field_name_to_index,
+               struct bt_ctf_field_type *field_type,
+               const char *field_name)
+{
+       int ret = 0;
+       GQuark name_quark = g_quark_from_string(field_name);
+       struct structure_field *field;
+
+       /* Make sure structure does not contain a field of the same name */
+       if (g_hash_table_contains(field_name_to_index,
+                       GUINT_TO_POINTER(name_quark))) {
+               ret = -1;
+               goto end;
+       }
+
+       field = g_new0(struct structure_field, 1);
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_type_get(field_type);
+       field->name = name_quark;
+       field->type = field_type;
+       g_hash_table_insert(field_name_to_index,
+               (gpointer) (unsigned long) name_quark,
+               (gpointer) (unsigned long) fields->len);
+       g_ptr_array_add(fields, field);
+       bt_ctf_field_type_freeze(field_type);
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size)
+{
+       struct bt_ctf_field_type_integer *integer =
+               g_new0(struct bt_ctf_field_type_integer, 1);
+
+       if (!integer || size > 64) {
+               return NULL;
+       }
+
+       integer->parent.declaration = &integer->declaration.p;
+       integer->parent.declaration->id = CTF_TYPE_INTEGER;
+       integer->declaration.len = size;
+       integer->declaration.base = BT_CTF_INTEGER_BASE_DECIMAL;
+       integer->declaration.encoding = CTF_STRING_NONE;
+       bt_ctf_field_type_init(&integer->parent);
+       return &integer->parent;
+}
+
+int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *type,
+               int is_signed)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_integer *integer;
+
+       if (!type || type->frozen ||
+               type->declaration->id != CTF_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+
+       integer = container_of(type, struct bt_ctf_field_type_integer, parent);
+       if (is_signed && integer->declaration.len <= 1) {
+               ret = -1;
+               goto end;
+       }
+
+       integer->declaration.signedness = !!is_signed;
+end:
+       return ret;
+}
+
+int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type,
+               enum bt_ctf_integer_base base)
+{
+       int ret = 0;
+
+       if (!type || type->frozen ||
+               type->declaration->id != CTF_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+
+       switch (base) {
+       case BT_CTF_INTEGER_BASE_BINARY:
+       case BT_CTF_INTEGER_BASE_OCTAL:
+       case BT_CTF_INTEGER_BASE_DECIMAL:
+       case BT_CTF_INTEGER_BASE_HEXADECIMAL:
+       {
+               struct bt_ctf_field_type_integer *integer = container_of(type,
+                       struct bt_ctf_field_type_integer, parent);
+               integer->declaration.base = base;
+               break;
+       }
+       default:
+               ret = -1;
+       }
+end:
+       return ret;
+}
+
+int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type,
+               enum ctf_string_encoding encoding)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_integer *integer;
+
+       if (!type || type->frozen ||
+               (type->declaration->id != CTF_TYPE_INTEGER) ||
+               (encoding < CTF_STRING_NONE) ||
+               (encoding >= CTF_STRING_UNKNOWN)) {
+               ret = -1;
+               goto end;
+       }
+
+       integer = container_of(type, struct bt_ctf_field_type_integer, parent);
+       integer->declaration.encoding = encoding;
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create(
+               struct bt_ctf_field_type *integer_container_type)
+{
+       struct bt_ctf_field_type_enumeration *enumeration = NULL;
+
+       if (!integer_container_type) {
+               goto error;
+       }
+
+       enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1);
+       if (!enumeration) {
+               goto error;
+       }
+
+       enumeration->parent.declaration = &enumeration->declaration.p;
+       enumeration->parent.declaration->id = CTF_TYPE_ENUM;
+       bt_ctf_field_type_get(integer_container_type);
+       enumeration->container = integer_container_type;
+       enumeration->entries = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)destroy_enumeration_mapping);
+       bt_ctf_field_type_init(&enumeration->parent);
+       return &enumeration->parent;
+error:
+       g_free(enumeration);
+       return NULL;
+}
+
+int bt_ctf_field_type_enumeration_add_mapping(
+               struct bt_ctf_field_type *type, const char *string,
+               int64_t range_start, int64_t range_end)
+{
+       int ret = 0;
+       GQuark mapping_name;
+       struct enumeration_mapping *mapping;
+       struct bt_ctf_field_type_enumeration *enumeration;
+       struct range_overlap_query query;
+
+       if (!type || (type->declaration->id != CTF_TYPE_ENUM) ||
+               type->frozen ||
+               (range_end < range_start)) {
+               ret = -1;
+               goto end;
+       }
+
+       if (validate_identifier(string)) {
+               ret = -1;
+               goto end;
+       }
+
+       mapping_name = g_quark_from_string(string);
+       query = (struct range_overlap_query) { .range_start = range_start,
+               .range_end = range_end,
+               .mapping_name = mapping_name,
+               .overlaps = 0 };
+       enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
+               parent);
+
+       /* Check that the range does not overlap with one already present */
+       g_ptr_array_foreach(enumeration->entries, check_ranges_overlap, &query);
+       if (query.overlaps) {
+               ret = -1;
+               goto end;
+       }
+
+       mapping = g_new(struct enumeration_mapping, 1);
+       if (!mapping) {
+               ret = -1;
+               goto end;
+       }
+
+       *mapping = (struct enumeration_mapping) {.range_start = range_start,
+               .range_end = range_end, .string = mapping_name};
+       g_ptr_array_add(enumeration->entries, mapping);
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void)
+{
+       struct bt_ctf_field_type_floating_point *floating_point =
+               g_new0(struct bt_ctf_field_type_floating_point, 1);
+
+       if (!floating_point) {
+               goto end;
+       }
+
+       floating_point->declaration.sign = &floating_point->sign;
+       floating_point->declaration.mantissa = &floating_point->mantissa;
+       floating_point->declaration.exp = &floating_point->exp;
+       floating_point->sign.len = 1;
+       floating_point->parent.declaration = &floating_point->declaration.p;
+       floating_point->parent.declaration->id = CTF_TYPE_FLOAT;
+       floating_point->declaration.exp->len =
+               sizeof(float) * CHAR_BIT - FLT_MANT_DIG;
+       floating_point->declaration.mantissa->len = FLT_MANT_DIG - 1;
+       floating_point->sign.p.alignment = 1;
+       floating_point->mantissa.p.alignment = 1;
+       floating_point->exp.p.alignment = 1;
+
+       bt_ctf_field_type_init(&floating_point->parent);
+end:
+       return floating_point ? &floating_point->parent : NULL;
+}
+
+int bt_ctf_field_type_floating_point_set_exponent_digits(
+               struct bt_ctf_field_type *type,
+               unsigned int exponent_digits)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_floating_point *floating_point;
+
+       if (!type || type->frozen ||
+               (type->declaration->id != CTF_TYPE_FLOAT)) {
+               ret = -1;
+               goto end;
+       }
+
+       floating_point = container_of(type,
+               struct bt_ctf_field_type_floating_point, parent);
+       if ((exponent_digits != sizeof(float) * CHAR_BIT - FLT_MANT_DIG) &&
+               (exponent_digits != sizeof(double) * CHAR_BIT - DBL_MANT_DIG) &&
+               (exponent_digits !=
+                       sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG)) {
+               ret = -1;
+               goto end;
+       }
+
+       floating_point->declaration.exp->len = exponent_digits;
+end:
+       return ret;
+}
+
+int bt_ctf_field_type_floating_point_set_mantissa_digits(
+               struct bt_ctf_field_type *type,
+               unsigned int mantissa_digits)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_floating_point *floating_point;
+
+       if (!type || type->frozen ||
+               (type->declaration->id != CTF_TYPE_FLOAT)) {
+               ret = -1;
+               goto end;
+       }
+
+       floating_point = container_of(type,
+               struct bt_ctf_field_type_floating_point, parent);
+
+       if ((mantissa_digits != FLT_MANT_DIG) &&
+               (mantissa_digits != DBL_MANT_DIG) &&
+               (mantissa_digits != LDBL_MANT_DIG)) {
+               ret = -1;
+               goto end;
+       }
+
+       floating_point->declaration.mantissa->len = mantissa_digits - 1;
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void)
+{
+       struct bt_ctf_field_type_structure *structure =
+               g_new0(struct bt_ctf_field_type_structure, 1);
+
+       if (!structure) {
+               goto error;
+       }
+
+       structure->parent.declaration = &structure->declaration.p;
+       structure->parent.declaration->id = CTF_TYPE_STRUCT;
+       bt_ctf_field_type_init(&structure->parent);
+       structure->fields = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)destroy_structure_field);
+       structure->field_name_to_index = g_hash_table_new(NULL, NULL);
+       return &structure->parent;
+error:
+       return NULL;
+}
+
+int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type,
+               struct bt_ctf_field_type *field_type,
+               const char *field_name)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_structure *structure;
+
+       if (!type || !field_type || type->frozen ||
+               validate_identifier(field_name) ||
+               (type->declaration->id != CTF_TYPE_STRUCT)) {
+               goto end;
+       }
+
+       structure = container_of(type,
+               struct bt_ctf_field_type_structure, parent);
+       if (add_structure_field(structure->fields,
+               structure->field_name_to_index, field_type, field_name)) {
+               ret = -1;
+               goto end;
+       }
+
+       if (type->declaration->alignment < field_type->declaration->alignment) {
+               type->declaration->alignment =
+                       field_type->declaration->alignment;
+       }
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_field_type_variant_create(
+       struct bt_ctf_field_type *enum_tag, const char *tag_name)
+{
+       struct bt_ctf_field_type_variant *variant = NULL;
+
+       if (!enum_tag || validate_identifier(tag_name) ||
+               (enum_tag->declaration->id != CTF_TYPE_ENUM)) {
+               goto error;
+       }
+
+       variant = g_new0(struct bt_ctf_field_type_variant, 1);
+       if (!variant) {
+               goto error;
+       }
+
+       variant->parent.declaration = &variant->declaration.p;
+       variant->parent.declaration->id = CTF_TYPE_VARIANT;
+       variant->tag_name = g_string_new(tag_name);
+       bt_ctf_field_type_init(&variant->parent);
+       variant->field_name_to_index = g_hash_table_new(NULL, NULL);
+       variant->fields = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)destroy_structure_field);
+       bt_ctf_field_type_get(enum_tag);
+       variant->tag = container_of(enum_tag,
+               struct bt_ctf_field_type_enumeration, parent);
+       return &variant->parent;
+error:
+       return NULL;
+}
+
+int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type,
+               struct bt_ctf_field_type *field_type,
+               const char *field_name)
+{
+       size_t i;
+       int ret = 0;
+       int name_found = 0;
+       struct bt_ctf_field_type_variant *variant;
+       GQuark field_name_quark = g_quark_from_string(field_name);
+
+       if (!type || !field_type || type->frozen ||
+               validate_identifier(field_name) ||
+               (type->declaration->id != CTF_TYPE_VARIANT)) {
+               ret = -1;
+               goto end;
+       }
+
+       variant = container_of(type, struct bt_ctf_field_type_variant, parent);
+       /* Make sure this name is present in the enum tag */
+       for (i = 0; i < variant->tag->entries->len; i++) {
+               struct enumeration_mapping *mapping =
+                       g_ptr_array_index(variant->tag->entries, i);
+
+               if (mapping->string == field_name_quark) {
+                       name_found = 1;
+                       break;
+               }
+       }
+
+       if (!name_found || add_structure_field(variant->fields,
+               variant->field_name_to_index, field_type, field_name)) {
+               ret = -1;
+               goto end;
+       }
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type *bt_ctf_field_type_array_create(
+               struct bt_ctf_field_type *element_type,
+               unsigned int length)
+{
+       struct bt_ctf_field_type_array *array = NULL;
+
+       if (!element_type || length == 0) {
+               goto error;
+       }
+
+       array = g_new0(struct bt_ctf_field_type_array, 1);
+       if (!array) {
+               goto error;
+       }
+
+       array->parent.declaration = &array->declaration.p;
+       array->parent.declaration->id = CTF_TYPE_ARRAY;
+       bt_ctf_field_type_init(&array->parent);
+       bt_ctf_field_type_get(element_type);
+       array->element_type = element_type;
+       array->length = length;
+       array->parent.declaration->alignment =
+               element_type->declaration->alignment;
+       return &array->parent;
+error:
+       return NULL;
+}
+
+struct bt_ctf_field_type *bt_ctf_field_type_sequence_create(
+               struct bt_ctf_field_type *element_type,
+               const char *length_field_name)
+{
+       struct bt_ctf_field_type_sequence *sequence = NULL;
+
+       if (!element_type || validate_identifier(length_field_name)) {
+               goto error;
+       }
+
+       sequence = g_new0(struct bt_ctf_field_type_sequence, 1);
+       if (!sequence) {
+               goto error;
+       }
+
+       sequence->parent.declaration = &sequence->declaration.p;
+       sequence->parent.declaration->id = CTF_TYPE_SEQUENCE;
+       bt_ctf_field_type_init(&sequence->parent);
+       bt_ctf_field_type_get(element_type);
+       sequence->element_type = element_type;
+       sequence->length_field_name = g_string_new(length_field_name);
+       sequence->parent.declaration->alignment =
+               element_type->declaration->alignment;
+       return &sequence->parent;
+error:
+       return NULL;
+}
+
+struct bt_ctf_field_type *bt_ctf_field_type_string_create(void)
+{
+       struct bt_ctf_field_type_string *string =
+               g_new0(struct bt_ctf_field_type_string, 1);
+
+       if (!string) {
+               return NULL;
+       }
+
+       string->parent.declaration = &string->declaration.p;
+       string->parent.declaration->id = CTF_TYPE_STRING;
+       bt_ctf_field_type_init(&string->parent);
+       string->declaration.encoding = CTF_STRING_UTF8;
+       string->parent.declaration->alignment = CHAR_BIT;
+       return &string->parent;
+}
+
+int bt_ctf_field_type_string_set_encoding(
+               struct bt_ctf_field_type *type,
+               enum ctf_string_encoding encoding)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_string *string;
+
+       if (!type || type->declaration->id != CTF_TYPE_STRING ||
+               (encoding != CTF_STRING_UTF8 &&
+               encoding != CTF_STRING_ASCII)) {
+               ret = -1;
+               goto end;
+       }
+
+       string = container_of(type, struct bt_ctf_field_type_string, parent);
+       string->declaration.encoding = encoding;
+end:
+       return ret;
+}
+
+int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type,
+               unsigned int alignment)
+{
+       int ret = 0;
+
+       /* Alignment must be bit-aligned (1) or byte aligned */
+       if (!type || type->frozen || (alignment != 1 && (alignment & 0x7))) {
+               ret = -1;
+               goto end;
+       }
+
+       if (type->declaration->id == CTF_TYPE_STRING &&
+               alignment != CHAR_BIT) {
+               ret = -1;
+               goto end;
+       }
+
+       type->declaration->alignment = alignment;
+       ret = 0;
+end:
+       return ret;
+}
+
+int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type,
+               enum bt_ctf_byte_order byte_order)
+{
+       int ret = 0;
+       int internal_byte_order;
+       enum ctf_type_id type_id;
+
+       if (!type || type->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       type_id = type->declaration->id;
+       switch (byte_order) {
+       case BT_CTF_BYTE_ORDER_NATIVE:
+               internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN ?
+                       LITTLE_ENDIAN : BIG_ENDIAN);
+               break;
+       case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
+               internal_byte_order = LITTLE_ENDIAN;
+               break;
+       case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
+       case BT_CTF_BYTE_ORDER_NETWORK:
+               internal_byte_order = BIG_ENDIAN;
+               break;
+       default:
+               ret = -1;
+               goto end;
+       }
+
+       if (set_byte_order_funcs[type_id]) {
+               set_byte_order_funcs[type_id](type, internal_byte_order);
+       }
+end:
+       return ret;
+}
+
+void bt_ctf_field_type_get(struct bt_ctf_field_type *type)
+{
+       if (!type) {
+               return;
+       }
+
+       bt_ctf_ref_get(&type->ref_count);
+}
+
+void bt_ctf_field_type_put(struct bt_ctf_field_type *type)
+{
+       enum ctf_type_id type_id;
+
+       if (!type) {
+               return;
+       }
+
+       type_id = type->declaration->id;
+       assert(type_id > CTF_TYPE_UNKNOWN && type_id < NR_CTF_TYPES);
+       bt_ctf_ref_put(&type->ref_count, type_destroy_funcs[type_id]);
+}
+
+BT_HIDDEN
+void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type)
+{
+       if (!type) {
+               return;
+       }
+
+       type->freeze(type);
+}
+
+BT_HIDDEN
+enum ctf_type_id bt_ctf_field_type_get_type_id(
+               struct bt_ctf_field_type *type)
+{
+       if (!type) {
+               return CTF_TYPE_UNKNOWN;
+       }
+
+       return type->declaration->id;
+}
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_structure_get_type(
+               struct bt_ctf_field_type_structure *structure,
+               const char *name)
+{
+       struct bt_ctf_field_type *type = NULL;
+       struct structure_field *field;
+       GQuark name_quark = g_quark_try_string(name);
+       size_t index;
+
+       if (!name_quark) {
+               goto end;
+       }
+
+       if (!g_hash_table_lookup_extended(structure->field_name_to_index,
+               GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
+               goto end;
+       }
+
+       field = structure->fields->pdata[index];
+       type = field->type;
+end:
+       return type;
+}
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type(
+               struct bt_ctf_field_type_array *array)
+{
+       assert(array);
+       return array->element_type;
+}
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type(
+               struct bt_ctf_field_type_sequence *sequence)
+{
+       assert(sequence);
+       return sequence->element_type;
+}
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type(
+               struct bt_ctf_field_type_variant *variant,
+               int64_t tag_value)
+{
+       struct bt_ctf_field_type *type = NULL;
+       GQuark field_name_quark;
+       gpointer index;
+       struct structure_field *field_entry;
+       struct range_overlap_query query = {.range_start = tag_value,
+               .range_end = tag_value, .mapping_name = 0, .overlaps = 0};
+
+       g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap,
+               &query);
+       if (!query.overlaps) {
+               goto end;
+       }
+
+       field_name_quark = query.mapping_name;
+       if (!g_hash_table_lookup_extended(variant->field_name_to_index,
+               GUINT_TO_POINTER(field_name_quark), NULL, &index)) {
+               goto end;
+       }
+
+       field_entry = g_ptr_array_index(variant->fields, (size_t)index);
+       type = field_entry->type;
+end:
+       return type;
+}
+
+BT_HIDDEN
+int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context)
+{
+       int ret;
+
+       if (!type || !context) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = type->serialize(type, context);
+end:
+       return ret;
+}
+
+static
+void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_integer *integer;
+
+       if (!ref) {
+               return;
+       }
+
+       integer = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_integer, parent);
+       g_free(integer);
+}
+
+static
+void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_enumeration *enumeration;
+
+       if (!ref) {
+               return;
+       }
+
+       enumeration = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_enumeration, parent);
+       g_ptr_array_free(enumeration->entries, TRUE);
+       bt_ctf_field_type_put(enumeration->container);
+       g_free(enumeration);
+}
+
+static
+void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_floating_point *floating_point;
+
+       if (!ref) {
+               return;
+       }
+
+       floating_point = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_floating_point, parent);
+       g_free(floating_point);
+}
+
+static
+void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_structure *structure;
+
+       if (!ref) {
+               return;
+       }
+
+       structure = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_structure, parent);
+       g_ptr_array_free(structure->fields, TRUE);
+       g_hash_table_destroy(structure->field_name_to_index);
+       g_free(structure);
+}
+
+static
+void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_variant *variant;
+
+       if (!ref) {
+               return;
+       }
+
+       variant = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_variant, parent);
+       g_ptr_array_free(variant->fields, TRUE);
+       g_hash_table_destroy(variant->field_name_to_index);
+       g_string_free(variant->tag_name, TRUE);
+       bt_ctf_field_type_put(&variant->tag->parent);
+       g_free(variant);
+}
+
+static
+void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_array *array;
+
+       if (!ref) {
+               return;
+       }
+
+       array = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_array, parent);
+       bt_ctf_field_type_put(array->element_type);
+       g_free(array);
+}
+
+static
+void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_sequence *sequence;
+
+       if (!ref) {
+               return;
+       }
+
+       sequence = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_sequence, parent);
+       bt_ctf_field_type_put(sequence->element_type);
+       g_string_free(sequence->length_field_name, TRUE);
+       g_free(sequence);
+}
+
+static
+void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_field_type_string *string;
+
+       if (!ref) {
+               return;
+       }
+
+       string = container_of(
+               container_of(ref, struct bt_ctf_field_type, ref_count),
+               struct bt_ctf_field_type_string, parent);
+       g_free(string);
+}
+
+static
+void generic_field_type_freeze(struct bt_ctf_field_type *type)
+{
+       type->frozen = 1;
+}
+
+static
+void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_type_enumeration *enumeration_type = container_of(
+               type, struct bt_ctf_field_type_enumeration, parent);
+
+       generic_field_type_freeze(type);
+       bt_ctf_field_type_freeze(enumeration_type->container);
+}
+
+static
+void freeze_structure_field(struct structure_field *field)
+{
+       bt_ctf_field_type_freeze(field->type);
+}
+
+static
+void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_type_structure *structure_type = container_of(
+               type, struct bt_ctf_field_type_structure, parent);
+
+       generic_field_type_freeze(type);
+       g_ptr_array_foreach(structure_type->fields, (GFunc)freeze_structure_field,
+               NULL);
+}
+
+static
+void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_type_variant *variant_type = container_of(
+               type, struct bt_ctf_field_type_variant, parent);
+
+       generic_field_type_freeze(type);
+       g_ptr_array_foreach(variant_type->fields, (GFunc)freeze_structure_field,
+               NULL);
+}
+
+static
+void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_type_array *array_type = container_of(
+               type, struct bt_ctf_field_type_array, parent);
+
+       generic_field_type_freeze(type);
+       bt_ctf_field_type_freeze(array_type->element_type);
+}
+
+static
+void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *type)
+{
+       struct bt_ctf_field_type_sequence *sequence_type = container_of(
+               type, struct bt_ctf_field_type_sequence, parent);
+
+       generic_field_type_freeze(type);
+       bt_ctf_field_type_freeze(sequence_type->element_type);
+}
+
+static
+const char *get_encoding_string(enum ctf_string_encoding encoding)
+{
+       const char *encoding_string;
+
+       switch (encoding) {
+       case CTF_STRING_NONE:
+               encoding_string = "none";
+               break;
+       case CTF_STRING_ASCII:
+               encoding_string = "ASCII";
+               break;
+       case CTF_STRING_UTF8:
+               encoding_string = "UTF8";
+               break;
+       default:
+               encoding_string = "unknown";
+               break;
+       }
+
+       return encoding_string;
+}
+
+static
+const char *get_integer_base_string(enum bt_ctf_integer_base base)
+{
+       const char *base_string;
+
+       switch (base) {
+       case BT_CTF_INTEGER_BASE_DECIMAL:
+               base_string = "decimal";
+               break;
+       case BT_CTF_INTEGER_BASE_HEXADECIMAL:
+               base_string = "hexadecimal";
+               break;
+       case BT_CTF_INTEGER_BASE_OCTAL:
+               base_string = "octal";
+               break;
+       case BT_CTF_INTEGER_BASE_BINARY:
+               base_string = "binary";
+               break;
+       default:
+               base_string = "unknown";
+               break;
+       }
+
+       return base_string;
+}
+
+static
+int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context)
+{
+       struct bt_ctf_field_type_integer *integer = container_of(type,
+               struct bt_ctf_field_type_integer, parent);
+
+       g_string_append_printf(context->string,
+               "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s; }",
+               integer->declaration.len, type->declaration->alignment,
+               (integer->declaration.signedness ? "true" : "false"),
+               get_encoding_string(integer->declaration.encoding),
+               get_integer_base_string(integer->declaration.base),
+               get_byte_order_string(integer->declaration.byte_order));
+       return 0;
+}
+
+static
+int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context)
+{
+       size_t entry;
+       int ret = 0;
+       struct bt_ctf_field_type_enumeration *enumeration = container_of(type,
+               struct bt_ctf_field_type_enumeration, parent);
+
+       g_string_append(context->string, "enum : ");
+       ret = bt_ctf_field_type_serialize(enumeration->container, context);
+       if (ret) {
+               goto end;
+       }
+
+       g_string_append(context->string, " { ");
+       for (entry = 0; entry < enumeration->entries->len; entry++) {
+               struct enumeration_mapping *mapping =
+                       enumeration->entries->pdata[entry];
+
+               if (mapping->range_start == mapping->range_end) {
+                       g_string_append_printf(context->string, "%s = %" PRId64,
+                               g_quark_to_string(mapping->string),
+                               mapping->range_start);
+               } else {
+                       g_string_append_printf(context->string,
+                               "%s = %" PRId64 " ... %" PRId64,
+                               g_quark_to_string(mapping->string),
+                               mapping->range_start, mapping->range_end);
+               }
+
+               g_string_append(context->string,
+                       ((entry != (enumeration->entries->len - 1)) ?
+                       ", " : " }"));
+       }
+
+       if (context->field_name->len) {
+               g_string_append_printf(context->string, " %s",
+                       context->field_name->str);
+               g_string_assign(context->field_name, "");
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context)
+{
+       struct bt_ctf_field_type_floating_point *floating_point = container_of(
+               type, struct bt_ctf_field_type_floating_point, parent);
+
+       g_string_append_printf(context->string,
+               "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
+               floating_point->declaration.exp->len,
+               floating_point->declaration.mantissa->len + 1,
+               get_byte_order_string(floating_point->declaration.byte_order),
+               type->declaration->alignment);
+       return 0;
+}
+
+static
+int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context)
+{
+       size_t i;
+       unsigned int indent;
+       int ret = 0;
+       struct bt_ctf_field_type_structure *structure = container_of(type,
+               struct bt_ctf_field_type_structure, parent);
+       GString *structure_field_name = context->field_name;
+
+       context->field_name = g_string_new("");
+
+       context->current_indentation_level++;
+       g_string_append(context->string, "struct {\n");
+
+       for (i = 0; i < structure->fields->len; i++) {
+               struct structure_field *field;
+
+               for (indent = 0; indent < context->current_indentation_level;
+                       indent++) {
+                       g_string_append_c(context->string, '\t');
+               }
+
+               field = structure->fields->pdata[i];
+               g_string_assign(context->field_name,
+                       g_quark_to_string(field->name));
+               ret = bt_ctf_field_type_serialize(field->type, context);
+               if (ret) {
+                       goto end;
+               }
+
+               if (context->field_name->len) {
+                       g_string_append_printf(context->string, " %s",
+                               context->field_name->str);
+               }
+               g_string_append(context->string, ";\n");
+       }
+
+       context->current_indentation_level--;
+       for (indent = 0; indent < context->current_indentation_level;
+               indent++) {
+               g_string_append_c(context->string, '\t');
+       }
+
+       g_string_append_printf(context->string, "} align(%zu)",
+                type->declaration->alignment);
+end:
+       g_string_free(context->field_name, TRUE);
+       context->field_name = structure_field_name;
+       return ret;
+}
+
+static
+int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context)
+{
+       size_t i;
+       unsigned int indent;
+       int ret = 0;
+       struct bt_ctf_field_type_variant *variant = container_of(
+               type, struct bt_ctf_field_type_variant, parent);
+       GString *variant_field_name = context->field_name;
+
+       context->field_name = g_string_new("");
+       g_string_append_printf(context->string,
+               "variant <%s> {\n", variant->tag_name->str);
+       context->current_indentation_level++;
+       for (i = 0; i < variant->fields->len; i++) {
+               struct structure_field *field = variant->fields->pdata[i];
+
+               g_string_assign(context->field_name,
+                       g_quark_to_string(field->name));
+               for (indent = 0; indent < context->current_indentation_level;
+                       indent++) {
+                       g_string_append_c(context->string, '\t');
+               }
+
+               g_string_assign(context->field_name,
+                       g_quark_to_string(field->name));
+               ret = bt_ctf_field_type_serialize(field->type, context);
+               if (ret) {
+                       goto end;
+               }
+
+               if (context->field_name->len) {
+                       g_string_append_printf(context->string, " %s;",
+                               context->field_name->str);
+               }
+
+               g_string_append_c(context->string, '\n');
+       }
+
+       context->current_indentation_level--;
+       for (indent = 0; indent < context->current_indentation_level;
+               indent++) {
+               g_string_append_c(context->string, '\t');
+       }
+
+       g_string_append(context->string, "}");
+end:
+       g_string_free(context->field_name, TRUE);
+       context->field_name = variant_field_name;
+       return ret;
+}
+
+static
+int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_array *array = container_of(type,
+               struct bt_ctf_field_type_array, parent);
+
+       ret = bt_ctf_field_type_serialize(array->element_type, context);
+       if (ret) {
+               goto end;
+       }
+
+       if (context->field_name->len) {
+               g_string_append_printf(context->string, " %s[%u]",
+                       context->field_name->str, array->length);
+               g_string_assign(context->field_name, "");
+       } else {
+               g_string_append_printf(context->string, "[%u]", array->length);
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context)
+{
+       int ret = 0;
+       struct bt_ctf_field_type_sequence *sequence = container_of(
+               type, struct bt_ctf_field_type_sequence, parent);
+
+       ret = bt_ctf_field_type_serialize(sequence->element_type, context);
+       if (ret) {
+               goto end;
+       }
+
+       if (context->field_name->len) {
+               g_string_append_printf(context->string, " %s[%s]",
+                       context->field_name->str,
+                       sequence->length_field_name->str);
+               g_string_assign(context->field_name, "");
+       } else {
+               g_string_append_printf(context->string, "[%s]",
+                       sequence->length_field_name->str);
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context)
+{
+       struct bt_ctf_field_type_string *string = container_of(
+               type, struct bt_ctf_field_type_string, parent);
+
+       g_string_append_printf(context->string,
+               "string { encoding = %s; }",
+               get_encoding_string(string->declaration.encoding));
+       return 0;
+}
+
+static
+void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type,
+               int byte_order)
+{
+       struct bt_ctf_field_type_integer *integer_type = container_of(type,
+               struct bt_ctf_field_type_integer, parent);
+
+       integer_type->declaration.byte_order = byte_order;
+}
+
+static
+void bt_ctf_field_type_floating_point_set_byte_order(
+               struct bt_ctf_field_type *type, int byte_order)
+{
+       struct bt_ctf_field_type_floating_point *floating_point_type =
+               container_of(type, struct bt_ctf_field_type_floating_point,
+               parent);
+
+       floating_point_type->declaration.byte_order = byte_order;
+       floating_point_type->sign.byte_order = byte_order;
+       floating_point_type->mantissa.byte_order = byte_order;
+       floating_point_type->exp.byte_order = byte_order;
+}
diff --git a/formats/ctf/writer/event.c b/formats/ctf/writer/event.c
new file mode 100644 (file)
index 0000000..c7f1b22
--- /dev/null
@@ -0,0 +1,368 @@
+/*
+ * event.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/event.h>
+#include <babeltrace/ctf-writer/event-types.h>
+#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/ctf-writer/event-fields-internal.h>
+#include <babeltrace/ctf-writer/event-types-internal.h>
+#include <babeltrace/ctf-writer/event-internal.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/compiler.h>
+
+static
+void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref);
+static
+void bt_ctf_event_destroy(struct bt_ctf_ref *ref);
+
+struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
+{
+       struct bt_ctf_event_class *event_class = NULL;
+
+       if (validate_identifier(name)) {
+               goto end;
+       }
+
+       event_class = g_new0(struct bt_ctf_event_class, 1);
+       if (!event_class) {
+               goto end;
+       }
+
+       bt_ctf_ref_init(&event_class->ref_count);
+       event_class->name = g_quark_from_string(name);
+end:
+       return event_class;
+}
+
+int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
+               struct bt_ctf_field_type *type,
+               const char *name)
+{
+       int ret = 0;
+
+       if (!event_class || !type || validate_identifier(name) ||
+               event_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!event_class->fields) {
+               event_class->fields = bt_ctf_field_type_structure_create();
+               if (!event_class->fields) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(event_class->fields,
+               type, name);
+end:
+       return ret;
+}
+
+void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
+{
+       if (!event_class) {
+               return;
+       }
+
+       bt_ctf_ref_get(&event_class->ref_count);
+}
+
+void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
+{
+       if (!event_class) {
+               return;
+       }
+
+       bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy);
+}
+
+struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
+{
+       struct bt_ctf_event *event = NULL;
+
+       if (!event_class) {
+               goto end;
+       }
+
+       event = g_new0(struct bt_ctf_event, 1);
+       if (!event) {
+               goto end;
+       }
+
+       bt_ctf_ref_init(&event->ref_count);
+       bt_ctf_event_class_get(event_class);
+       bt_ctf_event_class_freeze(event_class);
+       event->event_class = event_class;
+       event->context_payload = bt_ctf_field_create(event_class->context);
+       event->fields_payload = bt_ctf_field_create(event_class->fields);
+end:
+       return event;
+}
+
+int bt_ctf_event_set_payload(struct bt_ctf_event *event,
+               const char *name,
+               struct bt_ctf_field *value)
+{
+       int ret = 0;
+
+       if (!event || !value || validate_identifier(name)) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_structure_set_field(event->fields_payload,
+               name, value);
+end:
+       return ret;
+}
+
+
+struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
+               const char *name)
+{
+       struct bt_ctf_field *field = NULL;
+
+       if (!event || !name) {
+               goto end;
+       }
+
+       field = bt_ctf_field_structure_get_field(event->fields_payload, name);
+end:
+       return field;
+}
+
+void bt_ctf_event_get(struct bt_ctf_event *event)
+{
+       if (!event) {
+               return;
+       }
+
+       bt_ctf_ref_get(&event->ref_count);
+}
+
+void bt_ctf_event_put(struct bt_ctf_event *event)
+{
+       if (!event) {
+               return;
+       }
+
+       bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy);
+}
+
+static
+void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_event_class *event_class;
+
+       if (!ref) {
+               return;
+       }
+
+       event_class = container_of(ref, struct bt_ctf_event_class, ref_count);
+       bt_ctf_field_type_put(event_class->context);
+       bt_ctf_field_type_put(event_class->fields);
+       g_free(event_class);
+}
+
+static
+void bt_ctf_event_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_event *event;
+
+       if (!ref) {
+               return;
+       }
+
+       event = container_of(ref, struct bt_ctf_event,
+               ref_count);
+       bt_ctf_event_class_put(event->event_class);
+       bt_ctf_field_put(event->context_payload);
+       bt_ctf_field_put(event->fields_payload);
+       g_free(event);
+}
+
+BT_HIDDEN
+void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class)
+{
+       assert(event_class);
+       event_class->frozen = 1;
+       bt_ctf_field_type_freeze(event_class->context);
+       bt_ctf_field_type_freeze(event_class->fields);
+}
+
+BT_HIDDEN
+int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
+               uint32_t id)
+{
+       int ret = 0;
+
+       if (event_class->id_set && id != event_class->id) {
+               ret = -1;
+               goto end;
+       }
+
+       event_class->id = id;
+       event_class->id_set = 1;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+uint32_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
+{
+       assert(event_class);
+       return event_class->id;
+}
+
+BT_HIDDEN
+int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
+               uint32_t id)
+{
+       int ret = 0;
+
+       assert(event_class);
+       if (event_class->stream_id_set && id != event_class->stream_id) {
+               ret = -1;
+               goto end;
+       }
+
+       event_class->stream_id = id;
+       event_class->stream_id_set = 1;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
+               struct metadata_context *context)
+{
+       int ret = 0;
+
+       assert(event_class);
+       assert(context);
+       context->current_indentation_level = 1;
+       g_string_assign(context->field_name, "");
+       g_string_append_printf(context->string, "event {\n\tname = \"%s\";\n\tid = %u;\n\tstream_id = %" PRIu32 ";\n",
+               g_quark_to_string(event_class->name),
+               event_class->id,
+               event_class->stream_id);
+
+       if (event_class->context) {
+               g_string_append(context->string, "\tcontext := ");
+               ret = bt_ctf_field_type_serialize(event_class->context,
+                       context);
+               if (ret) {
+                       goto end;
+               }
+               g_string_append(context->string, ";\n");
+       }
+
+       if (event_class->fields) {
+               g_string_append(context->string, "\tfields := ");
+               ret = bt_ctf_field_type_serialize(event_class->fields, context);
+               if (ret) {
+                       goto end;
+               }
+               g_string_append(context->string, ";\n");
+       }
+
+       g_string_append(context->string, "};\n\n");
+end:
+       context->current_indentation_level = 0;
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_event_validate(struct bt_ctf_event *event)
+{
+       /* Make sure each field's payload has been set */
+       int ret;
+
+       assert(event);
+       ret = bt_ctf_field_validate(event->fields_payload);
+       if (ret) {
+               goto end;
+       }
+
+       if (event->event_class->context) {
+               ret = bt_ctf_field_validate(event->context_payload);
+       }
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_event_serialize(struct bt_ctf_event *event,
+               struct ctf_stream_pos *pos)
+{
+       int ret = 0;
+
+       assert(event);
+       assert(pos);
+       if (event->context_payload) {
+               ret = bt_ctf_field_serialize(event->context_payload, pos);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       if (event->fields_payload) {
+               ret = bt_ctf_field_serialize(event->fields_payload, pos);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_event_set_timestamp(struct bt_ctf_event *event,
+               uint64_t timestamp)
+{
+       int ret = 0;
+
+       assert(event);
+       if (event->timestamp) {
+               ret = -1;
+               goto end;
+       }
+
+       event->timestamp = timestamp;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event *event)
+{
+       assert(event);
+       return event->timestamp;
+}
diff --git a/formats/ctf/writer/functor.c b/formats/ctf/writer/functor.c
new file mode 100644 (file)
index 0000000..9c932cf
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * functor.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <glib.h>
+#include <babeltrace/ctf-writer/functor-internal.h>
+
+BT_HIDDEN
+void value_exists(gpointer element, gpointer search_query)
+{
+       if (element == ((struct search_query *)search_query)->value) {
+               ((struct search_query *)search_query)->found = 1;
+       }
+}
diff --git a/formats/ctf/writer/stream.c b/formats/ctf/writer/stream.c
new file mode 100644 (file)
index 0000000..f03f170
--- /dev/null
@@ -0,0 +1,697 @@
+/*
+ * stream.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/clock.h>
+#include <babeltrace/ctf-writer/clock-internal.h>
+#include <babeltrace/ctf-writer/event.h>
+#include <babeltrace/ctf-writer/event-internal.h>
+#include <babeltrace/ctf-writer/event-types-internal.h>
+#include <babeltrace/ctf-writer/event-fields-internal.h>
+#include <babeltrace/ctf-writer/stream.h>
+#include <babeltrace/ctf-writer/stream-internal.h>
+#include <babeltrace/ctf-writer/functor-internal.h>
+#include <babeltrace/compiler.h>
+#include <babeltrace/align.h>
+
+static
+void bt_ctf_stream_destroy(struct bt_ctf_ref *ref);
+static
+void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref);
+static
+int init_event_header(struct bt_ctf_stream_class *stream_class,
+               enum bt_ctf_byte_order byte_order);
+static
+int init_packet_context(struct bt_ctf_stream_class *stream_class,
+               enum bt_ctf_byte_order byte_order);
+static
+int set_structure_field_integer(struct bt_ctf_field *, char *, uint64_t);
+
+struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name)
+{
+       struct bt_ctf_stream_class *stream_class = NULL;
+
+       if (!name || !strlen(name)) {
+               goto error;
+       }
+
+       stream_class = g_new0(struct bt_ctf_stream_class, 1);
+       if (!stream_class) {
+               goto error;
+       }
+
+       stream_class->name = g_string_new(name);
+       stream_class->event_classes = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)bt_ctf_event_class_put);
+       if (!stream_class->event_classes) {
+               goto error_destroy;
+       }
+
+       bt_ctf_ref_init(&stream_class->ref_count);
+       return stream_class;
+
+error_destroy:
+       bt_ctf_stream_class_destroy(&stream_class->ref_count);
+       stream_class = NULL;
+error:
+       return stream_class;
+}
+
+int bt_ctf_stream_class_set_clock(struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_clock *clock)
+{
+       int ret = 0;
+
+       if (!stream_class || !clock || stream_class->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       if (stream_class->clock) {
+               bt_ctf_clock_put(stream_class->clock);
+       }
+
+       stream_class->clock = clock;
+       bt_ctf_clock_get(clock);
+end:
+       return ret;
+}
+
+int bt_ctf_stream_class_add_event_class(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_event_class *event_class)
+{
+       int ret = 0;
+
+       if (!stream_class || !event_class) {
+               ret = -1;
+               goto end;
+       }
+
+       /* Check for duplicate event classes */
+       struct search_query query = { .value = event_class, .found = 0 };
+       g_ptr_array_foreach(stream_class->event_classes, value_exists, &query);
+       if (query.found) {
+               ret = -1;
+               goto end;
+       }
+
+       if (bt_ctf_event_class_set_id(event_class,
+               stream_class->next_event_id++)) {
+               /* The event is already associated to a stream class */
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_event_class_get(event_class);
+       g_ptr_array_add(stream_class->event_classes, event_class);
+end:
+       return ret;
+}
+
+void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class)
+{
+       if (!stream_class) {
+               return;
+       }
+
+       bt_ctf_ref_get(&stream_class->ref_count);
+}
+
+void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class)
+{
+       if (!stream_class) {
+               return;
+       }
+
+       bt_ctf_ref_put(&stream_class->ref_count, bt_ctf_stream_class_destroy);
+}
+
+BT_HIDDEN
+void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class)
+{
+       if (!stream_class) {
+               return;
+       }
+
+       stream_class->frozen = 1;
+       bt_ctf_clock_freeze(stream_class->clock);
+       g_ptr_array_foreach(stream_class->event_classes,
+               (GFunc)bt_ctf_event_class_freeze, NULL);
+}
+
+BT_HIDDEN
+int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
+       uint32_t id)
+{
+       int ret = 0;
+
+       if (!stream_class ||
+               (stream_class->id_set && (id != stream_class->id))) {
+               ret = -1;
+               goto end;
+       }
+
+       stream_class->id = id;
+       stream_class->id_set = 1;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
+       enum bt_ctf_byte_order byte_order)
+{
+       int ret = 0;
+
+       ret = init_packet_context(stream_class, byte_order);
+       if (ret) {
+               goto end;
+       }
+
+       ret = init_event_header(stream_class, byte_order);
+       if (ret) {
+               goto end;
+       }
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
+               struct metadata_context *context)
+{
+       int ret = 0;
+       size_t i;
+
+       g_string_assign(context->field_name, "");
+       context->current_indentation_level = 1;
+       if (!stream_class->id_set) {
+               ret = -1;
+               goto end;
+       }
+
+       g_string_append_printf(context->string,
+               "stream {\n\tid = %" PRIu32 ";\n\tevent.header := ",
+               stream_class->id);
+       ret = bt_ctf_field_type_serialize(stream_class->event_header_type,
+               context);
+       if (ret) {
+               goto end;
+       }
+
+       g_string_append(context->string, ";\n\n\tpacket.context := ");
+       ret = bt_ctf_field_type_serialize(stream_class->packet_context_type,
+               context);
+       if (ret) {
+               goto end;
+       }
+
+       if (stream_class->event_context_type) {
+               g_string_append(context->string, ";\n\n\tevent.context := ");
+               ret = bt_ctf_field_type_serialize(
+                       stream_class->event_context_type, context);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       g_string_append(context->string, ";\n};\n\n");
+
+       /* Assign this stream's ID to every event and serialize them */
+       g_ptr_array_foreach(stream_class->event_classes,
+               (GFunc) bt_ctf_event_class_set_stream_id,
+               GUINT_TO_POINTER(stream_class->id));
+       for (i = 0; i < stream_class->event_classes->len; i++) {
+               struct bt_ctf_event_class *event_class =
+                       stream_class->event_classes->pdata[i];
+
+               ret = bt_ctf_event_class_set_stream_id(event_class,
+                       stream_class->id);
+               if (ret) {
+                       goto end;
+               }
+
+               ret = bt_ctf_event_class_serialize(event_class, context);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       context->current_indentation_level = 0;
+       return ret;
+}
+
+BT_HIDDEN
+struct bt_ctf_stream *bt_ctf_stream_create(
+               struct bt_ctf_stream_class *stream_class)
+{
+       struct bt_ctf_stream *stream = NULL;
+
+       if (!stream_class) {
+               goto end;
+       }
+
+       stream = g_new0(struct bt_ctf_stream, 1);
+       if (!stream) {
+               goto end;
+       }
+
+       bt_ctf_ref_init(&stream->ref_count);
+       stream->pos.fd = -1;
+       stream->id = stream_class->next_stream_id++;
+       stream->stream_class = stream_class;
+       bt_ctf_stream_class_get(stream_class);
+       bt_ctf_stream_class_freeze(stream_class);
+       stream->events = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)bt_ctf_event_put);
+end:
+       return stream;
+}
+
+BT_HIDDEN
+int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream *stream,
+       flush_func callback, void *data)
+{
+       int ret = stream ? 0 : -1;
+
+       if (!stream) {
+               goto end;
+       }
+
+       stream->flush.func = callback;
+       stream->flush.data = data;
+end:
+       return ret;
+}
+
+BT_HIDDEN
+int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd)
+{
+       int ret = 0;
+
+       if (stream->pos.fd != -1) {
+               ret = -1;
+               goto end;
+       }
+
+       ctf_init_pos(&stream->pos, NULL, fd, O_RDWR);
+       stream->pos.fd = fd;
+end:
+       return ret;
+}
+
+void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
+               uint64_t event_count)
+{
+       if (!stream) {
+               return;
+       }
+
+       stream->events_discarded += event_count;
+}
+
+int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
+               struct bt_ctf_event *event)
+{
+       int ret = 0;
+       uint64_t timestamp;
+
+       if (!stream || !event) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_event_validate(event);
+       if (ret) {
+               goto end;
+       }
+
+       timestamp = bt_ctf_clock_get_time(stream->stream_class->clock);
+       ret = bt_ctf_event_set_timestamp(event, timestamp);
+       if (ret) {
+               goto end;
+       }
+
+       bt_ctf_event_get(event);
+       g_ptr_array_add(stream->events, event);
+end:
+       return ret;
+}
+
+int bt_ctf_stream_flush(struct bt_ctf_stream *stream)
+{
+       int ret = 0;
+       size_t i;
+       uint64_t timestamp_begin, timestamp_end;
+       struct bt_ctf_stream_class *stream_class = stream->stream_class;
+       struct bt_ctf_field *integer = NULL;
+       struct ctf_stream_pos packet_context_pos;
+
+       if (!stream) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!stream->events->len) {
+               goto end;
+       }
+
+       if (stream->flush.func) {
+               stream->flush.func(stream, stream->flush.data);
+       }
+
+       timestamp_begin = ((struct bt_ctf_event *) g_ptr_array_index(
+               stream->events, 0))->timestamp;
+       timestamp_end = ((struct bt_ctf_event *) g_ptr_array_index(
+               stream->events, stream->events->len - 1))->timestamp;
+       ret = set_structure_field_integer(stream_class->packet_context,
+               "timestamp_begin", timestamp_begin);
+       if (ret) {
+               goto end;
+       }
+
+       ret = set_structure_field_integer(stream_class->packet_context,
+               "timestamp_end", timestamp_end);
+       if (ret) {
+               goto end;
+       }
+
+       ret = set_structure_field_integer(stream_class->packet_context,
+               "events_discarded", stream->events_discarded);
+       if (ret) {
+               goto end;
+       }
+
+       ret = set_structure_field_integer(stream_class->packet_context,
+               "content_size", UINT64_MAX);
+       if (ret) {
+               goto end;
+       }
+
+       ret = set_structure_field_integer(stream_class->packet_context,
+               "packet_size", UINT64_MAX);
+       if (ret) {
+               goto end;
+       }
+
+       /* Write packet context */
+       memcpy(&packet_context_pos, &stream->pos,
+              sizeof(struct ctf_stream_pos));
+       ret = bt_ctf_field_serialize(stream_class->packet_context,
+               &stream->pos);
+       if (ret) {
+               goto end;
+       }
+
+       for (i = 0; i < stream->events->len; i++) {
+               struct bt_ctf_event *event = g_ptr_array_index(
+                       stream->events, i);
+               uint32_t event_id = bt_ctf_event_class_get_id(
+                       event->event_class);
+               uint64_t timestamp = bt_ctf_event_get_timestamp(event);
+
+               ret = set_structure_field_integer(stream_class->event_header,
+                       "id", event_id);
+               if (ret) {
+                       goto end;
+               }
+               ret = set_structure_field_integer(stream_class->event_header,
+                       "timestamp", timestamp);
+               if (ret) {
+                       goto end;
+               }
+
+               /* Write event header */
+               ret = bt_ctf_field_serialize(stream_class->event_header,
+                       &stream->pos);
+               if (ret) {
+                       goto end;
+               }
+
+               /* Write event content */
+               ret = bt_ctf_event_serialize(event, &stream->pos);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       /*
+        * Update the packet total size and content size and overwrite the
+        * packet context.
+        */
+       ret = set_structure_field_integer(stream_class->packet_context,
+               "content_size", stream->pos.offset);
+       if (ret) {
+               goto end;
+       }
+
+       ret = set_structure_field_integer(stream_class->packet_context,
+               "packet_size", stream->pos.packet_size);
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_serialize(stream_class->packet_context,
+               &packet_context_pos);
+       if (ret) {
+               goto end;
+       }
+
+       g_ptr_array_set_size(stream->events, 0);
+       stream->flushed_packet_count++;
+end:
+       bt_ctf_field_put(integer);
+       return ret;
+}
+
+void bt_ctf_stream_get(struct bt_ctf_stream *stream)
+{
+       if (!stream) {
+               return;
+       }
+
+       bt_ctf_ref_get(&stream->ref_count);
+}
+
+void bt_ctf_stream_put(struct bt_ctf_stream *stream)
+{
+       if (!stream) {
+               return;
+       }
+
+       bt_ctf_ref_put(&stream->ref_count, bt_ctf_stream_destroy);
+}
+
+static
+void bt_ctf_stream_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_stream *stream;
+
+       if (!ref) {
+               return;
+       }
+
+       stream = container_of(ref, struct bt_ctf_stream, ref_count);
+       ctf_fini_pos(&stream->pos);
+       close(stream->pos.fd);
+       bt_ctf_stream_class_put(stream->stream_class);
+       g_ptr_array_free(stream->events, TRUE);
+       g_free(stream);
+}
+
+static
+void bt_ctf_stream_class_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_stream_class *stream_class;
+
+       if (!ref) {
+               return;
+       }
+
+       stream_class = container_of(ref, struct bt_ctf_stream_class, ref_count);
+       bt_ctf_clock_put(stream_class->clock);
+
+       if (stream_class->event_classes) {
+               g_ptr_array_free(stream_class->event_classes, TRUE);
+       }
+
+       if (stream_class->name) {
+               g_string_free(stream_class->name, TRUE);
+       }
+
+       bt_ctf_field_type_put(stream_class->event_header_type);
+       bt_ctf_field_put(stream_class->event_header);
+       bt_ctf_field_type_put(stream_class->packet_context_type);
+       bt_ctf_field_put(stream_class->packet_context);
+       bt_ctf_field_type_put(stream_class->event_context_type);
+       bt_ctf_field_put(stream_class->event_context);
+       g_free(stream_class);
+}
+
+static
+int init_event_header(struct bt_ctf_stream_class *stream_class,
+               enum bt_ctf_byte_order byte_order)
+{
+       int ret = 0;
+       struct bt_ctf_field_type *event_header_type =
+               bt_ctf_field_type_structure_create();
+       struct bt_ctf_field_type *_uint32_t =
+               get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
+       struct bt_ctf_field_type *_uint64_t =
+               get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
+
+       if (!event_header_type) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_set_byte_order(_uint32_t, byte_order);
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(event_header_type,
+               _uint32_t, "id");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(event_header_type,
+               _uint64_t, "timestamp");
+       if (ret) {
+               goto end;
+       }
+
+       stream_class->event_header_type = event_header_type;
+       stream_class->event_header = bt_ctf_field_create(
+               stream_class->event_header_type);
+       if (!stream_class->event_header) {
+               ret = -1;
+       }
+end:
+       if (ret) {
+               bt_ctf_field_type_put(event_header_type);
+       }
+
+       bt_ctf_field_type_put(_uint32_t);
+       bt_ctf_field_type_put(_uint64_t);
+       return ret;
+}
+
+static
+int init_packet_context(struct bt_ctf_stream_class *stream_class,
+               enum bt_ctf_byte_order byte_order)
+{
+       int ret = 0;
+       struct bt_ctf_field_type *packet_context_type =
+               bt_ctf_field_type_structure_create();
+       struct bt_ctf_field_type *_uint64_t =
+               get_field_type(FIELD_TYPE_ALIAS_UINT64_T);
+
+       if (!packet_context_type) {
+               ret = -1;
+               goto end;
+       }
+
+       /*
+        * We create a stream packet context as proposed in the CTF
+        * specification.
+        */
+       ret = bt_ctf_field_type_set_byte_order(_uint64_t, byte_order);
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "timestamp_begin");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "timestamp_end");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "content_size");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "packet_size");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(packet_context_type,
+               _uint64_t, "events_discarded");
+       if (ret) {
+               goto end;
+       }
+
+       stream_class->packet_context_type = packet_context_type;
+       stream_class->packet_context = bt_ctf_field_create(packet_context_type);
+       if (!stream_class->packet_context) {
+               ret = -1;
+       }
+end:
+       if (ret) {
+               bt_ctf_field_type_put(packet_context_type);
+               goto end;
+       }
+
+       bt_ctf_field_type_put(_uint64_t);
+       return ret;
+}
+
+static
+int set_structure_field_integer(struct bt_ctf_field *structure, char *name,
+               uint64_t value)
+{
+       int ret = 0;
+
+       struct bt_ctf_field *integer =
+               bt_ctf_field_structure_get_field(structure, name);
+       if (!integer) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_unsigned_integer_set_value(integer, value);
+end:
+       bt_ctf_field_put(integer);
+       return ret;
+}
diff --git a/formats/ctf/writer/writer.c b/formats/ctf/writer/writer.c
new file mode 100644 (file)
index 0000000..5600a92
--- /dev/null
@@ -0,0 +1,753 @@
+/*
+ * writer.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/writer.h>
+#include <babeltrace/ctf-writer/clock.h>
+#include <babeltrace/ctf-writer/clock-internal.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/ctf-writer/event-types-internal.h>
+#include <babeltrace/ctf-writer/event-fields-internal.h>
+#include <babeltrace/ctf-writer/functor-internal.h>
+#include <babeltrace/ctf-writer/stream-internal.h>
+#include <babeltrace/ctf-writer/stream.h>
+#include <babeltrace/compiler.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <inttypes.h>
+
+#define DEFAULT_IDENTIFIER_SIZE 128
+#define DEFAULT_METADATA_STRING_SIZE 4096
+
+static
+void environment_variable_destroy(struct environment_variable *var);
+static
+void bt_ctf_writer_destroy(struct bt_ctf_ref *ref);
+static
+int init_trace_packet_header(struct bt_ctf_writer *writer);
+static
+int create_stream_file(struct bt_ctf_writer *writer,
+               struct bt_ctf_stream *stream);
+static
+void stream_flush_cb(struct bt_ctf_stream *stream,
+               struct bt_ctf_writer *writer);
+
+static
+const char * const reserved_keywords_str[] = {"align", "callsite",
+       "const", "char", "clock", "double", "enum", "env", "event",
+       "floating_point", "float", "integer", "int", "long", "short", "signed",
+       "stream", "string", "struct", "trace", "typealias", "typedef",
+       "unsigned", "variant", "void" "_Bool", "_Complex", "_Imaginary"};
+
+static
+const unsigned int field_type_aliases_alignments[] = {
+       [FIELD_TYPE_ALIAS_UINT5_T] = 1,
+       [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
+       [FIELD_TYPE_ALIAS_UINT27_T] = 1,
+       [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
+};
+
+static
+const unsigned int field_type_aliases_sizes[] = {
+       [FIELD_TYPE_ALIAS_UINT5_T] = 5,
+       [FIELD_TYPE_ALIAS_UINT8_T] = 8,
+       [FIELD_TYPE_ALIAS_UINT16_T] = 16,
+       [FIELD_TYPE_ALIAS_UINT27_T] = 27,
+       [FIELD_TYPE_ALIAS_UINT32_T] = 32,
+       [FIELD_TYPE_ALIAS_UINT64_T] = 64,
+};
+
+static GHashTable *reserved_keywords_set;
+static int init_done;
+static int global_data_refcount;
+
+struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
+{
+       struct bt_ctf_writer *writer = NULL;
+
+       if (!path) {
+               goto error;
+       }
+
+       writer = g_new0(struct bt_ctf_writer, 1);
+       if (!writer) {
+               goto error;
+       }
+
+       bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_NATIVE);
+       bt_ctf_ref_init(&writer->ref_count);
+       writer->path = g_string_new(path);
+       if (!writer->path) {
+               goto error_destroy;
+       }
+
+       /* Create trace directory if necessary and open a metadata file */
+       if (g_mkdir_with_parents(path, S_IRWXU | S_IRWXG)) {
+               perror("g_mkdir_with_parents");
+               goto error_destroy;
+       }
+
+       writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG);
+       if (writer->trace_dir_fd < 0) {
+               perror("open");
+               goto error_destroy;
+       }
+
+       writer->metadata_fd = openat(writer->trace_dir_fd, "metadata",
+               O_WRONLY | O_CREAT | O_TRUNC,
+               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+       writer->environment = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)environment_variable_destroy);
+       writer->clocks = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)bt_ctf_clock_put);
+       writer->streams = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)bt_ctf_stream_put);
+       writer->stream_classes = g_ptr_array_new_with_free_func(
+               (GDestroyNotify)bt_ctf_stream_class_put);
+       if (!writer->environment || !writer->clocks ||
+               !writer->stream_classes || !writer->streams) {
+               goto error_destroy;
+       }
+
+       /* Generate a trace UUID */
+       uuid_generate(writer->uuid);
+       if (init_trace_packet_header(writer)) {
+               goto error_destroy;
+       }
+
+       return writer;
+error_destroy:
+       unlinkat(writer->trace_dir_fd, "metadata", 0);
+       bt_ctf_writer_destroy(&writer->ref_count);
+       writer = NULL;
+error:
+       return writer;
+}
+
+void bt_ctf_writer_destroy(struct bt_ctf_ref *ref)
+{
+       struct bt_ctf_writer *writer;
+
+       if (!ref) {
+               return;
+       }
+
+       writer = container_of(ref, struct bt_ctf_writer, ref_count);
+       bt_ctf_writer_flush_metadata(writer);
+       if (writer->path) {
+               g_string_free(writer->path, TRUE);
+       }
+
+       if (writer->trace_dir_fd > 0) {
+               if (close(writer->trace_dir_fd)) {
+                       perror("close");
+                       abort();
+               }
+       }
+
+       if (writer->metadata_fd > 0) {
+               if (close(writer->metadata_fd)) {
+                       perror("close");
+                       abort();
+               }
+       }
+
+       if (writer->environment) {
+               g_ptr_array_free(writer->environment, TRUE);
+       }
+
+       if (writer->clocks) {
+               g_ptr_array_free(writer->clocks, TRUE);
+       }
+
+       if (writer->streams) {
+               g_ptr_array_free(writer->streams, TRUE);
+       }
+
+       if (writer->stream_classes) {
+               g_ptr_array_free(writer->stream_classes, TRUE);
+       }
+
+       bt_ctf_field_type_put(writer->trace_packet_header_type);
+       bt_ctf_field_put(writer->trace_packet_header);
+       g_free(writer);
+}
+
+struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer,
+               struct bt_ctf_stream_class *stream_class)
+{
+       int ret;
+       int stream_class_found = 0;
+       size_t i;
+       int stream_fd;
+       struct bt_ctf_stream *stream = NULL;
+
+       if (!writer || !stream_class) {
+               goto error;
+       }
+
+       stream = bt_ctf_stream_create(stream_class);
+       if (!stream) {
+               goto error;
+       }
+
+       stream_fd = create_stream_file(writer, stream);
+       if (stream_fd < 0 || bt_ctf_stream_set_fd(stream, stream_fd)) {
+               goto error;
+       }
+
+       bt_ctf_stream_set_flush_callback(stream, (flush_func)stream_flush_cb,
+               writer);
+       ret = bt_ctf_stream_class_set_byte_order(stream->stream_class,
+               writer->byte_order == LITTLE_ENDIAN ?
+               BT_CTF_BYTE_ORDER_LITTLE_ENDIAN : BT_CTF_BYTE_ORDER_BIG_ENDIAN);
+       if (ret) {
+               goto error;
+       }
+
+       for (i = 0; i < writer->stream_classes->len; i++) {
+               if (writer->stream_classes->pdata[i] == stream->stream_class) {
+                       stream_class_found = 1;
+               }
+       }
+
+       if (!stream_class_found) {
+               if (bt_ctf_stream_class_set_id(stream->stream_class,
+                       writer->next_stream_id++)) {
+                       goto error;
+               }
+
+               bt_ctf_stream_class_get(stream->stream_class);
+               g_ptr_array_add(writer->stream_classes, stream->stream_class);
+       }
+
+       bt_ctf_stream_get(stream);
+       g_ptr_array_add(writer->streams, stream);
+       writer->frozen = 1;
+       return stream;
+error:
+       bt_ctf_stream_put(stream);
+       return NULL;
+}
+
+int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
+               const char *name,
+               const char *value)
+{
+       struct environment_variable *var = NULL;
+       char *escaped_value = NULL;
+       int ret = 0;
+
+       if (!writer || !name || !value || validate_identifier(name)) {
+               ret = -1;
+               goto error;
+       }
+
+       if (strchr(name, ' ')) {
+               ret = -1;
+               goto error;
+       }
+
+       var = g_new0(struct environment_variable, 1);
+       if (!var) {
+               ret = -1;
+               goto error;
+       }
+
+       escaped_value = g_strescape(value, NULL);
+       if (!escaped_value) {
+               ret = -1;
+               goto error;
+       }
+
+       var->name = g_string_new(name);
+       var->value = g_string_new(escaped_value);
+       g_free(escaped_value);
+       if (!var->name || !var->value) {
+               ret = -1;
+               goto error;
+       }
+
+       g_ptr_array_add(writer->environment, var);
+       return ret;
+
+error:
+       if (var && var->name) {
+               g_string_free(var->name, TRUE);
+       }
+
+       if (var && var->value) {
+               g_string_free(var->value, TRUE);
+       }
+
+       g_free(var);
+       return ret;
+}
+
+int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
+               struct bt_ctf_clock *clock)
+{
+       int ret = 0;
+       struct search_query query = { .value = clock, .found = 0 };
+
+       if (!writer || !clock) {
+               ret = -1;
+               goto end;
+       }
+
+       /* Check for duplicate clocks */
+       g_ptr_array_foreach(writer->clocks, value_exists, &query);
+       if (query.found) {
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_clock_get(clock);
+       g_ptr_array_add(writer->clocks, clock);
+end:
+       return ret;
+}
+
+BT_HIDDEN
+const char *get_byte_order_string(int byte_order)
+{
+       const char *string;
+
+       switch (byte_order) {
+       case LITTLE_ENDIAN:
+               string = "le";
+               break;
+       case BIG_ENDIAN:
+               string = "be";
+               break;
+       default:
+               string = "unknown";
+               break;
+       }
+
+       return string;
+}
+
+static
+void append_trace_metadata(struct bt_ctf_writer *writer,
+               struct metadata_context *context)
+{
+       unsigned char *uuid = writer->uuid;
+       int ret;
+
+       g_string_append(context->string, "trace {\n");
+
+       g_string_append(context->string, "\tmajor = 1;\n");
+       g_string_append(context->string, "\tminor = 8;\n");
+
+       g_string_append_printf(context->string,
+               "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
+               uuid[0], uuid[1], uuid[2], uuid[3],
+               uuid[4], uuid[5], uuid[6], uuid[7],
+               uuid[8], uuid[9], uuid[10], uuid[11],
+               uuid[12], uuid[13], uuid[14], uuid[15]);
+       g_string_append_printf(context->string, "\tbyte_order = %s;\n",
+               get_byte_order_string(writer->byte_order));
+
+       g_string_append(context->string, "\tpacket.header := ");
+       context->current_indentation_level++;
+       g_string_assign(context->field_name, "");
+       ret = bt_ctf_field_type_serialize(writer->trace_packet_header_type,
+               context);
+       assert(!ret);
+       context->current_indentation_level--;
+
+       g_string_append(context->string, ";\n};\n\n");
+}
+
+static
+void append_env_field_metadata(struct environment_variable *var,
+               struct metadata_context *context)
+{
+       g_string_append_printf(context->string, "\t%s = \"%s\";\n",
+               var->name->str, var->value->str);
+}
+
+static
+void append_env_metadata(struct bt_ctf_writer *writer,
+               struct metadata_context *context)
+{
+       if (writer->environment->len == 0) {
+               return;
+       }
+
+       g_string_append(context->string, "env {\n");
+       g_ptr_array_foreach(writer->environment,
+               (GFunc)append_env_field_metadata, context);
+       g_string_append(context->string, "};\n\n");
+}
+
+char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer)
+{
+       char *metadata = NULL;
+       struct metadata_context *context = NULL;
+       int err = 0;
+       size_t i;
+
+       if (!writer) {
+               goto end;
+       }
+
+       context = g_new0(struct metadata_context, 1);
+       if (!context) {
+               goto end;
+       }
+
+       context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
+       context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
+       g_string_append(context->string, "/* CTF 1.8 */\n\n");
+       append_trace_metadata(writer, context);
+       append_env_metadata(writer, context);
+       g_ptr_array_foreach(writer->clocks,
+               (GFunc)bt_ctf_clock_serialize, context);
+
+       for (i = 0; i < writer->stream_classes->len; i++) {
+               err = bt_ctf_stream_class_serialize(
+                       writer->stream_classes->pdata[i], context);
+               if (err) {
+                       goto error;
+               }
+       }
+
+       metadata = context->string->str;
+error:
+       g_string_free(context->string, err ? TRUE : FALSE);
+       g_string_free(context->field_name, TRUE);
+       g_free(context);
+end:
+       return metadata;
+}
+
+void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer)
+{
+       int ret;
+       char *metadata_string = NULL;
+
+       if (!writer) {
+               goto end;
+       }
+
+       metadata_string = bt_ctf_writer_get_metadata_string(writer);
+       if (!metadata_string) {
+               goto end;
+       }
+
+       if (lseek(writer->metadata_fd, 0, SEEK_SET) == (off_t)-1) {
+               perror("lseek");
+               goto end;
+       }
+
+       if (ftruncate(writer->metadata_fd, 0)) {
+               perror("ftruncate");
+               goto end;
+       }
+
+       ret = write(writer->metadata_fd, metadata_string,
+               strlen(metadata_string));
+       if (ret < 0) {
+               perror("write");
+               goto end;
+       }
+end:
+       g_free(metadata_string);
+}
+
+int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
+               enum bt_ctf_byte_order byte_order)
+{
+       int ret = 0;
+       int internal_byte_order;
+
+       if (!writer || writer->frozen) {
+               ret = -1;
+               goto end;
+       }
+
+       switch (byte_order) {
+       case BT_CTF_BYTE_ORDER_NATIVE:
+               internal_byte_order =  (G_BYTE_ORDER == G_LITTLE_ENDIAN) ?
+                       LITTLE_ENDIAN : BIG_ENDIAN;
+               break;
+       case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
+               internal_byte_order = LITTLE_ENDIAN;
+               break;
+       case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
+       case BT_CTF_BYTE_ORDER_NETWORK:
+               internal_byte_order = BIG_ENDIAN;
+               break;
+       default:
+               ret = -1;
+               goto end;
+       }
+
+       writer->byte_order = internal_byte_order;
+       if (writer->trace_packet_header_type ||
+               writer->trace_packet_header) {
+               init_trace_packet_header(writer);
+       }
+end:
+       return ret;
+}
+
+void bt_ctf_writer_get(struct bt_ctf_writer *writer)
+{
+       if (!writer) {
+               return;
+       }
+
+       bt_ctf_ref_get(&writer->ref_count);
+}
+
+void bt_ctf_writer_put(struct bt_ctf_writer *writer)
+{
+       if (!writer) {
+               return;
+       }
+
+       bt_ctf_ref_put(&writer->ref_count, bt_ctf_writer_destroy);
+}
+
+BT_HIDDEN
+int validate_identifier(const char *input_string)
+{
+       int ret = 0;
+       char *string = NULL;
+       char *save_ptr, *token;
+
+       if (!input_string || input_string[0] == '\0') {
+               ret = -1;
+               goto end;
+       }
+
+       string = strdup(input_string);
+       if (!string) {
+               ret = -1;
+               goto end;
+       }
+
+       token = strtok_r(string, " ", &save_ptr);
+       while (token) {
+               if (g_hash_table_contains(reserved_keywords_set,
+                       GINT_TO_POINTER(g_quark_from_string(token)))) {
+                       ret = -1;
+                       goto end;
+               }
+
+               token = strtok_r(NULL, " ", &save_ptr);
+       }
+end:
+       free(string);
+       return ret;
+}
+
+BT_HIDDEN
+struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
+{
+       unsigned int alignment, size;
+       struct bt_ctf_field_type *field_type;
+
+       if (alias >= NR_FIELD_TYPE_ALIAS) {
+               return NULL;
+       }
+
+       alignment = field_type_aliases_alignments[alias];
+       size = field_type_aliases_sizes[alias];
+       field_type = bt_ctf_field_type_integer_create(size);
+       bt_ctf_field_type_set_alignment(field_type, alignment);
+       return field_type;
+}
+
+static
+int init_trace_packet_header(struct bt_ctf_writer *writer)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field *trace_packet_header = NULL,
+               *magic = NULL, *uuid_array = NULL;
+       struct bt_ctf_field_type *_uint32_t =
+               get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
+       struct bt_ctf_field_type *_uint8_t =
+               get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
+       struct bt_ctf_field_type *trace_packet_header_type =
+               bt_ctf_field_type_structure_create();
+       struct bt_ctf_field_type *uuid_array_type =
+               bt_ctf_field_type_array_create(_uint8_t, 16);
+
+       if (!trace_packet_header_type || !uuid_array_type) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_set_byte_order(_uint32_t,
+               (writer->byte_order == LITTLE_ENDIAN ?
+               BT_CTF_BYTE_ORDER_LITTLE_ENDIAN :
+               BT_CTF_BYTE_ORDER_BIG_ENDIAN));
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
+               _uint32_t, "magic");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
+               uuid_array_type, "uuid");
+       if (ret) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
+               _uint32_t, "stream_id");
+       if (ret) {
+               goto end;
+       }
+
+       trace_packet_header = bt_ctf_field_create(trace_packet_header_type);
+       if (!trace_packet_header) {
+               ret = -1;
+               goto end;
+       }
+
+       magic = bt_ctf_field_structure_get_field(trace_packet_header, "magic");
+       ret = bt_ctf_field_unsigned_integer_set_value(magic, 0xC1FC1FC1);
+       if (ret) {
+               goto end;
+       }
+
+       uuid_array = bt_ctf_field_structure_get_field(trace_packet_header,
+               "uuid");
+       for (i = 0; i < 16; i++) {
+               struct bt_ctf_field *uuid_element =
+                       bt_ctf_field_array_get_field(uuid_array, i);
+               ret = bt_ctf_field_unsigned_integer_set_value(uuid_element,
+                       writer->uuid[i]);
+               bt_ctf_field_put(uuid_element);
+               if (ret) {
+                       goto end;
+               }
+       }
+
+       bt_ctf_field_type_put(writer->trace_packet_header_type);
+       bt_ctf_field_put(writer->trace_packet_header);
+       writer->trace_packet_header_type = trace_packet_header_type;
+       writer->trace_packet_header = trace_packet_header;
+end:
+       bt_ctf_field_type_put(uuid_array_type);
+       bt_ctf_field_type_put(_uint32_t);
+       bt_ctf_field_type_put(_uint8_t);
+       bt_ctf_field_put(magic);
+       bt_ctf_field_put(uuid_array);
+       if (ret) {
+               bt_ctf_field_type_put(trace_packet_header_type);
+               bt_ctf_field_put(trace_packet_header);
+       }
+
+       return ret;
+}
+
+static
+void environment_variable_destroy(struct environment_variable *var)
+{
+       g_string_free(var->name, TRUE);
+       g_string_free(var->value, TRUE);
+       g_free(var);
+}
+
+static
+int create_stream_file(struct bt_ctf_writer *writer,
+               struct bt_ctf_stream *stream)
+{
+       int fd;
+       GString *filename = g_string_new(stream->stream_class->name->str);
+
+       g_string_append_printf(filename, "_%" PRIu32, stream->id);
+       fd = openat(writer->trace_dir_fd, filename->str,
+               O_RDWR | O_CREAT | O_TRUNC,
+               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+       g_string_free(filename, TRUE);
+       return fd;
+}
+
+static
+void stream_flush_cb(struct bt_ctf_stream *stream, struct bt_ctf_writer *writer)
+{
+       struct bt_ctf_field *stream_id;
+
+       /* Start a new packet in the stream */
+       if (stream->flushed_packet_count) {
+               /* ctf_init_pos has already initialized the first packet */
+               ctf_packet_seek(&stream->pos.parent, 0, SEEK_CUR);
+       }
+
+       stream_id = bt_ctf_field_structure_get_field(
+               writer->trace_packet_header, "stream_id");
+       bt_ctf_field_unsigned_integer_set_value(stream_id, stream->id);
+       bt_ctf_field_put(stream_id);
+
+       /* Write the trace_packet_header */
+       bt_ctf_field_serialize(writer->trace_packet_header, &stream->pos);
+}
+
+static __attribute__((constructor))
+void writer_init(void)
+{
+       size_t i;
+       const size_t reserved_keywords_count =
+               sizeof(reserved_keywords_str) / sizeof(char *);
+
+       global_data_refcount++;
+       if (init_done) {
+               return;
+       }
+
+       reserved_keywords_set = g_hash_table_new(g_direct_hash, g_direct_equal);
+       for (i = 0; i < reserved_keywords_count; i++) {
+               g_hash_table_add(reserved_keywords_set,
+               GINT_TO_POINTER(g_quark_from_string(reserved_keywords_str[i])));
+       }
+
+       init_done = 1;
+}
+
+static __attribute__((destructor))
+void writer_finalize(void)
+{
+       if (--global_data_refcount == 0) {
+               g_hash_table_destroy(reserved_keywords_set);
+       }
+}
index 2d05ef9449665e642bbf20d7e67658b57f128546..ec927b9173ffa21f9a9557c157e80f5e38ba5d77 100644 (file)
@@ -12,6 +12,14 @@ babeltracectfinclude_HEADERS = \
        babeltrace/ctf/callbacks.h \
        babeltrace/ctf/iterator.h
 
+babeltracectfwriterinclude_HEADERS = \
+       babeltrace/ctf-writer/clock.h \
+       babeltrace/ctf-writer/writer.h \
+       babeltrace/ctf-writer/event-fields.h \
+       babeltrace/ctf-writer/event-types.h \
+       babeltrace/ctf-writer/event.h \
+       babeltrace/ctf-writer/stream.h
+
 noinst_HEADERS = \
        babeltrace/align.h \
        babeltrace/babeltrace-internal.h \
@@ -30,6 +38,14 @@ noinst_HEADERS = \
        babeltrace/ctf-text/types.h \
        babeltrace/ctf/types.h \
        babeltrace/ctf/callbacks-internal.h \
+       babeltrace/ctf-writer/ref-internal.h \
+       babeltrace/ctf-writer/writer-internal.h \
+       babeltrace/ctf-writer/event-types-internal.h \
+       babeltrace/ctf-writer/event-fields-internal.h \
+       babeltrace/ctf-writer/event-internal.h \
+       babeltrace/ctf-writer/clock-internal.h \
+       babeltrace/ctf-writer/stream-internal.h \
+       babeltrace/ctf-writer/functor-internal.h \
        babeltrace/trace-handle-internal.h \
        babeltrace/compat/uuid.h \
        babeltrace/compat/memstream.h \
index 40c1161489d66d6d56cba1fe1ba1803973289b18..b7fce09301781a7f8697921bc384aada739f0760 100644 (file)
@@ -57,7 +57,7 @@
        ___v <<= final;                                                 \
 })
 
-#define _bt_is_signed_type(type)       (((type)(-1)) < 0)
+#define _bt_is_signed_type(type)       ((type) -1 < (type) 0)
 
 #define _bt_unsigned_cast(type, v)                                     \
 ({                                                                     \
diff --git a/include/babeltrace/ctf-writer/clock-internal.h b/include/babeltrace/ctf-writer/clock-internal.h
new file mode 100644 (file)
index 0000000..c60e5c0
--- /dev/null
@@ -0,0 +1,65 @@
+#ifndef BABELTRACE_CTF_WRITER_CLOCK_INTERNAL_H
+#define BABELTRACE_CTF_WRITER_CLOCK_INTERNAL_H
+
+/*
+ * BabelTrace - CTF Writer: Clock internal
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/ctf-writer/clock.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/babeltrace-internal.h>
+#include <glib.h>
+#include <uuid/uuid.h>
+
+struct bt_ctf_clock {
+       struct bt_ctf_ref ref_count;
+       GString *name;
+       GString *description;
+       uint64_t frequency;
+       uint64_t precision;
+       uint64_t offset_s;      /* Offset in seconds */
+       uint64_t offset;        /* Offset in ticks */
+       uint64_t time;          /* Current clock value */
+       uuid_t uuid;
+       int absolute;
+       /*
+        * A clock's properties can't be modified once it is added to a stream
+        * class.
+        */
+       int frozen;
+};
+
+BT_HIDDEN
+void bt_ctf_clock_freeze(struct bt_ctf_clock *clock);
+
+BT_HIDDEN
+void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
+               struct metadata_context *context);
+
+BT_HIDDEN
+uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock);
+
+#endif /* BABELTRACE_CTF_WRITER_CLOCK_INTERNAL_H */
diff --git a/include/babeltrace/ctf-writer/clock.h b/include/babeltrace/ctf-writer/clock.h
new file mode 100644 (file)
index 0000000..7cfc073
--- /dev/null
@@ -0,0 +1,166 @@
+#ifndef BABELTRACE_CTF_WRITER_CLOCK_H
+#define BABELTRACE_CTF_WRITER_CLOCK_H
+
+/*
+ * BabelTrace - CTF Writer: Clock
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * The Common Trace Format (CTF) Specification is available at
+ * http://www.efficios.com/ctf
+ */
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct bt_ctf_clock;
+
+/*
+ * bt_ctf_clock_create: create a clock.
+ *
+ * Allocate a new clock setting its reference count to 1.
+ *
+ * @param name Name of the clock (will be copied).
+ *
+ * Returns an allocated clock on success, NULL on error.
+ */
+extern struct bt_ctf_clock *bt_ctf_clock_create(const char *name);
+
+/*
+ * bt_ctf_clock_set_description: set a clock's description.
+ *
+ * Set the clock's description. The description appears in the clock's TSDL
+ * meta-data.
+ *
+ * @param clock Clock instance.
+ * @param desc Description of the clock.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_clock_set_description(struct bt_ctf_clock *clock,
+               const char *desc);
+
+/*
+ * bt_ctf_clock_set_frequency: set a clock's frequency.
+ *
+ * Set the clock's frequency (Hz).
+ *
+ * @param clock Clock instance.
+ * @param freq Clock's frequency in Hz, defaults to 1 000 000 000 Hz (1ns).
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock,
+               uint64_t freq);
+
+/*
+ * bt_ctf_clock_set_precision: set a clock's precision.
+ *
+ * Set the clock's precision.
+ *
+ * @param clock Clock instance.
+ * @param precision Clock's precision in clock ticks, defaults to 1.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock,
+               uint64_t precision);
+
+/*
+ * bt_ctf_clock_set_offset_s: set a clock's offset in seconds.
+ *
+ * Set the clock's offset in seconds from POSIX.1 Epoch, 1970-01-01,
+ * defaults to 0.
+ *
+ * @param clock Clock instance.
+ * @param offset_s Clock's offset in seconds, defaults to 0.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock,
+               uint64_t offset_s);
+
+
+/*
+ * bt_ctf_clock_set_offset: set a clock's offset in ticks.
+ *
+ * Set the clock's offset in ticks from Epoch + offset_s.
+ *
+ * @param clock Clock instance.
+ * @param offset Clock's offset in ticks from Epoch + offset_s, defaults to 0.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock,
+               uint64_t offset);
+
+/*
+ * bt_ctf_clock_set_is_absolute: set a clock's absolute attribute.
+ *
+ * A clock is absolute if the clock is a global reference across the trace's
+ * other clocks.
+ *
+ * @param clock Clock instance.
+ * @param is_absolute Clock's absolute attribute, defaults to FALSE.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock,
+               int is_absolute);
+
+/*
+ * bt_ctf_clock_set_time: set a clock's current time value.
+ *
+ * Set the current time in nanoseconds since the clock's origin (offset and
+ * offset_s attributes). The clock's value will be sampled as events are
+ * appended to a stream.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time);
+
+/*
+ * bt_ctf_clock_get and bt_ctf_clock_put: increment and decrement the
+ * refcount of the clock
+ *
+ * These functions ensure that the clock won't be destroyed when it
+ * is in use. The same number of get and put (plus one extra put to
+ * release the initial reference done at creation) has to be done to
+ * destroy a clock.
+ *
+ * When the clock refcount is decremented to 0 by a bt_ctf_clock_put,
+ * the clock is freed.
+ *
+ * @param clock Clock instance.
+ */
+extern void bt_ctf_clock_get(struct bt_ctf_clock *clock);
+extern void bt_ctf_clock_put(struct bt_ctf_clock *clock);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BABELTRACE_CTF_WRITER_CLOCK_H */
diff --git a/include/babeltrace/ctf-writer/event-fields-internal.h b/include/babeltrace/ctf-writer/event-fields-internal.h
new file mode 100644 (file)
index 0000000..9f2670d
--- /dev/null
@@ -0,0 +1,100 @@
+#ifndef BABELTRACE_CTF_WRITER_EVENT_FIELDS_INTERNAL_H
+#define BABELTRACE_CTF_WRITER_EVENT_FIELDS_INTERNAL_H
+
+/*
+ * BabelTrace - CTF Writer: Event Fields internal
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/ctf/types.h>
+#include <glib.h>
+
+struct bt_ctf_field {
+       struct bt_ctf_ref ref_count;
+       struct bt_ctf_field_type *type;
+       int payload_set;
+};
+
+struct bt_ctf_field_integer {
+       struct bt_ctf_field parent;
+       struct definition_integer definition;
+};
+
+struct bt_ctf_field_enumeration {
+       struct bt_ctf_field parent;
+       struct bt_ctf_field *payload;
+};
+
+struct bt_ctf_field_floating_point {
+       struct bt_ctf_field parent;
+       struct definition_float definition;
+       struct definition_integer sign, mantissa, exp;
+};
+
+struct bt_ctf_field_structure {
+       struct bt_ctf_field parent;
+       GHashTable *field_name_to_index;
+       GPtrArray *fields; /* Array of pointers to struct bt_ctf_field */
+};
+
+struct bt_ctf_field_variant {
+       struct bt_ctf_field parent;
+       struct bt_ctf_field *tag;
+       struct bt_ctf_field *payload;
+};
+
+struct bt_ctf_field_array {
+       struct bt_ctf_field parent;
+       GPtrArray *elements; /* Array of pointers to struct bt_ctf_field */
+};
+
+struct bt_ctf_field_sequence {
+       struct bt_ctf_field parent;
+       struct bt_ctf_field *length;
+       GPtrArray *elements; /* Array of pointers to struct bt_ctf_field */
+};
+
+struct bt_ctf_field_string {
+       struct bt_ctf_field parent;
+       GString *payload;
+};
+
+/*
+ * Set a field's value with an already allocated field instance.
+ */
+BT_HIDDEN
+int bt_ctf_field_structure_set_field(struct bt_ctf_field *structure,
+               const char *name, struct bt_ctf_field *value);
+
+BT_HIDDEN
+int bt_ctf_field_validate(struct bt_ctf_field *field);
+
+BT_HIDDEN
+int bt_ctf_field_serialize(struct bt_ctf_field *field,
+               struct ctf_stream_pos *pos);
+
+#endif /* BABELTRACE_CTF_WRITER_EVENT_FIELDS_INTERNAL_H */
diff --git a/include/babeltrace/ctf-writer/event-fields.h b/include/babeltrace/ctf-writer/event-fields.h
new file mode 100644 (file)
index 0000000..8d3190b
--- /dev/null
@@ -0,0 +1,218 @@
+#ifndef BABELTRACE_CTF_WRITER_EVENT_FIELDS_H
+#define BABELTRACE_CTF_WRITER_EVENT_FIELDS_H
+
+/*
+ * BabelTrace - CTF Writer: Event Fields
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * The Common Trace Format (CTF) Specification is available at
+ * http://www.efficios.com/ctf
+ */
+
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct bt_ctf_event_class;
+struct bt_ctf_event;
+struct bt_ctf_field;
+struct bt_ctf_field_type;
+
+/*
+ * bt_ctf_field_create: create an instance of a field.
+ *
+ * Allocate a new field of the type described by the bt_ctf_field_type
+ * structure.The creation of a field sets its reference count to 1.
+ *
+ * @param type Field type to be instanciated.
+ *
+ * Returns an allocated field on success, NULL on error.
+ */
+extern struct bt_ctf_field *bt_ctf_field_create(
+               struct bt_ctf_field_type *type);
+
+/*
+ * bt_ctf_field_structure_get_field: get a structure's field.
+ *
+ * Get the structure's field corresponding to the provided field name.
+ * bt_ctf_field_put() must be called on the returned value.
+ *
+ * @param structure Structure field instance.
+ * @param name Name of the field in the provided structure.
+ *
+ * Returns a field instance on success, NULL on error.
+ */
+extern struct bt_ctf_field *bt_ctf_field_structure_get_field(
+               struct bt_ctf_field *structure, const char *name);
+
+/*
+ * bt_ctf_field_array_get_field: get an array's field at position "index".
+ *
+ * Return the array's field at position "index". bt_ctf_field_put() must be
+ * called on the returned value.
+ *
+ * @param array Array field instance.
+ * @param index Position of the array's desired element.
+ *
+ * Returns a field instance on success, NULL on error.
+ */
+extern struct bt_ctf_field *bt_ctf_field_array_get_field(
+               struct bt_ctf_field *array, uint64_t index);
+
+/*
+ * bt_ctf_field_sequence_set_length: set a sequence's length.
+ *
+ * Set the sequence's length field.
+ *
+ * @param sequence Sequence field instance.
+ * @param length_field Integer field instance indicating the sequence's length.
+ *
+ * Returns a field instance on success, NULL on error.
+ */
+extern int bt_ctf_field_sequence_set_length(struct bt_ctf_field *sequence,
+               struct bt_ctf_field *length_field);
+
+/*
+ * bt_ctf_field_sequence_get_field: get a sequence's field at position "index".
+ *
+ * Return the sequence's field at position "index". The sequence's length must
+ * have been set prior to calling this function using
+ * bt_ctf_field_sequence_set_length().
+ * bt_ctf_field_put() must be called on the returned value.
+ *
+ * @param array Sequence field instance.
+ * @param index Position of the sequence's desired element.
+ *
+ * Returns a field instance on success, NULL on error.
+ */
+extern struct bt_ctf_field *bt_ctf_field_sequence_get_field(
+               struct bt_ctf_field *sequence, uint64_t index);
+
+/*
+ * bt_ctf_field_variant_get_field: get a variant's selected field.
+ *
+ * Return the variant's selected field. The "tag" field is the selector enum
+ * field. bt_ctf_field_put() must be called on the returned value.
+ *
+ * @param variant Variant field instance.
+ * @param tag Selector enumeration field.
+ *
+ * Returns a field instance on success, NULL on error.
+ */
+extern struct bt_ctf_field *bt_ctf_field_variant_get_field(
+               struct bt_ctf_field *variant, struct bt_ctf_field *tag);
+
+/*
+ * bt_ctf_field_enumeration_get_container: get an enumeration field's container.
+ *
+ * Return the enumeration's underlying container field (an integer).
+ * bt_ctf_field_put() must be called on the returned value.
+ *
+ * @param enumeration Enumeration field instance.
+ *
+ * Returns a field instance on success, NULL on error.
+ */
+extern struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
+               struct bt_ctf_field *enumeration);
+
+/*
+ * bt_ctf_field_signed_integer_set_value: set a signed integer field's value
+ *
+ * Set a signed integer field's value. The value is checked to make sure it
+ * can be stored in the underlying field.
+ *
+ * @param integer Signed integer field instance.
+ * @param value Signed integer field value.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *integer,
+               int64_t value);
+
+/*
+ * bt_ctf_field_unsigned_integer_set_value: set unsigned integer field's value
+ *
+ * Set an unsigned integer field's value. The value is checked to make sure it
+ * can be stored in the underlying field.
+ *
+ * @param integer Unsigned integer field instance.
+ * @param value Unsigned integer field value.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *integer,
+               uint64_t value);
+
+/*
+ * bt_ctf_field_floating_point_set_value: set a floating point field's value
+ *
+ * Set a floating point field's value. The underlying type may not support the
+ * double's full precision.
+ *
+ * @param floating_point Floating point field instance.
+ * @param value Floating point field value.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_floating_point_set_value(
+               struct bt_ctf_field *floating_point,
+               double value);
+
+/*
+ * bt_ctf_field_string_set_value: set a string field's value
+ *
+ * Set a string field's value.
+ *
+ * @param string String field instance.
+ * @param value String field value (will be copied).
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_string_set_value(struct bt_ctf_field *string,
+               const char *value);
+
+/*
+ * bt_ctf_field_get and bt_ctf_field_put: increment and decrement the
+ * field's reference count.
+ *
+ * These functions ensure that the field won't be destroyed when it
+ * is in use. The same number of get and put (plus one extra put to
+ * release the initial reference done at creation) have to be done to
+ * destroy a field.
+ *
+ * When the field's reference count is decremented to 0 by a bt_ctf_field_put,
+ * the field is freed.
+ *
+ * @param field Field instance.
+ */
+extern void bt_ctf_field_get(struct bt_ctf_field *field);
+extern void bt_ctf_field_put(struct bt_ctf_field *field);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BABELTRACE_CTF_WRITER_EVENT_FIELDS_H */
diff --git a/include/babeltrace/ctf-writer/event-internal.h b/include/babeltrace/ctf-writer/event-internal.h
new file mode 100644 (file)
index 0000000..b84c3dd
--- /dev/null
@@ -0,0 +1,90 @@
+#ifndef BABELTRACE_CTF_WRITER_EVENT_INTERNAL_H
+#define BABELTRACE_CTF_WRITER_EVENT_INTERNAL_H
+
+/*
+ * BabelTrace - CTF Writer: Event internal
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/ctf-writer/event-types.h>
+#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/ctf/types.h>
+#include <glib.h>
+
+struct bt_ctf_event_class {
+       struct bt_ctf_ref ref_count;
+       GQuark name;
+       int id_set;
+       uint32_t id;
+       int stream_id_set;
+       uint32_t stream_id;
+       /* Structure type containing the event's context */
+       struct bt_ctf_field_type *context;
+       /* Structure type containing the event's fields */
+       struct bt_ctf_field_type *fields;
+       int frozen;
+};
+
+struct bt_ctf_event {
+       struct bt_ctf_ref ref_count;
+       uint64_t timestamp;
+       struct bt_ctf_event_class *event_class;
+       struct bt_ctf_field *context_payload;
+       struct bt_ctf_field *fields_payload;
+};
+
+BT_HIDDEN
+void bt_ctf_event_class_freeze(struct bt_ctf_event_class *event_class);
+
+BT_HIDDEN
+int bt_ctf_event_class_set_id(struct bt_ctf_event_class *event_class,
+               uint32_t id);
+
+BT_HIDDEN
+uint32_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class);
+
+BT_HIDDEN
+int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
+               uint32_t id);
+
+BT_HIDDEN
+int bt_ctf_event_class_serialize(struct bt_ctf_event_class *event_class,
+               struct metadata_context *context);
+
+BT_HIDDEN
+int bt_ctf_event_validate(struct bt_ctf_event *event);
+
+BT_HIDDEN
+int bt_ctf_event_serialize(struct bt_ctf_event *event,
+               struct ctf_stream_pos *pos);
+
+BT_HIDDEN
+int bt_ctf_event_set_timestamp(struct bt_ctf_event *event, uint64_t timestamp);
+
+BT_HIDDEN
+uint64_t bt_ctf_event_get_timestamp(struct bt_ctf_event *event);
+
+#endif /* BABELTRACE_CTF_WRITER_EVENT_INTERNAL_H */
diff --git a/include/babeltrace/ctf-writer/event-types-internal.h b/include/babeltrace/ctf-writer/event-types-internal.h
new file mode 100644 (file)
index 0000000..d9acdd3
--- /dev/null
@@ -0,0 +1,150 @@
+#ifndef BABELTRACE_CTF_WRITER_EVENT_TYPES_INTERNAL_H
+#define BABELTRACE_CTF_WRITER_EVENT_TYPES_INTERNAL_H
+
+/*
+ * BabelTrace - CTF Writer: Event types internal
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/event-types.h>
+#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/ctf-writer/writer.h>
+#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/types.h>
+#include <babeltrace/ctf/events.h>
+#include <glib.h>
+
+typedef void(*type_freeze_func)(struct bt_ctf_field_type *);
+typedef int(*type_serialize_func)(struct bt_ctf_field_type *,
+               struct metadata_context *);
+
+struct bt_ctf_field_type {
+       struct bt_ctf_ref ref_count;
+       struct bt_declaration *declaration;
+       type_freeze_func freeze;
+       type_serialize_func serialize;
+       /*
+        * A type can't be modified once it is added to an event or after a
+        * a field has been instanciated from it.
+        */
+       int frozen;
+};
+
+struct bt_ctf_field_type_integer {
+       struct bt_ctf_field_type parent;
+       struct declaration_integer declaration;
+};
+
+struct enumeration_mapping {
+       int64_t range_start;
+       int64_t range_end;
+       GQuark string;
+};
+
+struct bt_ctf_field_type_enumeration {
+       struct bt_ctf_field_type parent;
+       struct bt_ctf_field_type *container;
+       GPtrArray *entries; /* Array of pointers to struct enum_mapping */
+       struct declaration_enum declaration;
+};
+
+struct bt_ctf_field_type_floating_point {
+       struct bt_ctf_field_type parent;
+       struct declaration_float declaration;
+       struct declaration_integer sign;
+       struct declaration_integer mantissa;
+       struct declaration_integer exp;
+};
+
+struct structure_field {
+       GQuark name;
+       struct bt_ctf_field_type *type;
+};
+
+struct bt_ctf_field_type_structure {
+       struct bt_ctf_field_type parent;
+       GHashTable *field_name_to_index;
+       GPtrArray *fields; /* Array of pointers to struct structure_field */
+       struct declaration_enum declaration;
+};
+
+struct bt_ctf_field_type_variant {
+       struct bt_ctf_field_type parent;
+       GString *tag_name;
+       struct bt_ctf_field_type_enumeration *tag;
+       GHashTable *field_name_to_index;
+       GPtrArray *fields; /* Array of pointers to struct structure_field */
+       struct declaration_variant declaration;
+};
+
+struct bt_ctf_field_type_array {
+       struct bt_ctf_field_type parent;
+       struct bt_ctf_field_type *element_type;
+       unsigned int length; /* Number of elements */
+       struct declaration_array declaration;
+};
+
+struct bt_ctf_field_type_sequence {
+       struct bt_ctf_field_type parent;
+       struct bt_ctf_field_type *element_type;
+       GString *length_field_name;
+       struct declaration_sequence declaration;
+};
+
+struct bt_ctf_field_type_string {
+       struct bt_ctf_field_type parent;
+       struct declaration_string declaration;
+};
+
+BT_HIDDEN
+void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type);
+
+BT_HIDDEN
+enum ctf_type_id bt_ctf_field_type_get_type_id(
+               struct bt_ctf_field_type *type);
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_structure_get_type(
+               struct bt_ctf_field_type_structure *structure,
+               const char *name);
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type(
+               struct bt_ctf_field_type_array *array);
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type(
+               struct bt_ctf_field_type_sequence *sequence);
+
+BT_HIDDEN
+struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type(
+               struct bt_ctf_field_type_variant *variant, int64_t tag_value);
+
+BT_HIDDEN
+int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
+               struct metadata_context *context);
+
+#endif /* BABELTRACE_CTF_WRITER_EVENT_TYPES_INTERNAL_H */
diff --git a/include/babeltrace/ctf-writer/event-types.h b/include/babeltrace/ctf-writer/event-types.h
new file mode 100644 (file)
index 0000000..cf43ed6
--- /dev/null
@@ -0,0 +1,353 @@
+#ifndef BABELTRACE_CTF_WRITER_EVENT_TYPES_H
+#define BABELTRACE_CTF_WRITER_EVENT_TYPES_H
+
+/*
+ * BabelTrace - CTF Writer: Event Types
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * The Common Trace Format (CTF) Specification is available at
+ * http://www.efficios.com/ctf
+ */
+
+#include <babeltrace/ctf-writer/writer.h>
+#include <babeltrace/ctf/events.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct bt_ctf_event_class;
+struct bt_ctf_event;
+struct bt_ctf_field_type;
+
+enum bt_ctf_integer_base {
+       BT_CTF_INTEGER_BASE_UNKNOWN = -1,
+       BT_CTF_INTEGER_BASE_BINARY = 2,
+       BT_CTF_INTEGER_BASE_OCTAL = 8,
+       BT_CTF_INTEGER_BASE_DECIMAL = 10,
+       BT_CTF_INTEGER_BASE_HEXADECIMAL = 16,
+};
+
+/*
+ * bt_ctf_field_type_integer_create: create an integer field type.
+ *
+ * Allocate a new integer field type of the given size. The creation of a field
+ * type sets its reference count to 1.
+ *
+ * @param size Integer field type size/length in bits.
+ *
+ * Returns an allocated field type on success, NULL on error.
+ */
+extern struct bt_ctf_field_type *bt_ctf_field_type_integer_create(
+               unsigned int size);
+
+/*
+ * bt_ctf_field_type_integer_set_signed: set an integer type's signedness.
+ *
+ * Set an integer type's signedness attribute.
+ *
+ * @param integer Integer type.
+ * @param is_signed Integer's signedness, defaults to FALSE.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_integer_set_signed(
+               struct bt_ctf_field_type *integer, int is_signed);
+
+/*
+ * bt_ctf_field_type_integer_set_base: set an integer type's base.
+ *
+ * Set an integer type's base used to pretty-print the resulting trace.
+ *
+ * @param integer Integer type.
+ * @param base Integer base, defaults to BT_CTF_INTEGER_BASE_DECIMAL.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_integer_set_base(
+               struct bt_ctf_field_type *integer,
+               enum bt_ctf_integer_base base);
+
+/*
+ * bt_ctf_field_type_integer_set_encoding: set an integer type's encoding.
+ *
+ * An integer encoding may be set to signal that the integer must be printed as
+ * a text character.
+ *
+ * @param integer Integer type.
+ * @param encoding Integer output encoding, defaults to CTF_STRING_ENCODING_NONE
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_integer_set_encoding(
+               struct bt_ctf_field_type *integer,
+               enum ctf_string_encoding encoding);
+
+/*
+ * bt_ctf_field_type_enumeration_create: create an enumeration field type.
+ *
+ * Allocate a new enumeration field type with the given underlying type. The
+ * creation of a field type sets its reference count to 1.
+ * The resulting enumeration will share the integer_container_type's ownership
+ * by increasing its reference count.
+ *
+ * @param integer_container_type Underlying integer type of the enumeration
+ *     type.
+ *
+ * Returns an allocated field type on success, NULL on error.
+ */
+extern struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create(
+               struct bt_ctf_field_type *integer_container_type);
+
+/*
+ * bt_ctf_field_type_enumeration_add_mapping: add an enumeration mapping.
+ *
+ * Add a mapping to the enumeration. The range's values are inclusive.
+ *
+ * @param enumeration Enumeration type.
+ * @param string Enumeration mapping name (will be copied).
+ * @param range_start Enumeration mapping range start.
+ * @param range_end Enumeration mapping range end.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_enumeration_add_mapping(
+               struct bt_ctf_field_type *enumeration, const char *string,
+               int64_t range_start, int64_t range_end);
+
+/*
+ * bt_ctf_field_type_floating_point_create: create a floating point field type.
+ *
+ * Allocate a new floating point field type. The creation of a field type sets
+ * its reference count to 1.
+ *
+ * Returns an allocated field type on success, NULL on error.
+ */
+extern struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void);
+
+/*
+ * bt_ctf_field_type_floating_point_set_exponent_digits: set exponent digit
+ * count.
+ *
+ * Set the number of exponent digits to use to store the floating point field.
+ * The only values currently supported are FLT_EXP_DIG and DBL_EXP_DIG.
+ *
+ * @param floating_point Floating point type.
+ * @param exponent_digits Number of digits to allocate to the exponent (defaults
+ *     to FLT_EXP_DIG).
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_floating_point_set_exponent_digits(
+               struct bt_ctf_field_type *floating_point,
+               unsigned int exponent_digits);
+
+/*
+ * bt_ctf_field_type_floating_point_set_mantissa_digits: set mantissa digit
+ * count.
+ *
+ * Set the number of mantissa digits to use to store the floating point field.
+ * The only values currently supported are FLT_MANT_DIG and DBL_MANT_DIG.
+ *
+ * @param floating_point Floating point type.
+ * @param mantissa_digits Number of digits to allocate to the mantissa (defaults
+ *     to FLT_MANT_DIG).
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_floating_point_set_mantissa_digits(
+               struct bt_ctf_field_type *floating_point,
+               unsigned int mantissa_digits);
+
+/*
+ * bt_ctf_field_type_structure_create: create a structure field type.
+ *
+ * Allocate a new structure field type. The creation of a field type sets
+ * its reference count to 1.
+ *
+ * Returns an allocated field type on success, NULL on error.
+ */
+extern struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void);
+
+/*
+ * bt_ctf_field_type_structure_add_field: add a field to a structure.
+ *
+ * Add a field of type "field_type" to the structure. The structure will share
+ * field_type's ownership by increasing its reference count.
+ *
+ * @param structure Structure type.
+ * @param field_type Type of the field to add to the structure type.
+ * @param field_name Name of the structure's new field (will be copied).
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_structure_add_field(
+               struct bt_ctf_field_type *structure,
+               struct bt_ctf_field_type *field_type,
+               const char *field_name);
+
+/*
+ * bt_ctf_field_type_variant_create: create a variant field type.
+ *
+ * Allocate a new variant field type. The creation of a field type sets
+ * its reference count to 1. tag_name must be the name of an enumeration
+ * field declared in the same scope as this variant.
+ *
+ * @param enum_tag Type of the variant's tag/selector (must be an enumeration).
+ * @param tag_name Name of the variant's tag/selector field (will be copied).
+ *
+ * Returns an allocated field type on success, NULL on error.
+ */
+extern struct bt_ctf_field_type *bt_ctf_field_type_variant_create(
+               struct bt_ctf_field_type *enum_tag,
+               const char *tag_name);
+
+/*
+ * bt_ctf_field_type_variant_add_field: add a field to a variant.
+ *
+ * Add a field of type "field_type" to the variant.The variant will share
+ * field_type's ownership by increasing its reference count. The "field_name"
+ * will be copied. field_name must match a mapping in the tag/selector
+ * enumeration.
+ *
+ * @param variant Variant type.
+ * @param field_type Type of the variant type's new field.
+ * @param field_name Name of the variant type's new field (will be copied).
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_variant_add_field(
+               struct bt_ctf_field_type *variant,
+               struct bt_ctf_field_type *field_type,
+               const char *field_name);
+
+/*
+ * bt_ctf_field_type_array_create: create an array field type.
+ *
+ * Allocate a new array field type. The creation of a field type sets
+ * its reference count to 1.
+ *
+ * @param element_type Array's element type.
+ * @oaram length Array type's length.
+ *
+ * Returns an allocated field type on success, NULL on error.
+ */
+extern struct bt_ctf_field_type *bt_ctf_field_type_array_create(
+               struct bt_ctf_field_type *element_type,
+               unsigned int length);
+
+/*
+ * bt_ctf_field_type_sequence_create: create a sequence field type.
+ *
+ * Allocate a new sequence field type. The creation of a field type sets
+ * its reference count to 1. "length_field_name" must match an integer field
+ * declared in the same scope.
+ *
+ * @param element_type Sequence's element type.
+ * @param length_field_name Name of the sequence's length field (will be
+ *     copied).
+ *
+ * Returns an allocated field type on success, NULL on error.
+ */
+extern struct bt_ctf_field_type *bt_ctf_field_type_sequence_create(
+               struct bt_ctf_field_type *element_type,
+               const char *length_field_name);
+
+/*
+ * bt_ctf_field_type_string_create: create a string field type.
+ *
+ * Allocate a new string field type. The creation of a field type sets
+ * its reference count to 1.
+ *
+ * Returns an allocated field type on success, NULL on error.
+ */
+extern struct bt_ctf_field_type *bt_ctf_field_type_string_create(void);
+
+/*
+ * bt_ctf_field_type_string_set_encoding: set a string type's encoding.
+ *
+ * Set the string type's encoding.
+ *
+ * @param string String type.
+ * @param encoding String field encoding, default CTF_STRING_ENCODING_ASCII.
+ *     Valid values are CTF_STRING_ENCODING_ASCII and CTF_STRING_ENCODING_UTF8.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_string_set_encoding(
+               struct bt_ctf_field_type *string,
+               enum ctf_string_encoding encoding);
+
+/*
+ * bt_ctf_field_type_set_alignment: set a field type's alignment.
+ *
+ * Set the field type's alignment.
+ *
+ * @param type Field type.
+ * @param alignment Type's alignment. Defaults to 1 (bit-aligned). However,
+ *     some types, such as structures and string, may impose other alignment
+ *     constraints.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type,
+               unsigned int alignment);
+
+/*
+ * bt_ctf_field_type_set_byte_order: set a field type's byte order.
+ *
+ * Set the field type's byte order.
+ *
+ * @param type Field type.
+ * @param byte_order Field type's byte order. Defaults to
+ * BT_CTF_BYTE_ORDER_NATIVE, the host machine's endianness.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type,
+               enum bt_ctf_byte_order byte_order);
+
+/*
+ * bt_ctf_field_type_get and bt_ctf_field_type_put: increment and decrement
+ * the field type's reference count.
+ *
+ * These functions ensure that the field type won't be destroyed while it
+ * is in use. The same number of get and put (plus one extra put to
+ * release the initial reference done at creation) have to be done to
+ * destroy a field type.
+ *
+ * When the field type's reference count is decremented to 0 by a
+ * bt_ctf_field_type_put, the field type is freed.
+ *
+ * @param type Field type.
+ */
+extern void bt_ctf_field_type_get(struct bt_ctf_field_type *type);
+extern void bt_ctf_field_type_put(struct bt_ctf_field_type *type);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BABELTRACE_CTF_WRITER_EVENT_TYPES_H */
diff --git a/include/babeltrace/ctf-writer/event.h b/include/babeltrace/ctf-writer/event.h
new file mode 100644 (file)
index 0000000..d8ef9d9
--- /dev/null
@@ -0,0 +1,154 @@
+#ifndef BABELTRACE_CTF_WRITER_EVENT_H
+#define BABELTRACE_CTF_WRITER_EVENT_H
+
+/*
+ * BabelTrace - CTF Writer: Event
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * The Common Trace Format (CTF) Specification is available at
+ * http://www.efficios.com/ctf
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct bt_ctf_event_class;
+struct bt_ctf_event;
+struct bt_ctf_field;
+struct bt_ctf_field_type;
+
+/*
+ * bt_ctf_event_class_create: create an event class.
+ *
+ * Allocate a new event class of the given name. The creation of an event class
+ * sets its reference count to 1.
+ *
+ * @param name Event class name (will be copied).
+ *
+ * Returns an allocated event class on success, NULL on error.
+ */
+extern struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name);
+
+/*
+ * bt_ctf_event_class_add_field: add a field to an event class.
+ *
+ * Add a field of type "type" to the event class. The event class will share
+ * type's ownership by increasing its reference count. The "name" will be
+ * copied.
+ *
+ * @param event_class Event class.
+ * @param type Field type to add to the event class.
+ * @param name Name of the new field.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_event_class_add_field(struct bt_ctf_event_class *event_class,
+               struct bt_ctf_field_type *type,
+               const char *name);
+
+/*
+ * bt_ctf_event_class__get and bt_ctf_event_class_put: increment and decrement
+ * the event class' reference count.
+ *
+ * These functions ensure that the event class won't be destroyed while it
+ * is in use. The same number of get and put (plus one extra put to
+ * release the initial reference done at creation) have to be done to
+ * destroy an event class.
+ *
+ * When the event class' reference count is decremented to 0 by a
+ * bt_ctf_event_class_put, the event class is freed.
+ *
+ * @param event_class Event class.
+ */
+extern void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class);
+extern void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class);
+
+/*
+ * bt_ctf_event_create: instanciate an event.
+ *
+ * Allocate a new event of the given event class. The creation of an event
+ * sets its reference count to 1. Each instance shares the ownership of the
+ * event class using its reference count.
+ *
+ * @param event_class Event class.
+ *
+ * Returns an allocated field type on success, NULL on error.
+ */
+extern struct bt_ctf_event *bt_ctf_event_create(
+               struct bt_ctf_event_class *event_class);
+
+/*
+ * bt_ctf_event_set_payload: set an event's field.
+ *
+ * Set a manually allocated field as an event's payload. The event will share
+ * the field's ownership by using its reference count.
+ * bt_ctf_field_put() must be called on the returned value.
+ *
+ * @param event Event instance.
+ * @param name Event field name.
+ * @param value Instance of a field whose type corresponds to the event's field.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_event_set_payload(struct bt_ctf_event *event,
+               const char *name,
+               struct bt_ctf_field *value);
+
+/*
+ * bt_ctf_event_get_payload: get an event's field.
+ *
+ * Returns the field matching "name". bt_ctf_field_put() must be called on the
+ * returned value.
+ *
+ * @param event Event instance.
+ * @param name Event field name.
+ *
+ * Returns a field instance on success, NULL on error.
+ */
+extern struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
+               const char *name);
+
+/*
+ * bt_ctf_event_get and bt_ctf_event_put: increment and decrement
+ * the event's reference count.
+ *
+ * These functions ensure that the event won't be destroyed while it
+ * is in use. The same number of get and put (plus one extra put to
+ * release the initial reference done at creation) have to be done to
+ * destroy an event.
+ *
+ * When the event's reference count is decremented to 0 by a
+ * bt_ctf_event_put, the event is freed.
+ *
+ * @param event Event instance.
+ */
+extern void bt_ctf_event_get(struct bt_ctf_event *event);
+extern void bt_ctf_event_put(struct bt_ctf_event *event);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BABELTRACE_CTF_WRITER_EVENT_H */
diff --git a/include/babeltrace/ctf-writer/functor-internal.h b/include/babeltrace/ctf-writer/functor-internal.h
new file mode 100644 (file)
index 0000000..66d3fcf
--- /dev/null
@@ -0,0 +1,41 @@
+#ifndef BABELTRACE_CTF_WRITER_FUNCTOR_INTERNAL_H
+#define BABELTRACE_CTF_WRITER_FUNCTOR_INTERNAL_H
+
+/*
+ * BabelTrace - CTF Writer: Functors for use with glib data structures
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <glib.h>
+#include <babeltrace/babeltrace-internal.h>
+
+struct search_query {
+       gpointer value;
+       int found;
+};
+
+BT_HIDDEN
+void value_exists(gpointer element, gpointer search_query);
+
+#endif /* BABELTRACE_CTF_WRITER_FUNCTOR_INTERNAL_H */
diff --git a/include/babeltrace/ctf-writer/ref-internal.h b/include/babeltrace/ctf-writer/ref-internal.h
new file mode 100644 (file)
index 0000000..486e243
--- /dev/null
@@ -0,0 +1,61 @@
+#ifndef BABELTRACE_CTF_WRITER_REF_INTERNAL_H
+#define BABELTRACE_CTF_WRITER_REF_INTERNAL_H
+
+/*
+ * BabelTrace - CTF Writer: Reference count
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <assert.h>
+
+struct bt_ctf_ref {
+       long refcount;
+};
+
+static inline
+void bt_ctf_ref_init(struct bt_ctf_ref *ref)
+{
+       assert(ref);
+       ref->refcount = 1;
+}
+
+static inline
+void bt_ctf_ref_get(struct bt_ctf_ref *ref)
+{
+       assert(ref);
+       ref->refcount++;
+}
+
+static inline
+void bt_ctf_ref_put(struct bt_ctf_ref *ref,
+               void (*release)(struct bt_ctf_ref *))
+{
+       assert(ref);
+       assert(release);
+       if ((--ref->refcount) == 0) {
+               release(ref);
+       }
+}
+
+#endif /* BABELTRACE_CTF_WRITER_REF_INTERNAL_H */
diff --git a/include/babeltrace/ctf-writer/stream-internal.h b/include/babeltrace/ctf-writer/stream-internal.h
new file mode 100644 (file)
index 0000000..57dd992
--- /dev/null
@@ -0,0 +1,101 @@
+#ifndef BABELTRACE_CTF_WRITER_STREAM_INTERNAL_H
+#define BABELTRACE_CTF_WRITER_STREAM_INTERNAL_H
+
+/*
+ * BabelTrace - CTF Writer: Stream internal
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/ctf-writer/clock.h>
+#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/ctf-writer/event-types.h>
+#include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/ctf/types.h>
+#include <glib.h>
+
+typedef void(*flush_func)(struct bt_ctf_stream *, void *);
+
+struct bt_ctf_stream_class {
+       struct bt_ctf_ref ref_count;
+       GString *name;
+       struct bt_ctf_clock *clock;
+       GPtrArray *event_classes; /* Array of pointers to bt_ctf_event_class */
+       int id_set;
+       uint32_t id;
+       uint32_t next_event_id;
+       uint32_t next_stream_id;
+       struct bt_ctf_field_type *event_header_type;
+       struct bt_ctf_field *event_header;
+       struct bt_ctf_field_type *packet_context_type;
+       struct bt_ctf_field *packet_context;
+       struct bt_ctf_field_type *event_context_type;
+       struct bt_ctf_field *event_context;
+       int frozen;
+};
+
+struct flush_callback {
+       flush_func func;
+       void *data;
+};
+
+struct bt_ctf_stream {
+       struct bt_ctf_ref ref_count;
+       uint32_t id;
+       struct bt_ctf_stream_class *stream_class;
+       struct flush_callback flush;
+       /* Array of pointers to bt_ctf_event for the current packet */
+       GPtrArray *events;
+       struct ctf_stream_pos pos;
+       unsigned int flushed_packet_count;
+       uint64_t events_discarded;
+};
+
+BT_HIDDEN
+void bt_ctf_stream_class_freeze(struct bt_ctf_stream_class *stream_class);
+
+BT_HIDDEN
+int bt_ctf_stream_class_set_id(struct bt_ctf_stream_class *stream_class,
+               uint32_t id);
+
+BT_HIDDEN
+int bt_ctf_stream_class_serialize(struct bt_ctf_stream_class *stream_class,
+               struct metadata_context *context);
+
+BT_HIDDEN
+int bt_ctf_stream_class_set_byte_order(struct bt_ctf_stream_class *stream_class,
+               enum bt_ctf_byte_order byte_order);
+
+BT_HIDDEN
+struct bt_ctf_stream *bt_ctf_stream_create(
+               struct bt_ctf_stream_class *stream_class);
+
+BT_HIDDEN
+int bt_ctf_stream_set_flush_callback(struct bt_ctf_stream *stream,
+               flush_func callback, void *data);
+
+BT_HIDDEN
+int bt_ctf_stream_set_fd(struct bt_ctf_stream *stream, int fd);
+
+#endif /* BABELTRACE_CTF_WRITER_STREAM_INTERNAL_H */
diff --git a/include/babeltrace/ctf-writer/stream.h b/include/babeltrace/ctf-writer/stream.h
new file mode 100644 (file)
index 0000000..9229f77
--- /dev/null
@@ -0,0 +1,169 @@
+#ifndef BABELTRACE_CTF_WRITER_STREAM_H
+#define BABELTRACE_CTF_WRITER_STREAM_H
+
+/*
+ * BabelTrace - CTF Writer: Stream
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * The Common Trace Format (CTF) Specification is available at
+ * http://www.efficios.com/ctf
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct bt_ctf_event;
+struct bt_ctf_event_class;
+struct bt_ctf_stream_class;
+struct bt_ctf_stream;
+struct bt_ctf_clock;
+
+/*
+ * bt_ctf_stream_class_create: create a stream class.
+ *
+ * Allocate a new stream class of the given name. The creation of an event class
+ * sets its reference count to 1.
+ *
+ * @param name Stream name.
+ *
+ * Returns an allocated stream class on success, NULL on error.
+ */
+extern struct bt_ctf_stream_class *bt_ctf_stream_class_create(const char *name);
+
+/*
+ * bt_ctf_stream_class_set_clock: assign a clock to a stream class.
+ *
+ * Assign a clock to a stream class. This clock will be sampled each time an
+ * event is appended to an instance of this stream class.
+ *
+ * @param stream_class Stream class.
+ * @param clock Clock to assign to the provided stream class.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_stream_class_set_clock(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_clock *clock);
+
+/*
+ * bt_ctf_stream_class_set_clock: assign a clock to a stream class.
+ *
+ * Add an event class to a stream class. New events can be added even after a
+ * stream has beem instanciated and events have been appended. However, a stream
+ * will not accept events of a class that has not been registered beforehand.
+ * The stream class will share the ownership of "event_class" by incrementing
+ * its reference count.
+ *
+ * @param stream_class Stream class.
+ * @param event_class Event class to add to the provided stream class.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_stream_class_add_event_class(
+               struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_event_class *event_class);
+
+/*
+ * bt_ctf_stream_class_get and bt_ctf_stream_class_put: increment and
+ * decrement the stream class' reference count.
+ *
+ * These functions ensure that the stream class won't be destroyed while it
+ * is in use. The same number of get and put (plus one extra put to
+ * release the initial reference done at creation) have to be done to
+ * destroy a stream class.
+ *
+ * When the stream class' reference count is decremented to 0 by a
+ * bt_ctf_stream_class_put, the stream class is freed.
+ *
+ * @param stream_class Stream class.
+ */
+extern void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
+extern void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
+
+/*
+ * bt_ctf_stream_append_discarded_events: increment discarded events count.
+ *
+ * Increase the current packet's discarded event count.
+ *
+ * @param stream Stream instance.
+ * @param event_count Number of discarded events to add to the stream's current
+ *     packet.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern void bt_ctf_stream_append_discarded_events(struct bt_ctf_stream *stream,
+               uint64_t event_count);
+
+/*
+ * bt_ctf_stream_append_event: append an event to the stream.
+ *
+ * Append "event" to the stream's current packet. The stream's associated clock
+ * will be sampled during this call. The event shall not be modified after
+ * being appended to a stream. The stream will share the event's ownership by
+ * incrementing its reference count. The current packet is not flushed to disk
+ * until the next call to bt_ctf_stream_flush.
+ *
+ * @param stream Stream instance.
+ * @param event Event instance to append to the stream's current packet.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_stream_append_event(struct bt_ctf_stream *stream,
+               struct bt_ctf_event *event);
+
+/*
+ * bt_ctf_stream_flush: flush a stream.
+ *
+ * The stream's current packet's events will be flushed to disk. Events
+ * subsequently appended to the stream will be added to a new packet.
+ *
+ * @param stream Stream instance.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_stream_flush(struct bt_ctf_stream *stream);
+
+/*
+ * bt_ctf_stream_get and bt_ctf_stream_put: increment and decrement the
+ * stream's reference count.
+ *
+ * These functions ensure that the stream won't be destroyed while it
+ * is in use. The same number of get and put (plus one extra put to
+ * release the initial reference done at creation) have to be done to
+ * destroy a stream.
+ *
+ * When the stream's reference count is decremented to 0 by a
+ * bt_ctf_stream_put, the stream is freed.
+ *
+ * @param stream Stream instance.
+ */
+extern void bt_ctf_stream_get(struct bt_ctf_stream *stream);
+extern void bt_ctf_stream_put(struct bt_ctf_stream *stream);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BABELTRACE_CTF_WRITER_STREAM_H */
diff --git a/include/babeltrace/ctf-writer/writer-internal.h b/include/babeltrace/ctf-writer/writer-internal.h
new file mode 100644 (file)
index 0000000..5c682dd
--- /dev/null
@@ -0,0 +1,87 @@
+#ifndef BABELTRACE_CTF_WRITER_WRITER_INTERNAL_H
+#define BABELTRACE_CTF_WRITER_WRITER_INTERNAL_H
+
+/*
+ * BabelTrace - CTF Writer: Writer internal
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <babeltrace/ctf-writer/ref-internal.h>
+#include <babeltrace/ctf-writer/writer.h>
+#include <babeltrace/ctf-writer/event-types.h>
+#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/babeltrace-internal.h>
+#include <glib.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <uuid/uuid.h>
+
+enum field_type_alias {
+       FIELD_TYPE_ALIAS_UINT5_T = 0,
+       FIELD_TYPE_ALIAS_UINT8_T,
+       FIELD_TYPE_ALIAS_UINT16_T,
+       FIELD_TYPE_ALIAS_UINT27_T,
+       FIELD_TYPE_ALIAS_UINT32_T,
+       FIELD_TYPE_ALIAS_UINT64_T,
+       NR_FIELD_TYPE_ALIAS,
+};
+
+struct bt_ctf_writer {
+       struct bt_ctf_ref ref_count;
+       int frozen; /* Protects attributes that can't be changed mid-trace */
+       GString *path;
+       uuid_t uuid;
+       int byte_order;
+       int trace_dir_fd;
+       int metadata_fd;
+       GPtrArray *environment; /* Array of pointers to environment_variable */
+       GPtrArray *clocks; /* Array of pointers to bt_ctf_clock */
+       GPtrArray *stream_classes; /* Array of pointers to bt_ctf_stream_class */
+       GPtrArray *streams; /* Array of pointers to bt_ctf_stream */
+       struct bt_ctf_field_type *trace_packet_header_type;
+       struct bt_ctf_field *trace_packet_header;
+       uint32_t next_stream_id;
+};
+
+struct environment_variable {
+       GString *name, *value;
+};
+
+struct metadata_context {
+       GString *string;
+       GString *field_name;
+       unsigned int current_indentation_level;
+};
+
+/* Checks that the string does not contain a reserved keyword */
+BT_HIDDEN
+int validate_identifier(const char *string);
+
+BT_HIDDEN
+const char *get_byte_order_string(int byte_order);
+
+BT_HIDDEN
+struct bt_ctf_field_type *get_field_type(enum field_type_alias alias);
+
+#endif /* BABELTRACE_CTF_WRITER_WRITER_INTERNAL_H */
diff --git a/include/babeltrace/ctf-writer/writer.h b/include/babeltrace/ctf-writer/writer.h
new file mode 100644 (file)
index 0000000..68031ac
--- /dev/null
@@ -0,0 +1,164 @@
+#ifndef BABELTRACE_CTF_WRITER_WRITER_H
+#define BABELTRACE_CTF_WRITER_WRITER_H
+
+/*
+ * BabelTrace - CTF Writer: Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * The Common Trace Format (CTF) Specification is available at
+ * http://www.efficios.com/ctf
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct bt_ctf_writer;
+struct bt_ctf_stream;
+struct bt_ctf_stream_class;
+struct bt_ctf_clock;
+
+enum bt_ctf_byte_order {
+       BT_CTF_BYTE_ORDER_NATIVE = 0,
+       BT_CTF_BYTE_ORDER_LITTLE_ENDIAN,
+       BT_CTF_BYTE_ORDER_BIG_ENDIAN,
+       BT_CTF_BYTE_ORDER_NETWORK,
+};
+
+/*
+ * bt_ctf_writer_create: create a writer instance.
+ *
+ * Allocate a new writer that will produce a trace in the given path.
+ * The creation of a writer sets its reference count to 1.
+ *
+ * @param path Path to the trace's containing folder (string is copied).
+ *
+ * Returns an allocated writer on success, NULL on error.
+ */
+extern struct bt_ctf_writer *bt_ctf_writer_create(const char *path);
+
+/*
+ * bt_ctf_writer_create_stream: create a stream instance.
+ *
+ * Allocate a new stream instance and register it to the writer. The creation of
+ * a stream sets its reference count to 1.
+ *
+ * @param writer Writer instance.
+ * @param stream_class Stream class to instantiate.
+ *
+ * Returns an allocated writer on success, NULL on error.
+ */
+extern struct bt_ctf_stream *bt_ctf_writer_create_stream(
+               struct bt_ctf_writer *writer,
+               struct bt_ctf_stream_class *stream_class);
+
+/*
+ * bt_ctf_writer_add_environment_field: add an environment field to the trace.
+ *
+ * Add an environment field to the trace. The name and value parameters are
+ * copied.
+ *
+ * @param writer Writer instance.
+ * @param name Name of the environment field (will be copied).
+ * @param value Value of the environment field (will be copied).
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
+               const char *name,
+               const char *value);
+
+/*
+ * bt_ctf_writer_add_clock: add a clock to the trace.
+ *
+ * Add a clock to the trace. Clocks assigned to stream classes must be
+ * registered to the writer.
+ *
+ * @param writer Writer instance.
+ * @param clock Clock to add to the trace.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
+               struct bt_ctf_clock *clock);
+
+/*
+ * bt_ctf_writer_get_metadata_string: get meta-data string.
+ *
+ * Get the trace's TSDL meta-data. The caller assumes the ownership of the
+ * returned string.
+ *
+ * @param writer Writer instance.
+ *
+ * Returns the metadata string on success, NULL on error.
+ */
+extern char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer);
+
+/*
+ * bt_ctf_writer_flush_metadata: flush the trace's metadata to disk.
+ *
+ * Flush the trace's metadata to the metadata file. Note that the metadata will
+ * be flushed automatically when the Writer instance is released (last call to
+ * bt_ctf_writer_put).
+ *
+ * @param writer Writer instance.
+ */
+extern void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer);
+
+/*
+ * bt_ctf_writer_set_byte_order: set a field type's byte order.
+ *
+ * Set the trace's byte order. Defaults to BT_CTF_BYTE_ORDER_NATIVE,
+ * the host machine's endianness.
+ *
+ * @param writer Writer instance.
+ * @param byte_order Trace's byte order.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_writer_set_byte_order(struct bt_ctf_writer *writer,
+               enum bt_ctf_byte_order byte_order);
+
+/*
+ * bt_ctf_writer_get and bt_ctf_writer_put: increment and decrement the
+ * writer's reference count.
+ *
+ * These functions ensure that the writer won't be destroyed while it
+ * is in use. The same number of get and put (plus one extra put to
+ * release the initial reference done at creation) have to be done to
+ * destroy a writer.
+ *
+ * When the writer's reference count is decremented to 0 by a
+ * bt_ctf_writer_put, the writer is freed.
+ *
+ * @param writer Writer instance.
+ */
+extern void bt_ctf_writer_get(struct bt_ctf_writer *writer);
+extern void bt_ctf_writer_put(struct bt_ctf_writer *writer);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* BABELTRACE_CTF_WRITER_WRITER_H */
index 715cdbd0e182e39f204a4e19aba338ba49265e91..96c5083c2b7d5eea1e0154aa5954073353017139 100644 (file)
@@ -137,23 +137,6 @@ void ctf_move_pos(struct ctf_stream_pos *pos, uint64_t bit_offset)
        if (unlikely(pos->offset == EOF))
                return;
 
-       if (pos->fd >= 0) {
-               /*
-                * PROT_READ ctf_packet_seek is called from within
-                * ctf_pos_get_event so end of packet does not change
-                * the packet context on for the last event of the
-                * packet.
-                */
-               if ((pos->prot == PROT_WRITE)
-                       && (unlikely(pos->offset + bit_offset >= pos->packet_size))) {
-                       printf_debug("ctf_packet_seek (before call): %" PRId64 "\n",
-                                    pos->offset);
-                       ctf_packet_seek(&pos->parent, 0, SEEK_CUR);
-                       printf_debug("ctf_packet_seek (after call): %" PRId64 "\n",
-                                    pos->offset);
-                       return;
-               }
-       }
        pos->offset += bit_offset;
        printf_debug("ctf_move_pos after increment: %" PRId64 "\n", pos->offset);
 }
@@ -201,7 +184,7 @@ int ctf_pos_packet(struct ctf_stream_pos *dummy)
 static inline
 void ctf_pos_pad_packet(struct ctf_stream_pos *pos)
 {
-       ctf_move_pos(pos, pos->packet_size - pos->offset);
+       ctf_packet_seek(&pos->parent, 0, SEEK_CUR);
 }
 
 static inline
index 360a9c70a5a159eed3bd11b00e1bfa97060084c8..50232a905fc7df90268d62c4e58c28ed9d0cd025 100644 (file)
@@ -65,14 +65,16 @@ struct bt_saved_pos;
  * only use BT_SEEK_LAST to get the timestamp of the last event(s) in
  * the trace.
  */
+enum bt_iter_pos_type {
+       BT_SEEK_TIME,           /* uses u.seek_time */
+       BT_SEEK_RESTORE,        /* uses u.restore */
+       BT_SEEK_CUR,
+       BT_SEEK_BEGIN,
+       BT_SEEK_LAST,
+};
+
 struct bt_iter_pos {
-       enum {
-               BT_SEEK_TIME,           /* uses u.seek_time */
-               BT_SEEK_RESTORE,        /* uses u.restore */
-               BT_SEEK_CUR,
-               BT_SEEK_BEGIN,
-               BT_SEEK_LAST,
-       } type;
+       enum bt_iter_pos_type type;
        union {
                uint64_t seek_time;
                struct bt_saved_pos *restore;
index dc7736689170e10da3ba1164f543c79e334fdbe3..5820fb3e5533ba97693044ce0e419ca0382545d8 100644 (file)
@@ -34,6 +34,7 @@
 #include <babeltrace/trace-handle-internal.h>
 #include <babeltrace/trace-collection.h>
 #include <babeltrace/format.h>
+#include <babeltrace/format-internal.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <stdlib.h>
 #include <string.h>
@@ -44,6 +45,9 @@
 
 #include <glib.h>
 
+static
+void remove_trace_handle(struct bt_trace_handle *handle);
+
 struct bt_context *bt_context_create(void)
 {
        struct bt_context *ctx;
@@ -56,7 +60,7 @@ struct bt_context *bt_context_create(void)
        /* Instanciate the trace handle container */
        ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
                                g_direct_equal, NULL,
-                               (GDestroyNotify) bt_trace_handle_destroy);
+                               (GDestroyNotify) remove_trace_handle);
 
        ctx->current_iterator = NULL;
        ctx->tc = g_new0(struct trace_collection, 1);
@@ -155,43 +159,39 @@ end:
 
 int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
 {
-       struct bt_trace_handle *handle;
-       int ret;
+       int ret = 0;
 
-       if (!ctx)
-               return -EINVAL;
-
-       handle = g_hash_table_lookup(ctx->trace_handles,
-               (gpointer) (unsigned long) handle_id);
-       if (!handle)
-               return -ENOENT;
+       if (!ctx) {
+               ret = -EINVAL;
+               goto end;
+       }
 
-       /* Remove from containers */
-       bt_trace_collection_remove(ctx->tc, handle->td);
-       /* Close the trace */
-       ret = handle->format->close_trace(handle->td);
-       if (ret) {
-               fprintf(stderr, "Error in close_trace callback\n");
-               return ret;
+       /*
+        * Remove the handle. remove_trace_handle will be called
+        * automatically.
+        */
+       if (!g_hash_table_remove(ctx->trace_handles,
+               (gpointer) (unsigned long) handle_id)) {
+               ret = -ENOENT;
+               goto end;
        }
-       /* Remove and free the handle */
-       g_hash_table_remove(ctx->trace_handles,
-                       (gpointer) (unsigned long) handle_id);
-       return 0;
+end:
+       return ret;
 }
 
 static
 void bt_context_destroy(struct bt_context *ctx)
 {
        assert(ctx);
-       bt_finalize_trace_collection(ctx->tc);
 
        /*
         * Remove all traces. The g_hash_table_destroy will call
-        * bt_trace_handle_destroy on each elements.
+        * remove_trace_handle on each element.
         */
        g_hash_table_destroy(ctx->trace_handles);
 
+       bt_finalize_trace_collection(ctx->tc);
+
        /* ctx->tc should always be valid */
        assert(ctx->tc != NULL);
        g_free(ctx->tc);
@@ -211,3 +211,19 @@ void bt_context_put(struct bt_context *ctx)
        if (ctx->refcount == 0)
                bt_context_destroy(ctx);
 }
+
+static
+void remove_trace_handle(struct bt_trace_handle *handle)
+{
+       int ret;
+
+       /* Remove from containers */
+       bt_trace_collection_remove(handle->td->ctx->tc, handle->td);
+       /* Close the trace */
+       ret = handle->format->close_trace(handle->td);
+       if (ret) {
+               fprintf(stderr, "Error in close_trace callback\n");
+       }
+
+       bt_trace_handle_destroy(handle);
+}
index 849767a77e8a4ae96e6e5899a0191aa888a676c5..e1aa471ce47b2dc5a68e3b4481f60b59676cc7c8 100644 (file)
@@ -16,10 +16,15 @@ test_seek_LDADD = $(LIBTAP) libtestcommon.a \
 
 test_bitfield_LDADD = $(LIBTAP) libtestcommon.a
 
-noinst_PROGRAMS = test_seek test_bitfield
+test_ctf_writer_LDADD = $(LIBTAP) \
+       $(top_builddir)/lib/libbabeltrace.la \
+       $(top_builddir)/formats/ctf/libbabeltrace-ctf.la
+
+noinst_PROGRAMS = test_seek test_bitfield test_ctf_writer
 
 test_seek_SOURCES = test_seek.c
 test_bitfield_SOURCES = test_bitfield.c
+test_ctf_writer_SOURCES = test_ctf_writer.c
 
 SCRIPT_LIST = test_seek_big_trace test_seek_empty_packet
 
diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c
new file mode 100644 (file)
index 0000000..d55c629
--- /dev/null
@@ -0,0 +1,800 @@
+/*
+ * test-ctf-writer.c
+ *
+ * CTF Writer test
+ *
+ * Copyright 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; under version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#define _GNU_SOURCE
+#include <babeltrace/ctf-writer/writer.h>
+#include <babeltrace/ctf-writer/clock.h>
+#include <babeltrace/ctf-writer/stream.h>
+#include <babeltrace/ctf-writer/event.h>
+#include <babeltrace/ctf-writer/event-types.h>
+#include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/ctf/events.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/utsname.h>
+#include <limits.h>
+#include <string.h>
+#include <assert.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include "tap/tap.h"
+
+#define METADATA_LINE_SIZE 512
+#define SEQUENCE_TEST_LENGTH 10
+#define PACKET_RESIZE_TEST_LENGTH 100000
+
+static uint64_t current_time;
+
+void validate_metadata(char *parser_path, char *metadata_path)
+{
+       int ret = 0;
+       char parser_output_path[] = "/tmp/parser_output_XXXXXX";
+       int parser_output_fd = -1, metadata_fd = -1;
+
+       if (!metadata_path) {
+               ret = -1;
+               goto result;
+       }
+
+       parser_output_fd = mkstemp(parser_output_path);
+       metadata_fd = open(metadata_path, O_RDONLY);
+
+       unlink(parser_output_path);
+
+       if (parser_output_fd == -1 || metadata_fd == -1) {
+               diag("Failed create temporary files for metadata parsing.");
+               ret = -1;
+               goto result;
+       }
+
+       pid_t pid = fork();
+       if (pid) {
+               int status = 0;
+               waitpid(pid, &status, 0);
+               ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
+       } else {
+               /* ctf-parser-test expects a metadata string on stdin. */
+               ret = dup2(metadata_fd, STDIN_FILENO);
+               if (ret < 0) {
+                       perror("# dup2 metadata_fd to STDIN");
+                       goto result;
+               }
+
+               ret = dup2(parser_output_fd, STDOUT_FILENO);
+               if (ret < 0) {
+                       perror("# dup2 parser_output_fd to STDOUT");
+                       goto result;
+               }
+
+               ret = dup2(parser_output_fd, STDERR_FILENO);
+               if (ret < 0) {
+                       perror("# dup2 parser_output_fd to STDERR");
+                       goto result;
+               }
+
+               execl(parser_path, "ctf-parser-test", NULL);
+               perror("# Could not launch the ctf metadata parser process");
+               exit(-1);
+       }
+result:
+       ok(ret == 0, "Metadata string is valid");
+
+       if (ret && metadata_fd >= 0 && parser_output_fd >= 0) {
+               char *line;
+               size_t len = METADATA_LINE_SIZE;
+               FILE *metadata_fp = NULL, *parser_output_fp = NULL;
+
+               metadata_fp = fdopen(metadata_fd, "r");
+               if (!metadata_fp) {
+                       perror("fdopen on metadata_fd");
+                       goto close_fp;
+               }
+               metadata_fd = -1;
+
+               parser_output_fp = fdopen(parser_output_fd, "r");
+               if (!parser_output_fp) {
+                       perror("fdopen on parser_output_fd");
+                       goto close_fp;
+               }
+               parser_output_fd = -1;
+
+               line = malloc(len);
+               if (!line) {
+                       diag("malloc failure");
+               }
+
+               rewind(metadata_fp);
+
+               /* Output the metadata and parser output as diagnostic */
+               while (getline(&line, &len, metadata_fp) > 0) {
+                       diag("%s", line);
+               }
+
+               rewind(parser_output_fp);
+               while (getline(&line, &len, parser_output_fp) > 0) {
+                       diag("%s", line);
+               }
+
+               free(line);
+close_fp:
+               if (metadata_fp) {
+                       if (fclose(metadata_fp)) {
+                               diag("fclose failure");
+                       }
+               }
+               if (parser_output_fp) {
+                       if (fclose(parser_output_fp)) {
+                               diag("fclose failure");
+                       }
+               }
+       }
+
+       if (parser_output_fd >= 0) {
+               if (close(parser_output_fd)) {
+                       diag("close error");
+               }
+       }
+       if (metadata_fd >= 0) {
+               if (close(metadata_fd)) {
+                       diag("close error");
+               }
+       }
+}
+
+void validate_trace(char *parser_path, char *trace_path)
+{
+       int ret = 0;
+       char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX";
+       int babeltrace_output_fd = -1;
+
+       if (!trace_path) {
+               ret = -1;
+               goto result;
+       }
+
+       babeltrace_output_fd = mkstemp(babeltrace_output_path);
+       unlink(babeltrace_output_path);
+
+       if (babeltrace_output_fd == -1) {
+               diag("Failed to create a temporary file for trace parsing.");
+               ret = -1;
+               goto result;
+       }
+
+       pid_t pid = fork();
+       if (pid) {
+               int status = 0;
+               waitpid(pid, &status, 0);
+               ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
+       } else {
+               ret = dup2(babeltrace_output_fd, STDOUT_FILENO);
+               if (ret < 0) {
+                       perror("# dup2 babeltrace_output_fd to STDOUT");
+                       goto result;
+               }
+
+               ret = dup2(babeltrace_output_fd, STDERR_FILENO);
+               if (ret < 0) {
+                       perror("# dup2 babeltrace_output_fd to STDERR");
+                       goto result;
+               }
+
+               execl(parser_path, "babeltrace", trace_path, NULL);
+               perror("# Could not launch the babeltrace process");
+               exit(-1);
+       }
+result:
+       ok(ret == 0, "Babeltrace could read the resulting trace");
+
+       if (ret && babeltrace_output_fd >= 0) {
+               char *line;
+               size_t len = METADATA_LINE_SIZE;
+               FILE *babeltrace_output_fp = NULL;
+
+               babeltrace_output_fp = fdopen(babeltrace_output_fd, "r");
+               if (!babeltrace_output_fp) {
+                       perror("fdopen on babeltrace_output_fd");
+                       goto close_fp;
+               }
+               babeltrace_output_fd = -1;
+
+               line = malloc(len);
+               if (!line) {
+                       diag("malloc error");
+               }
+               rewind(babeltrace_output_fp);
+               while (getline(&line, &len, babeltrace_output_fp) > 0) {
+                       diag("%s", line);
+               }
+
+               free(line);
+close_fp:
+               if (babeltrace_output_fp) {
+                       if (fclose(babeltrace_output_fp)) {
+                               diag("fclose error");
+                       }
+               }
+       }
+
+       if (babeltrace_output_fd >= 0) {
+               if (close(babeltrace_output_fd)) {
+                       diag("close error");
+               }
+       }
+}
+
+void append_simple_event(struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
+{
+       /* Create and add a simple event class */
+       struct bt_ctf_event_class *simple_event_class =
+               bt_ctf_event_class_create("Simple Event");
+       struct bt_ctf_field_type *uint_12_type =
+               bt_ctf_field_type_integer_create(12);
+       struct bt_ctf_field_type *float_type =
+               bt_ctf_field_type_floating_point_create();
+       struct bt_ctf_event *simple_event;
+       struct bt_ctf_field *integer_field;
+       struct bt_ctf_field *float_field;
+
+       bt_ctf_field_type_set_alignment(float_type, 32);
+       bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11);
+       bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53);
+       ok(uint_12_type, "Create an unsigned integer type");
+       bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
+               "integer_field");
+       bt_ctf_event_class_add_field(simple_event_class, float_type,
+               "float_field");
+       bt_ctf_stream_class_add_event_class(stream_class,
+               simple_event_class);
+
+       simple_event = bt_ctf_event_create(simple_event_class);
+
+       ok(simple_event,
+               "Instantiate an event containing a single integer field");
+
+       integer_field = bt_ctf_field_create(uint_12_type);
+       bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
+       ok(bt_ctf_event_set_payload(simple_event, "integer_field",
+               integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
+
+       float_field = bt_ctf_event_get_payload(simple_event, "float_field");
+       bt_ctf_field_floating_point_set_value(float_field, 3.1415);
+
+       ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
+
+       ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
+               "Append simple event to trace stream");
+
+       ok(bt_ctf_stream_flush(stream) == 0,
+               "Flush trace stream with one event");
+
+       bt_ctf_event_class_put(simple_event_class);
+       bt_ctf_event_put(simple_event);
+       bt_ctf_field_type_put(uint_12_type);
+       bt_ctf_field_type_put(float_type);
+       bt_ctf_field_put(integer_field);
+       bt_ctf_field_put(float_field);
+}
+
+void append_complex_event(struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
+{
+       int i;
+       struct bt_ctf_field_type *uint_35_type =
+               bt_ctf_field_type_integer_create(35);
+       struct bt_ctf_field_type *int_16_type =
+               bt_ctf_field_type_integer_create(16);
+       struct bt_ctf_field_type *uint_3_type =
+               bt_ctf_field_type_integer_create(3);
+       struct bt_ctf_field_type *enum_variant_type =
+               bt_ctf_field_type_enumeration_create(uint_3_type);
+       struct bt_ctf_field_type *variant_type =
+               bt_ctf_field_type_variant_create(enum_variant_type,
+                       "variant_selector");
+       struct bt_ctf_field_type *string_type =
+               bt_ctf_field_type_string_create();
+       struct bt_ctf_field_type *sequence_type;
+       struct bt_ctf_field_type *inner_structure_type =
+               bt_ctf_field_type_structure_create();
+       struct bt_ctf_field_type *complex_structure_type =
+               bt_ctf_field_type_structure_create();
+       struct bt_ctf_event_class *event_class;
+       struct bt_ctf_event *event;
+       struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
+               *inner_structure_field, *complex_structure_field,
+               *a_sequence_field, *enum_variant_field, *enum_container_field,
+               *variant_field;
+
+       bt_ctf_field_type_set_alignment(int_16_type, 32);
+       bt_ctf_field_type_integer_set_signed(int_16_type, 1);
+       bt_ctf_field_type_integer_set_base(uint_35_type,
+               BT_CTF_INTEGER_BASE_HEXADECIMAL);
+
+       sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
+               "seq_len");
+       bt_ctf_field_type_structure_add_field(inner_structure_type,
+               uint_35_type, "seq_len");
+       bt_ctf_field_type_structure_add_field(inner_structure_type,
+               sequence_type, "a_sequence");
+
+       bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
+               "UINT3_TYPE", 0, 0);
+       bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
+               "INT16_TYPE", 1, 1);
+       bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
+               "UINT35_TYPE", 2, 7);
+       ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
+               "An unknown entry"), "Reject a variant field based on an unknown tag value");
+       ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
+               "UINT3_TYPE") == 0, "Add a field to a variant");
+       bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
+               "INT16_TYPE");
+       bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
+               "UINT35_TYPE");
+
+       bt_ctf_field_type_structure_add_field(complex_structure_type,
+               enum_variant_type, "variant_selector");
+       bt_ctf_field_type_structure_add_field(complex_structure_type,
+               string_type, "a_string");
+       bt_ctf_field_type_structure_add_field(complex_structure_type,
+               variant_type, "variant_value");
+       bt_ctf_field_type_structure_add_field(complex_structure_type,
+               inner_structure_type, "inner_structure");
+
+       ok(bt_ctf_event_class_create("clock") == NULL,
+               "Reject creation of an event class with an illegal name");
+       event_class = bt_ctf_event_class_create("Complex Test Event");
+       ok(event_class, "Create an event class");
+       ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
+               "Reject addition of a field with an empty name to an event");
+       ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
+               "Reject addition of a field with a NULL type to an event");
+       ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
+               "int"),
+               "Reject addition of a type with an illegal name to an event");
+       ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
+               "uint_35") == 0,
+               "Add field of type unsigned integer to an event");
+       ok(bt_ctf_event_class_add_field(event_class, int_16_type,
+               "int_16") == 0, "Add field of type signed integer to an event");
+       ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
+               "complex_structure") == 0,
+               "Add composite structure to an event");
+
+       /* Add event class to the stream class */
+       ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
+               "Reject addition of NULL event class to a stream class");
+       ok(bt_ctf_stream_class_add_event_class(stream_class,
+               event_class) == 0, "Add an event class to stream class");
+
+       event = bt_ctf_event_create(event_class);
+       ok(event, "Instanciate a complex event");
+
+       uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
+       ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
+       bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
+       bt_ctf_field_put(uint_35_field);
+
+       int_16_field = bt_ctf_event_get_payload(event, "int_16");
+       bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
+       bt_ctf_field_put(int_16_field);
+
+       complex_structure_field = bt_ctf_event_get_payload(event,
+               "complex_structure");
+       inner_structure_field = bt_ctf_field_structure_get_field(
+               complex_structure_field, "inner_structure");
+       a_string_field = bt_ctf_field_structure_get_field(
+               complex_structure_field, "a_string");
+       enum_variant_field = bt_ctf_field_structure_get_field(
+               complex_structure_field, "variant_selector");
+       variant_field = bt_ctf_field_structure_get_field(
+               complex_structure_field, "variant_value");
+       uint_35_field = bt_ctf_field_structure_get_field(
+               inner_structure_field, "seq_len");
+       a_sequence_field = bt_ctf_field_structure_get_field(
+               inner_structure_field, "a_sequence");
+
+       enum_container_field = bt_ctf_field_enumeration_get_container(
+               enum_variant_field);
+       bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
+       int_16_field = bt_ctf_field_variant_get_field(variant_field,
+               enum_variant_field);
+       bt_ctf_field_signed_integer_set_value(int_16_field, -200);
+       bt_ctf_field_put(int_16_field);
+       bt_ctf_field_string_set_value(a_string_field,
+               "Test string");
+       bt_ctf_field_unsigned_integer_set_value(uint_35_field,
+               SEQUENCE_TEST_LENGTH);
+       ok(bt_ctf_field_sequence_set_length(a_sequence_field,
+               uint_35_field) == 0, "Set a sequence field's length");
+
+       for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
+               int_16_field = bt_ctf_field_sequence_get_field(
+                       a_sequence_field, i);
+               bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
+               bt_ctf_field_put(int_16_field);
+       }
+
+       bt_ctf_clock_set_time(clock, ++current_time);
+       ok(bt_ctf_stream_append_event(stream, event) == 0,
+               "Append a complex event to a stream");
+       ok(bt_ctf_stream_flush(stream) == 0,
+               "Flush a stream containing a complex event");
+
+       bt_ctf_field_put(uint_35_field);
+       bt_ctf_field_put(a_string_field);
+       bt_ctf_field_put(inner_structure_field);
+       bt_ctf_field_put(complex_structure_field);
+       bt_ctf_field_put(a_sequence_field);
+       bt_ctf_field_put(enum_variant_field);
+       bt_ctf_field_put(enum_container_field);
+       bt_ctf_field_put(variant_field);
+       bt_ctf_field_type_put(uint_35_type);
+       bt_ctf_field_type_put(int_16_type);
+       bt_ctf_field_type_put(string_type);
+       bt_ctf_field_type_put(sequence_type);
+       bt_ctf_field_type_put(inner_structure_type);
+       bt_ctf_field_type_put(complex_structure_type);
+       bt_ctf_field_type_put(uint_3_type);
+       bt_ctf_field_type_put(enum_variant_type);
+       bt_ctf_field_type_put(variant_type);
+       bt_ctf_event_class_put(event_class);
+       bt_ctf_event_put(event);
+}
+
+void type_field_tests()
+{
+       struct bt_ctf_field *uint_12;
+       struct bt_ctf_field *int_16;
+       struct bt_ctf_field *string;
+       struct bt_ctf_field_type *composite_structure_type;
+       struct bt_ctf_field_type *structure_seq_type;
+       struct bt_ctf_field_type *string_type;
+       struct bt_ctf_field_type *sequence_type;
+       struct bt_ctf_field_type *uint_8_type;
+       struct bt_ctf_field_type *int_16_type;
+       struct bt_ctf_field_type *uint_12_type =
+               bt_ctf_field_type_integer_create(12);
+
+       ok(uint_12_type, "Create an unsigned integer type");
+       ok(bt_ctf_field_type_integer_set_base(uint_12_type,
+               BT_CTF_INTEGER_BASE_BINARY) == 0,
+               "Set integer type's base as binary");
+       ok(bt_ctf_field_type_integer_set_base(uint_12_type,
+               BT_CTF_INTEGER_BASE_DECIMAL) == 0,
+               "Set integer type's base as decimal");
+       ok(bt_ctf_field_type_integer_set_base(uint_12_type,
+               BT_CTF_INTEGER_BASE_UNKNOWN),
+               "Reject integer type's base set as unknown");
+       ok(bt_ctf_field_type_integer_set_base(uint_12_type,
+               BT_CTF_INTEGER_BASE_OCTAL) == 0,
+               "Set integer type's base as octal");
+       ok(bt_ctf_field_type_integer_set_base(uint_12_type,
+               BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
+               "Set integer type's base as hexadecimal");
+       ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
+               "Reject unknown integer base value");
+       ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
+               "Set integer type signedness to signed");
+       ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
+               "Set integer type signedness to unsigned");
+
+       int_16_type = bt_ctf_field_type_integer_create(16);
+       bt_ctf_field_type_integer_set_signed(int_16_type, 1);
+       uint_8_type = bt_ctf_field_type_integer_create(8);
+       sequence_type =
+               bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
+       ok(sequence_type, "Create a sequence of int16_t type");
+
+       string_type = bt_ctf_field_type_string_create();
+       ok(string_type, "Create a string type");
+       ok(bt_ctf_field_type_string_set_encoding(string_type,
+               CTF_STRING_NONE),
+               "Reject invalid \"None\" string encoding");
+       ok(bt_ctf_field_type_string_set_encoding(string_type,
+               42),
+               "Reject invalid string encoding");
+       ok(bt_ctf_field_type_string_set_encoding(string_type,
+               CTF_STRING_ASCII) == 0,
+               "Set string encoding to ASCII");
+
+       structure_seq_type = bt_ctf_field_type_structure_create();
+       ok(structure_seq_type, "Create a structure type");
+       ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
+               uint_8_type, "seq_len") == 0,
+               "Add a uint8_t type to a structure");
+       ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
+               sequence_type, "a_sequence") == 0,
+               "Add a sequence type to a structure");
+       composite_structure_type = bt_ctf_field_type_structure_create();
+       ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
+               string_type, "a_string") == 0,
+               "Add a string type to a structure");
+       ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
+               structure_seq_type, "inner_structure") == 0,
+               "Add a structure type to a structure");
+
+       int_16 = bt_ctf_field_create(int_16_type);
+       ok(int_16, "Instanciate a signed 16-bit integer");
+       uint_12 = bt_ctf_field_create(uint_12_type);
+       ok(uint_12, "Instanciate an unsigned 12-bit integer");
+
+       /* Can't modify types after instanciating them */
+       ok(bt_ctf_field_type_integer_set_base(uint_12_type,
+               BT_CTF_INTEGER_BASE_DECIMAL),
+               "Check an integer type' base can't be modified after instanciation");
+       ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
+               "Check an integer type's signedness can't be modified after instanciation");
+
+       /* Check signed property is checked */
+       ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
+               "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
+       ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
+               "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
+
+       /* Check overflows are properly tested for */
+       ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
+               "Check -32768 is allowed for a signed 16-bit integer");
+       ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
+               "Check 32767 is allowed for a signed 16-bit integer");
+       ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
+               "Check 32768 is not allowed for a signed 16-bit integer");
+       ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
+               "Check -32769 is not allowed for a signed 16-bit integer");
+       ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
+               "Check -42 is allowed for a signed 16-bit integer");
+
+       ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
+               "Check 4095 is allowed for an unsigned 12-bit integer");
+       ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
+               "Check 4096 is not allowed for a unsigned 12-bit integer");
+       ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
+               "Check 0 is allowed for an unsigned 12-bit integer");
+
+       string = bt_ctf_field_create(string_type);
+       ok(string, "Instanciate a string field");
+       ok(bt_ctf_field_string_set_value(string, "A value") == 0,
+               "Set a string's value");
+
+       bt_ctf_field_put(string);
+       bt_ctf_field_put(uint_12);
+       bt_ctf_field_put(int_16);
+       bt_ctf_field_type_put(composite_structure_type);
+       bt_ctf_field_type_put(structure_seq_type);
+       bt_ctf_field_type_put(string_type);
+       bt_ctf_field_type_put(sequence_type);
+       bt_ctf_field_type_put(uint_8_type);
+       bt_ctf_field_type_put(int_16_type);
+       bt_ctf_field_type_put(uint_12_type);
+}
+
+void packet_resize_test(struct bt_ctf_stream_class *stream_class,
+               struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
+{
+       /*
+        * Append enough events to force the underlying packet to be resized.
+        * Also tests that a new event can be declared after a stream has been
+        * instantiated and used/flushed.
+        */
+       int ret = 0;
+       int i;
+       struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
+               "Spammy_Event");
+       struct bt_ctf_field_type *integer_type =
+               bt_ctf_field_type_integer_create(17);
+       struct bt_ctf_field_type *string_type =
+               bt_ctf_field_type_string_create();
+
+       ret |= bt_ctf_event_class_add_field(event_class, integer_type,
+               "field_1");
+       ret |= bt_ctf_event_class_add_field(event_class, string_type,
+               "a_string");
+       ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
+       ok(ret == 0, "Add a new event class to a stream class after writing an event");
+       if (ret) {
+               goto end;
+       }
+
+       for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
+               struct bt_ctf_event *event = bt_ctf_event_create(event_class);
+               struct bt_ctf_field *integer =
+                       bt_ctf_field_create(integer_type);
+               struct bt_ctf_field *string =
+                       bt_ctf_field_create(string_type);
+
+               ret |= bt_ctf_clock_set_time(clock, ++current_time);
+               ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
+               ret |= bt_ctf_event_set_payload(event, "field_1",
+                       integer);
+               bt_ctf_field_put(integer);
+               ret |= bt_ctf_field_string_set_value(string, "This is a test");
+               ret |= bt_ctf_event_set_payload(event, "a_string",
+                       string);
+               bt_ctf_field_put(string);
+               ret |= bt_ctf_stream_append_event(stream, event);
+               bt_ctf_event_put(event);
+
+               if (ret) {
+                       break;
+               }
+       }
+end:
+       ok(ret == 0, "Append 100 000 events to a stream");
+       ok(bt_ctf_stream_flush(stream) == 0,
+               "Flush a stream that forces a packet resize");
+       bt_ctf_field_type_put(integer_type);
+       bt_ctf_field_type_put(string_type);
+       bt_ctf_event_class_put(event_class);
+}
+
+int main(int argc, char **argv)
+{
+       char trace_path[] = "/tmp/ctfwriter_XXXXXX";
+       char metadata_path[sizeof(trace_path) + 9];
+       const char *clock_name = "test_clock";
+       const char *clock_description = "This is a test clock";
+       const uint64_t frequency = 1000000000;
+       const uint64_t offset_s = 1351530929945824323;
+       const uint64_t offset = 1234567;
+       const uint64_t precision = 10;
+       char *metadata_string;
+       struct bt_ctf_writer *writer;
+       struct utsname name;
+       char hostname[HOST_NAME_MAX];
+       struct bt_ctf_clock *clock;
+       struct bt_ctf_stream_class *stream_class;
+       struct bt_ctf_stream *stream1;
+
+       if (argc < 3) {
+               printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
+               exit(-1);
+       }
+
+       plan_no_plan();
+
+       if (!mkdtemp(trace_path)) {
+               perror("# perror");
+       }
+
+       strcpy(metadata_path, trace_path);
+       strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
+
+       writer = bt_ctf_writer_create(trace_path);
+       ok(writer, "bt_ctf_create succeeds in creating trace with path");
+
+       /* Add environment context to the trace */
+       gethostname(hostname, HOST_NAME_MAX);
+       ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
+               "Add host (%s) environment field to writer instance",
+               hostname);
+       ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
+               "test_value"),
+               "bt_ctf_writer_add_environment_field error with NULL writer");
+       ok(bt_ctf_writer_add_environment_field(writer, NULL,
+               "test_value"),
+               "bt_ctf_writer_add_environment_field error with NULL field name");
+       ok(bt_ctf_writer_add_environment_field(writer, "test_field",
+               NULL),
+               "bt_ctf_writer_add_environment_field error with NULL field value");
+
+       if (uname(&name)) {
+               perror("uname");
+               return -1;
+       }
+
+       ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
+               == 0, "Add sysname (%s) environment field to writer instance",
+               name.sysname);
+       ok(bt_ctf_writer_add_environment_field(writer, "nodename",
+               name.nodename) == 0,
+               "Add nodename (%s) environment field to writer instance",
+               name.nodename);
+       ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
+               == 0, "Add release (%s) environment field to writer instance",
+               name.release);
+       ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
+               == 0, "Add version (%s) environment field to writer instance",
+               name.version);
+       ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
+               == 0, "Add machine (%s) environment field to writer istance",
+               name.machine);
+
+       /* Define a clock and add it to the trace */
+       ok(bt_ctf_clock_create("signed") == NULL, "Illegal clock name rejected");
+       ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
+       clock = bt_ctf_clock_create(clock_name);
+       ok(clock, "Clock created sucessfully");
+       bt_ctf_clock_set_description(clock, clock_description);
+
+       ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
+               "Set clock frequency");
+       ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
+               "Set clock offset (seconds)");
+       ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
+       ok(bt_ctf_clock_set_precision(clock, precision) == 0,
+               "Set clock precision");
+       ok(bt_ctf_clock_set_is_absolute(clock, 0xFF) == 0,
+               "Set clock absolute property");
+
+       ok(bt_ctf_writer_add_clock(writer, clock) == 0,
+               "Add clock to writer instance");
+       ok(bt_ctf_writer_add_clock(writer, clock),
+               "Verify a clock can't be added twice to a writer instance");
+
+       /* Define a stream class */
+       stream_class = bt_ctf_stream_class_create("test_stream");
+       ok(stream_class, "Create stream class");
+       ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
+               "Set a stream class' clock");
+
+       /* Test the event fields and event types APIs */
+       type_field_tests();
+
+       /* Instantiate a stream and append events */
+       stream1 = bt_ctf_writer_create_stream(writer, stream_class);
+       ok(stream1, "Instanciate a stream class from writer");
+
+       /* Should fail after instanciating a stream (locked)*/
+       ok(bt_ctf_stream_class_set_clock(stream_class, clock),
+               "Changes to a stream class that was already instantiated fail");
+
+       append_simple_event(stream_class, stream1, clock);
+
+       packet_resize_test(stream_class, stream1, clock);
+
+       append_complex_event(stream_class, stream1, clock);
+
+       metadata_string = bt_ctf_writer_get_metadata_string(writer);
+       ok(metadata_string, "Get metadata string");
+
+       bt_ctf_writer_flush_metadata(writer);
+       validate_metadata(argv[1], metadata_path);
+       validate_trace(argv[2], trace_path);
+
+       bt_ctf_clock_put(clock);
+       bt_ctf_stream_class_put(stream_class);
+       bt_ctf_writer_put(writer);
+       bt_ctf_stream_put(stream1);
+       free(metadata_string);
+
+       /* Remove all trace files and delete temporary trace directory */
+       DIR *trace_dir = opendir(trace_path);
+       if (!trace_dir) {
+               perror("# opendir");
+               return -1;
+       }
+
+       struct dirent *entry;
+       while ((entry = readdir(trace_dir))) {
+               if (entry->d_type == DT_REG) {
+                       unlinkat(dirfd(trace_dir), entry->d_name, 0);
+               }
+       }
+
+       rmdir(trace_path);
+       closedir(trace_dir);
+
+       return 0;
+}
diff --git a/tests/lib/test_ctf_writer_complete b/tests/lib/test_ctf_writer_complete
new file mode 100755 (executable)
index 0000000..a74b942
--- /dev/null
@@ -0,0 +1,22 @@
+#!/bin/sh
+#
+# Copyright (C) 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; only version 2
+# of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+CURDIR=$(dirname $0)/
+ROOTDIR=$CURDIR/../..
+
+$CURDIR/test_ctf_writer $ROOTDIR/formats/ctf/metadata/ctf-parser-test $ROOTDIR/converter/babeltrace
index 170c830dd6ec30aeced250e2295e9a7b8d279b6a..4e1a8232cc2e4a8ad131a80e28b607fd97509227 100644 (file)
@@ -44,17 +44,22 @@ void run_seek_begin(char *path, uint64_t expected_begin)
        int ret;
        uint64_t timestamp_begin;
        uint64_t timestamp_seek_begin;
+       unsigned int nr_seek_begin_test;
+
+       nr_seek_begin_test = 5;
 
        /* Open the trace */
        ctx = create_context_with_path(path);
        if (!ctx) {
-               plan_skip_all("Cannot create valid context");
+               skip(nr_seek_begin_test, "Cannot create valid context");
+               return;
        }
 
        /* Create iterator with null begin and end */
        iter = bt_ctf_iter_create(ctx, NULL, NULL);
        if (!iter) {
-               plan_skip_all("Cannot create valid iterator");
+               skip(nr_seek_begin_test, "Cannot create valid iterator");
+               return;
        }
 
        event = bt_ctf_iter_read_event(iter);
@@ -92,17 +97,22 @@ void run_seek_last(char *path, uint64_t expected_last)
        struct bt_iter_pos newpos;
        int ret;
        uint64_t timestamp_last;
+       unsigned int nr_seek_last_tests;
+
+       nr_seek_last_tests = 6;
 
        /* Open the trace */
        ctx = create_context_with_path(path);
        if (!ctx) {
-               plan_skip_all("Cannot create valid context");
+               skip(nr_seek_last_tests, "Cannot create valid context");
+               return;
        }
 
        /* Create iterator with null last and end */
        iter = bt_ctf_iter_create(ctx, NULL, NULL);
        if (!iter) {
-               plan_skip_all("Cannot create valid iterator");
+               skip(nr_seek_last_tests, "Cannot create valid iterator");
+               return;
        }
 
        event = bt_ctf_iter_read_event(iter);
@@ -143,17 +153,24 @@ void run_seek_time_at_last(char *path, uint64_t expected_last)
        struct bt_iter_pos newpos;
        int ret;
        uint64_t timestamp_last;
+       unsigned int nr_seek_time_at_last_tests;
+
+       nr_seek_time_at_last_tests = 6;
 
        /* Open the trace */
        ctx = create_context_with_path(path);
        if (!ctx) {
-               plan_skip_all("Cannot create valid context");
+               skip(nr_seek_time_at_last_tests,
+                    "Cannot create valid context");
+               return;
        }
 
        /* Create iterator with null last and end */
        iter = bt_ctf_iter_create(ctx, NULL, NULL);
        if (!iter) {
-               plan_skip_all("Cannot create valid iterator");
+               skip(nr_seek_time_at_last_tests,
+                    "Cannot create valid iterator");
+               return;
        }
 
        event = bt_ctf_iter_read_event(iter);
@@ -198,16 +215,22 @@ void run_seek_cycles(char *path,
        int ret;
        uint64_t timestamp;
 
+       unsigned int nr_seek_cycles_tests;
+
+       nr_seek_cycles_tests = 12;
+
        /* Open the trace */
        ctx = create_context_with_path(path);
        if (!ctx) {
-               plan_skip_all("Cannot create valid context");
+               skip(nr_seek_cycles_tests, "Cannot create valid context");
+               return;
        }
 
        /* Create iterator with null last and end */
        iter = bt_ctf_iter_create(ctx, NULL, NULL);
        if (!iter) {
-               plan_skip_all("Cannot create valid iterator");
+               skip(nr_seek_cycles_tests, "Cannot create valid iterator");
+               return;
        }
 
        event = bt_ctf_iter_read_event(iter);
@@ -281,8 +304,6 @@ int main(int argc, char **argv)
        babeltrace_debug = 0;   /* libbabeltrace.la */
        opt_clock_offset = 0;   /* libbabeltrace-ctf.la */
 
-       plan_tests(NR_TESTS);
-
        if (argc < 4) {
                plan_skip_all("Invalid arguments: need a trace path and the start and last timestamp");
 
@@ -301,6 +322,8 @@ int main(int argc, char **argv)
                plan_skip_all("Invalid value for last timestamp");
        }
 
+       plan_tests(NR_TESTS);
+
        run_seek_begin(path, expected_begin);
        run_seek_time_at_last(path, expected_last);
        run_seek_last(path, expected_last);
index 10efa4b06417e2fed4ef70d5b06e10e8a7940c74..f9faf175591254ec7f790eea0f283e35d7e86827 100644 (file)
@@ -1,4 +1,5 @@
 bin/test_trace_read
 lib/test_bitfield
 lib/test_seek_empty_packet
-lib/test_seek_big_trace
\ No newline at end of file
+lib/test_seek_big_trace
+lib/test_ctf_writer_complete
\ No newline at end of file
This page took 0.14014 seconds and 4 git commands to generate.