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>
17 #include <lttng/event-rule/event-rule-internal.hpp>
18 #include <lttng/event-rule/kernel-uprobe-internal.hpp>
19 #include <lttng/userspace-probe-internal.hpp>
21 #define IS_UPROBE_EVENT_RULE(rule) \
22 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE)
24 static void lttng_event_rule_kernel_uprobe_destroy(struct lttng_event_rule
*rule
)
26 struct lttng_event_rule_kernel_uprobe
*uprobe
;
28 uprobe
= container_of(rule
, struct lttng_event_rule_kernel_uprobe
, parent
);
30 lttng_userspace_probe_location_destroy(uprobe
->location
);
35 static bool lttng_event_rule_kernel_uprobe_validate(
36 const struct lttng_event_rule
*rule
)
39 struct lttng_event_rule_kernel_uprobe
*uprobe
;
45 uprobe
= container_of(rule
, struct lttng_event_rule_kernel_uprobe
, parent
);
49 ERR("Invalid uprobe event rule: a pattern must be set.");
53 if (!uprobe
->location
) {
54 ERR("Invalid uprobe event rule: a location must be set.");
63 static int lttng_event_rule_kernel_uprobe_serialize(
64 const struct lttng_event_rule
*rule
,
65 struct lttng_payload
*payload
)
68 size_t name_len
, header_offset
, size_before_probe
;
69 struct lttng_event_rule_kernel_uprobe
*uprobe
;
70 struct lttng_event_rule_kernel_uprobe_comm uprobe_comm
= {};
71 struct lttng_event_rule_kernel_uprobe_comm
*header
;
73 if (!rule
|| !IS_UPROBE_EVENT_RULE(rule
)) {
78 header_offset
= payload
->buffer
.size
;
80 DBG("Serializing uprobe event rule.");
81 uprobe
= container_of(rule
, struct lttng_event_rule_kernel_uprobe
, parent
);
83 name_len
= strlen(uprobe
->name
) + 1;
85 uprobe_comm
.name_len
= name_len
;
87 ret
= lttng_dynamic_buffer_append(
88 &payload
->buffer
, &uprobe_comm
, sizeof(uprobe_comm
));
92 ret
= lttng_dynamic_buffer_append(
93 &payload
->buffer
, uprobe
->name
, name_len
);
98 size_before_probe
= payload
->buffer
.size
;
100 /* This serialize return the size taken in the buffer. */
101 ret
= lttng_userspace_probe_location_serialize(
102 uprobe
->location
, payload
);
107 /* Update the header regarding the probe size. */
108 header
= (struct lttng_event_rule_kernel_uprobe_comm
109 *) ((char *) payload
->buffer
.data
+
111 header
->location_len
= payload
->buffer
.size
- size_before_probe
;
119 static bool lttng_event_rule_kernel_uprobe_is_equal(const struct lttng_event_rule
*_a
,
120 const struct lttng_event_rule
*_b
)
122 bool is_equal
= false;
123 struct lttng_event_rule_kernel_uprobe
*a
, *b
;
125 a
= container_of(_a
, struct lttng_event_rule_kernel_uprobe
, parent
);
126 b
= container_of(_b
, struct lttng_event_rule_kernel_uprobe
, parent
);
128 /* uprobe is invalid if this is not true. */
129 LTTNG_ASSERT(a
->name
);
130 LTTNG_ASSERT(b
->name
);
131 if (strcmp(a
->name
, b
->name
)) {
135 LTTNG_ASSERT(a
->location
);
136 LTTNG_ASSERT(b
->location
);
137 is_equal
= lttng_userspace_probe_location_is_equal(
138 a
->location
, b
->location
);
143 static enum lttng_error_code
lttng_event_rule_kernel_uprobe_generate_filter_bytecode(
144 struct lttng_event_rule
*rule
__attribute__((unused
)),
145 const struct lttng_credentials
*creds
__attribute__((unused
)))
151 static const char *lttng_event_rule_kernel_uprobe_get_filter(
152 const struct lttng_event_rule
*rule
__attribute__((unused
)))
158 static const struct lttng_bytecode
*
159 lttng_event_rule_kernel_uprobe_get_filter_bytecode(
160 const struct lttng_event_rule
*rule
__attribute__((unused
)))
166 static enum lttng_event_rule_generate_exclusions_status
167 lttng_event_rule_kernel_uprobe_generate_exclusions(
168 const struct lttng_event_rule
*rule
__attribute__((unused
)),
169 struct lttng_event_exclusion
**exclusions
)
173 return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
;
177 lttng_event_rule_kernel_uprobe_hash(
178 const struct lttng_event_rule
*rule
)
181 struct lttng_event_rule_kernel_uprobe
*urule
=
182 container_of(rule
, typeof(*urule
), parent
);
184 hash
= hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
,
186 hash
^= hash_key_str(urule
->name
, lttng_ht_seed
);
187 hash
^= lttng_userspace_probe_location_hash(urule
->location
);
193 int userspace_probe_set_location(
194 struct lttng_event_rule_kernel_uprobe
*uprobe
,
195 const struct lttng_userspace_probe_location
*location
)
198 struct lttng_userspace_probe_location
*location_copy
= NULL
;
200 if (!uprobe
|| !location
|| uprobe
->location
) {
205 location_copy
= lttng_userspace_probe_location_copy(location
);
206 if (!location_copy
) {
211 uprobe
->location
= location_copy
;
212 location_copy
= NULL
;
215 lttng_userspace_probe_location_destroy(location_copy
);
219 static enum lttng_error_code
lttng_event_rule_kernel_uprobe_mi_serialize(
220 const struct lttng_event_rule
*rule
, struct mi_writer
*writer
)
223 enum lttng_error_code ret_code
;
224 enum lttng_event_rule_status status
;
225 const char *event_name
= NULL
;
226 const struct lttng_userspace_probe_location
*location
= NULL
;
229 LTTNG_ASSERT(writer
);
230 LTTNG_ASSERT(IS_UPROBE_EVENT_RULE(rule
));
232 status
= lttng_event_rule_kernel_uprobe_get_event_name(
234 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
235 LTTNG_ASSERT(event_name
);
237 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
238 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
239 LTTNG_ASSERT(location
);
241 /* Open event rule kernel uprobe element. */
242 ret
= mi_lttng_writer_open_element(
243 writer
, mi_lttng_element_event_rule_kernel_uprobe
);
249 ret
= mi_lttng_writer_write_element_string(writer
,
250 mi_lttng_element_event_rule_event_name
, event_name
);
255 /* Probe location. */
256 ret_code
= lttng_userspace_probe_location_mi_serialize(location
, writer
);
257 if (ret_code
!= LTTNG_OK
) {
261 /* Close event rule kernel uprobe element. */
262 ret
= mi_lttng_writer_close_element(writer
);
271 ret_code
= LTTNG_ERR_MI_IO_FAIL
;
276 struct lttng_event_rule
*lttng_event_rule_kernel_uprobe_create(
277 const struct lttng_userspace_probe_location
*location
)
279 struct lttng_event_rule
*rule
= NULL
;
280 struct lttng_event_rule_kernel_uprobe
*urule
;
282 urule
= (lttng_event_rule_kernel_uprobe
*) zmalloc(sizeof(struct lttng_event_rule_kernel_uprobe
));
287 rule
= &urule
->parent
;
288 lttng_event_rule_init(&urule
->parent
, LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
);
289 urule
->parent
.validate
= lttng_event_rule_kernel_uprobe_validate
;
290 urule
->parent
.serialize
= lttng_event_rule_kernel_uprobe_serialize
;
291 urule
->parent
.equal
= lttng_event_rule_kernel_uprobe_is_equal
;
292 urule
->parent
.destroy
= lttng_event_rule_kernel_uprobe_destroy
;
293 urule
->parent
.generate_filter_bytecode
=
294 lttng_event_rule_kernel_uprobe_generate_filter_bytecode
;
295 urule
->parent
.get_filter
= lttng_event_rule_kernel_uprobe_get_filter
;
296 urule
->parent
.get_filter_bytecode
=
297 lttng_event_rule_kernel_uprobe_get_filter_bytecode
;
298 urule
->parent
.generate_exclusions
=
299 lttng_event_rule_kernel_uprobe_generate_exclusions
;
300 urule
->parent
.hash
= lttng_event_rule_kernel_uprobe_hash
;
301 urule
->parent
.mi_serialize
= lttng_event_rule_kernel_uprobe_mi_serialize
;
303 if (userspace_probe_set_location(urule
, location
)) {
304 lttng_event_rule_destroy(rule
);
312 ssize_t
lttng_event_rule_kernel_uprobe_create_from_payload(
313 struct lttng_payload_view
*view
,
314 struct lttng_event_rule
**_event_rule
)
316 ssize_t ret
, offset
= 0;
317 const struct lttng_event_rule_kernel_uprobe_comm
*uprobe_comm
;
319 struct lttng_buffer_view current_buffer_view
;
320 struct lttng_event_rule
*rule
= NULL
;
321 struct lttng_userspace_probe_location
*location
= NULL
;
322 enum lttng_event_rule_status status
;
329 current_buffer_view
= lttng_buffer_view_from_view(
330 &view
->buffer
, offset
, sizeof(*uprobe_comm
));
331 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
332 ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain header");
337 uprobe_comm
= (typeof(uprobe_comm
)) current_buffer_view
.data
;
339 /* Skip to payload. */
340 offset
+= current_buffer_view
.size
;
343 current_buffer_view
= lttng_buffer_view_from_view(
344 &view
->buffer
, offset
, uprobe_comm
->name_len
);
345 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
350 name
= current_buffer_view
.data
;
351 if (!lttng_buffer_view_contains_string(¤t_buffer_view
, name
,
352 uprobe_comm
->name_len
)) {
357 /* Skip after the name. */
358 offset
+= uprobe_comm
->name_len
;
360 /* Map the location. */
362 struct lttng_payload_view current_payload_view
=
363 lttng_payload_view_from_view(view
, offset
,
364 uprobe_comm
->location_len
);
366 if (!lttng_payload_view_is_valid(¤t_payload_view
)) {
367 ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain location");
372 ret
= lttng_userspace_probe_location_create_from_payload(
373 ¤t_payload_view
, &location
);
380 LTTNG_ASSERT(ret
== uprobe_comm
->location_len
);
382 /* Skip after the location. */
383 offset
+= uprobe_comm
->location_len
;
385 rule
= lttng_event_rule_kernel_uprobe_create(location
);
387 ERR("Failed to create event rule uprobe.");
392 status
= lttng_event_rule_kernel_uprobe_set_event_name(rule
, name
);
393 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
398 if (!lttng_event_rule_kernel_uprobe_validate(rule
)) {
407 lttng_userspace_probe_location_destroy(location
);
408 lttng_event_rule_destroy(rule
);
413 enum lttng_event_rule_status
lttng_event_rule_kernel_uprobe_get_location(
414 const struct lttng_event_rule
*rule
,
415 const struct lttng_userspace_probe_location
**location
)
417 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
419 if (!rule
|| !IS_UPROBE_EVENT_RULE(rule
) || !location
) {
420 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
424 *location
= lttng_event_rule_kernel_uprobe_get_location_mutable(rule
);
426 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
434 struct lttng_userspace_probe_location
*
435 lttng_event_rule_kernel_uprobe_get_location_mutable(
436 const struct lttng_event_rule
*rule
)
438 struct lttng_event_rule_kernel_uprobe
*uprobe
;
441 uprobe
= container_of(rule
, struct lttng_event_rule_kernel_uprobe
, parent
);
443 return uprobe
->location
;
446 enum lttng_event_rule_status
lttng_event_rule_kernel_uprobe_set_event_name(
447 struct lttng_event_rule
*rule
, const char *name
)
449 char *name_copy
= NULL
;
450 struct lttng_event_rule_kernel_uprobe
*uprobe
;
451 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
453 if (!rule
|| !IS_UPROBE_EVENT_RULE(rule
) || !name
||
455 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
459 uprobe
= container_of(rule
, struct lttng_event_rule_kernel_uprobe
, parent
);
460 name_copy
= strdup(name
);
462 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
470 uprobe
->name
= name_copy
;
476 enum lttng_event_rule_status
lttng_event_rule_kernel_uprobe_get_event_name(
477 const struct lttng_event_rule
*rule
, const char **name
)
479 struct lttng_event_rule_kernel_uprobe
*uprobe
;
480 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
482 if (!rule
|| !IS_UPROBE_EVENT_RULE(rule
) || !name
) {
483 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
487 uprobe
= container_of(rule
, struct lttng_event_rule_kernel_uprobe
, parent
);
489 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
493 *name
= uprobe
->name
;