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