503e7163fe3cd847bd2224cb1915ffd18e03168e
[lttng-tools.git] / src / common / event-rule / user-tracepoint.c
1 /*
2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <assert.h>
9 #include <common/credentials.h>
10 #include <common/error.h>
11 #include <common/hashtable/hashtable.h>
12 #include <common/hashtable/utils.h>
13 #include <common/macros.h>
14 #include <common/mi-lttng.h>
15 #include <common/optional.h>
16 #include <common/payload-view.h>
17 #include <common/payload.h>
18 #include <common/runas.h>
19 #include <common/string-utils/string-utils.h>
20 #include <lttng/event-rule/event-rule-internal.h>
21 #include <lttng/event-rule/user-tracepoint-internal.h>
22 #include <lttng/event.h>
23 #include <lttng/log-level-rule.h>
24
25 #define IS_USER_TRACEPOINT_EVENT_RULE(rule) \
26 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT)
27
28 static void lttng_event_rule_user_tracepoint_destroy(struct lttng_event_rule *rule)
29 {
30 struct lttng_event_rule_user_tracepoint *tracepoint;
31
32 if (rule == NULL) {
33 return;
34 }
35
36 tracepoint = container_of(
37 rule, struct lttng_event_rule_user_tracepoint, parent);
38
39 lttng_log_level_rule_destroy(tracepoint->log_level_rule);
40 lttng_dynamic_pointer_array_reset(&tracepoint->exclusions);
41 free(tracepoint->pattern);
42 free(tracepoint->filter_expression);
43 free(tracepoint->internal_filter.filter);
44 free(tracepoint->internal_filter.bytecode);
45 free(tracepoint);
46 }
47
48 static bool lttng_event_rule_user_tracepoint_validate(
49 const struct lttng_event_rule *rule)
50 {
51 bool valid = false;
52 struct lttng_event_rule_user_tracepoint *tracepoint;
53
54 if (!rule) {
55 goto end;
56 }
57
58 tracepoint = container_of(
59 rule, struct lttng_event_rule_user_tracepoint, parent);
60
61 /* Required field. */
62 if (!tracepoint->pattern) {
63 ERR("Invalid user tracepoint event rule: a pattern must be set.");
64 goto end;
65 }
66
67 valid = true;
68 end:
69 return valid;
70 }
71
72 static int lttng_event_rule_user_tracepoint_serialize(
73 const struct lttng_event_rule *rule,
74 struct lttng_payload *payload)
75 {
76 int ret, i;
77 size_t pattern_len, filter_expression_len, exclusions_len, header_offset;
78 size_t size_before_log_level_rule;
79 struct lttng_event_rule_user_tracepoint *tracepoint;
80 struct lttng_event_rule_user_tracepoint_comm tracepoint_comm;
81 enum lttng_event_rule_status status;
82 unsigned int exclusion_count;
83 size_t exclusions_appended_len = 0;
84 struct lttng_event_rule_user_tracepoint_comm *header;
85
86 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule)) {
87 ret = -1;
88 goto end;
89 }
90
91 header_offset = payload->buffer.size;
92
93 DBG("Serializing user tracepoint event rule.");
94 tracepoint = container_of(
95 rule, struct lttng_event_rule_user_tracepoint, parent);
96
97 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(rule, &exclusion_count);
98 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
99
100 pattern_len = strlen(tracepoint->pattern) + 1;
101
102 if (tracepoint->filter_expression != NULL) {
103 filter_expression_len =
104 strlen(tracepoint->filter_expression) + 1;
105 } else {
106 filter_expression_len = 0;
107 }
108
109 exclusions_len = 0;
110 for (i = 0; i < exclusion_count; i++) {
111 const char *exclusion;
112
113 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
114 rule, i, &exclusion);
115 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
116
117 /* Length field. */
118 exclusions_len += sizeof(uint32_t);
119 /* Payload (null terminated). */
120 exclusions_len += strlen(exclusion) + 1;
121 }
122
123 tracepoint_comm.pattern_len = pattern_len;
124 tracepoint_comm.filter_expression_len = filter_expression_len;
125 tracepoint_comm.exclusions_count = exclusion_count;
126 tracepoint_comm.exclusions_len = exclusions_len;
127
128 ret = lttng_dynamic_buffer_append(&payload->buffer, &tracepoint_comm,
129 sizeof(tracepoint_comm));
130 if (ret) {
131 goto end;
132 }
133
134 ret = lttng_dynamic_buffer_append(
135 &payload->buffer, tracepoint->pattern, pattern_len);
136 if (ret) {
137 goto end;
138 }
139
140 ret = lttng_dynamic_buffer_append(&payload->buffer, tracepoint->filter_expression,
141 filter_expression_len);
142 if (ret) {
143 goto end;
144 }
145
146 size_before_log_level_rule = payload->buffer.size;
147
148 ret = lttng_log_level_rule_serialize(tracepoint->log_level_rule, payload);
149 if (ret < 0) {
150 goto end;
151 }
152
153 header = (typeof(header)) ((char *) payload->buffer.data + header_offset);
154 header->log_level_rule_len =
155 payload->buffer.size - size_before_log_level_rule;
156
157 for (i = 0; i < exclusion_count; i++) {
158 size_t len;
159 const char *exclusion;
160
161 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
162 rule, i, &exclusion);
163 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
164
165 len = strlen(exclusion) + 1;
166 /* Append exclusion length, includes the null terminator. */
167 ret = lttng_dynamic_buffer_append(
168 &payload->buffer, &len, sizeof(uint32_t));
169 if (ret) {
170 goto end;
171 }
172
173 exclusions_appended_len += sizeof(uint32_t);
174
175 /* Include the '\0' in the payload. */
176 ret = lttng_dynamic_buffer_append(
177 &payload->buffer, exclusion, len);
178 if (ret) {
179 goto end;
180 }
181
182 exclusions_appended_len += len;
183 }
184
185 assert(exclusions_len == exclusions_appended_len);
186
187 end:
188 return ret;
189 }
190
191 static bool lttng_event_rule_user_tracepoint_is_equal(
192 const struct lttng_event_rule *_a,
193 const struct lttng_event_rule *_b)
194 {
195 int i;
196 bool is_equal = false;
197 struct lttng_event_rule_user_tracepoint *a, *b;
198 unsigned int count_a, count_b;
199 enum lttng_event_rule_status status;
200
201 a = container_of(_a, struct lttng_event_rule_user_tracepoint, parent);
202 b = container_of(_b, struct lttng_event_rule_user_tracepoint, parent);
203
204 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(_a, &count_a);
205 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
206 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(_b, &count_b);
207 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
208
209 /* Quick checks. */
210 if (count_a != count_b) {
211 goto end;
212 }
213
214 if (!!a->filter_expression != !!b->filter_expression) {
215 goto end;
216 }
217
218 /* Long check. */
219 assert(a->pattern);
220 assert(b->pattern);
221 if (strcmp(a->pattern, b->pattern)) {
222 goto end;
223 }
224
225 if (a->filter_expression && b->filter_expression) {
226 if (strcmp(a->filter_expression, b->filter_expression)) {
227 goto end;
228 }
229 } else if (!!a->filter_expression != !!b->filter_expression) {
230 /* One is set; not the other. */
231 goto end;
232 }
233
234 if (!lttng_log_level_rule_is_equal(
235 a->log_level_rule, b->log_level_rule)) {
236 goto end;
237 }
238
239 for (i = 0; i < count_a; i++) {
240 const char *exclusion_a, *exclusion_b;
241
242 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
243 _a, i, &exclusion_a);
244 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
245 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
246 _b, i, &exclusion_b);
247 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
248 if (strcmp(exclusion_a, exclusion_b)) {
249 goto end;
250 }
251 }
252
253 is_equal = true;
254 end:
255 return is_equal;
256 }
257
258 static enum lttng_error_code
259 lttng_event_rule_user_tracepoint_generate_filter_bytecode(
260 struct lttng_event_rule *rule,
261 const struct lttng_credentials *creds)
262 {
263 int ret;
264 enum lttng_error_code ret_code;
265 struct lttng_event_rule_user_tracepoint *tracepoint;
266 enum lttng_event_rule_status status;
267 const char *filter;
268 struct lttng_bytecode *bytecode = NULL;
269
270 assert(rule);
271
272 tracepoint = container_of(
273 rule, struct lttng_event_rule_user_tracepoint, parent);
274
275 status = lttng_event_rule_user_tracepoint_get_filter(rule, &filter);
276 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
277 filter = NULL;
278 } else if (status != LTTNG_EVENT_RULE_STATUS_OK) {
279 ret_code = LTTNG_ERR_FILTER_INVAL;
280 goto end;
281 }
282
283 if (filter && filter[0] == '\0') {
284 ret_code = LTTNG_ERR_FILTER_INVAL;
285 goto error;
286 }
287
288 if (filter) {
289 tracepoint->internal_filter.filter = strdup(filter);
290 if (tracepoint->internal_filter.filter == NULL) {
291 ret_code = LTTNG_ERR_NOMEM;
292 goto error;
293 }
294 } else {
295 tracepoint->internal_filter.filter = NULL;
296 }
297
298 if (tracepoint->internal_filter.filter == NULL) {
299 ret_code = LTTNG_OK;
300 goto end;
301 }
302
303 ret = run_as_generate_filter_bytecode(
304 tracepoint->internal_filter.filter, creds,
305 &bytecode);
306 if (ret) {
307 ret_code = LTTNG_ERR_FILTER_INVAL;
308 goto end;
309 }
310
311 tracepoint->internal_filter.bytecode = bytecode;
312 bytecode = NULL;
313 ret_code = LTTNG_OK;
314
315 error:
316 end:
317 free(bytecode);
318 return ret_code;
319 }
320
321 static const char *lttng_event_rule_user_tracepoint_get_internal_filter(
322 const struct lttng_event_rule *rule)
323 {
324 struct lttng_event_rule_user_tracepoint *tracepoint;
325
326 assert(rule);
327 tracepoint = container_of(
328 rule, struct lttng_event_rule_user_tracepoint, parent);
329 return tracepoint->internal_filter.filter;
330 }
331
332 static const struct lttng_bytecode *
333 lttng_event_rule_user_tracepoint_get_internal_filter_bytecode(
334 const struct lttng_event_rule *rule)
335 {
336 struct lttng_event_rule_user_tracepoint *tracepoint;
337
338 assert(rule);
339 tracepoint = container_of(
340 rule, struct lttng_event_rule_user_tracepoint, parent);
341 return tracepoint->internal_filter.bytecode;
342 }
343
344 static enum lttng_event_rule_generate_exclusions_status
345 lttng_event_rule_user_tracepoint_generate_exclusions(
346 const struct lttng_event_rule *rule,
347 struct lttng_event_exclusion **_exclusions)
348 {
349 unsigned int nb_exclusions = 0, i;
350 struct lttng_event_exclusion *exclusions;
351 enum lttng_event_rule_status event_rule_status;
352 enum lttng_event_rule_generate_exclusions_status ret_status;
353
354 assert(_exclusions);
355
356 event_rule_status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(
357 rule, &nb_exclusions);
358 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
359 if (nb_exclusions == 0) {
360 /* Nothing to do. */
361 exclusions = NULL;
362 ret_status = LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE;
363 goto end;
364 }
365
366 exclusions = zmalloc(sizeof(struct lttng_event_exclusion) +
367 (LTTNG_SYMBOL_NAME_LEN * nb_exclusions));
368 if (!exclusions) {
369 PERROR("Failed to allocate exclusions buffer");
370 ret_status = LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OUT_OF_MEMORY;
371 goto end;
372 }
373
374 exclusions->count = nb_exclusions;
375 for (i = 0; i < nb_exclusions; i++) {
376 int copy_ret;
377 const char *exclusion_str;
378
379 event_rule_status =
380 lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
381 rule, i, &exclusion_str);
382 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
383
384 copy_ret = lttng_strncpy(exclusions->names[i], exclusion_str,
385 LTTNG_SYMBOL_NAME_LEN);
386 if (copy_ret) {
387 free(exclusions);
388 exclusions = NULL;
389 ret_status = LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_ERROR;
390 goto end;
391 }
392 }
393
394 ret_status = LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK;
395
396 end:
397 *_exclusions = exclusions;
398 return ret_status;
399 }
400
401 static void destroy_lttng_exclusions_element(void *ptr)
402 {
403 free(ptr);
404 }
405
406 static unsigned long lttng_event_rule_user_tracepoint_hash(
407 const struct lttng_event_rule *rule)
408 {
409 unsigned long hash;
410 unsigned int i, exclusion_count;
411 enum lttng_event_rule_status status;
412 struct lttng_event_rule_user_tracepoint *tp_rule =
413 container_of(rule, typeof(*tp_rule), parent);
414
415 hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT,
416 lttng_ht_seed);
417 hash ^= hash_key_str(tp_rule->pattern, lttng_ht_seed);
418
419 if (tp_rule->filter_expression) {
420 hash ^= hash_key_str(tp_rule->filter_expression, lttng_ht_seed);
421 }
422
423 if (tp_rule->log_level_rule) {
424 hash ^= lttng_log_level_rule_hash(tp_rule->log_level_rule);
425 }
426
427 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(rule,
428 &exclusion_count);
429 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
430
431 for (i = 0; i < exclusion_count; i++) {
432 const char *exclusion;
433
434 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
435 rule, i, &exclusion);
436 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
437 hash ^= hash_key_str(exclusion, lttng_ht_seed);
438 }
439
440 return hash;
441 }
442
443 static enum lttng_error_code lttng_event_rule_user_tracepoint_mi_serialize(
444 const struct lttng_event_rule *rule, struct mi_writer *writer)
445 {
446 int ret;
447 enum lttng_error_code ret_code;
448 enum lttng_event_rule_status status;
449 const char *filter = NULL;
450 const char *name_pattern = NULL;
451 const struct lttng_log_level_rule *log_level_rule = NULL;
452 unsigned int exclusion_count = 0;
453
454 assert(rule);
455 assert(writer);
456 assert(IS_USER_TRACEPOINT_EVENT_RULE(rule));
457
458 status = lttng_event_rule_user_tracepoint_get_name_pattern(
459 rule, &name_pattern);
460 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
461 assert(name_pattern);
462
463 status = lttng_event_rule_user_tracepoint_get_filter(rule, &filter);
464 assert(status == LTTNG_EVENT_RULE_STATUS_OK ||
465 status == LTTNG_EVENT_RULE_STATUS_UNSET);
466
467 status = lttng_event_rule_user_tracepoint_get_log_level_rule(
468 rule, &log_level_rule);
469 assert(status == LTTNG_EVENT_RULE_STATUS_OK ||
470 status == LTTNG_EVENT_RULE_STATUS_UNSET);
471
472 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(
473 rule, &exclusion_count);
474 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
475
476 /* Open event rule user tracepoint element. */
477 ret = mi_lttng_writer_open_element(
478 writer, mi_lttng_element_event_rule_user_tracepoint);
479 if (ret) {
480 goto mi_error;
481 }
482
483 /* Name pattern. */
484 ret = mi_lttng_writer_write_element_string(writer,
485 mi_lttng_element_event_rule_name_pattern, name_pattern);
486 if (ret) {
487 goto mi_error;
488 }
489
490 /* Filter expression. */
491 if (filter != NULL) {
492 ret = mi_lttng_writer_write_element_string(writer,
493 mi_lttng_element_event_rule_filter_expression,
494 filter);
495 if (ret) {
496 goto mi_error;
497 }
498 }
499
500 /* Log level rule. */
501 if (log_level_rule) {
502 ret_code = lttng_log_level_rule_mi_serialize(
503 log_level_rule, writer);
504 if (ret_code != LTTNG_OK) {
505 goto end;
506 }
507 }
508
509 if (exclusion_count != 0) {
510 int i;
511
512 /* Open the exclusion list. */
513 ret = mi_lttng_writer_open_element(writer,
514 mi_lttng_element_event_rule_user_tracepoint_name_pattern_exclusions);
515 if (ret) {
516 goto mi_error;
517 }
518
519 for (i = 0; i < exclusion_count; i++) {
520 const char *exclusion;
521
522 status = lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
523 rule, i, &exclusion);
524 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
525
526 ret = mi_lttng_writer_write_element_string(writer,
527 mi_lttng_element_event_rule_user_tracepoint_name_pattern_exclusion,
528 exclusion);
529 if (ret) {
530 goto mi_error;
531 }
532 }
533
534 /* Close the list. */
535 ret = mi_lttng_writer_close_element(writer);
536 if (ret) {
537 goto mi_error;
538 }
539 }
540
541 /* Close event rule user tracepoint element. */
542 ret = mi_lttng_writer_close_element(writer);
543 if (ret) {
544 goto mi_error;
545 }
546
547 ret_code = LTTNG_OK;
548 goto end;
549
550 mi_error:
551 ret_code = LTTNG_ERR_MI_IO_FAIL;
552 end:
553 return ret_code;
554 }
555
556 struct lttng_event_rule *lttng_event_rule_user_tracepoint_create(void)
557 {
558 struct lttng_event_rule *rule = NULL;
559 struct lttng_event_rule_user_tracepoint *tp_rule;
560 enum lttng_event_rule_status status;
561
562 tp_rule = zmalloc(sizeof(struct lttng_event_rule_user_tracepoint));
563 if (!tp_rule) {
564 goto end;
565 }
566
567 rule = &tp_rule->parent;
568 lttng_event_rule_init(&tp_rule->parent, LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT);
569 tp_rule->parent.validate = lttng_event_rule_user_tracepoint_validate;
570 tp_rule->parent.serialize = lttng_event_rule_user_tracepoint_serialize;
571 tp_rule->parent.equal = lttng_event_rule_user_tracepoint_is_equal;
572 tp_rule->parent.destroy = lttng_event_rule_user_tracepoint_destroy;
573 tp_rule->parent.generate_filter_bytecode =
574 lttng_event_rule_user_tracepoint_generate_filter_bytecode;
575 tp_rule->parent.get_filter =
576 lttng_event_rule_user_tracepoint_get_internal_filter;
577 tp_rule->parent.get_filter_bytecode =
578 lttng_event_rule_user_tracepoint_get_internal_filter_bytecode;
579 tp_rule->parent.generate_exclusions =
580 lttng_event_rule_user_tracepoint_generate_exclusions;
581 tp_rule->parent.hash = lttng_event_rule_user_tracepoint_hash;
582 tp_rule->parent.mi_serialize = lttng_event_rule_user_tracepoint_mi_serialize;
583
584 /* Not necessary for now. */
585 tp_rule->parent.generate_lttng_event = NULL;
586
587 tp_rule->log_level_rule = NULL;
588
589 lttng_dynamic_pointer_array_init(&tp_rule->exclusions,
590 destroy_lttng_exclusions_element);
591
592 /* Default pattern is '*'. */
593 status = lttng_event_rule_user_tracepoint_set_name_pattern(rule, "*");
594 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
595 lttng_event_rule_destroy(rule);
596 rule = NULL;
597 }
598
599 end:
600 return rule;
601 }
602
603 LTTNG_HIDDEN
604 ssize_t lttng_event_rule_user_tracepoint_create_from_payload(
605 struct lttng_payload_view *view,
606 struct lttng_event_rule **_event_rule)
607 {
608 ssize_t ret, offset = 0;
609 int i;
610 enum lttng_event_rule_status status;
611 const struct lttng_event_rule_user_tracepoint_comm *tracepoint_comm;
612 const char *pattern;
613 const char *filter_expression = NULL;
614 const char **exclusions = NULL;
615 const uint32_t *exclusion_len;
616 const char *exclusion;
617 struct lttng_buffer_view current_buffer_view;
618 struct lttng_event_rule *rule = NULL;
619 struct lttng_log_level_rule *log_level_rule = NULL;
620
621 if (!_event_rule) {
622 ret = -1;
623 goto end;
624 }
625
626 current_buffer_view = lttng_buffer_view_from_view(
627 &view->buffer, offset, sizeof(*tracepoint_comm));
628 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
629 ERR("Failed to initialize from malformed event rule tracepoint: buffer too short to contain header.");
630 ret = -1;
631 goto end;
632 }
633
634 tracepoint_comm = (typeof(tracepoint_comm)) current_buffer_view.data;
635
636 rule = lttng_event_rule_user_tracepoint_create();
637 if (!rule) {
638 ERR("Failed to create event rule user tracepoint.");
639 ret = -1;
640 goto end;
641 }
642
643 /* Skip to payload. */
644 offset += current_buffer_view.size;
645
646 /* Map the pattern. */
647 current_buffer_view = lttng_buffer_view_from_view(
648 &view->buffer, offset, tracepoint_comm->pattern_len);
649
650 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
651 ret = -1;
652 goto end;
653 }
654
655 pattern = current_buffer_view.data;
656 if (!lttng_buffer_view_contains_string(&current_buffer_view, pattern,
657 tracepoint_comm->pattern_len)) {
658 ret = -1;
659 goto end;
660 }
661
662 /* Skip after the pattern. */
663 offset += tracepoint_comm->pattern_len;
664
665 if (!tracepoint_comm->filter_expression_len) {
666 goto skip_filter_expression;
667 }
668
669 /* Map the filter_expression. */
670 current_buffer_view = lttng_buffer_view_from_view(&view->buffer, offset,
671 tracepoint_comm->filter_expression_len);
672 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
673 ret = -1;
674 goto end;
675 }
676
677 filter_expression = current_buffer_view.data;
678 if (!lttng_buffer_view_contains_string(&current_buffer_view,
679 filter_expression,
680 tracepoint_comm->filter_expression_len)) {
681 ret = -1;
682 goto end;
683 }
684
685 /* Skip after the pattern. */
686 offset += tracepoint_comm->filter_expression_len;
687
688 skip_filter_expression:
689 if (!tracepoint_comm->log_level_rule_len) {
690 goto skip_log_level_rule;
691 }
692
693 {
694 /* Map the log level rule. */
695 struct lttng_payload_view current_payload_view =
696 lttng_payload_view_from_view(view, offset,
697 tracepoint_comm->log_level_rule_len);
698
699 ret = lttng_log_level_rule_create_from_payload(
700 &current_payload_view, &log_level_rule);
701 if (ret < 0) {
702 ret = -1;
703 goto end;
704 }
705
706 assert(ret == tracepoint_comm->log_level_rule_len);
707 }
708
709 /* Skip after the log level rule. */
710 offset += tracepoint_comm->log_level_rule_len;
711
712 skip_log_level_rule:
713 for (i = 0; i < tracepoint_comm->exclusions_count; i++) {
714 current_buffer_view = lttng_buffer_view_from_view(
715 &view->buffer, offset, sizeof(*exclusion_len));
716 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
717 ret = -1;
718 goto end;
719 }
720
721 exclusion_len = (typeof(exclusion_len)) current_buffer_view.data;
722 offset += sizeof(*exclusion_len);
723
724 current_buffer_view = lttng_buffer_view_from_view(
725 &view->buffer, offset, *exclusion_len);
726 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
727 ret = -1;
728 goto end;
729 }
730
731 exclusion = current_buffer_view.data;
732 if (!lttng_buffer_view_contains_string(&current_buffer_view,
733 exclusion, *exclusion_len)) {
734 ret = -1;
735 goto end;
736 }
737
738 status = lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(rule, exclusion);
739 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
740 ERR("Failed to add event rule user tracepoint exclusion \"%s\".",
741 exclusion);
742 ret = -1;
743 goto end;
744 }
745
746 /* Skip to next exclusion. */
747 offset += *exclusion_len;
748 }
749
750 status = lttng_event_rule_user_tracepoint_set_name_pattern(rule, pattern);
751 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
752 ERR("Failed to set event rule user tracepoint pattern.");
753 ret = -1;
754 goto end;
755 }
756
757 if (filter_expression) {
758 status = lttng_event_rule_user_tracepoint_set_filter(
759 rule, filter_expression);
760 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
761 ERR("Failed to set event rule user tracepoint pattern.");
762 ret = -1;
763 goto end;
764 }
765 }
766
767 if (log_level_rule) {
768 status = lttng_event_rule_user_tracepoint_set_log_level_rule(
769 rule, log_level_rule);
770 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
771 ERR("Failed to set event rule user tracepoint log level rule.");
772 ret = -1;
773 goto end;
774 }
775 }
776
777 *_event_rule = rule;
778 rule = NULL;
779 ret = offset;
780 end:
781 free(exclusions);
782 lttng_log_level_rule_destroy(log_level_rule);
783 lttng_event_rule_destroy(rule);
784 return ret;
785 }
786
787 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_set_name_pattern(
788 struct lttng_event_rule *rule, const char *pattern)
789 {
790 char *pattern_copy = NULL;
791 struct lttng_event_rule_user_tracepoint *tracepoint;
792 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
793
794 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !pattern ||
795 strlen(pattern) == 0) {
796 status = LTTNG_EVENT_RULE_STATUS_INVALID;
797 goto end;
798 }
799
800 tracepoint = container_of(
801 rule, struct lttng_event_rule_user_tracepoint, parent);
802 pattern_copy = strdup(pattern);
803 if (!pattern_copy) {
804 status = LTTNG_EVENT_RULE_STATUS_ERROR;
805 goto end;
806 }
807
808 /* Normalize the pattern. */
809 strutils_normalize_star_glob_pattern(pattern_copy);
810
811 free(tracepoint->pattern);
812
813 tracepoint->pattern = pattern_copy;
814 pattern_copy = NULL;
815 end:
816 return status;
817 }
818
819 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_name_pattern(
820 const struct lttng_event_rule *rule, const char **pattern)
821 {
822 struct lttng_event_rule_user_tracepoint *tracepoint;
823 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
824
825 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !pattern) {
826 status = LTTNG_EVENT_RULE_STATUS_INVALID;
827 goto end;
828 }
829
830 tracepoint = container_of(
831 rule, struct lttng_event_rule_user_tracepoint, parent);
832 if (!tracepoint->pattern) {
833 status = LTTNG_EVENT_RULE_STATUS_UNSET;
834 goto end;
835 }
836
837 *pattern = tracepoint->pattern;
838 end:
839 return status;
840 }
841
842 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_set_filter(
843 struct lttng_event_rule *rule, const char *expression)
844 {
845 char *expression_copy = NULL;
846 struct lttng_event_rule_user_tracepoint *tracepoint;
847 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
848
849 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !expression ||
850 strlen(expression) == 0) {
851 status = LTTNG_EVENT_RULE_STATUS_INVALID;
852 goto end;
853 }
854
855 tracepoint = container_of(
856 rule, struct lttng_event_rule_user_tracepoint, parent);
857 expression_copy = strdup(expression);
858 if (!expression_copy) {
859 PERROR("Failed to copy filter expression");
860 status = LTTNG_EVENT_RULE_STATUS_ERROR;
861 goto end;
862 }
863
864 if (tracepoint->filter_expression) {
865 free(tracepoint->filter_expression);
866 }
867
868 tracepoint->filter_expression = expression_copy;
869 expression_copy = NULL;
870 end:
871 return status;
872 }
873
874 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_filter(
875 const struct lttng_event_rule *rule, const char **expression)
876 {
877 struct lttng_event_rule_user_tracepoint *tracepoint;
878 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
879
880 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !expression) {
881 status = LTTNG_EVENT_RULE_STATUS_INVALID;
882 goto end;
883 }
884
885 tracepoint = container_of(
886 rule, struct lttng_event_rule_user_tracepoint, parent);
887 if (!tracepoint->filter_expression) {
888 status = LTTNG_EVENT_RULE_STATUS_UNSET;
889 goto end;
890 }
891
892 *expression = tracepoint->filter_expression;
893 end:
894 return status;
895 }
896
897 static bool log_level_rule_valid(const struct lttng_log_level_rule *rule)
898 {
899 bool valid = false;
900 enum lttng_log_level_rule_status status;
901 int level;
902
903 switch (lttng_log_level_rule_get_type(rule)) {
904 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
905 status = lttng_log_level_rule_exactly_get_level(rule, &level);
906 break;
907 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
908 status = lttng_log_level_rule_at_least_as_severe_as_get_level(
909 rule, &level);
910 break;
911 default:
912 abort();
913 }
914
915 assert(status == LTTNG_LOG_LEVEL_RULE_STATUS_OK);
916
917 if (level < LTTNG_LOGLEVEL_EMERG) {
918 /* Invalid. */
919 goto end;
920 }
921 if (level > LTTNG_LOGLEVEL_DEBUG) {
922 /* Invalid. */
923 goto end;
924 }
925
926 valid = true;
927
928 end:
929 return valid;
930 }
931
932 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_set_log_level_rule(
933 struct lttng_event_rule *rule,
934 const struct lttng_log_level_rule *log_level_rule)
935 {
936 struct lttng_event_rule_user_tracepoint *tracepoint;
937 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
938 struct lttng_log_level_rule *copy = NULL;
939
940 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule)) {
941 status = LTTNG_EVENT_RULE_STATUS_INVALID;
942 goto end;
943 }
944
945 tracepoint = container_of(
946 rule, struct lttng_event_rule_user_tracepoint, parent);
947
948 if (!log_level_rule_valid(log_level_rule)) {
949 status = LTTNG_EVENT_RULE_STATUS_INVALID;
950 goto end;
951 }
952
953 copy = lttng_log_level_rule_copy(log_level_rule);
954 if (copy == NULL) {
955 status = LTTNG_EVENT_RULE_STATUS_ERROR;
956 goto end;
957 }
958
959 if (tracepoint->log_level_rule) {
960 lttng_log_level_rule_destroy(tracepoint->log_level_rule);
961 }
962
963 tracepoint->log_level_rule = copy;
964
965 end:
966 return status;
967 }
968
969 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_log_level_rule(
970 const struct lttng_event_rule *rule,
971 const struct lttng_log_level_rule **log_level_rule
972 )
973 {
974 struct lttng_event_rule_user_tracepoint *tracepoint;
975 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
976
977 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !log_level_rule) {
978 status = LTTNG_EVENT_RULE_STATUS_INVALID;
979 goto end;
980 }
981
982 tracepoint = container_of(
983 rule, struct lttng_event_rule_user_tracepoint, parent);
984 if (tracepoint->log_level_rule == NULL) {
985 status = LTTNG_EVENT_RULE_STATUS_UNSET;
986 goto end;
987 }
988
989 *log_level_rule = tracepoint->log_level_rule;
990 end:
991 return status;
992 }
993
994 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
995 struct lttng_event_rule *rule,
996 const char *exclusion)
997 {
998 int ret;
999 char *exclusion_copy = NULL;
1000 struct lttng_event_rule_user_tracepoint *tracepoint;
1001 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
1002
1003 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) ||
1004 !exclusion) {
1005 status = LTTNG_EVENT_RULE_STATUS_INVALID;
1006 goto end;
1007 }
1008
1009 tracepoint = container_of(
1010 rule, struct lttng_event_rule_user_tracepoint, parent);
1011
1012 if (strlen(exclusion) >= LTTNG_SYMBOL_NAME_LEN) {
1013 status = LTTNG_EVENT_RULE_STATUS_INVALID;
1014 goto end;
1015 }
1016
1017 exclusion_copy = strdup(exclusion);
1018 if (!exclusion_copy) {
1019 status = LTTNG_EVENT_RULE_STATUS_ERROR;
1020 goto end;
1021 }
1022
1023 ret = lttng_dynamic_pointer_array_add_pointer(&tracepoint->exclusions,
1024 exclusion_copy);
1025 if (ret < 0) {
1026 status = LTTNG_EVENT_RULE_STATUS_ERROR;
1027 goto end;
1028 }
1029
1030 exclusion_copy = NULL;
1031 end:
1032 free(exclusion_copy);
1033 return status;
1034 }
1035
1036 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(
1037 const struct lttng_event_rule *rule, unsigned int *count)
1038 {
1039 struct lttng_event_rule_user_tracepoint *tracepoint;
1040 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
1041
1042 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !count) {
1043 status = LTTNG_EVENT_RULE_STATUS_INVALID;
1044 goto end;
1045 }
1046
1047 tracepoint = container_of(
1048 rule, struct lttng_event_rule_user_tracepoint, parent);
1049 *count = lttng_dynamic_pointer_array_get_count(&tracepoint->exclusions);
1050 end:
1051 return status;
1052 }
1053
1054 enum lttng_event_rule_status lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_at_index(
1055 const struct lttng_event_rule *rule,
1056 unsigned int index,
1057 const char **exclusion)
1058 {
1059 unsigned int count;
1060 struct lttng_event_rule_user_tracepoint *tracepoint;
1061 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
1062
1063 if (!rule || !IS_USER_TRACEPOINT_EVENT_RULE(rule) || !exclusion) {
1064 status = LTTNG_EVENT_RULE_STATUS_INVALID;
1065 goto end;
1066 }
1067
1068 tracepoint = container_of(
1069 rule, struct lttng_event_rule_user_tracepoint, parent);
1070 if (lttng_event_rule_user_tracepoint_get_name_pattern_exclusion_count(rule, &count) !=
1071 LTTNG_EVENT_RULE_STATUS_OK) {
1072 goto end;
1073 }
1074
1075 if (index >= count) {
1076 goto end;
1077 }
1078
1079 *exclusion = lttng_dynamic_pointer_array_get_pointer(
1080 &tracepoint->exclusions, index);
1081 end:
1082 return status;
1083 }
This page took 0.054293 seconds and 4 git commands to generate.