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>
15 #include <lttng/event-rule/kprobe-internal.h>
18 enum lttng_event_rule_type
lttng_event_rule_get_type(
19 const struct lttng_event_rule
*event_rule
)
21 return event_rule
? event_rule
->type
: LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
25 enum lttng_domain_type
lttng_event_rule_get_domain_type(
26 const struct lttng_event_rule
*event_rule
)
28 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
30 switch (lttng_event_rule_get_type(event_rule
)) {
31 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
33 domain_type
= LTTNG_DOMAIN_NONE
;
35 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
36 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
37 case LTTNG_EVENT_RULE_TYPE_KRETPROBE
:
38 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
39 domain_type
= LTTNG_DOMAIN_KERNEL
;
41 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
42 domain_type
= LTTNG_DOMAIN_NONE
;
49 static void lttng_event_rule_release(struct urcu_ref
*ref
)
51 struct lttng_event_rule
*event_rule
=
52 container_of(ref
, typeof(*event_rule
), ref
);
54 assert(event_rule
->destroy
);
55 event_rule
->destroy(event_rule
);
58 void lttng_event_rule_destroy(struct lttng_event_rule
*event_rule
)
60 lttng_event_rule_put(event_rule
);
64 bool lttng_event_rule_validate(const struct lttng_event_rule
*event_rule
)
73 if (!event_rule
->validate
) {
74 /* Sub-class guarantees that it can never be invalid. */
79 valid
= event_rule
->validate(event_rule
);
85 int lttng_event_rule_serialize(const struct lttng_event_rule
*event_rule
,
86 struct lttng_payload
*payload
)
89 struct lttng_event_rule_comm event_rule_comm
= {};
96 event_rule_comm
.event_rule_type
= (int8_t) event_rule
->type
;
98 ret
= lttng_dynamic_buffer_append(
99 &payload
->buffer
, &event_rule_comm
, sizeof(event_rule_comm
));
104 ret
= event_rule
->serialize(event_rule
, payload
);
113 bool lttng_event_rule_is_equal(const struct lttng_event_rule
*a
,
114 const struct lttng_event_rule
*b
)
116 bool is_equal
= false;
122 if (a
->type
!= b
->type
) {
131 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
137 ssize_t
lttng_event_rule_create_from_payload(
138 struct lttng_payload_view
*view
,
139 struct lttng_event_rule
**event_rule
)
141 ssize_t ret
, consumed
= 0;
142 const struct lttng_event_rule_comm
*event_rule_comm
;
143 event_rule_create_from_payload_cb create_from_payload
= NULL
;
145 if (!view
|| !event_rule
) {
150 DBG("Deserializing event_rule from payload.");
151 event_rule_comm
= (const struct lttng_event_rule_comm
*) view
->buffer
.data
;
152 consumed
+= sizeof(*event_rule_comm
);
154 switch ((enum lttng_event_rule_type
) event_rule_comm
->event_rule_type
) {
155 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
158 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
159 create_from_payload
= lttng_event_rule_kprobe_create_from_payload
;
161 case LTTNG_EVENT_RULE_TYPE_KRETPROBE
:
164 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
167 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
171 ERR("Attempted to create event rule of unknown type (%i)",
172 (int) event_rule_comm
->event_rule_type
);
177 assert(create_from_payload
);
180 struct lttng_payload_view child_view
=
181 lttng_payload_view_from_view(
184 ret
= create_from_payload(&child_view
, event_rule
);
192 if (!lttng_event_rule_validate(*event_rule
)) {
203 void lttng_event_rule_init(struct lttng_event_rule
*event_rule
,
204 enum lttng_event_rule_type type
)
206 urcu_ref_init(&event_rule
->ref
);
207 event_rule
->type
= type
;
211 bool lttng_event_rule_get(struct lttng_event_rule
*event_rule
)
213 return urcu_ref_get_unless_zero(&event_rule
->ref
);
217 void lttng_event_rule_put(struct lttng_event_rule
*event_rule
)
223 assert(event_rule
->ref
.refcount
);
224 urcu_ref_put(&event_rule
->ref
, lttng_event_rule_release
);
228 enum lttng_error_code
lttng_event_rule_generate_filter_bytecode(
229 struct lttng_event_rule
*rule
, uid_t uid
, gid_t gid
)
231 assert(rule
->generate_filter_bytecode
);
232 return rule
->generate_filter_bytecode(rule
, uid
, gid
);
236 const char *lttng_event_rule_get_filter(const struct lttng_event_rule
*rule
)
238 assert(rule
->get_filter
);
239 return rule
->get_filter(rule
);
243 const struct lttng_filter_bytecode
*lttng_event_rule_get_filter_bytecode(
244 const struct lttng_event_rule
*rule
)
246 assert(rule
->get_filter_bytecode
);
247 return rule
->get_filter_bytecode(rule
);
251 struct lttng_event_exclusion
*lttng_event_rule_generate_exclusions(
252 const struct lttng_event_rule
*rule
)
254 assert(rule
->generate_exclusions
);
255 return rule
->generate_exclusions(rule
);
259 const char *lttng_event_rule_type_str(enum lttng_event_rule_type type
)
262 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
264 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
266 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
268 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
270 case LTTNG_EVENT_RULE_TYPE_KRETPROBE
:
272 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
273 return "userspace-probe";