2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include "lttng/lttng-error.h"
10 #include <common/compat/string.h>
11 #include <common/error.h>
12 #include <common/hashtable/hashtable.h>
13 #include <common/hashtable/utils.h>
14 #include <common/macros.h>
15 #include <common/mi-lttng.h>
16 #include <common/payload-view.h>
17 #include <common/payload.h>
19 #include <lttng/constant.h>
20 #include <lttng/userspace-probe-internal.h>
22 #include <sys/types.h>
23 #include <sys/unistd.h>
26 int lttng_userspace_probe_location_function_set_binary_fd_handle(
27 struct lttng_userspace_probe_location
*location
,
28 struct fd_handle
*binary_fd_handle
);
31 int lttng_userspace_probe_location_tracepoint_set_binary_fd_handle(
32 struct lttng_userspace_probe_location
*location
,
33 struct fd_handle
*binary_fd_handle
);
36 enum lttng_error_code
lttng_userspace_probe_location_lookup_method_mi_serialize(
37 const struct lttng_userspace_probe_location_lookup_method
39 struct mi_writer
*writer
);
42 enum lttng_error_code
lttng_userspace_probe_location_tracepoint_mi_serialize(
43 const struct lttng_userspace_probe_location
*location
,
44 struct mi_writer
*writer
);
47 enum lttng_error_code
lttng_userspace_probe_location_function_mi_serialize(
48 const struct lttng_userspace_probe_location
*location
,
49 struct mi_writer
*writer
);
51 enum lttng_userspace_probe_location_lookup_method_type
52 lttng_userspace_probe_location_lookup_method_get_type(
53 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
)
55 return lookup_method
? lookup_method
->type
:
56 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_UNKNOWN
;
59 void lttng_userspace_probe_location_lookup_method_destroy(
60 struct lttng_userspace_probe_location_lookup_method
*lookup_method
)
69 struct lttng_userspace_probe_location_lookup_method
*
70 lttng_userspace_probe_location_lookup_method_function_elf_create(void)
72 struct lttng_userspace_probe_location_lookup_method
*ret
= NULL
;
73 struct lttng_userspace_probe_location_lookup_method_elf
*elf_method
;
75 elf_method
= zmalloc(sizeof(*elf_method
));
81 ret
= &elf_method
->parent
;
82 ret
->type
= LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
;
87 struct lttng_userspace_probe_location_lookup_method
*
88 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create(void)
90 struct lttng_userspace_probe_location_lookup_method
*ret
= NULL
;
91 struct lttng_userspace_probe_location_lookup_method_sdt
*sdt_method
;
93 sdt_method
= zmalloc(sizeof(*sdt_method
));
99 ret
= &sdt_method
->parent
;
100 ret
->type
= LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
;
105 enum lttng_userspace_probe_location_type
lttng_userspace_probe_location_get_type(
106 const struct lttng_userspace_probe_location
*location
)
108 return location
? location
->type
:
109 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN
;
113 void lttng_userspace_probe_location_function_destroy(
114 struct lttng_userspace_probe_location
*location
)
116 struct lttng_userspace_probe_location_function
*location_function
= NULL
;
120 location_function
= container_of(location
,
121 struct lttng_userspace_probe_location_function
, parent
);
123 assert(location_function
);
125 free(location_function
->function_name
);
126 free(location_function
->binary_path
);
127 fd_handle_put(location_function
->binary_fd_handle
);
132 void lttng_userspace_probe_location_tracepoint_destroy(
133 struct lttng_userspace_probe_location
*location
)
135 struct lttng_userspace_probe_location_tracepoint
*location_tracepoint
= NULL
;
139 location_tracepoint
= container_of(location
,
140 struct lttng_userspace_probe_location_tracepoint
,
143 assert(location_tracepoint
);
145 free(location_tracepoint
->probe_name
);
146 free(location_tracepoint
->provider_name
);
147 free(location_tracepoint
->binary_path
);
148 fd_handle_put(location_tracepoint
->binary_fd_handle
);
152 void lttng_userspace_probe_location_destroy(
153 struct lttng_userspace_probe_location
*location
)
159 lttng_userspace_probe_location_lookup_method_destroy(
160 location
->lookup_method
);
162 switch (location
->type
) {
163 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
164 lttng_userspace_probe_location_function_destroy(location
);
166 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
167 lttng_userspace_probe_location_tracepoint_destroy(location
);
174 /* Compare two file descriptors based on their inode and device numbers. */
175 static bool fd_is_equal(int a
, int b
)
178 bool is_equal
= false;
179 struct stat a_stat
, b_stat
;
181 if (a
< 0 && b
>= 0) {
185 if (b
< 0 && a
>= 0) {
189 if (a
< 0 && b
< 0) {
190 if (a
== -1 && b
== -1) {
195 /* Invalid state, abort. */
199 /* Both are valid file descriptors. */
200 ret
= fstat(a
, &a_stat
);
202 PERROR("Failed to fstat userspace probe location binary fd %d",
207 ret
= fstat(b
, &b_stat
);
209 PERROR("Failed to fstat userspace probe location binary fd %d",
214 is_equal
= (a_stat
.st_ino
== b_stat
.st_ino
) &&
215 (a_stat
.st_dev
== b_stat
.st_dev
);
221 static unsigned long lttng_userspace_probe_location_function_hash(
222 const struct lttng_userspace_probe_location
*location
)
224 unsigned long hash
= hash_key_ulong(
225 (void *) LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
,
227 struct lttng_userspace_probe_location_function
*function_location
=
228 container_of(location
, typeof(*function_location
),
231 hash
^= hash_key_str(function_location
->function_name
, lttng_ht_seed
);
232 hash
^= hash_key_str(function_location
->binary_path
, lttng_ht_seed
);
234 * No need to hash on the fd. Worst comes to worse,
235 * the equal function will discriminate.
240 static bool lttng_userspace_probe_location_function_is_equal(
241 const struct lttng_userspace_probe_location
*_a
,
242 const struct lttng_userspace_probe_location
*_b
)
244 bool is_equal
= false;
245 struct lttng_userspace_probe_location_function
*a
, *b
;
247 a
= container_of(_a
, struct lttng_userspace_probe_location_function
,
249 b
= container_of(_b
, struct lttng_userspace_probe_location_function
,
252 if (a
->instrumentation_type
!= b
->instrumentation_type
) {
256 assert(a
->function_name
);
257 assert(b
->function_name
);
258 if (strcmp(a
->function_name
, b
->function_name
)) {
262 assert(a
->binary_path
);
263 assert(b
->binary_path
);
264 if (strcmp(a
->binary_path
, b
->binary_path
)) {
268 is_equal
= fd_is_equal(a
->binary_fd_handle
? fd_handle_get_fd(a
->binary_fd_handle
) : -1,
269 b
->binary_fd_handle
? fd_handle_get_fd(b
->binary_fd_handle
) : -1);
274 static struct lttng_userspace_probe_location
*
275 lttng_userspace_probe_location_function_create_no_check(const char *binary_path
,
276 const char *function_name
,
277 struct lttng_userspace_probe_location_lookup_method
*lookup_method
,
281 struct fd_handle
*binary_fd_handle
= NULL
;
282 char *function_name_copy
= NULL
, *binary_path_copy
= NULL
;
283 struct lttng_userspace_probe_location
*ret
= NULL
;
284 struct lttng_userspace_probe_location_function
*location
;
287 binary_fd
= open(binary_path
, O_RDONLY
);
289 PERROR("Error opening the binary");
293 binary_fd_handle
= fd_handle_create(binary_fd
);
298 /* Ownership transferred to fd_handle. */
302 function_name_copy
= lttng_strndup(function_name
, LTTNG_SYMBOL_NAME_LEN
);
303 if (!function_name_copy
) {
304 PERROR("Error duplicating the function name");
308 binary_path_copy
= lttng_strndup(binary_path
, LTTNG_PATH_MAX
);
309 if (!binary_path_copy
) {
310 PERROR("Error duplicating the function name");
314 location
= zmalloc(sizeof(*location
));
316 PERROR("Error allocating userspace probe location");
320 location
->function_name
= function_name_copy
;
321 location
->binary_path
= binary_path_copy
;
322 location
->binary_fd_handle
= binary_fd_handle
;
323 binary_fd_handle
= NULL
;
324 location
->instrumentation_type
=
325 LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_ENTRY
;
327 ret
= &location
->parent
;
328 ret
->lookup_method
= lookup_method
;
329 ret
->type
= LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
;
330 ret
->equal
= lttng_userspace_probe_location_function_is_equal
;
331 ret
->hash
= lttng_userspace_probe_location_function_hash
;
335 free(function_name_copy
);
336 free(binary_path_copy
);
337 if (binary_fd
>= 0) {
338 if (close(binary_fd
)) {
339 PERROR("Error closing binary fd in error path");
342 fd_handle_put(binary_fd_handle
);
347 static unsigned long lttng_userspace_probe_location_tracepoint_hash(
348 const struct lttng_userspace_probe_location
*location
)
350 unsigned long hash
= hash_key_ulong(
351 (void *) LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
,
353 struct lttng_userspace_probe_location_tracepoint
*tp_location
=
354 container_of(location
, typeof(*tp_location
), parent
);
356 hash
^= hash_key_str(tp_location
->probe_name
, lttng_ht_seed
);
357 hash
^= hash_key_str(tp_location
->provider_name
, lttng_ht_seed
);
358 hash
^= hash_key_str(tp_location
->binary_path
, lttng_ht_seed
);
360 * No need to hash on the fd. Worst comes to worse,
361 * the equal function will discriminate.
366 static bool lttng_userspace_probe_location_tracepoint_is_equal(
367 const struct lttng_userspace_probe_location
*_a
,
368 const struct lttng_userspace_probe_location
*_b
)
370 bool is_equal
= false;
371 struct lttng_userspace_probe_location_tracepoint
*a
, *b
;
373 a
= container_of(_a
, struct lttng_userspace_probe_location_tracepoint
,
375 b
= container_of(_b
, struct lttng_userspace_probe_location_tracepoint
,
378 assert(a
->probe_name
);
379 assert(b
->probe_name
);
380 if (strcmp(a
->probe_name
, b
->probe_name
)) {
384 assert(a
->provider_name
);
385 assert(b
->provider_name
);
386 if (strcmp(a
->provider_name
, b
->provider_name
)) {
390 assert(a
->binary_path
);
391 assert(b
->binary_path
);
392 if (strcmp(a
->binary_path
, b
->binary_path
)) {
396 is_equal
= fd_is_equal(a
->binary_fd_handle
? fd_handle_get_fd(a
->binary_fd_handle
) : -1,
397 b
->binary_fd_handle
? fd_handle_get_fd(b
->binary_fd_handle
) : -1);
403 static struct lttng_userspace_probe_location
*
404 lttng_userspace_probe_location_tracepoint_create_no_check(const char *binary_path
,
405 const char *provider_name
, const char *probe_name
,
406 struct lttng_userspace_probe_location_lookup_method
*lookup_method
,
410 struct fd_handle
*binary_fd_handle
= NULL
;
411 char *probe_name_copy
= NULL
;
412 char *provider_name_copy
= NULL
;
413 char *binary_path_copy
= NULL
;
414 struct lttng_userspace_probe_location
*ret
= NULL
;
415 struct lttng_userspace_probe_location_tracepoint
*location
;
418 binary_fd
= open(binary_path
, O_RDONLY
);
424 binary_fd_handle
= fd_handle_create(binary_fd
);
429 /* Ownership transferred to fd_handle. */
433 probe_name_copy
= lttng_strndup(probe_name
, LTTNG_SYMBOL_NAME_LEN
);
434 if (!probe_name_copy
) {
435 PERROR("lttng_strndup");
439 provider_name_copy
= lttng_strndup(provider_name
, LTTNG_SYMBOL_NAME_LEN
);
440 if (!provider_name_copy
) {
441 PERROR("lttng_strndup");
445 binary_path_copy
= lttng_strndup(binary_path
, LTTNG_PATH_MAX
);
446 if (!binary_path_copy
) {
447 PERROR("lttng_strndup");
451 location
= zmalloc(sizeof(*location
));
457 location
->probe_name
= probe_name_copy
;
458 location
->provider_name
= provider_name_copy
;
459 location
->binary_path
= binary_path_copy
;
460 location
->binary_fd_handle
= binary_fd_handle
;
461 binary_fd_handle
= NULL
;
463 ret
= &location
->parent
;
464 ret
->lookup_method
= lookup_method
;
465 ret
->type
= LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
;
466 ret
->equal
= lttng_userspace_probe_location_tracepoint_is_equal
;
467 ret
->hash
= lttng_userspace_probe_location_tracepoint_hash
;
471 free(probe_name_copy
);
472 free(provider_name_copy
);
473 free(binary_path_copy
);
474 if (binary_fd
>= 0) {
475 if (close(binary_fd
)) {
476 PERROR("Error closing binary fd in error path");
479 fd_handle_put(binary_fd_handle
);
484 struct lttng_userspace_probe_location
*
485 lttng_userspace_probe_location_function_create(const char *binary_path
,
486 const char *function_name
,
487 struct lttng_userspace_probe_location_lookup_method
*lookup_method
)
489 struct lttng_userspace_probe_location
*ret
= NULL
;
491 if (!binary_path
|| !function_name
) {
492 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
496 switch (lttng_userspace_probe_location_lookup_method_get_type(
498 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
499 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
502 /* Invalid probe location lookup method. */
506 ret
= lttng_userspace_probe_location_function_create_no_check(
507 binary_path
, function_name
, lookup_method
, true);
512 struct lttng_userspace_probe_location
*
513 lttng_userspace_probe_location_tracepoint_create(const char *binary_path
,
514 const char *provider_name
, const char *probe_name
,
515 struct lttng_userspace_probe_location_lookup_method
*lookup_method
)
517 struct lttng_userspace_probe_location
*ret
= NULL
;
519 if (!binary_path
|| !probe_name
|| !provider_name
) {
520 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
524 switch (lttng_userspace_probe_location_lookup_method_get_type(
526 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
529 /* Invalid probe location lookup method. */
533 ret
= lttng_userspace_probe_location_tracepoint_create_no_check(
534 binary_path
, provider_name
, probe_name
, lookup_method
, true);
539 static struct lttng_userspace_probe_location_lookup_method
*
540 lttng_userspace_probe_location_lookup_method_function_elf_copy(
541 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
)
543 struct lttng_userspace_probe_location_lookup_method
*parent
= NULL
;
544 struct lttng_userspace_probe_location_lookup_method_elf
*elf_method
;
546 assert(lookup_method
);
547 assert(lookup_method
->type
==
548 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
550 elf_method
= zmalloc(sizeof(*elf_method
));
552 PERROR("Error allocating ELF userspace probe lookup method");
556 elf_method
->parent
.type
= lookup_method
->type
;
557 parent
= &elf_method
->parent
;
566 static struct lttng_userspace_probe_location_lookup_method
*
567 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_copy(
568 struct lttng_userspace_probe_location_lookup_method
*lookup_method
)
570 struct lttng_userspace_probe_location_lookup_method
*parent
= NULL
;
571 struct lttng_userspace_probe_location_lookup_method_sdt
*sdt_method
;
573 assert(lookup_method
);
574 assert(lookup_method
->type
==
575 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
577 sdt_method
= zmalloc(sizeof(*sdt_method
));
583 sdt_method
->parent
.type
= lookup_method
->type
;
584 parent
= &sdt_method
->parent
;
594 static struct lttng_userspace_probe_location
*
595 lttng_userspace_probe_location_function_copy(
596 const struct lttng_userspace_probe_location
*location
)
598 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
599 struct lttng_userspace_probe_location
*new_location
= NULL
;
600 struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
601 const char *binary_path
= NULL
;
602 const char *function_name
= NULL
;
603 struct lttng_userspace_probe_location_function
*function_location
;
606 assert(location
->type
== LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
607 function_location
= container_of(
608 location
, typeof(*function_location
), parent
);
610 /* Get probe location fields */
611 binary_path
= lttng_userspace_probe_location_function_get_binary_path(location
);
613 ERR("Userspace probe binary path is NULL");
617 function_name
= lttng_userspace_probe_location_function_get_function_name(location
);
618 if (!function_name
) {
619 ERR("Userspace probe function name is NULL");
624 * Duplicate probe location method fields
626 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(
627 location
->lookup_method
);
628 switch (lookup_type
) {
629 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
631 lttng_userspace_probe_location_lookup_method_function_elf_copy(
632 location
->lookup_method
);
633 if (!lookup_method
) {
638 /* Invalid probe location lookup method. */
642 /* Create the probe_location */
643 new_location
= lttng_userspace_probe_location_function_create_no_check(
644 binary_path
, function_name
, lookup_method
, false);
646 goto destroy_lookup_method
;
649 /* Set the duplicated fd to the new probe_location */
650 if (lttng_userspace_probe_location_function_set_binary_fd_handle(new_location
,
651 function_location
->binary_fd_handle
) < 0) {
652 goto destroy_probe_location
;
657 destroy_probe_location
:
658 lttng_userspace_probe_location_destroy(new_location
);
659 destroy_lookup_method
:
660 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
667 static struct lttng_userspace_probe_location
*
668 lttng_userspace_probe_location_tracepoint_copy(
669 const struct lttng_userspace_probe_location
*location
)
671 enum lttng_userspace_probe_location_lookup_method_type lookup_type
;
672 struct lttng_userspace_probe_location
*new_location
= NULL
;
673 struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
674 const char *binary_path
= NULL
;
675 const char *probe_name
= NULL
;
676 const char *provider_name
= NULL
;
677 struct lttng_userspace_probe_location_tracepoint
*tracepoint_location
;
680 assert(location
->type
== LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
681 tracepoint_location
= container_of(
682 location
, typeof(*tracepoint_location
), parent
);
684 /* Get probe location fields */
685 binary_path
= lttng_userspace_probe_location_tracepoint_get_binary_path(location
);
687 ERR("Userspace probe binary path is NULL");
691 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(location
);
693 ERR("Userspace probe probe name is NULL");
697 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(location
);
698 if (!provider_name
) {
699 ERR("Userspace probe provider name is NULL");
704 * Duplicate probe location method fields
706 lookup_type
= lttng_userspace_probe_location_lookup_method_get_type(
707 location
->lookup_method
);
708 switch (lookup_type
) {
709 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
711 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_copy(
712 location
->lookup_method
);
713 if (!lookup_method
) {
718 /* Invalid probe location lookup method. */
722 /* Create the probe_location */
723 new_location
= lttng_userspace_probe_location_tracepoint_create_no_check(
724 binary_path
, provider_name
, probe_name
, lookup_method
, false);
726 goto destroy_lookup_method
;
729 /* Set the duplicated fd to the new probe_location */
730 if (lttng_userspace_probe_location_tracepoint_set_binary_fd_handle(new_location
,
731 tracepoint_location
->binary_fd_handle
) < 0) {
732 goto destroy_probe_location
;
737 destroy_probe_location
:
738 lttng_userspace_probe_location_destroy(new_location
);
739 destroy_lookup_method
:
740 lttng_userspace_probe_location_lookup_method_destroy(lookup_method
);
747 const char *lttng_userspace_probe_location_function_get_binary_path(
748 const struct lttng_userspace_probe_location
*location
)
750 const char *ret
= NULL
;
751 struct lttng_userspace_probe_location_function
*function_location
;
753 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
754 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
) {
755 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
759 function_location
= container_of(location
,
760 struct lttng_userspace_probe_location_function
,
762 ret
= function_location
->binary_path
;
767 const char *lttng_userspace_probe_location_tracepoint_get_binary_path(
768 const struct lttng_userspace_probe_location
*location
)
770 const char *ret
= NULL
;
771 struct lttng_userspace_probe_location_tracepoint
*tracepoint_location
;
773 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
774 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
) {
775 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
779 tracepoint_location
= container_of(location
,
780 struct lttng_userspace_probe_location_tracepoint
,
782 ret
= tracepoint_location
->binary_path
;
787 const char *lttng_userspace_probe_location_function_get_function_name(
788 const struct lttng_userspace_probe_location
*location
)
790 const char *ret
= NULL
;
791 struct lttng_userspace_probe_location_function
*function_location
;
793 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
794 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
) {
795 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
799 function_location
= container_of(location
,
800 struct lttng_userspace_probe_location_function
, parent
);
801 ret
= function_location
->function_name
;
806 const char *lttng_userspace_probe_location_tracepoint_get_probe_name(
807 const struct lttng_userspace_probe_location
*location
)
809 const char *ret
= NULL
;
810 struct lttng_userspace_probe_location_tracepoint
*tracepoint_location
;
812 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
813 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
) {
814 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
818 tracepoint_location
= container_of(location
,
819 struct lttng_userspace_probe_location_tracepoint
, parent
);
820 ret
= tracepoint_location
->probe_name
;
825 const char *lttng_userspace_probe_location_tracepoint_get_provider_name(
826 const struct lttng_userspace_probe_location
*location
)
828 const char *ret
= NULL
;
829 struct lttng_userspace_probe_location_tracepoint
*tracepoint_location
;
831 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
832 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
) {
833 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
837 tracepoint_location
= container_of(location
,
838 struct lttng_userspace_probe_location_tracepoint
, parent
);
839 ret
= tracepoint_location
->provider_name
;
844 int lttng_userspace_probe_location_function_get_binary_fd(
845 const struct lttng_userspace_probe_location
*location
)
848 struct lttng_userspace_probe_location_function
*function_location
;
850 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
851 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
) {
852 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
856 function_location
= container_of(location
,
857 struct lttng_userspace_probe_location_function
, parent
);
858 ret
= function_location
->binary_fd_handle
?
859 fd_handle_get_fd(function_location
->binary_fd_handle
) : -1;
864 enum lttng_userspace_probe_location_function_instrumentation_type
865 lttng_userspace_probe_location_function_get_instrumentation_type(
866 const struct lttng_userspace_probe_location
*location
)
868 enum lttng_userspace_probe_location_function_instrumentation_type type
;
869 struct lttng_userspace_probe_location_function
*function_location
;
871 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
872 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
) {
873 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
874 type
= LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_UNKNOWN
;
878 function_location
= container_of(location
,
879 struct lttng_userspace_probe_location_function
, parent
);
880 type
= function_location
->instrumentation_type
;
885 enum lttng_userspace_probe_location_status
886 lttng_userspace_probe_location_function_set_instrumentation_type(
887 const struct lttng_userspace_probe_location
*location
,
888 enum lttng_userspace_probe_location_function_instrumentation_type instrumentation_type
)
890 enum lttng_userspace_probe_location_status status
=
891 LTTNG_USERSPACE_PROBE_LOCATION_STATUS_OK
;
892 struct lttng_userspace_probe_location_function
*function_location
;
894 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
895 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
||
896 instrumentation_type
!=
897 LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_ENTRY
) {
898 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
899 status
= LTTNG_USERSPACE_PROBE_LOCATION_STATUS_INVALID
;
903 function_location
= container_of(location
,
904 struct lttng_userspace_probe_location_function
, parent
);
905 function_location
->instrumentation_type
= instrumentation_type
;
910 int lttng_userspace_probe_location_tracepoint_get_binary_fd(
911 const struct lttng_userspace_probe_location
*location
)
914 struct lttng_userspace_probe_location_tracepoint
*tracepoint_location
;
916 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
917 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
) {
918 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
922 tracepoint_location
= container_of(location
,
923 struct lttng_userspace_probe_location_tracepoint
, parent
);
924 ret
= tracepoint_location
->binary_fd_handle
?
925 fd_handle_get_fd(tracepoint_location
->binary_fd_handle
) : -1;
930 static struct lttng_userspace_probe_location_lookup_method
*
931 lttng_userspace_probe_location_function_get_lookup_method(
932 const struct lttng_userspace_probe_location
*location
)
934 struct lttng_userspace_probe_location_lookup_method
*ret
= NULL
;
936 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
937 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
) {
938 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
942 ret
= location
->lookup_method
;
947 static struct lttng_userspace_probe_location_lookup_method
*
948 lttng_userspace_probe_location_tracepoint_get_lookup_method(
949 const struct lttng_userspace_probe_location
*location
)
951 struct lttng_userspace_probe_location_lookup_method
*ret
= NULL
;
953 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
954 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
) {
955 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
959 ret
= location
->lookup_method
;
964 const struct lttng_userspace_probe_location_lookup_method
*
965 lttng_userspace_probe_location_get_lookup_method(
966 const struct lttng_userspace_probe_location
*location
)
968 struct lttng_userspace_probe_location_lookup_method
*ret
= NULL
;
971 switch (location
->type
) {
972 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
973 ret
= lttng_userspace_probe_location_function_get_lookup_method(
976 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
977 ret
= lttng_userspace_probe_location_tracepoint_get_lookup_method(
981 ERR("Unknowned lookup method.");
988 int lttng_userspace_probe_location_lookup_method_serialize(
989 struct lttng_userspace_probe_location_lookup_method
*method
,
990 struct lttng_payload
*payload
)
993 struct lttng_userspace_probe_location_lookup_method_comm
996 lookup_method_comm
.type
= (int8_t) (method
? method
->type
:
997 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
);
999 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &lookup_method_comm
,
1000 sizeof(lookup_method_comm
));
1005 ret
= sizeof(lookup_method_comm
);
1011 int lttng_userspace_probe_location_function_serialize(
1012 const struct lttng_userspace_probe_location
*location
,
1013 struct lttng_payload
*payload
)
1016 size_t function_name_len
, binary_path_len
;
1017 struct lttng_userspace_probe_location_function
*location_function
;
1018 struct lttng_userspace_probe_location_function_comm location_function_comm
;
1021 assert(lttng_userspace_probe_location_get_type(location
) ==
1022 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
1024 location_function
= container_of(location
,
1025 struct lttng_userspace_probe_location_function
,
1027 if (!location_function
->function_name
|| !location_function
->binary_path
) {
1028 ret
= -LTTNG_ERR_INVALID
;
1032 if (payload
&& !location_function
->binary_fd_handle
) {
1033 ret
= -LTTNG_ERR_INVALID
;
1037 function_name_len
= strlen(location_function
->function_name
);
1038 if (function_name_len
== 0) {
1039 ret
= -LTTNG_ERR_INVALID
;
1042 binary_path_len
= strlen(location_function
->binary_path
);
1043 if (binary_path_len
== 0) {
1044 ret
= -LTTNG_ERR_INVALID
;
1048 location_function_comm
.function_name_len
= function_name_len
+ 1;
1049 location_function_comm
.binary_path_len
= binary_path_len
+ 1;
1052 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
1053 &location_function_comm
,
1054 sizeof(location_function_comm
));
1056 ret
= -LTTNG_ERR_INVALID
;
1059 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
1060 location_function
->function_name
,
1061 location_function_comm
.function_name_len
);
1063 ret
= -LTTNG_ERR_INVALID
;
1066 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
1067 location_function
->binary_path
,
1068 location_function_comm
.binary_path_len
);
1070 ret
= -LTTNG_ERR_INVALID
;
1073 ret
= lttng_payload_push_fd_handle(
1074 payload
, location_function
->binary_fd_handle
);
1076 ret
= -LTTNG_ERR_INVALID
;
1080 ret
= sizeof(location_function_comm
) +
1081 location_function_comm
.function_name_len
+
1082 location_function_comm
.binary_path_len
;
1088 int lttng_userspace_probe_location_tracepoint_serialize(
1089 const struct lttng_userspace_probe_location
*location
,
1090 struct lttng_payload
*payload
)
1093 size_t probe_name_len
, provider_name_len
, binary_path_len
;
1094 struct lttng_userspace_probe_location_tracepoint
*location_tracepoint
;
1095 struct lttng_userspace_probe_location_tracepoint_comm location_tracepoint_comm
;
1098 assert(lttng_userspace_probe_location_get_type(location
) ==
1099 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
1101 location_tracepoint
= container_of(location
,
1102 struct lttng_userspace_probe_location_tracepoint
,
1104 if (!location_tracepoint
->probe_name
||
1105 !location_tracepoint
->provider_name
||
1106 !location_tracepoint
->binary_path
) {
1107 ret
= -LTTNG_ERR_INVALID
;
1111 if (payload
&& !location_tracepoint
->binary_fd_handle
) {
1112 ret
= -LTTNG_ERR_INVALID
;
1116 probe_name_len
= strlen(location_tracepoint
->probe_name
);
1117 if (probe_name_len
== 0) {
1118 ret
= -LTTNG_ERR_INVALID
;
1122 provider_name_len
= strlen(location_tracepoint
->provider_name
);
1123 if (provider_name_len
== 0) {
1124 ret
= -LTTNG_ERR_INVALID
;
1128 binary_path_len
= strlen(location_tracepoint
->binary_path
);
1129 if (binary_path_len
== 0) {
1130 ret
= -LTTNG_ERR_INVALID
;
1134 location_tracepoint_comm
.probe_name_len
= probe_name_len
+ 1;
1135 location_tracepoint_comm
.provider_name_len
= provider_name_len
+ 1;
1136 location_tracepoint_comm
.binary_path_len
= binary_path_len
+ 1;
1139 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
1140 &location_tracepoint_comm
,
1141 sizeof(location_tracepoint_comm
));
1143 ret
= -LTTNG_ERR_INVALID
;
1146 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
1147 location_tracepoint
->probe_name
,
1148 location_tracepoint_comm
.probe_name_len
);
1150 ret
= -LTTNG_ERR_INVALID
;
1153 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
1154 location_tracepoint
->provider_name
,
1155 location_tracepoint_comm
.provider_name_len
);
1157 ret
= -LTTNG_ERR_INVALID
;
1160 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
1161 location_tracepoint
->binary_path
,
1162 location_tracepoint_comm
.binary_path_len
);
1164 ret
= -LTTNG_ERR_INVALID
;
1167 ret
= lttng_payload_push_fd_handle(
1168 payload
, location_tracepoint
->binary_fd_handle
);
1170 ret
= -LTTNG_ERR_INVALID
;
1175 ret
= sizeof(location_tracepoint_comm
) +
1176 location_tracepoint_comm
.probe_name_len
+
1177 location_tracepoint_comm
.provider_name_len
+
1178 location_tracepoint_comm
.binary_path_len
;
1184 int lttng_userspace_probe_location_serialize(
1185 const struct lttng_userspace_probe_location
*location
,
1186 struct lttng_payload
*payload
)
1188 int ret
, buffer_use
= 0;
1189 struct lttng_userspace_probe_location_comm location_generic_comm
;
1192 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__
);
1193 ret
= -LTTNG_ERR_INVALID
;
1197 memset(&location_generic_comm
, 0, sizeof(location_generic_comm
));
1199 location_generic_comm
.type
= (int8_t) location
->type
;
1201 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
1202 &location_generic_comm
,
1203 sizeof(location_generic_comm
));
1208 buffer_use
+= sizeof(location_generic_comm
);
1210 switch (lttng_userspace_probe_location_get_type(location
)) {
1211 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
1212 ret
= lttng_userspace_probe_location_function_serialize(
1215 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
1216 ret
= lttng_userspace_probe_location_tracepoint_serialize(
1220 ERR("Unsupported probe location type");
1221 ret
= -LTTNG_ERR_INVALID
;
1229 ret
= lttng_userspace_probe_location_lookup_method_serialize(
1230 location
->lookup_method
, payload
);
1240 int lttng_userspace_probe_location_function_create_from_payload(
1241 struct lttng_payload_view
*view
,
1242 struct lttng_userspace_probe_location
**location
)
1244 struct lttng_userspace_probe_location_function_comm
*location_function_comm
;
1245 const char *function_name_src
, *binary_path_src
;
1246 char *function_name
= NULL
, *binary_path
= NULL
;
1248 size_t expected_size
;
1249 struct fd_handle
*binary_fd_handle
= lttng_payload_view_pop_fd_handle(view
);
1253 if (view
->buffer
.size
< sizeof(*location_function_comm
)) {
1254 ret
= -LTTNG_ERR_INVALID
;
1258 location_function_comm
=
1259 (typeof(location_function_comm
)) view
->buffer
.data
;
1261 expected_size
= sizeof(*location_function_comm
) +
1262 location_function_comm
->function_name_len
+
1263 location_function_comm
->binary_path_len
;
1265 if (view
->buffer
.size
< expected_size
) {
1266 ret
= -LTTNG_ERR_INVALID
;
1270 function_name_src
= view
->buffer
.data
+ sizeof(*location_function_comm
);
1271 binary_path_src
= function_name_src
+
1272 location_function_comm
->function_name_len
;
1274 if (!lttng_buffer_view_contains_string(&view
->buffer
, function_name_src
,
1275 location_function_comm
->function_name_len
)) {
1276 ret
= -LTTNG_ERR_INVALID
;
1280 if (!lttng_buffer_view_contains_string(&view
->buffer
, binary_path_src
,
1281 location_function_comm
->binary_path_len
)) {
1282 ret
= -LTTNG_ERR_INVALID
;
1286 function_name
= lttng_strndup(function_name_src
, LTTNG_SYMBOL_NAME_LEN
);
1287 if (!function_name
) {
1288 PERROR("lttng_strndup");
1289 ret
= -LTTNG_ERR_NOMEM
;
1293 binary_path
= lttng_strndup(binary_path_src
, LTTNG_PATH_MAX
);
1295 PERROR("lttng_strndup");
1296 ret
= -LTTNG_ERR_NOMEM
;
1300 *location
= lttng_userspace_probe_location_function_create_no_check(
1301 binary_path
, function_name
, NULL
, false);
1303 ret
= -LTTNG_ERR_INVALID
;
1307 ret
= lttng_userspace_probe_location_function_set_binary_fd_handle(
1308 *location
, binary_fd_handle
);
1310 ret
= -LTTNG_ERR_INVALID
;
1314 ret
= (int) expected_size
;
1316 fd_handle_put(binary_fd_handle
);
1317 free(function_name
);
1323 int lttng_userspace_probe_location_tracepoint_create_from_payload(
1324 struct lttng_payload_view
*view
,
1325 struct lttng_userspace_probe_location
**location
)
1327 struct lttng_userspace_probe_location_tracepoint_comm
*location_tracepoint_comm
;
1328 const char *probe_name_src
, *provider_name_src
, *binary_path_src
;
1329 char *probe_name
= NULL
, *provider_name
= NULL
, *binary_path
= NULL
;
1331 size_t expected_size
;
1332 struct fd_handle
*binary_fd_handle
= lttng_payload_view_pop_fd_handle(view
);
1336 if (!binary_fd_handle
) {
1337 ret
= -LTTNG_ERR_INVALID
;
1341 if (view
->buffer
.size
< sizeof(*location_tracepoint_comm
)) {
1342 ret
= -LTTNG_ERR_INVALID
;
1346 location_tracepoint_comm
=
1347 (typeof(location_tracepoint_comm
)) view
->buffer
.data
;
1349 expected_size
= sizeof(*location_tracepoint_comm
) +
1350 location_tracepoint_comm
->probe_name_len
+
1351 location_tracepoint_comm
->provider_name_len
+
1352 location_tracepoint_comm
->binary_path_len
;
1354 if (view
->buffer
.size
< expected_size
) {
1355 ret
= -LTTNG_ERR_INVALID
;
1359 probe_name_src
= view
->buffer
.data
+ sizeof(*location_tracepoint_comm
);
1360 provider_name_src
= probe_name_src
+
1361 location_tracepoint_comm
->probe_name_len
;
1362 binary_path_src
= provider_name_src
+
1363 location_tracepoint_comm
->provider_name_len
;
1365 if (!lttng_buffer_view_contains_string(&view
->buffer
, probe_name_src
,
1366 location_tracepoint_comm
->probe_name_len
)) {
1367 ret
= -LTTNG_ERR_INVALID
;
1371 if (!lttng_buffer_view_contains_string(&view
->buffer
, provider_name_src
,
1372 location_tracepoint_comm
->provider_name_len
)) {
1373 ret
= -LTTNG_ERR_INVALID
;
1377 if (!lttng_buffer_view_contains_string(&view
->buffer
, binary_path_src
,
1378 location_tracepoint_comm
->binary_path_len
)) {
1379 ret
= -LTTNG_ERR_INVALID
;
1383 probe_name
= lttng_strndup(probe_name_src
, LTTNG_SYMBOL_NAME_LEN
);
1385 PERROR("lttng_strndup");
1388 provider_name
= lttng_strndup(provider_name_src
, LTTNG_SYMBOL_NAME_LEN
);
1389 if (!provider_name
) {
1390 PERROR("lttng_strndup");
1394 binary_path
= lttng_strndup(binary_path_src
, LTTNG_SYMBOL_NAME_LEN
);
1396 PERROR("lttng_strndup");
1400 *location
= lttng_userspace_probe_location_tracepoint_create_no_check(
1401 binary_path
, provider_name
, probe_name
, NULL
, false);
1403 ret
= -LTTNG_ERR_INVALID
;
1407 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd_handle(
1408 *location
, binary_fd_handle
);
1410 ret
= -LTTNG_ERR_INVALID
;
1414 ret
= (int) expected_size
;
1416 fd_handle_put(binary_fd_handle
);
1418 free(provider_name
);
1424 int lttng_userspace_probe_location_lookup_method_create_from_payload(
1425 struct lttng_payload_view
*view
,
1426 struct lttng_userspace_probe_location_lookup_method
**lookup_method
)
1429 struct lttng_userspace_probe_location_lookup_method_comm
*lookup_comm
;
1430 enum lttng_userspace_probe_location_lookup_method_type type
;
1433 assert(lookup_method
);
1435 if (view
->buffer
.size
< sizeof(*lookup_comm
)) {
1436 ret
= -LTTNG_ERR_INVALID
;
1440 lookup_comm
= (typeof(lookup_comm
)) view
->buffer
.data
;
1441 type
= (enum lttng_userspace_probe_location_lookup_method_type
)
1444 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
1445 *lookup_method
= NULL
;
1447 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
1449 lttng_userspace_probe_location_lookup_method_function_elf_create();
1450 if (!(*lookup_method
)) {
1451 ret
= -LTTNG_ERR_INVALID
;
1455 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
1457 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
1458 if (!(*lookup_method
)) {
1459 ret
= -LTTNG_ERR_INVALID
;
1464 ret
= -LTTNG_ERR_INVALID
;
1468 ret
= sizeof(*lookup_comm
);
1474 int lttng_userspace_probe_location_create_from_payload(
1475 struct lttng_payload_view
*view
,
1476 struct lttng_userspace_probe_location
**location
)
1478 struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
1479 enum lttng_userspace_probe_location_type type
;
1482 struct lttng_userspace_probe_location_comm
*probe_location_comm
;
1483 struct lttng_payload_view probe_location_comm_view
=
1484 lttng_payload_view_from_view(
1485 view
, 0, sizeof(*probe_location_comm
));
1490 lookup_method
= NULL
;
1492 if (!lttng_payload_view_is_valid(&probe_location_comm_view
)) {
1493 ret
= -LTTNG_ERR_INVALID
;
1497 probe_location_comm
= (typeof(probe_location_comm
)) probe_location_comm_view
.buffer
.data
;
1498 type
= (enum lttng_userspace_probe_location_type
) probe_location_comm
->type
;
1499 consumed
+= sizeof(*probe_location_comm
);
1502 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
1504 struct lttng_payload_view location_view
=
1505 lttng_payload_view_from_view(
1506 view
, consumed
, -1);
1508 ret
= lttng_userspace_probe_location_function_create_from_payload(
1509 &location_view
, location
);
1515 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
1517 struct lttng_payload_view location_view
=
1518 lttng_payload_view_from_view(view
, consumed
, -1);
1520 ret
= lttng_userspace_probe_location_tracepoint_create_from_payload(
1521 &location_view
, location
);
1528 ret
= -LTTNG_ERR_INVALID
;
1533 if (view
->buffer
.size
<= consumed
) {
1534 ret
= -LTTNG_ERR_INVALID
;
1539 struct lttng_payload_view lookup_method_view
=
1540 lttng_payload_view_from_view(
1541 view
, consumed
, -1);
1543 ret
= lttng_userspace_probe_location_lookup_method_create_from_payload(
1544 &lookup_method_view
, &lookup_method
);
1547 ret
= -LTTNG_ERR_INVALID
;
1551 assert(lookup_method
);
1552 (*location
)->lookup_method
= lookup_method
;
1553 lookup_method
= NULL
;
1560 int lttng_userspace_probe_location_function_set_binary_fd_handle(
1561 struct lttng_userspace_probe_location
*location
,
1562 struct fd_handle
*binary_fd
)
1565 struct lttng_userspace_probe_location_function
*function_location
;
1568 assert(location
->type
== LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
1570 function_location
= container_of(location
,
1571 struct lttng_userspace_probe_location_function
, parent
);
1572 fd_handle_put(function_location
->binary_fd_handle
);
1573 fd_handle_get(binary_fd
);
1574 function_location
->binary_fd_handle
= binary_fd
;
1579 int lttng_userspace_probe_location_tracepoint_set_binary_fd_handle(
1580 struct lttng_userspace_probe_location
*location
,
1581 struct fd_handle
*binary_fd
)
1584 struct lttng_userspace_probe_location_tracepoint
*tracepoint_location
;
1587 assert(location
->type
== LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
1589 tracepoint_location
= container_of(location
,
1590 struct lttng_userspace_probe_location_tracepoint
, parent
);
1591 fd_handle_put(tracepoint_location
->binary_fd_handle
);
1592 fd_handle_get(binary_fd
);
1593 tracepoint_location
->binary_fd_handle
= binary_fd
;
1598 int lttng_userspace_probe_location_function_flatten(
1599 const struct lttng_userspace_probe_location
*location
,
1600 struct lttng_dynamic_buffer
*buffer
)
1602 struct lttng_userspace_probe_location_lookup_method_elf flat_lookup_method
;
1603 struct lttng_userspace_probe_location_function
*probe_function
;
1604 struct lttng_userspace_probe_location_function flat_probe
;
1605 size_t function_name_len
, binary_path_len
;
1606 size_t padding_needed
= 0;
1607 char *flat_probe_start
;
1608 int storage_needed
= 0;
1613 if (location
->lookup_method
&& location
->lookup_method
->type
!=
1614 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
) {
1615 ret
= -LTTNG_ERR_INVALID
;
1619 probe_function
= container_of(location
,
1620 struct lttng_userspace_probe_location_function
,
1622 assert(probe_function
->function_name
);
1623 assert(probe_function
->binary_path
);
1626 sizeof(struct lttng_userspace_probe_location_function
);
1627 function_name_len
= strlen(probe_function
->function_name
) + 1;
1628 binary_path_len
= strlen(probe_function
->binary_path
) + 1;
1629 storage_needed
+= function_name_len
+ binary_path_len
;
1632 * The lookup method is aligned to 64-bit within the buffer.
1633 * This is needed even if there is no lookup method since
1634 * the next structure in the buffer probably needs to be
1635 * aligned too (depending on the arch).
1637 padding_needed
= ALIGN_TO(storage_needed
, sizeof(uint64_t)) - storage_needed
;
1638 storage_needed
+= padding_needed
;
1640 if (location
->lookup_method
) {
1641 /* NOTE: elf look-up method is assumed here. */
1642 storage_needed
+= sizeof(struct lttng_userspace_probe_location_lookup_method_elf
);
1646 ret
= storage_needed
;
1650 if (lttng_dynamic_buffer_get_capacity_left(buffer
) < storage_needed
) {
1651 ret
= lttng_dynamic_buffer_set_capacity(buffer
,
1652 buffer
->size
+ storage_needed
);
1658 memset(&flat_probe
, 0, sizeof(flat_probe
));
1660 flat_probe_start
= buffer
->data
+ buffer
->size
;
1661 flat_probe
.parent
.type
= location
->type
;
1663 * The lookup method, if present, is the last element in the flat
1664 * representation of the probe.
1666 if (location
->lookup_method
) {
1667 flat_probe
.parent
.lookup_method
=
1668 (struct lttng_userspace_probe_location_lookup_method
*)
1669 (flat_probe_start
+ sizeof(flat_probe
) +
1670 function_name_len
+ binary_path_len
+ padding_needed
);
1672 flat_probe
.parent
.lookup_method
= NULL
;
1675 flat_probe
.function_name
= flat_probe_start
+ sizeof(flat_probe
);
1676 flat_probe
.binary_path
= flat_probe
.function_name
+ function_name_len
;
1677 flat_probe
.binary_fd_handle
= NULL
;
1678 ret
= lttng_dynamic_buffer_append(buffer
, &flat_probe
,
1679 sizeof(flat_probe
));
1684 ret
= lttng_dynamic_buffer_append(buffer
,
1685 probe_function
->function_name
, function_name_len
);
1689 ret
= lttng_dynamic_buffer_append(buffer
,
1690 probe_function
->binary_path
, binary_path_len
);
1695 /* Insert padding before the lookup method. */
1696 ret
= lttng_dynamic_buffer_set_size(buffer
,
1697 buffer
->size
+ padding_needed
);
1702 if (!location
->lookup_method
) {
1703 /* Not an error, the default method is used. */
1704 ret
= storage_needed
;
1708 memset(&flat_lookup_method
, 0, sizeof(flat_lookup_method
));
1709 flat_lookup_method
.parent
.type
=
1710 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
;
1711 ret
= lttng_dynamic_buffer_append(buffer
,
1712 &flat_lookup_method
, sizeof(flat_lookup_method
));
1716 ret
= storage_needed
;
1722 int lttng_userspace_probe_location_tracepoint_flatten(
1723 const struct lttng_userspace_probe_location
*location
,
1724 struct lttng_dynamic_buffer
*buffer
)
1726 struct lttng_userspace_probe_location_lookup_method_sdt flat_lookup_method
;
1727 struct lttng_userspace_probe_location_tracepoint
*probe_tracepoint
;
1728 struct lttng_userspace_probe_location_tracepoint flat_probe
;
1729 size_t probe_name_len
, provider_name_len
, binary_path_len
;
1730 size_t padding_needed
= 0;
1731 int storage_needed
= 0;
1732 char *flat_probe_start
;
1737 /* Only SDT tracepoints are supported at the moment */
1738 if (location
->lookup_method
&& location
->lookup_method
->type
!=
1739 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
) {
1740 ret
= -LTTNG_ERR_INVALID
;
1743 probe_tracepoint
= container_of(location
,
1744 struct lttng_userspace_probe_location_tracepoint
,
1746 assert(probe_tracepoint
->probe_name
);
1747 assert(probe_tracepoint
->provider_name
);
1748 assert(probe_tracepoint
->binary_path
);
1750 /* Compute the storage space needed to flatten the probe location */
1751 storage_needed
+= sizeof(struct lttng_userspace_probe_location_tracepoint
);
1753 probe_name_len
= strlen(probe_tracepoint
->probe_name
) + 1;
1754 provider_name_len
= strlen(probe_tracepoint
->provider_name
) + 1;
1755 binary_path_len
= strlen(probe_tracepoint
->binary_path
) + 1;
1757 storage_needed
+= probe_name_len
+ provider_name_len
+ binary_path_len
;
1760 * The lookup method is aligned to 64-bit within the buffer.
1761 * This is needed even if there is no lookup method since
1762 * the next structure in the buffer probably needs to be
1763 * aligned too (depending on the arch).
1765 padding_needed
= ALIGN_TO(storage_needed
, sizeof(uint64_t)) - storage_needed
;
1766 storage_needed
+= padding_needed
;
1768 if (location
->lookup_method
) {
1769 /* NOTE: elf look-up method is assumed here. */
1771 sizeof(struct lttng_userspace_probe_location_lookup_method_elf
);
1775 * If the caller set buffer to NULL, return the size of the needed buffer.
1778 ret
= storage_needed
;
1782 if (lttng_dynamic_buffer_get_capacity_left(buffer
) < storage_needed
) {
1783 ret
= lttng_dynamic_buffer_set_capacity(buffer
,
1784 buffer
->size
+ storage_needed
);
1790 memset(&flat_probe
, 0, sizeof(flat_probe
));
1792 flat_probe_start
= buffer
->data
+ buffer
->size
;
1793 flat_probe
.parent
.type
= location
->type
;
1796 * The lookup method, if present, is the last element in the flat
1797 * representation of the probe.
1799 if (location
->lookup_method
) {
1800 flat_probe
.parent
.lookup_method
=
1801 (struct lttng_userspace_probe_location_lookup_method
*)
1802 (flat_probe_start
+ sizeof(flat_probe
) +
1803 probe_name_len
+ provider_name_len
+
1804 binary_path_len
+ padding_needed
);
1806 flat_probe
.parent
.lookup_method
= NULL
;
1809 flat_probe
.probe_name
= flat_probe_start
+ sizeof(flat_probe
);
1810 flat_probe
.provider_name
= flat_probe
.probe_name
+ probe_name_len
;
1811 flat_probe
.binary_path
= flat_probe
.provider_name
+ provider_name_len
;
1812 flat_probe
.binary_fd_handle
= NULL
;
1813 ret
= lttng_dynamic_buffer_append(buffer
, &flat_probe
, sizeof(flat_probe
));
1818 /* Append all the fields to the buffer */
1819 ret
= lttng_dynamic_buffer_append(buffer
,
1820 probe_tracepoint
->probe_name
, probe_name_len
);
1824 ret
= lttng_dynamic_buffer_append(buffer
,
1825 probe_tracepoint
->provider_name
, provider_name_len
);
1829 ret
= lttng_dynamic_buffer_append(buffer
,
1830 probe_tracepoint
->binary_path
, binary_path_len
);
1835 /* Insert padding before the lookup method. */
1836 ret
= lttng_dynamic_buffer_set_size(buffer
, buffer
->size
+ padding_needed
);
1841 if (!location
->lookup_method
) {
1842 /* Not an error, the default method is used. */
1843 ret
= storage_needed
;
1847 memset(&flat_lookup_method
, 0, sizeof(flat_lookup_method
));
1849 flat_lookup_method
.parent
.type
=
1850 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
;
1851 ret
= lttng_dynamic_buffer_append(buffer
,
1852 &flat_lookup_method
, sizeof(flat_lookup_method
));
1856 ret
= storage_needed
;
1862 int lttng_userspace_probe_location_flatten(
1863 const struct lttng_userspace_probe_location
*location
,
1864 struct lttng_dynamic_buffer
*buffer
)
1868 ret
= -LTTNG_ERR_INVALID
;
1872 /* Only types currently supported. */
1873 switch (location
->type
) {
1874 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
1875 ret
= lttng_userspace_probe_location_function_flatten(location
, buffer
);
1877 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
1878 ret
= lttng_userspace_probe_location_tracepoint_flatten(location
, buffer
);
1881 ret
= -LTTNG_ERR_INVALID
;
1890 struct lttng_userspace_probe_location
*lttng_userspace_probe_location_copy(
1891 const struct lttng_userspace_probe_location
*location
)
1893 struct lttng_userspace_probe_location
*new_location
= NULL
;
1894 enum lttng_userspace_probe_location_type type
;
1900 type
= lttng_userspace_probe_location_get_type(location
);
1902 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
1904 lttng_userspace_probe_location_function_copy(location
);
1905 if (!new_location
) {
1909 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
1911 lttng_userspace_probe_location_tracepoint_copy(location
);
1912 if (!new_location
) {
1917 new_location
= NULL
;
1921 return new_location
;
1925 bool lttng_userspace_probe_location_lookup_method_is_equal(
1926 const struct lttng_userspace_probe_location_lookup_method
*a
,
1927 const struct lttng_userspace_probe_location_lookup_method
*b
)
1929 bool is_equal
= false;
1940 if (a
->type
!= b
->type
) {
1950 bool lttng_userspace_probe_location_is_equal(
1951 const struct lttng_userspace_probe_location
*a
,
1952 const struct lttng_userspace_probe_location
*b
)
1954 bool is_equal
= false;
1965 if (!lttng_userspace_probe_location_lookup_method_is_equal(
1966 a
->lookup_method
, b
->lookup_method
)) {
1970 if (a
->type
!= b
->type
) {
1974 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
1980 unsigned long lttng_userspace_probe_location_hash(
1981 const struct lttng_userspace_probe_location
*location
)
1983 return location
->hash(location
);
1987 enum lttng_error_code
lttng_userspace_probe_location_mi_serialize(
1988 const struct lttng_userspace_probe_location
*location
,
1989 struct mi_writer
*writer
)
1991 typedef enum lttng_error_code (*mi_fp
)(
1992 const struct lttng_userspace_probe_location
*,
1993 struct mi_writer
*);
1996 enum lttng_error_code ret_code
;
1997 mi_fp mi_function
= NULL
;
2002 switch (lttng_userspace_probe_location_get_type(location
)) {
2003 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
2004 mi_function
= lttng_userspace_probe_location_function_mi_serialize
;
2006 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
:
2007 mi_function
= lttng_userspace_probe_location_tracepoint_mi_serialize
;
2014 /* Open userspace probe location element. */
2015 ret
= mi_lttng_writer_open_element(
2016 writer
, mi_lttng_element_userspace_probe_location
);
2021 /* Underlying user space probe location. */
2022 ret_code
= mi_function(location
, writer
);
2023 if (ret_code
!= LTTNG_OK
) {
2027 /* Close userspace probe location element. */
2028 ret
= mi_lttng_writer_close_element(writer
);
2033 ret_code
= LTTNG_OK
;
2037 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
2042 enum lttng_error_code
lttng_userspace_probe_location_lookup_method_mi_serialize(
2043 const struct lttng_userspace_probe_location_lookup_method
2045 struct mi_writer
*writer
)
2048 enum lttng_error_code ret_code
;
2049 const char *type_element_str
;
2054 switch (lttng_userspace_probe_location_lookup_method_get_type(method
)) {
2055 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
2057 mi_lttng_element_userspace_probe_location_lookup_method_function_default
;
2059 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
2061 mi_lttng_element_userspace_probe_location_lookup_method_function_elf
;
2063 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
2065 mi_lttng_element_userspace_probe_location_lookup_method_tracepoint_sdt
;
2072 /* Open userspace probe location lookup method element. */
2073 ret
= mi_lttng_writer_open_element(writer
,
2074 mi_lttng_element_userspace_probe_location_lookup_method
);
2079 /* User space probe location lookup method empty element. */
2080 ret
= mi_lttng_writer_open_element(writer
, type_element_str
);
2085 /* Close userspace probe location lookup method element. */
2086 ret
= mi_lttng_close_multi_element(writer
, 2);
2091 ret_code
= LTTNG_OK
;
2095 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
2100 static enum lttng_error_code
lttng_userspace_probe_location_tracepoint_mi_serialize(
2101 const struct lttng_userspace_probe_location
*location
,
2102 struct mi_writer
*writer
)
2105 enum lttng_error_code ret_code
;
2106 const char *probe_name
= NULL
;
2107 const char *provider_name
= NULL
;
2108 const char *binary_path
= NULL
;
2109 const struct lttng_userspace_probe_location_lookup_method
2110 *lookup_method
= NULL
;
2115 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
2117 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
2119 binary_path
= lttng_userspace_probe_location_tracepoint_get_binary_path(
2121 lookup_method
= lttng_userspace_probe_location_tracepoint_get_lookup_method(
2124 /* Open userspace probe location tracepoint element. */
2125 ret
= mi_lttng_writer_open_element(writer
,
2126 mi_lttng_element_userspace_probe_location_tracepoint
);
2132 ret
= mi_lttng_writer_write_element_string(writer
,
2133 mi_lttng_element_userspace_probe_location_tracepoint_probe_name
,
2139 /* Provider name. */
2140 ret
= mi_lttng_writer_write_element_string(writer
,
2141 mi_lttng_element_userspace_probe_location_tracepoint_provider_name
,
2148 ret
= mi_lttng_writer_write_element_string(writer
,
2149 mi_lttng_element_userspace_probe_location_binary_path
,
2155 /* The lookup method. */
2156 ret_code
= lttng_userspace_probe_location_lookup_method_mi_serialize(
2157 lookup_method
, writer
);
2158 if (ret_code
!= LTTNG_OK
) {
2162 /* Close userspace probe location tracepoint. */
2163 ret
= mi_lttng_writer_close_element(writer
);
2168 ret_code
= LTTNG_OK
;
2172 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
2177 static enum lttng_error_code
lttng_userspace_probe_location_function_mi_serialize(
2178 const struct lttng_userspace_probe_location
*location
,
2179 struct mi_writer
*writer
)
2182 enum lttng_error_code ret_code
;
2183 const char *function_name
= NULL
;
2184 const char *binary_path
= NULL
;
2185 const char *instrumentation_type_str
= NULL
;
2186 enum lttng_userspace_probe_location_function_instrumentation_type
2187 instrumentation_type
;
2188 const struct lttng_userspace_probe_location_lookup_method
2189 *lookup_method
= NULL
;
2194 function_name
= lttng_userspace_probe_location_function_get_function_name(
2196 binary_path
= lttng_userspace_probe_location_function_get_binary_path(
2198 instrumentation_type
=
2199 lttng_userspace_probe_location_function_get_instrumentation_type(
2201 lookup_method
= lttng_userspace_probe_location_function_get_lookup_method(
2204 switch (instrumentation_type
) {
2205 case LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_ENTRY
:
2206 instrumentation_type_str
=
2207 mi_lttng_userspace_probe_location_function_instrumentation_type_entry
;
2214 /* Open userspace probe location function element. */
2215 ret
= mi_lttng_writer_open_element(writer
,
2216 mi_lttng_element_userspace_probe_location_function
);
2221 /* Function name. */
2222 ret
= mi_lttng_writer_write_element_string(writer
,
2223 mi_lttng_element_userspace_probe_location_function_name
,
2230 ret
= mi_lttng_writer_write_element_string(writer
,
2231 mi_lttng_element_userspace_probe_location_binary_path
,
2237 /* Instrumentation type. */
2238 ret
= mi_lttng_writer_write_element_string(writer
,
2239 mi_lttng_element_userspace_probe_location_function_instrumentation_type
,
2240 instrumentation_type_str
);
2245 /* The lookup method. */
2246 ret_code
= lttng_userspace_probe_location_lookup_method_mi_serialize(
2247 lookup_method
, writer
);
2248 if (ret_code
!= LTTNG_OK
) {
2252 /* Close userspace probe location function element. */
2253 ret
= mi_lttng_writer_close_element(writer
);
2258 ret_code
= LTTNG_OK
;
2262 ret_code
= LTTNG_ERR_MI_IO_FAIL
;