[lttng-dev] [PATCH babeltrace 2/3] Add CTF Writer implementation

Jérémie Galarneau jeremie.galarneau at efficios.com
Wed Oct 16 14:35:01 EDT 2013


Signed-off-by: Jérémie Galarneau <jeremie.galarneau at efficios.com>
Reviewed-by: Christian Babeux <christian.babeux at efficios.com>
Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers at efficios.com>
---
 configure.ac                                       |    4 +
 formats/ctf/Makefile.am                            |   13 +-
 formats/ctf/writer/Makefile.am                     |   22 +
 formats/ctf/writer/clock.c                         |  264 ++++
 formats/ctf/writer/event-fields.c                  | 1249 +++++++++++++++++
 formats/ctf/writer/event-types.c                   | 1402 ++++++++++++++++++++
 formats/ctf/writer/event.c                         |  368 +++++
 formats/ctf/writer/functor.c                       |   38 +
 formats/ctf/writer/stream.c                        |  697 ++++++++++
 formats/ctf/writer/writer.c                        |  744 +++++++++++
 include/Makefile.am                                |   12 +-
 include/babeltrace/ctf-writer/clock-internal.h     |   65 +
 .../babeltrace/ctf-writer/event-fields-internal.h  |  100 ++
 include/babeltrace/ctf-writer/event-internal.h     |   90 ++
 .../babeltrace/ctf-writer/event-types-internal.h   |  150 +++
 include/babeltrace/ctf-writer/functor-internal.h   |   41 +
 include/babeltrace/ctf-writer/ref-internal.h       |   61 +
 include/babeltrace/ctf-writer/stream-internal.h    |  101 ++
 include/babeltrace/ctf-writer/writer-internal.h    |   87 ++
 19 files changed, 5501 insertions(+), 7 deletions(-)
 create mode 100644 formats/ctf/writer/Makefile.am
 create mode 100644 formats/ctf/writer/clock.c
 create mode 100644 formats/ctf/writer/event-fields.c
 create mode 100644 formats/ctf/writer/event-types.c
 create mode 100644 formats/ctf/writer/event.c
 create mode 100644 formats/ctf/writer/functor.c
 create mode 100644 formats/ctf/writer/stream.c
 create mode 100644 formats/ctf/writer/writer.c
 create mode 100644 include/babeltrace/ctf-writer/clock-internal.h
 create mode 100644 include/babeltrace/ctf-writer/event-fields-internal.h
 create mode 100644 include/babeltrace/ctf-writer/event-internal.h
 create mode 100644 include/babeltrace/ctf-writer/event-types-internal.h
 create mode 100644 include/babeltrace/ctf-writer/functor-internal.h
 create mode 100644 include/babeltrace/ctf-writer/ref-internal.h
 create mode 100644 include/babeltrace/ctf-writer/stream-internal.h
 create mode 100644 include/babeltrace/ctf-writer/writer-internal.h

