502ccdcf27423c0467750d0bb26b72ea70df9b64
[lttng-tools.git] / src / common / event.cpp
1 /*
2 * Copyright (C) 2018 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include "common/compat/string.h"
9 #include "common/macros.h"
10 #include "lttng/lttng-error.h"
11 #include <assert.h>
12 #include <common/buffer-view.h>
13 #include <common/dynamic-array.h>
14 #include <common/dynamic-buffer.h>
15 #include <common/error.h>
16 #include <common/sessiond-comm/sessiond-comm.h>
17 #include <common/align.h>
18 #include <lttng/constant.h>
19 #include <lttng/event-internal.h>
20 #include <lttng/event.h>
21 #include <lttng/userspace-probe-internal.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 struct event_list_element {
26 struct lttng_event *event;
27 struct lttng_event_exclusion *exclusions;
28 char *filter_expression;
29 };
30
31 static void event_list_destructor(void *ptr)
32 {
33 struct event_list_element *element = (struct event_list_element *) ptr;
34
35 free(element->filter_expression);
36 free(element->exclusions);
37 lttng_event_destroy(element->event);
38 free(element);
39 }
40
41 struct lttng_event *lttng_event_copy(const struct lttng_event *event)
42 {
43 struct lttng_event *new_event;
44 struct lttng_event_extended *new_event_extended;
45
46 new_event = (lttng_event *) zmalloc(sizeof(*event));
47 if (!new_event) {
48 PERROR("Error allocating event structure");
49 goto end;
50 }
51
52 /* Copy the content of the old event. */
53 memcpy(new_event, event, sizeof(*event));
54
55 /*
56 * We need to create a new extended since the previous pointer is now
57 * invalid.
58 */
59 new_event_extended = (lttng_event_extended *) zmalloc(sizeof(*new_event_extended));
60 if (!new_event_extended) {
61 PERROR("Error allocating event extended structure");
62 goto error;
63 }
64
65 new_event->extended.ptr = new_event_extended;
66 end:
67 return new_event;
68 error:
69 free(new_event);
70 new_event = NULL;
71 goto end;
72 }
73
74 static int lttng_event_probe_attr_serialize(
75 const struct lttng_event_probe_attr *probe,
76 struct lttng_payload *payload)
77 {
78 int ret;
79 size_t symbol_name_len;
80 struct lttng_event_probe_attr_comm comm = { 0 };
81
82 symbol_name_len = lttng_strnlen(probe->symbol_name, LTTNG_SYMBOL_NAME_LEN);
83 if (symbol_name_len == LTTNG_SYMBOL_NAME_LEN) {
84 /* Not null-termintated. */
85 ret = -1;
86 goto end;
87 }
88
89 /* Include the null terminator. */
90 symbol_name_len += 1;
91
92 comm.symbol_name_len = (uint32_t) symbol_name_len;
93 comm.addr = probe->addr;
94 comm.offset = probe->addr;
95
96 ret = lttng_dynamic_buffer_append(
97 &payload->buffer, &comm, sizeof(comm));
98 if (ret < 0) {
99 ret = -1;
100 goto end;
101 }
102
103 ret = lttng_dynamic_buffer_append(
104 &payload->buffer, probe->symbol_name, symbol_name_len);
105 end:
106 return ret;
107 }
108
109 static int lttng_event_function_attr_serialize(
110 const struct lttng_event_function_attr *function,
111 struct lttng_payload *payload)
112 {
113 int ret;
114 size_t symbol_name_len;
115 struct lttng_event_function_attr_comm comm = { 0 };
116
117 symbol_name_len = lttng_strnlen(function->symbol_name, LTTNG_SYMBOL_NAME_LEN);
118 if (symbol_name_len == LTTNG_SYMBOL_NAME_LEN) {
119 /* Not null-termintated. */
120 ret = -1;
121 goto end;
122 }
123
124 /* Include the null terminator. */
125 symbol_name_len += 1;
126
127 comm.symbol_name_len = (uint32_t) symbol_name_len;
128
129 ret = lttng_dynamic_buffer_append(
130 &payload->buffer, &comm, sizeof(comm));
131 if (ret < 0) {
132 ret = -1;
133 goto end;
134 }
135
136 ret = lttng_dynamic_buffer_append(&payload->buffer,
137 function->symbol_name, symbol_name_len);
138 end:
139 return ret;
140 }
141
142 static ssize_t lttng_event_probe_attr_create_from_payload(
143 struct lttng_payload_view *view,
144 struct lttng_event_probe_attr **probe_attr)
145 {
146 ssize_t ret, offset = 0;
147 const struct lttng_event_probe_attr_comm *comm;
148 struct lttng_event_probe_attr *local_attr = NULL;
149 struct lttng_payload_view comm_view = lttng_payload_view_from_view(
150 view, offset, sizeof(*comm));
151
152 if (!lttng_payload_view_is_valid(&comm_view)) {
153 ret = -1;
154 goto end;
155 }
156
157 comm = (typeof(comm)) comm_view.buffer.data;
158 offset += sizeof(*comm);
159
160 local_attr = (struct lttng_event_probe_attr *) zmalloc(
161 sizeof(*local_attr));
162 if (local_attr == NULL) {
163 ret = -1;
164 goto end;
165 }
166
167 local_attr->addr = comm->addr;
168 local_attr->offset = comm->offset;
169
170 {
171 const char *name;
172 struct lttng_payload_view name_view =
173 lttng_payload_view_from_view(view, offset,
174 comm->symbol_name_len);
175
176 if (!lttng_payload_view_is_valid(&name_view)) {
177 ret = -1;
178 goto end;
179 }
180
181 name = name_view.buffer.data;
182
183 if (!lttng_buffer_view_contains_string(&name_view.buffer, name,
184 comm->symbol_name_len)) {
185 ret = -1;
186 goto end;
187 }
188
189 ret = lttng_strncpy(local_attr->symbol_name, name,
190 LTTNG_SYMBOL_NAME_LEN);
191 if (ret) {
192 ret = -1;
193 goto end;
194 }
195
196 offset += comm->symbol_name_len;
197 }
198
199 *probe_attr = local_attr;
200 local_attr = NULL;
201 ret = offset;
202 end:
203 return ret;
204 }
205
206 static ssize_t lttng_event_function_attr_create_from_payload(
207 struct lttng_payload_view *view,
208 struct lttng_event_function_attr **function_attr)
209 {
210 ssize_t ret, offset = 0;
211 const struct lttng_event_function_attr_comm *comm;
212 struct lttng_event_function_attr *local_attr = NULL;
213 struct lttng_payload_view comm_view = lttng_payload_view_from_view(
214 view, offset, sizeof(*comm));
215
216 if (!lttng_payload_view_is_valid(&comm_view)) {
217 ret = -1;
218 goto end;
219 }
220
221 comm = (typeof(comm)) view->buffer.data;
222 offset += sizeof(*comm);
223
224 local_attr = (struct lttng_event_function_attr *) zmalloc(
225 sizeof(*local_attr));
226 if (local_attr == NULL) {
227 ret = -1;
228 goto end;
229 }
230
231 {
232 const char *name;
233 struct lttng_payload_view name_view =
234 lttng_payload_view_from_view(view, offset,
235 comm->symbol_name_len);
236
237 if (!lttng_payload_view_is_valid(&name_view)) {
238 ret = -1;
239 goto end;
240 }
241
242 name = name_view.buffer.data;
243
244 if (!lttng_buffer_view_contains_string(&name_view.buffer, name,
245 comm->symbol_name_len)) {
246 ret = -1;
247 goto end;
248 }
249
250 ret = lttng_strncpy(local_attr->symbol_name, name,
251 LTTNG_SYMBOL_NAME_LEN);
252 if (ret) {
253 ret = -1;
254 goto end;
255 }
256
257 offset += comm->symbol_name_len;
258 }
259
260 *function_attr = local_attr;
261 local_attr = NULL;
262 ret = offset;
263 end:
264 return ret;
265 }
266
267 static ssize_t lttng_event_exclusions_create_from_payload(
268 struct lttng_payload_view *view,
269 uint32_t count,
270 struct lttng_event_exclusion **exclusions)
271 {
272 ssize_t ret, offset = 0;
273 size_t size = (count * LTTNG_SYMBOL_NAME_LEN);
274 uint32_t i;
275 const struct lttng_event_exclusion_comm *comm;
276 struct lttng_event_exclusion *local_exclusions;
277
278 local_exclusions = (struct lttng_event_exclusion *) zmalloc(
279 sizeof(struct lttng_event_exclusion) + size);
280 if (!local_exclusions) {
281 ret = -1;
282 goto end;
283 }
284
285 local_exclusions->count = count;
286
287 for (i = 0; i < count; i++) {
288 const char *string;
289 struct lttng_buffer_view string_view;
290 const struct lttng_buffer_view comm_view =
291 lttng_buffer_view_from_view(&view->buffer,
292 offset, sizeof(*comm));
293
294 if (!lttng_buffer_view_is_valid(&comm_view)) {
295 ret = -1;
296 goto end;
297 }
298
299 comm = (typeof(comm)) comm_view.data;
300 offset += sizeof(*comm);
301
302 string_view = lttng_buffer_view_from_view(
303 &view->buffer, offset, comm->len);
304
305 if (!lttng_buffer_view_is_valid(&string_view)) {
306 ret = -1;
307 goto end;
308 }
309
310 string = string_view.data;
311
312 if (!lttng_buffer_view_contains_string(
313 &string_view, string, comm->len)) {
314 ret = -1;
315 goto end;
316 }
317
318 ret = lttng_strncpy(local_exclusions->names[i],
319 string, LTTNG_SYMBOL_NAME_LEN);
320 if (ret) {
321 ret = -1;
322 goto end;
323 }
324
325 offset += comm->len;
326 }
327
328 *exclusions = local_exclusions;
329 local_exclusions = NULL;
330 ret = offset;
331 end:
332 free(local_exclusions);
333 return ret;
334 }
335
336 ssize_t lttng_event_create_from_payload(struct lttng_payload_view *view,
337 struct lttng_event **out_event,
338 struct lttng_event_exclusion **out_exclusion,
339 char **out_filter_expression,
340 struct lttng_bytecode **out_bytecode)
341 {
342 ssize_t ret, offset = 0;
343 struct lttng_event *local_event = NULL;
344 struct lttng_event_exclusion *local_exclusions = NULL;
345 struct lttng_bytecode *local_bytecode = NULL;
346 char *local_filter_expression = NULL;
347 const struct lttng_event_comm *event_comm;
348 struct lttng_event_function_attr *local_function_attr = NULL;
349 struct lttng_event_probe_attr *local_probe_attr = NULL;
350 struct lttng_userspace_probe_location *local_userspace_probe_location =
351 NULL;
352
353 /*
354 * Only event is obligatory, the other output argument are optional and
355 * depends on what the caller is interested in.
356 */
357 assert(out_event);
358 assert(view);
359
360 {
361 struct lttng_payload_view comm_view =
362 lttng_payload_view_from_view(view, offset,
363 sizeof(*event_comm));
364
365 if (!lttng_payload_view_is_valid(&comm_view)) {
366 ret = -1;
367 goto end;
368 }
369
370 /* lttng_event_comm header */
371 event_comm = (typeof(event_comm)) comm_view.buffer.data;
372 offset += sizeof(*event_comm);
373 }
374
375 local_event = lttng_event_create();
376 if (local_event == NULL) {
377 ret = -1;
378 goto end;
379 }
380
381 local_event->type = (enum lttng_event_type) event_comm->event_type;
382 local_event->loglevel_type = (enum lttng_loglevel_type) event_comm->loglevel_type;
383 local_event->loglevel = event_comm->loglevel;
384 local_event->enabled = event_comm->enabled;
385 local_event->pid = event_comm->pid;
386 local_event->flags = (enum lttng_event_flag) event_comm->flags;
387
388 {
389 const char *name;
390 const struct lttng_buffer_view name_view =
391 lttng_buffer_view_from_view(&view->buffer,
392 offset, event_comm->name_len);
393
394 if (!lttng_buffer_view_is_valid(&name_view)) {
395 ret = -1;
396 goto end;
397 }
398
399 name = (const char *) name_view.data;
400
401 if (!lttng_buffer_view_contains_string(
402 &name_view, name, event_comm->name_len)) {
403 ret = -1;
404 goto end;
405 }
406
407 ret = lttng_strncpy(
408 local_event->name, name, LTTNG_SYMBOL_NAME_LEN);
409 if (ret) {
410 ret = -1;
411 goto end;
412 }
413
414 offset += event_comm->name_len;
415 }
416
417 /* Exclusions */
418 if (event_comm->exclusion_count == 0) {
419 goto deserialize_filter_expression;
420 }
421
422 {
423 struct lttng_payload_view exclusions_view =
424 lttng_payload_view_from_view(
425 view, offset, -1);
426
427 if (!lttng_payload_view_is_valid(&exclusions_view)) {
428 ret = -1;
429 goto end;
430 }
431
432 ret = lttng_event_exclusions_create_from_payload(&exclusions_view,
433 event_comm->exclusion_count, &local_exclusions);
434 if (ret < 0) {
435 ret = -1;
436 goto end;
437 }
438 offset += ret;
439
440 local_event->exclusion = 1;
441 }
442
443 deserialize_filter_expression:
444
445 if (event_comm->filter_expression_len == 0) {
446 if (event_comm->bytecode_len != 0) {
447 /*
448 * This is an invalid event payload.
449 *
450 * Filter expression without bytecode is possible but
451 * not the other way around.
452 * */
453 ret = -1;
454 goto end;
455 }
456 goto deserialize_event_type_payload;
457 }
458
459 {
460 const char *filter_expression_buffer;
461 struct lttng_buffer_view filter_expression_view =
462 lttng_buffer_view_from_view(&view->buffer, offset,
463 event_comm->filter_expression_len);
464
465 if (!lttng_buffer_view_is_valid(&filter_expression_view)) {
466 ret = -1;
467 goto end;
468 }
469
470 filter_expression_buffer = filter_expression_view.data;
471
472 if (!lttng_buffer_view_contains_string(&filter_expression_view,
473 filter_expression_buffer,
474 event_comm->filter_expression_len)) {
475 ret = -1;
476 goto end;
477 }
478
479 local_filter_expression = lttng_strndup(
480 filter_expression_buffer,
481 event_comm->filter_expression_len);
482 if (!local_filter_expression) {
483 ret = -1;
484 goto end;
485 }
486
487 local_event->filter = 1;
488
489 offset += event_comm->filter_expression_len;
490 }
491
492 if (event_comm->bytecode_len == 0) {
493 /*
494 * Filter expression can be present but without bytecode
495 * when dealing with event listing.
496 */
497 goto deserialize_event_type_payload;
498 }
499
500 /* Bytecode */
501 {
502 struct lttng_payload_view bytecode_view =
503 lttng_payload_view_from_view(view, offset,
504 event_comm->bytecode_len);
505
506 if (!lttng_payload_view_is_valid(&bytecode_view)) {
507 ret = -1;
508 goto end;
509 }
510
511 local_bytecode = (struct lttng_bytecode *) zmalloc(
512 event_comm->bytecode_len);
513 if (!local_bytecode) {
514 ret = -1;
515 goto end;
516 }
517
518 memcpy(local_bytecode, bytecode_view.buffer.data,
519 event_comm->bytecode_len);
520 if ((local_bytecode->len + sizeof(*local_bytecode)) !=
521 event_comm->bytecode_len) {
522 ret = -1;
523 goto end;
524 }
525
526 offset += event_comm->bytecode_len;
527 }
528
529 deserialize_event_type_payload:
530 /* Event type specific payload */
531 switch (local_event->type) {
532 case LTTNG_EVENT_FUNCTION:
533 /* Fallthrough */
534 case LTTNG_EVENT_PROBE:
535 {
536 struct lttng_payload_view probe_attr_view =
537 lttng_payload_view_from_view(view, offset,
538 event_comm->lttng_event_probe_attr_len);
539
540 if (event_comm->lttng_event_probe_attr_len == 0) {
541 ret = -1;
542 goto end;
543 }
544
545 if (!lttng_payload_view_is_valid(&probe_attr_view)) {
546 ret = -1;
547 goto end;
548 }
549
550 ret = lttng_event_probe_attr_create_from_payload(
551 &probe_attr_view, &local_probe_attr);
552 if (ret < 0 || ret != event_comm->lttng_event_probe_attr_len) {
553 ret = -1;
554 goto end;
555 }
556
557 /* Copy to the local event. */
558 memcpy(&local_event->attr.probe, local_probe_attr,
559 sizeof(local_event->attr.probe));
560
561 offset += ret;
562 break;
563 }
564 case LTTNG_EVENT_FUNCTION_ENTRY:
565 {
566 struct lttng_payload_view function_attr_view =
567 lttng_payload_view_from_view(view, offset,
568 event_comm->lttng_event_function_attr_len);
569
570 if (event_comm->lttng_event_function_attr_len == 0) {
571 ret = -1;
572 goto end;
573 }
574
575 if (!lttng_payload_view_is_valid(&function_attr_view)) {
576 ret = -1;
577 goto end;
578 }
579
580 ret = lttng_event_function_attr_create_from_payload(
581 &function_attr_view, &local_function_attr);
582 if (ret < 0 || ret != event_comm->lttng_event_function_attr_len) {
583 ret = -1;
584 goto end;
585 }
586
587 /* Copy to the local event. */
588 memcpy(&local_event->attr.ftrace, local_function_attr,
589 sizeof(local_event->attr.ftrace));
590
591 offset += ret;
592
593 break;
594 }
595 case LTTNG_EVENT_USERSPACE_PROBE:
596 {
597 struct lttng_payload_view userspace_probe_location_view =
598 lttng_payload_view_from_view(view, offset,
599 event_comm->userspace_probe_location_len);
600
601 if (event_comm->userspace_probe_location_len == 0) {
602 ret = -1;
603 goto end;
604 }
605
606 if (!lttng_payload_view_is_valid(
607 &userspace_probe_location_view)) {
608 ret = -1;
609 goto end;
610 }
611
612 ret = lttng_userspace_probe_location_create_from_payload(
613 &userspace_probe_location_view,
614 &local_userspace_probe_location);
615 if (ret < 0) {
616 WARN("Failed to create a userspace probe location from the received buffer");
617 ret = -1;
618 goto end;
619 }
620
621 if (ret != event_comm->userspace_probe_location_len) {
622 WARN("Userspace probe location from the received buffer is not the advertised length: header length = %" PRIu32 ", payload length = %lu", event_comm->userspace_probe_location_len, ret);
623 ret = -1;
624 goto end;
625 }
626
627 /* Attach the probe location to the event. */
628 ret = lttng_event_set_userspace_probe_location(
629 local_event, local_userspace_probe_location);
630 if (ret) {
631 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
632 goto end;
633 }
634
635 /*
636 * Userspace probe location object ownership transfered to the
637 * event object.
638 */
639 local_userspace_probe_location = NULL;
640 offset += event_comm->userspace_probe_location_len;
641 break;
642 }
643 case LTTNG_EVENT_TRACEPOINT:
644 /* Fallthrough */
645 case LTTNG_EVENT_ALL:
646 /* Fallthrough */
647 case LTTNG_EVENT_SYSCALL:
648 /* Fallthrough */
649 case LTTNG_EVENT_NOOP:
650 /* Nothing to do here */
651 break;
652 default:
653 ret = LTTNG_ERR_UND;
654 goto end;
655 break;
656 }
657
658 /* Transfer ownership to the caller. */
659 *out_event = local_event;
660 local_event = NULL;
661
662 if (out_bytecode) {
663 *out_bytecode = local_bytecode;
664 local_bytecode = NULL;
665 }
666
667 if (out_exclusion) {
668 *out_exclusion = local_exclusions;
669 local_exclusions = NULL;
670 }
671
672 if (out_filter_expression) {
673 *out_filter_expression = local_filter_expression;
674 local_filter_expression = NULL;
675 }
676
677 ret = offset;
678 end:
679 lttng_event_destroy(local_event);
680 lttng_userspace_probe_location_destroy(local_userspace_probe_location);
681 free(local_filter_expression);
682 free(local_exclusions);
683 free(local_bytecode);
684 free(local_function_attr);
685 free(local_probe_attr);
686 return ret;
687 }
688
689 int lttng_event_serialize(const struct lttng_event *event,
690 unsigned int exclusion_count,
691 char **exclusion_list,
692 char *filter_expression,
693 size_t bytecode_len,
694 struct lttng_bytecode *bytecode,
695 struct lttng_payload *payload)
696 {
697 int ret;
698 unsigned int i;
699 size_t header_offset, size_before_payload;
700 size_t name_len;
701 struct lttng_event_comm event_comm = { 0 };
702 struct lttng_event_comm *header;
703
704 assert(event);
705 assert(payload);
706 assert(exclusion_count == 0 || exclusion_list);
707
708 /* Save the header location for later in-place header update. */
709 header_offset = payload->buffer.size;
710
711 name_len = lttng_strnlen(event->name, LTTNG_SYMBOL_NAME_LEN);
712 if (name_len == LTTNG_SYMBOL_NAME_LEN) {
713 /* Event name is not NULL-terminated. */
714 ret = -1;
715 goto end;
716 }
717
718 /* Add null termination. */
719 name_len += 1;
720
721 if (exclusion_count > UINT32_MAX) {
722 /* Possible overflow. */
723 ret = -1;
724 goto end;
725 }
726
727 if (bytecode_len > UINT32_MAX) {
728 /* Possible overflow. */
729 ret = -1;
730 goto end;
731 }
732
733 event_comm.name_len = (uint32_t) name_len;
734 event_comm.event_type = (int8_t) event->type;
735 event_comm.loglevel_type = (int8_t) event->loglevel_type;
736 event_comm.loglevel = (int32_t) event->loglevel;
737 event_comm.enabled = (int8_t) event->enabled;
738 event_comm.pid = (int32_t) event->pid;
739 event_comm.exclusion_count = (uint32_t) exclusion_count;
740 event_comm.bytecode_len = (uint32_t) bytecode_len;
741 event_comm.flags = (int32_t) event->flags;
742
743 if (filter_expression) {
744 event_comm.filter_expression_len =
745 strlen(filter_expression) + 1;
746 }
747
748 /* Header */
749 ret = lttng_dynamic_buffer_append(
750 &payload->buffer, &event_comm, sizeof(event_comm));
751 if (ret) {
752 goto end;
753 }
754
755 /* Event name */
756 ret = lttng_dynamic_buffer_append(
757 &payload->buffer, event->name, name_len);
758 if (ret) {
759 goto end;
760 }
761
762 /* Exclusions */
763 for (i = 0; i < exclusion_count; i++) {
764 const size_t exclusion_len = lttng_strnlen(
765 *(exclusion_list + i), LTTNG_SYMBOL_NAME_LEN);
766 const struct lttng_event_exclusion_comm exclusion_header = {
767 .len = (uint32_t) exclusion_len + 1,
768 };
769
770 if (exclusion_len == LTTNG_SYMBOL_NAME_LEN) {
771 /* Exclusion is not NULL-terminated. */
772 ret = -1;
773 goto end;
774 }
775
776 ret = lttng_dynamic_buffer_append(&payload->buffer,
777 &exclusion_header, sizeof(exclusion_header));
778 if (ret) {
779 goto end;
780 }
781
782 ret = lttng_dynamic_buffer_append(&payload->buffer,
783 *(exclusion_list + i), exclusion_len + 1);
784 if (ret) {
785 goto end;
786 }
787 }
788
789 /* Filter expression and its bytecode */
790 if (filter_expression) {
791 ret = lttng_dynamic_buffer_append(&payload->buffer,
792 filter_expression,
793 event_comm.filter_expression_len);
794 if (ret) {
795 goto end;
796 }
797
798 /*
799 * Bytecode can be absent when we serialize to the client
800 * for listing.
801 */
802 if (bytecode) {
803 ret = lttng_dynamic_buffer_append(&payload->buffer,
804 bytecode, bytecode_len);
805 if (ret) {
806 goto end;
807 }
808 }
809 }
810
811 size_before_payload = payload->buffer.size;
812
813 /* Event type specific payload */
814 switch (event->type) {
815 case LTTNG_EVENT_FUNCTION:
816 /* Fallthrough */
817 case LTTNG_EVENT_PROBE:
818 ret = lttng_event_probe_attr_serialize(&event->attr.probe, payload);
819 if (ret) {
820 ret = -1;
821 goto end;
822 }
823
824 header = (struct lttng_event_comm *) ((char *) payload->buffer.data +
825 header_offset);
826 header->lttng_event_probe_attr_len =
827 payload->buffer.size - size_before_payload;
828
829 break;
830 case LTTNG_EVENT_FUNCTION_ENTRY:
831 ret = lttng_event_function_attr_serialize(
832 &event->attr.ftrace, payload);
833 if (ret) {
834 ret = -1;
835 goto end;
836 }
837
838 /* Update the lttng_event_function_attr len. */
839 header = (struct lttng_event_comm *) ((char *) payload->buffer.data +
840 header_offset);
841 header->lttng_event_function_attr_len =
842 payload->buffer.size - size_before_payload;
843
844 break;
845 case LTTNG_EVENT_USERSPACE_PROBE:
846 {
847 const struct lttng_event_extended *ev_ext =
848 (const struct lttng_event_extended *)
849 event->extended.ptr;
850
851 assert(event->extended.ptr);
852 assert(ev_ext->probe_location);
853
854 size_before_payload = payload->buffer.size;
855 if (ev_ext->probe_location) {
856 /*
857 * lttng_userspace_probe_location_serialize returns the
858 * number of bytes that were appended to the buffer.
859 */
860 ret = lttng_userspace_probe_location_serialize(
861 ev_ext->probe_location, payload);
862 if (ret < 0) {
863 goto end;
864 }
865
866 ret = 0;
867
868 /* Update the userspace probe location len. */
869 header = (struct lttng_event_comm *) ((char *) payload->buffer.data +
870 header_offset);
871 header->userspace_probe_location_len =
872 payload->buffer.size - size_before_payload;
873 }
874 break;
875 }
876 case LTTNG_EVENT_TRACEPOINT:
877 /* Fallthrough */
878 case LTTNG_EVENT_ALL:
879 /* Fallthrough */
880 default:
881 /* Nothing to do here. */
882 break;
883 }
884
885 end:
886 return ret;
887 }
888
889 static ssize_t lttng_event_context_app_populate_from_payload(
890 const struct lttng_payload_view *view,
891 struct lttng_event_context *event_ctx)
892 {
893 ssize_t ret, offset = 0;
894 const struct lttng_event_context_app_comm *comm;
895 char *provider_name = NULL, *context_name = NULL;
896 size_t provider_name_len, context_name_len;
897 const struct lttng_buffer_view comm_view = lttng_buffer_view_from_view(
898 &view->buffer, offset, sizeof(*comm));
899
900 assert(event_ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
901
902 if (!lttng_buffer_view_is_valid(&comm_view)) {
903 ret = -1;
904 goto end;
905 }
906
907 comm = (typeof(comm)) comm_view.data;
908 offset += sizeof(*comm);
909
910 provider_name_len = comm->provider_name_len;
911 context_name_len = comm->ctx_name_len;
912
913 if (provider_name_len == 0 || context_name_len == 0) {
914 /*
915 * Application provider and context names MUST
916 * be provided.
917 */
918 ret = -1;
919 goto end;
920 }
921
922 {
923 const char *name;
924 const struct lttng_buffer_view provider_name_view =
925 lttng_buffer_view_from_view(&view->buffer,
926 offset,
927 provider_name_len);
928
929 if (!lttng_buffer_view_is_valid(&provider_name_view)) {
930 ret = -1;
931 goto end;
932 }
933
934 name = provider_name_view.data;
935
936 if (!lttng_buffer_view_contains_string(&provider_name_view,
937 name, provider_name_len)) {
938 ret = -1;
939 goto end;
940 }
941
942 provider_name = lttng_strndup(name, provider_name_len);
943 if (!provider_name) {
944 ret = -1;
945 goto end;
946 }
947
948 offset += provider_name_len;
949 }
950
951 {
952 const char *name;
953 const struct lttng_buffer_view context_name_view =
954 lttng_buffer_view_from_view(
955 &view->buffer, offset,
956 context_name_len);
957
958 if (!lttng_buffer_view_is_valid(&context_name_view)) {
959 ret = -1;
960 goto end;
961 }
962
963 name = context_name_view.data;
964
965 if (!lttng_buffer_view_contains_string(&context_name_view, name,
966 context_name_len)) {
967 ret = -1;
968 goto end;
969 }
970
971 context_name = lttng_strndup(name, context_name_len);
972 if (!context_name) {
973 ret = -1;
974 goto end;
975 }
976
977 offset += context_name_len;
978 }
979
980 /* Transfer ownership of the strings */
981 event_ctx->u.app_ctx.provider_name = provider_name;
982 event_ctx->u.app_ctx.ctx_name = context_name;
983 provider_name = NULL;
984 context_name = NULL;
985
986 ret = offset;
987 end:
988 free(provider_name);
989 free(context_name);
990
991 return ret;
992 }
993
994 static ssize_t lttng_event_context_perf_counter_populate_from_payload(
995 const struct lttng_payload_view *view,
996 struct lttng_event_context *event_ctx)
997 {
998 ssize_t ret, offset = 0;
999 const struct lttng_event_context_perf_counter_comm *comm;
1000 size_t name_len;
1001 const struct lttng_buffer_view comm_view = lttng_buffer_view_from_view(
1002 &view->buffer, offset, sizeof(*comm));
1003
1004 assert(event_ctx->ctx == LTTNG_EVENT_CONTEXT_PERF_COUNTER ||
1005 event_ctx->ctx ==
1006 LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER ||
1007 event_ctx->ctx == LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER);
1008
1009 if (!lttng_buffer_view_is_valid(&comm_view)) {
1010 ret = -1;
1011 goto end;
1012 }
1013
1014 comm = (typeof(comm)) comm_view.data;
1015 offset += sizeof(*comm);
1016
1017 name_len = comm->name_len;
1018
1019 {
1020 const char *name;
1021 const struct lttng_buffer_view provider_name_view =
1022 lttng_buffer_view_from_view(
1023 &view->buffer, offset,
1024 name_len);
1025
1026 if (!lttng_buffer_view_is_valid(&provider_name_view)) {
1027 ret = -1;
1028 goto end;
1029 }
1030
1031 name = provider_name_view.data;
1032
1033 if (!lttng_buffer_view_contains_string(
1034 &provider_name_view, name, name_len)) {
1035 ret = -1;
1036 goto end;
1037 }
1038
1039 lttng_strncpy(event_ctx->u.perf_counter.name, name, name_len);
1040 offset += name_len;
1041 }
1042
1043 event_ctx->u.perf_counter.config = comm->config;
1044 event_ctx->u.perf_counter.type = comm->type;
1045
1046 ret = offset;
1047
1048 end:
1049 return ret;
1050 }
1051
1052 ssize_t lttng_event_context_create_from_payload(
1053 struct lttng_payload_view *view,
1054 struct lttng_event_context **event_ctx)
1055 {
1056 ssize_t ret, offset = 0;
1057 const struct lttng_event_context_comm *comm;
1058 struct lttng_event_context *local_context = NULL;
1059 struct lttng_buffer_view comm_view = lttng_buffer_view_from_view(
1060 &view->buffer, offset, sizeof(*comm));
1061
1062 assert(event_ctx);
1063 assert(view);
1064
1065 if (!lttng_buffer_view_is_valid(&comm_view)) {
1066 ret = -1;
1067 goto end;
1068 }
1069
1070 comm = (typeof(comm)) comm_view.data;
1071 offset += sizeof(*comm);
1072
1073 local_context = (struct lttng_event_context *)
1074 zmalloc(sizeof(*local_context));
1075 if (!local_context) {
1076 ret = -1;
1077 goto end;
1078 }
1079
1080 local_context->ctx = (lttng_event_context_type) comm->type;
1081
1082 {
1083 struct lttng_payload_view subtype_view =
1084 lttng_payload_view_from_view(view, offset, -1);
1085
1086 switch (local_context->ctx) {
1087 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
1088 ret = lttng_event_context_app_populate_from_payload(
1089 &subtype_view, local_context);
1090 break;
1091 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1092 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1093 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
1094 ret = lttng_event_context_perf_counter_populate_from_payload(
1095 &subtype_view, local_context);
1096 break;
1097 default:
1098 /* Nothing else to deserialize. */
1099 ret = 0;
1100 break;
1101 }
1102 }
1103
1104 if (ret < 0) {
1105 goto end;
1106 }
1107
1108 offset += ret;
1109
1110 *event_ctx = local_context;
1111 local_context = NULL;
1112 ret = offset;
1113
1114 end:
1115 free(local_context);
1116 return ret;
1117 }
1118
1119 static int lttng_event_context_app_serialize(
1120 struct lttng_event_context *context,
1121 struct lttng_payload *payload)
1122 {
1123 int ret;
1124 struct lttng_event_context_app_comm comm = { 0 };
1125 size_t provider_len, ctx_len;
1126 const char *provider_name;
1127 const char *ctx_name;
1128
1129 assert(payload);
1130 assert(context);
1131 assert(context->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT);
1132
1133 provider_name = context->u.app_ctx.provider_name;
1134 ctx_name = context->u.app_ctx.ctx_name;
1135
1136 if (!provider_name || !ctx_name) {
1137 ret = -LTTNG_ERR_INVALID;
1138 goto end;
1139 }
1140
1141 provider_len = strlen(provider_name);
1142 if (provider_len == 0) {
1143 ret = -LTTNG_ERR_INVALID;
1144 goto end;
1145 }
1146
1147 /* Include the null terminator. */
1148 comm.provider_name_len = provider_len + 1;
1149
1150 ctx_len = strlen(ctx_name);
1151 if (ctx_len == 0) {
1152 ret = -LTTNG_ERR_INVALID;
1153 goto end;
1154 }
1155
1156 /* Include the null terminator. */
1157 comm.ctx_name_len = ctx_len + 1;
1158
1159 /* Header */
1160 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm,
1161 sizeof(comm));
1162 if (ret) {
1163 ret = -1;
1164 goto end;
1165 }
1166
1167 ret = lttng_dynamic_buffer_append(&payload->buffer, provider_name,
1168 provider_len);
1169 if (ret) {
1170 ret = -1;
1171 goto end;
1172 }
1173
1174 ret = lttng_dynamic_buffer_append(&payload->buffer, ctx_name,
1175 ctx_len);
1176 if (ret) {
1177 ret = -1;
1178 goto end;
1179 }
1180
1181 end:
1182 return ret;
1183 }
1184
1185 static int lttng_event_context_perf_counter_serialize(
1186 struct lttng_event_perf_counter_ctx *context,
1187 struct lttng_payload *payload)
1188 {
1189 int ret;
1190 struct lttng_event_context_perf_counter_comm comm = { 0 };
1191
1192 assert(payload);
1193 assert(context);
1194
1195 comm.config = context->config;
1196 comm.type = context->type;
1197 comm.name_len = lttng_strnlen(context->name, LTTNG_SYMBOL_NAME_LEN);
1198
1199 if (comm.name_len == LTTNG_SYMBOL_NAME_LEN) {
1200 ret = -1;
1201 goto end;
1202 }
1203
1204 /* Include the null terminator. */
1205 comm.name_len += 1;
1206
1207 /* Header */
1208 ret = lttng_dynamic_buffer_append(&payload->buffer, &comm,
1209 sizeof(comm));
1210 if (ret) {
1211 ret = -1;
1212 goto end;
1213 }
1214
1215 ret = lttng_dynamic_buffer_append(&payload->buffer, context->name,
1216 comm.name_len);
1217 if (ret) {
1218 ret = -1;
1219 goto end;
1220 }
1221
1222 end:
1223 return ret;
1224 }
1225
1226 int lttng_event_context_serialize(struct lttng_event_context *context,
1227 struct lttng_payload *payload)
1228 {
1229 int ret;
1230 struct lttng_event_context_comm context_comm = { 0 };
1231
1232 assert(context);
1233 assert(payload);
1234
1235 context_comm.type = (uint32_t) context->ctx;
1236
1237 /* Header */
1238 ret = lttng_dynamic_buffer_append(
1239 &payload->buffer, &context_comm, sizeof(context_comm));
1240 if (ret) {
1241 goto end;
1242 }
1243
1244 switch (context->ctx) {
1245 case LTTNG_EVENT_CONTEXT_APP_CONTEXT:
1246 ret = lttng_event_context_app_serialize(context, payload);
1247 break;
1248 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
1249 case LTTNG_EVENT_CONTEXT_PERF_THREAD_COUNTER:
1250 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
1251 ret = lttng_event_context_perf_counter_serialize(
1252 &context->u.perf_counter, payload);
1253 break;
1254 default:
1255 /* Nothing else to serialize. */
1256 break;
1257 }
1258
1259 if (ret) {
1260 goto end;
1261 }
1262
1263 end:
1264 return ret;
1265 }
1266
1267 void lttng_event_context_destroy(struct lttng_event_context *context)
1268 {
1269 if (!context) {
1270 return;
1271 }
1272
1273 if (context->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1274 free(context->u.app_ctx.provider_name);
1275 free(context->u.app_ctx.ctx_name);
1276 }
1277
1278 free(context);
1279 }
1280
1281 static enum lttng_error_code compute_flattened_size(
1282 struct lttng_dynamic_pointer_array *events, size_t *size)
1283 {
1284 enum lttng_error_code ret_code;
1285 int ret = 0;
1286 size_t storage_req, event_count, i;
1287
1288 assert(size);
1289 assert(events);
1290
1291 event_count = lttng_dynamic_pointer_array_get_count(events);
1292
1293 /* The basic struct lttng_event */
1294 storage_req = event_count * sizeof(struct lttng_event);
1295
1296 for (i = 0; i < event_count; i++) {
1297 int probe_storage_req = 0;
1298 const struct event_list_element *element = (const struct event_list_element *)
1299 lttng_dynamic_pointer_array_get_pointer(
1300 events, i);
1301 const struct lttng_userspace_probe_location *location = NULL;
1302
1303 location = lttng_event_get_userspace_probe_location(
1304 element->event);
1305 if (location) {
1306 ret = lttng_userspace_probe_location_flatten(
1307 location, NULL);
1308 if (ret < 0) {
1309 ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL;
1310 goto end;
1311 }
1312
1313 probe_storage_req = ret;
1314 }
1315
1316 /* The struct·lttng_event_extended */
1317 storage_req += event_count *
1318 sizeof(struct lttng_event_extended);
1319
1320 if (element->filter_expression) {
1321 storage_req += strlen(element->filter_expression) + 1;
1322 }
1323
1324 if (element->exclusions) {
1325 storage_req += element->exclusions->count *
1326 LTTNG_SYMBOL_NAME_LEN;
1327 }
1328
1329 /* Padding to ensure the flat probe is aligned. */
1330 storage_req = lttng_align_ceil(storage_req, sizeof(uint64_t));
1331 storage_req += probe_storage_req;
1332 }
1333
1334 *size = storage_req;
1335 ret_code = LTTNG_OK;
1336
1337 end:
1338 return ret_code;
1339 }
1340
1341 /*
1342 * Flatten a list of struct lttng_event.
1343 *
1344 * The buffer that is returned to the API client must contain a "flat" version
1345 * of the events that are returned. In other words, all pointers within an
1346 * lttng_event must point to a location within the returned buffer so that the
1347 * user may free everything by simply calling free() on the returned buffer.
1348 * This is needed in order to maintain API compatibility.
1349 *
1350 * A first pass is performed to compute the size of the buffer that must be
1351 * allocated. A second pass is then performed to setup the returned events so
1352 * that their members always point within the buffer.
1353 *
1354 * The layout of the returned buffer is as follows:
1355 * - struct lttng_event[nb_events],
1356 * - nb_events times the following:
1357 * - struct lttng_event_extended,
1358 * - filter_expression
1359 * - exclusions
1360 * - padding to align to 64-bits
1361 * - flattened version of userspace_probe_location
1362 */
1363 static enum lttng_error_code flatten_lttng_events(
1364 struct lttng_dynamic_pointer_array *events,
1365 struct lttng_event **flattened_events)
1366 {
1367 enum lttng_error_code ret_code;
1368 int ret, i;
1369 size_t storage_req;
1370 struct lttng_dynamic_buffer local_flattened_events;
1371 int nb_events;
1372
1373 assert(events);
1374 assert(flattened_events);
1375
1376 lttng_dynamic_buffer_init(&local_flattened_events);
1377 nb_events = lttng_dynamic_pointer_array_get_count(events);
1378
1379 ret_code = compute_flattened_size(events, &storage_req);
1380 if (ret_code != LTTNG_OK) {
1381 goto end;
1382 }
1383
1384 /*
1385 * We must ensure that "local_flattened_events" is never resized so as
1386 * to preserve the validity of the flattened objects.
1387 */
1388 ret = lttng_dynamic_buffer_set_capacity(
1389 &local_flattened_events, storage_req);
1390 if (ret) {
1391 ret_code = LTTNG_ERR_NOMEM;
1392 goto end;
1393 }
1394
1395 /* Start by laying the struct lttng_event */
1396 for (i = 0; i < nb_events; i++) {
1397 const struct event_list_element *element = (const struct event_list_element *)
1398 lttng_dynamic_pointer_array_get_pointer(
1399 events, i);
1400
1401 if (!element) {
1402 ret_code = LTTNG_ERR_FATAL;
1403 goto end;
1404 }
1405
1406 ret = lttng_dynamic_buffer_append(&local_flattened_events,
1407 element->event, sizeof(struct lttng_event));
1408 if (ret) {
1409 ret_code = LTTNG_ERR_NOMEM;
1410 goto end;
1411 }
1412 }
1413
1414 for (i = 0; i < nb_events; i++) {
1415 const struct event_list_element *element = (const struct event_list_element *)
1416 lttng_dynamic_pointer_array_get_pointer(events, i);
1417 struct lttng_event *event = (struct lttng_event *)
1418 (local_flattened_events.data + (sizeof(struct lttng_event) * i));
1419 struct lttng_event_extended *event_extended =
1420 (struct lttng_event_extended *)
1421 (local_flattened_events.data + local_flattened_events.size);
1422 const struct lttng_userspace_probe_location *location = NULL;
1423
1424 assert(element);
1425
1426 /* Insert struct lttng_event_extended. */
1427 ret = lttng_dynamic_buffer_set_size(&local_flattened_events,
1428 local_flattened_events.size +
1429 sizeof(*event_extended));
1430 if (ret) {
1431 ret_code = LTTNG_ERR_NOMEM;
1432 goto end;
1433 }
1434 event->extended.ptr = event_extended;
1435
1436 /* Insert filter expression. */
1437 if (element->filter_expression) {
1438 const size_t len = strlen(element->filter_expression) + 1;
1439
1440 event_extended->filter_expression =
1441 local_flattened_events.data +
1442 local_flattened_events.size;
1443 ret = lttng_dynamic_buffer_append(
1444 &local_flattened_events,
1445 element->filter_expression, len);
1446 if (ret) {
1447 ret_code = LTTNG_ERR_NOMEM;
1448 goto end;
1449 }
1450 }
1451
1452 /* Insert exclusions. */
1453 if (element->exclusions) {
1454 event_extended->exclusions.count =
1455 element->exclusions->count;
1456 event_extended->exclusions.strings =
1457 local_flattened_events.data +
1458 local_flattened_events.size;
1459
1460 ret = lttng_dynamic_buffer_append(
1461 &local_flattened_events,
1462 element->exclusions->names,
1463 element->exclusions->count *
1464 LTTNG_SYMBOL_NAME_LEN);
1465 if (ret) {
1466 ret_code = LTTNG_ERR_NOMEM;
1467 goto end;
1468 }
1469 }
1470
1471 /* Insert padding to align to 64-bits. */
1472 ret = lttng_dynamic_buffer_set_size(&local_flattened_events,
1473 lttng_align_ceil(local_flattened_events.size,
1474 sizeof(uint64_t)));
1475 if (ret) {
1476 ret_code = LTTNG_ERR_NOMEM;
1477 goto end;
1478 }
1479
1480 location = lttng_event_get_userspace_probe_location(
1481 element->event);
1482 if (location) {
1483 event_extended->probe_location = (struct lttng_userspace_probe_location *)
1484 (local_flattened_events.data + local_flattened_events.size);
1485 ret = lttng_userspace_probe_location_flatten(
1486 location, &local_flattened_events);
1487 if (ret < 0) {
1488 ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL;
1489 goto end;
1490 }
1491 }
1492 }
1493
1494 /* Don't reset local_flattened_events buffer as we return its content. */
1495 *flattened_events = (struct lttng_event *) local_flattened_events.data;
1496 lttng_dynamic_buffer_init(&local_flattened_events);
1497 ret_code = LTTNG_OK;
1498 end:
1499 lttng_dynamic_buffer_reset(&local_flattened_events);
1500 return ret_code;
1501 }
1502
1503 static enum lttng_error_code event_list_create_from_payload(
1504 struct lttng_payload_view *view,
1505 unsigned int count,
1506 struct lttng_dynamic_pointer_array *event_list)
1507 {
1508 enum lttng_error_code ret_code;
1509 int ret;
1510 unsigned int i;
1511 int offset = 0;
1512
1513 assert(view);
1514 assert(event_list);
1515
1516 for (i = 0; i < count; i++) {
1517 ssize_t event_size;
1518 struct lttng_payload_view event_view =
1519 lttng_payload_view_from_view(view, offset, -1);
1520 struct event_list_element *element =
1521 (struct event_list_element *) zmalloc(sizeof(*element));
1522
1523 if (!element) {
1524 ret_code = LTTNG_ERR_NOMEM;
1525 goto end;
1526 }
1527
1528 /*
1529 * Lifetime and management of the object is now bound to the
1530 * array.
1531 */
1532 ret = lttng_dynamic_pointer_array_add_pointer(
1533 event_list, element);
1534 if (ret) {
1535 event_list_destructor(element);
1536 ret_code = LTTNG_ERR_NOMEM;
1537 goto end;
1538 }
1539
1540 /*
1541 * Bytecode is not transmitted on listing in any case we do not
1542 * care about it.
1543 */
1544 event_size = lttng_event_create_from_payload(&event_view,
1545 &element->event,
1546 &element->exclusions,
1547 &element->filter_expression, NULL);
1548 if (event_size < 0) {
1549 ret_code = LTTNG_ERR_INVALID;
1550 goto end;
1551 }
1552
1553 offset += event_size;
1554 }
1555
1556 if (view->buffer.size != offset) {
1557 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
1558 goto end;
1559 }
1560
1561 ret_code = LTTNG_OK;
1562
1563 end:
1564 return ret_code;
1565 }
1566
1567 enum lttng_error_code lttng_events_create_and_flatten_from_payload(
1568 struct lttng_payload_view *payload,
1569 unsigned int count,
1570 struct lttng_event **events)
1571 {
1572 enum lttng_error_code ret = LTTNG_OK;
1573 struct lttng_dynamic_pointer_array local_events;
1574
1575 lttng_dynamic_pointer_array_init(&local_events, event_list_destructor);
1576
1577 /* Deserialize the events. */
1578 {
1579 struct lttng_payload_view events_view =
1580 lttng_payload_view_from_view(payload, 0, -1);
1581
1582 ret = event_list_create_from_payload(
1583 &events_view, count, &local_events);
1584 if (ret != LTTNG_OK) {
1585 goto end;
1586 }
1587 }
1588
1589 ret = flatten_lttng_events(&local_events, events);
1590 if (ret != LTTNG_OK) {
1591 goto end;
1592 }
1593
1594 end:
1595 lttng_dynamic_pointer_array_reset(&local_events);
1596 return ret;
1597 }
This page took 0.065763 seconds and 3 git commands to generate.