Rename C++ header files to .hpp
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include <lttng/event.h>
15 #include <lttng/lttng-error.h>
16 #include <lttng/kernel-probe.h>
17 #include <lttng/userspace-probe.h>
18 #include <lttng/userspace-probe-internal.hpp>
19 #include <lttng/event-rule/event-rule.h>
20 #include <lttng/event-rule/event-rule-internal.hpp>
21 #include <lttng/event-rule/kernel-kprobe.h>
22 #include <lttng/event-rule/kernel-kprobe-internal.hpp>
23 #include <lttng/event-rule/kernel-syscall.h>
24 #include <lttng/event-rule/kernel-syscall-internal.hpp>
25 #include <lttng/event-rule/kernel-tracepoint.h>
26 #include <lttng/event-rule/kernel-tracepoint-internal.hpp>
27 #include <lttng/event-rule/kernel-uprobe.h>
28 #include <lttng/event-rule/kernel-uprobe-internal.hpp>
29 #include <common/common.hpp>
30 #include <common/defaults.hpp>
31 #include <common/trace-chunk.hpp>
32 #include <common/macros.hpp>
33
34 #include "consumer.hpp"
35 #include "trace-kernel.hpp"
36 #include "lttng-sessiond.hpp"
37 #include "notification-thread-commands.hpp"
38
39 /*
40 * Find the channel name for the given kernel session.
41 */
42 struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
43 const char *name, struct ltt_kernel_session *session)
44 {
45 struct ltt_kernel_channel *chan;
46
47 LTTNG_ASSERT(session);
48 LTTNG_ASSERT(name);
49
50 /*
51 * If we receive an empty string for channel name, it means the
52 * default channel name is requested.
53 */
54 if (name[0] == '\0')
55 name = DEFAULT_CHANNEL_NAME;
56
57 DBG("Trying to find channel %s", name);
58
59 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
60 if (strcmp(name, chan->channel->name) == 0) {
61 DBG("Found channel by name %s", name);
62 return chan;
63 }
64 }
65
66 return NULL;
67 }
68
69 /*
70 * Find the event for the given channel.
71 */
72 struct ltt_kernel_event *trace_kernel_find_event(
73 char *name, struct ltt_kernel_channel *channel,
74 enum lttng_event_type type,
75 struct lttng_bytecode *filter)
76 {
77 struct ltt_kernel_event *ev;
78 int found = 0;
79
80 LTTNG_ASSERT(name);
81 LTTNG_ASSERT(channel);
82
83 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
84 if (type != LTTNG_EVENT_ALL && ev->type != type) {
85 continue;
86 }
87 if (strcmp(name, ev->event->name)) {
88 continue;
89 }
90 if ((ev->filter && !filter) || (!ev->filter && filter)) {
91 continue;
92 }
93 if (ev->filter && filter) {
94 if (ev->filter->len != filter->len ||
95 memcmp(ev->filter->data, filter->data,
96 filter->len) != 0) {
97 continue;
98 }
99 }
100 found = 1;
101 break;
102 }
103 if (found) {
104 DBG("Found event %s for channel %s", name,
105 channel->channel->name);
106 return ev;
107 } else {
108 return NULL;
109 }
110 }
111
112 /*
113 * Find the event name for the given channel.
114 */
115 struct ltt_kernel_event *trace_kernel_get_event_by_name(
116 char *name, struct ltt_kernel_channel *channel,
117 enum lttng_event_type type)
118 {
119 struct ltt_kernel_event *ev;
120 int found = 0;
121
122 LTTNG_ASSERT(name);
123 LTTNG_ASSERT(channel);
124
125 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
126 if (type != LTTNG_EVENT_ALL && ev->type != type) {
127 continue;
128 }
129 if (strcmp(name, ev->event->name)) {
130 continue;
131 }
132 found = 1;
133 break;
134 }
135 if (found) {
136 DBG("Found event %s for channel %s", name,
137 channel->channel->name);
138 return ev;
139 } else {
140 return NULL;
141 }
142 }
143
144 /*
145 * Allocate and initialize a kernel session data structure.
146 *
147 * Return pointer to structure or NULL.
148 */
149 struct ltt_kernel_session *trace_kernel_create_session(void)
150 {
151 struct ltt_kernel_session *lks = NULL;
152
153 /* Allocate a new ltt kernel session */
154 lks = (ltt_kernel_session *) zmalloc(sizeof(struct ltt_kernel_session));
155 if (lks == NULL) {
156 PERROR("create kernel session zmalloc");
157 goto alloc_error;
158 }
159
160 /* Init data structure */
161 lks->fd = -1;
162 lks->metadata_stream_fd = -1;
163 lks->channel_count = 0;
164 lks->stream_count_global = 0;
165 lks->metadata = NULL;
166 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
167
168 lks->tracker_pid = process_attr_tracker_create();
169 if (!lks->tracker_pid) {
170 goto error;
171 }
172 lks->tracker_vpid = process_attr_tracker_create();
173 if (!lks->tracker_vpid) {
174 goto error;
175 }
176 lks->tracker_uid = process_attr_tracker_create();
177 if (!lks->tracker_uid) {
178 goto error;
179 }
180 lks->tracker_vuid = process_attr_tracker_create();
181 if (!lks->tracker_vuid) {
182 goto error;
183 }
184 lks->tracker_gid = process_attr_tracker_create();
185 if (!lks->tracker_gid) {
186 goto error;
187 }
188 lks->tracker_vgid = process_attr_tracker_create();
189 if (!lks->tracker_vgid) {
190 goto error;
191 }
192 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
193 if (lks->consumer == NULL) {
194 goto error;
195 }
196
197 return lks;
198
199 error:
200 process_attr_tracker_destroy(lks->tracker_pid);
201 process_attr_tracker_destroy(lks->tracker_vpid);
202 process_attr_tracker_destroy(lks->tracker_uid);
203 process_attr_tracker_destroy(lks->tracker_vuid);
204 process_attr_tracker_destroy(lks->tracker_gid);
205 process_attr_tracker_destroy(lks->tracker_vgid);
206 free(lks);
207
208 alloc_error:
209 return NULL;
210 }
211
212 /*
213 * Allocate and initialize a kernel channel data structure.
214 *
215 * Return pointer to structure or NULL.
216 */
217 struct ltt_kernel_channel *trace_kernel_create_channel(
218 struct lttng_channel *chan)
219 {
220 struct ltt_kernel_channel *lkc;
221 struct lttng_channel_extended *extended = NULL;
222
223 LTTNG_ASSERT(chan);
224
225 lkc = (ltt_kernel_channel *) zmalloc(sizeof(struct ltt_kernel_channel));
226 if (lkc == NULL) {
227 PERROR("ltt_kernel_channel zmalloc");
228 goto error;
229 }
230
231 lkc->channel = (lttng_channel *) zmalloc(sizeof(struct lttng_channel));
232 if (lkc->channel == NULL) {
233 PERROR("lttng_channel zmalloc");
234 goto error;
235 }
236
237 extended = (lttng_channel_extended *) zmalloc(sizeof(struct lttng_channel_extended));
238 if (!extended) {
239 PERROR("lttng_channel_channel zmalloc");
240 goto error;
241 }
242 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
243 memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended));
244 lkc->channel->attr.extended.ptr = extended;
245 extended = NULL;
246
247 /*
248 * If we receive an empty string for channel name, it means the
249 * default channel name is requested.
250 */
251 if (chan->name[0] == '\0') {
252 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
253 sizeof(lkc->channel->name));
254 }
255 lkc->channel->name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
256
257 lkc->fd = -1;
258 lkc->stream_count = 0;
259 lkc->event_count = 0;
260 lkc->enabled = 1;
261 lkc->published_to_notification_thread = false;
262 /* Init linked list */
263 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
264 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
265 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
266
267 return lkc;
268
269 error:
270 if (lkc) {
271 free(lkc->channel);
272 }
273 free(extended);
274 free(lkc);
275 return NULL;
276 }
277
278 /*
279 * Allocate and init a kernel context object.
280 *
281 * Return the allocated object or NULL on error.
282 */
283 struct ltt_kernel_context *trace_kernel_create_context(
284 struct lttng_kernel_abi_context *ctx)
285 {
286 struct ltt_kernel_context *kctx;
287
288 kctx = (ltt_kernel_context *) zmalloc(sizeof(*kctx));
289 if (!kctx) {
290 PERROR("zmalloc kernel context");
291 goto error;
292 }
293
294 if (ctx) {
295 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
296 }
297 error:
298 return kctx;
299 }
300
301 /*
302 * Allocate and init a kernel context object from an existing kernel context
303 * object.
304 *
305 * Return the allocated object or NULL on error.
306 */
307 struct ltt_kernel_context *trace_kernel_copy_context(
308 struct ltt_kernel_context *kctx)
309 {
310 struct ltt_kernel_context *kctx_copy;
311
312 LTTNG_ASSERT(kctx);
313 kctx_copy = (ltt_kernel_context *) zmalloc(sizeof(*kctx_copy));
314 if (!kctx_copy) {
315 PERROR("zmalloc ltt_kernel_context");
316 goto error;
317 }
318
319 memcpy(kctx_copy, kctx, sizeof(*kctx_copy));
320 memset(&kctx_copy->list, 0, sizeof(kctx_copy->list));
321
322 error:
323 return kctx_copy;
324 }
325
326 /*
327 * Allocate and initialize a kernel event. Set name and event type.
328 * We own filter_expression, and filter.
329 *
330 * Return pointer to structure or NULL.
331 */
332 enum lttng_error_code trace_kernel_create_event(
333 struct lttng_event *ev, char *filter_expression,
334 struct lttng_bytecode *filter,
335 struct ltt_kernel_event **kernel_event)
336 {
337 enum lttng_error_code ret;
338 struct lttng_kernel_abi_event *attr;
339 struct ltt_kernel_event *local_kernel_event;
340 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
341
342 LTTNG_ASSERT(ev);
343
344 local_kernel_event = (ltt_kernel_event *) zmalloc(sizeof(struct ltt_kernel_event));
345 attr = (lttng_kernel_abi_event *) zmalloc(sizeof(struct lttng_kernel_abi_event));
346 if (local_kernel_event == NULL || attr == NULL) {
347 PERROR("kernel event zmalloc");
348 ret = LTTNG_ERR_NOMEM;
349 goto error;
350 }
351
352 switch (ev->type) {
353 case LTTNG_EVENT_PROBE:
354 attr->instrumentation = LTTNG_KERNEL_ABI_KPROBE;
355 attr->u.kprobe.addr = ev->attr.probe.addr;
356 attr->u.kprobe.offset = ev->attr.probe.offset;
357 strncpy(attr->u.kprobe.symbol_name,
358 ev->attr.probe.symbol_name, LTTNG_KERNEL_ABI_SYM_NAME_LEN);
359 attr->u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
360 break;
361 case LTTNG_EVENT_USERSPACE_PROBE:
362 {
363 const struct lttng_userspace_probe_location* location = NULL;
364 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
365
366 location = lttng_event_get_userspace_probe_location(ev);
367 if (!location) {
368 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
369 goto error;
370 }
371
372 /*
373 * From this point on, the specific term 'uprobe' is used
374 * instead of the generic 'userspace probe' because it's the
375 * technology used at the moment for this instrumentation.
376 * LTTng currently implements userspace probes using uprobes.
377 * In the interactions with the kernel tracer, we use the
378 * uprobe term.
379 */
380 attr->instrumentation = LTTNG_KERNEL_ABI_UPROBE;
381
382 lookup = lttng_userspace_probe_location_get_lookup_method(
383 location);
384 if (!lookup) {
385 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
386 goto error;
387 }
388
389 /*
390 * From the kernel tracer's perspective, all userspace probe
391 * event types are all the same: a file and an offset.
392 */
393 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
394 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
395 /* Get the file descriptor on the target binary. */
396 attr->u.uprobe.fd =
397 lttng_userspace_probe_location_function_get_binary_fd(location);
398
399 /*
400 * Save a reference to the probe location used during
401 * the listing of events.
402 */
403 userspace_probe_location =
404 lttng_userspace_probe_location_copy(location);
405 break;
406 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
407 /* Get the file descriptor on the target binary. */
408 attr->u.uprobe.fd =
409 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
410
411 /*
412 * Save a reference to the probe location used during the listing of
413 * events.
414 */
415 userspace_probe_location =
416 lttng_userspace_probe_location_copy(location);
417 break;
418 default:
419 DBG("Unsupported lookup method type");
420 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
421 goto error;
422 }
423 break;
424 }
425 case LTTNG_EVENT_FUNCTION:
426 attr->instrumentation = LTTNG_KERNEL_ABI_KRETPROBE;
427 attr->u.kretprobe.addr = ev->attr.probe.addr;
428 attr->u.kretprobe.offset = ev->attr.probe.offset;
429 strncpy(attr->u.kretprobe.symbol_name,
430 ev->attr.probe.symbol_name, LTTNG_KERNEL_ABI_SYM_NAME_LEN);
431 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
432 break;
433 case LTTNG_EVENT_FUNCTION_ENTRY:
434 attr->instrumentation = LTTNG_KERNEL_ABI_FUNCTION;
435 strncpy(attr->u.ftrace.symbol_name,
436 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_ABI_SYM_NAME_LEN);
437 attr->u.ftrace.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
438 break;
439 case LTTNG_EVENT_TRACEPOINT:
440 attr->instrumentation = LTTNG_KERNEL_ABI_TRACEPOINT;
441 break;
442 case LTTNG_EVENT_SYSCALL:
443 attr->instrumentation = LTTNG_KERNEL_ABI_SYSCALL;
444 attr->u.syscall.abi = LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL;
445 attr->u.syscall.entryexit = LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT;
446 attr->u.syscall.match = LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME;
447 break;
448 case LTTNG_EVENT_ALL:
449 attr->instrumentation = LTTNG_KERNEL_ABI_ALL;
450 break;
451 default:
452 ERR("Unknown kernel instrumentation type (%d)", ev->type);
453 ret = LTTNG_ERR_INVALID;
454 goto error;
455 }
456
457 /* Copy event name */
458 strncpy(attr->name, ev->name, LTTNG_KERNEL_ABI_SYM_NAME_LEN);
459 attr->name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
460
461 /* Setting up a kernel event */
462 local_kernel_event->fd = -1;
463 local_kernel_event->event = attr;
464 local_kernel_event->enabled = 1;
465 local_kernel_event->filter_expression = filter_expression;
466 local_kernel_event->filter = filter;
467 local_kernel_event->userspace_probe_location = userspace_probe_location;
468
469 *kernel_event = local_kernel_event;
470
471 return LTTNG_OK;
472
473 error:
474 free(filter_expression);
475 free(filter);
476 free(local_kernel_event);
477 free(attr);
478 return ret;
479 }
480
481 /*
482 * Allocate and initialize a kernel token event rule.
483 *
484 * Return pointer to structure or NULL.
485 */
486 enum lttng_error_code trace_kernel_create_event_notifier_rule(
487 struct lttng_trigger *trigger,
488 uint64_t token,
489 uint64_t error_counter_index,
490 struct ltt_kernel_event_notifier_rule **event_notifier_rule)
491 {
492 enum lttng_error_code ret = LTTNG_OK;
493 enum lttng_condition_type condition_type;
494 enum lttng_event_rule_type event_rule_type;
495 enum lttng_condition_status condition_status;
496 struct ltt_kernel_event_notifier_rule *local_kernel_token_event_rule;
497 const struct lttng_condition *condition = NULL;
498 const struct lttng_event_rule *event_rule = NULL;
499
500 LTTNG_ASSERT(event_notifier_rule);
501
502 condition = lttng_trigger_get_const_condition(trigger);
503 LTTNG_ASSERT(condition);
504
505 condition_type = lttng_condition_get_type(condition);
506 LTTNG_ASSERT(condition_type == LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
507
508 condition_status = lttng_condition_event_rule_matches_get_rule(
509 condition, &event_rule);
510 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
511 LTTNG_ASSERT(event_rule);
512
513 event_rule_type = lttng_event_rule_get_type(event_rule);
514 LTTNG_ASSERT(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN);
515
516 local_kernel_token_event_rule =
517 (ltt_kernel_event_notifier_rule *) zmalloc(sizeof(struct ltt_kernel_event_notifier_rule));
518 if (local_kernel_token_event_rule == NULL) {
519 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
520 ret = LTTNG_ERR_NOMEM;
521 goto error;
522 }
523
524 local_kernel_token_event_rule->fd = -1;
525 local_kernel_token_event_rule->enabled = 1;
526 local_kernel_token_event_rule->token = token;
527 local_kernel_token_event_rule->error_counter_index = error_counter_index;
528
529 /* Get the reference of the event rule. */
530 lttng_trigger_get(trigger);
531
532 local_kernel_token_event_rule->trigger = trigger;
533 /* The event rule still owns the filter and bytecode. */
534 local_kernel_token_event_rule->filter =
535 lttng_event_rule_get_filter_bytecode(event_rule);
536
537 DBG3("Created kernel event notifier rule: token = %" PRIu64,
538 local_kernel_token_event_rule->token);
539 error:
540 *event_notifier_rule = local_kernel_token_event_rule;
541 return ret;
542 }
543
544 /*
545 * Initialize a kernel trigger from an event rule.
546 */
547 enum lttng_error_code trace_kernel_init_event_notifier_from_event_rule(
548 const struct lttng_event_rule *rule,
549 struct lttng_kernel_abi_event_notifier *kernel_event_notifier)
550 {
551 enum lttng_error_code ret_code;
552 const char *name;
553 int strncpy_ret;
554
555 switch (lttng_event_rule_get_type(rule)) {
556 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
557 {
558 uint64_t address = 0, offset = 0;
559 const char *symbol_name = NULL;
560 const struct lttng_kernel_probe_location *location = NULL;
561 enum lttng_kernel_probe_location_status k_status;
562 enum lttng_event_rule_status status;
563
564 status = lttng_event_rule_kernel_kprobe_get_location(rule, &location);
565 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
566 ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL;
567 goto error;
568 }
569
570 switch (lttng_kernel_probe_location_get_type(location)) {
571 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS:
572 {
573 k_status = lttng_kernel_probe_location_address_get_address(
574 location, &address);
575 LTTNG_ASSERT(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK);
576 break;
577 }
578 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET:
579 {
580 k_status = lttng_kernel_probe_location_symbol_get_offset(
581 location, &offset);
582 LTTNG_ASSERT(k_status == LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK);
583 symbol_name = lttng_kernel_probe_location_symbol_get_name(
584 location);
585 break;
586 }
587 default:
588 abort();
589 }
590
591 kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_ABI_KPROBE;
592 kernel_event_notifier->event.u.kprobe.addr = address;
593 kernel_event_notifier->event.u.kprobe.offset = offset;
594 if (symbol_name) {
595 strncpy_ret = lttng_strncpy(
596 kernel_event_notifier->event.u.kprobe.symbol_name,
597 symbol_name,
598 sizeof(kernel_event_notifier->event.u.kprobe.symbol_name));
599
600 if (strncpy_ret) {
601 ret_code = LTTNG_ERR_INVALID;
602 goto error;
603 }
604 }
605
606 kernel_event_notifier->event.u.kprobe.symbol_name[LTTNG_KERNEL_ABI_SYM_NAME_LEN - 1] = '\0';
607
608 status = lttng_event_rule_kernel_kprobe_get_event_name(rule, &name);
609 LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK);
610 ret_code = LTTNG_OK;
611 break;
612 }
613 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
614 {
615 const struct lttng_userspace_probe_location* location = NULL;
616 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
617 enum lttng_event_rule_status status;
618
619 status = lttng_event_rule_kernel_uprobe_get_location(rule, &location);
620 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
621 ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL;
622 goto error;
623 }
624
625 kernel_event_notifier->event.instrumentation = LTTNG_KERNEL_ABI_UPROBE;
626
627 lookup = lttng_userspace_probe_location_get_lookup_method(
628 location);
629 if (!lookup) {
630 ret_code = LTTNG_ERR_PROBE_LOCATION_INVAL;
631 goto error;
632 }
633
634 /*
635 * From the kernel tracer's perspective, all userspace probe
636 * event types are all the same: a file and an offset.
637 */
638 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) {
639 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
640 /* Get the file descriptor on the target binary. */
641 kernel_event_notifier->event.u.uprobe.fd =
642 lttng_userspace_probe_location_function_get_binary_fd(location);
643
644 break;
645 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
646 /* Get the file descriptor on the target binary. */
647 kernel_event_notifier->event.u.uprobe.fd =
648 lttng_userspace_probe_location_tracepoint_get_binary_fd(location);
649 break;
650 default:
651 abort();
652 }
653
654 status = lttng_event_rule_kernel_uprobe_get_event_name(
655 rule, &name);
656 LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK);
657 ret_code = LTTNG_OK;
658 break;
659 }
660 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
661 {
662 const enum lttng_event_rule_status status =
663 lttng_event_rule_kernel_tracepoint_get_name_pattern(
664 rule, &name);
665
666 LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK);
667 kernel_event_notifier->event.instrumentation =
668 LTTNG_KERNEL_ABI_TRACEPOINT;
669
670 ret_code = LTTNG_OK;
671 break;
672 }
673 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
674 {
675 const enum lttng_event_rule_status status =
676 lttng_event_rule_kernel_syscall_get_name_pattern(
677 rule, &name);
678 const enum lttng_event_rule_kernel_syscall_emission_site
679 emission_site =
680 lttng_event_rule_kernel_syscall_get_emission_site(rule);
681 enum lttng_kernel_abi_syscall_entryexit entryexit;
682
683 LTTNG_ASSERT(status == LTTNG_EVENT_RULE_STATUS_OK);
684 LTTNG_ASSERT(emission_site != LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN);
685
686 switch(emission_site) {
687 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY:
688 entryexit = LTTNG_KERNEL_ABI_SYSCALL_ENTRY;
689 break;
690 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT:
691 entryexit = LTTNG_KERNEL_ABI_SYSCALL_EXIT;
692 break;
693 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT:
694 entryexit = LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT;
695 break;
696 default:
697 abort();
698 break;
699 }
700
701 kernel_event_notifier->event.instrumentation =
702 LTTNG_KERNEL_ABI_SYSCALL;
703 kernel_event_notifier->event.u.syscall.abi =
704 LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL;
705 kernel_event_notifier->event.u.syscall.entryexit =
706 entryexit;
707 kernel_event_notifier->event.u.syscall.match =
708 LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME;
709 ret_code = LTTNG_OK;
710 break;
711 }
712 default:
713 abort();
714 break;
715 }
716
717 strncpy_ret = lttng_strncpy(kernel_event_notifier->event.name, name,
718 LTTNG_KERNEL_ABI_SYM_NAME_LEN);
719 if (strncpy_ret) {
720 ret_code = LTTNG_ERR_INVALID;
721 goto error;
722 }
723
724 error:
725 return ret_code;
726 }
727 /*
728 * Allocate and initialize a kernel metadata.
729 *
730 * Return pointer to structure or NULL.
731 */
732 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
733 {
734 int ret;
735 struct ltt_kernel_metadata *lkm;
736 struct lttng_channel *chan;
737
738 lkm = (ltt_kernel_metadata *) zmalloc(sizeof(struct ltt_kernel_metadata));
739 chan = (lttng_channel *) zmalloc(sizeof(struct lttng_channel));
740 if (lkm == NULL || chan == NULL) {
741 PERROR("kernel metadata zmalloc");
742 goto error;
743 }
744
745 ret = lttng_strncpy(
746 chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name));
747 if (ret) {
748 ERR("Failed to initialize metadata channel name to `%s`",
749 DEFAULT_METADATA_NAME);
750 goto error;
751 }
752
753 /* Set default attributes */
754 chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE;
755 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
756 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
757 chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER;
758 chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;;
759
760
761 /*
762 * The metadata channel of kernel sessions must use the "mmap"
763 * back-end since the consumer daemon accumulates complete
764 * metadata units before sending them to the relay daemon in
765 * live mode. The consumer daemon also needs to extract the contents
766 * of the metadata cache when computing a rotation position.
767 *
768 * In both cases, it is not possible to rely on the splice
769 * back-end as the consumer daemon may need to accumulate more
770 * content than can be backed by the ring buffer's underlying
771 * pages.
772 */
773 chan->attr.output = LTTNG_EVENT_MMAP;
774 chan->attr.tracefile_size = 0;
775 chan->attr.tracefile_count = 0;
776 chan->attr.live_timer_interval = 0;
777
778 /* Init metadata */
779 lkm->fd = -1;
780 lkm->conf = chan;
781
782 return lkm;
783
784 error:
785 free(lkm);
786 free(chan);
787 return NULL;
788 }
789
790 /*
791 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
792 * default.
793 *
794 * Return pointer to structure or NULL.
795 */
796 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
797 unsigned int count)
798 {
799 int ret;
800 struct ltt_kernel_stream *lks;
801
802 LTTNG_ASSERT(name);
803
804 lks = (ltt_kernel_stream *) zmalloc(sizeof(struct ltt_kernel_stream));
805 if (lks == NULL) {
806 PERROR("kernel stream zmalloc");
807 goto error;
808 }
809
810 /* Set name */
811 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
812 if (ret < 0) {
813 PERROR("snprintf stream name");
814 goto error;
815 }
816 lks->name[sizeof(lks->name) - 1] = '\0';
817
818 /* Init stream */
819 lks->fd = -1;
820 lks->state = 0;
821 lks->cpu = count;
822
823 return lks;
824
825 error:
826 return NULL;
827 }
828
829 /*
830 * Cleanup kernel stream structure.
831 */
832 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
833 {
834 LTTNG_ASSERT(stream);
835
836 DBG("[trace] Closing stream fd %d", stream->fd);
837 /* Close kernel fd */
838 if (stream->fd >= 0) {
839 int ret;
840
841 ret = close(stream->fd);
842 if (ret) {
843 PERROR("close");
844 }
845 }
846 /* Remove from stream list */
847 cds_list_del(&stream->list);
848
849 free(stream);
850 }
851
852 /*
853 * Cleanup kernel event structure.
854 */
855 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
856 {
857 LTTNG_ASSERT(event);
858
859 if (event->fd >= 0) {
860 int ret;
861
862 DBG("[trace] Closing event fd %d", event->fd);
863 /* Close kernel fd */
864 ret = close(event->fd);
865 if (ret) {
866 PERROR("close");
867 }
868 } else {
869 DBG("[trace] Tearing down event (no associated file descriptor)");
870 }
871
872 /* Remove from event list */
873 cds_list_del(&event->list);
874
875 free(event->filter_expression);
876 free(event->filter);
877
878 free(event->event);
879 free(event);
880 }
881
882 /*
883 * Cleanup kernel event structure.
884 */
885 static void free_token_event_rule_rcu(struct rcu_head *rcu_node)
886 {
887 struct ltt_kernel_event_notifier_rule *rule = caa_container_of(rcu_node,
888 struct ltt_kernel_event_notifier_rule, rcu_node);
889
890 free(rule);
891 }
892
893 void trace_kernel_destroy_event_notifier_rule(
894 struct ltt_kernel_event_notifier_rule *event)
895 {
896 LTTNG_ASSERT(event);
897
898 if (event->fd >= 0) {
899 const int ret = close(event->fd);
900
901 DBG("Closing kernel event notifier rule file descriptor: fd = %d",
902 event->fd);
903 if (ret) {
904 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
905 event->fd);
906 }
907 } else {
908 DBG("Destroying kernel event notifier rule (no associated file descriptor)");
909 }
910
911 lttng_trigger_put(event->trigger);
912 call_rcu(&event->rcu_node, free_token_event_rule_rcu);
913 }
914 /*
915 * Cleanup kernel context structure.
916 */
917 void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
918 {
919 LTTNG_ASSERT(ctx);
920
921 if (ctx->in_list) {
922 cds_list_del(&ctx->list);
923 }
924 free(ctx);
925 }
926
927 /*
928 * Cleanup kernel channel structure.
929 */
930 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
931 {
932 struct ltt_kernel_stream *stream, *stmp;
933 struct ltt_kernel_event *event, *etmp;
934 struct ltt_kernel_context *ctx, *ctmp;
935 int ret;
936 enum lttng_error_code status;
937
938 LTTNG_ASSERT(channel);
939
940 DBG("[trace] Closing channel fd %d", channel->fd);
941 /* Close kernel fd */
942 if (channel->fd >= 0) {
943 ret = close(channel->fd);
944 if (ret) {
945 PERROR("close");
946 }
947 }
948
949 /* For each stream in the channel list */
950 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
951 trace_kernel_destroy_stream(stream);
952 }
953
954 /* For each event in the channel list */
955 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
956 trace_kernel_destroy_event(event);
957 }
958
959 /* For each context in the channel list */
960 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
961 trace_kernel_destroy_context(ctx);
962 }
963
964 /* Remove from channel list */
965 cds_list_del(&channel->list);
966
967 if (the_notification_thread_handle &&
968 channel->published_to_notification_thread) {
969 status = notification_thread_command_remove_channel(
970 the_notification_thread_handle, channel->key,
971 LTTNG_DOMAIN_KERNEL);
972 LTTNG_ASSERT(status == LTTNG_OK);
973 }
974 free(channel->channel->attr.extended.ptr);
975 free(channel->channel);
976 free(channel);
977 }
978
979 /*
980 * Cleanup kernel metadata structure.
981 */
982 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
983 {
984 LTTNG_ASSERT(metadata);
985
986 DBG("[trace] Closing metadata fd %d", metadata->fd);
987 /* Close kernel fd */
988 if (metadata->fd >= 0) {
989 int ret;
990
991 ret = close(metadata->fd);
992 if (ret) {
993 PERROR("close");
994 }
995 }
996
997 free(metadata->conf);
998 free(metadata);
999 }
1000
1001 /*
1002 * Cleanup kernel session structure
1003 */
1004 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
1005 {
1006 struct ltt_kernel_channel *channel, *ctmp;
1007 int ret;
1008
1009 LTTNG_ASSERT(session);
1010
1011 DBG("[trace] Closing session fd %d", session->fd);
1012 /* Close kernel fds */
1013 if (session->fd >= 0) {
1014 ret = close(session->fd);
1015 if (ret) {
1016 PERROR("close");
1017 }
1018 }
1019
1020 if (session->metadata_stream_fd >= 0) {
1021 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
1022 ret = close(session->metadata_stream_fd);
1023 if (ret) {
1024 PERROR("close");
1025 }
1026 }
1027
1028 if (session->metadata != NULL) {
1029 trace_kernel_destroy_metadata(session->metadata);
1030 }
1031
1032 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
1033 trace_kernel_destroy_channel(channel);
1034 }
1035 }
1036
1037 /* Free elements needed by destroy notifiers. */
1038 void trace_kernel_free_session(struct ltt_kernel_session *session)
1039 {
1040 /* Wipe consumer output object */
1041 consumer_output_put(session->consumer);
1042
1043 process_attr_tracker_destroy(session->tracker_pid);
1044 process_attr_tracker_destroy(session->tracker_vpid);
1045 process_attr_tracker_destroy(session->tracker_uid);
1046 process_attr_tracker_destroy(session->tracker_vuid);
1047 process_attr_tracker_destroy(session->tracker_gid);
1048 process_attr_tracker_destroy(session->tracker_vgid);
1049
1050 free(session);
1051 }
This page took 0.051916 seconds and 5 git commands to generate.