X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng%2Fcommands%2Fadd_context.c;h=7aef4d50f7dd575e34c704f5b2a82896af8f5db1;hp=2f43dc7ff203e53a411d7178e649d80dd590d824;hb=1e2d9dfa3a424f2f668e97e91b20786f0edfaed6;hpb=62a7b8edf965b89d3ca57da120dcedfcf36dfe02 diff --git a/src/bin/lttng/commands/add_context.c b/src/bin/lttng/commands/add_context.c index 2f43dc7ff..7aef4d50f 100644 --- a/src/bin/lttng/commands/add_context.c +++ b/src/bin/lttng/commands/add_context.c @@ -41,6 +41,12 @@ static int opt_jul; static int opt_log4j; static char *opt_type; +#ifdef LTTNG_EMBED_HELP +static const char help_msg[] = +#include +; +#endif + enum { OPT_HELP = 1, OPT_TYPE, @@ -78,6 +84,8 @@ enum context_type { CONTEXT_PREEMPTIBLE = 17, CONTEXT_NEED_RESCHEDULE = 18, CONTEXT_MIGRATABLE = 19, + CONTEXT_CALLSTACK_KERNEL = 20, + CONTEXT_CALLSTACK_USER = 21, }; /* @@ -87,6 +95,7 @@ enum perf_type { PERF_TYPE_HARDWARE = 0, PERF_TYPE_SOFTWARE = 1, PERF_TYPE_HW_CACHE = 3, + PERF_TYPE_RAW = 4, }; enum perf_count_hard { @@ -233,6 +242,10 @@ const struct ctx_opts { { "preemptible", CONTEXT_PREEMPTIBLE }, { "need_reschedule", CONTEXT_NEED_RESCHEDULE }, { "migratable", CONTEXT_MIGRATABLE }, + { "callstack-kernel", CONTEXT_CALLSTACK_KERNEL }, +#if HAVE_MODULES_USERSPACE_CALLSTACK_CONTEXT + { "callstack-user", CONTEXT_CALLSTACK_USER }, +#endif /* Perf options */ @@ -475,20 +488,7 @@ struct ctx_type_list { .head = CDS_LIST_HEAD_INIT(ctx_type_list.head), }; -/* - * Pretty print context type. - */ -static void print_ctx_type(FILE *ofp) -{ - int i = 0; - while (ctx_opts[i].symbol != NULL) { - if (!ctx_opts[i].hide_help) { - fprintf(ofp, "%s\n", ctx_opts[i].symbol); - } - i++; - } -} /* * Find context numerical value from string. @@ -528,6 +528,195 @@ enum lttng_domain_type get_domain(void) } } +static +int mi_open(void) +{ + int ret; + + /* MI check */ + if (!lttng_opt_mi) { + ret = 0; + goto end; + } + + ret = fileno(stdout); + if (ret < 0) { + PERROR("Unable to retrieve fileno of stdout"); + ret = CMD_ERROR; + goto end; + } + + writer = mi_lttng_writer_create(ret, lttng_opt_mi); + if (!writer) { + ret = CMD_ERROR; + goto end; + } + + /* Open command element */ + ret = mi_lttng_writer_command_open(writer, + mi_lttng_element_command_add_context); + if (ret) { + ret = CMD_ERROR; + goto end; + } + + /* Open output element */ + ret = mi_lttng_writer_open_element(writer, + mi_lttng_element_command_output); + if (ret) { + ret = CMD_ERROR; + goto end; + } +end: + return ret; +} + +static +int mi_close(enum cmd_error_code success) +{ + int ret; + + /* MI closing */ + if (!lttng_opt_mi) { + ret = 0; + goto end; + } + /* Close output element */ + ret = mi_lttng_writer_close_element(writer); + if (ret) { + ret = CMD_ERROR; + goto end; + } + + /* Success ? */ + ret = mi_lttng_writer_write_element_bool(writer, + mi_lttng_element_command_success, !success); + if (ret) { + ret = CMD_ERROR; + goto end; + } + + /* Command element close */ + ret = mi_lttng_writer_command_close(writer); + if (ret) { + ret = CMD_ERROR; + goto end; + } +end: + return ret; +} + +static +void populate_context(struct lttng_event_context *context, + const struct ctx_opts *opt) +{ + char *ptr; + + context->ctx = (enum lttng_event_context_type) opt->ctx_type; + switch (context->ctx) { + case LTTNG_EVENT_CONTEXT_PERF_COUNTER: + case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER: + case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER: + context->u.perf_counter.type = opt->u.perf.type; + context->u.perf_counter.config = opt->u.perf.config; + strncpy(context->u.perf_counter.name, opt->symbol, + LTTNG_SYMBOL_NAME_LEN); + context->u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; + /* Replace : and - by _ */ + while ((ptr = strchr(context->u.perf_counter.name, '-')) != NULL) { + *ptr = '_'; + } + while ((ptr = strchr(context->u.perf_counter.name, ':')) != NULL) { + *ptr = '_'; + } + break; + case LTTNG_EVENT_CONTEXT_APP_CONTEXT: + context->u.app_ctx.provider_name = + opt->u.app_ctx.provider_name; + context->u.app_ctx.ctx_name = + opt->u.app_ctx.ctx_name; + break; + default: + break; + } +} + +/* + * Pretty print context type. + */ +static +int print_ctx_type(void) +{ + + FILE *ofp = stdout; + int i = 0; + int ret; + struct lttng_event_context context; + + memset(&context, 0, sizeof(context)); + + ret = mi_open(); + if (ret) { + ret = CMD_ERROR; + goto end; + } + + if (lttng_opt_mi) { + /* Open a contexts element */ + ret = mi_lttng_writer_open_element(writer, config_element_contexts); + if (ret) { + ret = CMD_ERROR; + goto end; + } + } + + while (ctx_opts[i].symbol != NULL) { + if (!ctx_opts[i].hide_help) { + if (lttng_opt_mi) { + populate_context(&context, &ctx_opts[i]); + ret = mi_lttng_context(writer, &context, 1); + if (ret) { + ret = CMD_ERROR; + goto end; + } + + ret = mi_lttng_writer_write_element_string( + writer, + mi_lttng_element_context_symbol, + ctx_opts[i].symbol); + if (ret) { + ret = CMD_ERROR; + goto end; + } + + ret = mi_lttng_writer_close_element(writer); + if (ret) { + ret = CMD_ERROR; + goto end; + } + } else { + fprintf(ofp, "%s\n", ctx_opts[i].symbol); + } + } + i++; + } + + if (lttng_opt_mi) { + /* Close contexts element */ + ret = mi_lttng_writer_close_element(writer); + if (ret) { + goto end; + } + } + +end: + ret = mi_close(ret); + if (ret) { + ret = CMD_ERROR; + } + return ret; +} + /* * Add context to channel or event. */ @@ -537,7 +726,6 @@ static int add_context(char *session_name) struct lttng_event_context context; struct lttng_domain dom; struct ctx_type *type; - char *ptr; memset(&context, 0, sizeof(context)); memset(&dom, 0, sizeof(dom)); @@ -559,35 +747,10 @@ static int add_context(char *session_name) /* Iterate over all the context types given */ cds_list_for_each_entry(type, &ctx_type_list.head, list) { - context.ctx = (enum lttng_event_context_type) type->opt->ctx_type; - switch (context.ctx) { - case LTTNG_EVENT_CONTEXT_PERF_COUNTER: - case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER: - case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER: - context.u.perf_counter.type = type->opt->u.perf.type; - context.u.perf_counter.config = type->opt->u.perf.config; - strncpy(context.u.perf_counter.name, type->opt->symbol, - LTTNG_SYMBOL_NAME_LEN); - context.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; - /* Replace : and - by _ */ - while ((ptr = strchr(context.u.perf_counter.name, '-')) != NULL) { - *ptr = '_'; - } - while ((ptr = strchr(context.u.perf_counter.name, ':')) != NULL) { - *ptr = '_'; - } - break; - case LTTNG_EVENT_CONTEXT_APP_CONTEXT: - context.u.app_ctx.provider_name = - type->opt->u.app_ctx.provider_name; - context.u.app_ctx.ctx_name = - type->opt->u.app_ctx.ctx_name; - break; - default: - break; - } DBG("Adding context..."); + populate_context(&context, type->opt); + if (lttng_opt_mi) { /* We leave context open the update the success of the command */ ret = mi_lttng_context(writer, &context, 1); @@ -595,6 +758,14 @@ static int add_context(char *session_name) ret = CMD_ERROR; goto error; } + + ret = mi_lttng_writer_write_element_string(writer, + mi_lttng_element_context_symbol, + type->opt->symbol); + if (ret) { + ret = CMD_ERROR; + goto error; + } } ret = lttng_add_context(handle, &context, NULL, opt_channel_name); @@ -661,7 +832,9 @@ void destroy_ctx_type(struct ctx_type *type) if (!type) { return; } - free(type->opt->symbol); + if (type->opt) { + free(type->opt->symbol); + } free(type->opt); free(type); } @@ -687,10 +860,101 @@ 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::". */ + 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: or " + "perf:thread:raw:rNNN:"); + 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; + int opt_index, ret; struct ctx_type *type = NULL; const char app_ctx_prefix[] = "$app."; char *provider_name = NULL, *ctx_name = NULL; @@ -713,6 +977,18 @@ struct ctx_type *get_context_type(const char *ctx) 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. @@ -782,10 +1058,10 @@ not_found: int cmd_add_context(int argc, const char **argv) { int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS; - int success = 1; static poptContext pc; struct ctx_type *type, *tmptype; char *session_name = NULL; + const char *leftover = NULL; if (argc < 2) { ret = CMD_ERROR; @@ -801,7 +1077,7 @@ int cmd_add_context(int argc, const char **argv) SHOW_HELP(); goto end; case OPT_LIST: - print_ctx_type(stdout); + ret = print_ctx_type(); goto end; case OPT_TYPE: { @@ -832,6 +1108,13 @@ int cmd_add_context(int argc, const char **argv) } } + leftover = poptGetArg(pc); + if (leftover) { + ERR("Unknown argument: %s", leftover); + ret = CMD_ERROR; + goto end; + } + ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace + opt_jul + opt_log4j); if (ret) { @@ -855,59 +1138,15 @@ int cmd_add_context(int argc, const char **argv) session_name = opt_session_name; } - /* Mi check */ - if (lttng_opt_mi) { - writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); - if (!writer) { - ret = -LTTNG_ERR_NOMEM; - goto end; - } - - /* Open command element */ - ret = mi_lttng_writer_command_open(writer, - mi_lttng_element_command_add_context); - if (ret) { - ret = CMD_ERROR; - goto end; - } - - /* Open output element */ - ret = mi_lttng_writer_open_element(writer, - mi_lttng_element_command_output); - if (ret) { - ret = CMD_ERROR; - goto end; - } + ret = mi_open(); + if (ret) { + goto end; } command_ret = add_context(session_name); - if (command_ret) { - success = 0; - } - - /* Mi closing */ - if (lttng_opt_mi) { - /* Close output element */ - ret = mi_lttng_writer_close_element(writer); - if (ret) { - ret = CMD_ERROR; - goto end; - } - - /* Success ? */ - ret = mi_lttng_writer_write_element_bool(writer, - mi_lttng_element_command_success, success); - if (ret) { - ret = CMD_ERROR; - goto end; - } - - /* Command element close */ - ret = mi_lttng_writer_command_close(writer); - if (ret) { - ret = CMD_ERROR; - goto end; - } + ret = mi_close(command_ret); + if (ret) { + goto end; } end: