X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=lttng%2Fcommands%2Fadd_context.c;h=02220415d88d5c17e186dfef123be903115b6c93;hb=6caa2bcc3ec40374ae1c3b81c0179eb3f693de84;hp=cb0c08741c7113fd21734b7d065fca518e147b7f;hpb=b13d56d78992e27e684fec7aef527670fc238e9b;p=lttng-tools.git diff --git a/lttng/commands/add_context.c b/lttng/commands/add_context.c index cb0c08741..02220415d 100644 --- a/lttng/commands/add_context.c +++ b/lttng/commands/add_context.c @@ -40,13 +40,18 @@ static char *opt_session_name; static int *opt_kernel; static int opt_pid_all; static int opt_userspace; +static char *opt_cmd_name; static pid_t opt_pid; +static char *opt_type; enum { OPT_HELP = 1, OPT_TYPE, + OPT_USERSPACE, }; +static struct lttng_handle *handle; + /* * Taken from the LTTng ABI */ @@ -136,10 +141,10 @@ static struct poptOption long_options[] = { {"channel", 'c', POPT_ARG_STRING, &opt_channel_name, 0, 0, 0}, {"event", 'e', POPT_ARG_STRING, &opt_event_name, 0, 0, 0}, {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0}, - {"userspace", 'u', POPT_ARG_VAL, &opt_userspace, 1, 0, 0}, + {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, 0, OPT_USERSPACE, 0, 0}, {"all", 0, POPT_ARG_VAL, &opt_pid_all, 1, 0, 0}, {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0}, - {"type", 't', POPT_ARG_STRING, 0, OPT_TYPE, 0, 0}, + {"type", 't', POPT_ARG_STRING, &opt_type, OPT_TYPE, 0, 0}, {0, 0, 0, 0, 0, 0, 0} }; @@ -164,8 +169,8 @@ static struct poptOption long_options[] = { .u.perf = { \ PERF_TYPE_HW_CACHE, \ (uint64_t) PERF_COUNT_HW_CACHE_##name \ - * (uint64_t) PERF_COUNT_HW_CACHE_OP_##op \ - * (uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result, \ + | ((uint64_t) PERF_COUNT_HW_CACHE_OP_##op << 8) \ + | ((uint64_t) PERF_COUNT_HW_CACHE_RESULT_##result << 16), \ }, \ } @@ -267,14 +272,14 @@ static void print_ctx_type(FILE *ofp) int indent_len = strlen(indent); int len, i = 0; - fprintf(ofp, indent); + fprintf(ofp, "%s", indent); len = indent_len; while (ctx_opts[i].symbol != NULL) { if (len > indent_len) { if (len + strlen(ctx_opts[i].symbol) + 2 >= PRINT_LINE_LEN) { fprintf(ofp, ",\n"); - fprintf(ofp, indent); + fprintf(ofp, "%s", indent); len = indent_len; } else { len += fprintf(ofp, ", "); @@ -313,7 +318,7 @@ static void usage(FILE *ofp) fprintf(ofp, "\n"); fprintf(ofp, "Example:\n"); fprintf(ofp, "This command will add the context information 'prio' and two perf\n" - "counters: hardware branch misses and cache-misses, to all events\n" + "counters: hardware branch misses and cache misses, to all events\n" "in the trace data output:\n"); fprintf(ofp, "# lttng add-context -k -t prio -t perf:branch-misses -t perf:cache-misses\n"); fprintf(ofp, "\n"); @@ -341,7 +346,7 @@ end: /* * Add context to channel or event. */ -static int add_context(void) +static int add_context(char *session_name) { int ret = CMD_SUCCESS; struct lttng_event_context context; @@ -349,19 +354,24 @@ static int add_context(void) struct ctx_type *type; char *ptr; - if (set_session_name(opt_session_name) < 0) { - ret = CMD_ERROR; + if (opt_kernel) { + dom.type = LTTNG_DOMAIN_KERNEL; + } + + handle = lttng_create_handle(session_name, &dom); + if (handle == NULL) { + ret = -1; goto error; } /* Iterate over all context type given */ cds_list_for_each_entry(type, &ctx_type_list.head, list) { + context.ctx = type->opt->ctx_type; if (context.ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER) { context.u.perf_counter.type = type->opt->u.perf.type; context.u.perf_counter.config = type->opt->u.perf.config; - strcpy(context.u.perf_counter.name, - type->opt->symbol); + strcpy(context.u.perf_counter.name, type->opt->symbol); /* Replace : and - by _ */ while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) { *ptr = '_'; @@ -371,14 +381,12 @@ static int add_context(void) } } if (opt_kernel) { - /* Create kernel domain */ - dom.type = LTTNG_DOMAIN_KERNEL; - DBG("Adding kernel context"); - ret = lttng_add_context(&dom, &context, opt_event_name, + ret = lttng_add_context(handle, &context, opt_event_name, opt_channel_name); if (ret < 0) { - goto error; + fprintf(stderr, "%s: ", type->opt->symbol); + continue; } else { MSG("Kernel context %s added", type->opt->symbol); } @@ -392,12 +400,14 @@ static int add_context(void) ret = CMD_NOT_IMPLEMENTED; goto error; } else { - ERR("Please specify a tracer (kernel or user-space)"); + ERR("Please specify a tracer (--kernel or --userspace)"); goto error; } } error: + lttng_destroy_handle(handle); + return ret; } @@ -407,9 +417,9 @@ error: int cmd_add_context(int argc, const char **argv) { int index, opt, ret = CMD_SUCCESS; - char *tmp; static poptContext pc; struct ctx_type *type, *tmptype; + char *session_name = NULL; if (argc < 2) { usage(stderr); @@ -426,32 +436,27 @@ int cmd_add_context(int argc, const char **argv) ret = CMD_SUCCESS; goto end; case OPT_TYPE: - /* Mandatory field */ - tmp = poptGetOptArg(pc); - if (tmp == NULL) { - usage(stderr); - ret = CMD_ERROR; - free(tmp); - goto end; - } type = malloc(sizeof(struct ctx_type)); if (type == NULL) { perror("malloc ctx_type"); ret = -1; goto end; } - index = find_ctx_type_idx(tmp); + index = find_ctx_type_idx(opt_type); if (index < 0) { - ERR("Unknown context type %s", tmp); + ERR("Unknown context type %s", opt_type); goto end; } type->opt = &ctx_opts[index]; if (type->opt->ctx_type == -1) { - ERR("Unknown context type %s", tmp); + ERR("Unknown context type %s", opt_type); } else { cds_list_add(&type->list, &ctx_type_list.head); } - free(tmp); + break; + case OPT_USERSPACE: + opt_userspace = 1; + opt_cmd_name = poptGetOptArg(pc); break; default: usage(stderr); @@ -460,7 +465,17 @@ int cmd_add_context(int argc, const char **argv) } } - ret = add_context(); + if (!opt_session_name) { + session_name = get_session_name(); + if (session_name == NULL) { + ret = -1; + goto end; + } + } else { + session_name = opt_session_name; + } + + ret = add_context(session_name); /* Cleanup allocated memory */ cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {