#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/trace-ir/field-classes.h>
+#include <babeltrace/trace-ir/field-path.h>
+#include <babeltrace/trace-ir/event-class.h>
+#include <babeltrace/values.h>
+#include <stdarg.h>
+#include <inttypes.h>
+#include <stdint.h>
+#include <glib.h>
#define BT_COMMON_COLOR_RESET "\033[0m"
#define BT_COMMON_COLOR_BOLD "\033[1m"
BT_HIDDEN
GString *bt_common_normalize_path(const char *path, const char *wd);
+typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data,
+ char **buf, size_t avail_size, const char **fmt, va_list *args);
+
+/*
+ * This is a custom vsnprintf() which handles the standard conversion
+ * specifier as well as custom ones.
+ *
+ * `fmt` is a typical printf()-style format string, with the following
+ * limitations:
+ *
+ * * The `*` width specifier is not accepted.
+ * * The `*` precision specifier is not accepted.
+ * * The `j` and `t` length modifiers are not accepted.
+ * * The `n` format specifier is not accepted.
+ * * The format specifiers defined in <inttypes.h> are not accepted
+ * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
+ * `PRIi64`.
+ *
+ * `intro` specifies which special character immediately following an
+ * introductory `%` character in `fmt` is used to indicate a custom
+ * conversion specifier. For example, if `intro` is '@', then any `%@`
+ * sequence in `fmt` is the beginning of a custom conversion specifier.
+ *
+ * When a custom conversion specifier is encountered in `fmt`,
+ * the function calls `handle_specifier`. This callback receives:
+ *
+ * `priv_data`:
+ * Custom, private data.
+ *
+ * `buf`:
+ * Address of the current buffer pointer. `*buf` is the position to
+ * append new data. The callback must update `*buf` when appending
+ * new data. The callback must ensure not to write passed the whole
+ * buffer passed to bt_common_custom_vsnprintf().
+ *
+ * `avail_size`:
+ * Number of bytes left in whole buffer from the `*buf` point.
+ *
+ * `fmt`:
+ * Address of the current format string pointer. `*fmt` points to
+ * the introductory `%` character, which is followed by the
+ * character `intro`. The callback must update `*fmt` so that it
+ * points after the whole custom conversion specifier.
+ *
+ * `args`:
+ * Variable argument list. Use va_arg() to get new arguments from
+ * this list and update it at the same time.
+ *
+ * Because this is an internal utility, this function and its callback
+ * do not return error codes: they abort when there's any error (bad
+ * format string, for example).
+ */
+BT_HIDDEN
+void bt_common_custom_vsnprintf(char *buf, size_t buf_size,
+ char intro,
+ bt_common_handle_custom_specifier_func handle_specifier,
+ void *priv_data, const char *fmt, va_list *args);
+
+/*
+ * Variadic form of bt_common_custom_vsnprintf().
+ */
+BT_HIDDEN
+void bt_common_custom_snprintf(char *buf, size_t buf_size,
+ char intro,
+ bt_common_handle_custom_specifier_func handle_specifier,
+ void *priv_data, const char *fmt, ...);
+
/*
- * Return the system page size.
+ * Returns the system page size.
*/
BT_HIDDEN
size_t bt_common_get_page_size(void);
+static inline
+const char *bt_common_field_class_type_string(enum bt_field_class_type class_type)
+{
+ 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_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base)
+{
+ switch (base) {
+ 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)";
+ }
+}
+
+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:
+ return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
+ case BT_SCOPE_EVENT_PAYLOAD:
+ return "BT_SCOPE_EVENT_PAYLOAD";
+ default:
+ return "(unknown)";
+ }
+}
+
+static inline
+const char *bt_common_event_class_log_level_string(
+ enum bt_event_class_log_level level)
+{
+ switch (level) {
+ case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
+ return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
+ case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
+ return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
+ case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
+ return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
+ case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
+ return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
+ case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
+ return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
+ case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
+ return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
+ case BT_EVENT_CLASS_LOG_LEVEL_INFO:
+ return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
+ case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
+ return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
+ case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
+ return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
+ case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
+ return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
+ case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
+ return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
+ case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
+ return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
+ case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
+ return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
+ case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
+ return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
+ case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
+ return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
+ default:
+ return "(unknown)";
+ }
+};
+
+static inline
+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_field_path_string(struct bt_field_path *path)
+{
+ GString *str = g_string_new(NULL);
+ uint64_t i;
+
+ BT_ASSERT(path);
+
+ if (!str) {
+ goto end;
+ }
+
+ 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));
+ }
+
+ g_string_append(str, "]");
+
+end:
+ return str;
+}
+
#endif /* BABELTRACE_COMMON_INTERNAL_H */