diff --git a/configure.ac b/configure.ac
index 58a472d..6ea5ef3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -115,6 +115,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
@@ -127,6 +130,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
diff --git a/formats/ctf/Makefile.am b/formats/ctf/Makefile.am
index 6dcc1bb..d79d9ef 100644
--- a/formats/ctf/Makefile.am
+++ b/formats/ctf/Makefile.am
@@ -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
diff --git a/formats/ctf/writer/Makefile.am b/formats/ctf/writer/Makefile.am
new file mode 100644
index 0000000..84ac03d
--- /dev/null
+++ b/formats/ctf/writer/Makefile.am
@@ -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
index 0000000..1146e00
--- /dev/null
+++ b/formats/ctf/writer/clock.c
@@ -0,0 +1,264 @@
+/*
+ * clock.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau at 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
index 0000000..aa389fd
--- /dev/null
+++ b/formats/ctf/writer/event-fields.c
@@ -0,0 +1,1249 @@
+/*
+ * event-fields.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau at 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
index 0000000..9b74b6e
--- /dev/null
+++ b/formats/ctf/writer/event-types.c
@@ -0,0 +1,1402 @@
+/*
+ * event-types.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau at 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;
+
+	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];
+	bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_NATIVE);
+	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
index 0000000..c7f1b22
--- /dev/null
+++ b/formats/ctf/writer/event.c
@@ -0,0 +1,368 @@
+/*
+ * event.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau at 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
index 0000000..9c932cf
--- /dev/null
+++ b/formats/ctf/writer/functor.c
@@ -0,0 +1,38 @@
+/*
+ * functor.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau at 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
index 0000000..f03f170
--- /dev/null
+++ b/formats/ctf/writer/stream.c
@@ -0,0 +1,697 @@
+/*
+ * stream.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau at 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
index 0000000..7490715
--- /dev/null
+++ b/formats/ctf/writer/writer.c
@@ -0,0 +1,744 @@
+/*
+ * writer.c
+ *
+ * Babeltrace CTF Writer
+ *
+ * Copyright 2013 EfficiOS Inc.
+ *
+ * Author: Jérémie Galarneau <jeremie.galarneau at 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 | O_DIRECTORY,
+		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) {
+		close(writer->trace_dir_fd);
+	}
+
+	if (writer->metadata_fd > 0) {
+		close(writer->metadata_fd);
+	}
+
+	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)
+{
+	g_string_append(context->string, "trace {\n");
+
+	g_string_append(context->string, "\tmajor = 1;\n");
+	g_string_append(context->string, "\tminor = 8;\n");
+
+	unsigned char *uuid = writer->uuid;
+	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, "");
+	bt_ctf_field_type_serialize(writer->trace_packet_header_type, context);
+	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);
+	}
+}
diff --git a/include/Makefile.am b/include/Makefile.am
index e368173..ec927b9 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -10,7 +10,9 @@ babeltraceinclude_HEADERS = \
 babeltracectfinclude_HEADERS = \
 	babeltrace/ctf/events.h \
 	babeltrace/ctf/callbacks.h \
-	babeltrace/ctf/iterator.h \
+	babeltrace/ctf/iterator.h
+
+babeltracectfwriterinclude_HEADERS = \
 	babeltrace/ctf-writer/clock.h \
 	babeltrace/ctf-writer/writer.h \
 	babeltrace/ctf-writer/event-fields.h \
@@ -36,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 \
diff --git a/include/babeltrace/ctf-writer/clock-internal.h b/include/babeltrace/ctf-writer/clock-internal.h
new file mode 100644
index 0000000..c60e5c0
--- /dev/null
+++ b/include/babeltrace/ctf-writer/clock-internal.h
@@ -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 at 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/event-fields-internal.h b/include/babeltrace/ctf-writer/event-fields-internal.h
new file mode 100644
index 0000000..9f2670d
--- /dev/null
+++ b/include/babeltrace/ctf-writer/event-fields-internal.h
@@ -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 at 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-internal.h b/include/babeltrace/ctf-writer/event-internal.h
new file mode 100644
index 0000000..b84c3dd
--- /dev/null
+++ b/include/babeltrace/ctf-writer/event-internal.h
@@ -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 at 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
index 0000000..d9acdd3
--- /dev/null
+++ b/include/babeltrace/ctf-writer/event-types-internal.h
@@ -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 at 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/functor-internal.h b/include/babeltrace/ctf-writer/functor-internal.h
new file mode 100644
index 0000000..66d3fcf
--- /dev/null
+++ b/include/babeltrace/ctf-writer/functor-internal.h
@@ -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 at 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
index 0000000..486e243
--- /dev/null
+++ b/include/babeltrace/ctf-writer/ref-internal.h
@@ -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 at 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
index 0000000..57dd992
--- /dev/null
+++ b/include/babeltrace/ctf-writer/stream-internal.h
@@ -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 at 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/writer-internal.h b/include/babeltrace/ctf-writer/writer-internal.h
new file mode 100644
index 0000000..5c682dd
--- /dev/null
+++ b/include/babeltrace/ctf-writer/writer-internal.h
@@ -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 at 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 */
-- 
1.8.4




More information about the lttng-dev mailing list