+ clock->absolute = 1;
+ }
+ if (!CTF_CLOCK_FIELD_IS_SET(clock, name)) {
+ ret = -EPERM;
+ fprintf(fd, "[error] %s: missing name field in clock declaration\n", __func__);
+ goto error;
+ }
+ if (g_hash_table_size(trace->clocks) > 0) {
+ fprintf(fd, "[error] Only CTF traces with a single clock description are supported by this babeltrace version.\n");
+ ret = -EINVAL;
+ goto error;
+ }
+ trace->single_clock = clock;
+ g_hash_table_insert(trace->clocks, (gpointer) (unsigned long) clock->name, clock);
+ return 0;
+
+error:
+ g_free(clock->description);
+ g_free(clock);
+ return ret;
+}
+
+static
+void ctf_clock_default(FILE *fd, int depth, struct ctf_trace *trace)
+{
+ struct ctf_clock *clock;
+
+ clock = g_new0(struct ctf_clock, 1);
+ clock->name = g_quark_from_string("monotonic");
+ clock->uuid = 0;
+ clock->description = g_strdup("Default clock");
+ /* Default clock frequency is set to 1000000000 */
+ clock->freq = 1000000000ULL;
+ if (opt_clock_force_correlate) {
+ /*
+ * User requested to forcibly correlate the clock
+ * sources, even if we have no correlatation
+ * information.
+ */
+ if (!clock->absolute) {
+ fprintf(fd, "[warning] Forcibly correlating trace clock sources (--clock-force-correlate).\n");
+ }
+ clock->absolute = 1;
+ } else {
+ clock->absolute = 0; /* Not an absolute reference across traces */
+ }
+
+ trace->single_clock = clock;
+ g_hash_table_insert(trace->clocks, (gpointer) (unsigned long) clock->name, clock);
+}
+
+static
+void clock_free(gpointer data)
+{
+ struct ctf_clock *clock = data;
+
+ g_free(clock->description);
+ g_free(clock);
+}
+
+static
+int ctf_callsite_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
+ struct ctf_callsite *callsite, struct ctf_trace *trace)
+{
+ int ret = 0;
+
+ switch (node->type) {
+ case NODE_CTF_EXPRESSION:
+ {
+ char *left;
+
+ left = concatenate_unary_strings(&node->u.ctf_expression.left);
+ if (!left)
+ return -EINVAL;
+ if (!strcmp(left, "name")) {
+ char *right;
+
+ if (CTF_CALLSITE_FIELD_IS_SET(callsite, name)) {
+ fprintf(fd, "[error] %s: name already declared in callsite declaration\n", __func__);
+ ret = -EPERM;
+ goto error;
+ }
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[error] %s: unexpected unary expression for callsite name\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ callsite->name = g_quark_from_string(right);
+ g_free(right);
+ CTF_CALLSITE_SET_FIELD(callsite, name);
+ } else if (!strcmp(left, "func")) {
+ char *right;
+
+ if (CTF_CALLSITE_FIELD_IS_SET(callsite, func)) {
+ fprintf(fd, "[error] %s: func already declared in callsite declaration\n", __func__);
+ ret = -EPERM;
+ goto error;
+ }
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[error] %s: unexpected unary expression for callsite func\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ callsite->func = right;
+ CTF_CALLSITE_SET_FIELD(callsite, func);
+ } else if (!strcmp(left, "file")) {
+ char *right;
+
+ if (CTF_CALLSITE_FIELD_IS_SET(callsite, file)) {
+ fprintf(fd, "[error] %s: file already declared in callsite declaration\n", __func__);
+ ret = -EPERM;
+ goto error;
+ }
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[error] %s: unexpected unary expression for callsite file\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ callsite->file = right;
+ CTF_CALLSITE_SET_FIELD(callsite, file);
+ } else if (!strcmp(left, "line")) {
+ if (CTF_CALLSITE_FIELD_IS_SET(callsite, line)) {
+ fprintf(fd, "[error] %s: line already declared in callsite declaration\n", __func__);
+ ret = -EPERM;
+ goto error;
+ }
+ ret = get_unary_unsigned(&node->u.ctf_expression.right, &callsite->line);
+ if (ret) {
+ fprintf(fd, "[error] %s: unexpected unary expression for callsite line\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ CTF_CALLSITE_SET_FIELD(callsite, line);
+ } else if (!strcmp(left, "ip")) {
+ if (CTF_CALLSITE_FIELD_IS_SET(callsite, ip)) {
+ fprintf(fd, "[error] %s: ip already declared in callsite declaration\n", __func__);
+ ret = -EPERM;
+ goto error;
+ }
+ ret = get_unary_unsigned(&node->u.ctf_expression.right, &callsite->ip);
+ if (ret) {
+ fprintf(fd, "[error] %s: unexpected unary expression for callsite ip\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ CTF_CALLSITE_SET_FIELD(callsite, ip);
+ } else {
+ fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in callsite declaration.\n", __func__, left);
+ }
+
+error:
+ g_free(left);
+ break;
+ }
+ default:
+ return -EPERM;
+ /* TODO: declaration specifier should be added. */
+ }
+
+ return ret;
+}
+
+static
+int ctf_callsite_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
+{
+ int ret = 0;
+ struct ctf_node *iter;
+ struct ctf_callsite *callsite;
+ struct ctf_callsite_dups *cs_dups;
+
+ callsite = g_new0(struct ctf_callsite, 1);
+ bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) {
+ ret = ctf_callsite_declaration_visit(fd, depth + 1, iter, callsite, trace);
+ if (ret)
+ goto error;
+ }
+ if (!CTF_CALLSITE_FIELD_IS_SET(callsite, name)) {
+ ret = -EPERM;
+ fprintf(fd, "[error] %s: missing name field in callsite declaration\n", __func__);
+ goto error;
+ }
+ if (!CTF_CALLSITE_FIELD_IS_SET(callsite, func)) {
+ ret = -EPERM;
+ fprintf(fd, "[error] %s: missing func field in callsite declaration\n", __func__);
+ goto error;
+ }
+ if (!CTF_CALLSITE_FIELD_IS_SET(callsite, file)) {
+ ret = -EPERM;
+ fprintf(fd, "[error] %s: missing file field in callsite declaration\n", __func__);
+ goto error;
+ }
+ if (!CTF_CALLSITE_FIELD_IS_SET(callsite, line)) {
+ ret = -EPERM;
+ fprintf(fd, "[error] %s: missing line field in callsite declaration\n", __func__);
+ goto error;
+ }
+
+ cs_dups = g_hash_table_lookup(trace->callsites,
+ (gpointer) (unsigned long) callsite->name);
+ if (!cs_dups) {
+ cs_dups = g_new0(struct ctf_callsite_dups, 1);
+ BT_INIT_LIST_HEAD(&cs_dups->head);
+ g_hash_table_insert(trace->callsites,
+ (gpointer) (unsigned long) callsite->name, cs_dups);
+ }
+ bt_list_add_tail(&callsite->node, &cs_dups->head);
+ return 0;
+
+error:
+ g_free(callsite->func);
+ g_free(callsite->file);
+ g_free(callsite);
+ return ret;
+}
+
+static
+void callsite_free(gpointer data)
+{
+ struct ctf_callsite_dups *cs_dups = data;
+ struct ctf_callsite *callsite, *cs_n;
+
+ bt_list_for_each_entry_safe(callsite, cs_n, &cs_dups->head, node) {
+ g_free(callsite->func);
+ g_free(callsite->file);
+ g_free(callsite);
+ }
+ g_free(cs_dups);
+}
+
+static
+int ctf_env_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
+ struct ctf_trace *trace)
+{
+ int ret = 0;
+ struct ctf_tracer_env *env = &trace->env;
+
+ switch (node->type) {
+ case NODE_CTF_EXPRESSION:
+ {
+ char *left;
+
+ left = concatenate_unary_strings(&node->u.ctf_expression.left);
+ if (!left)
+ return -EINVAL;
+ if (!strcmp(left, "vpid")) {
+ uint64_t v;
+
+ if (env->vpid != -1) {
+ fprintf(fd, "[error] %s: vpid already declared in env declaration\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ ret = get_unary_unsigned(&node->u.ctf_expression.right, &v);
+ if (ret) {
+ fprintf(fd, "[error] %s: unexpected unary expression for env vpid\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ env->vpid = (int) v;
+ printf_verbose("env.vpid = %d\n", env->vpid);
+ } else if (!strcmp(left, "procname")) {
+ char *right;
+
+ if (env->procname[0]) {
+ fprintf(fd, "[warning] %s: duplicated env procname\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[warning] %s: unexpected unary expression for env procname\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ strncpy(env->procname, right, TRACER_ENV_LEN);
+ env->procname[TRACER_ENV_LEN - 1] = '\0';
+ printf_verbose("env.procname = \"%s\"\n", env->procname);
+ g_free(right);
+ } else if (!strcmp(left, "hostname")) {
+ char *right;
+
+ if (env->hostname[0]) {
+ fprintf(fd, "[warning] %s: duplicated env hostname\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[warning] %s: unexpected unary expression for env hostname\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ strncpy(env->hostname, right, TRACER_ENV_LEN);
+ env->hostname[TRACER_ENV_LEN - 1] = '\0';
+ printf_verbose("env.hostname = \"%s\"\n", env->hostname);
+ g_free(right);
+ } else if (!strcmp(left, "domain")) {
+ char *right;
+
+ if (env->domain[0]) {
+ fprintf(fd, "[warning] %s: duplicated env domain\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[warning] %s: unexpected unary expression for env domain\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ strncpy(env->domain, right, TRACER_ENV_LEN);
+ env->domain[TRACER_ENV_LEN - 1] = '\0';
+ printf_verbose("env.domain = \"%s\"\n", env->domain);
+ g_free(right);
+ } else if (!strcmp(left, "sysname")) {
+ char *right;
+
+ if (env->sysname[0]) {
+ fprintf(fd, "[warning] %s: duplicated env sysname\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[warning] %s: unexpected unary expression for env sysname\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ strncpy(env->sysname, right, TRACER_ENV_LEN);
+ env->sysname[TRACER_ENV_LEN - 1] = '\0';
+ printf_verbose("env.sysname = \"%s\"\n", env->sysname);
+ g_free(right);
+ } else if (!strcmp(left, "kernel_release")) {
+ char *right;
+
+ if (env->release[0]) {
+ fprintf(fd, "[warning] %s: duplicated env release\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[warning] %s: unexpected unary expression for env release\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ strncpy(env->release, right, TRACER_ENV_LEN);
+ env->release[TRACER_ENV_LEN - 1] = '\0';
+ printf_verbose("env.release = \"%s\"\n", env->release);
+ g_free(right);
+ } else if (!strcmp(left, "kernel_version")) {
+ char *right;
+
+ if (env->version[0]) {
+ fprintf(fd, "[warning] %s: duplicated env version\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[warning] %s: unexpected unary expression for env version\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ strncpy(env->version, right, TRACER_ENV_LEN);
+ env->version[TRACER_ENV_LEN - 1] = '\0';
+ printf_verbose("env.version = \"%s\"\n", env->version);
+ g_free(right);
+ } else {
+ if (is_unary_string(&node->u.ctf_expression.right)) {
+ char *right;
+
+ right = concatenate_unary_strings(&node->u.ctf_expression.right);
+ if (!right) {
+ fprintf(fd, "[warning] %s: unexpected unary expression for env\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ printf_verbose("env.%s = \"%s\"\n", left, right);
+ g_free(right);
+ } else if (is_unary_unsigned(&node->u.ctf_expression.right)) {
+ uint64_t v;
+ int ret;
+
+ ret = get_unary_unsigned(&node->u.ctf_expression.right, &v);
+ if (ret)
+ goto error;
+ printf_verbose("env.%s = %" PRIu64 "\n", left, v);
+ } else if (is_unary_signed(&node->u.ctf_expression.right)) {
+ int64_t v;
+ int ret;
+
+ ret = get_unary_signed(&node->u.ctf_expression.right, &v);
+ if (ret)
+ goto error;
+ printf_verbose("env.%s = %" PRId64 "\n", left, v);
+ } else {
+ printf_verbose("%s: attribute \"%s\" has unknown type.\n", __func__, left);
+ }
+ }
+
+error:
+ g_free(left);
+ break;
+ }
+ default:
+ return -EPERM;
+ }
+
+ return ret;
+}
+
+static
+int ctf_env_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
+{
+ int ret = 0;
+ struct ctf_node *iter;
+
+ trace->env.vpid = -1;
+ trace->env.procname[0] = '\0';
+ trace->env.hostname[0] = '\0';
+ trace->env.domain[0] = '\0';
+ trace->env.sysname[0] = '\0';
+ trace->env.release[0] = '\0';
+ trace->env.version[0] = '\0';
+ bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) {
+ ret = ctf_env_declaration_visit(fd, depth + 1, iter, trace);
+ if (ret)
+ goto error;
+ }
+error:
+ return 0;
+}
+
+static
+int ctf_root_declaration_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
+{
+ int ret = 0;
+
+ switch (node->type) {
+ case NODE_TYPEDEF:
+ ret = ctf_typedef_visit(fd, depth + 1,
+ trace->root_declaration_scope,
+ node->u._typedef.type_specifier_list,
+ &node->u._typedef.type_declarators,
+ trace);
+ if (ret)
+ return ret;
+ break;
+ case NODE_TYPEALIAS:
+ ret = ctf_typealias_visit(fd, depth + 1,
+ trace->root_declaration_scope,
+ node->u.typealias.target, node->u.typealias.alias,
+ trace);
+ if (ret)
+ return ret;
+ break;
+ case NODE_TYPE_SPECIFIER_LIST:
+ {
+ struct bt_declaration *declaration;
+
+ /*
+ * Just add the type specifier to the root scope
+ * declaration scope. Release local reference.
+ */
+ declaration = ctf_type_specifier_list_visit(fd, depth + 1,
+ node, trace->root_declaration_scope, trace);
+ if (!declaration)
+ return -ENOMEM;
+ bt_declaration_unref(declaration);
+ break;
+ }
+ default:
+ return -EPERM;
+ }
+
+ return 0;
+}
+
+int ctf_visitor_construct_metadata(FILE *fd, int depth, struct ctf_node *node,
+ struct ctf_trace *trace, int byte_order)
+{
+ int ret = 0;
+ struct ctf_node *iter;
+ int env_clock_done = 0;
+
+ printf_verbose("CTF visitor: metadata construction...\n");
+ trace->byte_order = byte_order;
+ trace->clocks = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+ NULL, clock_free);
+ trace->callsites = g_hash_table_new_full(g_direct_hash, g_direct_equal,
+ NULL, callsite_free);
+
+retry:
+ trace->root_declaration_scope = bt_new_declaration_scope(NULL);
+
+ switch (node->type) {
+ case NODE_ROOT:
+ if (!env_clock_done) {
+ /*
+ * declarations need to query clock hash table,
+ * so clock need to be treated first.
+ */
+ if (bt_list_empty(&node->u.root.clock)) {
+ ctf_clock_default(fd, depth + 1, trace);
+ } else {
+ bt_list_for_each_entry(iter, &node->u.root.clock, siblings) {
+ ret = ctf_clock_visit(fd, depth + 1, iter,
+ trace);
+ if (ret) {
+ fprintf(fd, "[error] %s: clock declaration error\n", __func__);
+ goto error;
+ }
+ }
+ }
+ env_clock_done = 1;
+ }
+ bt_list_for_each_entry(iter, &node->u.root.declaration_list,
+ siblings) {
+ ret = ctf_root_declaration_visit(fd, depth + 1, iter, trace);
+ if (ret) {
+ fprintf(fd, "[error] %s: root declaration error\n", __func__);
+ goto error;
+ }
+ }
+ bt_list_for_each_entry(iter, &node->u.root.trace, siblings) {
+ ret = ctf_trace_visit(fd, depth + 1, iter, trace);
+ if (ret == -EINTR) {
+ bt_free_declaration_scope(trace->root_declaration_scope);
+ /*
+ * Need to restart creation of type
+ * definitions, aliases and
+ * trace header declarations.
+ */
+ goto retry;
+ }
+ if (ret) {
+ fprintf(fd, "[error] %s: trace declaration error\n", __func__);
+ goto error;
+ }
+ }
+ bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) {
+ ret = ctf_callsite_visit(fd, depth + 1, iter,
+ trace);
+ if (ret) {
+ fprintf(fd, "[error] %s: callsite declaration error\n", __func__);
+ goto error;
+ }
+ }
+ if (!trace->streams) {
+ fprintf(fd, "[error] %s: missing trace declaration\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ bt_list_for_each_entry(iter, &node->u.root.env, siblings) {
+ ret = ctf_env_visit(fd, depth + 1, iter, trace);
+ if (ret) {
+ fprintf(fd, "[error] %s: env declaration error\n", __func__);
+ goto error;
+ }
+ }
+ bt_list_for_each_entry(iter, &node->u.root.stream, siblings) {