Add --enable-embedded-help option to embed --help messages in binaries
[lttng-tools.git] / src / bin / lttng / commands / add_context.c
index 6f7c23c01e8980806fa4ed737be2fdc70f829cd9..209a9f4eb1eba9a804b701d017f46a7598ad65fa 100644 (file)
@@ -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 <lttng-add-context.1.h>
+;
+#endif
+
 enum {
        OPT_HELP = 1,
        OPT_TYPE,
@@ -87,6 +93,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 {
@@ -490,48 +497,6 @@ static void print_ctx_type(FILE *ofp)
        }
 }
 
-/*
- * usage
- */
-static void usage(FILE *ofp)
-{
-       fprintf(ofp, "usage: lttng add-context -t TYPE [-k|-u] [OPTIONS]\n");
-       fprintf(ofp, "\n");
-       fprintf(ofp, "If no channel is given (-c), the context is added to\n");
-       fprintf(ofp, "all channels.\n");
-       fprintf(ofp, "\n");
-       fprintf(ofp, "Otherwise the context is added only to the channel (-c).\n");
-       fprintf(ofp, "\n");
-       fprintf(ofp, "Exactly one domain (-k or -u) must be specified.\n");
-       fprintf(ofp, "\n");
-       fprintf(ofp, "Options:\n");
-       fprintf(ofp, "  -h, --help               Show this help\n");
-       fprintf(ofp, "      --list-options       Simple listing of options\n");
-       fprintf(ofp, "  -s, --session NAME       Apply to session name\n");
-       fprintf(ofp, "  -c, --channel NAME       Apply to channel\n");
-       fprintf(ofp, "  -k, --kernel             Apply to the kernel tracer\n");
-       fprintf(ofp, "  -u, --userspace          Apply to the user-space tracer\n");
-       fprintf(ofp, "  -j, --jul                Apply to Java application using JUL\n");
-       fprintf(ofp, "  -l, --log4j              Apply for Java application using LOG4j\n");
-       fprintf(ofp, "\n");
-       fprintf(ofp, "Context:\n");
-       fprintf(ofp, "  -t, --type TYPE          Context type. You can repeat that option on\n");
-       fprintf(ofp, "                           the command line to specify multiple contexts at once.\n");
-       fprintf(ofp, "                           (--kernel preempts --userspace)\n");
-       fprintf(ofp, "                           TYPE can be one of the strings below:\n");
-       print_ctx_type(ofp);
-       fprintf(ofp, "\n");
-       fprintf(ofp, "Note that the vpid, vppid and vtid context types represent the virtual process id,\n"
-                       "virtual parent process id and virtual thread id as seen from the current execution context\n"
-                       "as opposed to the pid, ppid and tid which are kernel internal data structures.\n\n");
-       fprintf(ofp, "Example:\n");
-       fprintf(ofp, "This command will add the context information 'prio' and two per-cpu\n"
-                       "perf counters (hardware branch misses and cache misses), to all channels\n"
-                       "in the trace data output:\n");
-       fprintf(ofp, "# lttng add-context -k -t prio -t perf:cpu:branch-misses -t perf:cpu:cache-misses\n");
-       fprintf(ofp, "\n");
-}
-
 /*
  * Find context numerical value from string.
  *
@@ -647,11 +612,11 @@ static int add_context(char *session_name)
                } else {
                        if (opt_channel_name) {
                                MSG("%s context %s added to channel %s",
-                                               opt_kernel ? "kernel" : "UST", type->opt->symbol,
+                                               get_domain_str(dom.type), type->opt->symbol,
                                                opt_channel_name);
                        } else {
                                MSG("%s context %s added to all channels",
-                                               opt_kernel ? "kernel" : "UST", type->opt->symbol)
+                                               get_domain_str(dom.type), type->opt->symbol);
                        }
                        success = 1;
                }
@@ -703,7 +668,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);
 }
@@ -729,10 +696,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:<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;
+       int opt_index, ret;
        struct ctx_type *type = NULL;
        const char app_ctx_prefix[] = "$app.";
        char *provider_name = NULL, *ctx_name = NULL;
@@ -755,6 +813,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.
@@ -830,7 +900,6 @@ int cmd_add_context(int argc, const char **argv)
        char *session_name = NULL;
 
        if (argc < 2) {
-               usage(stderr);
                ret = CMD_ERROR;
                goto end;
        }
@@ -841,7 +910,7 @@ int cmd_add_context(int argc, const char **argv)
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_HELP:
-                       usage(stdout);
+                       SHOW_HELP();
                        goto end;
                case OPT_LIST:
                        print_ctx_type(stdout);
@@ -870,7 +939,6 @@ int cmd_add_context(int argc, const char **argv)
                        list_cmd_options(stdout, long_options);
                        goto end;
                default:
-                       usage(stderr);
                        ret = CMD_UNDEFINED;
                        goto end;
                }
@@ -885,7 +953,6 @@ int cmd_add_context(int argc, const char **argv)
 
        if (!opt_type) {
                ERR("Missing mandatory -t TYPE");
-               usage(stderr);
                ret = CMD_ERROR;
                goto end;
        }
This page took 0.026679 seconds and 4 git commands to generate.