lib: add bt_{graph,query_executor}_add_interrupter()
[babeltrace.git] / src / lib / lib-logging.c
index 60ed8b6b82bafc2d6522537da4d6cd2dad470ddf..e9afb80656da61d7bb1cdbbc5dbdd28183e540d4 100644 (file)
  * SOFTWARE.
  */
 
-/*
- * This is just to satisfy the preprocessor check in "lib/logging.h":
- * this file does not log anything.
- */
-#define BT_LOG_TAG ""
+#define BT_LOG_TAG "LIB/LIB-LOGGING"
+#include "lib/logging.h"
 
 #include <stdarg.h>
 #include <stdio.h>
 #include <wchar.h>
 #include <glib.h>
 #include "common/common.h"
-#include "lib/value.h"
-#include "lib/value.h"
-#include "lib/object-pool.h"
-#include <babeltrace2/trace-ir/event-const.h>
-#include <babeltrace2/trace-ir/packet-const.h>
-#include <babeltrace2/trace-ir/stream-const.h>
+#include "common/uuid.h"
+#include <babeltrace2/babeltrace.h>
 
+#include "logging.h"
+#include "assert-pre.h"
+#include "assert-post.h"
+#include "value.h"
+#include "integer-range-set.h"
+#include "object-pool.h"
+#include "graph/interrupter.h"
 #include "graph/component-class.h"
 #include "graph/component-class-sink-colander.h"
 #include "graph/component-filter.h"
 #include "graph/message/message.h"
 #include "graph/message/message-iterator-inactivity.h"
 #include "graph/message/packet.h"
-#include "graph/message/stream-activity.h"
 #include "graph/message/stream.h"
 #include "graph/port.h"
-#include "logging.h"
 #include "plugin/plugin.h"
 #include "plugin/plugin-so.h"
 #include "trace-ir/clock-class.h"
@@ -75,6 +73,7 @@
 #include "trace-ir/trace-class.h"
 #include "trace-ir/trace.h"
 #include "trace-ir/utils.h"
+#include "error.h"
 #include "assert-pre.h"
 
 #define LIB_LOGGING_BUF_SIZE   (4096 * 4)
@@ -104,11 +103,17 @@ static __thread char lib_logging_buf[LIB_LOGGING_BUF_SIZE];
 
 #define PRFIELD_GSTRING(_expr) PRFIELD((_expr) ? (_expr)->str : NULL)
 
-#define TMP_PREFIX_LEN 64
+#define TMP_PREFIX_LEN 128
 #define SET_TMP_PREFIX(_prefix2)                                       \
        do {                                                            \
-               snprintf(tmp_prefix, TMP_PREFIX_LEN - 1, "%s%s",        \
-                       prefix, (_prefix2));                            \
+               int snprintf_ret =                                      \
+                       snprintf(tmp_prefix, TMP_PREFIX_LEN - 1, "%s%s", \
+                               prefix, (_prefix2));                    \
+                                                                       \
+               if (snprintf_ret < 0 || snprintf_ret >= TMP_PREFIX_LEN - 1) { \
+                       abort();                                        \
+               }                                                       \
+                                                                       \
                tmp_prefix[TMP_PREFIX_LEN - 1] = '\0';                  \
        } while (0)
 
