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