MI: implement all objects related to trigger machine interface
[lttng-tools.git] / src / common / event-rule / kernel-kprobe.c
CommitLineData
077192fd
JR
1/*
2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8#include <assert.h>
58daac01 9#include <common/credentials.h>
077192fd 10#include <common/error.h>
0f7c2963
JR
11#include <common/hashtable/hashtable.h>
12#include <common/hashtable/utils.h>
077192fd 13#include <common/macros.h>
0f7c2963 14#include <common/mi-lttng.h>
077192fd 15#include <common/payload-view.h>
0f7c2963 16#include <common/payload.h>
077192fd
JR
17#include <common/runas.h>
18#include <ctype.h>
19#include <lttng/constant.h>
20#include <lttng/event-rule/event-rule-internal.h>
0f7c2963 21#include <lttng/event-rule/event-rule.h>
d84633c4 22#include <lttng/event-rule/kernel-kprobe-internal.h>
077192fd 23#include <lttng/kernel-probe-internal.h>
0f7c2963 24#include <lttng/kernel-probe.h>
077192fd
JR
25#include <stdio.h>
26
27#define IS_KPROBE_EVENT_RULE(rule) \
d84633c4 28 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE)
077192fd
JR
29
30#if (LTTNG_SYMBOL_NAME_LEN == 256)
31#define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
32#endif
33
d84633c4 34static void lttng_event_rule_kernel_kprobe_destroy(struct lttng_event_rule *rule)
077192fd 35{
d84633c4 36 struct lttng_event_rule_kernel_kprobe *kprobe;
077192fd 37
d84633c4 38 kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent);
077192fd
JR
39
40 lttng_kernel_probe_location_destroy(kprobe->location);
41 free(kprobe->name);
42 free(kprobe);
43}
44
d84633c4 45static bool lttng_event_rule_kernel_kprobe_validate(
077192fd
JR
46 const struct lttng_event_rule *rule)
47{
48 bool valid = false;
d84633c4 49 struct lttng_event_rule_kernel_kprobe *kprobe;
077192fd
JR
50
51 if (!rule) {
52 goto end;
53 }
54
d84633c4 55 kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent);
077192fd
JR
56
57 /* Required field. */
58 if (!kprobe->name) {
59 ERR("Invalid name event rule: a name must be set.");
60 goto end;
61 }
62
63 /* Required field. */
64 if(!kprobe->location) {
65 ERR("Invalid name event rule: a location must be set.");
66 goto end;
67 }
68
69 valid = true;
70end:
71 return valid;
72}
73
d84633c4 74static int lttng_event_rule_kernel_kprobe_serialize(
077192fd
JR
75 const struct lttng_event_rule *rule,
76 struct lttng_payload *payload)
77{
78 int ret;
79 size_t name_len, header_offset, size_before_location;
d84633c4
JR
80 struct lttng_event_rule_kernel_kprobe *kprobe;
81 struct lttng_event_rule_kernel_kprobe_comm kprobe_comm;
82 struct lttng_event_rule_kernel_kprobe_comm *header;
077192fd
JR
83
84 if (!rule || !IS_KPROBE_EVENT_RULE(rule)) {
85 ret = -1;
86 goto end;
87 }
88
89 header_offset = payload->buffer.size;
90
91 DBG("Serializing kprobe event rule.");
d84633c4 92 kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent);
077192fd
JR
93
94 name_len = strlen(kprobe->name) + 1;
95 kprobe_comm.name_len = name_len;
96
97 ret = lttng_dynamic_buffer_append(
98 &payload->buffer, &kprobe_comm, sizeof(kprobe_comm));
99 if (ret) {
100 goto end;
101 }
102
103 ret = lttng_dynamic_buffer_append(&payload->buffer, kprobe->name, name_len);
104 if (ret) {
105 goto end;
106 }
107
108 size_before_location = payload->buffer.size;
109
110 ret = lttng_kernel_probe_location_serialize(kprobe->location, payload);
111 if (ret < 0) {
112 goto end;
113 }
114
115 /* Update the header regarding the probe size. */
d84633c4 116 header = (struct lttng_event_rule_kernel_kprobe_comm*) (
077192fd
JR
117 (char *) payload->buffer.data + header_offset);
118 header->location_len = payload->buffer.size - size_before_location;
119
120 ret = 0;
121
122end:
123 return ret;
124}
125
d84633c4 126static bool lttng_event_rule_kernel_kprobe_is_equal(const struct lttng_event_rule *_a,
077192fd
JR
127 const struct lttng_event_rule *_b)
128{
129 bool is_equal = false;
d84633c4 130 struct lttng_event_rule_kernel_kprobe *a, *b;
077192fd 131
d84633c4
JR
132 a = container_of(_a, struct lttng_event_rule_kernel_kprobe, parent);
133 b = container_of(_b, struct lttng_event_rule_kernel_kprobe, parent);
077192fd
JR
134
135 /* Quick checks */
136 if (!!a->name != !!b->name) {
137 goto end;
138 }
139
140 /* Long check */
141 assert(a->name);
142 assert(b->name);
143 if (strcmp(a->name, b->name)) {
144 goto end;
145 }
146
147 is_equal = lttng_kernel_probe_location_is_equal(
148 a->location, b->location);
149end:
150 return is_equal;
151}
152
d84633c4 153static enum lttng_error_code lttng_event_rule_kernel_kprobe_generate_filter_bytecode(
58daac01
JR
154 struct lttng_event_rule *rule,
155 const struct lttng_credentials *creds)
077192fd
JR
156{
157 /* Nothing to do. */
158 return LTTNG_OK;
159}
160
d84633c4 161static const char *lttng_event_rule_kernel_kprobe_get_filter(
077192fd
JR
162 const struct lttng_event_rule *rule)
163{
164 /* Not supported. */
165 return NULL;
166}
167
2b00d462 168static const struct lttng_bytecode *
d84633c4 169lttng_event_rule_kernel_kprobe_get_filter_bytecode(const struct lttng_event_rule *rule)
077192fd
JR
170{
171 /* Not supported. */
172 return NULL;
173}
174
993578ff 175static enum lttng_event_rule_generate_exclusions_status
d84633c4 176lttng_event_rule_kernel_kprobe_generate_exclusions(const struct lttng_event_rule *rule,
993578ff 177 struct lttng_event_exclusion **exclusions)
077192fd
JR
178{
179 /* Not supported. */
993578ff
JR
180 *exclusions = NULL;
181 return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE;
077192fd
JR
182}
183
959e3c66 184static unsigned long
d84633c4 185lttng_event_rule_kernel_kprobe_hash(
959e3c66
JR
186 const struct lttng_event_rule *rule)
187{
188 unsigned long hash;
d84633c4 189 struct lttng_event_rule_kernel_kprobe *krule =
959e3c66
JR
190 container_of(rule, typeof(*krule), parent);
191
d84633c4 192 hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE,
959e3c66
JR
193 lttng_ht_seed);
194 hash ^= hash_key_str(krule->name, lttng_ht_seed);
195 hash ^= lttng_kernel_probe_location_hash(krule->location);
196
197 return hash;
198}
199
602a6d40
JR
200static
201int kernel_probe_set_location(
d84633c4 202 struct lttng_event_rule_kernel_kprobe *kprobe,
602a6d40
JR
203 const struct lttng_kernel_probe_location *location)
204{
205 int ret;
206 struct lttng_kernel_probe_location *location_copy = NULL;
207
208 if (!kprobe || !location || kprobe->location) {
209 ret = -1;
210 goto end;
211 }
212
213 location_copy = lttng_kernel_probe_location_copy(location);
214 if (!location_copy) {
215 ret = -1;
216 goto end;
217 }
218
219 kprobe->location = location_copy;
220 location_copy = NULL;
221 ret = 0;
222end:
223 lttng_kernel_probe_location_destroy(location_copy);
224 return ret;
225}
226
0f7c2963
JR
227static
228enum lttng_error_code lttng_event_rule_kernel_kprobe_mi_serialize(
229 const struct lttng_event_rule *rule, struct mi_writer *writer)
230{
231 int ret;
232 enum lttng_error_code ret_code;
233 enum lttng_event_rule_status status;
234 const char *event_name = NULL;
235 const struct lttng_kernel_probe_location *location = NULL;
236
237 assert(rule);
238 assert(writer);
239 assert(IS_KPROBE_EVENT_RULE(rule));
240
241 status = lttng_event_rule_kernel_kprobe_get_event_name(
242 rule, &event_name);
243 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
244 assert(event_name);
245
246 status = lttng_event_rule_kernel_kprobe_get_location(rule, &location);
247 assert(status == LTTNG_EVENT_RULE_STATUS_OK);
248 assert(location);
249
250 /* Open event rule kernel kprobe element. */
251 ret = mi_lttng_writer_open_element(
252 writer, mi_lttng_element_event_rule_kernel_kprobe);
253 if (ret) {
254 goto mi_error;
255 }
256
257 /* Name. */
258 ret = mi_lttng_writer_write_element_string(writer,
259 mi_lttng_element_event_rule_event_name, event_name);
260 if (ret) {
261 goto mi_error;
262 }
263
264 /* Probe location. */
265 ret_code = lttng_kernel_probe_location_mi_serialize(location, writer);
266 if (ret_code != LTTNG_OK) {
267 goto end;
268 }
269
270 /* Close event rule kernel kprobe element. */
271 ret = mi_lttng_writer_close_element(writer);
272 if (ret) {
273 goto mi_error;
274 }
275
276 ret_code = LTTNG_OK;
277 goto end;
278
279mi_error:
280 ret_code = LTTNG_ERR_MI_IO_FAIL;
281end:
282 return ret_code;
283}
284
d84633c4 285struct lttng_event_rule *lttng_event_rule_kernel_kprobe_create(
602a6d40 286 const struct lttng_kernel_probe_location *location)
077192fd
JR
287{
288 struct lttng_event_rule *rule = NULL;
d84633c4 289 struct lttng_event_rule_kernel_kprobe *krule;
077192fd 290
d84633c4 291 krule = zmalloc(sizeof(struct lttng_event_rule_kernel_kprobe));
077192fd
JR
292 if (!krule) {
293 goto end;
294 }
295
296 rule = &krule->parent;
d84633c4
JR
297 lttng_event_rule_init(&krule->parent, LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE);
298 krule->parent.validate = lttng_event_rule_kernel_kprobe_validate;
299 krule->parent.serialize = lttng_event_rule_kernel_kprobe_serialize;
300 krule->parent.equal = lttng_event_rule_kernel_kprobe_is_equal;
301 krule->parent.destroy = lttng_event_rule_kernel_kprobe_destroy;
077192fd 302 krule->parent.generate_filter_bytecode =
d84633c4
JR
303 lttng_event_rule_kernel_kprobe_generate_filter_bytecode;
304 krule->parent.get_filter = lttng_event_rule_kernel_kprobe_get_filter;
077192fd 305 krule->parent.get_filter_bytecode =
d84633c4 306 lttng_event_rule_kernel_kprobe_get_filter_bytecode;
077192fd 307 krule->parent.generate_exclusions =
d84633c4
JR
308 lttng_event_rule_kernel_kprobe_generate_exclusions;
309 krule->parent.hash = lttng_event_rule_kernel_kprobe_hash;
0f7c2963 310 krule->parent.mi_serialize = lttng_event_rule_kernel_kprobe_mi_serialize;
602a6d40
JR
311
312 if (kernel_probe_set_location(krule, location)) {
313 lttng_event_rule_destroy(rule);
314 rule = NULL;
315 }
316
077192fd
JR
317end:
318 return rule;
319}
320
321LTTNG_HIDDEN
d84633c4 322ssize_t lttng_event_rule_kernel_kprobe_create_from_payload(
077192fd
JR
323 struct lttng_payload_view *view,
324 struct lttng_event_rule **_event_rule)
325{
326 ssize_t ret, offset = 0;
327 enum lttng_event_rule_status status;
d84633c4 328 const struct lttng_event_rule_kernel_kprobe_comm *kprobe_comm;
077192fd
JR
329 const char *name;
330 struct lttng_buffer_view current_buffer_view;
331 struct lttng_event_rule *rule = NULL;
602a6d40 332 struct lttng_kernel_probe_location *location = NULL;
077192fd
JR
333
334 if (!_event_rule) {
335 ret = -1;
336 goto end;
337 }
338
3e6e0df2
JG
339 current_buffer_view = lttng_buffer_view_from_view(
340 &view->buffer, offset, sizeof(*kprobe_comm));
341 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
077192fd
JR
342 ERR("Failed to initialize from malformed event rule kprobe: buffer too short to contain header.");
343 ret = -1;
344 goto end;
345 }
346
077192fd 347 kprobe_comm = (typeof(kprobe_comm)) current_buffer_view.data;
077192fd 348
077192fd
JR
349 /* Skip to payload */
350 offset += current_buffer_view.size;
351
352 {
353 /* Map the name. */
354 struct lttng_payload_view current_payload_view =
355 lttng_payload_view_from_view(view, offset,
356 kprobe_comm->name_len);
357
3e6e0df2 358 if (!lttng_payload_view_is_valid(&current_payload_view)) {
077192fd
JR
359 ret = -1;
360 goto end;
361 }
362
3e6e0df2 363 name = current_payload_view.buffer.data;
077192fd
JR
364 if (!lttng_buffer_view_contains_string(
365 &current_payload_view.buffer, name,
366 kprobe_comm->name_len)) {
367 ret = -1;
368 goto end;
369 }
370 }
371
372 /* Skip after the name. */
373 offset += kprobe_comm->name_len;
374
375 /* Map the kernel probe location. */
376 {
377 struct lttng_payload_view current_payload_view =
378 lttng_payload_view_from_view(view, offset,
379 kprobe_comm->location_len);
380
3e6e0df2
JG
381 if (!lttng_payload_view_is_valid(&current_payload_view)) {
382 ret = -1;
383 goto end;
384 }
385
077192fd
JR
386 ret = lttng_kernel_probe_location_create_from_payload(
387 &current_payload_view, &location);
388 if (ret < 0) {
389 ret = -1;
390 goto end;
391 }
392 }
393
394 if (ret != kprobe_comm->location_len) {
395 ret = -1;
396 goto end;
397 }
398
077192fd
JR
399 /* Skip after the location */
400 offset += kprobe_comm->location_len;
401
d84633c4 402 rule = lttng_event_rule_kernel_kprobe_create(location);
602a6d40
JR
403 if (!rule) {
404 ERR("Failed to create event rule kprobe.");
405 ret = -1;
406 goto end;
407 }
408
d84633c4 409 status = lttng_event_rule_kernel_kprobe_set_event_name(rule, name);
077192fd
JR
410 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
411 ERR("Failed to set event rule kprobe name.");
412 ret = -1;
413 goto end;
414 }
415
416 *_event_rule = rule;
417 rule = NULL;
418 ret = offset;
419end:
602a6d40 420 lttng_kernel_probe_location_destroy(location);
077192fd
JR
421 lttng_event_rule_destroy(rule);
422 return ret;
423}
424
d84633c4 425enum lttng_event_rule_status lttng_event_rule_kernel_kprobe_get_location(
077192fd
JR
426 const struct lttng_event_rule *rule,
427 const struct lttng_kernel_probe_location **location)
428{
429 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
d84633c4 430 struct lttng_event_rule_kernel_kprobe *kprobe;
077192fd
JR
431
432 if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !location) {
433 status = LTTNG_EVENT_RULE_STATUS_INVALID;
434 goto end;
435 }
436
d84633c4 437 kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent);
077192fd
JR
438 *location = kprobe->location;
439
440 if (!*location) {
441 status = LTTNG_EVENT_RULE_STATUS_UNSET;
442 goto end;
443 }
444
445end:
446 return status;
447}
448
d84633c4 449enum lttng_event_rule_status lttng_event_rule_kernel_kprobe_set_event_name(
077192fd
JR
450 struct lttng_event_rule *rule, const char *name)
451{
452 char *name_copy = NULL;
d84633c4 453 struct lttng_event_rule_kernel_kprobe *kprobe;
077192fd
JR
454 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
455
456 if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name ||
457 strlen(name) == 0) {
458 status = LTTNG_EVENT_RULE_STATUS_INVALID;
459 goto end;
460 }
461
d84633c4 462 kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent);
077192fd
JR
463 name_copy = strdup(name);
464 if (!name_copy) {
465 status = LTTNG_EVENT_RULE_STATUS_ERROR;
466 goto end;
467 }
468
469 free(kprobe->name);
470
471 kprobe->name = name_copy;
472 name_copy = NULL;
473end:
474 return status;
475}
476
d84633c4 477enum lttng_event_rule_status lttng_event_rule_kernel_kprobe_get_event_name(
077192fd
JR
478 const struct lttng_event_rule *rule, const char **name)
479{
d84633c4 480 struct lttng_event_rule_kernel_kprobe *kprobe;
077192fd
JR
481 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
482
483 if (!rule || !IS_KPROBE_EVENT_RULE(rule) || !name) {
484 status = LTTNG_EVENT_RULE_STATUS_INVALID;
485 goto end;
486 }
487
d84633c4 488 kprobe = container_of(rule, struct lttng_event_rule_kernel_kprobe, parent);
077192fd
JR
489 if (!kprobe->name) {
490 status = LTTNG_EVENT_RULE_STATUS_UNSET;
491 goto end;
492 }
493
494 *name = kprobe->name;
495end:
496 return status;
497}
This page took 0.050659 seconds and 4 git commands to generate.