Port: handle 'size_t' / 'off_t' on Solaris
[babeltrace.git] / lib / ctf-ir / fields.c
index 30466a8b7d72d9ee27f835854757b0b5ad5c7f85..1633c54cb436613f4dbb864b12719307e75cabc2 100644 (file)
@@ -39,8 +39,6 @@
 #include <babeltrace/align-internal.h>
 #include <inttypes.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
@@ -560,8 +558,14 @@ struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name(
 
        /* We don't want to modify this field if it's frozen */
        if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
+               /*
+                * Not logging a warning here because the user could
+                * legitimately check if a structure field is set with
+                * this function: if the preconditions are satisfied,
+                * a NULL return value means this.
+                */
+               BT_LOGV("Not creating a field because structure field is frozen: "
+                       "struct-field-addr=%p, name=\"%s\"", field, name);
                goto end;
        }
 
@@ -623,8 +627,14 @@ struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index(
 
        /* We don't want to modify this field if it's frozen */
        if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
+               /*
+                * Not logging a warning here because the user could
+                * legitimately check if a structure field is set with
+                * this function: if the preconditions are satisfied,
+                * a NULL return value means this.
+                */
+               BT_LOGV("Not creating a field because structure field is frozen: "
+                       "struct-field-addr=%p, index=%" PRIu64, field, index);
                goto end;
        }
 
@@ -652,7 +662,7 @@ error:
        return ret_field;
 }
 
-int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
+int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field,
                const char *name, struct bt_ctf_field *value)
 {
        int ret = 0;
@@ -764,8 +774,14 @@ struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field,
 
        /* We don't want to modify this field if it's frozen */
        if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
+               /*
+                * Not logging a warning here because the user could
+                * legitimately check if a array field is set with
+                * this function: if the preconditions are satisfied,
+                * a NULL return value means this.
+                */
+               BT_LOGV("Not creating a field because array field is frozen: "
+                       "array-field-addr=%p, index=%" PRIu64, field, index);
                goto end;
        }
 
@@ -824,8 +840,14 @@ struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
 
        /* We don't want to modify this field if it's frozen */
        if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
+               /*
+                * Not logging a warning here because the user could
+                * legitimately check if a sequence field is set with
+                * this function: if the preconditions are satisfied,
+                * a NULL return value means this.
+                */
+               BT_LOGV("Not creating a field because sequence field is frozen: "
+                       "sequence-field-addr=%p, index=%" PRIu64, field, index);
                goto end;
        }
 
@@ -927,8 +949,15 @@ struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field,
 
        /* We don't want to modify this field if it's frozen */
        if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
+               /*
+                * Not logging a warning here because the user could
+                * legitimately check if a variant field is set with
+                * this function: if the preconditions are satisfied,
+                * a NULL return value means this.
+                */
+               BT_LOGV("Not creating a field because variant field is frozen: "
+                       "variant-field-addr=%p, tag-field-addr=%p",
+                       field, tag_field);
                goto end;
        }
 
@@ -1048,8 +1077,15 @@ struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
        if (!enumeration->payload) {
                /* We don't want to modify this field if it's frozen */
                if (field->frozen) {
-                       BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                               field);
+                       /*
+                        * Not logging a warning here because the user
+                        * could legitimately check if an enumeration's
+                        * container field is set with this function: if
+                        * the preconditions are satisfied, a NULL
+                        * return value means this.
+                        */
+                       BT_LOGV("Not creating a field because enumeration field is frozen: "
+                               "enum-field-addr=%p", field);
                        goto end;
                }
 
@@ -2214,7 +2250,7 @@ int bt_ctf_field_sequence_validate(struct bt_ctf_field *field)
                if (ret) {
                        BT_LOGW("Invalid sequence field's element field: "
                                "sequence-field-addr=%p, field-addr=%p, "
-                               "index=%" PRId64, field, elem_field, i);
+                               "index=%zu", field, elem_field, i);
                        goto end;
                }
        }
@@ -2352,7 +2388,7 @@ int bt_ctf_field_array_reset(struct bt_ctf_field *field)
                if (ret) {
                        BT_LOGE("Failed to reset array field's field: "
                                "array-field-addr=%p, field-addr=%p, "
-                               "index=%" PRId64, field, member, i);
+                               "index=%zu", field, member, i);
                        goto end;
                }
        }
@@ -2389,7 +2425,7 @@ int bt_ctf_field_sequence_reset(struct bt_ctf_field *field)
                if (ret) {
                        BT_LOGE("Failed to reset sequence field's field: "
                                "sequence-field-addr=%p, field-addr=%p, "
-                               "index=%" PRId64, field, member, i);
+                               "index=%zu", field, member, i);
                        goto end;
                }
        }
