lttng UI: read opt_cmd_name opt arg for each command
[lttng-tools.git] / lttng / commands / add_context.c
index eb88f246517ed2e387e47846525fdd418a84c2c5..dd13d223be9e5aac96aab78cc1ba9a5889803995 100644 (file)
@@ -40,13 +40,17 @@ 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;
 
 enum {
        OPT_HELP = 1,
        OPT_TYPE,
+       OPT_USERSPACE,
 };
 
+static struct lttng_handle *handle;
+
 /*
  * Taken from the LTTng ABI
  */
@@ -136,7 +140,7 @@ 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},
@@ -341,7 +345,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,13 +353,18 @@ static int add_context(void)
        struct ctx_type *type;
        char *ptr;
 
+       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) {
-               /* Set session name for the current command */
-               if (set_session_name(opt_session_name) < 0) {
-                       ret = CMD_ERROR;
-                       goto error;
-               }
 
                context.ctx = type->opt->ctx_type;
                if (context.ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER) {
@@ -371,15 +380,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) {
                                fprintf(stderr, "%s: ", type->opt->symbol);
-                               goto error;
+                               continue;
                        } else {
                                MSG("Kernel context %s added", type->opt->symbol);
                        }
@@ -399,6 +405,8 @@ static int add_context(void)
        }
 
 error:
+       lttng_destroy_handle(handle);
+
        return ret;
 }
 
@@ -411,6 +419,7 @@ int cmd_add_context(int argc, const char **argv)
        char *tmp;
        static poptContext pc;
        struct ctx_type *type, *tmptype;
+       char *session_name = NULL;
 
        if (argc < 2) {
                usage(stderr);
@@ -454,6 +463,10 @@ int cmd_add_context(int argc, const char **argv)
                        }
                        free(tmp);
                        break;
+               case OPT_USERSPACE:
+                       opt_userspace = 1;
+                       opt_cmd_name = poptGetOptArg(pc);
+                       break;
                default:
                        usage(stderr);
                        ret = CMD_UNDEFINED;
@@ -461,7 +474,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.025446 seconds and 4 git commands to generate.