2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #include <common/credentials.hpp>
9 #include <common/error.hpp>
10 #include <common/hashtable/hashtable.hpp>
11 #include <common/hashtable/utils.hpp>
12 #include <common/macros.hpp>
13 #include <common/mi-lttng.hpp>
14 #include <common/payload-view.hpp>
15 #include <common/payload.hpp>
16 #include <common/runas.hpp>
18 #include <lttng/constant.h>
19 #include <lttng/event-rule/event-rule-internal.hpp>
20 #include <lttng/event-rule/event-rule.h>
21 #include <lttng/event-rule/kernel-kprobe-internal.hpp>
22 #include <lttng/kernel-probe-internal.hpp>
23 #include <lttng/kernel-probe.h>
26 #define IS_KPROBE_EVENT_RULE(rule) \
27 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE)
29 #if (LTTNG_SYMBOL_NAME_LEN == 256)
30 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
33 static void lttng_event_rule_kernel_kprobe_destroy(struct lttng_event_rule
*rule
)
35 struct lttng_event_rule_kernel_kprobe
*kprobe
;
37 kprobe
= container_of(rule
, struct lttng_event_rule_kernel_kprobe
, parent
);
39 lttng_kernel_probe_location_destroy(kprobe
->location
);
44 static bool lttng_event_rule_kernel_kprobe_validate(
45 const struct lttng_event_rule
*rule
)
48 struct lttng_event_rule_kernel_kprobe
*kprobe
;
54 kprobe
= container_of(rule
, struct lttng_event_rule_kernel_kprobe
, parent
);
58 ERR("Invalid name event rule: a name must be set.");
63 if(!kprobe
->location
) {
64 ERR("Invalid name event rule: a location must be set.");
73 static int lttng_event_rule_kernel_kprobe_serialize(
74 const struct lttng_event_rule
*rule
,
75 struct lttng_payload
*payload
)
78 size_t name_len
, header_offset
, size_before_location
;
79 struct lttng_event_rule_kernel_kprobe
*kprobe
;
80 struct lttng_event_rule_kernel_kprobe_comm kprobe_comm
;
81 struct lttng_event_rule_kernel_kprobe_comm
*header
;
83 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
)) {
88 header_offset
= payload
->buffer
.size
;
90 DBG("Serializing kprobe event rule.");
91 kprobe
= container_of(rule
, struct lttng_event_rule_kernel_kprobe
, parent
);
93 name_len
= strlen(kprobe
->name
) + 1;
94 kprobe_comm
.name_len
= name_len
;
96 ret
= lttng_dynamic_buffer_append(
97 &payload
->buffer
, &kprobe_comm
, sizeof(kprobe_comm
));
102 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, kprobe
->name
, name_len
);
107 size_before_location
= payload
->buffer
.size
;
109 ret
= lttng_kernel_probe_location_serialize(kprobe
->location
, payload
);
114 /* Update the header regarding the probe size. */
115 header
= (struct lttng_event_rule_kernel_kprobe_comm
*) (
116 (char *) payload
->buffer
.data
+ header_offset
);
117 header
->location_len
= payload
->buffer
.size
- size_before_location
;
125 static bool lttng_event_rule_kernel_kprobe_is_equal(const struct lttng_event_rule
*_a
,
126 const struct lttng_event_rule
*_b
)
128 bool is_equal
= false;
129 struct lttng_event_rule_kernel_kprobe
*a
, *b
;
131 a
= container_of(_a
, struct lttng_event_rule_kernel_kprobe
, parent
);
132 b
= container_of(_b
, struct lttng_event_rule_kernel_kprobe
, parent
);
135 if (!!a
->name
!= !!b
->name
) {
140 LTTNG_ASSERT(a
->name
);
141 LTTNG_ASSERT(b
->name
);
142 if (strcmp(a
->name
, b
->name
)) {
146 is_equal
= lttng_kernel_probe_location_is_equal(
147 a
->location
, b
->location
);
152 static enum lttng_error_code
lttng_event_rule_kernel_kprobe_generate_filter_bytecode(
153 struct lttng_event_rule
*rule
__attribute__((unused
)),
154 const struct lttng_credentials
*creds
__attribute__((unused
)))
160 static const char *lttng_event_rule_kernel_kprobe_get_filter(
161 const struct lttng_event_rule
*rule
__attribute__((unused
)))
167 static const struct lttng_bytecode
*
168 lttng_event_rule_kernel_kprobe_get_filter_bytecode(
169 const struct lttng_event_rule
*rule
__attribute__((unused
)))
175 static enum lttng_event_rule_generate_exclusions_status
176 lttng_event_rule_kernel_kprobe_generate_exclusions(
177 const struct lttng_event_rule
*rule
__attribute__((unused
)),
178 struct lttng_event_exclusion
**exclusions
)
182 return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
;
186 lttng_event_rule_kernel_kprobe_hash(
187 const struct lttng_event_rule
*rule
)
190 struct lttng_event_rule_kernel_kprobe
*krule
=
191 container_of(rule
, typeof(*krule
), parent
);
193 hash
= hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
,
195 hash
^= hash_key_str(krule
->name
, lttng_ht_seed
);
196 hash
^= lttng_kernel_probe_location_hash(krule
->location
);
202 int kernel_probe_set_location(
203 struct lttng_event_rule_kernel_kprobe
*kprobe
,
204 const struct lttng_kernel_probe_location
*location
)
207 struct lttng_kernel_probe_location
*location_copy
= NULL
;
209 if (!kprobe
|| !location
|| kprobe
->location
) {
214 location_copy
= lttng_kernel_probe_location_copy(location
);
215 if (!location_copy
) {
220 kprobe
->location
= location_copy
;
221 location_copy
= NULL
;
224 lttng_kernel_probe_location_destroy(location_copy
);
229 enum lttng_error_code
lttng_event_rule_kernel_kprobe_mi_serialize(
230 const struct lttng_event_rule
*rule
, struct mi_writer
*writer
)
233 enum lttng_error_code ret_code
;
234 enum lttng_event_rule_status status
;
235 const char *event_name
= NULL
;
236 const struct lttng_kernel_probe_location
*location
= NULL
;
239 LTTNG_ASSERT(writer
);
240 LTTNG_ASSERT(IS_KPROBE_EVENT_RULE(rule
));
242 status
= lttng_event_rule_kernel_kprobe_get_event_name(
244 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
245 LTTNG_ASSERT(event_name
);
247 status
= lttng_event_rule_kernel_kprobe_get_location(rule
, &location
);
248 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
249 LTTNG_ASSERT(location
);
251 /* Open event rule kernel kprobe element. */
252 ret
= mi_lttng_writer_open_element(
253 writer
, mi_lttng_element_event_rule_kernel_kprobe
);
259 ret
= mi_lttng_writer_write_element_string(writer
,
260 mi_lttng_element_event_rule_event_name
, event_name
);
265 /* Probe location. */
266 ret_code
= lttng_kernel_probe_location_mi_serialize(location
, writer
);
267 if (ret_code
!= LTTNG_OK
) {
271 /* Close event rule kernel kprobe element. */
272 ret
= mi_lttng_writer_close_element(writer
);
281 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
286 struct lttng_event_rule
*lttng_event_rule_kernel_kprobe_create(
287 const struct lttng_kernel_probe_location
*location
)
289 struct lttng_event_rule
*rule
= NULL
;
290 struct lttng_event_rule_kernel_kprobe
*krule
;
292 krule
= zmalloc
<lttng_event_rule_kernel_kprobe
>();
297 rule
= &krule
->parent
;
298 lttng_event_rule_init(&krule
->parent
, LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
);
299 krule
->parent
.validate
= lttng_event_rule_kernel_kprobe_validate
;
300 krule
->parent
.serialize
= lttng_event_rule_kernel_kprobe_serialize
;
301 krule
->parent
.equal
= lttng_event_rule_kernel_kprobe_is_equal
;
302 krule
->parent
.destroy
= lttng_event_rule_kernel_kprobe_destroy
;
303 krule
->parent
.generate_filter_bytecode
=
304 lttng_event_rule_kernel_kprobe_generate_filter_bytecode
;
305 krule
->parent
.get_filter
= lttng_event_rule_kernel_kprobe_get_filter
;
306 krule
->parent
.get_filter_bytecode
=
307 lttng_event_rule_kernel_kprobe_get_filter_bytecode
;
308 krule
->parent
.generate_exclusions
=
309 lttng_event_rule_kernel_kprobe_generate_exclusions
;
310 krule
->parent
.hash
= lttng_event_rule_kernel_kprobe_hash
;
311 krule
->parent
.mi_serialize
= lttng_event_rule_kernel_kprobe_mi_serialize
;
313 if (kernel_probe_set_location(krule
, location
)) {
314 lttng_event_rule_destroy(rule
);
322 ssize_t
lttng_event_rule_kernel_kprobe_create_from_payload(
323 struct lttng_payload_view
*view
,
324 struct lttng_event_rule
**_event_rule
)
326 ssize_t ret
, offset
= 0;
327 enum lttng_event_rule_status status
;
328 const struct lttng_event_rule_kernel_kprobe_comm
*kprobe_comm
;
330 struct lttng_buffer_view current_buffer_view
;
331 struct lttng_event_rule
*rule
= NULL
;
332 struct lttng_kernel_probe_location
*location
= NULL
;
339 current_buffer_view
= lttng_buffer_view_from_view(
340 &view
->buffer
, offset
, sizeof(*kprobe_comm
));
341 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
342 ERR("Failed to initialize from malformed event rule kprobe: buffer too short to contain header.");
347 kprobe_comm
= (typeof(kprobe_comm
)) current_buffer_view
.data
;
349 /* Skip to payload */
350 offset
+= current_buffer_view
.size
;
354 struct lttng_payload_view current_payload_view
=
355 lttng_payload_view_from_view(view
, offset
,
356 kprobe_comm
->name_len
);
358 if (!lttng_payload_view_is_valid(¤t_payload_view
)) {
363 name
= current_payload_view
.buffer
.data
;
364 if (!lttng_buffer_view_contains_string(
365 ¤t_payload_view
.buffer
, name
,
366 kprobe_comm
->name_len
)) {
372 /* Skip after the name. */
373 offset
+= kprobe_comm
->name_len
;
375 /* Map the kernel probe location. */
377 struct lttng_payload_view current_payload_view
=
378 lttng_payload_view_from_view(view
, offset
,
379 kprobe_comm
->location_len
);
381 if (!lttng_payload_view_is_valid(¤t_payload_view
)) {
386 ret
= lttng_kernel_probe_location_create_from_payload(
387 ¤t_payload_view
, &location
);
394 if (ret
!= kprobe_comm
->location_len
) {
399 /* Skip after the location */
400 offset
+= kprobe_comm
->location_len
;
402 rule
= lttng_event_rule_kernel_kprobe_create(location
);
404 ERR("Failed to create event rule kprobe.");
409 status
= lttng_event_rule_kernel_kprobe_set_event_name(rule
, name
);
410 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
411 ERR("Failed to set event rule kprobe name.");
420 lttng_kernel_probe_location_destroy(location
);
421 lttng_event_rule_destroy(rule
);
425 enum lttng_event_rule_status
lttng_event_rule_kernel_kprobe_get_location(
426 const struct lttng_event_rule
*rule
,
427 const struct lttng_kernel_probe_location
**location
)
429 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
430 struct lttng_event_rule_kernel_kprobe
*kprobe
;
432 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !location
) {
433 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
437 kprobe
= container_of(rule
, struct lttng_event_rule_kernel_kprobe
, parent
);
438 *location
= kprobe
->location
;
441 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
449 enum lttng_event_rule_status
lttng_event_rule_kernel_kprobe_set_event_name(
450 struct lttng_event_rule
*rule
, const char *name
)
452 char *name_copy
= NULL
;
453 struct lttng_event_rule_kernel_kprobe
*kprobe
;
454 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
456 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !name
||
458 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
462 kprobe
= container_of(rule
, struct lttng_event_rule_kernel_kprobe
, parent
);
463 name_copy
= strdup(name
);
465 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
471 kprobe
->name
= name_copy
;
477 enum lttng_event_rule_status
lttng_event_rule_kernel_kprobe_get_event_name(
478 const struct lttng_event_rule
*rule
, const char **name
)
480 struct lttng_event_rule_kernel_kprobe
*kprobe
;
481 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
483 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !name
) {
484 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
488 kprobe
= container_of(rule
, struct lttng_event_rule_kernel_kprobe
, parent
);
490 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
494 *name
= kprobe
->name
;