Add base support for event rule hit
[lttng-tools.git] / src / common / event-rule / uprobe.c
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>
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/uprobe-internal.h>
19 #include <lttng/userspace-probe-internal.h>
20
21 #define IS_UPROBE_EVENT_RULE(rule) \
22 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_UPROBE)
23
24 static void lttng_event_rule_uprobe_destroy(struct lttng_event_rule *rule)
25 {
26 struct lttng_event_rule_uprobe *uprobe;
27
28 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
29
30 lttng_userspace_probe_location_destroy(uprobe->location);
31 free(uprobe->name);
32 free(uprobe);
33 }
34
35 static bool lttng_event_rule_uprobe_validate(
36 const struct lttng_event_rule *rule)
37 {
38 bool valid = false;
39 struct lttng_event_rule_uprobe *uprobe;
40
41 if (!rule) {
42 goto end;
43 }
44
45 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
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;
59 end:
60 return valid;
61 }
62
63 static int lttng_event_rule_uprobe_serialize(
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;
69 struct lttng_event_rule_uprobe *uprobe;
70 struct lttng_event_rule_uprobe_comm uprobe_comm = {};
71 struct lttng_event_rule_uprobe_comm *header;
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.");
81 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
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. */
108 header = (struct lttng_event_rule_uprobe_comm
109 *) ((char *) payload->buffer.data +
110 header_offset);
111 header->location_len = payload->buffer.size - size_before_probe;
112
113 ret = 0;
114
115 end:
116 return ret;
117 }
118
119 static bool lttng_event_rule_uprobe_is_equal(const struct lttng_event_rule *_a,
120 const struct lttng_event_rule *_b)
121 {
122 bool is_equal = false;
123 struct lttng_event_rule_uprobe *a, *b;
124
125 a = container_of(_a, struct lttng_event_rule_uprobe, parent);
126 b = container_of(_b, struct lttng_event_rule_uprobe, parent);
127
128 /* uprobe is invalid if this is not true. */
129 assert(a->name);
130 assert(b->name);
131 if (strcmp(a->name, b->name)) {
132 goto end;
133 }
134
135 assert(a->location);
136 assert(b->location);
137 is_equal = lttng_userspace_probe_location_is_equal(
138 a->location, b->location);
139 end:
140 return is_equal;
141 }
142
143 static enum lttng_error_code lttng_event_rule_uprobe_generate_filter_bytecode(
144 struct lttng_event_rule *rule,
145 const struct lttng_credentials *creds)
146 {
147 /* Nothing to do. */
148 return LTTNG_OK;
149 }
150
151 static const char *lttng_event_rule_uprobe_get_filter(
152 const struct lttng_event_rule *rule)
153 {
154 /* Unsupported. */
155 return NULL;
156 }
157
158 static const struct lttng_filter_bytecode *
159 lttng_event_rule_uprobe_get_filter_bytecode(const struct lttng_event_rule *rule)
160 {
161 /* Unsupported. */
162 return NULL;
163 }
164
165 static struct lttng_event_exclusion *
166 lttng_event_rule_uprobe_generate_exclusions(const struct lttng_event_rule *rule)
167 {
168 /* Unsupported. */
169 return NULL;
170 }
171
172 static unsigned long
173 lttng_event_rule_uprobe_hash(
174 const struct lttng_event_rule *rule)
175 {
176 unsigned long hash;
177 struct lttng_event_rule_uprobe *urule =
178 container_of(rule, typeof(*urule), parent);
179
180 hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_UPROBE,
181 lttng_ht_seed);
182 hash ^= hash_key_str(urule->name, lttng_ht_seed);
183 hash ^= lttng_userspace_probe_location_hash(urule->location);
184
185 return hash;
186 }
187
188 struct lttng_event_rule *lttng_event_rule_uprobe_create()
189 {
190 struct lttng_event_rule *rule = NULL;
191 struct lttng_event_rule_uprobe *urule;
192
193 urule = zmalloc(sizeof(struct lttng_event_rule_uprobe));
194 if (!urule) {
195 goto end;
196 }
197
198 rule = &urule->parent;
199 lttng_event_rule_init(&urule->parent, LTTNG_EVENT_RULE_TYPE_UPROBE);
200 urule->parent.validate = lttng_event_rule_uprobe_validate;
201 urule->parent.serialize = lttng_event_rule_uprobe_serialize;
202 urule->parent.equal = lttng_event_rule_uprobe_is_equal;
203 urule->parent.destroy = lttng_event_rule_uprobe_destroy;
204 urule->parent.generate_filter_bytecode =
205 lttng_event_rule_uprobe_generate_filter_bytecode;
206 urule->parent.get_filter = lttng_event_rule_uprobe_get_filter;
207 urule->parent.get_filter_bytecode =
208 lttng_event_rule_uprobe_get_filter_bytecode;
209 urule->parent.generate_exclusions =
210 lttng_event_rule_uprobe_generate_exclusions;
211 urule->parent.hash = lttng_event_rule_uprobe_hash;
212 end:
213 return rule;
214 }
215
216 LTTNG_HIDDEN
217 ssize_t lttng_event_rule_uprobe_create_from_payload(
218 struct lttng_payload_view *view,
219 struct lttng_event_rule **_event_rule)
220 {
221 ssize_t ret, offset = 0;
222 const struct lttng_event_rule_uprobe_comm *uprobe_comm;
223 const char *name;
224 struct lttng_buffer_view current_buffer_view;
225 struct lttng_event_rule *rule = NULL;
226 struct lttng_userspace_probe_location *location;
227 struct lttng_event_rule_uprobe *uprobe;
228 enum lttng_event_rule_status status;
229
230 if (!_event_rule) {
231 ret = -1;
232 goto end;
233 }
234
235 current_buffer_view = lttng_buffer_view_from_view(
236 &view->buffer, offset, sizeof(*uprobe_comm));
237 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
238 ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain header");
239 ret = -1;
240 goto end;
241 }
242
243 uprobe_comm = (typeof(uprobe_comm)) current_buffer_view.data;
244
245 rule = lttng_event_rule_uprobe_create();
246 if (!rule) {
247 ERR("Failed to create event rule uprobe");
248 ret = -1;
249 goto end;
250 }
251
252 /* Skip to payload. */
253 offset += current_buffer_view.size;
254
255 /* Map the name. */
256 current_buffer_view = lttng_buffer_view_from_view(
257 &view->buffer, offset, uprobe_comm->name_len);
258 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
259 ret = -1;
260 goto end;
261 }
262
263 name = current_buffer_view.data;
264 if (!lttng_buffer_view_contains_string(&current_buffer_view, name,
265 uprobe_comm->name_len)) {
266 ret = -1;
267 goto end;
268 }
269
270 /* Skip after the name. */
271 offset += uprobe_comm->name_len;
272
273 /* Map the location. */
274 {
275 struct lttng_payload_view current_payload_view =
276 lttng_payload_view_from_view(view, offset,
277 uprobe_comm->location_len);
278
279 if (!lttng_payload_view_is_valid(&current_payload_view)) {
280 ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain location");
281 ret = -1;
282 goto end;
283 }
284
285 ret = lttng_userspace_probe_location_create_from_payload(
286 &current_payload_view, &location);
287 if (ret < 0) {
288 ret = -1;
289 goto end;
290 }
291 }
292
293 assert(ret == uprobe_comm->location_len);
294
295 /* Skip after the location. */
296 offset += uprobe_comm->location_len;
297
298 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
299 uprobe->location = location;
300
301 status = lttng_event_rule_uprobe_set_name(rule, name);
302 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
303 ret = -1;
304 goto end;
305 }
306
307 if (!lttng_event_rule_uprobe_validate(rule)) {
308 ret = -1;
309 goto end;
310 }
311
312 *_event_rule = rule;
313 rule = NULL;
314 ret = offset;
315 end:
316 lttng_event_rule_destroy(rule);
317 return ret;
318 }
319
320 enum lttng_event_rule_status lttng_event_rule_uprobe_set_location(
321 struct lttng_event_rule *rule,
322 const struct lttng_userspace_probe_location *location)
323 {
324 struct lttng_userspace_probe_location *location_copy = NULL;
325 struct lttng_event_rule_uprobe *uprobe;
326 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
327
328 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !location) {
329 status = LTTNG_EVENT_RULE_STATUS_INVALID;
330 goto end;
331 }
332
333 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
334 location_copy = lttng_userspace_probe_location_copy(location);
335 if (!location_copy) {
336 status = LTTNG_EVENT_RULE_STATUS_ERROR;
337 goto end;
338 }
339
340 if (uprobe->location) {
341 lttng_userspace_probe_location_destroy(uprobe->location);
342 }
343
344 uprobe->location = location_copy;
345 location_copy = NULL;
346 end:
347 lttng_userspace_probe_location_destroy(location_copy);
348 return status;
349 }
350
351 enum lttng_event_rule_status lttng_event_rule_uprobe_get_location(
352 const struct lttng_event_rule *rule,
353 const struct lttng_userspace_probe_location **location)
354 {
355 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
356
357 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !location) {
358 status = LTTNG_EVENT_RULE_STATUS_INVALID;
359 goto end;
360 }
361
362 *location = lttng_event_rule_uprobe_get_location_mutable(rule);
363 if (!*location) {
364 status = LTTNG_EVENT_RULE_STATUS_UNSET;
365 goto end;
366 }
367
368 end:
369 return status;
370 }
371
372 LTTNG_HIDDEN
373 struct lttng_userspace_probe_location *
374 lttng_event_rule_uprobe_get_location_mutable(
375 const struct lttng_event_rule *rule)
376 {
377 struct lttng_event_rule_uprobe *uprobe;
378
379 assert(rule);
380 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
381
382 return uprobe->location;
383 }
384
385 enum lttng_event_rule_status lttng_event_rule_uprobe_set_name(
386 struct lttng_event_rule *rule, const char *name)
387 {
388 char *name_copy = NULL;
389 struct lttng_event_rule_uprobe *uprobe;
390 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
391
392 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !name ||
393 strlen(name) == 0) {
394 status = LTTNG_EVENT_RULE_STATUS_INVALID;
395 goto end;
396 }
397
398 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
399 name_copy = strdup(name);
400 if (!name_copy) {
401 status = LTTNG_EVENT_RULE_STATUS_ERROR;
402 goto end;
403 }
404
405 if (uprobe->name) {
406 free(uprobe->name);
407 }
408
409 uprobe->name = name_copy;
410 name_copy = NULL;
411 end:
412 return status;
413 }
414
415 enum lttng_event_rule_status lttng_event_rule_uprobe_get_name(
416 const struct lttng_event_rule *rule, const char **name)
417 {
418 struct lttng_event_rule_uprobe *uprobe;
419 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
420
421 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !name) {
422 status = LTTNG_EVENT_RULE_STATUS_INVALID;
423 goto end;
424 }
425
426 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
427 if (!uprobe->name) {
428 status = LTTNG_EVENT_RULE_STATUS_UNSET;
429 goto end;
430 }
431
432 *name = uprobe->name;
433 end:
434 return status;
435 }
This page took 0.056602 seconds and 4 git commands to generate.