+static
+int ctf_clock_declaration_visit(FILE *fd, int depth, struct ctf_node *node,
+ struct ctf_clock *clock, 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 (!strcmp(left, "name")) {
+ char *right;
+
+ if (CTF_CLOCK_FIELD_IS_SET(clock, name)) {
+ fprintf(fd, "[error] %s: name already declared in clock 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 clock name\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ clock->name = g_quark_from_string(right);
+ g_free(right);
+ CTF_CLOCK_SET_FIELD(clock, name);
+ } else if (!strcmp(left, "uuid")) {
+ char *right;
+
+ if (clock->uuid) {
+ fprintf(fd, "[error] %s: uuid already declared in clock 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 clock uuid\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ clock->uuid = g_quark_from_string(right);
+ g_free(right);
+ } else if (!strcmp(left, "description")) {
+ char *right;
+
+ if (clock->description) {
+ fprintf(fd, "[warning] %s: duplicated clock description\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 clock description\n", __func__);
+ goto error; /* ret is 0, so not an actual error, just warn. */
+ }
+ clock->description = right;
+ } else if (!strcmp(left, "freq")) {
+ if (CTF_CLOCK_FIELD_IS_SET(clock, freq)) {
+ fprintf(fd, "[error] %s: freq already declared in clock declaration\n", __func__);
+ ret = -EPERM;
+ goto error;
+ }
+ ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->freq);
+ if (ret) {
+ fprintf(fd, "[error] %s: unexpected unary expression for clock freq\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ CTF_CLOCK_SET_FIELD(clock, freq);
+ } else if (!strcmp(left, "precision")) {
+ if (clock->precision) {
+ fprintf(fd, "[error] %s: precision already declared in clock declaration\n", __func__);
+ ret = -EPERM;
+ goto error;
+ }
+ ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->precision);
+ if (ret) {
+ fprintf(fd, "[error] %s: unexpected unary expression for clock precision\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ } else if (!strcmp(left, "offset_s")) {
+ if (clock->offset_s) {
+ fprintf(fd, "[error] %s: offset_s already declared in clock declaration\n", __func__);
+ ret = -EPERM;
+ goto error;
+ }
+ ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset_s);
+ if (ret) {
+ fprintf(fd, "[error] %s: unexpected unary expression for clock offset_s\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ } else if (!strcmp(left, "offset")) {
+ if (clock->offset) {
+ fprintf(fd, "[error] %s: offset already declared in clock declaration\n", __func__);
+ ret = -EPERM;
+ goto error;
+ }
+ ret = get_unary_unsigned(&node->u.ctf_expression.right, &clock->offset);
+ if (ret) {
+ fprintf(fd, "[error] %s: unexpected unary expression for clock offset\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ } else if (!strcmp(left, "absolute")) {
+ struct ctf_node *right;
+
+ right = _bt_list_first_entry(&node->u.ctf_expression.right, struct ctf_node, siblings);
+ ret = get_boolean(fd, depth, right);
+ if (ret < 0) {
+ fprintf(fd, "[error] %s: unexpected \"absolute\" right member\n", __func__);
+ ret = -EINVAL;
+ goto error;
+ }
+ clock->absolute = ret;
+ } else {
+ fprintf(fd, "[warning] %s: attribute \"%s\" is unknown in clock declaration.\n", __func__, left);
+ }
+
+error:
+ g_free(left);
+ break;
+ }
+ default:
+ return -EPERM;
+ /* TODO: declaration specifier should be added. */
+ }
+
+ return ret;
+}
+
+static
+int ctf_clock_visit(FILE *fd, int depth, struct ctf_node *node, struct ctf_trace *trace)
+{
+ int ret = 0;
+ struct ctf_node *iter;
+ struct ctf_clock *clock;
+
+ clock = g_new0(struct ctf_clock, 1);
+ /* Default clock frequency is set to 1000000000 */
+ clock->freq = 1000000000ULL;
+ bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) {
+ ret = ctf_clock_declaration_visit(fd, depth + 1, iter, clock, trace);
+ if (ret)
+ goto error;
+ }
+ 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;
+ }
+ 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 (!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 (!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);
+ 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);
+ assert(ret == 0);
+ 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);
+ assert(ret == 0);
+ 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;
+}
+