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