trace: fix build with gcc 10
[dpdk.git] / lib / librte_eal / common / eal_common_trace_utils.c
index 4fce605..64f58fb 100644 (file)
@@ -135,28 +135,24 @@ fail:
 }
 
 int
-eal_trace_args_save(const char *optarg)
+eal_trace_args_save(const char *val)
 {
        struct trace *trace = trace_obj_get();
-       char *trace_args;
-       uint8_t nb_args;
+       struct trace_arg *arg = malloc(sizeof(*arg));
 
-       nb_args = trace->args.nb_args;
-
-       if (nb_args >= TRACE_MAX_ARGS) {
-               trace_err("ignoring trace %s as limit exceeds", optarg);
-               return 0;
+       if (arg == NULL) {
+               trace_err("failed to allocate memory for %s", val);
+               return -ENOMEM;
        }
 
-       trace_args = calloc(1, (strlen(optarg) + 1));
-       if (trace_args == NULL) {
-               trace_err("fail to allocate memory for %s", optarg);
+       arg->val = strdup(val);
+       if (arg->val == NULL) {
+               trace_err("failed to allocate memory for %s", val);
+               free(arg);
                return -ENOMEM;
        }
 
-       memcpy(trace_args, optarg, strlen(optarg));
-       trace->args.args[nb_args++] = trace_args;
-       trace->args.nb_args = nb_args;
+       STAILQ_INSERT_TAIL(&trace->args, arg, next);
        return 0;
 }
 
@@ -164,47 +160,34 @@ void
 eal_trace_args_free(void)
 {
        struct trace *trace = trace_obj_get();
-       int i;
+       struct trace_arg *arg;
 
-       for (i = 0; i < trace->args.nb_args; i++) {
-               if (trace->args.args[i]) {
-                       free((void *)trace->args.args[i]);
-                       trace->args.args[i] = NULL;
-               }
+       while (!STAILQ_EMPTY(&trace->args)) {
+               arg = STAILQ_FIRST(&trace->args);
+               STAILQ_REMOVE_HEAD(&trace->args, next);
+               free(arg->val);
+               free(arg);
        }
 }
 
 int
 trace_args_apply(const char *arg)
 {
-       char *str;
-
-       str = strdup(arg);
-       if (str == NULL)
-               return -1;
-
-       if (rte_trace_regexp(str, true) < 0) {
-               trace_err("cannot enable trace for %s", str);
-               free(str);
+       if (rte_trace_regexp(arg, true) < 0) {
+               trace_err("cannot enable trace for %s", arg);
                return -1;
        }
 
-       free(str);
        return 0;
 }
 
 int
-eal_trace_bufsz_args_save(char const *optarg)
+eal_trace_bufsz_args_save(char const *val)
 {
        struct trace *trace = trace_obj_get();
        uint64_t bufsz;
 
-       if (optarg == NULL) {
-               trace_err("no optarg is passed");
-               return -EINVAL;
-       }
-
-       bufsz = rte_str_to_size(optarg);
+       bufsz = rte_str_to_size(val);
        if (bufsz == 0) {
                trace_err("buffer size cannot be zero");
                return -EINVAL;
@@ -224,30 +207,57 @@ trace_bufsz_args_apply(void)
 }
 
 int
-eal_trace_dir_args_save(char const *optarg)
+eal_trace_mode_args_save(const char *val)
 {
        struct trace *trace = trace_obj_get();
-       uint32_t size = sizeof(trace->dir);
-       char *dir_path = NULL;
-       int rc;
+       size_t len = strlen(val);
+       unsigned long tmp;
+       char *pattern;
 
-       if (optarg == NULL) {
-               trace_err("no optarg is passed");
+       if (len == 0) {
+               trace_err("value is not provided with option");
                return -EINVAL;
        }
 
-       if (strlen(optarg) >= size) {
+       pattern = (char *)calloc(1, len + 2);
+       if (pattern == NULL) {
+               trace_err("fail to allocate memory");
+               return -ENOMEM;
+       }
+
+       sprintf(pattern, "%s*", val);
+
+       if (fnmatch(pattern, "overwrite", 0) == 0)
+               tmp = RTE_TRACE_MODE_OVERWRITE;
+       else if (fnmatch(pattern, "discard", 0) == 0)
+               tmp = RTE_TRACE_MODE_DISCARD;
+       else {
+               free(pattern);
+               return -EINVAL;
+       }
+
+       trace->mode = tmp;
+       free(pattern);
+       return 0;
+}
+
+int
+eal_trace_dir_args_save(char const *val)
+{
+       struct trace *trace = trace_obj_get();
+       char *dir_path;
+       int rc;
+
+       if (strlen(val) >= sizeof(trace->dir) - 1) {
                trace_err("input string is too big");
                return -ENAMETOOLONG;
        }
 
-       dir_path = (char *)calloc(1, size);
-       if (dir_path == NULL) {
-               trace_err("fail to allocate memory");
+       if (asprintf(&dir_path, "%s/", val) == -1) {
+               trace_err("failed to copy directory: %s", strerror(errno));
                return -ENOMEM;
        }
 
-       sprintf(dir_path, "%s/", optarg);
        rc = trace_dir_update(dir_path);
 
        free(dir_path);