Enforce const-correctness in UNIX socket wrappers
[lttng-tools.git] / src / bin / lttng / commands / add_context.c
index 405b7828f043dea695beff3bde4206cec59ee4ff..fb37c0aa738f202b6d9a8dbd1a32f1470e91420c 100644 (file)
@@ -15,7 +15,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#define _GNU_SOURCE
+#define _LGPL_SOURCE
 #include <ctype.h>
 #include <popt.h>
 #include <stdio.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <assert.h>
 
 #include <urcu/list.h>
 
+#include <common/mi-lttng.h>
+
 #include "../command.h"
 
 #define PRINT_LINE_LEN 80
@@ -36,11 +39,6 @@ static char *opt_session_name;
 static int opt_kernel;
 static int opt_userspace;
 static char *opt_type;
-#if 0
-/* Not implemented yet */
-static char *opt_cmd_name;
-static pid_t opt_pid;
-#endif
 
 enum {
        OPT_HELP = 1,
@@ -50,6 +48,7 @@ enum {
 };
 
 static struct lttng_handle *handle;
+static struct mi_writer *writer;
 
 /*
  * Taken from the LTTng ABI
@@ -514,6 +513,9 @@ static void usage(FILE *ofp)
        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"
@@ -546,7 +548,7 @@ end:
  */
 static int add_context(char *session_name)
 {
-       int ret = CMD_SUCCESS, warn = 0;
+       int ret = CMD_SUCCESS, warn = 0, success = 0;
        struct lttng_event_context context;
        struct lttng_domain dom;
        struct ctx_type *type;
@@ -560,9 +562,7 @@ static int add_context(char *session_name)
        } else if (opt_userspace) {
                dom.type = LTTNG_DOMAIN_UST;
        } else {
-               print_missing_domain();
-               ret = CMD_ERROR;
-               goto error;
+               assert(0);
        }
 
        handle = lttng_create_handle(session_name, &dom);
@@ -571,6 +571,14 @@ static int add_context(char *session_name)
                goto error;
        }
 
+       if (lttng_opt_mi) {
+               /* Open a contexts element */
+               ret = mi_lttng_writer_open_element(writer, config_element_contexts);
+               if (ret) {
+                       goto error;
+               }
+       }
+
        /* 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;
@@ -596,11 +604,20 @@ static int add_context(char *session_name)
                }
                DBG("Adding context...");
 
+               if (lttng_opt_mi) {
+                       /* We leave context open the update the success of the command */
+                       ret = mi_lttng_context(writer, &context, 1);
+                       if (ret) {
+                               ret = CMD_ERROR;
+                               goto error;
+                       }
+               }
+
                ret = lttng_add_context(handle, &context, NULL, opt_channel_name);
                if (ret < 0) {
                        ERR("%s: %s", type->opt->symbol, lttng_strerror(ret));
                        warn = 1;
-                       continue;
+                       success = 0;
                } else {
                        if (opt_channel_name) {
                                MSG("%s context %s added to channel %s",
@@ -610,6 +627,32 @@ static int add_context(char *session_name)
                                MSG("%s context %s added to all channels",
                                                opt_kernel ? "kernel" : "UST", type->opt->symbol)
                        }
+                       success = 1;
+               }
+
+               if (lttng_opt_mi) {
+                       /* Is the single operation a success ? */
+                       ret = mi_lttng_writer_write_element_bool(writer,
+                                       mi_lttng_element_success, success);
+                       if (ret) {
+                               ret = CMD_ERROR;
+                               goto error;
+                       }
+
+                       /* Close the context element */
+                       ret = mi_lttng_writer_close_element(writer);
+                       if (ret) {
+                               ret = CMD_ERROR;
+                               goto error;
+                       }
+               }
+       }
+
+       if (lttng_opt_mi) {
+               /* Close contexts element */
+               ret = mi_lttng_writer_close_element(writer);
+               if (ret) {
+                       goto error;
                }
        }
 
@@ -622,7 +665,7 @@ error:
         * This means that at least one add_context failed and tells the user to
         * look on stderr for error(s).
         */
-       if (warn) {
+       if (!ret && warn) {
                ret = CMD_WARNING;
        }
        return ret;
@@ -633,7 +676,8 @@ error:
  */
 int cmd_add_context(int argc, const char **argv)
 {
-       int index, opt, ret = CMD_SUCCESS;
+       int index, opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
+       int success = 1;
        static poptContext pc;
        struct ctx_type *type, *tmptype;
        char *session_name = NULL;
@@ -664,9 +708,9 @@ int cmd_add_context(int argc, const char **argv)
                                goto end;
                        }
 
-                       type = malloc(sizeof(struct ctx_type));
+                       type = zmalloc(sizeof(struct ctx_type));
                        if (type == NULL) {
-                               perror("malloc ctx_type");
+                               PERROR("malloc ctx_type");
                                ret = CMD_FATAL;
                                goto end;
                        }
@@ -683,9 +727,6 @@ int cmd_add_context(int argc, const char **argv)
                        break;
                case OPT_USERSPACE:
                        opt_userspace = 1;
-#if 0
-                       opt_cmd_name = poptGetOptArg(pc);
-#endif
                        break;
                case OPT_LIST_OPTIONS:
                        list_cmd_options(stdout, long_options);
@@ -697,6 +738,13 @@ int cmd_add_context(int argc, const char **argv)
                }
        }
 
+       ret = print_missing_or_multiple_domains(opt_kernel + opt_userspace);
+
+       if (ret) {
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        if (!opt_type) {
                ERR("Missing mandatory -t TYPE");
                usage(stderr);
@@ -714,18 +762,80 @@ int cmd_add_context(int argc, const char **argv)
                session_name = opt_session_name;
        }
 
-       ret = add_context(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;
+               }
+       }
+
+       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;
+               }
+       }
 
+end:
        if (!opt_session_name) {
                free(session_name);
        }
 
-end:
+       /* Mi clean-up */
+       if (writer && mi_lttng_writer_destroy(writer)) {
+               /* Preserve original error code */
+               ret = ret ? ret : LTTNG_ERR_MI_IO_FAIL;
+       }
+
        /* Cleanup allocated memory */
        cds_list_for_each_entry_safe(type, tmptype, &ctx_type_list.head, list) {
                free(type);
        }
 
+       /* Overwrite ret if an error occurred during add_context() */
+       ret = command_ret ? command_ret : ret;
+
        poptFreeContext(pc);
        return ret;
 }
This page took 0.031971 seconds and 4 git commands to generate.