Fix: illegal memory access in cmd_snapshot_record
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
index f62dadbf08321dd5995d2f2ae4581deab5700c91..3def6dab84875052063d9afd9c4b13504f8ab49f 100644 (file)
@@ -112,7 +112,7 @@ static struct poptOption long_options[] = {
  */
 static void usage(FILE *ofp)
 {
-       fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] [-k|-u] [OPTIONS] \n");
+       fprintf(ofp, "usage: lttng enable-event NAME[,NAME2,...] (-k | -u | -j | -l | -p) [OPTIONS] \n");
        fprintf(ofp, "\n");
        fprintf(ofp, "Options:\n");
        fprintf(ofp, "  -h, --help               Show this help\n");
@@ -120,9 +120,9 @@ static void usage(FILE *ofp)
        fprintf(ofp, "  -s, --session NAME       Apply to session name\n");
        fprintf(ofp, "  -c, --channel NAME       Apply to this channel\n");
        fprintf(ofp, "  -a, --all                Enable all tracepoints and syscalls\n");
-       fprintf(ofp, "  -k, --kernel             Apply for the kernel tracer\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 for Java application using JUL\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, "Event options:\n");
@@ -247,26 +247,27 @@ static void usage(FILE *ofp)
  */
 static int parse_probe_opts(struct lttng_event *ev, char *opt)
 {
-       int ret;
+       int ret = CMD_SUCCESS;
+       int match;
        char s_hex[19];
 #define S_HEX_LEN_SCANF_IS_A_BROKEN_API "18"   /* 18 is (19 - 1) (\0 is extra) */
        char name[LTTNG_SYMBOL_NAME_LEN];
 
        if (opt == NULL) {
-               ret = -1;
+               ret = CMD_ERROR;
                goto end;
        }
 
        /* Check for symbol+offset */
-       ret = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
+       match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API
                        "[^'+']+%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", name, s_hex);
-       if (ret == 2) {
+       if (match == 2) {
                strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
                ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                DBG("probe symbol %s", ev->attr.probe.symbol_name);
                if (*s_hex == '\0') {
                        ERR("Invalid probe offset %s", s_hex);
-                       ret = -1;
+                       ret = CMD_ERROR;
                        goto end;
                }
                ev->attr.probe.offset = strtoul(s_hex, NULL, 0);
@@ -277,9 +278,9 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt)
 
        /* Check for symbol */
        if (isalpha(name[0])) {
-               ret = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
+               match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
                        name);
-               if (ret == 1) {
+               if (match == 1) {
                        strncpy(ev->attr.probe.symbol_name, name, LTTNG_SYMBOL_NAME_LEN);
                        ev->attr.probe.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                        DBG("probe symbol %s", ev->attr.probe.symbol_name);
@@ -291,11 +292,11 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt)
        }
 
        /* Check for address */
-       ret = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex);
-       if (ret > 0) {
+       match = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex);
+       if (match > 0) {
                if (*s_hex == '\0') {
                        ERR("Invalid probe address %s", s_hex);
-                       ret = -1;
+                       ret = CMD_ERROR;
                        goto end;
                }
                ev->attr.probe.addr = strtoul(s_hex, NULL, 0);
@@ -306,7 +307,7 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt)
        }
 
        /* No match */
-       ret = -1;
+       ret = CMD_ERROR;
 
 end:
        return ret;
@@ -513,7 +514,10 @@ char *print_exclusions(int count, char **names)
 
        /* add length of preamble + one for NUL - one for last (missing) comma */
        length += strlen(preamble);
-       ret = malloc(length);
+       ret = zmalloc(length);
+       if (!ret) {
+               return NULL;
+       }
        strncpy(ret, preamble, length);
        for (i = 0; i < count; i++) {
                strcat(ret, names[i]);
@@ -572,11 +576,25 @@ int check_exclusion_subsets(const char *event_name,
                                goto error;
                        }
                        if (e == '*') {
+                               char *string;
+                               char **new_exclusion_list;
+
                                /* Excluder is a proper subset of event */
+                               string = strndup(next_excluder, excluder_length);
+                               if (!string) {
+                                       PERROR("strndup error");
+                                       goto error;
+                               }
+                               new_exclusion_list = realloc(exclusion_list,
+                                       sizeof(char *) * (exclusion_count + 1));
+                               if (!new_exclusion_list) {
+                                       PERROR("realloc");
+                                       free(string);
+                                       goto error;
+                               }
+                               exclusion_list = new_exclusion_list;
                                exclusion_count++;
-                               exclusion_list = realloc(exclusion_list, sizeof(char **) * exclusion_count);
-                               exclusion_list[exclusion_count - 1] = strndup(next_excluder, excluder_length);
-
+                               exclusion_list[exclusion_count - 1] = string;
                                break;
                        }
                        if (x != e) {
@@ -658,15 +676,25 @@ static int enable_events(char *session_name)
                /* Default. */
                dom.buf_type = LTTNG_BUFFER_PER_UID;
        } else {
-               print_missing_domain();
-               ret = CMD_ERROR;
-               goto error;
+               /* Checked by the caller. */
+               assert(0);
        }
 
-       if (opt_kernel && opt_exclude) {
-               ERR("Event name exclusions are not yet implemented for kernel events");
-               ret = CMD_ERROR;
-               goto error;
+       if (opt_exclude) {
+               switch (dom.type) {
+               case LTTNG_DOMAIN_KERNEL:
+               case LTTNG_DOMAIN_JUL:
+               case LTTNG_DOMAIN_LOG4J:
+                       ERR("Event name exclusions are not yet implemented for %s events",
+                                       get_domain_str(dom.type));
+                       ret = CMD_ERROR;
+                       goto error;
+               case LTTNG_DOMAIN_UST:
+                       /* Exclusions supported */
+                       break;
+               default:
+                       assert(0);
+               }
        }
 
        channel_name = opt_channel_name;
@@ -742,6 +770,16 @@ static int enable_events(char *session_name)
                                                        print_channel_name(channel_name), session_name);
                                        warn = 1;
                                        break;
+                               case LTTNG_ERR_TRACE_ALREADY_STARTED:
+                               {
+                                       const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
+                                       ERR("Events: %s (channel %s, session %s)",
+                                                       msg,
+                                                       print_channel_name(channel_name),
+                                                       session_name);
+                                       error = 1;
+                                       break;
+                               }
                                default:
                                        ERR("Events: %s (channel %s, session %s)",
                                                        lttng_strerror(ret),
@@ -759,6 +797,12 @@ static int enable_events(char *session_name)
                        case LTTNG_EVENT_TRACEPOINT:
                                if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
                                        char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
+
+                                       if (!exclusion_string) {
+                                               PERROR("Cannot allocate exclusion_string");
+                                               error = 1;
+                                               goto end;
+                                       }
                                        MSG("All %s tracepoints%s are enabled in channel %s for loglevel %s",
                                                        get_domain_str(dom.type),
                                                        exclusion_string,
@@ -767,6 +811,12 @@ static int enable_events(char *session_name)
                                        free(exclusion_string);
                                } else {
                                        char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
+
+                                       if (!exclusion_string) {
+                                               PERROR("Cannot allocate exclusion_string");
+                                               error = 1;
+                                               goto end;
+                                       }
                                        MSG("All %s tracepoints%s are enabled in channel %s",
                                                        get_domain_str(dom.type),
                                                        exclusion_string,
@@ -776,13 +826,20 @@ static int enable_events(char *session_name)
                                break;
                        case LTTNG_EVENT_SYSCALL:
                                if (opt_kernel) {
-                                       MSG("All kernel system calls are enabled in channel %s",
+                                       MSG("All %s system calls are enabled in channel %s",
+                                                       get_domain_str(dom.type),
                                                        print_channel_name(channel_name));
                                }
                                break;
                        case LTTNG_EVENT_ALL:
                                if (opt_loglevel && dom.type != LTTNG_DOMAIN_KERNEL) {
                                        char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
+
+                                       if (!exclusion_string) {
+                                               PERROR("Cannot allocate exclusion_string");
+                                               error = 1;
+                                               goto end;
+                                       }
                                        MSG("All %s events%s are enabled in channel %s for loglevel %s",
                                                        get_domain_str(dom.type),
                                                        exclusion_string,
@@ -791,6 +848,12 @@ static int enable_events(char *session_name)
                                        free(exclusion_string);
                                } else {
                                        char *exclusion_string = print_exclusions(exclusion_count, exclusion_list);
+
+                                       if (!exclusion_string) {
+                                               PERROR("Cannot allocate exclusion_string");
+                                               error = 1;
+                                               goto end;
+                                       }
                                        MSG("All %s events%s are enabled in channel %s",
                                                        get_domain_str(dom.type),
                                                        exclusion_string,
@@ -818,6 +881,16 @@ static int enable_events(char *session_name)
                                                print_channel_name(channel_name), session_name);
                                        warn = 1;
                                        break;
+                               case LTTNG_ERR_TRACE_ALREADY_STARTED:
+                               {
+                                       const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
+                                       ERR("All events: %s (channel %s, session %s, filter \'%s\')",
+                                                       msg,
+                                                       print_channel_name(channel_name),
+                                                       session_name, opt_filter);
+                                       error = 1;
+                                       break;
+                               }
                                default:
                                        ERR("All events: %s (channel %s, session %s, filter \'%s\')",
                                                        lttng_strerror(command_ret),
@@ -852,7 +925,7 @@ static int enable_events(char *session_name)
                                ev.enabled = 0;
                                success = 0;
                        }
-                       ret = mi_lttng_event(writer, &ev, 1);
+                       ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
                        if (ret) {
                                ret = CMD_ERROR;
                                goto error;
@@ -906,7 +979,7 @@ static int enable_events(char *session_name)
                                break;
                        case LTTNG_EVENT_PROBE:
                                ret = parse_probe_opts(&ev, opt_probe);
-                               if (ret < 0) {
+                               if (ret) {
                                        ERR("Unable to parse probe options");
                                        ret = 0;
                                        goto error;
@@ -914,7 +987,7 @@ static int enable_events(char *session_name)
                                break;
                        case LTTNG_EVENT_FUNCTION:
                                ret = parse_probe_opts(&ev, opt_function);
-                               if (ret < 0) {
+                               if (ret) {
                                        ERR("Unable to parse function probe options");
                                        ret = 0;
                                        goto error;
@@ -926,8 +999,8 @@ static int enable_events(char *session_name)
                                ev.attr.ftrace.symbol_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                                break;
                        case LTTNG_EVENT_SYSCALL:
-                               MSG("per-syscall selection not supported yet. Use \"-a\" "
-                                               "for all syscalls.");
+                               ev.type = LTTNG_EVENT_SYSCALL;
+                               break;
                        default:
                                ret = CMD_UNDEFINED;
                                goto error;
@@ -1031,9 +1104,7 @@ static int enable_events(char *session_name)
                        strncpy(ev.name, event_name, LTTNG_SYMBOL_NAME_LEN);
                        ev.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
                } else {
-                       print_missing_domain();
-                       ret = CMD_ERROR;
-                       goto error;
+                       assert(0);
                }
 
                if (!opt_filter) {
@@ -1043,6 +1114,11 @@ static int enable_events(char *session_name)
                                        &ev, channel_name,
                                        NULL, exclusion_count, exclusion_list);
                        exclusion_string = print_exclusions(exclusion_count, exclusion_list);
+                       if (!exclusion_string) {
+                               PERROR("Cannot allocate exclusion_string");
+                               error = 1;
+                               goto end;
+                       }
                        if (command_ret < 0) {
                                /* Turn ret to positive value to handle the positive error code */
                                switch (-command_ret) {
@@ -1053,6 +1129,17 @@ static int enable_events(char *session_name)
                                                        print_channel_name(channel_name), session_name);
                                        warn = 1;
                                        break;
+                               case LTTNG_ERR_TRACE_ALREADY_STARTED:
+                               {
+                                       const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
+                                       ERR("Event %s%s: %s (channel %s, session %s)", event_name,
+                                                       exclusion_string,
+                                                       msg,
+                                                       print_channel_name(channel_name),
+                                                       session_name);
+                                       error = 1;
+                                       break;
+                               }
                                default:
                                        ERR("Event %s%s: %s (channel %s, session %s)", event_name,
                                                        exclusion_string,
@@ -1066,17 +1153,28 @@ static int enable_events(char *session_name)
                                }
                                error_holder = command_ret;
                        } else {
-                               /* So we don't print the default channel name for agent domain. */
-                               if (dom.type == LTTNG_DOMAIN_JUL ||
-                                               dom.type == LTTNG_DOMAIN_LOG4J) {
-                                       MSG("%s event %s%s enabled.",
-                                                       get_domain_str(dom.type), event_name,
-                                                       exclusion_string);
-                               } else {
+                               switch (dom.type) {
+                               case LTTNG_DOMAIN_KERNEL:
+                               case LTTNG_DOMAIN_UST:
                                        MSG("%s event %s%s created in channel %s",
-                                                       get_domain_str(dom.type), event_name,
-                                                       exclusion_string,
-                                                       print_channel_name(channel_name));
+                                               get_domain_str(dom.type),
+                                               event_name,
+                                               exclusion_string,
+                                               print_channel_name(channel_name));
+                                       break;
+                               case LTTNG_DOMAIN_JUL:
+                               case LTTNG_DOMAIN_LOG4J:
+                                       /*
+                                        * Don't print the default channel
+                                        * name for agent domains.
+                                        */
+                                       MSG("%s event %s%s enabled",
+                                               get_domain_str(dom.type),
+                                               event_name,
+                                               exclusion_string);
+                                       break;
+                               default:
+                                       assert(0);
                                }
                        }
                        free(exclusion_string);
@@ -1091,7 +1189,11 @@ static int enable_events(char *session_name)
                        command_ret = lttng_enable_event_with_exclusions(handle, &ev, channel_name,
                                        opt_filter, exclusion_count, exclusion_list);
                        exclusion_string = print_exclusions(exclusion_count, exclusion_list);
-
+                       if (!exclusion_string) {
+                               PERROR("Cannot allocate exclusion_string");
+                               error = 1;
+                               goto end;
+                       }
                        if (command_ret < 0) {
                                switch (-command_ret) {
                                case LTTNG_ERR_FILTER_EXIST:
@@ -1102,6 +1204,17 @@ static int enable_events(char *session_name)
                                                print_channel_name(channel_name), session_name);
                                        warn = 1;
                                        break;
+                               case LTTNG_ERR_TRACE_ALREADY_STARTED:
+                               {
+                                       const char *msg = "The command tried to enable an event in a new domain for a session that has already been started once.";
+                                       ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
+                                                       exclusion_string,
+                                                       msg,
+                                                       print_channel_name(channel_name),
+                                                       session_name, opt_filter);
+                                       error = 1;
+                                       break;
+                               }
                                default:
                                        ERR("Event %s%s: %s (channel %s, session %s, filter \'%s\')", ev.name,
                                                        exclusion_string,
@@ -1131,7 +1244,7 @@ static int enable_events(char *session_name)
                                ev.enabled = 1;
                        }
 
-                       ret = mi_lttng_event(writer, &ev, 1);
+                       ret = mi_lttng_event(writer, &ev, 1, handle->domain.type);
                        if (ret) {
                                ret = CMD_ERROR;
                                goto error;
@@ -1272,6 +1385,13 @@ int cmd_enable_events(int argc, const char **argv)
                }
        }
 
+       ret = print_missing_or_multiple_domains(
+               opt_kernel + opt_userspace + opt_jul + opt_log4j);
+       if (ret) {
+               ret = CMD_ERROR;
+               goto end;
+       }
+
        /* Mi check */
        if (lttng_opt_mi) {
                writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi);
This page took 0.032137 seconds and 4 git commands to generate.