+struct lttng_userspace_probe_location *
+lttng_userspace_probe_location_tracepoint_create(const char *binary_path,
+ const char *provider_name, const char *probe_name,
+ struct lttng_userspace_probe_location_lookup_method *lookup_method)
+{
+ struct lttng_userspace_probe_location *ret = NULL;
+
+ if (!binary_path || !probe_name || !provider_name) {
+ ERR("Invalid argument(s)");
+ goto end;
+ }
+
+ switch (lttng_userspace_probe_location_lookup_method_get_type(
+ lookup_method)) {
+ case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
+ break;
+ default:
+ /* Invalid probe location lookup method. */
+ goto end;
+ }
+
+ ret = lttng_userspace_probe_location_tracepoint_create_no_check(
+ binary_path, provider_name, probe_name, lookup_method, true);
+end:
+ return ret;
+}
+
+static struct lttng_userspace_probe_location_lookup_method *
+lttng_userspace_probe_location_lookup_method_function_elf_copy(
+ const struct lttng_userspace_probe_location_lookup_method *lookup_method)
+{
+ struct lttng_userspace_probe_location_lookup_method *parent = NULL;
+ struct lttng_userspace_probe_location_lookup_method_elf *elf_method;
+
+ assert(lookup_method);
+ assert(lookup_method->type ==
+ LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF);
+
+ elf_method = zmalloc(sizeof(*elf_method));
+ if (!elf_method) {
+ PERROR("Error allocating ELF userspace probe lookup method");
+ goto error;
+ }
+
+ elf_method->parent.type = lookup_method->type;
+ parent = &elf_method->parent;
+
+ goto end;
+error:
+ parent = NULL;
+end:
+ return parent;
+}
+
+static struct lttng_userspace_probe_location_lookup_method *
+lttng_userspace_probe_location_lookup_method_tracepoint_sdt_copy(
+ struct lttng_userspace_probe_location_lookup_method *lookup_method)
+{
+ struct lttng_userspace_probe_location_lookup_method *parent = NULL;
+ struct lttng_userspace_probe_location_lookup_method_sdt *sdt_method;
+
+ assert(lookup_method);
+ assert(lookup_method->type ==
+ LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT);
+
+ sdt_method = zmalloc(sizeof(*sdt_method));
+ if (!sdt_method) {
+ PERROR("zmalloc");
+ goto error;
+ }
+
+ sdt_method->parent.type = lookup_method->type;
+ parent = &sdt_method->parent;
+
+ goto end;
+
+error:
+ parent = NULL;
+end:
+ return parent;
+}
+
+static struct lttng_userspace_probe_location *
+lttng_userspace_probe_location_function_copy(
+ const struct lttng_userspace_probe_location *location)
+{
+ 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;
+ 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);
+
+ /* 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_userspace_probe_location_function_get_function_name(location);
+ if (!function_name) {
+ ERR("Userspace probe function name is NULL");
+ goto error;
+ }
+
+ /* Duplicate the binary fd */
+ 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;
+ }
+
+ /*
+ * Duplicate probe location method fields
+ */
+ lookup_type = lttng_userspace_probe_location_lookup_method_get_type(
+ location->lookup_method);
+ switch (lookup_type) {
+ case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
+ lookup_method =
+ lttng_userspace_probe_location_lookup_method_function_elf_copy(
+ location->lookup_method);
+ if (!lookup_method) {
+ goto close_fd;
+ }
+ break;
+ default:
+ /* Invalid probe location lookup method. */
+ goto close_fd;
+ }
+
+ /* Create the probe_location */
+ new_location = lttng_userspace_probe_location_function_create_no_check(
+ 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, new_fd) < 0) {
+ goto destroy_probe_location;
+ }
+
+ goto end;
+
+destroy_probe_location:
+ lttng_userspace_probe_location_destroy(new_location);
+destroy_lookup_method:
+ lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
+close_fd:
+ if (close(new_fd) < 0) {
+ PERROR("Error closing duplicated file descriptor in error path");
+ }
+error:
+ new_location = NULL;
+end:
+ return new_location;
+}
+
+static struct lttng_userspace_probe_location *
+lttng_userspace_probe_location_tracepoint_copy(
+ const struct lttng_userspace_probe_location *location)
+{
+ 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;
+ 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);
+
+ /* Get probe location fields */
+ binary_path = lttng_userspace_probe_location_tracepoint_get_binary_path(location);
+ if (!binary_path) {
+ ERR("Userspace probe binary path is NULL");
+ goto error;
+ }
+
+ probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(location);
+ if (!probe_name) {
+ ERR("Userspace probe probe name is NULL");
+ goto error;
+ }
+
+ provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(location);
+ if (!provider_name) {
+ ERR("Userspace probe provider name is NULL");
+ goto error;
+ }
+
+ /* Duplicate the binary fd */
+ fd = lttng_userspace_probe_location_tracepoint_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;
+ }
+
+ /*
+ * Duplicate probe location method fields
+ */
+ lookup_type = lttng_userspace_probe_location_lookup_method_get_type(
+ location->lookup_method);
+ switch (lookup_type) {
+ case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
+ lookup_method =
+ lttng_userspace_probe_location_lookup_method_tracepoint_sdt_copy(
+ location->lookup_method);
+ if (!lookup_method) {
+ goto close_fd;
+ }
+ break;
+ default:
+ /* Invalid probe location lookup method. */
+ goto close_fd;
+ }
+
+ /* Create the probe_location */
+ new_location = lttng_userspace_probe_location_tracepoint_create_no_check(
+ 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, new_fd) < 0) {
+ goto destroy_probe_location;
+ }
+
+ goto end;
+
+destroy_probe_location:
+ lttng_userspace_probe_location_destroy(new_location);
+destroy_lookup_method:
+ lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
+close_fd:
+ if (close(new_fd) < 0) {
+ PERROR("Error closing duplicated file descriptor in error path");
+ }
+error:
+ new_location = NULL;
+end:
+ return new_location;
+}
+