Commit | Line | Data |
---|---|---|
ebdb334b JR |
1 | /* |
2 | * Copyright (C) 2021 Francis Deslauriers <francis.deslauriers@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 <ctype.h> | |
18 | #include <lttng/constant.h> | |
19 | #include <lttng/event-rule/event-rule.h> | |
20 | #include <lttng/event-rule/event-rule-internal.h> | |
21 | #include <lttng/event-rule/kernel-function-internal.h> | |
22 | #include <lttng/kernel-function.h> | |
23 | #include <lttng/kernel-function-internal.h> | |
24 | #include <stdio.h> | |
25 | ||
26 | #define IS_KERNEL_FUNCTION_EVENT_RULE(rule) \ | |
27 | (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION) | |
28 | ||
29 | #if (LTTNG_SYMBOL_NAME_LEN == 256) | |
30 | #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255" | |
31 | #endif | |
32 | ||
33 | static void lttng_event_rule_kernel_function_destroy(struct lttng_event_rule *rule) | |
34 | { | |
35 | struct lttng_event_rule_kernel_function *kfunction; | |
36 | ||
37 | kfunction = container_of(rule, struct lttng_event_rule_kernel_function, parent); | |
38 | ||
39 | lttng_kernel_function_location_destroy(kfunction->location); | |
40 | free(kfunction->name); | |
41 | free(kfunction); | |
42 | } | |
43 | ||
44 | static bool lttng_event_rule_kernel_function_validate( | |
45 | const struct lttng_event_rule *rule) | |
46 | { | |
47 | bool valid = false; | |
48 | struct lttng_event_rule_kernel_function *kfunction; | |
49 | ||
50 | if (!rule) { | |
51 | goto end; | |
52 | } | |
53 | ||
54 | kfunction = container_of(rule, struct lttng_event_rule_kernel_function, parent); | |
55 | ||
56 | /* Required field. */ | |
57 | if (!kfunction->name) { | |
58 | ERR("Invalid name event rule: a name must be set."); | |
59 | goto end; | |
60 | } | |
61 | ||
62 | /* Required field. */ | |
63 | if(!kfunction->location) { | |
64 | ERR("Invalid name event rule: a location must be set."); | |
65 | goto end; | |
66 | } | |
67 | ||
68 | valid = true; | |
69 | end: | |
70 | return valid; | |
71 | } | |
72 | ||
73 | static int lttng_event_rule_kernel_function_serialize( | |
74 | const struct lttng_event_rule *rule, | |
75 | struct lttng_payload *payload) | |
76 | { | |
77 | int ret; | |
78 | size_t name_len, header_offset, size_before_location; | |
79 | struct lttng_event_rule_kernel_function *kfunction; | |
80 | struct lttng_event_rule_kernel_function_comm kfunction_comm; | |
81 | struct lttng_event_rule_kernel_function_comm *header; | |
82 | ||
83 | if (!rule || !IS_KERNEL_FUNCTION_EVENT_RULE(rule)) { | |
84 | ret = -1; | |
85 | goto end; | |
86 | } | |
87 | ||
88 | header_offset = payload->buffer.size; | |
89 | ||
90 | DBG("Serializing kfunction event rule."); | |
91 | kfunction = container_of(rule, struct lttng_event_rule_kernel_function, parent); | |
92 | ||
93 | name_len = strlen(kfunction->name) + 1; | |
94 | kfunction_comm.name_len = name_len; | |
95 | ||
96 | ret = lttng_dynamic_buffer_append( | |
97 | &payload->buffer, &kfunction_comm, sizeof(kfunction_comm)); | |
98 | if (ret) { | |
99 | goto end; | |
100 | } | |
101 | ||
102 | ret = lttng_dynamic_buffer_append(&payload->buffer, kfunction->name, name_len); | |
103 | if (ret) { | |
104 | goto end; | |
105 | } | |
106 | ||
107 | size_before_location = payload->buffer.size; | |
108 | ||
109 | ret = lttng_kernel_function_location_serialize(kfunction->location, payload); | |
110 | if (ret < 0) { | |
111 | goto end; | |
112 | } | |
113 | ||
114 | /* Update the header regarding the function size. */ | |
115 | header = (struct lttng_event_rule_kernel_function_comm*) ( | |
116 | (char *) payload->buffer.data + header_offset); | |
117 | header->location_len = payload->buffer.size - size_before_location; | |
118 | ||
119 | ret = 0; | |
120 | ||
121 | end: | |
122 | return ret; | |
123 | } | |
124 | ||
125 | static bool lttng_event_rule_kernel_function_is_equal(const struct lttng_event_rule *_a, | |
126 | const struct lttng_event_rule *_b) | |
127 | { | |
128 | bool is_equal = false; | |
129 | struct lttng_event_rule_kernel_function *a, *b; | |
130 | ||
131 | a = container_of(_a, struct lttng_event_rule_kernel_function, parent); | |
132 | b = container_of(_b, struct lttng_event_rule_kernel_function, parent); | |
133 | ||
134 | /* Quick checks */ | |
135 | if (!!a->name != !!b->name) { | |
136 | goto end; | |
137 | } | |
138 | ||
139 | /* Long check */ | |
140 | assert(a->name); | |
141 | assert(b->name); | |
142 | if (strcmp(a->name, b->name)) { | |
143 | goto end; | |
144 | } | |
145 | ||
146 | is_equal = lttng_kernel_function_location_is_equal( | |
147 | a->location, b->location); | |
148 | end: | |
149 | return is_equal; | |
150 | } | |
151 | ||
152 | static enum lttng_error_code lttng_event_rule_kernel_function_generate_filter_bytecode( | |
153 | struct lttng_event_rule *rule, | |
154 | const struct lttng_credentials *creds) | |
155 | { | |
156 | /* Nothing to do. */ | |
157 | return LTTNG_OK; | |
158 | } | |
159 | ||
160 | static const char *lttng_event_rule_kernel_function_get_filter( | |
161 | const struct lttng_event_rule *rule) | |
162 | { | |
163 | /* Not supported. */ | |
164 | return NULL; | |
165 | } | |
166 | ||
167 | static const struct lttng_bytecode * | |
168 | lttng_event_rule_kernel_function_get_filter_bytecode(const struct lttng_event_rule *rule) | |
169 | { | |
170 | /* Not supported. */ | |
171 | return NULL; | |
172 | } | |
173 | ||
174 | static enum lttng_event_rule_generate_exclusions_status | |
175 | lttng_event_rule_kernel_function_generate_exclusions(const struct lttng_event_rule *rule, | |
176 | struct lttng_event_exclusion **exclusions) | |
177 | { | |
178 | /* Not supported. */ | |
179 | *exclusions = NULL; | |
180 | return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE; | |
181 | } | |
182 | ||
183 | static unsigned long | |
184 | lttng_event_rule_kernel_function_hash( | |
185 | const struct lttng_event_rule *rule) | |
186 | { | |
187 | unsigned long hash; | |
188 | struct lttng_event_rule_kernel_function *krule = | |
189 | container_of(rule, typeof(*krule), parent); | |
190 | ||
191 | hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION, | |
192 | lttng_ht_seed); | |
193 | hash ^= hash_key_str(krule->name, lttng_ht_seed); | |
194 | hash ^= lttng_kernel_function_location_hash(krule->location); | |
195 | ||
196 | return hash; | |
197 | } | |
198 | ||
199 | static | |
200 | int kernel_function_set_location( | |
201 | struct lttng_event_rule_kernel_function *kfunction, | |
202 | const struct lttng_kernel_function_location *location) | |
203 | { | |
204 | int ret; | |
205 | struct lttng_kernel_function_location *location_copy = NULL; | |
206 | ||
207 | if (!kfunction || !location || kfunction->location) { | |
208 | ret = -1; | |
209 | goto end; | |
210 | } | |
211 | ||
212 | location_copy = lttng_kernel_function_location_copy(location); | |
213 | if (!location_copy) { | |
214 | ret = -1; | |
215 | goto end; | |
216 | } | |
217 | ||
218 | kfunction->location = location_copy; | |
219 | location_copy = NULL; | |
220 | ret = 0; | |
221 | end: | |
222 | lttng_kernel_function_location_destroy(location_copy); | |
223 | return ret; | |
224 | } | |
225 | ||
226 | struct lttng_event_rule *lttng_event_rule_kernel_function_create( | |
227 | const struct lttng_kernel_function_location *location) | |
228 | { | |
229 | struct lttng_event_rule *rule = NULL; | |
230 | struct lttng_event_rule_kernel_function *krule; | |
231 | ||
232 | krule = zmalloc(sizeof(struct lttng_event_rule_kernel_function)); | |
233 | if (!krule) { | |
234 | goto end; | |
235 | } | |
236 | ||
237 | rule = &krule->parent; | |
238 | lttng_event_rule_init(&krule->parent, LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION); | |
239 | krule->parent.validate = lttng_event_rule_kernel_function_validate; | |
240 | krule->parent.serialize = lttng_event_rule_kernel_function_serialize; | |
241 | krule->parent.equal = lttng_event_rule_kernel_function_is_equal; | |
242 | krule->parent.destroy = lttng_event_rule_kernel_function_destroy; | |
243 | krule->parent.generate_filter_bytecode = | |
244 | lttng_event_rule_kernel_function_generate_filter_bytecode; | |
245 | krule->parent.get_filter = lttng_event_rule_kernel_function_get_filter; | |
246 | krule->parent.get_filter_bytecode = | |
247 | lttng_event_rule_kernel_function_get_filter_bytecode; | |
248 | krule->parent.generate_exclusions = | |
249 | lttng_event_rule_kernel_function_generate_exclusions; | |
250 | krule->parent.hash = lttng_event_rule_kernel_function_hash; | |
251 | ||
252 | if (kernel_function_set_location(krule, location)) { | |
253 | lttng_event_rule_destroy(rule); | |
254 | rule = NULL; | |
255 | } | |
256 | ||
257 | end: | |
258 | return rule; | |
259 | } | |
260 | ||
261 | LTTNG_HIDDEN | |
262 | ssize_t lttng_event_rule_kernel_function_create_from_payload( | |
263 | struct lttng_payload_view *view, | |
264 | struct lttng_event_rule **_event_rule) | |
265 | { | |
266 | ssize_t ret, offset = 0; | |
267 | enum lttng_event_rule_status status; | |
268 | const struct lttng_event_rule_kernel_function_comm *kfunction_comm; | |
269 | const char *name; | |
270 | struct lttng_buffer_view current_buffer_view; | |
271 | struct lttng_event_rule *rule = NULL; | |
272 | struct lttng_kernel_function_location *location = NULL; | |
273 | ||
274 | if (!_event_rule) { | |
275 | ret = -1; | |
276 | goto end; | |
277 | } | |
278 | ||
279 | current_buffer_view = lttng_buffer_view_from_view( | |
280 | &view->buffer, offset, sizeof(*kfunction_comm)); | |
281 | if (!lttng_buffer_view_is_valid(¤t_buffer_view)) { | |
282 | ERR("Failed to initialize from malformed event rule kfunction: buffer too short to contain header."); | |
283 | ret = -1; | |
284 | goto end; | |
285 | } | |
286 | ||
287 | kfunction_comm = (typeof(kfunction_comm)) current_buffer_view.data; | |
288 | ||
289 | /* Skip to payload */ | |
290 | offset += current_buffer_view.size; | |
291 | ||
292 | { | |
293 | /* Map the name. */ | |
294 | struct lttng_payload_view current_payload_view = | |
295 | lttng_payload_view_from_view(view, offset, | |
296 | kfunction_comm->name_len); | |
297 | ||
298 | if (!lttng_payload_view_is_valid(¤t_payload_view)) { | |
299 | ret = -1; | |
300 | goto end; | |
301 | } | |
302 | ||
303 | name = current_payload_view.buffer.data; | |
304 | if (!lttng_buffer_view_contains_string( | |
305 | ¤t_payload_view.buffer, name, | |
306 | kfunction_comm->name_len)) { | |
307 | ret = -1; | |
308 | goto end; | |
309 | } | |
310 | } | |
311 | ||
312 | /* Skip after the name. */ | |
313 | offset += kfunction_comm->name_len; | |
314 | ||
315 | /* Map the kernel function location. */ | |
316 | { | |
317 | struct lttng_payload_view current_payload_view = | |
318 | lttng_payload_view_from_view(view, offset, | |
319 | kfunction_comm->location_len); | |
320 | ||
321 | if (!lttng_payload_view_is_valid(¤t_payload_view)) { | |
322 | ret = -1; | |
323 | goto end; | |
324 | } | |
325 | ||
326 | ret = lttng_kernel_function_location_create_from_payload( | |
327 | ¤t_payload_view, &location); | |
328 | if (ret < 0) { | |
329 | ret = -1; | |
330 | goto end; | |
331 | } | |
332 | } | |
333 | ||
334 | if (ret != kfunction_comm->location_len) { | |
335 | ret = -1; | |
336 | goto end; | |
337 | } | |
338 | ||
339 | /* Skip after the location */ | |
340 | offset += kfunction_comm->location_len; | |
341 | ||
342 | rule = lttng_event_rule_kernel_function_create(location); | |
343 | if (!rule) { | |
344 | ERR("Failed to create event rule kfunction."); | |
345 | ret = -1; | |
346 | goto end; | |
347 | } | |
348 | ||
349 | status = lttng_event_rule_kernel_function_set_event_name(rule, name); | |
350 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { | |
351 | ERR("Failed to set event rule kfunction name."); | |
352 | ret = -1; | |
353 | goto end; | |
354 | } | |
355 | ||
356 | *_event_rule = rule; | |
357 | rule = NULL; | |
358 | ret = offset; | |
359 | end: | |
360 | lttng_kernel_function_location_destroy(location); | |
361 | lttng_event_rule_destroy(rule); | |
362 | return ret; | |
363 | } | |
364 | ||
365 | enum lttng_event_rule_status lttng_event_rule_kernel_function_get_location( | |
366 | const struct lttng_event_rule *rule, | |
367 | const struct lttng_kernel_function_location **location) | |
368 | { | |
369 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; | |
370 | struct lttng_event_rule_kernel_function *kfunction; | |
371 | ||
372 | if (!rule || !IS_KERNEL_FUNCTION_EVENT_RULE(rule) || !location) { | |
373 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
374 | goto end; | |
375 | } | |
376 | ||
377 | kfunction = container_of(rule, struct lttng_event_rule_kernel_function, parent); | |
378 | *location = kfunction->location; | |
379 | ||
380 | if (!*location) { | |
381 | status = LTTNG_EVENT_RULE_STATUS_UNSET; | |
382 | goto end; | |
383 | } | |
384 | ||
385 | end: | |
386 | return status; | |
387 | } | |
388 | ||
389 | enum lttng_event_rule_status lttng_event_rule_kernel_function_set_event_name( | |
390 | struct lttng_event_rule *rule, const char *name) | |
391 | { | |
392 | char *name_copy = NULL; | |
393 | struct lttng_event_rule_kernel_function *kfunction; | |
394 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; | |
395 | ||
396 | if (!rule || !IS_KERNEL_FUNCTION_EVENT_RULE(rule) || !name || | |
397 | strlen(name) == 0) { | |
398 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
399 | goto end; | |
400 | } | |
401 | ||
402 | kfunction = container_of(rule, struct lttng_event_rule_kernel_function, parent); | |
403 | name_copy = strdup(name); | |
404 | if (!name_copy) { | |
405 | status = LTTNG_EVENT_RULE_STATUS_ERROR; | |
406 | goto end; | |
407 | } | |
408 | ||
409 | free(kfunction->name); | |
410 | ||
411 | kfunction->name = name_copy; | |
412 | name_copy = NULL; | |
413 | end: | |
414 | return status; | |
415 | } | |
416 | ||
417 | enum lttng_event_rule_status lttng_event_rule_kernel_function_get_event_name( | |
418 | const struct lttng_event_rule *rule, const char **name) | |
419 | { | |
420 | struct lttng_event_rule_kernel_function *kfunction; | |
421 | enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK; | |
422 | ||
423 | if (!rule || !IS_KERNEL_FUNCTION_EVENT_RULE(rule) || !name) { | |
424 | status = LTTNG_EVENT_RULE_STATUS_INVALID; | |
425 | goto end; | |
426 | } | |
427 | ||
428 | kfunction = container_of(rule, struct lttng_event_rule_kernel_function, parent); | |
429 | if (!kfunction->name) { | |
430 | status = LTTNG_EVENT_RULE_STATUS_UNSET; | |
431 | goto end; | |
432 | } | |
433 | ||
434 | *name = kfunction->name; | |
435 | end: | |
436 | return status; | |
437 | } |