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