Fix: allow duplicate keys and overlapping ranges in enumerations
[babeltrace.git] / plugins / text / print.c
index a7b67409ffbc7165e01940289d76c04589d42483..5ba979588d9381231bb6b0d2c20827faa3991547 100644 (file)
@@ -4,6 +4,7 @@
  * Babeltrace CTF Text Output Plugin Event Printing
  *
  * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
@@ -31,7 +32,7 @@
 #include <babeltrace/ctf-ir/packet.h>
 #include <babeltrace/ctf-ir/stream.h>
 #include <babeltrace/ctf-ir/stream-class.h>
-#include <babeltrace/ctf-ir/clock.h>
+#include <babeltrace/ctf-ir/clock-class.h>
 #include <babeltrace/ctf-ir/field-types.h>
 #include <babeltrace/ctf-ir/fields.h>
 #include <babeltrace/ctf-ir/trace.h>
@@ -61,14 +62,14 @@ enum bt_component_status print_field(struct text_component *text,
 
 static
 void print_timestamp_cycles(struct text_component *text,
-               struct bt_ctf_clock *clock,
+               struct bt_ctf_clock_class *clock_class,
                struct bt_ctf_event *event)
 {
        int ret;
        struct bt_ctf_clock_value *clock_value;
        uint64_t cycles;
 
-       clock_value = bt_ctf_event_get_clock_value(event, clock);
+       clock_value = bt_ctf_event_get_clock_value(event, clock_class);
        if (!clock_value) {
                fputs("????????????????????", text->out);
                return;
@@ -81,11 +82,16 @@ void print_timestamp_cycles(struct text_component *text,
                return;
        }
        fprintf(text->out, "%020" PRIu64, cycles);
+
+       if (text->last_cycles_timestamp != -1ULL) {
+               text->delta_cycles = cycles - text->last_cycles_timestamp;
+       }
+       text->last_cycles_timestamp = cycles;
 }
 
 static
 void print_timestamp_wall(struct text_component *text,
-               struct bt_ctf_clock *clock,
+               struct bt_ctf_clock_class *clock_class,
                struct bt_ctf_event *event)
 {
        int ret;
@@ -95,7 +101,7 @@ void print_timestamp_wall(struct text_component *text,
        uint64_t ts_sec_abs, ts_nsec_abs;
        bool is_negative;
 
-       clock_value = bt_ctf_event_get_clock_value(event, clock);
+       clock_value = bt_ctf_event_get_clock_value(event, clock_class);
        if (!clock_value) {
                fputs("??:??:??.?????????", text->out);
                return;
@@ -108,6 +114,11 @@ void print_timestamp_wall(struct text_component *text,
                return;
        }
 
+       if (text->last_real_timestamp != -1ULL) {
+               text->delta_real_timestamp = ts_nsec - text->last_real_timestamp;
+       }
+       text->last_real_timestamp = ts_nsec;
+
        ts_sec += ts_nsec / NSEC_PER_SEC;
        ts_nsec = ts_nsec % NSEC_PER_SEC;
        if (ts_sec >= 0 && ts_nsec >= 0) {
@@ -136,7 +147,7 @@ void print_timestamp_wall(struct text_component *text,
                ts_nsec_abs = -ts_nsec;
        }
 
-       if (/*!opt_clock_seconds*/true) {
+       if (!text->options.clock_seconds) {
                struct tm tm;
                time_t time_s = (time_t) ts_sec_abs;
 
@@ -145,7 +156,7 @@ void print_timestamp_wall(struct text_component *text,
                        goto seconds;
                }
 
-               if (/*!opt_clock_gmt*/true) {
+               if (!text->options.clock_gmt) {
                        struct tm *res;
 
                        res = localtime_r(&time_s, &tm);
@@ -162,7 +173,7 @@ void print_timestamp_wall(struct text_component *text,
                                goto seconds;
                        }
                }
-               if (/*opt_clock_date*/false) {
+               if (text->options.clock_date) {
                        char timestr[26];
                        size_t res;
 
@@ -189,17 +200,15 @@ end:
 
 static
 enum bt_component_status print_event_timestamp(struct text_component *text,
-               struct bt_ctf_event *event)
+               struct bt_ctf_event *event, bool *start_line)
 {
        bool print_names = text->options.print_header_field_names;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_stream *stream = NULL;
        struct bt_ctf_stream_class *stream_class = NULL;
        struct bt_ctf_trace *trace = NULL;
-       struct bt_ctf_clock *clock = NULL;
+       struct bt_ctf_clock_class *clock_class = NULL;
        FILE *out = text->out;
-       FILE *err = text->err;
-       uint64_t real_timestamp;
 
        stream = bt_ctf_event_get_stream(event);
        if (!stream) {
@@ -207,25 +216,65 @@ enum bt_component_status print_event_timestamp(struct text_component *text,
                goto end;
        }
 
-       /* FIXME - error checking */
        stream_class = bt_ctf_stream_get_class(stream);
+       if (!stream_class) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
        trace = bt_ctf_stream_class_get_trace(stream_class);
-       clock = bt_ctf_trace_get_clock(trace, 0);
+       if (!trace) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       clock_class = bt_ctf_trace_get_clock_class(trace, 0);
+       if (!clock_class) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
 
        fputs(print_names ? "timestamp = " : "[", out);
        if (text->options.print_timestamp_cycles) {
-               print_timestamp_cycles(text, clock, event);
+               print_timestamp_cycles(text, clock_class, event);
        } else {
-               print_timestamp_wall(text, clock, event);
+               print_timestamp_wall(text, clock_class, event);
+       }
+
+       if (!print_names)
+               fputs("] ", out);
+
+       if (text->options.print_delta_field) {
+               if (print_names)
+                       fputs(", delta = ", text->out);
+               else
+                       fputs("(", text->out);
+               if (text->options.print_timestamp_cycles) {
+                       if (text->delta_cycles == -1ULL) {
+                               fputs("+??????????\?\?) ", text->out); /* Not a trigraph. */
+                       } else {
+                               fprintf(text->out, "+%012" PRIu64, text->delta_cycles);
+                       }
+               } else {
+                       if (text->delta_real_timestamp != -1ULL) {
+                               uint64_t delta_sec, delta_nsec, delta;
+
+                               delta = text->delta_real_timestamp;
+                               delta_sec = delta / NSEC_PER_SEC;
+                               delta_nsec = delta % NSEC_PER_SEC;
+                               fprintf(text->out, "+%" PRIu64 ".%09" PRIu64,
+                                       delta_sec, delta_nsec);
+                       } else {
+                               fputs("+?.?????????", text->out);
+                       }
+               }
+               if (!print_names) {
+                       fputs(") ", text->out);
+               }
        }
+       *start_line = !print_names;
 
-       fputs(print_names ? ", " : "] ", out);
-       if (!text->options.print_delta_field) {
-               goto end;
-       }
 end:
        bt_put(stream);
-       bt_put(clock);
+       bt_put(clock_class);
        bt_put(stream_class);
        bt_put(trace);
        return ret;
@@ -238,25 +287,209 @@ enum bt_component_status print_event_header(struct text_component *text,
        bool print_names = text->options.print_header_field_names;
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_event_class *event_class = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL;
+       struct bt_ctf_trace *trace_class = NULL;
 
        event_class = bt_ctf_event_get_class(event);
        if (!event_class) {
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
+       stream_class = bt_ctf_event_class_get_stream_class(event_class);
+       if (!stream_class) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       trace_class = bt_ctf_stream_class_get_trace(stream_class);
+       if (!trace_class) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
        if (!text->start_line) {
                fputs(", ", text->out);
        }
        text->start_line = false;
-       ret = print_event_timestamp(text, event);
+       ret = print_event_timestamp(text, event, &text->start_line);
        if (ret != BT_COMPONENT_STATUS_OK) {
                goto end;
        }
+       if (text->options.print_trace_field) {
+               const char *name;
+
+               name = bt_ctf_trace_get_name(trace_class);
+               if (name) {
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       text->start_line = false;
+                       if (print_names) {
+                               fputs("trace = ", text->out);
+                       }
+                       fprintf(text->out, "%s", name);
+               }
+       }
+       if (text->options.print_trace_hostname_field) {
+               struct bt_value *hostname_str;
+
+               hostname_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
+                               "hostname");
+               if (hostname_str) {
+                       const char *str;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       text->start_line = false;
+                       if (print_names) {
+                               fputs("trace:hostname = ", text->out);
+                       }
+                       if (bt_value_string_get(hostname_str, &str)
+                                       == BT_VALUE_STATUS_OK) {
+                               fprintf(text->out, "%s", str);
+                       }
+                       bt_put(hostname_str);
+               }
+       }
+       if (text->options.print_trace_domain_field) {
+               struct bt_value *domain_str;
+
+               domain_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
+                               "domain");
+               if (domain_str) {
+                       const char *str;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       text->start_line = false;
+                       if (print_names) {
+                               fputs("trace:domain = ", text->out);
+                       }
+                       if (bt_value_string_get(domain_str, &str)
+                                       == BT_VALUE_STATUS_OK) {
+                               fprintf(text->out, "%s", str);
+                       }
+                       bt_put(domain_str);
+               }
+       }
+       if (text->options.print_trace_procname_field) {
+               struct bt_value *procname_str;
+
+               procname_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
+                               "procname");
+               if (procname_str) {
+                       const char *str;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       text->start_line = false;
+                       if (print_names) {
+                               fputs("trace:procname = ", text->out);
+                       }
+                       if (bt_value_string_get(procname_str, &str)
+                                       == BT_VALUE_STATUS_OK) {
+                               fprintf(text->out, "%s", str);
+                       }
+                       bt_put(procname_str);
+               }
+       }
+       if (text->options.print_trace_vpid_field) {
+               struct bt_value *vpid_value;
+
+               vpid_value = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
+                               "vpid");
+               if (vpid_value) {
+                       int64_t value;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       text->start_line = false;
+                       if (print_names) {
+                               fputs("trace:vpid = ", text->out);
+                       }
+                       if (bt_value_integer_get(vpid_value, &value)
+                                       == BT_VALUE_STATUS_OK) {
+                               fprintf(text->out, "(%" PRId64 ")", value);
+                       }
+                       bt_put(vpid_value);
+               }
+       }
+       if (text->options.print_loglevel_field) {
+               struct bt_value *loglevel_str, *loglevel_value;
+
+               loglevel_str = bt_ctf_event_class_get_attribute_value_by_name(event_class,
+                               "loglevel_string");
+               loglevel_value = bt_ctf_event_class_get_attribute_value_by_name(event_class,
+                               "loglevel");
+               if (loglevel_str || loglevel_value) {
+                       bool has_str = false;
+
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       text->start_line = false;
+                       if (print_names) {
+                               fputs("loglevel = ", text->out);
+                       }
+                       if (loglevel_str) {
+                               const char *str;
+
+                               if (bt_value_string_get(loglevel_str, &str)
+                                               == BT_VALUE_STATUS_OK) {
+                                       fprintf(text->out, "%s", str);
+                                       has_str = true;
+                               }
+                       }
+                       if (loglevel_value) {
+                               int64_t value;
+
+                               if (bt_value_integer_get(loglevel_value, &value)
+                                               == BT_VALUE_STATUS_OK) {
+                                       fprintf(text->out, "%s(%" PRId64 ")",
+                                               has_str ? " " : "", value);
+                               }
+                       }
+                       bt_put(loglevel_str);
+                       bt_put(loglevel_value);
+               }
+       }
+       if (text->options.print_emf_field) {
+               struct bt_value *uri_str;
+
+               uri_str = bt_ctf_event_class_get_attribute_value_by_name(event_class,
+                               "model.emf.uri");
+               if (uri_str) {
+                       if (!text->start_line) {
+                               fputs(", ", text->out);
+                       }
+                       text->start_line = false;
+                       if (print_names) {
+                               fputs("model.emf.uri = ", text->out);
+                       }
+                       if (uri_str) {
+                               const char *str;
+
+                               if (bt_value_string_get(uri_str, &str)
+                                               == BT_VALUE_STATUS_OK) {
+                                       fprintf(text->out, "%s", str);
+                               }
+                       }
+                       bt_put(uri_str);
+               }
+       }
+       if (!text->start_line) {
+               fputs(", ", text->out);
+       }
+       text->start_line = false;
        if (print_names) {
                fputs("name = ", text->out);
        }
        fputs(bt_ctf_event_class_get_name(event_class), text->out);
 end:
+       bt_put(trace_class);
+       bt_put(stream_class);
        bt_put(event_class);
        return ret;
 }
@@ -394,18 +627,75 @@ enum bt_component_status print_enum(struct text_component *text,
 {
        enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
        struct bt_ctf_field *container_field = NULL;
-       const char *mapping_name;
-
+       struct bt_ctf_field_type *enumeration_field_type = NULL;
+       struct bt_ctf_field_type *container_field_type = NULL;
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
+       int nr_mappings = 0;
+       int is_signed;
+
+       enumeration_field_type = bt_ctf_field_get_type(field);
+       if (!enumeration_field_type) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
        container_field = bt_ctf_field_enumeration_get_container(field);
        if (!container_field) {
                ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
-       mapping_name = bt_ctf_field_enumeration_get_mapping_name(field);
-       if (mapping_name) {
-               fprintf(text->out, "( \"%s\"", mapping_name);
+       container_field_type = bt_ctf_field_get_type(container_field);
+       if (!container_field_type) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       is_signed = bt_ctf_field_type_integer_get_signed(container_field_type);
+       if (is_signed < 0) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       if (is_signed) {
+               int64_t value;
+
+               if (bt_ctf_field_signed_integer_get_value(container_field,
+                               &value)) {
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+               iter = bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
+                               enumeration_field_type, value);
        } else {
-               fprintf(text->out, "( <unknown>");
+               uint64_t value;
+
+               if (bt_ctf_field_unsigned_integer_get_value(container_field,
+                               &value)) {
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+               iter = bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
+                               enumeration_field_type, value);
+       }
+       if (!iter) {
+               ret = BT_COMPONENT_STATUS_ERROR;
+               goto end;
+       }
+       fprintf(text->out, "( ");
+       for (;;) {
+               const char *mapping_name;
+
+               if (bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
+                               iter, &mapping_name, NULL, NULL) < 0) {
+                       ret = BT_COMPONENT_STATUS_ERROR;
+                       goto end;
+               }
+               if (nr_mappings++)
+                       fprintf(text->out, ", ");
+               fprintf(text->out, "\"%s\"", mapping_name);
+               if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter) < 0) {
+                       break;
+               }
+       }
+       if (!nr_mappings) {
+               fprintf(text->out, "<unknown>");
        }
        fprintf(text->out, " : container = ");
        ret = print_integer(text, container_field);
@@ -414,7 +704,10 @@ enum bt_component_status print_enum(struct text_component *text,
        }
        fprintf(text->out, " )");
 end:
+       bt_put(iter);
+       bt_put(container_field_type);
        bt_put(container_field);
+       bt_put(enumeration_field_type);
        return ret;
 }
 
@@ -725,7 +1018,7 @@ enum bt_component_status print_variant(struct text_component *text,
                        ret = BT_COMPONENT_STATUS_ERROR;
                        goto end;
                }
-               tag_choice = bt_ctf_field_enumeration_get_mapping_name(tag_field);
+               tag_choice = bt_ctf_field_enumeration_get_single_mapping_name(tag_field);
                if (!tag_choice) {
                        bt_put(tag_field);
                        ret = BT_COMPONENT_STATUS_ERROR;
@@ -800,7 +1093,6 @@ enum bt_component_status print_stream_packet_context(struct text_component *text
        }
        main_field = bt_ctf_packet_get_context(packet);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
        if (!text->start_line) {
@@ -827,7 +1119,6 @@ enum bt_component_status print_event_header_raw(struct text_component *text,
 
        main_field = bt_ctf_event_get_header(event);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
        if (!text->start_line) {
@@ -853,7 +1144,6 @@ enum bt_component_status print_stream_event_context(struct text_component *text,
 
        main_field = bt_ctf_event_get_stream_event_context(event);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
        if (!text->start_line) {
@@ -879,7 +1169,6 @@ enum bt_component_status print_event_context(struct text_component *text,
 
        main_field = bt_ctf_event_get_event_context(event);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
        if (!text->start_line) {
@@ -905,7 +1194,6 @@ enum bt_component_status print_event_payload(struct text_component *text,
 
        main_field = bt_ctf_event_get_payload_field(event);
        if (!main_field) {
-               ret = BT_COMPONENT_STATUS_ERROR;
                goto end;
        }
        if (!text->start_line) {
This page took 0.031745 seconds and 4 git commands to generate.