+static
+void destroy_ctx_type(struct ctx_type *type)
+{
+ if (!type) {
+ return;
+ }
+ if (type->opt) {
+ free(type->opt->symbol);
+ }
+ free(type->opt);
+ free(type);
+}
+
+static
+struct ctx_type *create_ctx_type(void)
+{
+ struct ctx_type *type = zmalloc(sizeof(*type));
+
+ if (!type) {
+ PERROR("malloc ctx_type");
+ goto end;
+ }
+
+ type->opt = zmalloc(sizeof(*type->opt));
+ if (!type->opt) {
+ PERROR("malloc ctx_type options");
+ destroy_ctx_type(type);
+ type = NULL;
+ goto end;
+ }
+end:
+ return type;
+}
+
+static
+int find_ctx_type_perf_raw(const char *ctx, struct ctx_type *type)
+{
+ int ret;
+ int field_pos = 0;
+ char *tmp_list, *cur_list;
+
+ cur_list = tmp_list = strdup(ctx);
+ if (!tmp_list) {
+ PERROR("strdup temp list");
+ ret = -ENOMEM;
+ goto end;
+ }
+
+ /* Looking for "perf:[cpu|thread]:raw:<mask>:<name>". */
+ for (;;) {
+ char *next;
+
+ next = strtok(cur_list, ":");
+ if (!next) {
+ break;
+ }
+ cur_list = NULL;
+ switch (field_pos) {
+ case 0:
+ if (strncmp(next, "perf", 4) != 0) {
+ ret = -1;
+ goto end;
+ }
+ break;
+ case 1:
+ if (strncmp(next, "cpu", 3) == 0) {
+ type->opt->ctx_type = CONTEXT_PERF_CPU_COUNTER;
+ } else if (strncmp(next, "thread", 4) == 0) {
+ type->opt->ctx_type = CONTEXT_PERF_THREAD_COUNTER;
+ } else {
+ ret = -1;
+ goto end;
+ }
+ break;
+ case 2:
+ if (strncmp(next, "raw", 3) != 0) {
+ ret = -1;
+ goto end;
+ }
+ break;
+ case 3:
+ {
+ char *endptr;
+
+ if (strlen(next) < 2 || next[0] != 'r') {
+ ERR("Wrong perf raw mask format: expected rNNN");
+ ret = -1;
+ goto end;
+ }
+ errno = 0;
+ type->opt->u.perf.config = strtoll(next + 1, &endptr, 16);
+ if (errno != 0 || !endptr || *endptr) {
+ ERR("Wrong perf raw mask format: expected rNNN");
+ ret = -1;
+ goto end;
+ }
+ break;
+ }
+ case 4:
+ /* name */
+ break;
+ case 5:
+ ERR("Too many ':' in perf raw format");
+ ret = -1;
+ goto end;
+ };
+ field_pos++;
+ }
+
+ if (field_pos < 5) {
+ ERR("Invalid perf counter specifier, expected a specifier of "
+ "the form perf:cpu:raw:rNNN:<name> or "
+ "perf:thread:raw:rNNN:<name>");
+ ret = -1;
+ goto end;
+ }
+
+ ret = 0;
+ goto end;
+
+end:
+ free(tmp_list);
+ return ret;
+}
+
+static
+struct ctx_type *get_context_type(const char *ctx)
+{
+ int opt_index, ret;
+ struct ctx_type *type = NULL;
+ const char app_ctx_prefix[] = "$app.";
+ char *provider_name = NULL, *ctx_name = NULL;
+ size_t i, len, colon_pos = 0, provider_name_len, ctx_name_len;
+
+ if (!ctx) {
+ goto not_found;
+ }
+
+ type = create_ctx_type();
+ if (!type) {
+ goto not_found;
+ }
+
+ /* Check if ctx matches a known static context. */
+ opt_index = find_ctx_type_idx(ctx);
+ if (opt_index >= 0) {
+ *type->opt = ctx_opts[opt_index];
+ type->opt->symbol = strdup(ctx_opts[opt_index].symbol);
+ goto found;
+ }
+
+ /* Check if ctx is a raw perf context. */
+ ret = find_ctx_type_perf_raw(ctx, type);
+ if (ret == 0) {
+ type->opt->u.perf.type = PERF_TYPE_RAW;
+ type->opt->symbol = strdup(ctx);
+ if (!type->opt->symbol) {
+ PERROR("Copy perf field name");
+ goto not_found;
+ }
+ goto found;
+ }
+
+ /*
+ * No match found against static contexts; check if it is an app
+ * context.
+ */
+ len = strlen(ctx);
+ if (len <= sizeof(app_ctx_prefix) - 1) {
+ goto not_found;
+ }
+
+ /* String starts with $app. */
+ if (strncmp(ctx, app_ctx_prefix, sizeof(app_ctx_prefix) - 1)) {
+ goto not_found;
+ }
+
+ /* Validate that the ':' separator is present. */
+ for (i = sizeof(app_ctx_prefix); i < len; i++) {
+ const char c = ctx[i];
+
+ if (c == ':') {
+ colon_pos = i;
+ break;
+ }
+ }
+
+ /*
+ * No colon found or no ctx name ("$app.provider:") or no provider name
+ * given ("$app.:..."), which is invalid.
+ */
+ if (!colon_pos || colon_pos == len ||
+ colon_pos == sizeof(app_ctx_prefix)) {
+ ERR("Invalid application context provided: no provider or context name provided.");
+ goto not_found;
+ }
+
+ provider_name_len = colon_pos - sizeof(app_ctx_prefix) + 2;
+ provider_name = zmalloc(provider_name_len);
+ if (!provider_name) {
+ PERROR("malloc provider_name");
+ goto not_found;
+ }
+ strncpy(provider_name, ctx + sizeof(app_ctx_prefix) - 1,
+ provider_name_len - 1);
+ type->opt->u.app_ctx.provider_name = provider_name;
+
+ ctx_name_len = len - colon_pos;
+ ctx_name = zmalloc(ctx_name_len);
+ if (!ctx_name) {
+ PERROR("malloc ctx_name");
+ goto not_found;
+ }
+ strncpy(ctx_name, ctx + colon_pos + 1, ctx_name_len - 1);
+ type->opt->u.app_ctx.ctx_name = ctx_name;
+ type->opt->ctx_type = CONTEXT_APP_CONTEXT;
+ type->opt->symbol = strdup(ctx);
+found:
+ return type;
+not_found:
+ free(provider_name);
+ free(ctx_name);
+ destroy_ctx_type(type);
+ return NULL;
+}
+