Update version to 2.0-pre17
[lttng-tools.git] / lttng / commands / add_context.c
index b7a482bf7963e6b13024eff7b4a13dc389499750..2d9699ce2c2b4bed3159d5d8b3af8426fecae96e 100644 (file)
 static char *opt_event_name;
 static char *opt_channel_name;
 static char *opt_session_name;
-static int *opt_kernel;
-static int opt_pid_all;
+static int opt_kernel;
 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
  */
 enum context_type {
        CONTEXT_PID          = 0,
        CONTEXT_PERF_COUNTER = 1,
-       CONTEXT_COMM         = 2,
+       CONTEXT_PROCNAME     = 2,
        CONTEXT_PRIO         = 3,
        CONTEXT_NICE         = 4,
        CONTEXT_VPID         = 5,
@@ -136,10 +140,9 @@ 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},
-       {"all",            0,   POPT_ARG_VAL, &opt_pid_all, 1, 0, 0},
+       {"userspace",      'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 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}
 };
 
@@ -189,7 +192,7 @@ const struct ctx_opts {
        } u;
 } ctx_opts[] = {
        { "pid", CONTEXT_PID },
-       { "comm", CONTEXT_COMM },
+       { "procname", CONTEXT_PROCNAME },
        { "prio", CONTEXT_PRIO },
        { "nice", CONTEXT_NICE },
        { "vpid", CONTEXT_VPID },
@@ -292,10 +295,9 @@ static void usage(FILE *ofp)
 {
        fprintf(ofp, "usage: lttng add-context -t TYPE\n");
        fprintf(ofp, "\n");
-       fprintf(ofp, "If no event name is given (-e), the context will be added to\n");
-       fprintf(ofp, "all events in the channel.\n");
+       fprintf(ofp, "If no event name is given (-e), the context will be added to the channel\n");
        fprintf(ofp, "If no channel and no event is given (-c/-e), the context\n");
-       fprintf(ofp, "will be added to all events in all channels.\n");
+       fprintf(ofp, "will be added to all events and all channels.\n");
        fprintf(ofp, "\n");
        fprintf(ofp, "Options:\n");
        fprintf(ofp, "  -h, --help               Show this help\n");
@@ -303,9 +305,10 @@ static void usage(FILE *ofp)
        fprintf(ofp, "  -c, --channel NAME       Apply on channel\n");
        fprintf(ofp, "  -e, --event NAME         Apply on event\n");
        fprintf(ofp, "  -k, --kernel             Apply for the kernel tracer\n");
-       fprintf(ofp, "  -u, --userspace          Apply for the user-space tracer\n");
-       fprintf(ofp, "      --all                If -u, apply on all traceable apps\n");
-       fprintf(ofp, "  -p, --pid PID            If -u, apply on a specific PID\n");
+       fprintf(ofp, "  -u, --userspace [CMD]    Apply for the user-space tracer\n");
+       fprintf(ofp, "                           If no CMD, the domain used is UST global\n");
+       fprintf(ofp, "                           or else the domain is UST EXEC_NAME\n");
+       fprintf(ofp, "  -p, --pid PID            If -u, apply to specific PID (domain: UST PID)\n");
        fprintf(ofp, "  -t, --type TYPE          Context type. You can repeat that option on\n");
        fprintf(ofp, "                           the command line.\n");
        fprintf(ofp, "                           TYPE can be one of the strings below:\n");
@@ -341,7 +344,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,8 +352,26 @@ 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;
+       } else if (opt_pid != 0) {
+               dom.type = LTTNG_DOMAIN_UST_PID;
+               dom.attr.pid = opt_pid;
+               DBG("PID %d set to lttng handle", opt_pid);
+       } else if (opt_userspace && opt_cmd_name == NULL) {
+               dom.type = LTTNG_DOMAIN_UST;
+       } else if (opt_userspace && opt_cmd_name != NULL) {
+               dom.type = LTTNG_DOMAIN_UST_EXEC_NAME;
+               strncpy(dom.attr.exec_name, opt_cmd_name, NAME_MAX);
+       } else {
+               ERR("Please specify a tracer (-k/--kernel or -u/--userspace)");
+               ret = CMD_NOT_IMPLEMENTED;
+               goto error;
+       }
+
+       handle = lttng_create_handle(session_name, &dom);
+       if (handle == NULL) {
+               ret = -1;
                goto error;
        }
 
@@ -360,8 +381,7 @@ static int add_context(void)
                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 = '_';
@@ -370,34 +390,24 @@ static int add_context(void)
                                *ptr = '_';
                        }
                }
-               if (opt_kernel) {
-                       /* Create kernel domain */
-                       dom.type = LTTNG_DOMAIN_KERNEL;
-
-                       DBG("Adding kernel context");
-                       ret = lttng_add_context(&dom, &context, opt_event_name,
-                                       opt_channel_name);
-                       if (ret < 0) {
-                               goto error;
-                       } else {
-                               MSG("Kernel context %s added", type->opt->symbol);
-                       }
-               } else if (opt_userspace) {             /* User-space tracer action */
-                       /*
-                        * TODO: Waiting on lttng UST 2.0
-                        */
-                       if (opt_pid_all) {
-                       } else if (opt_pid != 0) {
-                       }
-                       ret = CMD_NOT_IMPLEMENTED;
-                       goto error;
+               DBG("Adding context...");
+
+               ret = lttng_add_context(handle, &context, opt_event_name,
+                               opt_channel_name);
+               if (ret < 0) {
+                       fprintf(stderr, "%s: ", type->opt->symbol);
+                       continue;
                } else {
-                       ERR("Please specify a tracer (kernel or user-space)");
-                       goto error;
+                       MSG("%s context %s added to %s event in %s",
+                                       opt_kernel ? "kernel" : "UST", type->opt->symbol,
+                                       opt_event_name ? opt_event_name : "all",
+                                       opt_channel_name ? opt_channel_name : "all channels");
                }
        }
 
 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) {
This page took 0.028049 seconds and 5 git commands to generate.