sessiond: document effect of rotated_after_last_stop on clear
[lttng-tools.git] / src / bin / lttng-sessiond / ust-metadata.c
index fc1ea2e7b6a8f1a97b754c5c38a3972acb4badac..c29ed348b05fe006492d32345992aa47e1f2bd40 100644 (file)
@@ -28,6 +28,7 @@
 #include <unistd.h>
 #include <inttypes.h>
 #include <common/common.h>
+#include <common/time.h>
 
 #include "ust-registry.h"
 #include "ust-clock.h"
@@ -37,7 +38,6 @@
 #define max_t(type, a, b)      ((type) ((a) > (b) ? (a) : (b)))
 #endif
 
-#define NSEC_PER_SEC                   1000000000ULL
 #define NR_CLOCK_OFFSET_SAMPLES                10
 
 struct offset_sample {
@@ -74,7 +74,6 @@ int fls(unsigned int x)
                r -= 2;
        }
        if (!(x & 0x80000000U)) {
-               x <<= 1;
                r -= 1;
        }
        return r;
@@ -86,8 +85,10 @@ int get_count_order(unsigned int count)
        int order;
 
        order = fls(count) - 1;
-       if (count & (count - 1))
+       if (count & (count - 1)) {
                order++;
+       }
+       assert(order >= 0);
        return order;
 }
 
@@ -200,6 +201,61 @@ int print_tabs(struct ust_registry_session *session, size_t nesting)
        return 0;
 }
 
+static
+void sanitize_ctf_identifier(char *out, const char *in)
+{
+       size_t i;
+
+       for (i = 0; i < LTTNG_UST_SYM_NAME_LEN; i++) {
+               switch (in[i]) {
+               case '.':
+               case '$':
+               case ':':
+                       out[i] = '_';
+                       break;
+               default:
+                       out[i] = in[i];
+               }
+       }
+}
+
+static
+int print_escaped_ctf_string(struct ust_registry_session *session, const char *string)
+{
+       int ret = 0;
+       size_t i;
+       char cur;
+
+       i = 0;
+       cur = string[i];
+       while (cur != '\0') {
+               switch (cur) {
+               case '\n':
+                       ret = lttng_metadata_printf(session, "%s", "\\n");
+                       break;
+               case '\\':
+               case '"':
+                       ret = lttng_metadata_printf(session, "%c", '\\');
+                       if (ret) {
+                               goto error;
+                       }
+                       /* We still print the current char */
+                       /* Fallthrough */
+               default:
+                       ret = lttng_metadata_printf(session, "%c", cur);
+                       break;
+               }
+
+               if (ret) {
+                       goto error;
+               }
+
+               cur = string[++i];
+       }
+error:
+       return ret;
+}
+
 /* Called with session registry mutex held. */
 static
 int ust_metadata_enum_statedump(struct ust_registry_session *session,
@@ -213,6 +269,7 @@ int ust_metadata_enum_statedump(struct ust_registry_session *session,
        size_t nr_entries;
        int ret = 0;
        size_t i;
+       char identifier[LTTNG_UST_SYM_NAME_LEN];
 
        rcu_read_lock();
        reg_enum = ust_registry_lookup_enum_by_id(session, enum_name, enum_id);
@@ -243,13 +300,18 @@ int ust_metadata_enum_statedump(struct ust_registry_session *session,
        if (ret) {
                goto end;
        }
+       nesting++;
        /* Dump all entries */
        for (i = 0; i < nr_entries; i++) {
                const struct ustctl_enum_entry *entry = &entries[i];
                int j, len;
 
+               ret = print_tabs(session, nesting);
+               if (ret) {
+                       goto end;
+               }
                ret = lttng_metadata_printf(session,
-                               "                       \"");
+                               "\"");
                if (ret) {
                        goto end;
                }
@@ -276,32 +338,68 @@ int ust_metadata_enum_statedump(struct ust_registry_session *session,
                                goto end;
                        }
                }
-               ret = lttng_metadata_printf(session,
-                               "\" = ");
+               ret = lttng_metadata_printf(session, "\"");
                if (ret) {
                        goto end;
                }
-               if (entry->start == entry->end) {
-                       ret = lttng_metadata_printf(session,
-                                       "%d,\n",
-                                       entry->start);
+
+               if (entry->u.extra.options &
+                               USTCTL_UST_ENUM_ENTRY_OPTION_IS_AUTO) {
+                       ret = lttng_metadata_printf(session, ",\n");
+                       if (ret) {
+                               goto end;
+                       }
                } else {
                        ret = lttng_metadata_printf(session,
-                                       "%d ... %d,\n",
-                                       entry->start, entry->end);
-               }
-               if (ret) {
-                       goto end;
+                                       " = ");
+                       if (ret) {
+                               goto end;
+                       }
+
+                       if (entry->start.signedness) {
+                               ret = lttng_metadata_printf(session,
+                                       "%lld", (long long) entry->start.value);
+                       } else {
+                               ret = lttng_metadata_printf(session,
+                                       "%llu", entry->start.value);
+                       }
+                       if (ret) {
+                               goto end;
+                       }
+
+                       if (entry->start.signedness == entry->end.signedness &&
+                                       entry->start.value ==
+                                               entry->end.value) {
+                               ret = lttng_metadata_printf(session, ",\n");
+                       } else {
+                               if (entry->end.signedness) {
+                                       ret = lttng_metadata_printf(session,
+                                               " ... %lld,\n",
+                                               (long long) entry->end.value);
+                               } else {
+                                       ret = lttng_metadata_printf(session,
+                                               " ... %llu,\n",
+                                               entry->end.value);
+                               }
+                       }
+                       if (ret) {
+                               goto end;
+                       }
                }
        }
-       ret = lttng_metadata_printf(session, "          } _%s;\n",
-                       field_name);
+       nesting--;
+       sanitize_ctf_identifier(identifier, field_name);
+       ret = print_tabs(session, nesting);
+       if (ret) {
+               goto end;
+       }
+       ret = lttng_metadata_printf(session, "} _%s;\n",
+                       identifier);
 end:
        (*iter_field)++;
        return ret;
 }
 