@@ -2544,25 +2580,33 @@ int bt_ctf_field_structure_serialize(struct bt_ctf_field *field,
        for (i = 0; i < structure->fields->len; i++) {
                struct bt_ctf_field *member = g_ptr_array_index(
                        structure->fields, i);
+               const char *field_name = NULL;
+
+               if (BT_LOG_ON_WARN) {
+                       ret = bt_ctf_field_type_structure_get_field(
+                               field->type, &field_name, NULL, i);
+                       assert(ret == 0);
+               }
 
                BT_LOGV("Serializing structure field's field: pos-offset=%" PRId64 ", "
                        "field-addr=%p, index=%" PRId64,
                        pos->offset, member, i);
+
+               if (!member) {
+                       BT_LOGW("Cannot serialize structure field's field: field is not set: "
+                               "struct-field-addr=%p, "
+                               "field-name=\"%s\", index=%" PRId64,
+                               field, field_name, i);
+                       ret = -1;
+                       goto end;
+               }
+
                ret = bt_ctf_field_serialize(member, pos, native_byte_order);
                if (ret) {
-                       int this_ret;
-                       const char *name;
-                       struct bt_ctf_field_type *structure_type =
-                                       bt_ctf_field_get_type(field);
-
-                       this_ret = bt_ctf_field_type_structure_get_field(
-                               structure_type, &name, NULL, i);
-                       assert(this_ret == 0);
                        BT_LOGW("Cannot serialize structure field's field: "
                                "struct-field-addr=%p, field-addr=%p, "
                                "field-name=\"%s\", index=%" PRId64,
-                               field, member, name, i);
-                       bt_put(structure_type);
+                               field->type, member, field_name, i);
                        break;
                }
        }
@@ -2679,7 +2723,7 @@ int bt_ctf_field_string_serialize(struct bt_ctf_field *field,
 
                ret = bt_ctf_field_unsigned_integer_set_value(character, chr);
                if (ret) {
-                       BT_LOGE("Cannot set character field's value: "
+                       BT_LOGW("Cannot set character field's value: "
                                "pos-offset=%" PRId64 ", field-addr=%p, "
                                "index=%" PRId64 ", char-int=%" PRIu64,
                                pos->offset, character, i, chr);
@@ -2693,7 +2737,7 @@ int bt_ctf_field_string_serialize(struct bt_ctf_field *field,
                ret = bt_ctf_field_integer_serialize(character, pos,
                        native_byte_order);
                if (ret) {
-                       BT_LOGE_STR("Cannot serialize character field.");
+                       BT_LOGW_STR("Cannot serialize character field.");
                        goto end;
                }
        }
@@ -2785,7 +2829,7 @@ int bt_ctf_field_structure_copy(struct bt_ctf_field *src,
                struct bt_ctf_field *field_copy = NULL;
 
                if (field) {
-                       BT_LOGD("Copying structure field's field: src-field-addr=%p",
+                       BT_LOGD("Copying structure field's field: src-field-addr=%p"
                                "index=%" PRId64, field, i);
                        field_copy = bt_ctf_field_copy(field);
                        if (!field_copy) {
@@ -2992,8 +3036,8 @@ int increase_packet_size(struct bt_ctf_stream_pos *pos)
                pos->offset, pos->packet_size);
        ret = munmap_align(pos->base_mma);
        if (ret) {
-               BT_LOGE("Failed to perform an aligned memory unmapping: "
-                       "ret=%d, errno=%d", ret, errno);
+               BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping",
+                       ": ret=%d", ret);
                goto end;
        }
 
@@ -3003,8 +3047,8 @@ int increase_packet_size(struct bt_ctf_stream_pos *pos)
                        pos->packet_size / CHAR_BIT);
        } while (ret == EINTR);
        if (ret) {
-               BT_LOGE("Failed to preallocate memory space: ret=%d, errno=%d",
-                       ret, errno);
+               BT_LOGE_ERRNO("Failed to preallocate memory space",
+                       ": ret=%d", ret);
                errno = EINTR;
                ret = -1;
                goto end;
@@ -3013,14 +3057,16 @@ int increase_packet_size(struct bt_ctf_stream_pos *pos)
        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) {
-               BT_LOGE("Failed to perform an aligned memory mapping: "
-                       "ret=%d, errno=%d", ret, errno);
+               BT_LOGE_ERRNO("Failed to perform an aligned memory mapping",
+                       ": ret=%d", ret);
                ret = -1;
        }
 
        BT_LOGV("Increased packet size: pos-offset=%" PRId64 ", "
                "new-packet-size=%" PRIu64,
                pos->offset, pos->packet_size);
+       assert(pos->packet_size % 8 == 0);
+
 end:
        return ret;
 }
This page took 0.028736 seconds and 4 git commands to generate.