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 memset(&location_generic_comm
, 0, sizeof(location_generic_comm
));
420 location_generic_comm
.type
= (int8_t) location
->type
;
422 ret
= lttng_dynamic_buffer_append(buffer
, &location_generic_comm
,
423 sizeof(location_generic_comm
));
428 buffer_use
+= sizeof(location_generic_comm
);
430 switch (lttng_userspace_probe_location_get_type(location
)) {
431 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
432 ret
= lttng_userspace_probe_location_function_serialize(
433 location
, buffer
, binary_fd
);
436 ERR("Unsupported probe location type");
437 ret
= -LTTNG_ERR_INVALID
;
445 ret
= lttng_userspace_probe_location_lookup_method_serialize(
446 location
->lookup_method
, buffer
);
456 int lttng_userspace_probe_location_function_create_from_buffer(
457 const struct lttng_buffer_view
*buffer
,
458 struct lttng_userspace_probe_location
**location
)
460 struct lttng_userspace_probe_location_function_comm
*location_function_comm
;
461 const char *function_name_src
, *binary_path_src
;
462 char *function_name
= NULL
, *binary_path
= NULL
;
466 assert(buffer
->data
);
469 location_function_comm
=
470 (struct lttng_userspace_probe_location_function_comm
*) buffer
->data
;
472 const size_t expected_size
= sizeof(*location_function_comm
) +
473 location_function_comm
->function_name_len
+
474 location_function_comm
->binary_path_len
;
476 if (buffer
->size
< expected_size
) {
477 ret
= -LTTNG_ERR_INVALID
;
481 function_name_src
= buffer
->data
+ sizeof(*location_function_comm
);
482 binary_path_src
= function_name_src
+
483 location_function_comm
->function_name_len
;
485 if (function_name_src
[location_function_comm
->function_name_len
- 1] != '\0') {
486 ret
= -LTTNG_ERR_INVALID
;
489 if (binary_path_src
[location_function_comm
->binary_path_len
- 1] != '\0') {
490 ret
= -LTTNG_ERR_INVALID
;
494 function_name
= lttng_strndup(function_name_src
, LTTNG_SYMBOL_NAME_LEN
);
495 if (!function_name
) {
496 PERROR("lttng_strndup");
500 binary_path
= lttng_strndup(binary_path_src
, LTTNG_PATH_MAX
);
502 PERROR("lttng_strndup");
506 *location
= lttng_userspace_probe_location_function_create_no_check(
507 binary_path
, function_name
, NULL
, false);
509 ret
= -LTTNG_ERR_INVALID
;
513 ret
= (int) expected_size
;
521 int lttng_userspace_probe_location_lookup_method_create_from_buffer(
522 struct lttng_buffer_view
*buffer
,
523 struct lttng_userspace_probe_location_lookup_method
**lookup_method
)
526 struct lttng_userspace_probe_location_lookup_method_comm
*lookup_comm
;
527 enum lttng_userspace_probe_location_lookup_method_type type
;
530 assert(buffer
->data
);
531 assert(lookup_method
);
533 if (buffer
->size
< sizeof(*lookup_comm
)) {
534 ret
= -LTTNG_ERR_INVALID
;
538 lookup_comm
= (struct lttng_userspace_probe_location_lookup_method_comm
*)
540 type
= (enum lttng_userspace_probe_location_lookup_method_type
)
543 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT
:
544 *lookup_method
= NULL
;
546 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
548 lttng_userspace_probe_location_lookup_method_function_elf_create();
549 if (!(*lookup_method
)) {
550 ret
= -LTTNG_ERR_INVALID
;
555 ret
= -LTTNG_ERR_INVALID
;
559 ret
= sizeof(*lookup_comm
);
565 int lttng_userspace_probe_location_create_from_buffer(
566 const struct lttng_buffer_view
*buffer
,
567 struct lttng_userspace_probe_location
**location
)
569 struct lttng_userspace_probe_location_lookup_method
*lookup_method
;
570 struct lttng_userspace_probe_location_comm
*probe_location_comm
;
571 enum lttng_userspace_probe_location_type type
;
572 struct lttng_buffer_view lookup_method_view
;
578 assert(buffer
->data
);
581 lookup_method
= NULL
;
583 if (buffer
->size
<= sizeof(*probe_location_comm
)) {
584 ret
= -LTTNG_ERR_INVALID
;
588 probe_location_comm
=
589 (struct lttng_userspace_probe_location_comm
*) buffer
->data
;
590 type
= (enum lttng_userspace_probe_location_type
) probe_location_comm
->type
;
591 consumed
+= sizeof(*probe_location_comm
);
594 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
596 struct lttng_buffer_view view
= lttng_buffer_view_from_view(
597 buffer
, consumed
, buffer
->size
- consumed
);
599 ret
= lttng_userspace_probe_location_function_create_from_buffer(
607 ret
= -LTTNG_ERR_INVALID
;
612 if (buffer
->size
<= consumed
) {
613 ret
= -LTTNG_ERR_INVALID
;
617 lookup_method_view
= lttng_buffer_view_from_view(buffer
, consumed
,
618 buffer
->size
- consumed
);
619 ret
= lttng_userspace_probe_location_lookup_method_create_from_buffer(
620 &lookup_method_view
, &lookup_method
);
622 ret
= -LTTNG_ERR_INVALID
;
626 assert(lookup_method
);
627 (*location
)->lookup_method
= lookup_method
;
628 lookup_method
= NULL
;
635 int lttng_userspace_probe_location_function_set_binary_fd(
636 struct lttng_userspace_probe_location
*location
, int binary_fd
)
639 struct lttng_userspace_probe_location_function
*function_location
;
642 assert(location
->type
== LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
644 function_location
= container_of(location
,
645 struct lttng_userspace_probe_location_function
, parent
);
646 if (function_location
->binary_fd
>= 0) {
647 ret
= close(function_location
->binary_fd
);
650 ret
= -LTTNG_ERR_INVALID
;
655 function_location
->binary_fd
= binary_fd
;
661 int lttng_userspace_probe_location_function_flatten(
662 const struct lttng_userspace_probe_location
*location
,
663 struct lttng_dynamic_buffer
*buffer
)
665 struct lttng_userspace_probe_location_lookup_method_elf flat_lookup_method
;
666 struct lttng_userspace_probe_location_function
*probe_function
;
667 struct lttng_userspace_probe_location_function flat_probe
;
668 size_t function_name_len
, binary_path_len
;
669 size_t padding_needed
= 0;
670 char *flat_probe_start
;
671 int storage_needed
= 0;
676 if (location
->lookup_method
&& location
->lookup_method
->type
!=
677 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
) {
678 ret
= -LTTNG_ERR_INVALID
;
682 probe_function
= container_of(location
,
683 struct lttng_userspace_probe_location_function
,
685 assert(probe_function
->function_name
);
686 assert(probe_function
->binary_path
);
689 sizeof(struct lttng_userspace_probe_location_function
);
690 function_name_len
= strlen(probe_function
->function_name
) + 1;
691 binary_path_len
= strlen(probe_function
->binary_path
) + 1;
692 storage_needed
+= function_name_len
+ binary_path_len
;
695 * The lookup method is aligned to 64-bit within the buffer.
696 * This is needed even if there is no lookup method since
697 * the next structure in the buffer probably needs to be
698 * aligned too (depending on the arch).
700 padding_needed
= ALIGN_TO(storage_needed
, sizeof(uint64_t)) - storage_needed
;
701 storage_needed
+= padding_needed
;
703 if (location
->lookup_method
) {
704 /* NOTE: elf look-up method is assumed here. */
705 storage_needed
+= sizeof(struct lttng_userspace_probe_location_lookup_method_elf
);
709 ret
= storage_needed
;
713 if (lttng_dynamic_buffer_get_capacity_left(buffer
) < storage_needed
) {
714 ret
= lttng_dynamic_buffer_set_capacity(buffer
,
715 buffer
->size
+ storage_needed
);
721 memset(&flat_probe
, 0, sizeof(flat_probe
));
723 flat_probe_start
= buffer
->data
+ buffer
->size
;
724 flat_probe
.parent
.type
= location
->type
;
726 * The lookup method, if present, is the last element in the flat
727 * representation of the probe.
729 if (location
->lookup_method
) {
730 flat_probe
.parent
.lookup_method
=
731 (struct lttng_userspace_probe_location_lookup_method
*)
732 (flat_probe_start
+ sizeof(flat_probe
) +
733 function_name_len
+ binary_path_len
+ padding_needed
);
735 flat_probe
.parent
.lookup_method
= NULL
;
738 flat_probe
.function_name
= flat_probe_start
+ sizeof(flat_probe
);
739 flat_probe
.binary_path
= flat_probe
.function_name
+ function_name_len
;
740 flat_probe
.binary_fd
= -1;
741 ret
= lttng_dynamic_buffer_append(buffer
, &flat_probe
,
747 ret
= lttng_dynamic_buffer_append(buffer
,
748 probe_function
->function_name
, function_name_len
);
752 ret
= lttng_dynamic_buffer_append(buffer
,
753 probe_function
->binary_path
, binary_path_len
);
758 /* Insert padding before the lookup method. */
759 ret
= lttng_dynamic_buffer_set_size(buffer
,
760 buffer
->size
+ padding_needed
);
765 if (!location
->lookup_method
) {
766 /* Not an error, the default method is used. */
767 ret
= storage_needed
;
771 memset(&flat_lookup_method
, 0, sizeof(flat_lookup_method
));
772 flat_lookup_method
.parent
.type
=
773 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
;
774 ret
= lttng_dynamic_buffer_append(buffer
,
775 &flat_lookup_method
, sizeof(flat_lookup_method
));
779 ret
= storage_needed
;
785 int lttng_userspace_probe_location_flatten(
786 const struct lttng_userspace_probe_location
*location
,
787 struct lttng_dynamic_buffer
*buffer
)
791 ret
= -LTTNG_ERR_INVALID
;
795 /* Only types currently supported. */
796 switch (location
->type
) {
797 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
:
798 ret
= lttng_userspace_probe_location_function_flatten(location
, buffer
);
801 ret
= -LTTNG_ERR_INVALID
;