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