-
 static
 int _lttng_variant_statedump(struct ust_registry_session *session,
                const struct ustctl_field *fields, size_t nr_fields,
@@ -310,6 +408,7 @@ int _lttng_variant_statedump(struct ust_registry_session *session,
        const struct ustctl_field *variant = &fields[*iter_field];
        uint32_t nr_choices, i;
        int ret;
+       char identifier[LTTNG_UST_SYM_NAME_LEN];
 
        if (variant->type.atype != ustctl_atype_variant) {
                ret = -EINVAL;
@@ -317,9 +416,14 @@ int _lttng_variant_statedump(struct ust_registry_session *session,
        }
        nr_choices = variant->type.u.variant.nr_choices;
        (*iter_field)++;
+       sanitize_ctf_identifier(identifier, variant->type.u.variant.tag_name);
+       ret = print_tabs(session, nesting);
+       if (ret) {
+               goto end;
+       }
        ret = lttng_metadata_printf(session,
-                       "               variant <_%s> {\n",
-                       variant->type.u.variant.tag_name);
+                       "variant <_%s> {\n",
+                       identifier);
        if (ret) {
                goto end;
        }
@@ -332,10 +436,18 @@ int _lttng_variant_statedump(struct ust_registry_session *session,
                ret = _lttng_field_statedump(session,
                                fields, nr_fields,
                                iter_field, nesting + 1);
+               if (ret) {
+                       goto end;
+               }
+       }
+       sanitize_ctf_identifier(identifier, variant->name);
+       ret = print_tabs(session, nesting);
+       if (ret) {
+               goto end;
        }
        ret = lttng_metadata_printf(session,
-                       "               } _%s;\n",
-                       variant->name);
+                       "} _%s;\n",
+                       identifier);
        if (ret) {
                goto end;
        }
@@ -685,6 +797,7 @@ int _lttng_stream_packet_context_declare(struct ust_registry_session *session)
                "       uint64_clock_monotonic_t timestamp_end;\n"
                "       uint64_t content_size;\n"
                "       uint64_t packet_size;\n"
+               "       uint64_t packet_seq_num;\n"
                "       unsigned long events_discarded;\n"
                "       uint32_t cpu_id;\n"
                "};\n\n"
@@ -747,7 +860,7 @@ int measure_single_clock_offset(struct offset_sample *sample)
        int ret;
 
        monotonic[0] = trace_clock_read64();
-       ret = clock_gettime(CLOCK_REALTIME, &rts);
+       ret = lttng_clock_gettime(CLOCK_REALTIME, &rts);
        if (ret < 0) {
                return ret;
        }
@@ -795,6 +908,98 @@ int64_t measure_clock_offset(void)
        return offset_best_sample.offset;
 }
 
