Add kernel uid/gid contexts
[lttng-tools.git] / src / common / userspace-probe.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
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.
7 *
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
11 * for more details.
12 *
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
16 */
17
18#include <assert.h>
19#include <common/error.h>
20#include <common/macros.h>
21#include <common/compat/string.h>
22#include <fcntl.h>
23#include <lttng/constant.h>
24#include <lttng/userspace-probe-internal.h>
25
26enum lttng_userspace_probe_location_lookup_method_type
27lttng_userspace_probe_location_lookup_method_get_type(
28 const struct lttng_userspace_probe_location_lookup_method *lookup_method)
29{
30 return lookup_method ? lookup_method->type :
31 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_UNKNOWN;
32}
33
34void lttng_userspace_probe_location_lookup_method_destroy(
35 struct lttng_userspace_probe_location_lookup_method *lookup_method)
36{
37 if (!lookup_method){
38 return;
39 }
40
41 free(lookup_method);
42}
43
44struct lttng_userspace_probe_location_lookup_method *
45lttng_userspace_probe_location_lookup_method_function_elf_create(void)
46{
47 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
48 struct lttng_userspace_probe_location_lookup_method_elf *elf_method;
49
50 elf_method = zmalloc(sizeof(*elf_method));
51 if (!elf_method) {
52 PERROR("zmalloc");
53 goto end;
54 }
55
56 ret = &elf_method->parent;
57 ret->type = LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF;
58end:
59 return ret;
60}
61
62struct lttng_userspace_probe_location_lookup_method *
63lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create(void)
64{
65 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
66 struct lttng_userspace_probe_location_lookup_method_sdt *sdt_method;
67
68 sdt_method = zmalloc(sizeof(*sdt_method));
69 if (!sdt_method) {
70 PERROR("zmalloc");
71 goto end;
72 }
73
74 ret = &sdt_method->parent;
75 ret->type = LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT;
76end:
77 return ret;
78}
79
80enum lttng_userspace_probe_location_type lttng_userspace_probe_location_get_type(
81 const struct lttng_userspace_probe_location *location)
82{
83 return location ? location->type :
84 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN;
85}
86
87static
88void lttng_userspace_probe_location_function_destroy(
89 struct lttng_userspace_probe_location *location)
90{
91 struct lttng_userspace_probe_location_function *location_function = NULL;
92
93 assert(location);
94
95 location_function = container_of(location,
96 struct lttng_userspace_probe_location_function, parent);
97
98 assert(location_function);
99
100 free(location_function->function_name);
101 free(location_function->binary_path);
102 if (location_function->binary_fd >= 0) {
103 if (close(location_function->binary_fd)) {
104 PERROR("close");
105 }
106 }
107 free(location);
108}
109
110static
111void lttng_userspace_probe_location_tracepoint_destroy(
112 struct lttng_userspace_probe_location *location)
113{
114 struct lttng_userspace_probe_location_tracepoint *location_tracepoint = NULL;
115
116 assert(location);
117
118 location_tracepoint = container_of(location,
119 struct lttng_userspace_probe_location_tracepoint,
120 parent);
121
122 assert(location_tracepoint);
123
124 free(location_tracepoint->probe_name);
125 free(location_tracepoint->provider_name);
126 free(location_tracepoint->binary_path);
127 if (location_tracepoint->binary_fd >= 0) {
128 if (close(location_tracepoint->binary_fd)) {
129 PERROR("close");
130 }
131 }
132 free(location);
133}
134
135void lttng_userspace_probe_location_destroy(
136 struct lttng_userspace_probe_location *location)
137{
138 if (!location) {
139 return;
140 }
141
142 lttng_userspace_probe_location_lookup_method_destroy(
143 location->lookup_method);
144
145 switch (location->type) {
146 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
147 lttng_userspace_probe_location_function_destroy(location);
148 break;
149 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
150 lttng_userspace_probe_location_tracepoint_destroy(location);
151 break;
152 default:
153 abort();
154 }
155}
156
157static struct lttng_userspace_probe_location *
158lttng_userspace_probe_location_function_create_no_check(const char *binary_path,
159 const char *function_name,
160 struct lttng_userspace_probe_location_lookup_method *lookup_method,
161 bool open_binary)
162{
163 int binary_fd = -1;
164 char *function_name_copy = NULL, *binary_path_copy = NULL;
165 struct lttng_userspace_probe_location *ret = NULL;
166 struct lttng_userspace_probe_location_function *location;
167
168 if (open_binary) {
169 binary_fd = open(binary_path, O_RDONLY);
170 if (binary_fd < 0) {
171 PERROR("Error opening the binary");
172 goto error;
173 }
174 } else {
175 binary_fd = -1;
176 }
177
178 function_name_copy = lttng_strndup(function_name, LTTNG_SYMBOL_NAME_LEN);
179 if (!function_name_copy) {
180 PERROR("Error duplicating the function name");
181 goto error;
182 }
183
184 binary_path_copy = lttng_strndup(binary_path, LTTNG_PATH_MAX);
185 if (!binary_path_copy) {
186 PERROR("Error duplicating the function name");
187 goto error;
188 }
189
190 location = zmalloc(sizeof(*location));
191 if (!location) {
192 PERROR("Error allocating userspace probe location");
193 goto error;
194 }
195
196 location->function_name = function_name_copy;
197 location->binary_path = binary_path_copy;
198 location->binary_fd = binary_fd;
199 location->instrumentation_type =
200 LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_ENTRY;
201
202 ret = &location->parent;
203 ret->lookup_method = lookup_method;
204 ret->type = LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION;
205 goto end;
206
207error:
208 free(function_name_copy);
209 free(binary_path_copy);
210 if (binary_fd >= 0) {
211 if (close(binary_fd)) {
212 PERROR("Error closing binary fd in error path");
213 }
214 }
215end:
216 return ret;
217}
218
219static struct lttng_userspace_probe_location *
220lttng_userspace_probe_location_tracepoint_create_no_check(const char *binary_path,
221 const char *provider_name, const char *probe_name,
222 struct lttng_userspace_probe_location_lookup_method *lookup_method,
223 bool open_binary)
224{
225 int binary_fd = -1;
226 char *probe_name_copy = NULL;
227 char *provider_name_copy = NULL;
228 char *binary_path_copy = NULL;
229 struct lttng_userspace_probe_location *ret = NULL;
230 struct lttng_userspace_probe_location_tracepoint *location;
231
232 if (open_binary) {
233 binary_fd = open(binary_path, O_RDONLY);
234 if (binary_fd < 0) {
235 PERROR("open");
236 goto error;
237 }
238 } else {
239 binary_fd = -1;
240 }
241
242 probe_name_copy = lttng_strndup(probe_name, LTTNG_SYMBOL_NAME_LEN);
243 if (!probe_name_copy) {
244 PERROR("lttng_strndup");
245 goto error;
246 }
247
248 provider_name_copy = lttng_strndup(provider_name, LTTNG_SYMBOL_NAME_LEN);
249 if (!provider_name_copy) {
250 PERROR("lttng_strndup");
251 goto error;
252 }
253
254 binary_path_copy = lttng_strndup(binary_path, LTTNG_PATH_MAX);
255 if (!binary_path_copy) {
256 PERROR("lttng_strndup");
257 goto error;
258 }
259
260 location = zmalloc(sizeof(*location));
261 if (!location) {
262 PERROR("zmalloc");
263 goto error;
264 }
265
266 location->probe_name = probe_name_copy;
267 location->provider_name = provider_name_copy;
268 location->binary_path = binary_path_copy;
269 location->binary_fd = binary_fd;
270
271 ret = &location->parent;
272 ret->lookup_method = lookup_method;
273 ret->type = LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT;
274 goto end;
275
276error:
277 free(probe_name_copy);
278 free(provider_name_copy);
279 free(binary_path_copy);
280 if (binary_fd >= 0) {
281 if (close(binary_fd)) {
282 PERROR("Error closing binary fd in error path");
283 }
284 }
285end:
286 return ret;
287}
288
289struct lttng_userspace_probe_location *
290lttng_userspace_probe_location_function_create(const char *binary_path,
291 const char *function_name,
292 struct lttng_userspace_probe_location_lookup_method *lookup_method)
293{
294 struct lttng_userspace_probe_location *ret = NULL;
295
296 if (!binary_path || !function_name) {
297 ERR("Invalid argument(s)");
298 goto end;
299 }
300
301 switch (lttng_userspace_probe_location_lookup_method_get_type(
302 lookup_method)) {
303 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT:
304 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
305 break;
306 default:
307 /* Invalid probe location lookup method. */
308 goto end;
309 }
310
311 ret = lttng_userspace_probe_location_function_create_no_check(
312 binary_path, function_name, lookup_method, true);
313end:
314 return ret;
315}
316
317struct lttng_userspace_probe_location *
318lttng_userspace_probe_location_tracepoint_create(const char *binary_path,
319 const char *provider_name, const char *probe_name,
320 struct lttng_userspace_probe_location_lookup_method *lookup_method)
321{
322 struct lttng_userspace_probe_location *ret = NULL;
323
324 if (!binary_path || !probe_name || !provider_name) {
325 ERR("Invalid argument(s)");
326 goto end;
327 }
328
329 switch (lttng_userspace_probe_location_lookup_method_get_type(
330 lookup_method)) {
331 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
332 break;
333 default:
334 /* Invalid probe location lookup method. */
335 goto end;
336 }
337
338 ret = lttng_userspace_probe_location_tracepoint_create_no_check(
339 binary_path, provider_name, probe_name, lookup_method, true);
340end:
341 return ret;
342}
343
344static struct lttng_userspace_probe_location_lookup_method *
345lttng_userspace_probe_location_lookup_method_function_elf_copy(
346 const struct lttng_userspace_probe_location_lookup_method *lookup_method)
347{
348 struct lttng_userspace_probe_location_lookup_method *parent = NULL;
349 struct lttng_userspace_probe_location_lookup_method_elf *elf_method;
350
351 assert(lookup_method);
352 assert(lookup_method->type ==
353 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF);
354
355 elf_method = zmalloc(sizeof(*elf_method));
356 if (!elf_method) {
357 PERROR("Error allocating ELF userspace probe lookup method");
358 goto error;
359 }
360
361 elf_method->parent.type = lookup_method->type;
362 parent = &elf_method->parent;
363
364 goto end;
365error:
366 parent = NULL;
367end:
368 return parent;
369}
370
371static struct lttng_userspace_probe_location_lookup_method *
372lttng_userspace_probe_location_lookup_method_tracepoint_sdt_copy(
373 struct lttng_userspace_probe_location_lookup_method *lookup_method)
374{
375 struct lttng_userspace_probe_location_lookup_method *parent = NULL;
376 struct lttng_userspace_probe_location_lookup_method_sdt *sdt_method;
377
378 assert(lookup_method);
379 assert(lookup_method->type ==
380 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT);
381
382 sdt_method = zmalloc(sizeof(*sdt_method));
383 if (!sdt_method) {
384 PERROR("zmalloc");
385 goto error;
386 }
387
388 sdt_method->parent.type = lookup_method->type;
389 parent = &sdt_method->parent;
390
391 goto end;
392
393error:
394 parent = NULL;
395end:
396 return parent;
397}
398
399static struct lttng_userspace_probe_location *
400lttng_userspace_probe_location_function_copy(
401 const struct lttng_userspace_probe_location *location)
402{
403 enum lttng_userspace_probe_location_lookup_method_type lookup_type;
404 struct lttng_userspace_probe_location *new_location = NULL;
405 struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
406 const char *binary_path = NULL;
407 const char *function_name = NULL;
408 int fd, new_fd;
409
410 assert(location);
411 assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
412
413 /* Get probe location fields */
414 binary_path = lttng_userspace_probe_location_function_get_binary_path(location);
415 if (!binary_path) {
416 ERR("Userspace probe binary path is NULL");
417 goto error;
418 }
419
420 function_name = lttng_userspace_probe_location_function_get_function_name(location);
421 if (!function_name) {
422 ERR("Userspace probe function name is NULL");
423 goto error;
424 }
425
426 /* Duplicate the binary fd */
427 fd = lttng_userspace_probe_location_function_get_binary_fd(location);
428 if (fd == -1) {
429 ERR("Error getting file descriptor to binary");
430 goto error;
431 }
432
433 new_fd = dup(fd);
434 if (new_fd == -1) {
435 PERROR("Error duplicating file descriptor to binary");
436 goto error;
437 }
438
439 /*
440 * Duplicate probe location method fields
441 */
442 lookup_type = lttng_userspace_probe_location_lookup_method_get_type(
443 location->lookup_method);
444 switch (lookup_type) {
445 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
446 lookup_method =
447 lttng_userspace_probe_location_lookup_method_function_elf_copy(
448 location->lookup_method);
449 if (!lookup_method) {
450 goto close_fd;
451 }
452 break;
453 default:
454 /* Invalid probe location lookup method. */
455 goto close_fd;
456 }
457
458 /* Create the probe_location */
459 new_location = lttng_userspace_probe_location_function_create_no_check(
460 binary_path, function_name, lookup_method, false);
461 if (!new_location) {
462 goto destroy_lookup_method;
463 }
464
465 /* Set the duplicated fd to the new probe_location */
466 if (lttng_userspace_probe_location_function_set_binary_fd(new_location, new_fd) < 0) {
467 goto destroy_probe_location;
468 }
469
470 goto end;
471
472destroy_probe_location:
473 lttng_userspace_probe_location_destroy(new_location);
474destroy_lookup_method:
475 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
476close_fd:
477 if (close(new_fd) < 0) {
478 PERROR("Error closing duplicated file descriptor in error path");
479 }
480error:
481 new_location = NULL;
482end:
483 return new_location;
484}
485
486static struct lttng_userspace_probe_location *
487lttng_userspace_probe_location_tracepoint_copy(
488 const struct lttng_userspace_probe_location *location)
489{
490 enum lttng_userspace_probe_location_lookup_method_type lookup_type;
491 struct lttng_userspace_probe_location *new_location = NULL;
492 struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
493 const char *binary_path = NULL;
494 const char *probe_name = NULL;
495 const char *provider_name = NULL;
496 int fd, new_fd;
497
498 assert(location);
499 assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
500
501 /* Get probe location fields */
502 binary_path = lttng_userspace_probe_location_tracepoint_get_binary_path(location);
503 if (!binary_path) {
504 ERR("Userspace probe binary path is NULL");
505 goto error;
506 }
507
508 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(location);
509 if (!probe_name) {
510 ERR("Userspace probe probe name is NULL");
511 goto error;
512 }
513
514 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(location);
515 if (!provider_name) {
516 ERR("Userspace probe provider name is NULL");
517 goto error;
518 }
519
520 /* Duplicate the binary fd */
521 fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
522 if (fd == -1) {
523 ERR("Error getting file descriptor to binary");
524 goto error;
525 }
526
527 new_fd = dup(fd);
528 if (new_fd == -1) {
529 PERROR("Error duplicating file descriptor to binary");
530 goto error;
531 }
532
533 /*
534 * Duplicate probe location method fields
535 */
536 lookup_type = lttng_userspace_probe_location_lookup_method_get_type(
537 location->lookup_method);
538 switch (lookup_type) {
539 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
540 lookup_method =
541 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_copy(
542 location->lookup_method);
543 if (!lookup_method) {
544 goto close_fd;
545 }
546 break;
547 default:
548 /* Invalid probe location lookup method. */
549 goto close_fd;
550 }
551
552 /* Create the probe_location */
553 new_location = lttng_userspace_probe_location_tracepoint_create_no_check(
554 binary_path, provider_name, probe_name, lookup_method, false);
555 if (!new_location) {
556 goto destroy_lookup_method;
557 }
558
559 /* Set the duplicated fd to the new probe_location */
560 if (lttng_userspace_probe_location_tracepoint_set_binary_fd(new_location, new_fd) < 0) {
561 goto destroy_probe_location;
562 }
563
564 goto end;
565
566destroy_probe_location:
567 lttng_userspace_probe_location_destroy(new_location);
568destroy_lookup_method:
569 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
570close_fd:
571 if (close(new_fd) < 0) {
572 PERROR("Error closing duplicated file descriptor in error path");
573 }
574error:
575 new_location = NULL;
576end:
577 return new_location;
578}
579
580const char *lttng_userspace_probe_location_function_get_binary_path(
581 const struct lttng_userspace_probe_location *location)
582{
583 const char *ret = NULL;
584 struct lttng_userspace_probe_location_function *function_location;
585
586 if (!location || lttng_userspace_probe_location_get_type(location) !=
587 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
588 ERR("Invalid argument(s)");
589 goto end;
590 }
591
592 function_location = container_of(location,
593 struct lttng_userspace_probe_location_function,
594 parent);
595 ret = function_location->binary_path;
596end:
597 return ret;
598}
599
600const char *lttng_userspace_probe_location_tracepoint_get_binary_path(
601 const struct lttng_userspace_probe_location *location)
602{
603 const char *ret = NULL;
604 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
605
606 if (!location || lttng_userspace_probe_location_get_type(location) !=
607 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
608 ERR("Invalid argument(s)");
609 goto end;
610 }
611
612 tracepoint_location = container_of(location,
613 struct lttng_userspace_probe_location_tracepoint,
614 parent);
615 ret = tracepoint_location->binary_path;
616end:
617 return ret;
618}
619
620const char *lttng_userspace_probe_location_function_get_function_name(
621 const struct lttng_userspace_probe_location *location)
622{
623 const char *ret = NULL;
624 struct lttng_userspace_probe_location_function *function_location;
625
626 if (!location || lttng_userspace_probe_location_get_type(location) !=
627 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
628 ERR("Invalid argument(s)");
629 goto end;
630 }
631
632 function_location = container_of(location,
633 struct lttng_userspace_probe_location_function, parent);
634 ret = function_location->function_name;
635end:
636 return ret;
637}
638
639const char *lttng_userspace_probe_location_tracepoint_get_probe_name(
640 const struct lttng_userspace_probe_location *location)
641{
642 const char *ret = NULL;
643 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
644
645 if (!location || lttng_userspace_probe_location_get_type(location) !=
646 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
647 ERR("Invalid argument(s)");
648 goto end;
649 }
650
651 tracepoint_location = container_of(location,
652 struct lttng_userspace_probe_location_tracepoint, parent);
653 ret = tracepoint_location->probe_name;
654end:
655 return ret;
656}
657
658const char *lttng_userspace_probe_location_tracepoint_get_provider_name(
659 const struct lttng_userspace_probe_location *location)
660{
661 const char *ret = NULL;
662 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
663
664 if (!location || lttng_userspace_probe_location_get_type(location) !=
665 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
666 ERR("Invalid argument(s)");
667 goto end;
668 }
669
670 tracepoint_location = container_of(location,
671 struct lttng_userspace_probe_location_tracepoint, parent);
672 ret = tracepoint_location->provider_name;
673end:
674 return ret;
675}
676
677int lttng_userspace_probe_location_function_get_binary_fd(
678 const struct lttng_userspace_probe_location *location)
679{
680 int ret = -1;
681 struct lttng_userspace_probe_location_function *function_location;
682
683 if (!location || lttng_userspace_probe_location_get_type(location) !=
684 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
685 ERR("Invalid argument(s)");
686 goto end;
687 }
688
689 function_location = container_of(location,
690 struct lttng_userspace_probe_location_function, parent);
691 ret = function_location->binary_fd;
692end:
693 return ret;
694}
695
696enum lttng_userspace_probe_location_function_instrumentation_type
697lttng_userspace_probe_location_function_get_instrumentation_type(
698 const struct lttng_userspace_probe_location *location)
699{
700 enum lttng_userspace_probe_location_function_instrumentation_type type;
701 struct lttng_userspace_probe_location_function *function_location;
702
703 if (!location || lttng_userspace_probe_location_get_type(location) !=
704 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
705 ERR("Invalid argument(s)");
706 type = LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_UNKNOWN;
707 goto end;
708 }
709
710 function_location = container_of(location,
711 struct lttng_userspace_probe_location_function, parent);
712 type = function_location->instrumentation_type;
713end:
714 return type;
715}
716
717enum lttng_userspace_probe_location_status
718lttng_userspace_probe_location_function_set_instrumentation_type(
719 const struct lttng_userspace_probe_location *location,
720 enum lttng_userspace_probe_location_function_instrumentation_type instrumentation_type)
721{
722 enum lttng_userspace_probe_location_status status =
723 LTTNG_USERSPACE_PROBE_LOCATION_STATUS_OK;
724 struct lttng_userspace_probe_location_function *function_location;
725
726 if (!location || lttng_userspace_probe_location_get_type(location) !=
727 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION ||
728 instrumentation_type !=
729 LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_ENTRY) {
730 ERR("Invalid argument(s)");
731 status = LTTNG_USERSPACE_PROBE_LOCATION_STATUS_INVALID;
732 goto end;
733 }
734
735 function_location = container_of(location,
736 struct lttng_userspace_probe_location_function, parent);
737 function_location->instrumentation_type = instrumentation_type;
738end:
739 return status;
740}
741
742int lttng_userspace_probe_location_tracepoint_get_binary_fd(
743 const struct lttng_userspace_probe_location *location)
744{
745 int ret = -1;
746 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
747
748 if (!location || lttng_userspace_probe_location_get_type(location) !=
749 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
750 ERR("Invalid argument(s)");
751 goto end;
752 }
753
754 tracepoint_location = container_of(location,
755 struct lttng_userspace_probe_location_tracepoint, parent);
756 ret = tracepoint_location->binary_fd;
757end:
758 return ret;
759}
760
761static struct lttng_userspace_probe_location_lookup_method *
762lttng_userspace_probe_location_function_get_lookup_method(
763 const struct lttng_userspace_probe_location *location)
764{
765 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
766
767 if (!location || lttng_userspace_probe_location_get_type(location) !=
768 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
769 ERR("Invalid argument(s)");
770 goto end;
771 }
772
773 ret = location->lookup_method;
774end:
775 return ret;
776}
777
778static struct lttng_userspace_probe_location_lookup_method *
779lttng_userspace_probe_location_tracepoint_get_lookup_method(
780 const struct lttng_userspace_probe_location *location)
781{
782 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
783
784 if (!location || lttng_userspace_probe_location_get_type(location) !=
785 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
786 ERR("Invalid argument(s)");
787 goto end;
788 }
789
790 ret = location->lookup_method;
791end:
792 return ret;
793}
794
795const struct lttng_userspace_probe_location_lookup_method *
796lttng_userspace_probe_location_get_lookup_method(
797 const struct lttng_userspace_probe_location *location)
798{
799 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
800
801 assert(location);
802 switch (location->type) {
803 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
804 ret = lttng_userspace_probe_location_function_get_lookup_method(
805 location);
806 break;
807 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
808 ret = lttng_userspace_probe_location_tracepoint_get_lookup_method(
809 location);
810 break;
811 default:
812 ERR("Unknowned lookup method.");
813 break;
814 }
815 return ret;
816}
817
818static
819int lttng_userspace_probe_location_lookup_method_serialize(
820 struct lttng_userspace_probe_location_lookup_method *method,
821 struct lttng_dynamic_buffer *buffer)
822{
823 int ret;
824 struct lttng_userspace_probe_location_lookup_method_comm
825 lookup_method_comm;
826
827 lookup_method_comm.type = (int8_t) (method ? method->type :
828 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT);
829 if (buffer) {
830 ret = lttng_dynamic_buffer_append(buffer, &lookup_method_comm,
831 sizeof(lookup_method_comm));
832 if (ret) {
833 goto end;
834 }
835 }
836 ret = sizeof(lookup_method_comm);
837end:
838 return ret;
839}
840
841static
842int lttng_userspace_probe_location_function_serialize(
843 const struct lttng_userspace_probe_location *location,
844 struct lttng_dynamic_buffer *buffer,
845 int *binary_fd)
846{
847 int ret;
848 size_t function_name_len, binary_path_len;
849 struct lttng_userspace_probe_location_function *location_function;
850 struct lttng_userspace_probe_location_function_comm location_function_comm;
851
852 assert(location);
853 assert(lttng_userspace_probe_location_get_type(location) ==
854 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
855
856 location_function = container_of(location,
857 struct lttng_userspace_probe_location_function,
858 parent);
859 if (!location_function->function_name || !location_function->binary_path) {
860 ret = -LTTNG_ERR_INVALID;
861 goto end;
862 }
863
864 if (binary_fd && location_function->binary_fd < 0) {
865 ret = -LTTNG_ERR_INVALID;
866 goto end;
867 }
868
869 if (binary_fd) {
870 *binary_fd = location_function->binary_fd;
871 }
872
873 function_name_len = strlen(location_function->function_name);
874 if (function_name_len == 0) {
875 ret = -LTTNG_ERR_INVALID;
876 goto end;
877 }
878 binary_path_len = strlen(location_function->binary_path);
879 if (binary_path_len == 0) {
880 ret = -LTTNG_ERR_INVALID;
881 goto end;
882 }
883
884 location_function_comm.function_name_len = function_name_len + 1;
885 location_function_comm.binary_path_len = binary_path_len + 1;
886
887 if (buffer) {
888 ret = lttng_dynamic_buffer_append(buffer,
889 &location_function_comm,
890 sizeof(location_function_comm));
891 if (ret) {
892 ret = -LTTNG_ERR_INVALID;
893 goto end;
894 }
895 ret = lttng_dynamic_buffer_append(buffer,
896 location_function->function_name,
897 location_function_comm.function_name_len);
898 if (ret) {
899 ret = -LTTNG_ERR_INVALID;
900 goto end;
901 }
902 ret = lttng_dynamic_buffer_append(buffer,
903 location_function->binary_path,
904 location_function_comm.binary_path_len);
905 if (ret) {
906 ret = -LTTNG_ERR_INVALID;
907 goto end;
908 }
909 }
910 ret = sizeof(location_function_comm) +
911 location_function_comm.function_name_len +
912 location_function_comm.binary_path_len;
913end:
914 return ret;
915}
916
917static
918int lttng_userspace_probe_location_tracepoint_serialize(
919 const struct lttng_userspace_probe_location *location,
920 struct lttng_dynamic_buffer *buffer,
921 int *binary_fd)
922{
923 int ret;
924 size_t probe_name_len, provider_name_len, binary_path_len;
925 struct lttng_userspace_probe_location_tracepoint *location_tracepoint;
926 struct lttng_userspace_probe_location_tracepoint_comm location_tracepoint_comm;
927
928 assert(location);
929 assert(lttng_userspace_probe_location_get_type(location) ==
930 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
931
932 location_tracepoint = container_of(location,
933 struct lttng_userspace_probe_location_tracepoint,
934 parent);
935 if (!location_tracepoint->probe_name ||
936 !location_tracepoint->provider_name ||
937 !location_tracepoint->binary_path) {
938 ret = -LTTNG_ERR_INVALID;
939 goto end;
940 }
941
942 if (binary_fd && location_tracepoint->binary_fd < 0) {
943 ret = -LTTNG_ERR_INVALID;
944 goto end;
945 }
946
947 if (binary_fd) {
948 *binary_fd = location_tracepoint->binary_fd;
949 }
950
951 probe_name_len = strlen(location_tracepoint->probe_name);
952 if (probe_name_len == 0) {
953 ret = -LTTNG_ERR_INVALID;
954 goto end;
955 }
956
957 provider_name_len = strlen(location_tracepoint->provider_name);
958 if (provider_name_len == 0) {
959 ret = -LTTNG_ERR_INVALID;
960 goto end;
961 }
962
963 binary_path_len = strlen(location_tracepoint->binary_path);
964 if (binary_path_len == 0) {
965 ret = -LTTNG_ERR_INVALID;
966 goto end;
967 }
968
969 location_tracepoint_comm.probe_name_len = probe_name_len + 1;
970 location_tracepoint_comm.provider_name_len = provider_name_len + 1;
971 location_tracepoint_comm.binary_path_len = binary_path_len + 1;
972
973 if (buffer) {
974 ret = lttng_dynamic_buffer_append(buffer,
975 &location_tracepoint_comm,
976 sizeof(location_tracepoint_comm));
977 if (ret) {
978 ret = -LTTNG_ERR_INVALID;
979 goto end;
980 }
981 ret = lttng_dynamic_buffer_append(buffer,
982 location_tracepoint->probe_name,
983 location_tracepoint_comm.probe_name_len);
984 if (ret) {
985 ret = -LTTNG_ERR_INVALID;
986 goto end;
987 }
988 ret = lttng_dynamic_buffer_append(buffer,
989 location_tracepoint->provider_name,
990 location_tracepoint_comm.provider_name_len);
991 if (ret) {
992 ret = -LTTNG_ERR_INVALID;
993 goto end;
994 }
995 ret = lttng_dynamic_buffer_append(buffer,
996 location_tracepoint->binary_path,
997 location_tracepoint_comm.binary_path_len);
998 if (ret) {
999 ret = -LTTNG_ERR_INVALID;
1000 goto end;
1001 }
1002 }
1003 ret = sizeof(location_tracepoint_comm) +
1004 location_tracepoint_comm.probe_name_len +
1005 location_tracepoint_comm.provider_name_len +
1006 location_tracepoint_comm.binary_path_len;
1007end:
1008 return ret;
1009}
1010
1011LTTNG_HIDDEN
1012int lttng_userspace_probe_location_serialize(
1013 const struct lttng_userspace_probe_location *location,
1014 struct lttng_dynamic_buffer *buffer,
1015 int *binary_fd)
1016{
1017 int ret, buffer_use = 0;
1018 struct lttng_userspace_probe_location_comm location_generic_comm;
1019
1020 if (!location) {
1021 ERR("Invalid argument(s)");
1022 ret = -LTTNG_ERR_INVALID;
1023 goto end;
1024 }
1025
1026 memset(&location_generic_comm, 0, sizeof(location_generic_comm));
1027
1028 location_generic_comm.type = (int8_t) location->type;
1029 if (buffer) {
1030 ret = lttng_dynamic_buffer_append(buffer, &location_generic_comm,
1031 sizeof(location_generic_comm));
1032 if (ret) {
1033 goto end;
1034 }
1035 }
1036 buffer_use += sizeof(location_generic_comm);
1037
1038 switch (lttng_userspace_probe_location_get_type(location)) {
1039 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
1040 ret = lttng_userspace_probe_location_function_serialize(
1041 location, buffer, binary_fd);
1042 break;
1043 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
1044 ret = lttng_userspace_probe_location_tracepoint_serialize(
1045 location, buffer, binary_fd);
1046 break;
1047 default:
1048 ERR("Unsupported probe location type");
1049 ret = -LTTNG_ERR_INVALID;
1050 goto end;
1051 }
1052 if (ret < 0) {
1053 goto end;
1054 }
1055 buffer_use += ret;
1056
1057 ret = lttng_userspace_probe_location_lookup_method_serialize(
1058 location->lookup_method, buffer);
1059 if (ret < 0) {
1060 goto end;
1061 }
1062 ret += buffer_use;
1063end:
1064 return ret;
1065}
1066
1067static
1068int lttng_userspace_probe_location_function_create_from_buffer(
1069 const struct lttng_buffer_view *buffer,
1070 struct lttng_userspace_probe_location **location)
1071{
1072 struct lttng_userspace_probe_location_function_comm *location_function_comm;
1073 const char *function_name_src, *binary_path_src;
1074 char *function_name = NULL, *binary_path = NULL;
1075 int ret = 0;
1076
1077 assert(buffer);
1078 assert(buffer->data);
1079 assert(location);
1080
1081 location_function_comm =
1082 (struct lttng_userspace_probe_location_function_comm *) buffer->data;
1083
1084 const size_t expected_size = sizeof(*location_function_comm) +
1085 location_function_comm->function_name_len +
1086 location_function_comm->binary_path_len;
1087
1088 if (buffer->size < expected_size) {
1089 ret = -LTTNG_ERR_INVALID;
1090 goto end;
1091 }
1092
1093 function_name_src = buffer->data + sizeof(*location_function_comm);
1094 binary_path_src = function_name_src +
1095 location_function_comm->function_name_len;
1096
1097 if (function_name_src[location_function_comm->function_name_len - 1] != '\0') {
1098 ret = -LTTNG_ERR_INVALID;
1099 goto end;
1100 }
1101 if (binary_path_src[location_function_comm->binary_path_len - 1] != '\0') {
1102 ret = -LTTNG_ERR_INVALID;
1103 goto end;
1104 }
1105
1106 function_name = lttng_strndup(function_name_src, LTTNG_SYMBOL_NAME_LEN);
1107 if (!function_name) {
1108 PERROR("lttng_strndup");
1109 goto end;
1110 }
1111
1112 binary_path = lttng_strndup(binary_path_src, LTTNG_PATH_MAX);
1113 if (!binary_path) {
1114 PERROR("lttng_strndup");
1115 goto end;
1116 }
1117
1118 *location = lttng_userspace_probe_location_function_create_no_check(
1119 binary_path, function_name, NULL, false);
1120 if (!(*location)) {
1121 ret = -LTTNG_ERR_INVALID;
1122 goto end;
1123 }
1124
1125 ret = (int) expected_size;
1126end:
1127 free(function_name);
1128 free(binary_path);
1129 return ret;
1130}
1131
1132static
1133int lttng_userspace_probe_location_tracepoint_create_from_buffer(
1134 const struct lttng_buffer_view *buffer,
1135 struct lttng_userspace_probe_location **location)
1136{
1137 struct lttng_userspace_probe_location_tracepoint_comm *location_tracepoint_comm;
1138 const char *probe_name_src, *provider_name_src, *binary_path_src;
1139 char *probe_name = NULL, *provider_name = NULL, *binary_path = NULL;
1140 int ret = 0;
1141
1142 assert(buffer);
1143 assert(buffer->data);
1144 assert(location);
1145
1146 location_tracepoint_comm =
1147 (struct lttng_userspace_probe_location_tracepoint_comm *) buffer->data;
1148
1149 const size_t expected_size = sizeof(*location_tracepoint_comm) +
1150 location_tracepoint_comm->probe_name_len +
1151 location_tracepoint_comm->provider_name_len +
1152 location_tracepoint_comm->binary_path_len;
1153
1154 if (buffer->size < expected_size) {
1155 ret = -LTTNG_ERR_INVALID;
1156 goto end;
1157 }
1158
1159 probe_name_src = buffer->data + sizeof(*location_tracepoint_comm);
1160 provider_name_src = probe_name_src +
1161 location_tracepoint_comm->probe_name_len;
1162 binary_path_src = provider_name_src +
1163 location_tracepoint_comm->provider_name_len;
1164
1165 if (probe_name_src[location_tracepoint_comm->probe_name_len - 1] != '\0') {
1166 ret = -LTTNG_ERR_INVALID;
1167 goto end;
1168 }
1169
1170 if (provider_name_src[location_tracepoint_comm->provider_name_len - 1] != '\0') {
1171 ret = -LTTNG_ERR_INVALID;
1172 goto end;
1173 }
1174
1175 if (binary_path_src[location_tracepoint_comm->binary_path_len - 1] != '\0') {
1176 ret = -LTTNG_ERR_INVALID;
1177 goto end;
1178 }
1179
1180 probe_name = lttng_strndup(probe_name_src, LTTNG_SYMBOL_NAME_LEN);
1181 if (!probe_name) {
1182 PERROR("lttng_strndup");
1183 goto end;
1184 }
1185 provider_name = lttng_strndup(provider_name_src, LTTNG_SYMBOL_NAME_LEN);
1186 if (!provider_name) {
1187 PERROR("lttng_strndup");
1188 goto end;
1189 }
1190
1191 binary_path = lttng_strndup(binary_path_src, LTTNG_SYMBOL_NAME_LEN);
1192 if (!binary_path) {
1193 PERROR("lttng_strndup");
1194 goto end;
1195 }
1196
1197 *location = lttng_userspace_probe_location_tracepoint_create_no_check(
1198 binary_path, provider_name, probe_name, NULL, false);
1199 if (!(*location)) {
1200 ret = -LTTNG_ERR_INVALID;
1201 goto end;
1202 }
1203
1204 ret = (int) expected_size;
1205end:
1206 free(probe_name);
1207 free(provider_name);
1208 free(binary_path);
1209 return ret;
1210}
1211
1212static
1213int lttng_userspace_probe_location_lookup_method_create_from_buffer(
1214 struct lttng_buffer_view *buffer,
1215 struct lttng_userspace_probe_location_lookup_method **lookup_method)
1216{
1217 int ret;
1218 struct lttng_userspace_probe_location_lookup_method_comm *lookup_comm;
1219 enum lttng_userspace_probe_location_lookup_method_type type;
1220
1221 assert(buffer);
1222 assert(buffer->data);
1223 assert(lookup_method);
1224
1225 if (buffer->size < sizeof(*lookup_comm)) {
1226 ret = -LTTNG_ERR_INVALID;
1227 goto end;
1228 }
1229
1230 lookup_comm = (struct lttng_userspace_probe_location_lookup_method_comm *)
1231 buffer->data;
1232 type = (enum lttng_userspace_probe_location_lookup_method_type)
1233 lookup_comm->type;
1234 switch (type) {
1235 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT:
1236 *lookup_method = NULL;
1237 break;
1238 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
1239 *lookup_method =
1240 lttng_userspace_probe_location_lookup_method_function_elf_create();
1241 if (!(*lookup_method)) {
1242 ret = -LTTNG_ERR_INVALID;
1243 goto end;
1244 }
1245 break;
1246 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
1247 *lookup_method =
1248 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
1249 if (!(*lookup_method)) {
1250 ret = -LTTNG_ERR_INVALID;
1251 goto end;
1252 }
1253 break;
1254 default:
1255 ret = -LTTNG_ERR_INVALID;
1256 goto end;
1257 }
1258
1259 ret = sizeof(*lookup_comm);
1260end:
1261 return ret;
1262}
1263
1264LTTNG_HIDDEN
1265int lttng_userspace_probe_location_create_from_buffer(
1266 const struct lttng_buffer_view *buffer,
1267 struct lttng_userspace_probe_location **location)
1268{
1269 struct lttng_userspace_probe_location_lookup_method *lookup_method;
1270 struct lttng_userspace_probe_location_comm *probe_location_comm;
1271 enum lttng_userspace_probe_location_type type;
1272 struct lttng_buffer_view lookup_method_view;
1273 int consumed = 0;
1274 int ret;
1275
1276
1277 assert(buffer);
1278 assert(buffer->data);
1279 assert(location);
1280
1281 lookup_method = NULL;
1282
1283 if (buffer->size <= sizeof(*probe_location_comm)) {
1284 ret = -LTTNG_ERR_INVALID;
1285 goto end;
1286 }
1287
1288 probe_location_comm =
1289 (struct lttng_userspace_probe_location_comm *) buffer->data;
1290 type = (enum lttng_userspace_probe_location_type) probe_location_comm->type;
1291 consumed += sizeof(*probe_location_comm);
1292
1293 switch (type) {
1294 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
1295 {
1296 struct lttng_buffer_view view = lttng_buffer_view_from_view(
1297 buffer, consumed, buffer->size - consumed);
1298
1299 ret = lttng_userspace_probe_location_function_create_from_buffer(
1300 &view, location);
1301 if (ret < 0) {
1302 goto end;
1303 }
1304 break;
1305 }
1306 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
1307 {
1308 struct lttng_buffer_view view = lttng_buffer_view_from_view(
1309 buffer, consumed, buffer->size - consumed);
1310
1311 ret = lttng_userspace_probe_location_tracepoint_create_from_buffer(
1312 &view, location);
1313 if (ret < 0) {
1314 goto end;
1315 }
1316 break;
1317 }
1318 default:
1319 ret = -LTTNG_ERR_INVALID;
1320 goto end;
1321 }
1322
1323 consumed += ret;
1324 if (buffer->size <= consumed) {
1325 ret = -LTTNG_ERR_INVALID;
1326 goto end;
1327 }
1328
1329 lookup_method_view = lttng_buffer_view_from_view(buffer, consumed,
1330 buffer->size - consumed);
1331 ret = lttng_userspace_probe_location_lookup_method_create_from_buffer(
1332 &lookup_method_view, &lookup_method);
1333 if (ret < 0) {
1334 ret = -LTTNG_ERR_INVALID;
1335 goto end;
1336 }
1337
1338 assert(lookup_method);
1339 (*location)->lookup_method = lookup_method;
1340 lookup_method = NULL;
1341 ret += consumed;
1342end:
1343 return ret;
1344}
1345
1346LTTNG_HIDDEN
1347int lttng_userspace_probe_location_function_set_binary_fd(
1348 struct lttng_userspace_probe_location *location, int binary_fd)
1349{
1350 int ret = 0;
1351 struct lttng_userspace_probe_location_function *function_location;
1352
1353 assert(location);
1354 assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
1355
1356 function_location = container_of(location,
1357 struct lttng_userspace_probe_location_function, parent);
1358 if (function_location->binary_fd >= 0) {
1359 ret = close(function_location->binary_fd);
1360 if (ret) {
1361 PERROR("close");
1362 ret = -LTTNG_ERR_INVALID;
1363 goto end;
1364 }
1365 }
1366
1367 function_location->binary_fd = binary_fd;
1368end:
1369 return ret;
1370}
1371
1372LTTNG_HIDDEN
1373int lttng_userspace_probe_location_tracepoint_set_binary_fd(
1374 struct lttng_userspace_probe_location *location, int binary_fd)
1375{
1376 int ret = 0;
1377 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
1378
1379 assert(location);
1380 assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
1381
1382 tracepoint_location = container_of(location,
1383 struct lttng_userspace_probe_location_tracepoint, parent);
1384 if (tracepoint_location->binary_fd >= 0) {
1385 ret = close(tracepoint_location->binary_fd);
1386 if (ret) {
1387 PERROR("close");
1388 ret = -LTTNG_ERR_INVALID;
1389 goto end;
1390 }
1391 }
1392
1393 tracepoint_location->binary_fd = binary_fd;
1394end:
1395 return ret;
1396}
1397
1398static
1399int lttng_userspace_probe_location_function_flatten(
1400 const struct lttng_userspace_probe_location *location,
1401 struct lttng_dynamic_buffer *buffer)
1402{
1403 struct lttng_userspace_probe_location_lookup_method_elf flat_lookup_method;
1404 struct lttng_userspace_probe_location_function *probe_function;
1405 struct lttng_userspace_probe_location_function flat_probe;
1406 size_t function_name_len, binary_path_len;
1407 size_t padding_needed = 0;
1408 char *flat_probe_start;
1409 int storage_needed = 0;
1410 int ret;
1411
1412 assert(location);
1413
1414 if (location->lookup_method && location->lookup_method->type !=
1415 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF) {
1416 ret = -LTTNG_ERR_INVALID;
1417 goto end;
1418 }
1419
1420 probe_function = container_of(location,
1421 struct lttng_userspace_probe_location_function,
1422 parent);
1423 assert(probe_function->function_name);
1424 assert(probe_function->binary_path);
1425
1426 storage_needed +=
1427 sizeof(struct lttng_userspace_probe_location_function);
1428 function_name_len = strlen(probe_function->function_name) + 1;
1429 binary_path_len = strlen(probe_function->binary_path) + 1;
1430 storage_needed += function_name_len + binary_path_len;
1431
1432 /*
1433 * The lookup method is aligned to 64-bit within the buffer.
1434 * This is needed even if there is no lookup method since
1435 * the next structure in the buffer probably needs to be
1436 * aligned too (depending on the arch).
1437 */
1438 padding_needed = ALIGN_TO(storage_needed, sizeof(uint64_t)) - storage_needed;
1439 storage_needed += padding_needed;
1440
1441 if (location->lookup_method) {
1442 /* NOTE: elf look-up method is assumed here. */
1443 storage_needed += sizeof(struct lttng_userspace_probe_location_lookup_method_elf);
1444 }
1445
1446 if (!buffer) {
1447 ret = storage_needed;
1448 goto end;
1449 }
1450
1451 if (lttng_dynamic_buffer_get_capacity_left(buffer) < storage_needed) {
1452 ret = lttng_dynamic_buffer_set_capacity(buffer,
1453 buffer->size + storage_needed);
1454 if (ret) {
1455 goto end;
1456 }
1457 }
1458
1459 memset(&flat_probe, 0, sizeof(flat_probe));
1460
1461 flat_probe_start = buffer->data + buffer->size;
1462 flat_probe.parent.type = location->type;
1463 /*
1464 * The lookup method, if present, is the last element in the flat
1465 * representation of the probe.
1466 */
1467 if (location->lookup_method) {
1468 flat_probe.parent.lookup_method =
1469 (struct lttng_userspace_probe_location_lookup_method *)
1470 (flat_probe_start + sizeof(flat_probe) +
1471 function_name_len + binary_path_len + padding_needed);
1472 } else {
1473 flat_probe.parent.lookup_method = NULL;
1474 }
1475
1476 flat_probe.function_name = flat_probe_start + sizeof(flat_probe);
1477 flat_probe.binary_path = flat_probe.function_name + function_name_len;
1478 flat_probe.binary_fd = -1;
1479 ret = lttng_dynamic_buffer_append(buffer, &flat_probe,
1480 sizeof(flat_probe));
1481 if (ret) {
1482 goto end;
1483 }
1484
1485 ret = lttng_dynamic_buffer_append(buffer,
1486 probe_function->function_name, function_name_len);
1487 if (ret) {
1488 goto end;
1489 }
1490 ret = lttng_dynamic_buffer_append(buffer,
1491 probe_function->binary_path, binary_path_len);
1492 if (ret) {
1493 goto end;
1494 }
1495
1496 /* Insert padding before the lookup method. */
1497 ret = lttng_dynamic_buffer_set_size(buffer,
1498 buffer->size + padding_needed);
1499 if (ret) {
1500 goto end;
1501 }
1502
1503 if (!location->lookup_method) {
1504 /* Not an error, the default method is used. */
1505 ret = storage_needed;
1506 goto end;
1507 }
1508
1509 memset(&flat_lookup_method, 0, sizeof(flat_lookup_method));
1510 flat_lookup_method.parent.type =
1511 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF;
1512 ret = lttng_dynamic_buffer_append(buffer,
1513 &flat_lookup_method, sizeof(flat_lookup_method));
1514 if (ret) {
1515 goto end;
1516 }
1517 ret = storage_needed;
1518end:
1519 return ret;
1520}
1521
1522static
1523int lttng_userspace_probe_location_tracepoint_flatten(
1524 const struct lttng_userspace_probe_location *location,
1525 struct lttng_dynamic_buffer *buffer)
1526{
1527 struct lttng_userspace_probe_location_lookup_method_sdt flat_lookup_method;
1528 struct lttng_userspace_probe_location_tracepoint *probe_tracepoint;
1529 struct lttng_userspace_probe_location_tracepoint flat_probe;
1530 size_t probe_name_len, provider_name_len, binary_path_len;
1531 size_t padding_needed = 0;
1532 int storage_needed = 0;
1533 char *flat_probe_start;
1534 int ret = 0;
1535
1536 assert(location);
1537
1538 /* Only SDT tracepoints are supported at the moment */
1539 if (location->lookup_method && location->lookup_method->type !=
1540 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT) {
1541 ret = -LTTNG_ERR_INVALID;
1542 goto end;
1543 }
1544 probe_tracepoint = container_of(location,
1545 struct lttng_userspace_probe_location_tracepoint,
1546 parent);
1547 assert(probe_tracepoint->probe_name);
1548 assert(probe_tracepoint->provider_name);
1549 assert(probe_tracepoint->binary_path);
1550
1551 /* Compute the storage space needed to flatten the probe location */
1552 storage_needed += sizeof(struct lttng_userspace_probe_location_tracepoint);
1553
1554 probe_name_len = strlen(probe_tracepoint->probe_name) + 1;
1555 provider_name_len = strlen(probe_tracepoint->provider_name) + 1;
1556 binary_path_len = strlen(probe_tracepoint->binary_path) + 1;
1557
1558 storage_needed += probe_name_len + provider_name_len + binary_path_len;
1559
1560 /*
1561 * The lookup method is aligned to 64-bit within the buffer.
1562 * This is needed even if there is no lookup method since
1563 * the next structure in the buffer probably needs to be
1564 * aligned too (depending on the arch).
1565 */
1566 padding_needed = ALIGN_TO(storage_needed, sizeof(uint64_t)) - storage_needed;
1567 storage_needed += padding_needed;
1568
1569 if (location->lookup_method) {
1570 /* NOTE: elf look-up method is assumed here. */
1571 storage_needed +=
1572 sizeof(struct lttng_userspace_probe_location_lookup_method_elf);
1573 }
1574
1575 /*
1576 * If the caller set buffer to NULL, return the size of the needed buffer.
1577 */
1578 if (!buffer) {
1579 ret = storage_needed;
1580 goto end;
1581 }
1582
1583 if (lttng_dynamic_buffer_get_capacity_left(buffer) < storage_needed) {
1584 ret = lttng_dynamic_buffer_set_capacity(buffer,
1585 buffer->size + storage_needed);
1586 if (ret) {
1587 goto end;
1588 }
1589 }
1590
1591 memset(&flat_probe, 0, sizeof(flat_probe));
1592
1593 flat_probe_start = buffer->data + buffer->size;
1594 flat_probe.parent.type = location->type;
1595
1596 /*
1597 * The lookup method, if present, is the last element in the flat
1598 * representation of the probe.
1599 */
1600 if (location->lookup_method) {
1601 flat_probe.parent.lookup_method =
1602 (struct lttng_userspace_probe_location_lookup_method *)
1603 (flat_probe_start + sizeof(flat_probe) +
1604 probe_name_len + provider_name_len +
1605 binary_path_len + padding_needed);
1606 } else {
1607 flat_probe.parent.lookup_method = NULL;
1608 }
1609
1610 flat_probe.probe_name = flat_probe_start + sizeof(flat_probe);
1611 flat_probe.provider_name = flat_probe.probe_name + probe_name_len;
1612 flat_probe.binary_path = flat_probe.provider_name + provider_name_len;
1613 flat_probe.binary_fd = -1;
1614 ret = lttng_dynamic_buffer_append(buffer, &flat_probe, sizeof(flat_probe));
1615 if (ret) {
1616 goto end;
1617 }
1618
1619 /* Append all the fields to the buffer */
1620 ret = lttng_dynamic_buffer_append(buffer,
1621 probe_tracepoint->probe_name, probe_name_len);
1622 if (ret) {
1623 goto end;
1624 }
1625 ret = lttng_dynamic_buffer_append(buffer,
1626 probe_tracepoint->provider_name, provider_name_len);
1627 if (ret) {
1628 goto end;
1629 }
1630 ret = lttng_dynamic_buffer_append(buffer,
1631 probe_tracepoint->binary_path, binary_path_len);
1632 if (ret) {
1633 goto end;
1634 }
1635
1636 /* Insert padding before the lookup method. */
1637 ret = lttng_dynamic_buffer_set_size(buffer, buffer->size + padding_needed);
1638 if (ret) {
1639 goto end;
1640 }
1641
1642 if (!location->lookup_method) {
1643 /* Not an error, the default method is used. */
1644 ret = storage_needed;
1645 goto end;
1646 }
1647
1648 memset(&flat_lookup_method, 0, sizeof(flat_lookup_method));
1649
1650 flat_lookup_method.parent.type =
1651 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT;
1652 ret = lttng_dynamic_buffer_append(buffer,
1653 &flat_lookup_method, sizeof(flat_lookup_method));
1654 if (ret) {
1655 goto end;
1656 }
1657 ret = storage_needed;
1658end:
1659 return ret;
1660}
1661
1662LTTNG_HIDDEN
1663int lttng_userspace_probe_location_flatten(
1664 const struct lttng_userspace_probe_location *location,
1665 struct lttng_dynamic_buffer *buffer)
1666{
1667 int ret;
1668 if (!location) {
1669 ret = -LTTNG_ERR_INVALID;
1670 goto end;
1671 }
1672
1673 /* Only types currently supported. */
1674 switch (location->type) {
1675 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
1676 ret = lttng_userspace_probe_location_function_flatten(location, buffer);
1677 break;
1678 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
1679 ret = lttng_userspace_probe_location_tracepoint_flatten(location, buffer);
1680 break;
1681 default:
1682 ret = -LTTNG_ERR_INVALID;
1683 goto end;
1684 }
1685
1686end:
1687 return ret;
1688}
1689
1690LTTNG_HIDDEN
1691struct lttng_userspace_probe_location *lttng_userspace_probe_location_copy(
1692 const struct lttng_userspace_probe_location *location)
1693{
1694 struct lttng_userspace_probe_location *new_location = NULL;
1695 enum lttng_userspace_probe_location_type type;
1696
1697 if (!location) {
1698 goto err;
1699 }
1700
1701 type = lttng_userspace_probe_location_get_type(location);
1702 switch (type) {
1703 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
1704 new_location =
1705 lttng_userspace_probe_location_function_copy(location);
1706 if (!new_location) {
1707 goto err;
1708 }
1709 break;
1710 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
1711 new_location =
1712 lttng_userspace_probe_location_tracepoint_copy(location);
1713 if (!new_location) {
1714 goto err;
1715 }
1716 break;
1717 default:
1718 new_location = NULL;
1719 goto err;
1720 }
1721err:
1722 return new_location;
1723}
This page took 0.02878 seconds and 4 git commands to generate.