SoW-2019-0007-2: Dynamic Snapshot: Triggers send partial event payload with notifications
[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/error.h>
10 #include <common/macros.h>
11 #include <common/runas.h>
12 #include <lttng/event-rule/event-rule-internal.h>
13 #include <lttng/event-rule/uprobe-internal.h>
14 #include <lttng/userspace-probe-internal.h>
15
16 #define IS_UPROBE_EVENT_RULE(rule) \
17 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_UPROBE)
18
19 static void lttng_event_rule_uprobe_destroy(struct lttng_event_rule *rule)
20 {
21 struct lttng_event_rule_uprobe *uprobe;
22
23 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
24
25 lttng_userspace_probe_location_destroy(uprobe->location);
26 free(uprobe->name);
27 free(uprobe);
28 }
29
30 static bool lttng_event_rule_uprobe_validate(
31 const struct lttng_event_rule *rule)
32 {
33 bool valid = false;
34 struct lttng_event_rule_uprobe *uprobe;
35
36 if (!rule) {
37 goto end;
38 }
39
40 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
41
42 /* Required field */
43 if (!uprobe->name) {
44 ERR("Invalid uprobe event rule: a pattern must be set.");
45 goto end;
46 }
47
48 if (!uprobe->location) {
49 ERR("Invalid uprobe event rule: a location must be set.");
50 goto end;
51 }
52
53 /* TODO should we validate the probe location? */
54
55 valid = true;
56 end:
57 return valid;
58 }
59
60 static int lttng_event_rule_uprobe_serialize(
61 const struct lttng_event_rule *rule,
62 struct lttng_dynamic_buffer *buf,
63 int *fd_to_send)
64 {
65 int ret;
66 size_t name_len, header_offset, size_before_probe;
67 struct lttng_event_rule_uprobe *uprobe;
68 struct lttng_event_rule_uprobe_comm uprobe_comm = {0};
69 struct lttng_event_rule_uprobe_comm *header;
70
71 if (!rule || !IS_UPROBE_EVENT_RULE(rule)) {
72 ret = -1;
73 goto end;
74 }
75
76 header_offset = buf->size;
77
78 DBG("Serializing uprobe event rule");
79 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
80
81 name_len = strlen(uprobe->name) + 1;
82
83 uprobe_comm.name_len = name_len;
84
85 ret = lttng_dynamic_buffer_append(
86 buf, &uprobe_comm, sizeof(uprobe_comm));
87 if (ret) {
88 goto end;
89 }
90 ret = lttng_dynamic_buffer_append(buf, uprobe->name, name_len);
91 if (ret) {
92 goto end;
93 }
94
95 size_before_probe = buf->size;
96
97 /* This serialize return the size taken in the buffer */
98 /* TODO: should all serialize standardise on this? */
99 ret = lttng_userspace_probe_location_serialize(
100 uprobe->location, buf, fd_to_send);
101 if (ret < 0) {
102 goto end;
103 }
104
105 /* Update the header regarding the probe size */
106 header = (struct lttng_event_rule_uprobe_comm *) ((char *) buf->data +
107 header_offset);
108 header->location_len = buf->size - size_before_probe;
109
110 ret = 0;
111
112 end:
113 return ret;
114 }
115
116 static bool lttng_event_rule_uprobe_is_equal(const struct lttng_event_rule *_a,
117 const struct lttng_event_rule *_b)
118 {
119 bool is_equal = false;
120 struct lttng_event_rule_uprobe *a, *b;
121
122 a = container_of(_a, struct lttng_event_rule_uprobe, parent);
123 b = container_of(_b, struct lttng_event_rule_uprobe, parent);
124
125 /* uprobe is invalid if this is not true */
126 assert(a->name);
127 assert(b->name);
128 if (strcmp(a->name, b->name)) {
129 goto end;
130 }
131
132 assert(a->location);
133 assert(b->location);
134 is_equal = lttng_userspace_probe_location_is_equal(
135 a->location, b->location);
136 end:
137 return is_equal;
138 }
139
140 static enum lttng_error_code lttng_event_rule_uprobe_populate(
141 struct lttng_event_rule *rule, uid_t uid, gid_t gid)
142 {
143 /* Nothing to do */
144 return LTTNG_OK;
145 }
146
147 static const char *lttng_event_rule_uprobe_get_filter(
148 const struct lttng_event_rule *rule)
149 {
150 /* Unsupported */
151 return NULL;
152 }
153
154 static const struct lttng_bytecode *
155 lttng_event_rule_uprobe_get_filter_bytecode(const struct lttng_event_rule *rule)
156 {
157 /* Unsupported */
158 return NULL;
159 }
160
161 static struct lttng_event_exclusion *
162 lttng_event_rule_uprobe_generate_exclusions(struct lttng_event_rule *rule)
163 {
164 /* Unsupported */
165 return NULL;
166 }
167
168 struct lttng_event_rule *lttng_event_rule_uprobe_create()
169 {
170 struct lttng_event_rule_uprobe *rule;
171
172 rule = zmalloc(sizeof(struct lttng_event_rule_uprobe));
173 if (!rule) {
174 return NULL;
175 }
176
177 lttng_event_rule_init(&rule->parent, LTTNG_EVENT_RULE_TYPE_UPROBE);
178 rule->parent.validate = lttng_event_rule_uprobe_validate;
179 rule->parent.serialize = lttng_event_rule_uprobe_serialize;
180 rule->parent.equal = lttng_event_rule_uprobe_is_equal;
181 rule->parent.destroy = lttng_event_rule_uprobe_destroy;
182 rule->parent.populate = lttng_event_rule_uprobe_populate;
183 rule->parent.get_filter = lttng_event_rule_uprobe_get_filter;
184 rule->parent.get_filter_bytecode =
185 lttng_event_rule_uprobe_get_filter_bytecode;
186 rule->parent.generate_exclusions =
187 lttng_event_rule_uprobe_generate_exclusions;
188 return &rule->parent;
189 }
190
191 LTTNG_HIDDEN
192 ssize_t lttng_event_rule_uprobe_create_from_buffer(
193 const struct lttng_buffer_view *view,
194 struct lttng_event_rule **_event_rule)
195 {
196 ssize_t ret, offset = 0;
197 const struct lttng_event_rule_uprobe_comm *uprobe_comm;
198 const char *name;
199 struct lttng_buffer_view current_view;
200 struct lttng_event_rule *rule = NULL;
201 struct lttng_userspace_probe_location *location;
202 struct lttng_event_rule_uprobe *uprobe;
203
204 if (!_event_rule) {
205 ret = -1;
206 goto end;
207 }
208
209 if (view->size < sizeof(*uprobe_comm)) {
210 ERR("Failed to initialize from malformed event rule uprobe: buffer too short to contain header");
211 ret = -1;
212 goto end;
213 }
214
215 current_view = lttng_buffer_view_from_view(
216 view, offset, sizeof(*uprobe_comm));
217 uprobe_comm = (typeof(uprobe_comm)) current_view.data;
218
219 if (!uprobe_comm) {
220 ret = -1;
221 goto end;
222 }
223
224 rule = lttng_event_rule_uprobe_create();
225 if (!rule) {
226 ERR("Failed to create event rule uprobe");
227 ret = -1;
228 goto end;
229 }
230
231 /* Skip to payload */
232 offset += current_view.size;
233
234 /* Map the name */
235 current_view = lttng_buffer_view_from_view(
236 view, offset, uprobe_comm->name_len);
237 name = current_view.data;
238 if (!name) {
239 ret = -1;
240 goto end;
241 }
242
243 if (uprobe_comm->name_len == 1 ||
244 name[uprobe_comm->name_len - 1] != '\0' ||
245 strlen(name) != uprobe_comm->name_len - 1) {
246 /*
247 * Check that the name is not NULL, is NULL-terminated, and
248 * does not contain a NULL before the last byte.
249 */
250 ret = -1;
251 goto end;
252 }
253
254 /* Skip after the name */
255 offset += uprobe_comm->name_len;
256
257 /* Map the location */
258 current_view = lttng_buffer_view_from_view(
259 view, offset, uprobe_comm->location_len);
260 ret = lttng_userspace_probe_location_create_from_buffer(
261 &current_view, &location);
262 if (ret < 0) {
263 ret = -1;
264 goto end;
265 }
266
267 assert(ret == uprobe_comm->location_len);
268
269 /* Skip after the location */
270 offset += uprobe_comm->location_len;
271
272 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
273 uprobe->location = location;
274
275 (void) lttng_event_rule_uprobe_set_name(rule, name);
276
277 if (!lttng_event_rule_uprobe_validate(rule)) {
278 ret = -1;
279 goto end;
280 }
281
282 *_event_rule = rule;
283 rule = NULL;
284 ret = offset;
285 end:
286 lttng_event_rule_destroy(rule);
287 return ret;
288 }
289
290 enum lttng_event_rule_status lttng_event_rule_uprobe_set_location(
291 struct lttng_event_rule *rule,
292 const struct lttng_userspace_probe_location *location)
293 {
294 struct lttng_userspace_probe_location *location_copy = NULL;
295 struct lttng_event_rule_uprobe *uprobe;
296 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
297
298 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !location) {
299 status = LTTNG_EVENT_RULE_STATUS_INVALID;
300 goto end;
301 }
302
303 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
304 location_copy = lttng_userspace_probe_location_copy(location);
305 if (!location_copy) {
306 status = LTTNG_EVENT_RULE_STATUS_ERROR;
307 goto end;
308 }
309
310 if (uprobe->location) {
311 lttng_userspace_probe_location_destroy(uprobe->location);
312 }
313
314 uprobe->location = location_copy;
315 location_copy = NULL;
316 end:
317 lttng_userspace_probe_location_destroy(location_copy);
318 return status;
319 }
320
321 enum lttng_event_rule_status lttng_event_rule_uprobe_get_location(
322 const struct lttng_event_rule *rule,
323 const struct lttng_userspace_probe_location **location)
324 {
325 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
326
327 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !location) {
328 status = LTTNG_EVENT_RULE_STATUS_INVALID;
329 goto end;
330 }
331
332 *location = lttng_event_rule_uprobe_get_location_no_const(rule);
333 if (!*location) {
334 status = LTTNG_EVENT_RULE_STATUS_UNSET;
335 goto end;
336 }
337
338 end:
339 return status;
340 }
341
342 LTTNG_HIDDEN
343 struct lttng_userspace_probe_location *
344 lttng_event_rule_uprobe_get_location_no_const(
345 const struct lttng_event_rule *rule)
346 {
347 struct lttng_event_rule_uprobe *uprobe;
348 assert(rule);
349 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
350
351 return uprobe->location;
352 }
353
354 enum lttng_event_rule_status lttng_event_rule_uprobe_set_name(
355 struct lttng_event_rule *rule, const char *name)
356 {
357 char *name_copy = NULL;
358 struct lttng_event_rule_uprobe *uprobe;
359 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
360
361 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !name ||
362 strlen(name) == 0) {
363 status = LTTNG_EVENT_RULE_STATUS_INVALID;
364 goto end;
365 }
366
367 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
368 name_copy = strdup(name);
369 if (!name_copy) {
370 status = LTTNG_EVENT_RULE_STATUS_ERROR;
371 goto end;
372 }
373
374 if (uprobe->name) {
375 free(uprobe->name);
376 }
377
378 uprobe->name = name_copy;
379 name_copy = NULL;
380 end:
381 return status;
382 }
383
384 enum lttng_event_rule_status lttng_event_rule_uprobe_get_name(
385 const struct lttng_event_rule *rule, const char **name)
386 {
387 struct lttng_event_rule_uprobe *uprobe;
388 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
389
390 if (!rule || !IS_UPROBE_EVENT_RULE(rule) || !name) {
391 status = LTTNG_EVENT_RULE_STATUS_INVALID;
392 goto end;
393 }
394
395 uprobe = container_of(rule, struct lttng_event_rule_uprobe, parent);
396 if (!uprobe->name) {
397 status = LTTNG_EVENT_RULE_STATUS_UNSET;
398 goto end;
399 }
400
401 *name = uprobe->name;
402 end:
403 return status;
404 }
This page took 0.038333 seconds and 5 git commands to generate.