lttng-create(1): specify that `--shm-path` only applies to UST channels
[lttng-tools.git] / src / common / userspace-probe.c
CommitLineData
1ce46cfe 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
1ce46cfe 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
1ce46cfe 5 *
1ce46cfe
JG
6 */
7
e368fb43 8#include "lttng/lttng-error.h"
1ce46cfe 9#include <assert.h>
dfcfa983 10#include <common/compat/string.h>
1ce46cfe 11#include <common/error.h>
959e3c66
JR
12#include <common/hashtable/hashtable.h>
13#include <common/hashtable/utils.h>
0f7c2963
JR
14#include <common/macros.h>
15#include <common/mi-lttng.h>
16#include <common/payload-view.h>
17#include <common/payload.h>
1ce46cfe
JG
18#include <fcntl.h>
19#include <lttng/constant.h>
20#include <lttng/userspace-probe-internal.h>
dfcfa983
JR
21#include <sys/stat.h>
22#include <sys/types.h>
04db7659 23#include <unistd.h>
1ce46cfe 24
fe489250 25static
2cde0510 26int lttng_userspace_probe_location_function_set_binary_fd_handle(
fe489250 27 struct lttng_userspace_probe_location *location,
2cde0510 28 struct fd_handle *binary_fd_handle);
fe489250
JG
29
30static
2cde0510 31int lttng_userspace_probe_location_tracepoint_set_binary_fd_handle(
fe489250 32 struct lttng_userspace_probe_location *location,
2cde0510 33 struct fd_handle *binary_fd_handle);
fe489250 34
0f7c2963
JR
35static
36enum lttng_error_code lttng_userspace_probe_location_lookup_method_mi_serialize(
37 const struct lttng_userspace_probe_location_lookup_method
38 *method,
39 struct mi_writer *writer);
40
41static
42enum lttng_error_code lttng_userspace_probe_location_tracepoint_mi_serialize(
43 const struct lttng_userspace_probe_location *location,
44 struct mi_writer *writer);
45
46static
47enum lttng_error_code lttng_userspace_probe_location_function_mi_serialize(
48 const struct lttng_userspace_probe_location *location,
49 struct mi_writer *writer);
50
1ce46cfe
JG
51enum lttng_userspace_probe_location_lookup_method_type
52lttng_userspace_probe_location_lookup_method_get_type(
53 const struct lttng_userspace_probe_location_lookup_method *lookup_method)
54{
55 return lookup_method ? lookup_method->type :
56 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_UNKNOWN;
57}
58
59void lttng_userspace_probe_location_lookup_method_destroy(
60 struct lttng_userspace_probe_location_lookup_method *lookup_method)
61{
62 if (!lookup_method){
63 return;
64 }
65
5d21f143 66 free(lookup_method);
1ce46cfe
JG
67}
68
69struct lttng_userspace_probe_location_lookup_method *
70lttng_userspace_probe_location_lookup_method_function_elf_create(void)
71{
72 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
73 struct lttng_userspace_probe_location_lookup_method_elf *elf_method;
74
75 elf_method = zmalloc(sizeof(*elf_method));
76 if (!elf_method) {
77 PERROR("zmalloc");
78 goto end;
79 }
80
81 ret = &elf_method->parent;
82 ret->type = LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF;
83end:
84 return ret;
85}
86
f4d0bb2e
FD
87struct lttng_userspace_probe_location_lookup_method *
88lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create(void)
89{
90 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
91 struct lttng_userspace_probe_location_lookup_method_sdt *sdt_method;
92
93 sdt_method = zmalloc(sizeof(*sdt_method));
94 if (!sdt_method) {
95 PERROR("zmalloc");
96 goto end;
97 }
98
99 ret = &sdt_method->parent;
100 ret->type = LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT;
101end:
102 return ret;
103}
104
1ce46cfe
JG
105enum lttng_userspace_probe_location_type lttng_userspace_probe_location_get_type(
106 const struct lttng_userspace_probe_location *location)
107{
108 return location ? location->type :
109 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_UNKNOWN;
110}
111
112static
113void lttng_userspace_probe_location_function_destroy(
114 struct lttng_userspace_probe_location *location)
115{
116 struct lttng_userspace_probe_location_function *location_function = NULL;
117
118 assert(location);
119
120 location_function = container_of(location,
121 struct lttng_userspace_probe_location_function, parent);
122
123 assert(location_function);
124
125 free(location_function->function_name);
126 free(location_function->binary_path);
2cde0510 127 fd_handle_put(location_function->binary_fd_handle);
1ce46cfe
JG
128 free(location);
129}
130
f4d0bb2e
FD
131static
132void lttng_userspace_probe_location_tracepoint_destroy(
133 struct lttng_userspace_probe_location *location)
134{
135 struct lttng_userspace_probe_location_tracepoint *location_tracepoint = NULL;
136
137 assert(location);
138
139 location_tracepoint = container_of(location,
140 struct lttng_userspace_probe_location_tracepoint,
141 parent);
142
143 assert(location_tracepoint);
144
145 free(location_tracepoint->probe_name);
146 free(location_tracepoint->provider_name);
147 free(location_tracepoint->binary_path);
2cde0510 148 fd_handle_put(location_tracepoint->binary_fd_handle);
f4d0bb2e
FD
149 free(location);
150}
151
1ce46cfe
JG
152void lttng_userspace_probe_location_destroy(
153 struct lttng_userspace_probe_location *location)
154{
155 if (!location) {
156 return;
157 }
158
159 lttng_userspace_probe_location_lookup_method_destroy(
160 location->lookup_method);
161
162 switch (location->type) {
163 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
164 lttng_userspace_probe_location_function_destroy(location);
165 break;
f4d0bb2e
FD
166 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
167 lttng_userspace_probe_location_tracepoint_destroy(location);
168 break;
1ce46cfe 169 default:
adf53c67 170 abort();
1ce46cfe
JG
171 }
172}
173
dfcfa983
JR
174/* Compare two file descriptors based on their inode and device numbers. */
175static bool fd_is_equal(int a, int b)
176{
177 int ret;
178 bool is_equal = false;
179 struct stat a_stat, b_stat;
180
181 if (a < 0 && b >= 0) {
182 goto end;
183 }
184
185 if (b < 0 && a >= 0) {
186 goto end;
187 }
188
189 if (a < 0 && b < 0) {
190 if (a == -1 && b == -1) {
191 is_equal = true;
192 goto end;
193 }
194
195 /* Invalid state, abort. */
196 abort();
197 }
198
199 /* Both are valid file descriptors. */
200 ret = fstat(a, &a_stat);
201 if (ret) {
202 PERROR("Failed to fstat userspace probe location binary fd %d",
203 a);
204 goto end;
205 }
206
207 ret = fstat(b, &b_stat);
208 if (ret) {
209 PERROR("Failed to fstat userspace probe location binary fd %d",
210 b);
211 goto end;
212 }
213
214 is_equal = (a_stat.st_ino == b_stat.st_ino) &&
215 (a_stat.st_dev == b_stat.st_dev);
216
217end:
218 return is_equal;
219}
220
959e3c66
JR
221static unsigned long lttng_userspace_probe_location_function_hash(
222 const struct lttng_userspace_probe_location *location)
223{
224 unsigned long hash = hash_key_ulong(
225 (void *) LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION,
226 lttng_ht_seed);
227 struct lttng_userspace_probe_location_function *function_location =
228 container_of(location, typeof(*function_location),
229 parent);
230
231 hash ^= hash_key_str(function_location->function_name, lttng_ht_seed);
232 hash ^= hash_key_str(function_location->binary_path, lttng_ht_seed);
233 /*
234 * No need to hash on the fd. Worst comes to worse,
235 * the equal function will discriminate.
236 */
237 return hash;
238}
239
dfcfa983
JR
240static bool lttng_userspace_probe_location_function_is_equal(
241 const struct lttng_userspace_probe_location *_a,
242 const struct lttng_userspace_probe_location *_b)
243{
244 bool is_equal = false;
245 struct lttng_userspace_probe_location_function *a, *b;
246
247 a = container_of(_a, struct lttng_userspace_probe_location_function,
248 parent);
249 b = container_of(_b, struct lttng_userspace_probe_location_function,
250 parent);
251
252 if (a->instrumentation_type != b->instrumentation_type) {
253 goto end;
254 }
255
256 assert(a->function_name);
257 assert(b->function_name);
258 if (strcmp(a->function_name, b->function_name)) {
259 goto end;
260 }
261
262 assert(a->binary_path);
263 assert(b->binary_path);
264 if (strcmp(a->binary_path, b->binary_path)) {
265 goto end;
266 }
267
2cde0510
JG
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);
dfcfa983
JR
270end:
271 return is_equal;
272}
273
1ce46cfe
JG
274static struct lttng_userspace_probe_location *
275lttng_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,
278 bool open_binary)
279{
280 int binary_fd = -1;
fe489250 281 struct fd_handle *binary_fd_handle = NULL;
1ce46cfe
JG
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;
285
286 if (open_binary) {
287 binary_fd = open(binary_path, O_RDONLY);
288 if (binary_fd < 0) {
289 PERROR("Error opening the binary");
290 goto error;
291 }
fe489250
JG
292
293 binary_fd_handle = fd_handle_create(binary_fd);
294 if (!binary_fd) {
295 goto error;
296 }
297
298 /* Ownership transferred to fd_handle. */
1ce46cfe
JG
299 binary_fd = -1;
300 }
301
302 function_name_copy = lttng_strndup(function_name, LTTNG_SYMBOL_NAME_LEN);
303 if (!function_name_copy) {
304 PERROR("Error duplicating the function name");
305 goto error;
306 }
307
308 binary_path_copy = lttng_strndup(binary_path, LTTNG_PATH_MAX);
309 if (!binary_path_copy) {
310 PERROR("Error duplicating the function name");
311 goto error;
312 }
313
314 location = zmalloc(sizeof(*location));
315 if (!location) {
316 PERROR("Error allocating userspace probe location");
317 goto error;
318 }
319
320 location->function_name = function_name_copy;
321 location->binary_path = binary_path_copy;
2cde0510 322 location->binary_fd_handle = binary_fd_handle;
fe489250 323 binary_fd_handle = NULL;
9d3981b5
JG
324 location->instrumentation_type =
325 LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_ENTRY;
1ce46cfe
JG
326
327 ret = &location->parent;
328 ret->lookup_method = lookup_method;
329 ret->type = LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION;
dfcfa983 330 ret->equal = lttng_userspace_probe_location_function_is_equal;
959e3c66 331 ret->hash = lttng_userspace_probe_location_function_hash;
1ce46cfe
JG
332 goto end;
333
334error:
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");
340 }
341 }
fe489250 342 fd_handle_put(binary_fd_handle);
1ce46cfe
JG
343end:
344 return ret;
345}
346
959e3c66
JR
347static unsigned long lttng_userspace_probe_location_tracepoint_hash(
348 const struct lttng_userspace_probe_location *location)
349{
350 unsigned long hash = hash_key_ulong(
351 (void *) LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT,
352 lttng_ht_seed);
353 struct lttng_userspace_probe_location_tracepoint *tp_location =
354 container_of(location, typeof(*tp_location), parent);
355
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);
359 /*
360 * No need to hash on the fd. Worst comes to worse,
361 * the equal function will discriminate.
362 */
363 return hash;
364}
365
dfcfa983
JR
366static bool lttng_userspace_probe_location_tracepoint_is_equal(
367 const struct lttng_userspace_probe_location *_a,
368 const struct lttng_userspace_probe_location *_b)
369{
370 bool is_equal = false;
371 struct lttng_userspace_probe_location_tracepoint *a, *b;
372
373 a = container_of(_a, struct lttng_userspace_probe_location_tracepoint,
374 parent);
375 b = container_of(_b, struct lttng_userspace_probe_location_tracepoint,
376 parent);
377
378 assert(a->probe_name);
379 assert(b->probe_name);
380 if (strcmp(a->probe_name, b->probe_name)) {
381 goto end;
382 }
383
384 assert(a->provider_name);
385 assert(b->provider_name);
386 if (strcmp(a->provider_name, b->provider_name)) {
387 goto end;
388 }
389
390 assert(a->binary_path);
391 assert(b->binary_path);
392 if (strcmp(a->binary_path, b->binary_path)) {
393 goto end;
394 }
395
2cde0510
JG
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);
dfcfa983
JR
398
399end:
400 return is_equal;
401}
402
f4d0bb2e
FD
403static struct lttng_userspace_probe_location *
404lttng_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,
407 bool open_binary)
408{
409 int binary_fd = -1;
fe489250 410 struct fd_handle *binary_fd_handle = NULL;
f4d0bb2e
FD
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;
416
417 if (open_binary) {
418 binary_fd = open(binary_path, O_RDONLY);
419 if (binary_fd < 0) {
420 PERROR("open");
421 goto error;
422 }
fe489250
JG
423
424 binary_fd_handle = fd_handle_create(binary_fd);
425 if (!binary_fd) {
426 goto error;
427 }
428
429 /* Ownership transferred to fd_handle. */
f4d0bb2e
FD
430 binary_fd = -1;
431 }
432
433 probe_name_copy = lttng_strndup(probe_name, LTTNG_SYMBOL_NAME_LEN);
434 if (!probe_name_copy) {
435 PERROR("lttng_strndup");
436 goto error;
437 }
438
439 provider_name_copy = lttng_strndup(provider_name, LTTNG_SYMBOL_NAME_LEN);
440 if (!provider_name_copy) {
441 PERROR("lttng_strndup");
442 goto error;
443 }
444
445 binary_path_copy = lttng_strndup(binary_path, LTTNG_PATH_MAX);
446 if (!binary_path_copy) {
447 PERROR("lttng_strndup");
448 goto error;
449 }
450
451 location = zmalloc(sizeof(*location));
452 if (!location) {
453 PERROR("zmalloc");
454 goto error;
455 }
456
457 location->probe_name = probe_name_copy;
458 location->provider_name = provider_name_copy;
459 location->binary_path = binary_path_copy;
2cde0510 460 location->binary_fd_handle = binary_fd_handle;
fe489250 461 binary_fd_handle = NULL;
f4d0bb2e
FD
462
463 ret = &location->parent;
464 ret->lookup_method = lookup_method;
465 ret->type = LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT;
dfcfa983 466 ret->equal = lttng_userspace_probe_location_tracepoint_is_equal;
959e3c66 467 ret->hash = lttng_userspace_probe_location_tracepoint_hash;
f4d0bb2e
FD
468 goto end;
469
470error:
471 free(probe_name_copy);
472 free(provider_name_copy);
7c86453b 473 free(binary_path_copy);
f4d0bb2e
FD
474 if (binary_fd >= 0) {
475 if (close(binary_fd)) {
476 PERROR("Error closing binary fd in error path");
477 }
478 }
fe489250 479 fd_handle_put(binary_fd_handle);
f4d0bb2e
FD
480end:
481 return ret;
482}
483
1ce46cfe
JG
484struct lttng_userspace_probe_location *
485lttng_userspace_probe_location_function_create(const char *binary_path,
486 const char *function_name,
487 struct lttng_userspace_probe_location_lookup_method *lookup_method)
488{
489 struct lttng_userspace_probe_location *ret = NULL;
490
491 if (!binary_path || !function_name) {
f04a8200 492 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
1ce46cfe
JG
493 goto end;
494 }
495
496 switch (lttng_userspace_probe_location_lookup_method_get_type(
497 lookup_method)) {
498 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT:
499 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
500 break;
501 default:
502 /* Invalid probe location lookup method. */
503 goto end;
504 }
505
506 ret = lttng_userspace_probe_location_function_create_no_check(
507 binary_path, function_name, lookup_method, true);
508end:
509 return ret;
510}
511
f4d0bb2e
FD
512struct lttng_userspace_probe_location *
513lttng_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)
516{
517 struct lttng_userspace_probe_location *ret = NULL;
518
519 if (!binary_path || !probe_name || !provider_name) {
f04a8200 520 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
f4d0bb2e
FD
521 goto end;
522 }
523
524 switch (lttng_userspace_probe_location_lookup_method_get_type(
525 lookup_method)) {
526 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
527 break;
528 default:
529 /* Invalid probe location lookup method. */
530 goto end;
531 }
532
533 ret = lttng_userspace_probe_location_tracepoint_create_no_check(
534 binary_path, provider_name, probe_name, lookup_method, true);
535end:
536 return ret;
537}
538
394357fe
FD
539static struct lttng_userspace_probe_location_lookup_method *
540lttng_userspace_probe_location_lookup_method_function_elf_copy(
541 const struct lttng_userspace_probe_location_lookup_method *lookup_method)
542{
543 struct lttng_userspace_probe_location_lookup_method *parent = NULL;
544 struct lttng_userspace_probe_location_lookup_method_elf *elf_method;
545
546 assert(lookup_method);
547 assert(lookup_method->type ==
548 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF);
549
550 elf_method = zmalloc(sizeof(*elf_method));
551 if (!elf_method) {
552 PERROR("Error allocating ELF userspace probe lookup method");
553 goto error;
554 }
555
556 elf_method->parent.type = lookup_method->type;
557 parent = &elf_method->parent;
558
559 goto end;
560error:
561 parent = NULL;
562end:
563 return parent;
564}
565
f4d0bb2e
FD
566static struct lttng_userspace_probe_location_lookup_method *
567lttng_userspace_probe_location_lookup_method_tracepoint_sdt_copy(
568 struct lttng_userspace_probe_location_lookup_method *lookup_method)
569{
570 struct lttng_userspace_probe_location_lookup_method *parent = NULL;
571 struct lttng_userspace_probe_location_lookup_method_sdt *sdt_method;
572
573 assert(lookup_method);
574 assert(lookup_method->type ==
575 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT);
576
577 sdt_method = zmalloc(sizeof(*sdt_method));
578 if (!sdt_method) {
579 PERROR("zmalloc");
580 goto error;
581 }
582
583 sdt_method->parent.type = lookup_method->type;
584 parent = &sdt_method->parent;
585
586 goto end;
587
588error:
589 parent = NULL;
590end:
591 return parent;
592}
593
394357fe
FD
594static struct lttng_userspace_probe_location *
595lttng_userspace_probe_location_function_copy(
596 const struct lttng_userspace_probe_location *location)
597{
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;
cc831309
FD
601 const char *binary_path = NULL;
602 const char *function_name = NULL;
fe489250 603 struct lttng_userspace_probe_location_function *function_location;
394357fe
FD
604
605 assert(location);
606 assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
fe489250
JG
607 function_location = container_of(
608 location, typeof(*function_location), parent);
394357fe 609
cc831309
FD
610 /* Get probe location fields */
611 binary_path = lttng_userspace_probe_location_function_get_binary_path(location);
394357fe 612 if (!binary_path) {
cc831309 613 ERR("Userspace probe binary path is NULL");
394357fe
FD
614 goto error;
615 }
616
cc831309 617 function_name = lttng_userspace_probe_location_function_get_function_name(location);
394357fe 618 if (!function_name) {
cc831309 619 ERR("Userspace probe function name is NULL");
394357fe
FD
620 goto error;
621 }
622
394357fe
FD
623 /*
624 * Duplicate probe location method fields
625 */
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:
630 lookup_method =
631 lttng_userspace_probe_location_lookup_method_function_elf_copy(
632 location->lookup_method);
633 if (!lookup_method) {
fe489250 634 goto error;
394357fe
FD
635 }
636 break;
637 default:
638 /* Invalid probe location lookup method. */
fe489250 639 goto error;
394357fe
FD
640 }
641
642 /* Create the probe_location */
643 new_location = lttng_userspace_probe_location_function_create_no_check(
cc831309 644 binary_path, function_name, lookup_method, false);
394357fe
FD
645 if (!new_location) {
646 goto destroy_lookup_method;
647 }
648
649 /* Set the duplicated fd to the new probe_location */
2cde0510
JG
650 if (lttng_userspace_probe_location_function_set_binary_fd_handle(new_location,
651 function_location->binary_fd_handle) < 0) {
394357fe
FD
652 goto destroy_probe_location;
653 }
654
655 goto end;
656
657destroy_probe_location:
658 lttng_userspace_probe_location_destroy(new_location);
659destroy_lookup_method:
660 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
394357fe 661error:
394357fe
FD
662 new_location = NULL;
663end:
664 return new_location;
665}
666
f4d0bb2e
FD
667static struct lttng_userspace_probe_location *
668lttng_userspace_probe_location_tracepoint_copy(
669 const struct lttng_userspace_probe_location *location)
670{
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;
cc831309
FD
674 const char *binary_path = NULL;
675 const char *probe_name = NULL;
676 const char *provider_name = NULL;
fe489250 677 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
f4d0bb2e
FD
678
679 assert(location);
680 assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
fe489250
JG
681 tracepoint_location = container_of(
682 location, typeof(*tracepoint_location), parent);
f4d0bb2e 683
fe489250 684 /* Get probe location fields */
cc831309 685 binary_path = lttng_userspace_probe_location_tracepoint_get_binary_path(location);
f4d0bb2e 686 if (!binary_path) {
cc831309 687 ERR("Userspace probe binary path is NULL");
f4d0bb2e
FD
688 goto error;
689 }
690
cc831309 691 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(location);
f4d0bb2e 692 if (!probe_name) {
cc831309 693 ERR("Userspace probe probe name is NULL");
f4d0bb2e
FD
694 goto error;
695 }
696
cc831309 697 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(location);
f4d0bb2e 698 if (!provider_name) {
cc831309 699 ERR("Userspace probe provider name is NULL");
f4d0bb2e
FD
700 goto error;
701 }
702
f4d0bb2e
FD
703 /*
704 * Duplicate probe location method fields
705 */
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:
710 lookup_method =
711 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_copy(
712 location->lookup_method);
713 if (!lookup_method) {
fe489250 714 goto error;
f4d0bb2e
FD
715 }
716 break;
717 default:
718 /* Invalid probe location lookup method. */
fe489250 719 goto error;
f4d0bb2e
FD
720 }
721
722 /* Create the probe_location */
723 new_location = lttng_userspace_probe_location_tracepoint_create_no_check(
cc831309 724 binary_path, provider_name, probe_name, lookup_method, false);
f4d0bb2e
FD
725 if (!new_location) {
726 goto destroy_lookup_method;
727 }
728
729 /* Set the duplicated fd to the new probe_location */
2cde0510
JG
730 if (lttng_userspace_probe_location_tracepoint_set_binary_fd_handle(new_location,
731 tracepoint_location->binary_fd_handle) < 0) {
f4d0bb2e
FD
732 goto destroy_probe_location;
733 }
734
735 goto end;
736
737destroy_probe_location:
738 lttng_userspace_probe_location_destroy(new_location);
739destroy_lookup_method:
740 lttng_userspace_probe_location_lookup_method_destroy(lookup_method);
f4d0bb2e 741error:
f4d0bb2e
FD
742 new_location = NULL;
743end:
744 return new_location;
745}
746
1ce46cfe
JG
747const char *lttng_userspace_probe_location_function_get_binary_path(
748 const struct lttng_userspace_probe_location *location)
749{
750 const char *ret = NULL;
751 struct lttng_userspace_probe_location_function *function_location;
752
753 if (!location || lttng_userspace_probe_location_get_type(location) !=
754 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
f04a8200 755 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
1ce46cfe
JG
756 goto end;
757 }
758
759 function_location = container_of(location,
760 struct lttng_userspace_probe_location_function,
761 parent);
762 ret = function_location->binary_path;
763end:
764 return ret;
765}
766
f4d0bb2e
FD
767const char *lttng_userspace_probe_location_tracepoint_get_binary_path(
768 const struct lttng_userspace_probe_location *location)
769{
770 const char *ret = NULL;
771 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
772
773 if (!location || lttng_userspace_probe_location_get_type(location) !=
774 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
f04a8200 775 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
f4d0bb2e
FD
776 goto end;
777 }
778
779 tracepoint_location = container_of(location,
780 struct lttng_userspace_probe_location_tracepoint,
781 parent);
782 ret = tracepoint_location->binary_path;
783end:
784 return ret;
785}
786
1ce46cfe
JG
787const char *lttng_userspace_probe_location_function_get_function_name(
788 const struct lttng_userspace_probe_location *location)
789{
790 const char *ret = NULL;
791 struct lttng_userspace_probe_location_function *function_location;
792
793 if (!location || lttng_userspace_probe_location_get_type(location) !=
794 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
f04a8200 795 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
1ce46cfe
JG
796 goto end;
797 }
798
799 function_location = container_of(location,
800 struct lttng_userspace_probe_location_function, parent);
801 ret = function_location->function_name;
802end:
803 return ret;
804}
805
f4d0bb2e
FD
806const char *lttng_userspace_probe_location_tracepoint_get_probe_name(
807 const struct lttng_userspace_probe_location *location)
808{
809 const char *ret = NULL;
810 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
811
812 if (!location || lttng_userspace_probe_location_get_type(location) !=
813 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
f04a8200 814 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
f4d0bb2e
FD
815 goto end;
816 }
817
818 tracepoint_location = container_of(location,
819 struct lttng_userspace_probe_location_tracepoint, parent);
820 ret = tracepoint_location->probe_name;
821end:
822 return ret;
823}
824
825const char *lttng_userspace_probe_location_tracepoint_get_provider_name(
826 const struct lttng_userspace_probe_location *location)
827{
828 const char *ret = NULL;
829 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
830
831 if (!location || lttng_userspace_probe_location_get_type(location) !=
832 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
f04a8200 833 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
f4d0bb2e
FD
834 goto end;
835 }
836
837 tracepoint_location = container_of(location,
838 struct lttng_userspace_probe_location_tracepoint, parent);
839 ret = tracepoint_location->provider_name;
840end:
841 return ret;
842}
843
1ce46cfe
JG
844int lttng_userspace_probe_location_function_get_binary_fd(
845 const struct lttng_userspace_probe_location *location)
846{
847 int ret = -1;
848 struct lttng_userspace_probe_location_function *function_location;
849
850 if (!location || lttng_userspace_probe_location_get_type(location) !=
851 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
f04a8200 852 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
1ce46cfe
JG
853 goto end;
854 }
855
856 function_location = container_of(location,
857 struct lttng_userspace_probe_location_function, parent);
2cde0510
JG
858 ret = function_location->binary_fd_handle ?
859 fd_handle_get_fd(function_location->binary_fd_handle) : -1;
1ce46cfe
JG
860end:
861 return ret;
862}
863
9d3981b5
JG
864enum lttng_userspace_probe_location_function_instrumentation_type
865lttng_userspace_probe_location_function_get_instrumentation_type(
866 const struct lttng_userspace_probe_location *location)
867{
868 enum lttng_userspace_probe_location_function_instrumentation_type type;
869 struct lttng_userspace_probe_location_function *function_location;
870
871 if (!location || lttng_userspace_probe_location_get_type(location) !=
872 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
f04a8200 873 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
9d3981b5
JG
874 type = LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_UNKNOWN;
875 goto end;
876 }
877
878 function_location = container_of(location,
879 struct lttng_userspace_probe_location_function, parent);
880 type = function_location->instrumentation_type;
881end:
882 return type;
883}
884
885enum lttng_userspace_probe_location_status
886lttng_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)
889{
890 enum lttng_userspace_probe_location_status status =
891 LTTNG_USERSPACE_PROBE_LOCATION_STATUS_OK;
892 struct lttng_userspace_probe_location_function *function_location;
893
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) {
f04a8200 898 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
9d3981b5
JG
899 status = LTTNG_USERSPACE_PROBE_LOCATION_STATUS_INVALID;
900 goto end;
901 }
902
903 function_location = container_of(location,
904 struct lttng_userspace_probe_location_function, parent);
905 function_location->instrumentation_type = instrumentation_type;
906end:
907 return status;
908}
909
f4d0bb2e
FD
910int lttng_userspace_probe_location_tracepoint_get_binary_fd(
911 const struct lttng_userspace_probe_location *location)
912{
913 int ret = -1;
914 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
915
916 if (!location || lttng_userspace_probe_location_get_type(location) !=
917 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
f04a8200 918 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
f4d0bb2e
FD
919 goto end;
920 }
921
922 tracepoint_location = container_of(location,
923 struct lttng_userspace_probe_location_tracepoint, parent);
2cde0510
JG
924 ret = tracepoint_location->binary_fd_handle ?
925 fd_handle_get_fd(tracepoint_location->binary_fd_handle) : -1;
f4d0bb2e
FD
926end:
927 return ret;
928}
929
1ce46cfe
JG
930static struct lttng_userspace_probe_location_lookup_method *
931lttng_userspace_probe_location_function_get_lookup_method(
932 const struct lttng_userspace_probe_location *location)
933{
934 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
935
936 if (!location || lttng_userspace_probe_location_get_type(location) !=
937 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION) {
f04a8200 938 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
1ce46cfe
JG
939 goto end;
940 }
941
942 ret = location->lookup_method;
943end:
944 return ret;
945}
946
f4d0bb2e
FD
947static struct lttng_userspace_probe_location_lookup_method *
948lttng_userspace_probe_location_tracepoint_get_lookup_method(
949 const struct lttng_userspace_probe_location *location)
950{
951 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
952
953 if (!location || lttng_userspace_probe_location_get_type(location) !=
954 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT) {
f04a8200 955 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
f4d0bb2e
FD
956 goto end;
957 }
958
959 ret = location->lookup_method;
960end:
961 return ret;
962}
963
87597c2c 964const struct lttng_userspace_probe_location_lookup_method *
1ce46cfe
JG
965lttng_userspace_probe_location_get_lookup_method(
966 const struct lttng_userspace_probe_location *location)
967{
968 struct lttng_userspace_probe_location_lookup_method *ret = NULL;
969
970 assert(location);
971 switch (location->type) {
972 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
973 ret = lttng_userspace_probe_location_function_get_lookup_method(
974 location);
975 break;
f4d0bb2e
FD
976 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
977 ret = lttng_userspace_probe_location_tracepoint_get_lookup_method(
978 location);
979 break;
1ce46cfe
JG
980 default:
981 ERR("Unknowned lookup method.");
982 break;
983 }
984 return ret;
985}
986
987static
988int lttng_userspace_probe_location_lookup_method_serialize(
989 struct lttng_userspace_probe_location_lookup_method *method,
e368fb43 990 struct lttng_payload *payload)
1ce46cfe
JG
991{
992 int ret;
993 struct lttng_userspace_probe_location_lookup_method_comm
994 lookup_method_comm;
995
996 lookup_method_comm.type = (int8_t) (method ? method->type :
997 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT);
e368fb43
JG
998 if (payload) {
999 ret = lttng_dynamic_buffer_append(&payload->buffer, &lookup_method_comm,
1ce46cfe
JG
1000 sizeof(lookup_method_comm));
1001 if (ret) {
1002 goto end;
1003 }
1004 }
f4d0bb2e
FD
1005 ret = sizeof(lookup_method_comm);
1006end:
1007 return ret;
1008}
1009
1010static
1011int lttng_userspace_probe_location_function_serialize(
1012 const struct lttng_userspace_probe_location *location,
e368fb43 1013 struct lttng_payload *payload)
f4d0bb2e
FD
1014{
1015 int ret;
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;
1019
1020 assert(location);
1021 assert(lttng_userspace_probe_location_get_type(location) ==
1022 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
1023
1024 location_function = container_of(location,
1025 struct lttng_userspace_probe_location_function,
1026 parent);
1027 if (!location_function->function_name || !location_function->binary_path) {
1028 ret = -LTTNG_ERR_INVALID;
1029 goto end;
1030 }
1031
2cde0510 1032 if (payload && !location_function->binary_fd_handle) {
f4d0bb2e
FD
1033 ret = -LTTNG_ERR_INVALID;
1034 goto end;
1035 }
1036
f4d0bb2e
FD
1037 function_name_len = strlen(location_function->function_name);
1038 if (function_name_len == 0) {
1039 ret = -LTTNG_ERR_INVALID;
1040 goto end;
1041 }
1042 binary_path_len = strlen(location_function->binary_path);
1043 if (binary_path_len == 0) {
1044 ret = -LTTNG_ERR_INVALID;
1045 goto end;
1046 }
1047
1048 location_function_comm.function_name_len = function_name_len + 1;
1049 location_function_comm.binary_path_len = binary_path_len + 1;
1050
e368fb43
JG
1051 if (payload) {
1052 ret = lttng_dynamic_buffer_append(&payload->buffer,
f4d0bb2e
FD
1053 &location_function_comm,
1054 sizeof(location_function_comm));
1055 if (ret) {
1056 ret = -LTTNG_ERR_INVALID;
1057 goto end;
1058 }
e368fb43 1059 ret = lttng_dynamic_buffer_append(&payload->buffer,
f4d0bb2e
FD
1060 location_function->function_name,
1061 location_function_comm.function_name_len);
1062 if (ret) {
1063 ret = -LTTNG_ERR_INVALID;
1064 goto end;
1065 }
e368fb43 1066 ret = lttng_dynamic_buffer_append(&payload->buffer,
f4d0bb2e
FD
1067 location_function->binary_path,
1068 location_function_comm.binary_path_len);
1069 if (ret) {
1070 ret = -LTTNG_ERR_INVALID;
1071 goto end;
1072 }
fe489250 1073 ret = lttng_payload_push_fd_handle(
2cde0510 1074 payload, location_function->binary_fd_handle);
e368fb43
JG
1075 if (ret) {
1076 ret = -LTTNG_ERR_INVALID;
1077 goto end;
1078 }
f4d0bb2e
FD
1079 }
1080 ret = sizeof(location_function_comm) +
1081 location_function_comm.function_name_len +
1082 location_function_comm.binary_path_len;
1ce46cfe
JG
1083end:
1084 return ret;
1085}
1086
1087static
f4d0bb2e 1088int lttng_userspace_probe_location_tracepoint_serialize(
1ce46cfe 1089 const struct lttng_userspace_probe_location *location,
e368fb43 1090 struct lttng_payload *payload)
1ce46cfe
JG
1091{
1092 int ret;
f4d0bb2e
FD
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;
1ce46cfe
JG
1096
1097 assert(location);
1098 assert(lttng_userspace_probe_location_get_type(location) ==
f4d0bb2e 1099 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
1ce46cfe 1100
f4d0bb2e
FD
1101 location_tracepoint = container_of(location,
1102 struct lttng_userspace_probe_location_tracepoint,
1ce46cfe 1103 parent);
f4d0bb2e
FD
1104 if (!location_tracepoint->probe_name ||
1105 !location_tracepoint->provider_name ||
1106 !location_tracepoint->binary_path) {
1ce46cfe
JG
1107 ret = -LTTNG_ERR_INVALID;
1108 goto end;
1109 }
1110
2cde0510 1111 if (payload && !location_tracepoint->binary_fd_handle) {
1ce46cfe
JG
1112 ret = -LTTNG_ERR_INVALID;
1113 goto end;
1114 }
1115
f4d0bb2e
FD
1116 probe_name_len = strlen(location_tracepoint->probe_name);
1117 if (probe_name_len == 0) {
1ce46cfe
JG
1118 ret = -LTTNG_ERR_INVALID;
1119 goto end;
1120 }
f4d0bb2e
FD
1121
1122 provider_name_len = strlen(location_tracepoint->provider_name);
1123 if (provider_name_len == 0) {
1124 ret = -LTTNG_ERR_INVALID;
1125 goto end;
1126 }
1127
1128 binary_path_len = strlen(location_tracepoint->binary_path);
1ce46cfe
JG
1129 if (binary_path_len == 0) {
1130 ret = -LTTNG_ERR_INVALID;
1131 goto end;
1132 }
1133
f4d0bb2e
FD
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;
1ce46cfe 1137
e368fb43
JG
1138 if (payload) {
1139 ret = lttng_dynamic_buffer_append(&payload->buffer,
f4d0bb2e
FD
1140 &location_tracepoint_comm,
1141 sizeof(location_tracepoint_comm));
1ce46cfe
JG
1142 if (ret) {
1143 ret = -LTTNG_ERR_INVALID;
1144 goto end;
1145 }
e368fb43 1146 ret = lttng_dynamic_buffer_append(&payload->buffer,
f4d0bb2e
FD
1147 location_tracepoint->probe_name,
1148 location_tracepoint_comm.probe_name_len);
1ce46cfe
JG
1149 if (ret) {
1150 ret = -LTTNG_ERR_INVALID;
1151 goto end;
1152 }
e368fb43 1153 ret = lttng_dynamic_buffer_append(&payload->buffer,
f4d0bb2e
FD
1154 location_tracepoint->provider_name,
1155 location_tracepoint_comm.provider_name_len);
1156 if (ret) {
1157 ret = -LTTNG_ERR_INVALID;
1158 goto end;
1159 }
e368fb43 1160 ret = lttng_dynamic_buffer_append(&payload->buffer,
f4d0bb2e
FD
1161 location_tracepoint->binary_path,
1162 location_tracepoint_comm.binary_path_len);
1ce46cfe
JG
1163 if (ret) {
1164 ret = -LTTNG_ERR_INVALID;
1165 goto end;
1166 }
fe489250 1167 ret = lttng_payload_push_fd_handle(
2cde0510 1168 payload, location_tracepoint->binary_fd_handle);
e368fb43
JG
1169 if (ret) {
1170 ret = -LTTNG_ERR_INVALID;
1171 goto end;
1172 }
1ce46cfe 1173 }
e368fb43 1174
f4d0bb2e
FD
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;
1ce46cfe
JG
1179end:
1180 return ret;
1181}
1182
1183LTTNG_HIDDEN
1184int lttng_userspace_probe_location_serialize(
1185 const struct lttng_userspace_probe_location *location,
e368fb43 1186 struct lttng_payload *payload)
1ce46cfe
JG
1187{
1188 int ret, buffer_use = 0;
1189 struct lttng_userspace_probe_location_comm location_generic_comm;
1190
1191 if (!location) {
f04a8200 1192 ERR("Invalid argument(s) passed to '%s'", __FUNCTION__);
1ce46cfe
JG
1193 ret = -LTTNG_ERR_INVALID;
1194 goto end;
1195 }
1196
56f0bc67
JG
1197 memset(&location_generic_comm, 0, sizeof(location_generic_comm));
1198
1ce46cfe 1199 location_generic_comm.type = (int8_t) location->type;
e368fb43
JG
1200 if (payload) {
1201 ret = lttng_dynamic_buffer_append(&payload->buffer,
1202 &location_generic_comm,
1ce46cfe
JG
1203 sizeof(location_generic_comm));
1204 if (ret) {
1205 goto end;
1206 }
1207 }
1208 buffer_use += sizeof(location_generic_comm);
1209
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(
e368fb43 1213 location, payload);
1ce46cfe 1214 break;
f4d0bb2e
FD
1215 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
1216 ret = lttng_userspace_probe_location_tracepoint_serialize(
e368fb43 1217 location, payload);
f4d0bb2e 1218 break;
1ce46cfe
JG
1219 default:
1220 ERR("Unsupported probe location type");
1221 ret = -LTTNG_ERR_INVALID;
1222 goto end;
1223 }
1224 if (ret < 0) {
1225 goto end;
1226 }
1227 buffer_use += ret;
1228
1229 ret = lttng_userspace_probe_location_lookup_method_serialize(
e368fb43 1230 location->lookup_method, payload);
1ce46cfe
JG
1231 if (ret < 0) {
1232 goto end;
1233 }
1234 ret += buffer_use;
1235end:
1236 return ret;
1237}
1238
1239static
e368fb43
JG
1240int lttng_userspace_probe_location_function_create_from_payload(
1241 struct lttng_payload_view *view,
1ce46cfe
JG
1242 struct lttng_userspace_probe_location **location)
1243{
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;
1247 int ret = 0;
e368fb43 1248 size_t expected_size;
2cde0510 1249 struct fd_handle *binary_fd_handle = lttng_payload_view_pop_fd_handle(view);
1ce46cfe 1250
1ce46cfe
JG
1251 assert(location);
1252
e368fb43
JG
1253 if (view->buffer.size < sizeof(*location_function_comm)) {
1254 ret = -LTTNG_ERR_INVALID;
1255 goto end;
1256 }
1257
1ce46cfe 1258 location_function_comm =
e368fb43 1259 (typeof(location_function_comm)) view->buffer.data;
1ce46cfe 1260
e368fb43 1261 expected_size = sizeof(*location_function_comm) +
1ce46cfe
JG
1262 location_function_comm->function_name_len +
1263 location_function_comm->binary_path_len;
1264
e368fb43 1265 if (view->buffer.size < expected_size) {
1ce46cfe
JG
1266 ret = -LTTNG_ERR_INVALID;
1267 goto end;
1268 }
1269
e368fb43 1270 function_name_src = view->buffer.data + sizeof(*location_function_comm);
1ce46cfe
JG
1271 binary_path_src = function_name_src +
1272 location_function_comm->function_name_len;
1273
48c24aea
JG
1274 if (!lttng_buffer_view_contains_string(&view->buffer, function_name_src,
1275 location_function_comm->function_name_len)) {
1ce46cfe
JG
1276 ret = -LTTNG_ERR_INVALID;
1277 goto end;
1278 }
48c24aea
JG
1279
1280 if (!lttng_buffer_view_contains_string(&view->buffer, binary_path_src,
1281 location_function_comm->binary_path_len)) {
1ce46cfe
JG
1282 ret = -LTTNG_ERR_INVALID;
1283 goto end;
1284 }
1285
1286 function_name = lttng_strndup(function_name_src, LTTNG_SYMBOL_NAME_LEN);
1287 if (!function_name) {
1288 PERROR("lttng_strndup");
2aa28610 1289 ret = -LTTNG_ERR_NOMEM;
1ce46cfe
JG
1290 goto end;
1291 }
1292
1293 binary_path = lttng_strndup(binary_path_src, LTTNG_PATH_MAX);
1294 if (!binary_path) {
1295 PERROR("lttng_strndup");
2aa28610 1296 ret = -LTTNG_ERR_NOMEM;
1ce46cfe
JG
1297 goto end;
1298 }
1299
1300 *location = lttng_userspace_probe_location_function_create_no_check(
1301 binary_path, function_name, NULL, false);
1302 if (!(*location)) {
1303 ret = -LTTNG_ERR_INVALID;
1304 goto end;
1305 }
1306
2cde0510
JG
1307 ret = lttng_userspace_probe_location_function_set_binary_fd_handle(
1308 *location, binary_fd_handle);
e368fb43 1309 if (ret) {
e368fb43
JG
1310 ret = -LTTNG_ERR_INVALID;
1311 goto end;
1312 }
1313
1ce46cfe
JG
1314 ret = (int) expected_size;
1315end:
2cde0510 1316 fd_handle_put(binary_fd_handle);
1ce46cfe
JG
1317 free(function_name);
1318 free(binary_path);
1319 return ret;
1320}
1321
f4d0bb2e 1322static
e368fb43
JG
1323int lttng_userspace_probe_location_tracepoint_create_from_payload(
1324 struct lttng_payload_view *view,
f4d0bb2e
FD
1325 struct lttng_userspace_probe_location **location)
1326{
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;
1330 int ret = 0;
e368fb43 1331 size_t expected_size;
2cde0510 1332 struct fd_handle *binary_fd_handle = lttng_payload_view_pop_fd_handle(view);
f4d0bb2e 1333
f4d0bb2e
FD
1334 assert(location);
1335
2cde0510 1336 if (!binary_fd_handle) {
e368fb43
JG
1337 ret = -LTTNG_ERR_INVALID;
1338 goto end;
1339 }
1340
1341 if (view->buffer.size < sizeof(*location_tracepoint_comm)) {
1342 ret = -LTTNG_ERR_INVALID;
1343 goto end;
1344 }
1345
f4d0bb2e 1346 location_tracepoint_comm =
e368fb43 1347 (typeof(location_tracepoint_comm)) view->buffer.data;
f4d0bb2e 1348
e368fb43 1349 expected_size = sizeof(*location_tracepoint_comm) +
f4d0bb2e
FD
1350 location_tracepoint_comm->probe_name_len +
1351 location_tracepoint_comm->provider_name_len +
1352 location_tracepoint_comm->binary_path_len;
1353
e368fb43 1354 if (view->buffer.size < expected_size) {
f4d0bb2e
FD
1355 ret = -LTTNG_ERR_INVALID;
1356 goto end;
1357 }
1358
e368fb43 1359 probe_name_src = view->buffer.data + sizeof(*location_tracepoint_comm);
f4d0bb2e
FD
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;
1364
48c24aea
JG
1365 if (!lttng_buffer_view_contains_string(&view->buffer, probe_name_src,
1366 location_tracepoint_comm->probe_name_len)) {
f4d0bb2e
FD
1367 ret = -LTTNG_ERR_INVALID;
1368 goto end;
1369 }
1370
48c24aea
JG
1371 if (!lttng_buffer_view_contains_string(&view->buffer, provider_name_src,
1372 location_tracepoint_comm->provider_name_len)) {
f4d0bb2e
FD
1373 ret = -LTTNG_ERR_INVALID;
1374 goto end;
1375 }
1376
48c24aea
JG
1377 if (!lttng_buffer_view_contains_string(&view->buffer, binary_path_src,
1378 location_tracepoint_comm->binary_path_len)) {
f4d0bb2e
FD
1379 ret = -LTTNG_ERR_INVALID;
1380 goto end;
1381 }
1382
1383 probe_name = lttng_strndup(probe_name_src, LTTNG_SYMBOL_NAME_LEN);
1384 if (!probe_name) {
8434f919
JG
1385 PERROR("Failed to allocate probe name");
1386 ret = -LTTNG_ERR_INVALID;
f4d0bb2e
FD
1387 goto end;
1388 }
1389 provider_name = lttng_strndup(provider_name_src, LTTNG_SYMBOL_NAME_LEN);
1390 if (!provider_name) {
8434f919
JG
1391 PERROR("Failed to allocate provider name");
1392 ret = -LTTNG_ERR_INVALID;
f4d0bb2e
FD
1393 goto end;
1394 }
1395
72a77198 1396 binary_path = lttng_strndup(binary_path_src, LTTNG_PATH_MAX);
f4d0bb2e 1397 if (!binary_path) {
8434f919
JG
1398 PERROR("Failed to allocate binary path");
1399 ret = -LTTNG_ERR_INVALID;
f4d0bb2e
FD
1400 goto end;
1401 }
1402
1403 *location = lttng_userspace_probe_location_tracepoint_create_no_check(
1404 binary_path, provider_name, probe_name, NULL, false);
1405 if (!(*location)) {
1406 ret = -LTTNG_ERR_INVALID;
1407 goto end;
1408 }
1409
2cde0510
JG
1410 ret = lttng_userspace_probe_location_tracepoint_set_binary_fd_handle(
1411 *location, binary_fd_handle);
e368fb43 1412 if (ret) {
e368fb43
JG
1413 ret = -LTTNG_ERR_INVALID;
1414 goto end;
1415 }
1416
f4d0bb2e
FD
1417 ret = (int) expected_size;
1418end:
2cde0510 1419 fd_handle_put(binary_fd_handle);
f4d0bb2e
FD
1420 free(probe_name);
1421 free(provider_name);
1422 free(binary_path);
1423 return ret;
1424}
1425
1ce46cfe 1426static
e368fb43
JG
1427int lttng_userspace_probe_location_lookup_method_create_from_payload(
1428 struct lttng_payload_view *view,
1ce46cfe
JG
1429 struct lttng_userspace_probe_location_lookup_method **lookup_method)
1430{
1431 int ret;
1432 struct lttng_userspace_probe_location_lookup_method_comm *lookup_comm;
1433 enum lttng_userspace_probe_location_lookup_method_type type;
1434
e368fb43 1435 assert(view);
1ce46cfe
JG
1436 assert(lookup_method);
1437
e368fb43 1438 if (view->buffer.size < sizeof(*lookup_comm)) {
1ce46cfe
JG
1439 ret = -LTTNG_ERR_INVALID;
1440 goto end;
1441 }
1442
e368fb43 1443 lookup_comm = (typeof(lookup_comm)) view->buffer.data;
1ce46cfe
JG
1444 type = (enum lttng_userspace_probe_location_lookup_method_type)
1445 lookup_comm->type;
1446 switch (type) {
1447 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT:
1448 *lookup_method = NULL;
1449 break;
1450 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
1451 *lookup_method =
1452 lttng_userspace_probe_location_lookup_method_function_elf_create();
1453 if (!(*lookup_method)) {
1454 ret = -LTTNG_ERR_INVALID;
1455 goto end;
1456 }
1457 break;
f4d0bb2e
FD
1458 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
1459 *lookup_method =
1460 lttng_userspace_probe_location_lookup_method_tracepoint_sdt_create();
1461 if (!(*lookup_method)) {
1462 ret = -LTTNG_ERR_INVALID;
1463 goto end;
1464 }
1465 break;
1ce46cfe
JG
1466 default:
1467 ret = -LTTNG_ERR_INVALID;
1468 goto end;
1469 }
1470
1471 ret = sizeof(*lookup_comm);
1472end:
1473 return ret;
1474}
1475
1476LTTNG_HIDDEN
e368fb43
JG
1477int lttng_userspace_probe_location_create_from_payload(
1478 struct lttng_payload_view *view,
1ce46cfe
JG
1479 struct lttng_userspace_probe_location **location)
1480{
1481 struct lttng_userspace_probe_location_lookup_method *lookup_method;
1ce46cfe 1482 enum lttng_userspace_probe_location_type type;
1ce46cfe
JG
1483 int consumed = 0;
1484 int ret;
3e6e0df2
JG
1485 struct lttng_userspace_probe_location_comm *probe_location_comm;
1486 struct lttng_payload_view probe_location_comm_view =
1487 lttng_payload_view_from_view(
1488 view, 0, sizeof(*probe_location_comm));
1ce46cfe 1489
e368fb43 1490 assert(view);
1ce46cfe
JG
1491 assert(location);
1492
1493 lookup_method = NULL;
1494
3e6e0df2 1495 if (!lttng_payload_view_is_valid(&probe_location_comm_view)) {
1ce46cfe
JG
1496 ret = -LTTNG_ERR_INVALID;
1497 goto end;
1498 }
1499
3e6e0df2 1500 probe_location_comm = (typeof(probe_location_comm)) probe_location_comm_view.buffer.data;
1ce46cfe
JG
1501 type = (enum lttng_userspace_probe_location_type) probe_location_comm->type;
1502 consumed += sizeof(*probe_location_comm);
1503
1504 switch (type) {
1505 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
1506 {
e368fb43
JG
1507 struct lttng_payload_view location_view =
1508 lttng_payload_view_from_view(
1509 view, consumed, -1);
1ce46cfe 1510
e368fb43
JG
1511 ret = lttng_userspace_probe_location_function_create_from_payload(
1512 &location_view, location);
1ce46cfe
JG
1513 if (ret < 0) {
1514 goto end;
1515 }
1516 break;
1517 }
f4d0bb2e
FD
1518 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
1519 {
e368fb43
JG
1520 struct lttng_payload_view location_view =
1521 lttng_payload_view_from_view(view, consumed, -1);
f4d0bb2e 1522
e368fb43
JG
1523 ret = lttng_userspace_probe_location_tracepoint_create_from_payload(
1524 &location_view, location);
f4d0bb2e
FD
1525 if (ret < 0) {
1526 goto end;
1527 }
1528 break;
1529 }
1ce46cfe
JG
1530 default:
1531 ret = -LTTNG_ERR_INVALID;
1532 goto end;
1533 }
1534
1535 consumed += ret;
e368fb43 1536 if (view->buffer.size <= consumed) {
1ce46cfe
JG
1537 ret = -LTTNG_ERR_INVALID;
1538 goto end;
1539 }
1540
e368fb43
JG
1541 {
1542 struct lttng_payload_view lookup_method_view =
1543 lttng_payload_view_from_view(
1544 view, consumed, -1);
1545
1546 ret = lttng_userspace_probe_location_lookup_method_create_from_payload(
1547 &lookup_method_view, &lookup_method);
1548 }
1ce46cfe
JG
1549 if (ret < 0) {
1550 ret = -LTTNG_ERR_INVALID;
1551 goto end;
1552 }
1553
1554 assert(lookup_method);
1555 (*location)->lookup_method = lookup_method;
1556 lookup_method = NULL;
1557 ret += consumed;
1558end:
1559 return ret;
1560}
1561
fe489250 1562static
2cde0510 1563int lttng_userspace_probe_location_function_set_binary_fd_handle(
fe489250
JG
1564 struct lttng_userspace_probe_location *location,
1565 struct fd_handle *binary_fd)
1ce46cfe
JG
1566{
1567 int ret = 0;
1568 struct lttng_userspace_probe_location_function *function_location;
1569
1570 assert(location);
1571 assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
1572
1573 function_location = container_of(location,
1574 struct lttng_userspace_probe_location_function, parent);
2cde0510 1575 fd_handle_put(function_location->binary_fd_handle);
fe489250 1576 fd_handle_get(binary_fd);
2cde0510 1577 function_location->binary_fd_handle = binary_fd;
1ce46cfe
JG
1578 return ret;
1579}
1580
fe489250 1581static
2cde0510 1582int lttng_userspace_probe_location_tracepoint_set_binary_fd_handle(
fe489250
JG
1583 struct lttng_userspace_probe_location *location,
1584 struct fd_handle *binary_fd)
f4d0bb2e
FD
1585{
1586 int ret = 0;
1587 struct lttng_userspace_probe_location_tracepoint *tracepoint_location;
1588
1589 assert(location);
1590 assert(location->type == LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
1591
1592 tracepoint_location = container_of(location,
1593 struct lttng_userspace_probe_location_tracepoint, parent);
2cde0510 1594 fd_handle_put(tracepoint_location->binary_fd_handle);
fe489250 1595 fd_handle_get(binary_fd);
2cde0510 1596 tracepoint_location->binary_fd_handle = binary_fd;
f4d0bb2e
FD
1597 return ret;
1598}
1599
1ce46cfe
JG
1600static
1601int lttng_userspace_probe_location_function_flatten(
1602 const struct lttng_userspace_probe_location *location,
1603 struct lttng_dynamic_buffer *buffer)
1604{
1605 struct lttng_userspace_probe_location_lookup_method_elf flat_lookup_method;
1606 struct lttng_userspace_probe_location_function *probe_function;
1607 struct lttng_userspace_probe_location_function flat_probe;
1608 size_t function_name_len, binary_path_len;
1609 size_t padding_needed = 0;
1610 char *flat_probe_start;
1611 int storage_needed = 0;
1612 int ret;
1613
1614 assert(location);
1615
1616 if (location->lookup_method && location->lookup_method->type !=
1617 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF) {
1618 ret = -LTTNG_ERR_INVALID;
1619 goto end;
1620 }
1621
1622 probe_function = container_of(location,
1623 struct lttng_userspace_probe_location_function,
1624 parent);
1625 assert(probe_function->function_name);
1626 assert(probe_function->binary_path);
1627
1628 storage_needed +=
1629 sizeof(struct lttng_userspace_probe_location_function);
1630 function_name_len = strlen(probe_function->function_name) + 1;
1631 binary_path_len = strlen(probe_function->binary_path) + 1;
1632 storage_needed += function_name_len + binary_path_len;
1633
1634 /*
1635 * The lookup method is aligned to 64-bit within the buffer.
1636 * This is needed even if there is no lookup method since
1637 * the next structure in the buffer probably needs to be
1638 * aligned too (depending on the arch).
1639 */
1640 padding_needed = ALIGN_TO(storage_needed, sizeof(uint64_t)) - storage_needed;
1641 storage_needed += padding_needed;
1642
1643 if (location->lookup_method) {
1644 /* NOTE: elf look-up method is assumed here. */
1645 storage_needed += sizeof(struct lttng_userspace_probe_location_lookup_method_elf);
1646 }
1647
1648 if (!buffer) {
1649 ret = storage_needed;
1650 goto end;
1651 }
1652
1653 if (lttng_dynamic_buffer_get_capacity_left(buffer) < storage_needed) {
1654 ret = lttng_dynamic_buffer_set_capacity(buffer,
1655 buffer->size + storage_needed);
1656 if (ret) {
1657 goto end;
1658 }
1659 }
1660
1661 memset(&flat_probe, 0, sizeof(flat_probe));
1662
1663 flat_probe_start = buffer->data + buffer->size;
1664 flat_probe.parent.type = location->type;
1665 /*
1666 * The lookup method, if present, is the last element in the flat
1667 * representation of the probe.
1668 */
1669 if (location->lookup_method) {
1670 flat_probe.parent.lookup_method =
1671 (struct lttng_userspace_probe_location_lookup_method *)
1672 (flat_probe_start + sizeof(flat_probe) +
1673 function_name_len + binary_path_len + padding_needed);
1674 } else {
1675 flat_probe.parent.lookup_method = NULL;
1676 }
1677
1678 flat_probe.function_name = flat_probe_start + sizeof(flat_probe);
1679 flat_probe.binary_path = flat_probe.function_name + function_name_len;
2cde0510 1680 flat_probe.binary_fd_handle = NULL;
1ce46cfe
JG
1681 ret = lttng_dynamic_buffer_append(buffer, &flat_probe,
1682 sizeof(flat_probe));
1683 if (ret) {
1684 goto end;
1685 }
1686
1687 ret = lttng_dynamic_buffer_append(buffer,
1688 probe_function->function_name, function_name_len);
1689 if (ret) {
1690 goto end;
1691 }
1692 ret = lttng_dynamic_buffer_append(buffer,
1693 probe_function->binary_path, binary_path_len);
1694 if (ret) {
1695 goto end;
1696 }
1697
1698 /* Insert padding before the lookup method. */
1699 ret = lttng_dynamic_buffer_set_size(buffer,
1700 buffer->size + padding_needed);
1701 if (ret) {
1702 goto end;
1703 }
1704
1705 if (!location->lookup_method) {
1706 /* Not an error, the default method is used. */
1707 ret = storage_needed;
1708 goto end;
1709 }
1710
1711 memset(&flat_lookup_method, 0, sizeof(flat_lookup_method));
1712 flat_lookup_method.parent.type =
1713 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF;
1714 ret = lttng_dynamic_buffer_append(buffer,
1715 &flat_lookup_method, sizeof(flat_lookup_method));
1716 if (ret) {
1717 goto end;
1718 }
1719 ret = storage_needed;
1720end:
1721 return ret;
1722}
1723
f4d0bb2e
FD
1724static
1725int lttng_userspace_probe_location_tracepoint_flatten(
1726 const struct lttng_userspace_probe_location *location,
1727 struct lttng_dynamic_buffer *buffer)
1728{
1729 struct lttng_userspace_probe_location_lookup_method_sdt flat_lookup_method;
1730 struct lttng_userspace_probe_location_tracepoint *probe_tracepoint;
1731 struct lttng_userspace_probe_location_tracepoint flat_probe;
1732 size_t probe_name_len, provider_name_len, binary_path_len;
1733 size_t padding_needed = 0;
1734 int storage_needed = 0;
1735 char *flat_probe_start;
1736 int ret = 0;
1737
1738 assert(location);
1739
1740 /* Only SDT tracepoints are supported at the moment */
1741 if (location->lookup_method && location->lookup_method->type !=
1742 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT) {
1743 ret = -LTTNG_ERR_INVALID;
1744 goto end;
1745 }
1746 probe_tracepoint = container_of(location,
1747 struct lttng_userspace_probe_location_tracepoint,
1748 parent);
1749 assert(probe_tracepoint->probe_name);
1750 assert(probe_tracepoint->provider_name);
1751 assert(probe_tracepoint->binary_path);
1752
1753 /* Compute the storage space needed to flatten the probe location */
1754 storage_needed += sizeof(struct lttng_userspace_probe_location_tracepoint);
1755
1756 probe_name_len = strlen(probe_tracepoint->probe_name) + 1;
1757 provider_name_len = strlen(probe_tracepoint->provider_name) + 1;
1758 binary_path_len = strlen(probe_tracepoint->binary_path) + 1;
1759
1760 storage_needed += probe_name_len + provider_name_len + binary_path_len;
1761
1762 /*
1763 * The lookup method is aligned to 64-bit within the buffer.
1764 * This is needed even if there is no lookup method since
1765 * the next structure in the buffer probably needs to be
1766 * aligned too (depending on the arch).
1767 */
1768 padding_needed = ALIGN_TO(storage_needed, sizeof(uint64_t)) - storage_needed;
1769 storage_needed += padding_needed;
1770
1771 if (location->lookup_method) {
1772 /* NOTE: elf look-up method is assumed here. */
1773 storage_needed +=
1774 sizeof(struct lttng_userspace_probe_location_lookup_method_elf);
1775 }
1776
1777 /*
1778 * If the caller set buffer to NULL, return the size of the needed buffer.
1779 */
1780 if (!buffer) {
1781 ret = storage_needed;
1782 goto end;
1783 }
1784
1785 if (lttng_dynamic_buffer_get_capacity_left(buffer) < storage_needed) {
1786 ret = lttng_dynamic_buffer_set_capacity(buffer,
1787 buffer->size + storage_needed);
1788 if (ret) {
1789 goto end;
1790 }
1791 }
1792
1793 memset(&flat_probe, 0, sizeof(flat_probe));
1794
1795 flat_probe_start = buffer->data + buffer->size;
1796 flat_probe.parent.type = location->type;
1797
1798 /*
1799 * The lookup method, if present, is the last element in the flat
1800 * representation of the probe.
1801 */
1802 if (location->lookup_method) {
1803 flat_probe.parent.lookup_method =
1804 (struct lttng_userspace_probe_location_lookup_method *)
1805 (flat_probe_start + sizeof(flat_probe) +
1806 probe_name_len + provider_name_len +
1807 binary_path_len + padding_needed);
1808 } else {
1809 flat_probe.parent.lookup_method = NULL;
1810 }
1811
1812 flat_probe.probe_name = flat_probe_start + sizeof(flat_probe);
1813 flat_probe.provider_name = flat_probe.probe_name + probe_name_len;
1814 flat_probe.binary_path = flat_probe.provider_name + provider_name_len;
2cde0510 1815 flat_probe.binary_fd_handle = NULL;
f4d0bb2e
FD
1816 ret = lttng_dynamic_buffer_append(buffer, &flat_probe, sizeof(flat_probe));
1817 if (ret) {
1818 goto end;
1819 }
1820
1821 /* Append all the fields to the buffer */
1822 ret = lttng_dynamic_buffer_append(buffer,
1823 probe_tracepoint->probe_name, probe_name_len);
1824 if (ret) {
1825 goto end;
1826 }
1827 ret = lttng_dynamic_buffer_append(buffer,
1828 probe_tracepoint->provider_name, provider_name_len);
1829 if (ret) {
1830 goto end;
1831 }
1832 ret = lttng_dynamic_buffer_append(buffer,
1833 probe_tracepoint->binary_path, binary_path_len);
1834 if (ret) {
1835 goto end;
1836 }
1837
1838 /* Insert padding before the lookup method. */
1839 ret = lttng_dynamic_buffer_set_size(buffer, buffer->size + padding_needed);
1840 if (ret) {
1841 goto end;
1842 }
1843
1844 if (!location->lookup_method) {
1845 /* Not an error, the default method is used. */
1846 ret = storage_needed;
1847 goto end;
1848 }
1849
1850 memset(&flat_lookup_method, 0, sizeof(flat_lookup_method));
1851
1852 flat_lookup_method.parent.type =
1853 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT;
1854 ret = lttng_dynamic_buffer_append(buffer,
1855 &flat_lookup_method, sizeof(flat_lookup_method));
1856 if (ret) {
1857 goto end;
1858 }
1859 ret = storage_needed;
1860end:
1861 return ret;
1862}
1863
1ce46cfe
JG
1864LTTNG_HIDDEN
1865int lttng_userspace_probe_location_flatten(
1866 const struct lttng_userspace_probe_location *location,
1867 struct lttng_dynamic_buffer *buffer)
1868{
1869 int ret;
1870 if (!location) {
1871 ret = -LTTNG_ERR_INVALID;
1872 goto end;
1873 }
1874
1875 /* Only types currently supported. */
1876 switch (location->type) {
1877 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
1878 ret = lttng_userspace_probe_location_function_flatten(location, buffer);
1879 break;
f4d0bb2e
FD
1880 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
1881 ret = lttng_userspace_probe_location_tracepoint_flatten(location, buffer);
1882 break;
1ce46cfe
JG
1883 default:
1884 ret = -LTTNG_ERR_INVALID;
1885 goto end;
1886 }
1887
1888end:
1889 return ret;
1890}
394357fe
FD
1891
1892LTTNG_HIDDEN
1893struct lttng_userspace_probe_location *lttng_userspace_probe_location_copy(
1894 const struct lttng_userspace_probe_location *location)
1895{
1896 struct lttng_userspace_probe_location *new_location = NULL;
1897 enum lttng_userspace_probe_location_type type;
1898
1899 if (!location) {
1900 goto err;
1901 }
1902
1903 type = lttng_userspace_probe_location_get_type(location);
1904 switch (type) {
1905 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
1906 new_location =
1907 lttng_userspace_probe_location_function_copy(location);
1908 if (!new_location) {
1909 goto err;
1910 }
1911 break;
f4d0bb2e
FD
1912 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
1913 new_location =
1914 lttng_userspace_probe_location_tracepoint_copy(location);
1915 if (!new_location) {
1916 goto err;
1917 }
1918 break;
394357fe
FD
1919 default:
1920 new_location = NULL;
1921 goto err;
1922 }
1923err:
1924 return new_location;
1925}
dfcfa983
JR
1926
1927LTTNG_HIDDEN
1928bool lttng_userspace_probe_location_lookup_method_is_equal(
1929 const struct lttng_userspace_probe_location_lookup_method *a,
1930 const struct lttng_userspace_probe_location_lookup_method *b)
1931{
1932 bool is_equal = false;
1933
1934 if (!a || !b) {
1935 goto end;
1936 }
1937
1938 if (a == b) {
1939 is_equal = true;
1940 goto end;
1941 }
1942
1943 if (a->type != b->type) {
1944 goto end;
1945 }
1946
1947 is_equal = true;
1948end:
1949 return is_equal;
1950}
1951
1952LTTNG_HIDDEN
1953bool lttng_userspace_probe_location_is_equal(
1954 const struct lttng_userspace_probe_location *a,
1955 const struct lttng_userspace_probe_location *b)
1956{
1957 bool is_equal = false;
1958
1959 if (!a || !b) {
1960 goto end;
1961 }
1962
1963 if (a == b) {
1964 is_equal = true;
1965 goto end;
1966 }
1967
1968 if (!lttng_userspace_probe_location_lookup_method_is_equal(
1969 a->lookup_method, b->lookup_method)) {
1970 goto end;
1971 }
1972
1973 if (a->type != b->type) {
1974 goto end;
1975 }
1976
1977 is_equal = a->equal ? a->equal(a, b) : true;
1978end:
1979 return is_equal;
1980}
959e3c66
JR
1981
1982LTTNG_HIDDEN
1983unsigned long lttng_userspace_probe_location_hash(
1984 const struct lttng_userspace_probe_location *location)
1985{
1986 return location->hash(location);
1987}
0f7c2963
JR
1988
1989LTTNG_HIDDEN
1990enum lttng_error_code lttng_userspace_probe_location_mi_serialize(
1991 const struct lttng_userspace_probe_location *location,
1992 struct mi_writer *writer)
1993{
1994 typedef enum lttng_error_code (*mi_fp)(
1995 const struct lttng_userspace_probe_location *,
1996 struct mi_writer *);
1997
1998 int ret;
1999 enum lttng_error_code ret_code;
2000 mi_fp mi_function = NULL;
2001
2002 assert(location);
2003 assert(writer);
2004
2005 switch (lttng_userspace_probe_location_get_type(location)) {
2006 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
2007 mi_function = lttng_userspace_probe_location_function_mi_serialize;
2008 break;
2009 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
2010 mi_function = lttng_userspace_probe_location_tracepoint_mi_serialize;
2011 break;
2012 default:
2013 abort();
2014 break;
2015 }
2016
2017 /* Open userspace probe location element. */
2018 ret = mi_lttng_writer_open_element(
2019 writer, mi_lttng_element_userspace_probe_location);
2020 if (ret) {
2021 goto mi_error;
2022 }
2023
2024 /* Underlying user space probe location. */
2025 ret_code = mi_function(location, writer);
2026 if (ret_code != LTTNG_OK) {
2027 goto end;
2028 }
2029
2030 /* Close userspace probe location element. */
2031 ret = mi_lttng_writer_close_element(writer);
2032 if (ret) {
2033 goto mi_error;
2034 }
2035
2036 ret_code = LTTNG_OK;
2037 goto end;
2038
2039mi_error:
2040 ret_code = LTTNG_ERR_MI_IO_FAIL;
2041end:
2042 return ret_code;
2043}
2044
2045enum lttng_error_code lttng_userspace_probe_location_lookup_method_mi_serialize(
2046 const struct lttng_userspace_probe_location_lookup_method
2047 *method,
2048 struct mi_writer *writer)
2049{
2050 int ret;
2051 enum lttng_error_code ret_code;
2052 const char *type_element_str;
2053
2054 assert(method);
2055 assert(writer);
2056
2057 switch (lttng_userspace_probe_location_lookup_method_get_type(method)) {
2058 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_DEFAULT:
2059 type_element_str =
2060 mi_lttng_element_userspace_probe_location_lookup_method_function_default;
2061 break;
2062 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
2063 type_element_str =
2064 mi_lttng_element_userspace_probe_location_lookup_method_function_elf;
2065 break;
2066 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
2067 type_element_str =
2068 mi_lttng_element_userspace_probe_location_lookup_method_tracepoint_sdt;
2069 break;
2070 default:
2071 abort();
2072 break;
2073 }
2074
2075 /* Open userspace probe location lookup method element. */
2076 ret = mi_lttng_writer_open_element(writer,
2077 mi_lttng_element_userspace_probe_location_lookup_method);
2078 if (ret) {
2079 goto mi_error;
2080 }
2081
2082 /* User space probe location lookup method empty element. */
2083 ret = mi_lttng_writer_open_element(writer, type_element_str);
2084 if (ret) {
2085 goto mi_error;
2086 }
2087
2088 /* Close userspace probe location lookup method element. */
2089 ret = mi_lttng_close_multi_element(writer, 2);
2090 if (ret) {
2091 goto mi_error;
2092 }
2093
2094 ret_code = LTTNG_OK;
2095 goto end;
2096
2097mi_error:
2098 ret_code = LTTNG_ERR_MI_IO_FAIL;
2099end:
2100 return ret_code;
2101}
2102
2103static enum lttng_error_code lttng_userspace_probe_location_tracepoint_mi_serialize(
2104 const struct lttng_userspace_probe_location *location,
2105 struct mi_writer *writer)
2106{
2107 int ret;
2108 enum lttng_error_code ret_code;
2109 const char *probe_name = NULL;
2110 const char *provider_name = NULL;
2111 const char *binary_path = NULL;
2112 const struct lttng_userspace_probe_location_lookup_method
2113 *lookup_method = NULL;
2114
2115 assert(location);
2116 assert(writer);
2117
2118 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(
2119 location);
2120 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(
2121 location);
2122 binary_path = lttng_userspace_probe_location_tracepoint_get_binary_path(
2123 location);
2124 lookup_method = lttng_userspace_probe_location_tracepoint_get_lookup_method(
2125 location);
2126
2127 /* Open userspace probe location tracepoint element. */
2128 ret = mi_lttng_writer_open_element(writer,
2129 mi_lttng_element_userspace_probe_location_tracepoint);
2130 if (ret) {
2131 goto mi_error;
2132 }
2133
2134 /* Probe name. */
2135 ret = mi_lttng_writer_write_element_string(writer,
2136 mi_lttng_element_userspace_probe_location_tracepoint_probe_name,
2137 probe_name);
2138 if (ret) {
2139 goto mi_error;
2140 }
2141
2142 /* Provider name. */
2143 ret = mi_lttng_writer_write_element_string(writer,
2144 mi_lttng_element_userspace_probe_location_tracepoint_provider_name,
2145 provider_name);
2146 if (ret) {
2147 goto mi_error;
2148 }
2149
2150 /* Binary path. */
2151 ret = mi_lttng_writer_write_element_string(writer,
2152 mi_lttng_element_userspace_probe_location_binary_path,
2153 binary_path);
2154 if (ret) {
2155 goto mi_error;
2156 }
2157
2158 /* The lookup method. */
2159 ret_code = lttng_userspace_probe_location_lookup_method_mi_serialize(
2160 lookup_method, writer);
2161 if (ret_code != LTTNG_OK) {
2162 goto end;
2163 }
2164
2165 /* Close userspace probe location tracepoint. */
2166 ret = mi_lttng_writer_close_element(writer);
2167 if (ret) {
2168 goto mi_error;
2169 }
2170
2171 ret_code = LTTNG_OK;
2172 goto end;
2173
2174mi_error:
2175 ret_code = LTTNG_ERR_MI_IO_FAIL;
2176end:
2177 return ret_code;
2178}
2179
2180static enum lttng_error_code lttng_userspace_probe_location_function_mi_serialize(
2181 const struct lttng_userspace_probe_location *location,
2182 struct mi_writer *writer)
2183{
2184 int ret;
2185 enum lttng_error_code ret_code;
2186 const char *function_name = NULL;
2187 const char *binary_path = NULL;
2188 const char *instrumentation_type_str = NULL;
2189 enum lttng_userspace_probe_location_function_instrumentation_type
2190 instrumentation_type;
2191 const struct lttng_userspace_probe_location_lookup_method
2192 *lookup_method = NULL;
2193
2194 assert(location);
2195 assert(writer);
2196
2197 function_name = lttng_userspace_probe_location_function_get_function_name(
2198 location);
2199 binary_path = lttng_userspace_probe_location_function_get_binary_path(
2200 location);
2201 instrumentation_type =
2202 lttng_userspace_probe_location_function_get_instrumentation_type(
2203 location);
2204 lookup_method = lttng_userspace_probe_location_function_get_lookup_method(
2205 location);
2206
2207 switch (instrumentation_type) {
2208 case LTTNG_USERSPACE_PROBE_LOCATION_FUNCTION_INSTRUMENTATION_TYPE_ENTRY:
2209 instrumentation_type_str =
2210 mi_lttng_userspace_probe_location_function_instrumentation_type_entry;
2211 break;
2212 default:
2213 abort();
2214 break;
2215 }
2216
2217 /* Open userspace probe location function element. */
2218 ret = mi_lttng_writer_open_element(writer,
2219 mi_lttng_element_userspace_probe_location_function);
2220 if (ret) {
2221 goto mi_error;
2222 }
2223
2224 /* Function name. */
2225 ret = mi_lttng_writer_write_element_string(writer,
2226 mi_lttng_element_userspace_probe_location_function_name,
2227 function_name);
2228 if (ret) {
2229 goto mi_error;
2230 }
2231
2232 /* Binary path. */
2233 ret = mi_lttng_writer_write_element_string(writer,
2234 mi_lttng_element_userspace_probe_location_binary_path,
2235 binary_path);
2236 if (ret) {
2237 goto mi_error;
2238 }
2239
2240 /* Instrumentation type. */
2241 ret = mi_lttng_writer_write_element_string(writer,
2242 mi_lttng_element_userspace_probe_location_function_instrumentation_type,
2243 instrumentation_type_str);
2244 if (ret) {
2245 goto mi_error;
2246 }
2247
2248 /* The lookup method. */
2249 ret_code = lttng_userspace_probe_location_lookup_method_mi_serialize(
2250 lookup_method, writer);
2251 if (ret_code != LTTNG_OK) {
2252 goto end;
2253 }
2254
2255 /* Close userspace probe location function element. */
2256 ret = mi_lttng_writer_close_element(writer);
2257 if (ret) {
2258 goto mi_error;
2259 }
2260
2261 ret_code = LTTNG_OK;
2262 goto end;
2263
2264mi_error:
2265 ret_code = LTTNG_ERR_MI_IO_FAIL;
2266end:
2267 return ret_code;
2268}
This page took 0.134334 seconds and 4 git commands to generate.