+static
+int print_tabs(struct ust_registry_session *session, size_t nesting)
+{
+ size_t i;
+
+ for (i = 0; i < nesting; i++) {
+ int ret;
+
+ ret = lttng_metadata_printf(session, " ");
+ if (ret) {
+ return ret;
+ }
+ }
+ 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,
+ const char *enum_name,
+ uint64_t enum_id,
+ const struct ustctl_integer_type *container_type,
+ const char *field_name, size_t *iter_field, size_t nesting)
+{
+ struct ust_registry_enum *reg_enum;
+ const struct ustctl_enum_entry *entries;
+ 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);
+ rcu_read_unlock();
+ /* reg_enum can still be used because session registry mutex is held. */
+ if (!reg_enum) {
+ ret = -ENOENT;
+ goto end;
+ }
+ entries = reg_enum->entries;
+ nr_entries = reg_enum->nr_entries;
+
+ ret = print_tabs(session, nesting);
+ if (ret) {
+ goto end;
+ }
+ ret = lttng_metadata_printf(session,
+ "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u; } {\n",
+ container_type->size,
+ container_type->alignment,
+ container_type->signedness,
+ (container_type->encoding == ustctl_encode_none)
+ ? "none"
+ : (container_type->encoding == ustctl_encode_UTF8)
+ ? "UTF8"
+ : "ASCII",
+ container_type->base);
+ 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;
+ }
+ len = strlen(entry->string);
+ /* Escape the character '"' */
+ for (j = 0; j < len; j++) {
+ char c = entry->string[j];
+
+ switch (c) {
+ case '"':
+ ret = lttng_metadata_printf(session,
+ "\\\"");
+ break;
+ case '\\':
+ ret = lttng_metadata_printf(session,
+ "\\\\");
+ break;
+ default:
+ ret = lttng_metadata_printf(session,
+ "%c", c);
+ break;
+ }
+ if (ret) {
+ goto end;
+ }
+ }
+ ret = lttng_metadata_printf(session, "\"");
+ if (ret) {
+ goto end;
+ }
+
+ 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,
+ " = ");
+ 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;
+ }
+ }
+ }
+ 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,
+ uint32_t nr_choices, const char *tag_name,
+ uint32_t alignment,
+ const struct ustctl_field *fields, size_t nr_fields,
+ size_t *iter_field, size_t nesting)
+{
+ const struct ustctl_field *variant = &fields[*iter_field];
+ uint32_t i;
+ int ret;
+ char identifier[LTTNG_UST_SYM_NAME_LEN];
+
+ if (variant->type.atype != ustctl_atype_variant) {
+ ret = -EINVAL;
+ goto end;
+ }
+ (*iter_field)++;
+ sanitize_ctf_identifier(identifier, tag_name);
+ if (alignment) {
+ ret = print_tabs(session, nesting);
+ if (ret) {
+ goto end;
+ }
+ ret = lttng_metadata_printf(session,
+ "struct { } align(%u) _%s_padding;\n",
+ alignment * CHAR_BIT,
+ variant->name);
+ if (ret) {
+ goto end;
+ }
+ }
+ ret = print_tabs(session, nesting);
+ if (ret) {
+ goto end;
+ }
+ ret = lttng_metadata_printf(session,
+ "variant <_%s> {\n",
+ identifier);
+ if (ret) {
+ goto end;
+ }
+
+ for (i = 0; i < nr_choices; i++) {
+ if (*iter_field >= nr_fields) {
+ ret = -EOVERFLOW;
+ goto end;
+ }
+ 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",
+ identifier);
+ if (ret) {
+ goto end;
+ }
+end:
+ return ret;
+}
+