2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/credentials.h>
10 #include <common/error.h>
11 #include <common/macros.h>
12 #include <common/payload.h>
13 #include <common/payload-view.h>
14 #include <common/runas.h>
15 #include <common/hashtable/hashtable.h>
16 #include <common/hashtable/utils.h>
17 #include <lttng/event-rule/event-rule-internal.h>
18 #include <lttng/event-rule/kernel-uprobe-internal.h>
19 #include <lttng/userspace-probe-internal.h>
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. */
131 if (strcmp(a
->name
, b
->name
)) {
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
,
145 const struct lttng_credentials
*creds
)
151 static const char *lttng_event_rule_kernel_uprobe_get_filter(
152 const struct lttng_event_rule
*rule
)
158 static const struct lttng_bytecode
*
159 lttng_event_rule_kernel_uprobe_get_filter_bytecode(const struct lttng_event_rule
*rule
)
165 static enum lttng_event_rule_generate_exclusions_status
166 lttng_event_rule_kernel_uprobe_generate_exclusions(const struct lttng_event_rule
*rule
,
167 struct lttng_event_exclusion
**exclusions
)
171 return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
;
175 lttng_event_rule_kernel_uprobe_hash(
176 const struct lttng_event_rule
*rule
)
179 struct lttng_event_rule_kernel_uprobe
*urule
=
180 container_of(rule
, typeof(*urule
), parent
);
182 hash
= hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
,
184 hash
^= hash_key_str(urule
->name
, lttng_ht_seed
);
185 hash
^= lttng_userspace_probe_location_hash(urule
->location
);
191 int userspace_probe_set_location(
192 struct lttng_event_rule_kernel_uprobe
*uprobe
,
193 const struct lttng_userspace_probe_location
*location
)
196 struct lttng_userspace_probe_location
*location_copy
= NULL
;
198 if (!uprobe
|| !location
|| uprobe
->location
) {
203 location_copy
= lttng_userspace_probe_location_copy(location
);
204 if (!location_copy
) {
209 uprobe
->location
= location_copy
;
210 location_copy
= NULL
;
213 lttng_userspace_probe_location_destroy(location_copy
);
217 struct lttng_event_rule
*lttng_event_rule_kernel_uprobe_create(
218 const struct lttng_userspace_probe_location
*location
)
220 struct lttng_event_rule
*rule
= NULL
;
221 struct lttng_event_rule_kernel_uprobe
*urule
;
223 urule
= zmalloc(sizeof(struct lttng_event_rule_kernel_uprobe
));
228 rule
= &urule
->parent
;
229 lttng_event_rule_init(&urule
->parent
, LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
);
230 urule
->parent
.validate
= lttng_event_rule_kernel_uprobe_validate
;
231 urule
->parent
.serialize
= lttng_event_rule_kernel_uprobe_serialize
;
232 urule
->parent
.equal
= lttng_event_rule_kernel_uprobe_is_equal
;
233 urule
->parent
.destroy
= lttng_event_rule_kernel_uprobe_destroy
;
234 urule
->parent
.generate_filter_bytecode
=
235 lttng_event_rule_kernel_uprobe_generate_filter_bytecode
;
236 urule
->parent
.get_filter
= lttng_event_rule_kernel_uprobe_get_filter
;
237 urule
->parent
.get_filter_bytecode
=
238 lttng_event_rule_kernel_uprobe_get_filter_bytecode
;
239 urule
->parent
.generate_exclusions
=
240 lttng_event_rule_kernel_uprobe_generate_exclusions
;
241 urule
->parent
.hash
= lttng_event_rule_kernel_uprobe_hash
;
243 if (userspace_probe_set_location(urule
, location
)) {
244 lttng_event_rule_destroy(rule
);
253 ssize_t
lttng_event_rule_kernel_uprobe_create_from_payload(
254 struct lttng_payload_view
*view
,
255 struct lttng_event_rule
**_event_rule
)
257 ssize_t ret
, offset
= 0;
258 const struct lttng_event_rule_kernel_uprobe_comm
*uprobe_comm
;
260 struct lttng_buffer_view current_buffer_view
;
261 struct lttng_event_rule
*rule
= NULL
;
262 struct lttng_userspace_probe_location
*location
= NULL
;
263 enum lttng_event_rule_status status
;
270 current_buffer_view
= lttng_buffer_view_from_view(
271 &view
->buffer
, offset
, sizeof(*uprobe_comm
));
272 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
273 ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain header");
278 uprobe_comm
= (typeof(uprobe_comm
)) current_buffer_view
.data
;
280 /* Skip to payload. */
281 offset
+= current_buffer_view
.size
;
284 current_buffer_view
= lttng_buffer_view_from_view(
285 &view
->buffer
, offset
, uprobe_comm
->name_len
);
286 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
291 name
= current_buffer_view
.data
;
292 if (!lttng_buffer_view_contains_string(¤t_buffer_view
, name
,
293 uprobe_comm
->name_len
)) {
298 /* Skip after the name. */
299 offset
+= uprobe_comm
->name_len
;
301 /* Map the location. */
303 struct lttng_payload_view current_payload_view
=
304 lttng_payload_view_from_view(view
, offset
,
305 uprobe_comm
->location_len
);
307 if (!lttng_payload_view_is_valid(¤t_payload_view
)) {
308 ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain location");
313 ret
= lttng_userspace_probe_location_create_from_payload(
314 ¤t_payload_view
, &location
);
321 assert(ret
== uprobe_comm
->location_len
);
323 /* Skip after the location. */
324 offset
+= uprobe_comm
->location_len
;
326 rule
= lttng_event_rule_kernel_uprobe_create(location
);
328 ERR("Failed to create event rule uprobe.");
333 status
= lttng_event_rule_kernel_uprobe_set_event_name(rule
, name
);
334 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
339 if (!lttng_event_rule_kernel_uprobe_validate(rule
)) {
348 lttng_userspace_probe_location_destroy(location
);
349 lttng_event_rule_destroy(rule
);
354 enum lttng_event_rule_status
lttng_event_rule_kernel_uprobe_get_location(
355 const struct lttng_event_rule
*rule
,
356 const struct lttng_userspace_probe_location
**location
)
358 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
360 if (!rule
|| !IS_UPROBE_EVENT_RULE(rule
) || !location
) {
361 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
365 *location
= lttng_event_rule_kernel_uprobe_get_location_mutable(rule
);
367 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
376 struct lttng_userspace_probe_location
*
377 lttng_event_rule_kernel_uprobe_get_location_mutable(
378 const struct lttng_event_rule
*rule
)
380 struct lttng_event_rule_kernel_uprobe
*uprobe
;
383 uprobe
= container_of(rule
, struct lttng_event_rule_kernel_uprobe
, parent
);
385 return uprobe
->location
;
388 enum lttng_event_rule_status
lttng_event_rule_kernel_uprobe_set_event_name(
389 struct lttng_event_rule
*rule
, const char *name
)
391 char *name_copy
= NULL
;
392 struct lttng_event_rule_kernel_uprobe
*uprobe
;
393 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
395 if (!rule
|| !IS_UPROBE_EVENT_RULE(rule
) || !name
||
397 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
401 uprobe
= container_of(rule
, struct lttng_event_rule_kernel_uprobe
, parent
);
402 name_copy
= strdup(name
);
404 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
412 uprobe
->name
= name_copy
;
418 enum lttng_event_rule_status
lttng_event_rule_kernel_uprobe_get_event_name(
419 const struct lttng_event_rule
*rule
, const char **name
)
421 struct lttng_event_rule_kernel_uprobe
*uprobe
;
422 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
424 if (!rule
|| !IS_UPROBE_EVENT_RULE(rule
) || !name
) {
425 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
429 uprobe
= container_of(rule
, struct lttng_event_rule_kernel_uprobe
, parent
);
431 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
435 *name
= uprobe
->name
;