common: add bt_common_read() function
[babeltrace.git] / include / babeltrace / common-internal.h
index e22a9231a8590b6d661a8d3375a10ef0dc22bde3..19594fe3c347f4345788befe9078df3dac13bcf2 100644 (file)
@@ -1,15 +1,41 @@
 #ifndef BABELTRACE_COMMON_INTERNAL_H
 #define BABELTRACE_COMMON_INTERNAL_H
 
+/*
+ * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
+ * Copyright (c) 2018 Philippe Proulx <pproulx@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 <stdbool.h>
 #include <babeltrace/assert-internal.h>
 #include <babeltrace/babeltrace-internal.h>
-#include <babeltrace/ctf-ir/field-types.h>
-#include <babeltrace/ctf-ir/field-path.h>
-#include <babeltrace/ctf-ir/event-class.h>
-#include <stdarg.h>
+#include <babeltrace/trace-ir/field-class-const.h>
+#include <babeltrace/trace-ir/field-path-const.h>
+#include <babeltrace/trace-ir/event-class-const.h>
+#include <babeltrace/graph/self-message-iterator.h>
+#include <babeltrace/value.h>
 #include <inttypes.h>
+#include <stdarg.h>
 #include <stdint.h>
+#include <unistd.h>
 #include <glib.h>
 
 #define BT_COMMON_COLOR_RESET              "\033[0m"
@@ -280,47 +306,96 @@ void bt_common_custom_snprintf(char *buf, size_t buf_size,
 BT_HIDDEN
 size_t bt_common_get_page_size(void);
 
+/*
+ * Wraps read() function to handle EINTR and partial reads.
+ * On success, it returns `count` received as parameter. On error, it returns a
+ * value smaller than the requested `count`.
+ */
+static inline
+ssize_t bt_common_read(int fd, void *buf, size_t count)
+{
+       size_t i = 0;
+       ssize_t ret;
+
+       BT_ASSERT(buf);
+
+       /* Never return an overflow value. */
+       BT_ASSERT(count <= SSIZE_MAX);
+
+       do {
+               ret = read(fd, buf + i, count - i);
+               if (ret < 0) {
+                       if (errno == EINTR) {
+#ifdef BT_LOGD_STR
+                               BT_LOGD_STR("read() call interrupted. Retrying...");
+#endif
+                               /* retry operation */
+                               continue;
+                       } else {
+#ifdef BT_LOGE_ERRNO
+                               BT_LOGE_ERRNO("Error while reading", ": fd=%d",
+                                       fd);
+#endif
+                               goto end;
+                       }
+               }
+               i += ret;
+               BT_ASSERT(i <= count);
+       } while (count - i > 0 && ret > 0);
+
+end:
+       if (ret >= 0) {
+               if (i == 0) {
+                       ret = -1;
+               } else {
+                       ret = i;
+               }
+       }
+
+       return ret;
+}
+
 static inline
