2 * Copyright (C) 2019 Jonathan Rajotte
3 * <jonathan.rajotte-julien@efficios.com>
5 * SPDX-License-Identifier: LGPL-2.1-only
10 #include <common/error.h>
11 #include <common/macros.h>
12 #include <common/payload.h>
13 #include <common/payload-view.h>
14 #include <lttng/event-rule/event-rule-internal.h>
17 enum lttng_event_rule_type
lttng_event_rule_get_type(
18 const struct lttng_event_rule
*event_rule
)
20 return event_rule
? event_rule
->type
: LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
24 enum lttng_domain_type
lttng_event_rule_get_domain_type(
25 const struct lttng_event_rule
*event_rule
)
27 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
29 switch (lttng_event_rule_get_type(event_rule
)) {
30 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
32 domain_type
= LTTNG_DOMAIN_NONE
;
34 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
35 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
36 case LTTNG_EVENT_RULE_TYPE_KRETPROBE
:
37 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
38 domain_type
= LTTNG_DOMAIN_KERNEL
;
40 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
41 domain_type
= LTTNG_DOMAIN_NONE
;
48 static void lttng_event_rule_release(struct urcu_ref
*ref
)
50 struct lttng_event_rule
*event_rule
=
51 container_of(ref
, typeof(*event_rule
), ref
);
53 assert(event_rule
->destroy
);
54 event_rule
->destroy(event_rule
);
57 void lttng_event_rule_destroy(struct lttng_event_rule
*event_rule
)
59 lttng_event_rule_put(event_rule
);
63 bool lttng_event_rule_validate(const struct lttng_event_rule
*event_rule
)
72 if (!event_rule
->validate
) {
73 /* Sub-class guarantees that it can never be invalid. */
78 valid
= event_rule
->validate(event_rule
);
84 int lttng_event_rule_serialize(const struct lttng_event_rule
*event_rule
,
85 struct lttng_payload
*payload
)
88 struct lttng_event_rule_comm event_rule_comm
= {};
95 event_rule_comm
.event_rule_type
= (int8_t) event_rule
->type
;
97 ret
= lttng_dynamic_buffer_append(
98 &payload
->buffer
, &event_rule_comm
, sizeof(event_rule_comm
));
103 ret
= event_rule
->serialize(event_rule
, payload
);
112 bool lttng_event_rule_is_equal(const struct lttng_event_rule
*a
,
113 const struct lttng_event_rule
*b
)
115 bool is_equal
= false;
121 if (a
->type
!= b
->type
) {
130 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
136 ssize_t
lttng_event_rule_create_from_payload(
137 struct lttng_payload_view
*view
,
138 struct lttng_event_rule
**event_rule
)
140 ssize_t ret
, consumed
= 0;
141 const struct lttng_event_rule_comm
*event_rule_comm
;
142 event_rule_create_from_payload_cb create_from_payload
= NULL
;
144 if (!view
|| !event_rule
) {
149 DBG("Deserializing event_rule from payload");
150 event_rule_comm
= (const struct lttng_event_rule_comm
*) view
->buffer
.data
;
151 consumed
+= sizeof(*event_rule_comm
);
153 switch ((enum lttng_event_rule_type
) event_rule_comm
->event_rule_type
) {
154 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
157 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
160 case LTTNG_EVENT_RULE_TYPE_KRETPROBE
:
163 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
166 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
170 ERR("Attempted to create event rule of unknown type (%i)",
171 (int) event_rule_comm
->event_rule_type
);
176 assert(create_from_payload
);
179 struct lttng_payload_view child_view
=
180 lttng_payload_view_from_view(
183 ret
= create_from_payload(&child_view
, event_rule
);
191 if (!lttng_event_rule_validate(*event_rule
)) {
202 void lttng_event_rule_init(struct lttng_event_rule
*event_rule
,
203 enum lttng_event_rule_type type
)
205 urcu_ref_init(&event_rule
->ref
);
206 event_rule
->type
= type
;
210 bool lttng_event_rule_get(struct lttng_event_rule
*event_rule
)
212 return urcu_ref_get_unless_zero(&event_rule
->ref
);
216 void lttng_event_rule_put(struct lttng_event_rule
*event_rule
)
222 assert(event_rule
->ref
.refcount
);
223 urcu_ref_put(&event_rule
->ref
, lttng_event_rule_release
);
227 enum lttng_error_code
lttng_event_rule_generate_filter_bytecode(
228 struct lttng_event_rule
*rule
, uid_t uid
, gid_t gid
)
230 assert(rule
->generate_filter_bytecode
);
231 return rule
->generate_filter_bytecode(rule
, uid
, gid
);
235 const char *lttng_event_rule_get_filter(const struct lttng_event_rule
*rule
)
237 assert(rule
->get_filter
);
238 return rule
->get_filter(rule
);
242 const struct lttng_filter_bytecode
*lttng_event_rule_get_filter_bytecode(
243 const struct lttng_event_rule
*rule
)
245 assert(rule
->get_filter_bytecode
);
246 return rule
->get_filter_bytecode(rule
);
250 struct lttng_event_exclusion
*lttng_event_rule_generate_exclusions(
251 const struct lttng_event_rule
*rule
)
253 assert(rule
->generate_exclusions
);
254 return rule
->generate_exclusions(rule
);
258 const char *lttng_event_rule_type_str(enum lttng_event_rule_type type
)
261 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
263 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
265 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
267 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
269 case LTTNG_EVENT_RULE_TYPE_KRETPROBE
:
271 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
272 return "userspace-probe";