- 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);
- }