Fix: uprobe: missing error code on allocation failure
[lttng-tools.git] / src / common / userspace-probe.c
index ac26a0c47411790b2a130067bcfe4821c1c73a1a..8c6adaca620b62d6fb7a47a94cbf67bffb051d7f 100644 (file)
@@ -1,27 +1,20 @@
 /*
- * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License, version 2.1 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: LGPL-2.1-only
  *
- * This library 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 Lesser General Public License
- * for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <assert.h>
+#include <common/compat/string.h>
 #include <common/error.h>
 #include <common/macros.h>
-#include <common/compat/string.h>
 #include <fcntl.h>
 #include <lttng/constant.h>
 #include <lttng/userspace-probe-internal.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/unistd.h>
 
 enum lttng_userspace_probe_location_lookup_method_type
 lttng_userspace_probe_location_lookup_method_get_type(
@@ -150,10 +143,90 @@ void lttng_userspace_probe_location_destroy(
                lttng_userspace_probe_location_tracepoint_destroy(location);
                break;
        default:
-               free(location);
+               abort();
        }
 }
 
+/* Compare two file descriptors based on their inode and device numbers. */
+static bool fd_is_equal(int a, int b)
+{
+       int ret;
+       bool is_equal = false;
+       struct stat a_stat, b_stat;
+
+       if (a < 0 && b >= 0) {
+               goto end;
+       }
+
+       if (b < 0 && a >= 0) {
+               goto end;
+       }
+
+       if (a < 0 && b < 0) {
+               if (a == -1 && b == -1) {
+                       is_equal = true;
+                       goto end;
+               }
+
+               /* Invalid state, abort. */
+               abort();
+       }
+
+       /* Both are valid file descriptors. */
+       ret = fstat(a, &a_stat);
+       if (ret) {
+               PERROR("Failed to fstat userspace probe location binary fd %d",
+                               a);
+               goto end;
+       }
+
+       ret = fstat(b, &b_stat);
+       if (ret) {
+               PERROR("Failed to fstat userspace probe location binary fd %d",
+                               b);
+               goto end;
+       }
+
+       is_equal = (a_stat.st_ino == b_stat.st_ino) &&
+                       (a_stat.st_dev == b_stat.st_dev);
+
+end:
+       return is_equal;
+}
+
+static bool lttng_userspace_probe_location_function_is_equal(
+               const struct lttng_userspace_probe_location *_a,
+               const struct lttng_userspace_probe_location *_b)
+{
+       bool is_equal = false;
+       struct lttng_userspace_probe_location_function *a, *b;
+
+       a = container_of(_a, struct lttng_userspace_probe_location_function,
+                       parent);
+       b = container_of(_b, struct lttng_userspace_probe_location_function,
+                       parent);
+
+       if (a->instrumentation_type != b->instrumentation_type) {
+               goto end;
+       }
+
+       assert(a->function_name);
+       assert(b->function_name);
+       if (strcmp(a->function_name, b->function_name)) {
+               goto end;
+       }
+
+       assert(a->binary_path);
+       assert(b->binary_path);
+       if (strcmp(a->binary_path, b->binary_path)) {
+               goto end;
+       }
+
+       is_equal = fd_is_equal(a->binary_fd, b->binary_fd);
+end:
+       return is_equal;
+}
+
 static struct lttng_userspace_probe_location *
 lttng_userspace_probe_location_function_create_no_check(const char *binary_path,
                const char *function_name,
@@ -196,10 +269,13 @@ lttng_userspace_probe_location_function_create_no_check(const char *binary_path,
        location->function_name = function_name_copy;
        location->binary_path = binary_path_copy;
        location->binary_fd = binary_fd;
+       location->instrumentation_type =
+                       LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_ENTRY;
 
        ret = &location->parent;
        ret->lookup_method = lookup_method;
        ret->type = LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION;
+       ret->equal = lttng_userspace_probe_location_function_is_equal;
        goto end;
 
 error:
@@ -214,6 +290,42 @@ end:
        return ret;
 }
 
+static bool lttng_userspace_probe_location_tracepoint_is_equal(
+               const struct lttng_userspace_probe_location *_a,
+               const struct lttng_userspace_probe_location *_b)
+{
+       bool is_equal = false;
+       struct lttng_userspace_probe_location_tracepoint *a, *b;
+
+       a = container_of(_a, struct lttng_userspace_probe_location_tracepoint,
+                       parent);
+       b = container_of(_b, struct lttng_userspace_probe_location_tracepoint,
+                       parent);
+
+       assert(a->probe_name);
+       assert(b->probe_name);
+       if (strcmp(a->probe_name, b->probe_name)) {
+               goto end;
+       }
+
+       assert(a->provider_name);
+       assert(b->provider_name);
+       if (strcmp(a->provider_name, b->provider_name)) {
+               goto end;
+       }
+
+       assert(a->binary_path);
+       assert(b->binary_path);
+       if (strcmp(a->binary_path, b->binary_path)) {
+               goto end;
+       }
+
+       is_equal = fd_is_equal(a->binary_fd, b->binary_fd);
+
+end:
+       return is_equal;
+}
+
 static struct lttng_userspace_probe_location *
 lttng_userspace_probe_location_tracepoint_create_no_check(const char *binary_path,
                const char *provider_name, const char *probe_name,
@@ -269,11 +381,13 @@ lttng_userspace_probe_location_tracepoint_create_no_check(const char *binary_pat
        ret = &location->parent;
        ret->lookup_method = lookup_method;
        ret->type = LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT;
+       ret->equal = lttng_userspace_probe_location_tracepoint_is_equal;
        goto end;
 
 error:
        free(probe_name_copy);
        free(provider_name_copy);
+       free(binary_path_copy);
        if (binary_fd >= 0) {
                if (close(binary_fd)) {
                        PERROR("Error closing binary fd in error path");
@@ -400,32 +514,35 @@ lttng_userspace_probe_location_function_copy(
        enum lttng_userspace_probe_location_lookup_method_type lookup_type;
        struct lttng_userspace_probe_location *new_location = NULL;
        struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
-       char *binary_path = NULL;
-       char *function_name = NULL;
-       int fd;
+       const char *binary_path = NULL;
+       const char *function_name = NULL;
+       int fd, new_fd;
 
        assert(location);
        assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
 
-        /* Duplicate probe location fields */
-       binary_path =
-               lttng_strndup(lttng_userspace_probe_location_function_get_binary_path(location),
-                               LTTNG_PATH_MAX);
+        /* Get probe location fields */
+       binary_path = lttng_userspace_probe_location_function_get_binary_path(location);
        if (!binary_path) {
+               ERR("Userspace probe binary path is NULL");
                goto error;
        }
 
-       function_name =
-               lttng_strndup(lttng_userspace_probe_location_function_get_function_name(location),
-                               LTTNG_SYMBOL_NAME_LEN);
+       function_name = lttng_userspace_probe_location_function_get_function_name(location);
        if (!function_name) {
-               PERROR("Error duplicating function name string");
+               ERR("Userspace probe function name is NULL");
                goto error;
        }
 
        /* Duplicate the binary fd */
-       fd = dup(lttng_userspace_probe_location_function_get_binary_fd(location));
+       fd = lttng_userspace_probe_location_function_get_binary_fd(location);
        if (fd == -1) {
+               ERR("Error getting file descriptor to binary");
+               goto error;
+       }
+
+       new_fd = dup(fd);
+       if (new_fd == -1) {
                PERROR("Error duplicating file descriptor to binary");
                goto error;
        }
@@ -451,13 +568,13 @@ lttng_userspace_probe_location_function_copy(
 
        /* Create the probe_location */
        new_location = lttng_userspace_probe_location_function_create_no_check(
-                       binary_path, function_name, lookup_method, true);
+                       binary_path, function_name, lookup_method, false);
        if (!new_location) {
                goto destroy_lookup_method;
        }
 
        /* Set the duplicated fd to the new probe_location */
-       if (lttng_userspace_probe_location_function_set_binary_fd(new_location, fd) < 0) {
+       if (lttng_userspace_probe_location_function_set_binary_fd(new_location, new_fd) < 0) {
                goto destroy_probe_location;
        }
 
@@ -468,12 +585,10 @@ destroy_probe_location:
 destroy_lookup_method:
        lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
 close_fd:
-       if (close(fd) < 0) {
+       if (close(new_fd) < 0) {
                PERROR("Error closing duplicated file descriptor in error path");
        }
 error:
-       free(function_name);
-       free(binary_path);
        new_location = NULL;
 end:
        return new_location;
@@ -486,43 +601,43 @@ lttng_userspace_probe_location_tracepoint_copy(
        enum lttng_userspace_probe_location_lookup_method_type lookup_type;
        struct lttng_userspace_probe_location *new_location = NULL;
        struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
-       char *binary_path = NULL;
-       char *probe_name = NULL;
-       char *provider_name = NULL;
-       int fd;
+       const char *binary_path = NULL;
+       const char *probe_name = NULL;
+       const char *provider_name = NULL;
+       int fd, new_fd;
 
        assert(location);
        assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
 
-        /* Duplicate probe location fields */
-       binary_path =
-               lttng_strndup(lttng_userspace_probe_location_tracepoint_get_binary_path(location),
-                               LTTNG_PATH_MAX);
+        /* Get probe location fields */
+       binary_path = lttng_userspace_probe_location_tracepoint_get_binary_path(location);
        if (!binary_path) {
-               PERROR("lttng_strndup");
+               ERR("Userspace probe binary path is NULL");
                goto error;
        }
 
-       probe_name =
-               lttng_strndup(lttng_userspace_probe_location_tracepoint_get_probe_name(location),
-                               LTTNG_SYMBOL_NAME_LEN);
+       probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(location);
        if (!probe_name) {
-               PERROR("lttng_strndup");
+               ERR("Userspace probe probe name is NULL");
                goto error;
        }
 
-       provider_name =
-               lttng_strndup(lttng_userspace_probe_location_tracepoint_get_provider_name(location),
-                               LTTNG_SYMBOL_NAME_LEN);
+       provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(location);
        if (!provider_name) {
-               PERROR("lttng_strndup");
+               ERR("Userspace probe provider name is NULL");
                goto error;
        }
 
        /* Duplicate the binary fd */
-       fd = dup(lttng_userspace_probe_location_tracepoint_get_binary_fd(location));
+       fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
        if (fd == -1) {
-               PERROR("dup");
+               ERR("Error getting file descriptor to binary");
+               goto error;
+       }
+
+       new_fd = dup(fd);
+       if (new_fd == -1) {
+               PERROR("Error duplicating file descriptor to binary");
                goto error;
        }
 
@@ -547,13 +662,13 @@ lttng_userspace_probe_location_tracepoint_copy(
 
        /* Create the probe_location */
        new_location = lttng_userspace_probe_location_tracepoint_create_no_check(
-                       binary_path, provider_name, probe_name, lookup_method, true);
+                       binary_path, provider_name, probe_name, lookup_method, false);
        if (!new_location) {
                goto destroy_lookup_method;
        }
 
        /* Set the duplicated fd to the new probe_location */
-       if (lttng_userspace_probe_location_tracepoint_set_binary_fd(new_location, fd) < 0) {
+       if (lttng_userspace_probe_location_tracepoint_set_binary_fd(new_location, new_fd) < 0) {
                goto destroy_probe_location;
        }
 
@@ -564,13 +679,10 @@ destroy_probe_location:
 destroy_lookup_method:
        lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
 close_fd:
-       if (close(fd) < 0) {
-               PERROR("close");
+       if (close(new_fd) < 0) {
+               PERROR("Error closing duplicated file descriptor in error path");
        }
 error:
-       free(provider_name);
-       free(probe_name);
-       free(binary_path);
        new_location = NULL;
 end:
        return new_location;
@@ -692,6 +804,52 @@ end:
        return ret;
 }
 
+enum lttng_userspace_probe_location_function_instrumentation_type
+lttng_userspace_probe_location_function_get_instrumentation_type(
+               const struct lttng_userspace_probe_location *location)
+{
+       enum lttng_userspace_probe_location_function_instrumentation_type type;
+       struct lttng_userspace_probe_location_function *function_location;
+
+       if (!location || lttng_userspace_probe_location_get_type(location) !=
+                       LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
+               ERR("Invalid argument(s)");
+               type = LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_UNKNOWN;
+               goto end;
+       }
+
+       function_location = container_of(location,
+               struct lttng_userspace_probe_location_function, parent);
+       type = function_location->instrumentation_type;
+end:
+       return type;
+}
+
+enum lttng_userspace_probe_location_status
+lttng_userspace_probe_location_function_set_instrumentation_type(
+               const struct lttng_userspace_probe_location *location,
+               enum lttng_userspace_probe_location_function_instrumentation_type instrumentation_type)
+{
+       enum lttng_userspace_probe_location_status status =
+                       LTTNG_USERSPACE_PROBE_LOCATION_STATUS_OK;
+       struct lttng_userspace_probe_location_function *function_location;
+
+       if (!location || lttng_userspace_probe_location_get_type(location) !=
+                       LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION ||
+                       instrumentation_type !=
+                       LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_ENTRY) {
+               ERR("Invalid argument(s)");
+               status = LTTNG_USERSPACE_PROBE_LOCATION_STATUS_INVALID;
+               goto end;
+       }
+
+       function_location = container_of(location,
+               struct lttng_userspace_probe_location_function, parent);
+       function_location->instrumentation_type = instrumentation_type;
+end:
+       return status;
+}
+
 int lttng_userspace_probe_location_tracepoint_get_binary_fd(
                const struct lttng_userspace_probe_location *location)
 {
@@ -1059,12 +1217,14 @@ int lttng_userspace_probe_location_function_create_from_buffer(
        function_name = lttng_strndup(function_name_src, LTTNG_SYMBOL_NAME_LEN);
        if (!function_name) {
                PERROR("lttng_strndup");
+               ret = -LTTNG_ERR_NOMEM;
                goto end;
        }
 
        binary_path = lttng_strndup(binary_path_src, LTTNG_PATH_MAX);
        if (!binary_path) {
                PERROR("lttng_strndup");
+               ret = -LTTNG_ERR_NOMEM;
                goto end;
        }
 
@@ -1674,3 +1834,58 @@ struct lttng_userspace_probe_location *lttng_userspace_probe_location_copy(
 err:
        return new_location;
 }
+
+LTTNG_HIDDEN
+bool lttng_userspace_probe_location_lookup_method_is_equal(
+               const struct lttng_userspace_probe_location_lookup_method *a,
+               const struct lttng_userspace_probe_location_lookup_method *b)
+{
+       bool is_equal = false;
+
+       if (!a || !b) {
+               goto end;
+       }
+
+       if (a == b) {
+               is_equal = true;
+               goto end;
+       }
+
+       if (a->type != b->type) {
+               goto end;
+       }
+
+       is_equal = true;
+end:
+       return is_equal;
+}
+
+LTTNG_HIDDEN
+bool lttng_userspace_probe_location_is_equal(
+               const struct lttng_userspace_probe_location *a,
+               const struct lttng_userspace_probe_location *b)
+{
+       bool is_equal = false;
+
+       if (!a || !b) {
+               goto end;
+       }
+
+       if (a == b) {
+               is_equal = true;
+               goto end;
+       }
+
+       if (!lttng_userspace_probe_location_lookup_method_is_equal(
+                           a->lookup_method, b->lookup_method)) {
+               goto end;
+       }
+
+       if (a->type != b->type) {
+               goto end;
+       }
+
+       is_equal = a->equal ? a->equal(a, b) : true;
+end:
+       return is_equal;
+}
This page took 0.02882 seconds and 4 git commands to generate.