+static
+int print_metadata_session_information(struct ust_registry_session *registry)
+{
+       int ret;
+       struct ltt_session *session = NULL;
+       char creation_datetime[ISO8601_STR_LEN];
+
+       rcu_read_lock();
+       session = session_find_by_id(registry->tracing_id);
+       if (!session) {
+               ret = -1;
+               goto error;
+       }
+
+       /* Print the trace name */
+       ret = lttng_metadata_printf(registry, " trace_name = \"");
+       if (ret) {
+               goto error;
+       }
+
+       /*
+        * This is necessary since the creation time is present in the session
+        * name when it is generated.
+        */
+       if (session->has_auto_generated_name) {
+               ret = print_escaped_ctf_string(registry, DEFAULT_SESSION_NAME);
+       } else {
+               ret = print_escaped_ctf_string(registry, session->name);
+       }
+       if (ret) {
+               goto error;
+       }
+
+       ret = lttng_metadata_printf(registry, "\";\n");
+       if (ret) {
+               goto error;
+       }
+
+       /* Prepare creation time */
+       ret = time_to_iso8601_str(session->creation_time, creation_datetime,
+                       sizeof(creation_datetime));
+       if (ret) {
+               goto error;
+       }
+
+       /* Output the reste of the information */
+       ret = lttng_metadata_printf(registry,
+                       "       trace_creation_datetime = \"%s\";\n"
+                       "       hostname = \"%s\";\n",
+                       creation_datetime, session->hostname);
+       if (ret) {
+               goto error;
+       }
+
+error:
+       if (session) {
+               session_put(session);
+       }
+       rcu_read_unlock();
+       return ret;
+}
+
+static
+int print_metadata_app_information(struct ust_registry_session *registry,
+               struct ust_app *app)
+{
+       int ret;
+       char datetime[ISO8601_STR_LEN];
+
+       if (!app) {
+               ret = 0;
+               goto end;
+       }
+
+       ret = time_to_iso8601_str(
+                       app->registration_time, datetime, sizeof(datetime));
+       if (ret) {
+               goto end;
+       }
+
+       ret = lttng_metadata_printf(registry,
+                       "       tracer_patchlevel = %u;\n"
+                       "       vpid = %d;\n"
+                       "       procname = \"%s\";\n"
+                       "       vpid_datetime = \"%s\";\n",
+                       app->version.patchlevel, (int) app->pid, app->name,
+                       datetime);
+
+end:
+       return ret;
+}
+
 /*
  * Should be called with session registry mutex held.
  */
@@ -803,22 +1008,13 @@ int ust_metadata_session_statedump(struct ust_registry_session *session,
                uint32_t major,
                uint32_t minor)
 {
-       unsigned char *uuid_c;
-       char uuid_s[UUID_STR_LEN],
-               clock_uuid_s[UUID_STR_LEN];
+       char uuid_s[LTTNG_UUID_STR_LEN],
+               clock_uuid_s[LTTNG_UUID_STR_LEN];
        int ret = 0;
-       char hostname[HOST_NAME_MAX];
 
        assert(session);
 
-       uuid_c = session->uuid;
-
-       snprintf(uuid_s, sizeof(uuid_s),
-               "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
-               uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
-               uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
-               uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
-               uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
+       lttng_uuid_to_str(session->uuid, uuid_s);
 
        /* For crash ABI */
        ret = lttng_metadata_printf(session,
@@ -847,6 +1043,7 @@ int ust_metadata_session_statedump(struct ust_registry_session *session,
                "               uint32_t magic;\n"
                "               uint8_t  uuid[16];\n"
                "               uint32_t stream_id;\n"
+               "               uint64_t stream_instance_id;\n"
                "       };\n"
                "};\n\n",
                session->uint8_t_alignment,
@@ -863,40 +1060,36 @@ int ust_metadata_session_statedump(struct ust_registry_session *session,
        if (ret)
                goto end;
 
-       /* ignore error, just use empty string if error. */
-       hostname[0] = '\0';
-       ret = gethostname(hostname, sizeof(hostname));
-       if (ret && errno == ENAMETOOLONG)
-               hostname[HOST_NAME_MAX - 1] = '\0';
        ret = lttng_metadata_printf(session,
                "env {\n"
-               "       hostname = \"%s\";\n"
                "       domain = \"ust\";\n"
                "       tracer_name = \"lttng-ust\";\n"
                "       tracer_major = %u;\n"
-               "       tracer_minor = %u;\n",
-               hostname,
+               "       tracer_minor = %u;\n"
+               "       tracer_buffering_scheme = \"%s\";\n"
+               "       tracer_buffering_id = %u;\n"
+               "       architecture_bit_width = %u;\n",
                major,
-               minor
-               );
-       if (ret)
+               minor,
+               app ? "pid" : "uid",
+               app ? (int) app->pid : (int) session->tracing_uid,
+               session->bits_per_long);
+       if (ret) {
                goto end;
+       }
+
+       ret = print_metadata_session_information(session);
+       if (ret) {
+               goto end;
+       }
 
        /*
         * If per-application registry, we can output extra information
         * about the application.
         */
-       if (app) {
-               ret = lttng_metadata_printf(session,
-                       "       tracer_patchlevel = %u;\n"
-                       "       vpid = %d;\n"
-                       "       procname = \"%s\";\n",
-                       app->version.patchlevel,
-                       (int) app->pid,
-                       app->name
-                       );
-               if (ret)
-                       goto end;
+       ret = print_metadata_app_information(session, app);
+       if (ret) {
+               goto end;
        }
 
        ret = lttng_metadata_printf(session,
This page took 0.03118 seconds and 5 git commands to generate.