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 <common/hashtable/hashtable.h>
15 #include <common/hashtable/utils.h>
16 #include <lttng/event-rule/event-rule-internal.h>
17 #include <lttng/event-rule/kernel-kprobe-internal.h>
18 #include <lttng/event-rule/kernel-syscall-internal.h>
19 #include <lttng/event-rule/log4j-logging-internal.h>
20 #include <lttng/event-rule/jul-logging-internal.h>
21 #include <lttng/event-rule/python-logging-internal.h>
22 #include <lttng/event-rule/kernel-tracepoint-internal.h>
23 #include <lttng/event-rule/kernel-uprobe-internal.h>
24 #include <lttng/event-rule/user-tracepoint-internal.h>
27 enum lttng_event_rule_type
lttng_event_rule_get_type(
28 const struct lttng_event_rule
*event_rule
)
30 return event_rule
? event_rule
->type
: LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
34 enum lttng_domain_type
lttng_event_rule_get_domain_type(
35 const struct lttng_event_rule
*event_rule
)
37 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
39 switch (lttng_event_rule_get_type(event_rule
)) {
40 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
41 domain_type
= LTTNG_DOMAIN_UST
;
43 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
44 domain_type
= LTTNG_DOMAIN_JUL
;
46 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
47 domain_type
= LTTNG_DOMAIN_LOG4J
;
49 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
50 domain_type
= LTTNG_DOMAIN_PYTHON
;
52 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
53 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
54 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
55 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
56 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
57 domain_type
= LTTNG_DOMAIN_KERNEL
;
59 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
60 domain_type
= LTTNG_DOMAIN_NONE
;
67 static void lttng_event_rule_release(struct urcu_ref
*ref
)
69 struct lttng_event_rule
*event_rule
=
70 container_of(ref
, typeof(*event_rule
), ref
);
72 assert(event_rule
->destroy
);
73 event_rule
->destroy(event_rule
);
76 void lttng_event_rule_destroy(struct lttng_event_rule
*event_rule
)
78 lttng_event_rule_put(event_rule
);
82 bool lttng_event_rule_validate(const struct lttng_event_rule
*event_rule
)
91 if (!event_rule
->validate
) {
92 /* Sub-class guarantees that it can never be invalid. */
97 valid
= event_rule
->validate(event_rule
);
103 int lttng_event_rule_serialize(const struct lttng_event_rule
*event_rule
,
104 struct lttng_payload
*payload
)
107 struct lttng_event_rule_comm event_rule_comm
= {};
114 event_rule_comm
.event_rule_type
= (int8_t) event_rule
->type
;
116 ret
= lttng_dynamic_buffer_append(
117 &payload
->buffer
, &event_rule_comm
, sizeof(event_rule_comm
));
122 ret
= event_rule
->serialize(event_rule
, payload
);
131 bool lttng_event_rule_is_equal(const struct lttng_event_rule
*a
,
132 const struct lttng_event_rule
*b
)
134 bool is_equal
= false;
140 if (a
->type
!= b
->type
) {
149 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
155 ssize_t
lttng_event_rule_create_from_payload(
156 struct lttng_payload_view
*view
,
157 struct lttng_event_rule
**event_rule
)
159 ssize_t ret
, consumed
= 0;
160 event_rule_create_from_payload_cb create_from_payload
= NULL
;
161 const struct lttng_event_rule_comm
*event_rule_comm
;
162 const struct lttng_payload_view event_rule_comm_view
=
163 lttng_payload_view_from_view(
164 view
, 0, sizeof(*event_rule_comm
));
166 if (!view
|| !event_rule
) {
171 if (!lttng_payload_view_is_valid(&event_rule_comm_view
)) {
176 DBG("Deserializing event_rule from payload");
177 event_rule_comm
= (const struct lttng_event_rule_comm
*) event_rule_comm_view
.buffer
.data
;
178 consumed
+= sizeof(*event_rule_comm
);
180 switch ((enum lttng_event_rule_type
) event_rule_comm
->event_rule_type
) {
181 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
182 create_from_payload
= lttng_event_rule_kernel_kprobe_create_from_payload
;
184 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
187 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
188 create_from_payload
= lttng_event_rule_kernel_uprobe_create_from_payload
;
190 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
191 create_from_payload
=
192 lttng_event_rule_kernel_syscall_create_from_payload
;
194 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
195 create_from_payload
=
196 lttng_event_rule_kernel_tracepoint_create_from_payload
;
198 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
199 create_from_payload
=
200 lttng_event_rule_user_tracepoint_create_from_payload
;
202 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
203 create_from_payload
=
204 lttng_event_rule_jul_logging_create_from_payload
;
206 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
207 create_from_payload
=
208 lttng_event_rule_log4j_logging_create_from_payload
;
210 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
211 create_from_payload
=
212 lttng_event_rule_python_logging_create_from_payload
;
215 ERR("Attempted to create event rule of unknown type (%i)",
216 (int) event_rule_comm
->event_rule_type
);
221 assert(create_from_payload
);
224 struct lttng_payload_view child_view
=
225 lttng_payload_view_from_view(
228 ret
= create_from_payload(&child_view
, event_rule
);
236 if (!lttng_event_rule_validate(*event_rule
)) {
247 void lttng_event_rule_init(struct lttng_event_rule
*event_rule
,
248 enum lttng_event_rule_type type
)
250 urcu_ref_init(&event_rule
->ref
);
251 event_rule
->type
= type
;
255 bool lttng_event_rule_get(struct lttng_event_rule
*event_rule
)
257 return urcu_ref_get_unless_zero(&event_rule
->ref
);
261 void lttng_event_rule_put(struct lttng_event_rule
*event_rule
)
267 assert(event_rule
->ref
.refcount
);
268 urcu_ref_put(&event_rule
->ref
, lttng_event_rule_release
);
272 enum lttng_error_code
lttng_event_rule_generate_filter_bytecode(
273 struct lttng_event_rule
*rule
,
274 const struct lttng_credentials
*creds
)
276 assert(rule
->generate_filter_bytecode
);
277 return rule
->generate_filter_bytecode(rule
, creds
);
281 const char *lttng_event_rule_get_filter(const struct lttng_event_rule
*rule
)
283 assert(rule
->get_filter
);
284 return rule
->get_filter(rule
);
288 const struct lttng_bytecode
*lttng_event_rule_get_filter_bytecode(
289 const struct lttng_event_rule
*rule
)
291 assert(rule
->get_filter_bytecode
);
292 return rule
->get_filter_bytecode(rule
);
296 enum lttng_event_rule_generate_exclusions_status
297 lttng_event_rule_generate_exclusions(const struct lttng_event_rule
*rule
,
298 struct lttng_event_exclusion
**exclusions
)
300 assert(rule
->generate_exclusions
);
301 return rule
->generate_exclusions(rule
, exclusions
);
305 struct lttng_event
*lttng_event_rule_generate_lttng_event(
306 const struct lttng_event_rule
*rule
)
308 assert(rule
->generate_lttng_event
);
309 return rule
->generate_lttng_event(rule
);
313 bool lttng_event_rule_targets_agent_domain(const struct lttng_event_rule
*rule
)
315 bool targets_agent_domain
= false;
316 enum lttng_domain_type type
= lttng_event_rule_get_domain_type(rule
);
319 case LTTNG_DOMAIN_JUL
:
320 case LTTNG_DOMAIN_LOG4J
:
321 case LTTNG_DOMAIN_PYTHON
:
322 targets_agent_domain
= true;
324 case LTTNG_DOMAIN_UST
:
325 case LTTNG_DOMAIN_KERNEL
:
326 targets_agent_domain
= false;
332 return targets_agent_domain
;
335 const char *lttng_event_rule_type_str(enum lttng_event_rule_type type
)
338 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
340 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
341 return "kernel syscall";
342 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
343 return "kernel kprobe";
344 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
346 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
347 return "kernel uprobe";
348 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
349 return "kernel tracepoint";
350 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
351 return "user tracepoint";
352 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
353 return "jul logging";
354 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
355 return "log4j logging";
356 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
357 return "python logging";
365 unsigned long lttng_event_rule_hash(const struct lttng_event_rule
*rule
)
368 return rule
->hash(rule
);