Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
index 41be3a5a03fbd5476732657e35b46769b347c02d..a833c23ad4a3fe7162867ca41803633276a541f8 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
+ * Copyright (C) 2011 EfficiOS Inc.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #define _LGPL_SOURCE
@@ -31,6 +21,7 @@
 #include <common/compat/getenv.h>
 #include <common/string-utils/string-utils.h>
 #include <common/utils.h>
+#include <common/path.h>
 
 #include <lttng/constant.h>
 /* Mi dependancy */
@@ -42,7 +33,6 @@
 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API    "255"
 #endif
 
-static char *opt_event_list;
 static int opt_event_type;
 static const char *opt_loglevel;
 static int opt_loglevel_type;
@@ -143,7 +133,7 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt)
        }
 
        /* Check for symbol */
-       if (isalpha(name[0])) {
+       if (isalpha(name[0]) || name[0] == '_') {
                match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
                        name);
                if (match == 1) {
@@ -160,8 +150,13 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt)
        /* Check for address */
        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);
+               /*
+                * Return an error if the first character of the tentative
+                * address is NULL or not a digit. It can be "0" if the address
+                * is in hexadecimal and can be 1 to 9 if it's in decimal.
+                */
+               if (*s_hex == '\0' || !isdigit(*s_hex)) {
+                       ERR("Invalid probe description %s", s_hex);
                        ret = CMD_ERROR;
                        goto end;
                }
@@ -305,12 +300,10 @@ end:
  */
 static int warn_userspace_probe_syntax(const char *symbol)
 {
-       unsigned long addr = 0;
-       size_t offset = 0;
        int ret;
 
        /* Check if the symbol field is an hex address. */
-       ret = sscanf(symbol, "0x%lx", &addr);
+       ret = sscanf(symbol, "0x%*x");
        if (ret > 0) {
                /* If there is a match, print a warning and return an error. */
                ERR("Userspace probe on address not supported yet.");
@@ -319,7 +312,7 @@ static int warn_userspace_probe_syntax(const char *symbol)
        }
 
        /* Check if the symbol field is an decimal address. */
-       ret = sscanf(symbol, "%lu", &addr);
+       ret = sscanf(symbol, "%*u");
        if (ret > 0) {
                /* If there is a match, print a warning and return an error. */
                ERR("Userspace probe on address not supported yet.");
@@ -328,7 +321,7 @@ static int warn_userspace_probe_syntax(const char *symbol)
        }
 
        /* Check if the symbol field is symbol+hex_offset. */
-       ret = sscanf(symbol, "%*[^+]+0x%lx", &offset);
+       ret = sscanf(symbol, "%*[^+]+0x%*x");
        if (ret > 0) {
                /* If there is a match, print a warning and return an error. */
                ERR("Userspace probe on symbol+offset not supported yet.");
@@ -337,7 +330,7 @@ static int warn_userspace_probe_syntax(const char *symbol)
        }
 
        /* Check if the symbol field is symbol+decimal_offset. */
-       ret = sscanf(symbol, "%*[^+]+%lu", &offset);
+       ret = sscanf(symbol, "%*[^+]+%*u");
        if (ret > 0) {
                /* If there is a match, print a warning and return an error. */
                ERR("Userspace probe on symbol+offset not supported yet.");
@@ -437,7 +430,7 @@ static int parse_userspace_probe_opts(struct lttng_event *ev, char *opt)
                lookup_method =
                        lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
                if (!lookup_method) {
-                       WARN("Failed to create ELF lookup method");
+                       WARN("Failed to create SDT lookup method");
                        ret = CMD_ERROR;
                        goto end_string;
                }
@@ -486,11 +479,12 @@ static int parse_userspace_probe_opts(struct lttng_event *ev, char *opt)
                }
 
                /*
-                * Check if the file exists using access(2). If it does not, walk the
-                * $PATH.
+                * Check if the file exists using access(2), If it does not,
+                * return an error.
                 */
                ret = access(real_target_path, F_OK);
                if (ret) {
+                       ERR("Cannot find binary at path: %s.", real_target_path);
                        ret = CMD_ERROR;
                        goto end_destroy_lookup_method;
                }
@@ -806,7 +800,7 @@ char *print_exclusions(char **names)
 {
        int length = 0;
        int i;
-       const char *preamble = " excluding ";
+       const char preamble[] = " excluding ";
        char *ret;
        int count = names ? strutils_array_of_strings_len(names) : 0;
 
@@ -819,9 +813,8 @@ char *print_exclusions(char **names)
                length += strlen(names[i]) + 4;
        }
 
-       /* add length of preamble + one for NUL - one for last (missing) comma */
-       length += strlen(preamble);
-       ret = zmalloc(length + 1);
+       length += sizeof(preamble);
+       ret = zmalloc(length);
        if (!ret) {
                return NULL;
        }
@@ -968,7 +961,7 @@ static void warn_on_truncated_exclusion_names(char * const *exclusion_list,
  * Enabling event using the lttng API.
  * Note: in case of error only the last error code will be return.
  */
-static int enable_events(char *session_name)
+static int enable_events(char *session_name, char *event_list)
 {
        int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
        int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
@@ -1332,7 +1325,7 @@ static int enable_events(char *session_name)
        }
 
        /* Strip event list */
-       event_name = strtok(opt_event_list, ",");
+       event_name = strtok(event_list, ",");
        while (event_name != NULL) {
                /* Copy name and type of the event */
                strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
@@ -1714,6 +1707,8 @@ int cmd_enable_events(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       char *event_list = NULL;
+       const char *arg_event_list = NULL;
        const char *leftover = NULL;
        int event_type = -1;
 
@@ -1779,7 +1774,9 @@ int cmd_enable_events(int argc, const char **argv)
        }
 
        ret = print_missing_or_multiple_domains(
-               opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python);
+                       opt_kernel + opt_userspace + opt_jul + opt_log4j +
+                                       opt_python,
+                       true);
        if (ret) {
                ret = CMD_ERROR;
                goto end;
@@ -1810,13 +1807,22 @@ int cmd_enable_events(int argc, const char **argv)
                }
        }
 
-       opt_event_list = (char*) poptGetArg(pc);
-       if (opt_event_list == NULL && opt_enable_all == 0) {
-               ERR("Missing event name(s).\n");
+       arg_event_list = poptGetArg(pc);
+       if (arg_event_list == NULL && opt_enable_all == 0) {
+               ERR("Missing event name(s).");
                ret = CMD_ERROR;
                goto end;
        }
 
+       if (opt_enable_all == 0) {
+               event_list = strdup(arg_event_list);
+               if (event_list == NULL) {
+                       PERROR("Failed to copy event name(s)");
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
+
        leftover = poptGetArg(pc);
        if (leftover) {
                ERR("Unknown argument: %s", leftover);
@@ -1835,7 +1841,7 @@ int cmd_enable_events(int argc, const char **argv)
                session_name = opt_session_name;
        }
 
-       command_ret = enable_events(session_name);
+       command_ret = enable_events(session_name, event_list);
        if (command_ret) {
                success = 0;
                goto mi_closing;
@@ -1877,6 +1883,8 @@ end:
                free(session_name);
        }
 
+       free(event_list);
+
        /* Overwrite ret if an error occurred in enable_events */
        ret = command_ret ? command_ret : ret;
 
This page took 0.028808 seconds and 4 git commands to generate.