@@ -135,23 +140,7 @@ static inline void format_object(char **buf_ch, bool extended,
 
 static inline void format_uuid(char **buf_ch, bt_uuid uuid)
 {
-       BUF_APPEND("\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
-               (unsigned int) uuid[0],
-               (unsigned int) uuid[1],
-               (unsigned int) uuid[2],
-               (unsigned int) uuid[3],
-               (unsigned int) uuid[4],
-               (unsigned int) uuid[5],
-               (unsigned int) uuid[6],
-               (unsigned int) uuid[7],
-               (unsigned int) uuid[8],
-               (unsigned int) uuid[9],
-               (unsigned int) uuid[10],
-               (unsigned int) uuid[11],
-               (unsigned int) uuid[12],
-               (unsigned int) uuid[13],
-               (unsigned int) uuid[14],
-               (unsigned int) uuid[15]);
+       BUF_APPEND("\"" BT_UUID_FMT "\"", BT_UUID_FMT_VALUES(uuid));
 }
 
 static inline void format_object_pool(char **buf_ch, bool extended,
@@ -244,7 +233,7 @@ static inline void format_field_class(char **buf_ch, bool extended,
        }
        case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
        {
-               const struct bt_field_class_static_array *array_fc =
+               const struct bt_field_class_array_static *array_fc =
                        (const void *) field_class;
 
                format_array_field_class(buf_ch, extended, prefix, field_class);
@@ -253,7 +242,7 @@ static inline void format_field_class(char **buf_ch, bool extended,
        }
        case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY:
        {
-               const struct bt_field_class_dynamic_array *array_fc =
+               const struct bt_field_class_array_dynamic *array_fc =
                        (const void *) field_class;
 
                format_array_field_class(buf_ch, extended, prefix, field_class);
@@ -272,7 +261,9 @@ static inline void format_field_class(char **buf_ch, bool extended,
 
                break;
        }
-       case BT_FIELD_CLASS_TYPE_VARIANT:
+       case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
+       case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
+       case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
        {
                const struct bt_field_class_variant *var_fc =
                        (const void *) field_class;
@@ -282,16 +273,22 @@ static inline void format_field_class(char **buf_ch, bool extended,
                                PRFIELD(var_fc->common.named_fcs->len));
                }
 
-               if (var_fc->selector_fc) {
-                       SET_TMP_PREFIX("selector-fc-");
-                       format_field_class(buf_ch, extended, tmp_prefix,
-                               var_fc->selector_fc);
-               }
+               if (field_class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR ||
+                               field_class->type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) {
+                       const struct bt_field_class_variant_with_selector *var_with_sel_fc =
+                               (const void *) var_fc;
 
-               if (var_fc->selector_field_path) {
-                       SET_TMP_PREFIX("selector-field-path-");
-                       format_field_path(buf_ch, extended, tmp_prefix,
-                               var_fc->selector_field_path);
+                       if (var_with_sel_fc->selector_fc) {
+                               SET_TMP_PREFIX("selector-fc-");
+                               format_field_class(buf_ch, extended, tmp_prefix,
+                                       var_with_sel_fc->selector_fc);
+                       }
+
+                       if (var_with_sel_fc->selector_field_path) {
+                               SET_TMP_PREFIX("selector-field-path-");
+                               format_field_path(buf_ch, extended, tmp_prefix,
+                                       var_with_sel_fc->selector_field_path);
+                       }
                }
 
                break;
@@ -397,7 +394,9 @@ static inline void format_field(char **buf_ch, bool extended,
 
                break;
        }
-       case BT_FIELD_CLASS_TYPE_VARIANT:
+       case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR:
+       case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR:
+       case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR:
        {
                const struct bt_field_variant *var_field = (const void *) field;
 
@@ -451,21 +450,12 @@ static inline void format_field_path(char **buf_ch, bool extended,
 static inline void format_trace_class(char **buf_ch, bool extended,
                const char *prefix, const struct bt_trace_class *trace_class)
 {
-       if (trace_class->name.value) {
-               BUF_APPEND(", %sname=\"%s\"",
-                       PRFIELD(trace_class->name.value));
-       }
-
        if (!extended) {
                return;
        }
 
        BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace_class->frozen));
 
-       if (trace_class->uuid.value) {
-               BUF_APPEND_UUID(trace_class->uuid.value);
-       }
-
        if (trace_class->stream_classes) {
                BUF_APPEND(", %sstream-class-count=%u",
                        PRFIELD(trace_class->stream_classes->len));
@@ -488,6 +478,10 @@ static inline void format_trace(char **buf_ch, bool extended,
                return;
        }
 
+       if (trace->uuid.value) {
+               BUF_APPEND_UUID(trace->uuid.value);
+       }
+
        BUF_APPEND(", %sis-frozen=%d", PRFIELD(trace->frozen));
 
        if (trace->streams) {
@@ -539,14 +533,16 @@ static inline void format_stream_class(char **buf_ch, bool extended,
        }
 
        BUF_APPEND(", %sassigns-auto-ec-id=%d, %sassigns-auto-stream-id=%d, "
-               "%spackets-have-default-beginning-cs=%d, "
-               "%spackets-have-default-end-cs=%d, "
+               "%ssupports-packets=%d, "
+               "%spackets-have-begin-default-cs=%d, "
+               "%spackets-have-end-default-cs=%d, "
                "%ssupports-discarded-events=%d, "
                "%sdiscarded-events-have-default-cs=%d, "
                "%ssupports-discarded-packets=%d, "
                "%sdiscarded-packets-have-default-cs=%d",
                PRFIELD(stream_class->assigns_automatic_event_class_id),
                PRFIELD(stream_class->assigns_automatic_stream_id),
+               PRFIELD(stream_class->supports_packets),
                PRFIELD(stream_class->packets_have_beginning_default_clock_snapshot),
                PRFIELD(stream_class->packets_have_end_default_clock_snapshot),
                PRFIELD(stream_class->supports_discarded_events),
@@ -695,8 +691,6 @@ static inline void format_packet(char **buf_ch, bool extended,
 static inline void format_event(char **buf_ch, bool extended,
                const char *prefix, const struct bt_event *event)
 {
-       const struct bt_packet *packet;
-       const struct bt_stream *stream;
        const struct bt_trace_class *trace_class;
        const struct bt_stream_class *stream_class;
        char tmp_prefix[TMP_PREFIX_LEN];
@@ -739,22 +733,17 @@ static inline void format_event(char **buf_ch, bool extended,
                }
        }
 
-       packet = bt_event_borrow_packet_const(event);
-       if (!packet) {
-               return;
+       if (event->stream) {
+               BUF_APPEND(", %sstream-addr=%p", PRFIELD(event->stream));
+               SET_TMP_PREFIX("stream-");
+               format_stream(buf_ch, false, tmp_prefix, event->stream);
        }
 
-       BUF_APPEND(", %spacket-addr=%p", PRFIELD(packet));
-       SET_TMP_PREFIX("packet-");
-       format_packet(buf_ch, false, tmp_prefix, packet);
-       stream = bt_packet_borrow_stream_const(packet);
-       if (!stream) {
-               return;
+       if (event->packet) {
+               BUF_APPEND(", %spacket-addr=%p", PRFIELD(event->packet));
+               SET_TMP_PREFIX("packet-");
+               format_packet(buf_ch, false, tmp_prefix, event->packet);
        }
-
-       BUF_APPEND(", %sstream-addr=%p", PRFIELD(stream));
-       SET_TMP_PREFIX("stream-");
-       format_stream(buf_ch, false, tmp_prefix, stream);
 }
 
 static inline void format_clock_class(char **buf_ch, bool extended,
@@ -819,6 +808,12 @@ static inline void format_clock_snapshot(char **buf_ch, bool extended,
        }
 }
 
+static inline void format_interrupter(char **buf_ch, bool extended,
+               const char *prefix, const struct bt_interrupter *intr)
+{
+       BUF_APPEND(", %sis-set=%d", PRFIELD(intr->is_set));
+}
+
 static inline void format_value(char **buf_ch, bool extended,
                const char *prefix, const struct bt_value *value)
 {
@@ -840,13 +835,13 @@ static inline void format_value(char **buf_ch, bool extended,
        case BT_VALUE_TYPE_UNSIGNED_INTEGER:
        {
                BUF_APPEND(", %svalue=%" PRIu64,
-                       PRFIELD(bt_value_unsigned_integer_get(value)));
+                       PRFIELD(bt_value_integer_unsigned_get(value)));
                break;
        }
        case BT_VALUE_TYPE_SIGNED_INTEGER:
        {
                BUF_APPEND(", %svalue=%" PRId64,
-                       PRFIELD(bt_value_signed_integer_get(value)));
+                       PRFIELD(bt_value_integer_signed_get(value)));
                break;
        }
        case BT_VALUE_TYPE_REAL:
@@ -884,6 +879,19 @@ static inline void format_value(char **buf_ch, bool extended,
        }
 }
 
+static inline void format_integer_range_set(char **buf_ch, bool extended,
+               const char *prefix,
+               const struct bt_integer_range_set *range_set)
+{
+       BUF_APPEND(", %srange-count=%u", PRFIELD(range_set->ranges->len));
+
+       if (!extended) {
+               return;
+       }
+
+       BUF_APPEND(", %sis-frozen=%d", PRFIELD(range_set->frozen));
+}
+
 static inline void format_message(char **buf_ch, bool extended,
                const char *prefix, const struct bt_message *msg)
 {
@@ -930,28 +938,14 @@ static inline void format_message(char **buf_ch, bool extended,
                                msg_stream->stream);
                }
 
-               break;
-       }
-       case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
-       case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
-       {
-               const struct bt_message_stream_activity *msg_stream_activity =
-                       (const void *) msg;
-
-               if (msg_stream_activity->stream) {
-                       SET_TMP_PREFIX("stream-");
-                       format_stream(buf_ch, true, tmp_prefix,
-                               msg_stream_activity->stream);
-               }
-
                BUF_APPEND(", %sdefault-cs-state=%s",
-                       PRFIELD(bt_message_stream_activity_clock_snapshot_state_string(
-                               msg_stream_activity->default_cs_state)));
+                       PRFIELD(bt_message_stream_clock_snapshot_state_string(
+                               msg_stream->default_cs_state)));
 
-               if (msg_stream_activity->default_cs) {
+               if (msg_stream->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
                        SET_TMP_PREFIX("default-cs-");
                        format_clock_snapshot(buf_ch, true, tmp_prefix,
-                               msg_stream_activity->default_cs);
+                               msg_stream->default_cs);
                }
 
                break;
@@ -988,13 +982,13 @@ static inline void format_message(char **buf_ch, bool extended,
                }
 
                if (msg_disc_items->default_begin_cs) {
-                       SET_TMP_PREFIX("default-begin-cs-");
+                       SET_TMP_PREFIX("begin-default-cs-");
                        format_clock_snapshot(buf_ch, true, tmp_prefix,
                                msg_disc_items->default_begin_cs);
                }
 
                if (msg_disc_items->default_end_cs) {
-                       SET_TMP_PREFIX("default-end-cs-");
+                       SET_TMP_PREFIX("end-default-cs-");
                        format_clock_snapshot(buf_ch, true, tmp_prefix,
                                msg_disc_items->default_end_cs);
                }
@@ -1126,9 +1120,7 @@ static inline void format_graph(char **buf_ch, bool extended,
 {
        char tmp_prefix[TMP_PREFIX_LEN];
 
-       BUF_APPEND(", %sis-canceled=%d, %scan-consume=%d, "
-               "%sconfig-state=%s",
-               PRFIELD(graph->canceled),
+       BUF_APPEND(", %scan-consume=%d, %sconfig-state=%s",
                PRFIELD(graph->can_consume),
                PRFIELD(bt_graph_configuration_state_string(graph->config_state)));
 
@@ -1281,6 +1273,66 @@ static inline void format_plugin(char **buf_ch, bool extended,
        }
 }
 
+static inline void format_error_cause(char **buf_ch, bool extended,
+               const char *prefix, const struct bt_error_cause *cause)
+{
+       const struct bt_error_cause_component_class_id *comp_class_id = NULL;
+
+       BUF_APPEND(", %sactor-type=%s, %smodule-name=\"%s\"",
+               PRFIELD(bt_error_cause_actor_type_string(cause->actor_type)),
+               PRFIELD_GSTRING(cause->module_name));
+
+       if (!extended) {
+               return;
+       }
+
+       BUF_APPEND(", %spartial-msg=\"%.32s\"",
+               PRFIELD_GSTRING(cause->message));
+
+       switch (cause->actor_type) {
+       case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT:
+       {
+               const struct bt_error_cause_component_actor *spec_cause =
+                       (const void *) cause;
+
+               BUF_APPEND(", %scomp-name=\"%s\"",
+                       PRFIELD_GSTRING(spec_cause->comp_name));
+               comp_class_id = &spec_cause->comp_class_id;
+               break;
+       }
+       case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS:
+       {
+               const struct bt_error_cause_component_class_actor *spec_cause =
+                       (const void *) cause;
+
+               comp_class_id = &spec_cause->comp_class_id;
+               break;
+       }
+       case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR:
+       {
+               const struct bt_error_cause_message_iterator_actor *spec_cause =
+                       (const void *) cause;
+
+               BUF_APPEND(", %scomp-name=\"%s\", %scomp-out-port-name=\"%s\"",
+                       PRFIELD_GSTRING(spec_cause->comp_name),
+                       PRFIELD_GSTRING(spec_cause->output_port_name));
+               comp_class_id = &spec_cause->comp_class_id;
+               break;
+       }
+       default:
+               break;
+       }
+
+       if (comp_class_id) {
+               BUF_APPEND(", %scomp-cls-type=%s, %scomp-cls-name=\"%s\", "
+                       "%splugin-name=\"%s\"",
+                       PRFIELD(bt_component_class_type_string(
+                               comp_class_id->type)),
+                       PRFIELD_GSTRING(comp_class_id->name),
+                       PRFIELD_GSTRING(comp_class_id->plugin_name));
+       }
+}
+
 static inline void handle_conversion_specifier_bt(void *priv_data,
                char **buf_ch, size_t avail_size,
                const char **out_fmt_ch, va_list *args)
@@ -1372,6 +1424,9 @@ static inline void handle_conversion_specifier_bt(void *priv_data,
        case 'v':
                format_value(buf_ch, extended, prefix, obj);
                break;
+       case 'R':
+               format_integer_range_set(buf_ch, extended, prefix, obj);
+               break;
        case 'n':
                format_message(buf_ch, extended, prefix, obj);
                break;
@@ -1396,12 +1451,18 @@ static inline void handle_conversion_specifier_bt(void *priv_data,
        case 'g':
                format_graph(buf_ch, extended, prefix, obj);
                break;
+       case 'z':
+               format_interrupter(buf_ch, extended, prefix, obj);
+               break;
        case 'o':
                format_object_pool(buf_ch, extended, prefix, obj);
                break;
        case 'O':
                format_object(buf_ch, extended, prefix, obj);
                break;
+       case 'r':
+               format_error_cause(buf_ch, extended, prefix, obj);
+               break;
        default:
                abort();
        }
@@ -1423,3 +1484,41 @@ void bt_lib_log(const char *func, const char *file, unsigned line,
        va_end(args);
        _bt_log_write_d(func, file, line, lvl, tag, "%s", lib_logging_buf);
 }
+
+void bt_lib_maybe_log_and_append_cause(const char *func, const char *file,
+               unsigned line, int lvl, const char *tag,
+               const char *fmt, ...)
+{
+       va_list args;
+       bt_current_thread_error_append_cause_status status;
+
+       BT_ASSERT(fmt);
+       va_start(args, fmt);
+       bt_common_custom_vsnprintf(lib_logging_buf, LIB_LOGGING_BUF_SIZE, '!',
+               handle_conversion_specifier_bt, NULL, fmt, &args);
+       va_end(args);
+
+       /* Log conditionally, but always append the error cause */
+       if (BT_LOG_ON(lvl)) {
+               _bt_log_write_d(func, file, line, lvl, tag, "%s",
+                       lib_logging_buf);
+       }
+
+       status = bt_current_thread_error_append_cause_from_unknown(
+               "Babeltrace library", file, line, "%s", lib_logging_buf);
+       if (status) {
+               /*
+                * Worst case: this error cause is not appended to the
+                * current thread's error.
+                *
+                * We can accept this as it's an almost impossible
+                * scenario and returning an error here would mean you
+                * need to check the return value of each
+                * BT_LIB_LOG*_APPEND_CAUSE() macro and that would be
+                * cumbersome.
+                */
+               BT_LOGE("Cannot append error cause to current thread's "
+                       "error object: status=%s",
+                       bt_common_func_status_string(status));
+       }
+}
This page took 0.029937 seconds and 4 git commands to generate.