event-rule: userspace probe: rename get/set_name to get/set_event_name
[lttng-tools.git] / src / common / event-rule / userspace-probe.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/userspace-probe-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_USERSPACE_PROBE)
23
24 static void lttng_event_rule_userspace_probe_destroy(struct lttng_event_rule *rule)
25 {
26 struct lttng_event_rule_userspace_probe *uprobe;
27
28 uprobe = container_of(rule, struct lttng_event_rule_userspace_probe, 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_userspace_probe_validate(
36 const struct lttng_event_rule *rule)
37 {
38 bool valid = false;
39 struct lttng_event_rule_userspace_probe *uprobe;
40
41 if (!rule) {
42 goto end;
43 }
44
45 uprobe = container_of(rule, struct lttng_event_rule_userspace_probe, 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_userspace_probe_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_userspace_probe *uprobe;
70 struct lttng_event_rule_userspace_probe_comm uprobe_comm = {};
71 struct lttng_event_rule_userspace_probe_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_userspace_probe, 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_userspace_probe_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_userspace_probe_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_userspace_probe *a, *b;
124
125 a = container_of(_a, struct lttng_event_rule_userspace_probe, parent);
126 b = container_of(_b, struct lttng_event_rule_userspace_probe, 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_userspace_probe_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_userspace_probe_get_filter(
152 const struct lttng_event_rule *rule)
153 {
154 /* Unsupported. */
155 return NULL;
156 }
157
158 static const struct lttng_bytecode *
159 lttng_event_rule_userspace_probe_get_filter_bytecode(const struct lttng_event_rule *rule)
160 {
161 /* Unsupported. */
162 return NULL;
163 }
164
165 static enum lttng_event_rule_generate_exclusions_status
166 lttng_event_rule_userspace_probe_generate_exclusions(const struct lttng_event_rule *rule,
167 struct lttng_event_exclusion **exclusions)
168 {
169 /* Unsupported. */
170 *exclusions = NULL;
171 return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE;
172 }
173
174 static unsigned long
175 lttng_event_rule_userspace_probe_hash(
176 const struct lttng_event_rule *rule)
177 {
178 unsigned long hash;
179 struct lttng_event_rule_userspace_probe *urule =
180 container_of(rule, typeof(*urule), parent);
181
182 hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE,
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
190 struct lttng_event_rule *lttng_event_rule_userspace_probe_create(void)
191 {
192 struct lttng_event_rule *rule = NULL;
193 struct lttng_event_rule_userspace_probe *urule;
194
195 urule = zmalloc(sizeof(struct lttng_event_rule_userspace_probe));
196 if (!urule) {
197 goto end;
198 }
199
200 rule = &urule->parent;
201 lttng_event_rule_init(&urule->parent, LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE);
202 urule->parent.validate = lttng_event_rule_userspace_probe_validate;
203 urule->parent.serialize = lttng_event_rule_userspace_probe_serialize;
204 urule->parent.equal = lttng_event_rule_userspace_probe_is_equal;
205 urule->parent.destroy = lttng_event_rule_userspace_probe_destroy;
206 urule->parent.generate_filter_bytecode =
207 lttng_event_rule_userspace_probe_generate_filter_bytecode;
208 urule->parent.get_filter = lttng_event_rule_userspace_probe_get_filter;
209 urule->parent.get_filter_bytecode =
210 lttng_event_rule_userspace_probe_get_filter_bytecode;
211 urule->parent.generate_exclusions =
212 lttng_event_rule_userspace_probe_generate_exclusions;
213 urule->parent.hash = lttng_event_rule_userspace_probe_hash;
214
215 end:
216 return rule;
217 }
218
219 LTTNG_HIDDEN
220 ssize_t lttng_event_rule_userspace_probe_create_from_payload(
221 struct lttng_payload_view *view,
222 struct lttng_event_rule **_event_rule)
223 {
224 ssize_t ret, offset = 0;
225 const struct lttng_event_rule_userspace_probe_comm *uprobe_comm;
226 const char *name;
227 struct lttng_buffer_view current_buffer_view;
228 struct lttng_event_rule *rule = NULL;
229 struct lttng_userspace_probe_location *location;
230 struct lttng_event_rule_userspace_probe *uprobe;
231 enum lttng_event_rule_status status;
232
233 if (!_event_rule) {
234 ret = -1;
235 goto end;
236 }
237
238 current_buffer_view = lttng_buffer_view_from_view(
239 &view->buffer, offset, sizeof(*uprobe_comm));
240 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
241 ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain header");
242 ret = -1;
243 goto end;
244 }
245
246 uprobe_comm = (typeof(uprobe_comm)) current_buffer_view.data;
247 rule = lttng_event_rule_userspace_probe_create();
248 if (!rule) {
249 ERR("Failed to create event rule uprobe");
250 ret = -1;
251 goto end;
252 }
253
254 /* Skip to payload. */
255 offset += current_buffer_view.size;
256
257 /* Map the name. */
258 current_buffer_view = lttng_buffer_view_from_view(
259 &view->buffer, offset, uprobe_comm->name_len);
260 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
261 ret = -1;
262 goto end;
263 }
264
265 name = current_buffer_view.data;
266 if (!lttng_buffer_view_contains_string(&current_buffer_view, name,
267 uprobe_comm->name_len)) {
268 ret = -1;
269 goto end;
270 }
271
272 /* Skip after the name. */
273 offset += uprobe_comm->name_len;
274
275 /* Map the location. */
276 {
277 struct lttng_payload_view current_payload_view =
278 lttng_payload_view_from_view(view, offset,
279 uprobe_comm->location_len);
280
281 if (!lttng_payload_view_is_valid(&current_payload_view)) {
282 ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain location");
283 ret = -1;
284 goto end;
285 }
286
287 ret = lttng_userspace_probe_location_create_from_payload(
288 &current_payload_view, &location);
289 if (ret < 0) {
290 ret = -1;
291 goto end;
292 }
293 }
294
295 assert(ret == uprobe_comm->location_len);
296
297 /* Skip after the location. */
298 offset += uprobe_comm->location_len;
299
300 uprobe = container_of(rule, struct lttng_event_rule_userspace_probe, parent);
301 uprobe->location = location;
302
303 status = lttng_event_rule_userspace_probe_set_event_name(rule, name);
304 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
305 ret = -1;
306 goto end;
307 }
308
309 if (!lttng_event_rule_userspace_probe_validate(rule)) {
310 ret = -1;
311 goto end;
312 }
313
314 *_event_rule = rule;
315 rule = NULL;
316 ret = offset;
317 end:
318 lttng_event_rule_destroy(rule);
319 return ret;
320 }
321
322 enum lttng_event_rule_status lttng_event_rule_userspace_probe_set_location(
323 struct lttng_event_rule *rule,
324 const struct lttng_userspace_probe_location *location)
325 {
326 struct lttng_userspace_probe_location *location_copy = NULL;
327 struct lttng_event_rule_userspace_probe *uprobe;
328 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
329
330 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !location) {
331 status = LTTNG_EVENT_RULE_STATUS_INVALID;
332 goto end;
333 }
334
335 uprobe = container_of(rule, struct lttng_event_rule_userspace_probe, parent);
336 location_copy = lttng_userspace_probe_location_copy(location);
337 if (!location_copy) {
338 status = LTTNG_EVENT_RULE_STATUS_ERROR;
339 goto end;
340 }
341
342 if (uprobe->location) {
343 lttng_userspace_probe_location_destroy(uprobe->location);
344 }
345
346 uprobe->location = location_copy;
347 location_copy = NULL;
348 end:
349 lttng_userspace_probe_location_destroy(location_copy);
350 return status;
351 }
352
353 enum lttng_event_rule_status lttng_event_rule_userspace_probe_get_location(
354 const struct lttng_event_rule *rule,
355 const struct lttng_userspace_probe_location **location)
356 {
357 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
358
359 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !location) {
360 status = LTTNG_EVENT_RULE_STATUS_INVALID;
361 goto end;
362 }
363
364 *location = lttng_event_rule_userspace_probe_get_location_mutable(rule);
365 if (!*location) {
366 status = LTTNG_EVENT_RULE_STATUS_UNSET;
367 goto end;
368 }
369
370 end:
371 return status;
372 }
373
374 LTTNG_HIDDEN
375 struct lttng_userspace_probe_location *
376 lttng_event_rule_userspace_probe_get_location_mutable(
377 const struct lttng_event_rule *rule)
378 {
379 struct lttng_event_rule_userspace_probe *uprobe;
380
381 assert(rule);
382 uprobe = container_of(rule, struct lttng_event_rule_userspace_probe, parent);
383
384 return uprobe->location;
385 }
386
387 enum lttng_event_rule_status lttng_event_rule_userspace_probe_set_event_name(
388 struct lttng_event_rule *rule, const char *name)
389 {
390 char *name_copy = NULL;
391 struct lttng_event_rule_userspace_probe *uprobe;
392 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
393
394 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !name ||
395 strlen(name) == 0) {
396 status = LTTNG_EVENT_RULE_STATUS_INVALID;
397 goto end;
398 }
399
400 uprobe = container_of(rule, struct lttng_event_rule_userspace_probe, parent);
401 name_copy = strdup(name);
402 if (!name_copy) {
403 status = LTTNG_EVENT_RULE_STATUS_ERROR;
404 goto end;
405 }
406
407 if (uprobe->name) {
408 free(uprobe->name);
409 }
410
411 uprobe->name = name_copy;
412 name_copy = NULL;
413 end:
414 return status;
415 }
416
417 enum lttng_event_rule_status lttng_event_rule_userspace_probe_get_event_name(
418 const struct lttng_event_rule *rule, const char **name)
419 {
420 struct lttng_event_rule_userspace_probe *uprobe;
421 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
422
423 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !name) {
424 status = LTTNG_EVENT_RULE_STATUS_INVALID;
425 goto end;
426 }
427
428 uprobe = container_of(rule, struct lttng_event_rule_userspace_probe, parent);
429 if (!uprobe->name) {
430 status = LTTNG_EVENT_RULE_STATUS_UNSET;
431 goto end;
432 }
433
434 *name = uprobe->name;
435 end:
436 return status;
437 }
This page took 0.037943 seconds and 5 git commands to generate.