-const char *bt_common_field_type_id_string(enum bt_field_type_id type_id)
+const char *bt_common_field_class_type_string(enum bt_field_class_type class_type)
 {
-       switch (type_id) {
-       case BT_FIELD_TYPE_ID_UNSIGNED_INTEGER:
-               return "BT_FIELD_TYPE_ID_UNSIGNED_INTEGER";
-       case BT_FIELD_TYPE_ID_SIGNED_INTEGER:
-               return "BT_FIELD_TYPE_ID_SIGNED_INTEGER";
-       case BT_FIELD_TYPE_ID_REAL:
-               return "BT_FIELD_TYPE_ID_REAL";
-       case BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION:
-               return "BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION";
-       case BT_FIELD_TYPE_ID_SIGNED_ENUMERATION:
-               return "BT_FIELD_TYPE_ID_SIGNED_ENUMERATION";
-       case BT_FIELD_TYPE_ID_STRING:
-               return "BT_FIELD_TYPE_ID_STRING";
-       case BT_FIELD_TYPE_ID_STRUCTURE:
-               return "BT_FIELD_TYPE_ID_STRUCTURE";
-       case BT_FIELD_TYPE_ID_STATIC_ARRAY:
-               return "BT_FIELD_TYPE_ID_STATIC_ARRAY";
-       case BT_FIELD_TYPE_ID_DYNAMIC_ARRAY:
-               return "BT_FIELD_TYPE_ID_DYNAMIC_ARRAY";
-       case BT_FIELD_TYPE_ID_VARIANT:
-               return "BT_FIELD_TYPE_ID_VARIANT";
+       switch (class_type) {
+       case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
+               return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
+       case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
+               return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
+       case BT_FIELD_CLASS_TYPE_REAL:
+               return "BT_FIELD_CLASS_TYPE_REAL";
+       case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
+               return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
+       case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
+               return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
+       case BT_FIELD_CLASS_TYPE_STRING:
+               return "BT_FIELD_CLASS_TYPE_STRING";
+       case BT_FIELD_CLASS_TYPE_STRUCTURE:
+               return "BT_FIELD_CLASS_TYPE_STRUCTURE";
+       case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
+               return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
+       case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
+               return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
+       case BT_FIELD_CLASS_TYPE_VARIANT:
+               return "BT_FIELD_CLASS_TYPE_VARIANT";
        default:
                return "(unknown)";
        }
 };
 
 static inline
-const char *bt_common_field_type_integer_preferred_display_base_string(enum bt_field_type_integer_preferred_display_base base)
+const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base)
 {
        switch (base) {
-       case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
-               return "BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
-       case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
-               return "BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
-       case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
-               return "BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
-       case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
-               return "BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
+       case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
+               return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
+       case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
+               return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
+       case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
+               return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
+       case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
+               return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
        default:
                return "(unknown)";
        }
@@ -330,12 +405,8 @@ static inline
 const char *bt_common_scope_string(enum bt_scope scope)
 {
        switch (scope) {
-       case BT_SCOPE_PACKET_HEADER:
-               return "BT_SCOPE_PACKET_HEADER";
        case BT_SCOPE_PACKET_CONTEXT:
                return "BT_SCOPE_PACKET_CONTEXT";
-       case BT_SCOPE_EVENT_HEADER:
-               return "BT_SCOPE_EVENT_HEADER";
        case BT_SCOPE_EVENT_COMMON_CONTEXT:
                return "BT_SCOPE_EVENT_COMMON_CONTEXT";
        case BT_SCOPE_EVENT_SPECIFIC_CONTEXT:
@@ -388,7 +459,30 @@ const char *bt_common_event_class_log_level_string(
 };
 
 static inline
-GString *bt_field_path_string(struct bt_field_path *path)
+const char *bt_common_value_type_string(enum bt_value_type type)
+{
+       switch (type) {
+       case BT_VALUE_TYPE_NULL:
+               return "BT_VALUE_TYPE_NULL";
+       case BT_VALUE_TYPE_BOOL:
+               return "BT_VALUE_TYPE_BOOL";
+       case BT_VALUE_TYPE_INTEGER:
+               return "BT_VALUE_TYPE_INTEGER";
+       case BT_VALUE_TYPE_REAL:
+               return "BT_VALUE_TYPE_REAL";
+       case BT_VALUE_TYPE_STRING:
+               return "BT_VALUE_TYPE_STRING";
+       case BT_VALUE_TYPE_ARRAY:
+               return "BT_VALUE_TYPE_ARRAY";
+       case BT_VALUE_TYPE_MAP:
+               return "BT_VALUE_TYPE_MAP";
+       default:
+               return "(unknown)";
+       }
+};
+
+static inline
+GString *bt_common_field_path_string(struct bt_field_path *path)
 {
        GString *str = g_string_new(NULL);
        uint64_t i;
@@ -402,9 +496,21 @@ GString *bt_field_path_string(struct bt_field_path *path)
        g_string_append_printf(str, "[%s", bt_common_scope_string(
                bt_field_path_get_root_scope(path)));
 
-       for (i = 0; i < bt_field_path_get_index_count(path); i++) {
-               g_string_append_printf(str, ", %" PRIu64,
-                       bt_field_path_get_index_by_index(path, i));
+       for (i = 0; i < bt_field_path_get_item_count(path); i++) {
+               const struct bt_field_path_item *fp_item =
+                       bt_field_path_borrow_item_by_index_const(path, i);
+
+               switch (bt_field_path_item_get_type(fp_item)) {
+               case BT_FIELD_PATH_ITEM_TYPE_INDEX:
+                       g_string_append_printf(str, ", %" PRIu64,
+                               bt_field_path_item_index_get_index(fp_item));
+                       break;
+               case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT:
+                       g_string_append(str, ", <CUR>");
+                       break;
+               default:
+                       abort();
+               }
        }
 
        g_string_append(str, "]");
@@ -413,4 +519,121 @@ end:
        return str;
 }
 
+static inline
+const char *bt_common_self_message_iterator_status_string(
+               enum bt_self_message_iterator_status status)
+{
+       switch (status) {
+       case BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN:
+               return "BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN";
+       case BT_SELF_MESSAGE_ITERATOR_STATUS_END:
+               return "BT_SELF_MESSAGE_ITERATOR_STATUS_END";
+       case BT_SELF_MESSAGE_ITERATOR_STATUS_OK:
+               return "BT_SELF_MESSAGE_ITERATOR_STATUS_OK";
+       case BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR:
+               return "BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR";
+       case BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM:
+               return "BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM";
+       default:
+               return "(unknown)";
+       }
+};
+
+#define NS_PER_S_I     INT64_C(1000000000)
+#define NS_PER_S_U     UINT64_C(1000000000)
+
+static inline
+int bt_common_clock_value_from_ns_from_origin(
+               int64_t cc_offset_seconds, uint64_t cc_offset_cycles,
+               uint64_t cc_freq, int64_t ns_from_origin,
+               uint64_t *raw_value)
+{
+       int ret = 0;
+       int64_t offset_in_ns;
+       uint64_t value_in_ns;
+       uint64_t rem_value_in_ns;
+       uint64_t value_periods;
+       uint64_t value_period_cycles;
+       int64_t ns_to_add;
+
+       BT_ASSERT(raw_value);
+
+       /* Compute offset part of requested value, in nanoseconds */
+       if (!bt_safe_to_mul_int64(cc_offset_seconds, NS_PER_S_I)) {
+               ret = -1;
+               goto end;
+       }
+
+       offset_in_ns = cc_offset_seconds * NS_PER_S_I;
+
+       if (cc_freq == NS_PER_S_U) {
+               ns_to_add = (int64_t) cc_offset_cycles;
+       } else {
+               if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles,
+                               NS_PER_S_I)) {
+                       ret = -1;
+                       goto end;
+               }
+
+               ns_to_add = ((int64_t) cc_offset_cycles * NS_PER_S_I) /
+                       (int64_t) cc_freq;
+       }
+
+       if (!bt_safe_to_add_int64(offset_in_ns, ns_to_add)) {
+               ret = -1;
+               goto end;
+       }
+
+       offset_in_ns += ns_to_add;
+
+       /* Value part in nanoseconds */
+       if (ns_from_origin < offset_in_ns) {
+               ret = -1;
+               goto end;
+       }
+
+       value_in_ns = (uint64_t) (ns_from_origin - offset_in_ns);
+
+       /* Number of whole clock periods in `value_in_ns` */
+       value_periods = value_in_ns / NS_PER_S_U;
+
+       /* Remaining nanoseconds in cycles + whole clock periods in cycles */
+       rem_value_in_ns = value_in_ns - value_periods * NS_PER_S_U;
+
+       if (value_periods > UINT64_MAX / cc_freq) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!bt_safe_to_mul_uint64(value_periods, cc_freq)) {
+               ret = -1;
+               goto end;
+       }
+
+       value_period_cycles = value_periods * cc_freq;
+
+       if (!bt_safe_to_mul_uint64(cc_freq, rem_value_in_ns)) {
+               ret = -1;
+               goto end;
+       }
+
+       if (!bt_safe_to_add_uint64(cc_freq * rem_value_in_ns / NS_PER_S_U,
+                       value_period_cycles)) {
+               ret = -1;
+               goto end;
+       }
+
+       *raw_value = cc_freq * rem_value_in_ns / NS_PER_S_U +
+               value_period_cycles;
+
+end:
+       return ret;
+}
+
+static inline
+enum bt_self_message_iterator_status bt_common_message_iterator_status_to_self(
+               enum bt_message_iterator_status status)
+{
+       return (int) status;
+}
 #endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.029213 seconds and 4 git commands to generate.