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>
18 #include <lttng/constant.h>
19 #include <lttng/event-rule/event-rule-internal.h>
20 #include <lttng/event-rule/kprobe-internal.h>
21 #include <lttng/kernel-probe.h>
22 #include <lttng/kernel-probe-internal.h>
25 #define IS_KPROBE_EVENT_RULE(rule) \
26 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KPROBE)
28 #if (LTTNG_SYMBOL_NAME_LEN == 256)
29 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
32 static void lttng_event_rule_kprobe_destroy(struct lttng_event_rule
*rule
)
34 struct lttng_event_rule_kprobe
*kprobe
;
36 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
38 lttng_kernel_probe_location_destroy(kprobe
->location
);
43 static bool lttng_event_rule_kprobe_validate(
44 const struct lttng_event_rule
*rule
)
47 struct lttng_event_rule_kprobe
*kprobe
;
53 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
57 ERR("Invalid name event rule: a name must be set.");
62 if(!kprobe
->location
) {
63 ERR("Invalid name event rule: a location must be set.");
72 static int lttng_event_rule_kprobe_serialize(
73 const struct lttng_event_rule
*rule
,
74 struct lttng_payload
*payload
)
77 size_t name_len
, header_offset
, size_before_location
;
78 struct lttng_event_rule_kprobe
*kprobe
;
79 struct lttng_event_rule_kprobe_comm kprobe_comm
;
80 struct lttng_event_rule_kprobe_comm
*header
;
82 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
)) {
87 header_offset
= payload
->buffer
.size
;
89 DBG("Serializing kprobe event rule.");
90 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
92 name_len
= strlen(kprobe
->name
) + 1;
93 kprobe_comm
.name_len
= name_len
;
95 ret
= lttng_dynamic_buffer_append(
96 &payload
->buffer
, &kprobe_comm
, sizeof(kprobe_comm
));
101 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, kprobe
->name
, name_len
);
106 size_before_location
= payload
->buffer
.size
;
108 ret
= lttng_kernel_probe_location_serialize(kprobe
->location
, payload
);
113 /* Update the header regarding the probe size. */
114 header
= (struct lttng_event_rule_kprobe_comm
*) (
115 (char *) payload
->buffer
.data
+ header_offset
);
116 header
->location_len
= payload
->buffer
.size
- size_before_location
;
124 static bool lttng_event_rule_kprobe_is_equal(const struct lttng_event_rule
*_a
,
125 const struct lttng_event_rule
*_b
)
127 bool is_equal
= false;
128 struct lttng_event_rule_kprobe
*a
, *b
;
130 a
= container_of(_a
, struct lttng_event_rule_kprobe
, parent
);
131 b
= container_of(_b
, struct lttng_event_rule_kprobe
, parent
);
134 if (!!a
->name
!= !!b
->name
) {
141 if (strcmp(a
->name
, b
->name
)) {
145 is_equal
= lttng_kernel_probe_location_is_equal(
146 a
->location
, b
->location
);
151 static enum lttng_error_code
lttng_event_rule_kprobe_generate_filter_bytecode(
152 struct lttng_event_rule
*rule
,
153 const struct lttng_credentials
*creds
)
159 static const char *lttng_event_rule_kprobe_get_filter(
160 const struct lttng_event_rule
*rule
)
166 static const struct lttng_filter_bytecode
*
167 lttng_event_rule_kprobe_get_filter_bytecode(const struct lttng_event_rule
*rule
)
173 static struct lttng_event_exclusion
*
174 lttng_event_rule_kprobe_generate_exclusions(const struct lttng_event_rule
*rule
)
181 lttng_event_rule_kprobe_hash(
182 const struct lttng_event_rule
*rule
)
185 struct lttng_event_rule_kprobe
*krule
=
186 container_of(rule
, typeof(*krule
), parent
);
188 hash
= hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KPROBE
,
190 hash
^= hash_key_str(krule
->name
, lttng_ht_seed
);
191 hash
^= lttng_kernel_probe_location_hash(krule
->location
);
196 struct lttng_event_rule
*lttng_event_rule_kprobe_create()
198 struct lttng_event_rule
*rule
= NULL
;
199 struct lttng_event_rule_kprobe
*krule
;
201 krule
= zmalloc(sizeof(struct lttng_event_rule_kprobe
));
206 rule
= &krule
->parent
;
207 lttng_event_rule_init(&krule
->parent
, LTTNG_EVENT_RULE_TYPE_KPROBE
);
208 krule
->parent
.validate
= lttng_event_rule_kprobe_validate
;
209 krule
->parent
.serialize
= lttng_event_rule_kprobe_serialize
;
210 krule
->parent
.equal
= lttng_event_rule_kprobe_is_equal
;
211 krule
->parent
.destroy
= lttng_event_rule_kprobe_destroy
;
212 krule
->parent
.generate_filter_bytecode
=
213 lttng_event_rule_kprobe_generate_filter_bytecode
;
214 krule
->parent
.get_filter
= lttng_event_rule_kprobe_get_filter
;
215 krule
->parent
.get_filter_bytecode
=
216 lttng_event_rule_kprobe_get_filter_bytecode
;
217 krule
->parent
.generate_exclusions
=
218 lttng_event_rule_kprobe_generate_exclusions
;
219 krule
->parent
.hash
= lttng_event_rule_kprobe_hash
;
225 ssize_t
lttng_event_rule_kprobe_create_from_payload(
226 struct lttng_payload_view
*view
,
227 struct lttng_event_rule
**_event_rule
)
229 ssize_t ret
, offset
= 0;
230 enum lttng_event_rule_status status
;
231 const struct lttng_event_rule_kprobe_comm
*kprobe_comm
;
233 struct lttng_buffer_view current_buffer_view
;
234 struct lttng_event_rule
*rule
= NULL
;
235 struct lttng_event_rule_kprobe
*kprobe
= NULL
;
236 struct lttng_kernel_probe_location
*location
;
243 current_buffer_view
= lttng_buffer_view_from_view(
244 &view
->buffer
, offset
, sizeof(*kprobe_comm
));
245 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
246 ERR("Failed to initialize from malformed event rule kprobe: buffer too short to contain header.");
251 kprobe_comm
= (typeof(kprobe_comm
)) current_buffer_view
.data
;
253 rule
= lttng_event_rule_kprobe_create();
255 ERR("Failed to create event rule kprobe.");
260 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
262 /* Skip to payload */
263 offset
+= current_buffer_view
.size
;
267 struct lttng_payload_view current_payload_view
=
268 lttng_payload_view_from_view(view
, offset
,
269 kprobe_comm
->name_len
);
271 if (!lttng_payload_view_is_valid(¤t_payload_view
)) {
276 name
= current_payload_view
.buffer
.data
;
277 if (!lttng_buffer_view_contains_string(
278 ¤t_payload_view
.buffer
, name
,
279 kprobe_comm
->name_len
)) {
285 /* Skip after the name. */
286 offset
+= kprobe_comm
->name_len
;
288 /* Map the kernel probe location. */
290 struct lttng_payload_view current_payload_view
=
291 lttng_payload_view_from_view(view
, offset
,
292 kprobe_comm
->location_len
);
294 if (!lttng_payload_view_is_valid(¤t_payload_view
)) {
299 ret
= lttng_kernel_probe_location_create_from_payload(
300 ¤t_payload_view
, &location
);
307 if (ret
!= kprobe_comm
->location_len
) {
312 kprobe
->location
= location
;
314 /* Skip after the location */
315 offset
+= kprobe_comm
->location_len
;
317 status
= lttng_event_rule_kprobe_set_name(rule
, name
);
318 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
319 ERR("Failed to set event rule kprobe name.");
328 lttng_event_rule_destroy(rule
);
332 enum lttng_event_rule_status
lttng_event_rule_kprobe_set_location(
333 struct lttng_event_rule
*rule
,
334 const struct lttng_kernel_probe_location
*location
)
336 struct lttng_kernel_probe_location
*location_copy
= NULL
;
337 struct lttng_event_rule_kprobe
*kprobe
;
338 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
340 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !location
) {
341 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
345 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
346 location_copy
= lttng_kernel_probe_location_copy(location
);
347 if (!location_copy
) {
348 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
352 if (kprobe
->location
) {
353 lttng_kernel_probe_location_destroy(kprobe
->location
);
356 kprobe
->location
= location_copy
;
357 location_copy
= NULL
;
359 lttng_kernel_probe_location_destroy(location_copy
);
363 enum lttng_event_rule_status
lttng_event_rule_kprobe_get_location(
364 const struct lttng_event_rule
*rule
,
365 const struct lttng_kernel_probe_location
**location
)
367 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
368 struct lttng_event_rule_kprobe
*kprobe
;
370 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !location
) {
371 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
375 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
376 *location
= kprobe
->location
;
379 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
387 enum lttng_event_rule_status
lttng_event_rule_kprobe_set_name(
388 struct lttng_event_rule
*rule
, const char *name
)
390 char *name_copy
= NULL
;
391 struct lttng_event_rule_kprobe
*kprobe
;
392 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
394 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !name
||
396 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
400 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
401 name_copy
= strdup(name
);
403 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
409 kprobe
->name
= name_copy
;
415 enum lttng_event_rule_status
lttng_event_rule_kprobe_get_name(
416 const struct lttng_event_rule
*rule
, const char **name
)
418 struct lttng_event_rule_kprobe
*kprobe
;
419 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
421 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !name
) {
422 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
426 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
428 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
432 *name
= kprobe
->name
;