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>
16 #include <lttng/constant.h>
17 #include <lttng/event-rule/event-rule-internal.h>
18 #include <lttng/event-rule/kprobe-internal.h>
19 #include <lttng/kernel-probe.h>
20 #include <lttng/kernel-probe-internal.h>
23 #define IS_KPROBE_EVENT_RULE(rule) \
24 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KPROBE)
26 #if (LTTNG_SYMBOL_NAME_LEN == 256)
27 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
30 static void lttng_event_rule_kprobe_destroy(struct lttng_event_rule
*rule
)
32 struct lttng_event_rule_kprobe
*kprobe
;
34 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
36 lttng_kernel_probe_location_destroy(kprobe
->location
);
41 static bool lttng_event_rule_kprobe_validate(
42 const struct lttng_event_rule
*rule
)
45 struct lttng_event_rule_kprobe
*kprobe
;
51 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
55 ERR("Invalid name event rule: a name must be set.");
60 if(!kprobe
->location
) {
61 ERR("Invalid name event rule: a location must be set.");
70 static int lttng_event_rule_kprobe_serialize(
71 const struct lttng_event_rule
*rule
,
72 struct lttng_payload
*payload
)
75 size_t name_len
, header_offset
, size_before_location
;
76 struct lttng_event_rule_kprobe
*kprobe
;
77 struct lttng_event_rule_kprobe_comm kprobe_comm
;
78 struct lttng_event_rule_kprobe_comm
*header
;
80 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
)) {
85 header_offset
= payload
->buffer
.size
;
87 DBG("Serializing kprobe event rule.");
88 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
90 name_len
= strlen(kprobe
->name
) + 1;
91 kprobe_comm
.name_len
= name_len
;
93 ret
= lttng_dynamic_buffer_append(
94 &payload
->buffer
, &kprobe_comm
, sizeof(kprobe_comm
));
99 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, kprobe
->name
, name_len
);
104 size_before_location
= payload
->buffer
.size
;
106 ret
= lttng_kernel_probe_location_serialize(kprobe
->location
, payload
);
111 /* Update the header regarding the probe size. */
112 header
= (struct lttng_event_rule_kprobe_comm
*) (
113 (char *) payload
->buffer
.data
+ header_offset
);
114 header
->location_len
= payload
->buffer
.size
- size_before_location
;
122 static bool lttng_event_rule_kprobe_is_equal(const struct lttng_event_rule
*_a
,
123 const struct lttng_event_rule
*_b
)
125 bool is_equal
= false;
126 struct lttng_event_rule_kprobe
*a
, *b
;
128 a
= container_of(_a
, struct lttng_event_rule_kprobe
, parent
);
129 b
= container_of(_b
, struct lttng_event_rule_kprobe
, parent
);
132 if (!!a
->name
!= !!b
->name
) {
139 if (strcmp(a
->name
, b
->name
)) {
143 is_equal
= lttng_kernel_probe_location_is_equal(
144 a
->location
, b
->location
);
149 static enum lttng_error_code
lttng_event_rule_kprobe_generate_filter_bytecode(
150 struct lttng_event_rule
*rule
,
151 const struct lttng_credentials
*creds
)
157 static const char *lttng_event_rule_kprobe_get_filter(
158 const struct lttng_event_rule
*rule
)
164 static const struct lttng_filter_bytecode
*
165 lttng_event_rule_kprobe_get_filter_bytecode(const struct lttng_event_rule
*rule
)
171 static struct lttng_event_exclusion
*
172 lttng_event_rule_kprobe_generate_exclusions(const struct lttng_event_rule
*rule
)
178 struct lttng_event_rule
*lttng_event_rule_kprobe_create()
180 struct lttng_event_rule
*rule
= NULL
;
181 struct lttng_event_rule_kprobe
*krule
;
183 krule
= zmalloc(sizeof(struct lttng_event_rule_kprobe
));
188 rule
= &krule
->parent
;
189 lttng_event_rule_init(&krule
->parent
, LTTNG_EVENT_RULE_TYPE_KPROBE
);
190 krule
->parent
.validate
= lttng_event_rule_kprobe_validate
;
191 krule
->parent
.serialize
= lttng_event_rule_kprobe_serialize
;
192 krule
->parent
.equal
= lttng_event_rule_kprobe_is_equal
;
193 krule
->parent
.destroy
= lttng_event_rule_kprobe_destroy
;
194 krule
->parent
.generate_filter_bytecode
=
195 lttng_event_rule_kprobe_generate_filter_bytecode
;
196 krule
->parent
.get_filter
= lttng_event_rule_kprobe_get_filter
;
197 krule
->parent
.get_filter_bytecode
=
198 lttng_event_rule_kprobe_get_filter_bytecode
;
199 krule
->parent
.generate_exclusions
=
200 lttng_event_rule_kprobe_generate_exclusions
;
206 ssize_t
lttng_event_rule_kprobe_create_from_payload(
207 struct lttng_payload_view
*view
,
208 struct lttng_event_rule
**_event_rule
)
210 ssize_t ret
, offset
= 0;
211 enum lttng_event_rule_status status
;
212 const struct lttng_event_rule_kprobe_comm
*kprobe_comm
;
214 struct lttng_buffer_view current_buffer_view
;
215 struct lttng_event_rule
*rule
= NULL
;
216 struct lttng_event_rule_kprobe
*kprobe
= NULL
;
217 struct lttng_kernel_probe_location
*location
;
224 current_buffer_view
= lttng_buffer_view_from_view(
225 &view
->buffer
, offset
, sizeof(*kprobe_comm
));
226 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
227 ERR("Failed to initialize from malformed event rule kprobe: buffer too short to contain header.");
232 kprobe_comm
= (typeof(kprobe_comm
)) current_buffer_view
.data
;
234 rule
= lttng_event_rule_kprobe_create();
236 ERR("Failed to create event rule kprobe.");
241 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
243 /* Skip to payload */
244 offset
+= current_buffer_view
.size
;
248 struct lttng_payload_view current_payload_view
=
249 lttng_payload_view_from_view(view
, offset
,
250 kprobe_comm
->name_len
);
252 if (!lttng_payload_view_is_valid(¤t_payload_view
)) {
257 name
= current_payload_view
.buffer
.data
;
258 if (!lttng_buffer_view_contains_string(
259 ¤t_payload_view
.buffer
, name
,
260 kprobe_comm
->name_len
)) {
266 /* Skip after the name. */
267 offset
+= kprobe_comm
->name_len
;
269 /* Map the kernel probe location. */
271 struct lttng_payload_view current_payload_view
=
272 lttng_payload_view_from_view(view
, offset
,
273 kprobe_comm
->location_len
);
275 if (!lttng_payload_view_is_valid(¤t_payload_view
)) {
280 ret
= lttng_kernel_probe_location_create_from_payload(
281 ¤t_payload_view
, &location
);
288 if (ret
!= kprobe_comm
->location_len
) {
293 kprobe
->location
= location
;
295 /* Skip after the location */
296 offset
+= kprobe_comm
->location_len
;
298 status
= lttng_event_rule_kprobe_set_name(rule
, name
);
299 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
300 ERR("Failed to set event rule kprobe name.");
309 lttng_event_rule_destroy(rule
);
313 enum lttng_event_rule_status
lttng_event_rule_kprobe_set_location(
314 struct lttng_event_rule
*rule
,
315 const struct lttng_kernel_probe_location
*location
)
317 struct lttng_kernel_probe_location
*location_copy
= NULL
;
318 struct lttng_event_rule_kprobe
*kprobe
;
319 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
321 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !location
) {
322 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
326 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
327 location_copy
= lttng_kernel_probe_location_copy(location
);
328 if (!location_copy
) {
329 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
333 if (kprobe
->location
) {
334 lttng_kernel_probe_location_destroy(kprobe
->location
);
337 kprobe
->location
= location_copy
;
338 location_copy
= NULL
;
340 lttng_kernel_probe_location_destroy(location_copy
);
344 enum lttng_event_rule_status
lttng_event_rule_kprobe_get_location(
345 const struct lttng_event_rule
*rule
,
346 const struct lttng_kernel_probe_location
**location
)
348 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
349 struct lttng_event_rule_kprobe
*kprobe
;
351 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !location
) {
352 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
356 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
357 *location
= kprobe
->location
;
360 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
368 enum lttng_event_rule_status
lttng_event_rule_kprobe_set_name(
369 struct lttng_event_rule
*rule
, const char *name
)
371 char *name_copy
= NULL
;
372 struct lttng_event_rule_kprobe
*kprobe
;
373 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
375 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !name
||
377 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
381 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
382 name_copy
= strdup(name
);
384 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
390 kprobe
->name
= name_copy
;
396 enum lttng_event_rule_status
lttng_event_rule_kprobe_get_name(
397 const struct lttng_event_rule
*rule
, const char **name
)
399 struct lttng_event_rule_kprobe
*kprobe
;
400 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
402 if (!rule
|| !IS_KPROBE_EVENT_RULE(rule
) || !name
) {
403 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
407 kprobe
= container_of(rule
, struct lttng_event_rule_kprobe
, parent
);
409 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
413 *name
= kprobe
->name
;