2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <common/error.h>
20 #include <common/macros.h>
21 #include <common/compat/string.h>
23 #include <lttng/constant.h>
24 #include <lttng/userspace-probe-internal.h>
26 enum lttng_userspace_probe_location_lookup_method_type
27 lttng_userspace_probe_location_lookup_method_get_type(
28 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
)
30 return lookup_method
? lookup_method
->type
:
31 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_UNKNOWN
;
34 void lttng_userspace_probe_location_lookup_method_destroy(
35 struct lttng_userspace_probe_location_lookup_method
*lookup_method
)
41 switch (lookup_method
->type
) {
42 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
44 struct lttng_userspace_probe_location_lookup_method_elf
*elf_method
=
45 container_of(lookup_method
,
46 struct lttng_userspace_probe_location_lookup_method_elf
, parent
);
55 struct lttng_userspace_probe_location_lookup_method
*
56 lttng_userspace_probe_location_lookup_method_function_elf_create(void)
58 struct lttng_userspace_probe_location_lookup_method
*ret
= NULL
;
59 struct lttng_userspace_probe_location_lookup_method_elf
*elf_method
;
61 elf_method
= zmalloc(sizeof(*elf_method
));
67 ret
= &elf_method
->parent
;
68 ret
->type
= LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
;
73 enum lttng_userspace_probe_location_type
lttng_userspace_probe_location_get_type(
74 const struct lttng_userspace_probe_location
*location
)
76 return location
? location
->type
:
77 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN
;
81 void lttng_userspace_probe_location_function_destroy(
82 struct lttng_userspace_probe_location
*location
)
84 struct lttng_userspace_probe_location_function
*location_function
= NULL
;
88 location_function
= container_of(location
,
89 struct lttng_userspace_probe_location_function
, parent
);
91 assert(location_function
);
93 free(location_function
->function_name
);
94 free(location_function
->binary_path
);
95 if (location_function
->binary_fd
>= 0) {
96 if (close(location_function
->binary_fd
)) {
103 void lttng_userspace_probe_location_destroy(
104 struct lttng_userspace_probe_location
*location
)
110 lttng_userspace_probe_location_lookup_method_destroy(
111 location
->lookup_method
);
113 switch (location
->type
) {
114 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
115 lttng_userspace_probe_location_function_destroy(location
);
122 static struct lttng_userspace_probe_location
*
123 lttng_userspace_probe_location_function_create_no_check(const char *binary_path
,
124 const char *function_name
,
125 struct lttng_userspace_probe_location_lookup_method
*lookup_method
,
129 char *function_name_copy
= NULL
, *binary_path_copy
= NULL
;
130 struct lttng_userspace_probe_location
*ret
= NULL
;
131 struct lttng_userspace_probe_location_function
*location
;
134 binary_fd
= open(binary_path
, O_RDONLY
);
136 PERROR("Error opening the binary");
143 function_name_copy
= lttng_strndup(function_name
, LTTNG_SYMBOL_NAME_LEN
);
144 if (!function_name_copy
) {
145 PERROR("Error duplicating the function name");
149 binary_path_copy
= lttng_strndup(binary_path
, LTTNG_PATH_MAX
);
150 if (!binary_path_copy
) {
151 PERROR("Error duplicating the function name");
155 location
= zmalloc(sizeof(*location
));
157 PERROR("Error allocating userspace probe location");
161 location
->function_name
= function_name_copy
;
162 location
->binary_path
= binary_path_copy
;
163 location
->binary_fd
= binary_fd
;
165 ret
= &location
->parent
;
166 ret
->lookup_method
= lookup_method
;
167 ret
->type
= LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
;
171 free(function_name_copy
);
172 free(binary_path_copy
);
173 if (binary_fd
>= 0) {
174 if (close(binary_fd
)) {
175 PERROR("Error closing binary fd in error path");
182 struct lttng_userspace_probe_location
*
183 lttng_userspace_probe_location_function_create(const char *binary_path
,
184 const char *function_name
,
185 struct lttng_userspace_probe_location_lookup_method
*lookup_method
)
187 struct lttng_userspace_probe_location
*ret
= NULL
;
189 if (!binary_path
|| !function_name
) {
190 ERR("Invalid argument(s)");
194 switch (lttng_userspace_probe_location_lookup_method_get_type(
196 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
197 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
200 /* Invalid probe location lookup method. */
204 ret
= lttng_userspace_probe_location_function_create_no_check(
205 binary_path
, function_name
, lookup_method
, true);
210 const char *lttng_userspace_probe_location_function_get_binary_path(
211 const struct lttng_userspace_probe_location
*location
)
213 const char *ret
= NULL
;
214 struct lttng_userspace_probe_location_function
*function_location
;
216 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
217 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
) {
218 ERR("Invalid argument(s)");
222 function_location
= container_of(location
,
223 struct lttng_userspace_probe_location_function
,
225 ret
= function_location
->binary_path
;
230 const char *lttng_userspace_probe_location_function_get_function_name(
231 const struct lttng_userspace_probe_location
*location
)
233 const char *ret
= NULL
;
234 struct lttng_userspace_probe_location_function
*function_location
;
236 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
237 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
) {
238 ERR("Invalid argument(s)");
242 function_location
= container_of(location
,
243 struct lttng_userspace_probe_location_function
, parent
);
244 ret
= function_location
->function_name
;
249 int lttng_userspace_probe_location_function_get_binary_fd(
250 const struct lttng_userspace_probe_location
*location
)
253 struct lttng_userspace_probe_location_function
*function_location
;
255 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
256 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
) {
257 ERR("Invalid argument(s)");
261 function_location
= container_of(location
,
262 struct lttng_userspace_probe_location_function
, parent
);
263 ret
= function_location
->binary_fd
;
268 static struct lttng_userspace_probe_location_lookup_method
*
269 lttng_userspace_probe_location_function_get_lookup_method(
270 const struct lttng_userspace_probe_location
*location
)
272 struct lttng_userspace_probe_location_lookup_method
*ret
= NULL
;
274 if (!location
|| lttng_userspace_probe_location_get_type(location
) !=
275 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
) {
276 ERR("Invalid argument(s)");
280 ret
= location
->lookup_method
;
285 struct lttng_userspace_probe_location_lookup_method
*
286 lttng_userspace_probe_location_get_lookup_method(
287 const struct lttng_userspace_probe_location
*location
)
289 struct lttng_userspace_probe_location_lookup_method
*ret
= NULL
;
292 switch (location
->type
) {
293 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
294 ret
= lttng_userspace_probe_location_function_get_lookup_method(
298 ERR("Unknowned lookup method.");
305 int lttng_userspace_probe_location_lookup_method_serialize(
306 struct lttng_userspace_probe_location_lookup_method
*method
,
307 struct lttng_dynamic_buffer
*buffer
)
310 struct lttng_userspace_probe_location_lookup_method_comm
313 lookup_method_comm
.type
= (int8_t) (method
? method
->type
:
314 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
);
316 ret
= lttng_dynamic_buffer_append(buffer
, &lookup_method_comm
,
317 sizeof(lookup_method_comm
));
322 ret
= sizeof(lookup_method_comm
);
328 int lttng_userspace_probe_location_function_serialize(
329 const struct lttng_userspace_probe_location
*location
,
330 struct lttng_dynamic_buffer
*buffer
,
334 size_t function_name_len
, binary_path_len
;
335 struct lttng_userspace_probe_location_function
*location_function
;
336 struct lttng_userspace_probe_location_function_comm location_function_comm
;
339 assert(lttng_userspace_probe_location_get_type(location
) ==
340 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
342 location_function
= container_of(location
,
343 struct lttng_userspace_probe_location_function
,
345 if (!location_function
->function_name
|| !location_function
->binary_path
) {
346 ret
= -LTTNG_ERR_INVALID
;
350 if (binary_fd
&& location_function
->binary_fd
< 0) {
351 ret
= -LTTNG_ERR_INVALID
;
356 *binary_fd
= location_function
->binary_fd
;
359 function_name_len
= strlen(location_function
->function_name
);
360 if (function_name_len
== 0) {
361 ret
= -LTTNG_ERR_INVALID
;
364 binary_path_len
= strlen(location_function
->binary_path
);
365 if (binary_path_len
== 0) {
366 ret
= -LTTNG_ERR_INVALID
;
370 location_function_comm
.function_name_len
= function_name_len
+ 1;
371 location_function_comm
.binary_path_len
= binary_path_len
+ 1;
374 ret
= lttng_dynamic_buffer_append(buffer
,
375 &location_function_comm
,
376 sizeof(location_function_comm
));
378 ret
= -LTTNG_ERR_INVALID
;
381 ret
= lttng_dynamic_buffer_append(buffer
,
382 location_function
->function_name
,
383 location_function_comm
.function_name_len
);
385 ret
= -LTTNG_ERR_INVALID
;
388 ret
= lttng_dynamic_buffer_append(buffer
,
389 location_function
->binary_path
,
390 location_function_comm
.binary_path_len
);
392 ret
= -LTTNG_ERR_INVALID
;
396 ret
= sizeof(location_function_comm
) +
397 location_function_comm
.function_name_len
+
398 location_function_comm
.binary_path_len
;
404 int lttng_userspace_probe_location_serialize(
405 const struct lttng_userspace_probe_location
*location
,
406 struct lttng_dynamic_buffer
*buffer
,
409 int ret
, buffer_use
= 0;
410 struct lttng_userspace_probe_location_comm location_generic_comm
;
413 ERR("Invalid argument(s)");
414 ret
= -LTTNG_ERR_INVALID
;
418 location_generic_comm
.type
= (int8_t) location
->type
;
420 ret
= lttng_dynamic_buffer_append(buffer
, &location_generic_comm
,
421 sizeof(location_generic_comm
));
426 buffer_use
+= sizeof(location_generic_comm
);
428 switch (lttng_userspace_probe_location_get_type(location
)) {
429 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
430 ret
= lttng_userspace_probe_location_function_serialize(
431 location
, buffer
, binary_fd
);
434 ERR("Unsupported probe location type");
435 ret
= -LTTNG_ERR_INVALID
;
443 ret
= lttng_userspace_probe_location_lookup_method_serialize(
444 location
->lookup_method
, buffer
);
454 int lttng_userspace_probe_location_function_create_from_buffer(
455 const struct lttng_buffer_view
*buffer
,
456 struct lttng_userspace_probe_location
**location
)
458 struct lttng_userspace_probe_location_function_comm
*location_function_comm
;
459 const char *function_name_src
, *binary_path_src
;
460 char *function_name
= NULL
, *binary_path
= NULL
;
464 assert(buffer
->data
);
467 location_function_comm
=
468 (struct lttng_userspace_probe_location_function_comm
*) buffer
->data
;
470 const size_t expected_size
= sizeof(*location_function_comm
) +
471 location_function_comm
->function_name_len
+
472 location_function_comm
->binary_path_len
;
474 if (buffer
->size
< expected_size
) {
475 ret
= -LTTNG_ERR_INVALID
;
479 function_name_src
= buffer
->data
+ sizeof(*location_function_comm
);
480 binary_path_src
= function_name_src
+
481 location_function_comm
->function_name_len
;
483 if (function_name_src
[location_function_comm
->function_name_len
- 1] != '\0') {
484 ret
= -LTTNG_ERR_INVALID
;
487 if (binary_path_src
[location_function_comm
->binary_path_len
- 1] != '\0') {
488 ret
= -LTTNG_ERR_INVALID
;
492 function_name
= lttng_strndup(function_name_src
, LTTNG_SYMBOL_NAME_LEN
);
493 if (!function_name
) {
494 PERROR("lttng_strndup");
498 binary_path
= lttng_strndup(binary_path_src
, LTTNG_PATH_MAX
);
500 PERROR("lttng_strndup");
504 *location
= lttng_userspace_probe_location_function_create_no_check(
505 binary_path
, function_name
, NULL
, false);
507 ret
= -LTTNG_ERR_INVALID
;
511 ret
= (int) expected_size
;
519 int lttng_userspace_probe_location_lookup_method_create_from_buffer(
520 struct lttng_buffer_view
*buffer
,
521 struct lttng_userspace_probe_location_lookup_method
**lookup_method
)
524 struct lttng_userspace_probe_location_lookup_method_comm
*lookup_comm
;
525 enum lttng_userspace_probe_location_lookup_method_type type
;
528 assert(buffer
->data
);
529 assert(lookup_method
);
531 if (buffer
->size
< sizeof(*lookup_comm
)) {
532 ret
= -LTTNG_ERR_INVALID
;
536 lookup_comm
= (struct lttng_userspace_probe_location_lookup_method_comm
*)
538 type
= (enum lttng_userspace_probe_location_lookup_method_type
)
541 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
542 *lookup_method
= NULL
;
544 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
546 lttng_userspace_probe_location_lookup_method_function_elf_create();
547 if (!(*lookup_method
)) {
548 ret
= -LTTNG_ERR_INVALID
;
553 ret
= -LTTNG_ERR_INVALID
;
557 ret
= sizeof(*lookup_comm
);
563 int lttng_userspace_probe_location_create_from_buffer(
564 const struct lttng_buffer_view
*buffer
,
565 struct lttng_userspace_probe_location
**location
)
567 struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
568 struct lttng_userspace_probe_location_comm
*probe_location_comm
;
569 enum lttng_userspace_probe_location_type type
;
570 struct lttng_buffer_view lookup_method_view
;
576 assert(buffer
->data
);
579 lookup_method
= NULL
;
581 if (buffer
->size
<= sizeof(*probe_location_comm
)) {
582 ret
= -LTTNG_ERR_INVALID
;
586 probe_location_comm
=
587 (struct lttng_userspace_probe_location_comm
*) buffer
->data
;
588 type
= (enum lttng_userspace_probe_location_type
) probe_location_comm
->type
;
589 consumed
+= sizeof(*probe_location_comm
);
592 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
594 struct lttng_buffer_view view
= lttng_buffer_view_from_view(
595 buffer
, consumed
, buffer
->size
- consumed
);
597 ret
= lttng_userspace_probe_location_function_create_from_buffer(
605 ret
= -LTTNG_ERR_INVALID
;
610 if (buffer
->size
<= consumed
) {
611 ret
= -LTTNG_ERR_INVALID
;
615 lookup_method_view
= lttng_buffer_view_from_view(buffer
, consumed
,
616 buffer
->size
- consumed
);
617 ret
= lttng_userspace_probe_location_lookup_method_create_from_buffer(
618 &lookup_method_view
, &lookup_method
);
620 ret
= -LTTNG_ERR_INVALID
;
624 assert(lookup_method
);
625 (*location
)->lookup_method
= lookup_method
;
626 lookup_method
= NULL
;
633 int lttng_userspace_probe_location_function_set_binary_fd(
634 struct lttng_userspace_probe_location
*location
, int binary_fd
)
637 struct lttng_userspace_probe_location_function
*function_location
;
640 assert(location
->type
== LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
642 function_location
= container_of(location
,
643 struct lttng_userspace_probe_location_function
, parent
);
644 if (function_location
->binary_fd
>= 0) {
645 ret
= close(function_location
->binary_fd
);
648 ret
= -LTTNG_ERR_INVALID
;
653 function_location
->binary_fd
= binary_fd
;
659 int lttng_userspace_probe_location_function_flatten(
660 const struct lttng_userspace_probe_location
*location
,
661 struct lttng_dynamic_buffer
*buffer
)
663 struct lttng_userspace_probe_location_lookup_method_elf flat_lookup_method
;
664 struct lttng_userspace_probe_location_function
*probe_function
;
665 struct lttng_userspace_probe_location_function flat_probe
;
666 size_t function_name_len
, binary_path_len
;
667 size_t padding_needed
= 0;
668 char *flat_probe_start
;
669 int storage_needed
= 0;
674 if (location
->lookup_method
&& location
->lookup_method
->type
!=
675 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
) {
676 ret
= -LTTNG_ERR_INVALID
;
680 probe_function
= container_of(location
,
681 struct lttng_userspace_probe_location_function
,
683 assert(probe_function
->function_name
);
684 assert(probe_function
->binary_path
);
687 sizeof(struct lttng_userspace_probe_location_function
);
688 function_name_len
= strlen(probe_function
->function_name
) + 1;
689 binary_path_len
= strlen(probe_function
->binary_path
) + 1;
690 storage_needed
+= function_name_len
+ binary_path_len
;
693 * The lookup method is aligned to 64-bit within the buffer.
694 * This is needed even if there is no lookup method since
695 * the next structure in the buffer probably needs to be
696 * aligned too (depending on the arch).
698 padding_needed
= ALIGN_TO(storage_needed
, sizeof(uint64_t)) - storage_needed
;
699 storage_needed
+= padding_needed
;
701 if (location
->lookup_method
) {
702 /* NOTE: elf look-up method is assumed here. */
703 storage_needed
+= sizeof(struct lttng_userspace_probe_location_lookup_method_elf
);
707 ret
= storage_needed
;
711 if (lttng_dynamic_buffer_get_capacity_left(buffer
) < storage_needed
) {
712 ret
= lttng_dynamic_buffer_set_capacity(buffer
,
713 buffer
->size
+ storage_needed
);
719 memset(&flat_probe
, 0, sizeof(flat_probe
));
721 flat_probe_start
= buffer
->data
+ buffer
->size
;
722 flat_probe
.parent
.type
= location
->type
;
724 * The lookup method, if present, is the last element in the flat
725 * representation of the probe.
727 if (location
->lookup_method
) {
728 flat_probe
.parent
.lookup_method
=
729 (struct lttng_userspace_probe_location_lookup_method
*)
730 (flat_probe_start
+ sizeof(flat_probe
) +
731 function_name_len
+ binary_path_len
+ padding_needed
);
733 flat_probe
.parent
.lookup_method
= NULL
;
736 flat_probe
.function_name
= flat_probe_start
+ sizeof(flat_probe
);
737 flat_probe
.binary_path
= flat_probe
.function_name
+ function_name_len
;
738 flat_probe
.binary_fd
= -1;
739 ret
= lttng_dynamic_buffer_append(buffer
, &flat_probe
,
745 ret
= lttng_dynamic_buffer_append(buffer
,
746 probe_function
->function_name
, function_name_len
);
750 ret
= lttng_dynamic_buffer_append(buffer
,
751 probe_function
->binary_path
, binary_path_len
);
756 /* Insert padding before the lookup method. */
757 ret
= lttng_dynamic_buffer_set_size(buffer
,
758 buffer
->size
+ padding_needed
);
763 if (!location
->lookup_method
) {
764 /* Not an error, the default method is used. */
765 ret
= storage_needed
;
769 memset(&flat_lookup_method
, 0, sizeof(flat_lookup_method
));
770 flat_lookup_method
.parent
.type
=
771 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
;
772 ret
= lttng_dynamic_buffer_append(buffer
,
773 &flat_lookup_method
, sizeof(flat_lookup_method
));
777 ret
= storage_needed
;
783 int lttng_userspace_probe_location_flatten(
784 const struct lttng_userspace_probe_location
*location
,
785 struct lttng_dynamic_buffer
*buffer
)
789 ret
= -LTTNG_ERR_INVALID
;
793 /* Only types currently supported. */
794 switch (location
->type
) {
795 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
796 ret
= lttng_userspace_probe_location_function_flatten(location
, buffer
);
799 ret
= -LTTNG_ERR_INVALID
;