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/hashtable/hashtable.h>
12 #include <common/hashtable/utils.h>
13 #include <common/macros.h>
14 #include <common/mi-lttng.h>
15 #include <common/payload-view.h>
16 #include <common/payload.h>
17 #include <lttng/event-rule/event-rule-internal.h>
18 #include <lttng/event-rule/jul-logging-internal.h>
19 #include <lttng/event-rule/kernel-kprobe-internal.h>
20 #include <lttng/event-rule/kernel-syscall-internal.h>
21 #include <lttng/event-rule/kernel-tracepoint-internal.h>
22 #include <lttng/event-rule/kernel-uprobe-internal.h>
23 #include <lttng/event-rule/log4j-logging-internal.h>
24 #include <lttng/event-rule/python-logging-internal.h>
25 #include <lttng/event-rule/user-tracepoint-internal.h>
28 enum lttng_event_rule_type
lttng_event_rule_get_type(
29 const struct lttng_event_rule
*event_rule
)
31 return event_rule
? event_rule
->type
: LTTNG_EVENT_RULE_TYPE_UNKNOWN
;
35 enum lttng_domain_type
lttng_event_rule_get_domain_type(
36 const struct lttng_event_rule
*event_rule
)
38 enum lttng_domain_type domain_type
= LTTNG_DOMAIN_NONE
;
40 switch (lttng_event_rule_get_type(event_rule
)) {
41 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
42 domain_type
= LTTNG_DOMAIN_UST
;
44 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
45 domain_type
= LTTNG_DOMAIN_JUL
;
47 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
48 domain_type
= LTTNG_DOMAIN_LOG4J
;
50 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
51 domain_type
= LTTNG_DOMAIN_PYTHON
;
53 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
54 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
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_UPROBE
:
185 create_from_payload
= lttng_event_rule_kernel_uprobe_create_from_payload
;
187 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
188 create_from_payload
=
189 lttng_event_rule_kernel_syscall_create_from_payload
;
191 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
192 create_from_payload
=
193 lttng_event_rule_kernel_tracepoint_create_from_payload
;
195 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
196 create_from_payload
=
197 lttng_event_rule_user_tracepoint_create_from_payload
;
199 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
200 create_from_payload
=
201 lttng_event_rule_jul_logging_create_from_payload
;
203 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
204 create_from_payload
=
205 lttng_event_rule_log4j_logging_create_from_payload
;
207 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
208 create_from_payload
=
209 lttng_event_rule_python_logging_create_from_payload
;
212 ERR("Attempted to create event rule of unknown type (%i)",
213 (int) event_rule_comm
->event_rule_type
);
218 assert(create_from_payload
);
221 struct lttng_payload_view child_view
=
222 lttng_payload_view_from_view(
225 ret
= create_from_payload(&child_view
, event_rule
);
233 if (!lttng_event_rule_validate(*event_rule
)) {
244 void lttng_event_rule_init(struct lttng_event_rule
*event_rule
,
245 enum lttng_event_rule_type type
)
247 urcu_ref_init(&event_rule
->ref
);
248 event_rule
->type
= type
;
252 bool lttng_event_rule_get(struct lttng_event_rule
*event_rule
)
254 return urcu_ref_get_unless_zero(&event_rule
->ref
);
258 void lttng_event_rule_put(struct lttng_event_rule
*event_rule
)
264 assert(event_rule
->ref
.refcount
);
265 urcu_ref_put(&event_rule
->ref
, lttng_event_rule_release
);
269 enum lttng_error_code
lttng_event_rule_generate_filter_bytecode(
270 struct lttng_event_rule
*rule
,
271 const struct lttng_credentials
*creds
)
273 assert(rule
->generate_filter_bytecode
);
274 return rule
->generate_filter_bytecode(rule
, creds
);
278 const char *lttng_event_rule_get_filter(const struct lttng_event_rule
*rule
)
280 assert(rule
->get_filter
);
281 return rule
->get_filter(rule
);
285 const struct lttng_bytecode
*lttng_event_rule_get_filter_bytecode(
286 const struct lttng_event_rule
*rule
)
288 assert(rule
->get_filter_bytecode
);
289 return rule
->get_filter_bytecode(rule
);
293 enum lttng_event_rule_generate_exclusions_status
294 lttng_event_rule_generate_exclusions(const struct lttng_event_rule
*rule
,
295 struct lttng_event_exclusion
**exclusions
)
297 assert(rule
->generate_exclusions
);
298 return rule
->generate_exclusions(rule
, exclusions
);
302 struct lttng_event
*lttng_event_rule_generate_lttng_event(
303 const struct lttng_event_rule
*rule
)
305 assert(rule
->generate_lttng_event
);
306 return rule
->generate_lttng_event(rule
);
310 bool lttng_event_rule_targets_agent_domain(const struct lttng_event_rule
*rule
)
312 bool targets_agent_domain
= false;
313 enum lttng_domain_type type
= lttng_event_rule_get_domain_type(rule
);
316 case LTTNG_DOMAIN_JUL
:
317 case LTTNG_DOMAIN_LOG4J
:
318 case LTTNG_DOMAIN_PYTHON
:
319 targets_agent_domain
= true;
321 case LTTNG_DOMAIN_UST
:
322 case LTTNG_DOMAIN_KERNEL
:
323 targets_agent_domain
= false;
329 return targets_agent_domain
;
332 const char *lttng_event_rule_type_str(enum lttng_event_rule_type type
)
335 case LTTNG_EVENT_RULE_TYPE_UNKNOWN
:
337 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
338 return "kernel syscall";
339 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
340 return "kernel kprobe";
341 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
342 return "kernel uprobe";
343 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
344 return "kernel tracepoint";
345 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT
:
346 return "user tracepoint";
347 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING
:
348 return "jul logging";
349 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING
:
350 return "log4j logging";
351 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING
:
352 return "python logging";
360 unsigned long lttng_event_rule_hash(const struct lttng_event_rule
*rule
)
363 return rule
->hash(rule
);
367 enum lttng_error_code
lttng_event_rule_mi_serialize(
368 const struct lttng_event_rule
*rule
, struct mi_writer
*writer
)
371 enum lttng_error_code ret_code
;
375 assert(rule
->mi_serialize
);
377 /* Open event rule element. */
378 ret
= mi_lttng_writer_open_element(writer
, mi_lttng_element_event_rule
);
383 /* Serialize underlying event rule. */
384 ret_code
= rule
->mi_serialize(rule
, writer
);
385 if (ret_code
!= LTTNG_OK
) {
389 /* Close event rule element. */
390 ret
= mi_lttng_writer_close_element(writer
);
399 ret_code
= LTTNG_ERR_MI_IO_FAIL
;