Commit | Line | Data |
---|---|---|
54012638 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
54012638 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
54012638 | 5 | * |
54012638 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
54012638 DG |
9 | #include <stdio.h> |
10 | #include <stdlib.h> | |
11 | #include <string.h> | |
c363b55d | 12 | #include <unistd.h> |
54012638 | 13 | |
dcabc190 FD |
14 | #include <lttng/event.h> |
15 | #include <lttng/lttng-error.h> | |
16 | #include <lttng/userspace-probe.h> | |
17 | #include <lttng/userspace-probe-internal.h> | |
18 | ||
990570ed DG |
19 | #include <common/common.h> |
20 | #include <common/defaults.h> | |
82b69413 | 21 | #include <common/trace-chunk.h> |
70513913 | 22 | #include <common/macros.h> |
1e307fab | 23 | |
00e2e675 | 24 | #include "consumer.h" |
62499ad6 | 25 | #include "trace-kernel.h" |
e9404c27 JG |
26 | #include "lttng-sessiond.h" |
27 | #include "notification-thread-commands.h" | |
54012638 | 28 | |
19e70852 | 29 | /* |
050349bb | 30 | * Find the channel name for the given kernel session. |
19e70852 | 31 | */ |
62499ad6 | 32 | struct ltt_kernel_channel *trace_kernel_get_channel_by_name( |
df4f5a87 | 33 | const char *name, struct ltt_kernel_session *session) |
19e70852 DG |
34 | { |
35 | struct ltt_kernel_channel *chan; | |
36 | ||
0525e9ae DG |
37 | assert(session); |
38 | assert(name); | |
19e70852 | 39 | |
85076754 MD |
40 | /* |
41 | * If we receive an empty string for channel name, it means the | |
42 | * default channel name is requested. | |
43 | */ | |
44 | if (name[0] == '\0') | |
45 | name = DEFAULT_CHANNEL_NAME; | |
46 | ||
54d01ffb DG |
47 | DBG("Trying to find channel %s", name); |
48 | ||
19e70852 DG |
49 | cds_list_for_each_entry(chan, &session->channel_list.head, list) { |
50 | if (strcmp(name, chan->channel->name) == 0) { | |
51 | DBG("Found channel by name %s", name); | |
52 | return chan; | |
53 | } | |
54 | } | |
55 | ||
19e70852 DG |
56 | return NULL; |
57 | } | |
58 | ||
00a62084 MD |
59 | /* |
60 | * Find the event for the given channel. | |
61 | */ | |
62 | struct ltt_kernel_event *trace_kernel_find_event( | |
63 | char *name, struct ltt_kernel_channel *channel, | |
64 | enum lttng_event_type type, | |
65 | struct lttng_filter_bytecode *filter) | |
66 | { | |
67 | struct ltt_kernel_event *ev; | |
68 | int found = 0; | |
69 | ||
70 | assert(name); | |
71 | assert(channel); | |
72 | ||
73 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { | |
74 | if (type != LTTNG_EVENT_ALL && ev->type != type) { | |
75 | continue; | |
76 | } | |
77 | if (strcmp(name, ev->event->name)) { | |
78 | continue; | |
79 | } | |
80 | if ((ev->filter && !filter) || (!ev->filter && filter)) { | |
81 | continue; | |
82 | } | |
83 | if (ev->filter && filter) { | |
84 | if (ev->filter->len != filter->len || | |
85 | memcmp(ev->filter->data, filter->data, | |
86 | filter->len) != 0) { | |
87 | continue; | |
88 | } | |
89 | } | |
90 | found = 1; | |
91 | break; | |
92 | } | |
93 | if (found) { | |
94 | DBG("Found event %s for channel %s", name, | |
95 | channel->channel->name); | |
96 | return ev; | |
97 | } else { | |
98 | return NULL; | |
99 | } | |
100 | } | |
101 | ||
19e70852 | 102 | /* |
050349bb | 103 | * Find the event name for the given channel. |
19e70852 | 104 | */ |
62499ad6 | 105 | struct ltt_kernel_event *trace_kernel_get_event_by_name( |
d0ae4ea8 MD |
106 | char *name, struct ltt_kernel_channel *channel, |
107 | enum lttng_event_type type) | |
19e70852 DG |
108 | { |
109 | struct ltt_kernel_event *ev; | |
00a62084 | 110 | int found = 0; |
19e70852 | 111 | |
0525e9ae DG |
112 | assert(name); |
113 | assert(channel); | |
19e70852 DG |
114 | |
115 | cds_list_for_each_entry(ev, &channel->events_list.head, list) { | |
00a62084 | 116 | if (type != LTTNG_EVENT_ALL && ev->type != type) { |
d0ae4ea8 | 117 | continue; |
19e70852 | 118 | } |
00a62084 MD |
119 | if (strcmp(name, ev->event->name)) { |
120 | continue; | |
121 | } | |
122 | found = 1; | |
123 | break; | |
124 | } | |
125 | if (found) { | |
126 | DBG("Found event %s for channel %s", name, | |
127 | channel->channel->name); | |
128 | return ev; | |
129 | } else { | |
130 | return NULL; | |
19e70852 | 131 | } |
19e70852 DG |
132 | } |
133 | ||
54012638 | 134 | /* |
050349bb | 135 | * Allocate and initialize a kernel session data structure. |
54012638 | 136 | * |
050349bb | 137 | * Return pointer to structure or NULL. |
54012638 | 138 | */ |
dec56f6c | 139 | struct ltt_kernel_session *trace_kernel_create_session(void) |
54012638 | 140 | { |
a4b92340 | 141 | struct ltt_kernel_session *lks = NULL; |
54012638 DG |
142 | |
143 | /* Allocate a new ltt kernel session */ | |
ba7f0ae5 | 144 | lks = zmalloc(sizeof(struct ltt_kernel_session)); |
54012638 | 145 | if (lks == NULL) { |
df0f840b | 146 | PERROR("create kernel session zmalloc"); |
a4b92340 | 147 | goto alloc_error; |
54012638 DG |
148 | } |
149 | ||
150 | /* Init data structure */ | |
03550b58 MD |
151 | lks->fd = -1; |
152 | lks->metadata_stream_fd = -1; | |
54012638 DG |
153 | lks->channel_count = 0; |
154 | lks->stream_count_global = 0; | |
155 | lks->metadata = NULL; | |
156 | CDS_INIT_LIST_HEAD(&lks->channel_list.head); | |
157 | ||
f953b297 JG |
158 | lks->tracker_pid = process_attr_tracker_create(); |
159 | if (!lks->tracker_pid) { | |
55c9e7ca JR |
160 | goto error; |
161 | } | |
f953b297 JG |
162 | lks->tracker_vpid = process_attr_tracker_create(); |
163 | if (!lks->tracker_vpid) { | |
55c9e7ca JR |
164 | goto error; |
165 | } | |
f953b297 JG |
166 | lks->tracker_uid = process_attr_tracker_create(); |
167 | if (!lks->tracker_uid) { | |
55c9e7ca JR |
168 | goto error; |
169 | } | |
f953b297 JG |
170 | lks->tracker_vuid = process_attr_tracker_create(); |
171 | if (!lks->tracker_vuid) { | |
55c9e7ca JR |
172 | goto error; |
173 | } | |
f953b297 JG |
174 | lks->tracker_gid = process_attr_tracker_create(); |
175 | if (!lks->tracker_gid) { | |
55c9e7ca JR |
176 | goto error; |
177 | } | |
f953b297 JG |
178 | lks->tracker_vgid = process_attr_tracker_create(); |
179 | if (!lks->tracker_vgid) { | |
55c9e7ca JR |
180 | goto error; |
181 | } | |
00e2e675 DG |
182 | lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL); |
183 | if (lks->consumer == NULL) { | |
184 | goto error; | |
185 | } | |
186 | ||
54012638 DG |
187 | return lks; |
188 | ||
189 | error: | |
f953b297 JG |
190 | process_attr_tracker_destroy(lks->tracker_pid); |
191 | process_attr_tracker_destroy(lks->tracker_vpid); | |
192 | process_attr_tracker_destroy(lks->tracker_uid); | |
193 | process_attr_tracker_destroy(lks->tracker_vuid); | |
194 | process_attr_tracker_destroy(lks->tracker_gid); | |
195 | process_attr_tracker_destroy(lks->tracker_vgid); | |
a4b92340 DG |
196 | free(lks); |
197 | ||
198 | alloc_error: | |
54012638 DG |
199 | return NULL; |
200 | } | |
201 | ||
202 | /* | |
050349bb | 203 | * Allocate and initialize a kernel channel data structure. |
54012638 | 204 | * |
050349bb | 205 | * Return pointer to structure or NULL. |
54012638 | 206 | */ |
00e2e675 | 207 | struct ltt_kernel_channel *trace_kernel_create_channel( |
fdd9eb17 | 208 | struct lttng_channel *chan) |
54012638 | 209 | { |
54012638 | 210 | struct ltt_kernel_channel *lkc; |
61a5b6b1 | 211 | struct lttng_channel_extended *extended = NULL; |
54012638 | 212 | |
0525e9ae DG |
213 | assert(chan); |
214 | ||
ba7f0ae5 | 215 | lkc = zmalloc(sizeof(struct ltt_kernel_channel)); |
f3ed775e | 216 | if (lkc == NULL) { |
df0f840b | 217 | PERROR("ltt_kernel_channel zmalloc"); |
54012638 DG |
218 | goto error; |
219 | } | |
220 | ||
ba7f0ae5 | 221 | lkc->channel = zmalloc(sizeof(struct lttng_channel)); |
f3ed775e | 222 | if (lkc->channel == NULL) { |
df0f840b | 223 | PERROR("lttng_channel zmalloc"); |
e9404c27 JG |
224 | goto error; |
225 | } | |
226 | ||
227 | extended = zmalloc(sizeof(struct lttng_channel_extended)); | |
228 | if (!extended) { | |
229 | PERROR("lttng_channel_channel zmalloc"); | |
f3ed775e DG |
230 | goto error; |
231 | } | |
232 | memcpy(lkc->channel, chan, sizeof(struct lttng_channel)); | |
e9404c27 JG |
233 | memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended)); |
234 | lkc->channel->attr.extended.ptr = extended; | |
235 | extended = NULL; | |
54012638 | 236 | |
85076754 MD |
237 | /* |
238 | * If we receive an empty string for channel name, it means the | |
239 | * default channel name is requested. | |
240 | */ | |
241 | if (chan->name[0] == '\0') { | |
242 | strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME, | |
243 | sizeof(lkc->channel->name)); | |
244 | } | |
bd722d76 | 245 | lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; |
85076754 | 246 | |
03550b58 | 247 | lkc->fd = -1; |
54012638 | 248 | lkc->stream_count = 0; |
cbbbb275 | 249 | lkc->event_count = 0; |
d36b8583 | 250 | lkc->enabled = 1; |
753873bf | 251 | lkc->published_to_notification_thread = false; |
54012638 DG |
252 | /* Init linked list */ |
253 | CDS_INIT_LIST_HEAD(&lkc->events_list.head); | |
254 | CDS_INIT_LIST_HEAD(&lkc->stream_list.head); | |
645328ae | 255 | CDS_INIT_LIST_HEAD(&lkc->ctx_list); |
54012638 DG |
256 | |
257 | return lkc; | |
258 | ||
259 | error: | |
e9404c27 JG |
260 | if (lkc) { |
261 | free(lkc->channel); | |
262 | } | |
263 | free(extended); | |
264 | free(lkc); | |
54012638 DG |
265 | return NULL; |
266 | } | |
267 | ||
645328ae DG |
268 | /* |
269 | * Allocate and init a kernel context object. | |
270 | * | |
271 | * Return the allocated object or NULL on error. | |
272 | */ | |
273 | struct ltt_kernel_context *trace_kernel_create_context( | |
274 | struct lttng_kernel_context *ctx) | |
275 | { | |
276 | struct ltt_kernel_context *kctx; | |
277 | ||
278 | kctx = zmalloc(sizeof(*kctx)); | |
279 | if (!kctx) { | |
280 | PERROR("zmalloc kernel context"); | |
281 | goto error; | |
282 | } | |
283 | ||
284 | if (ctx) { | |
285 | memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx)); | |
286 | } | |
df3c77c8 JG |
287 | error: |
288 | return kctx; | |
289 | } | |
645328ae | 290 | |
df3c77c8 JG |
291 | /* |
292 | * Allocate and init a kernel context object from an existing kernel context | |
293 | * object. | |
294 | * | |
295 | * Return the allocated object or NULL on error. | |
296 | */ | |
297 | struct ltt_kernel_context *trace_kernel_copy_context( | |
298 | struct ltt_kernel_context *kctx) | |
299 | { | |
300 | struct ltt_kernel_context *kctx_copy; | |
301 | ||
302 | assert(kctx); | |
303 | kctx_copy = zmalloc(sizeof(*kctx_copy)); | |
304 | if (!kctx_copy) { | |
305 | PERROR("zmalloc ltt_kernel_context"); | |
306 | goto error; | |
307 | } | |
308 | ||
309 | memcpy(kctx_copy, kctx, sizeof(*kctx_copy)); | |
310 | memset(&kctx_copy->list, 0, sizeof(kctx_copy->list)); | |
7b9445b3 | 311 | |
645328ae | 312 | error: |
df3c77c8 | 313 | return kctx_copy; |
645328ae DG |
314 | } |
315 | ||
54012638 | 316 | /* |
050349bb | 317 | * Allocate and initialize a kernel event. Set name and event type. |
a969e101 | 318 | * We own filter_expression, and filter. |
54012638 | 319 | * |
050349bb | 320 | * Return pointer to structure or NULL. |
54012638 | 321 | */ |
71a3bb01 FD |
322 | enum lttng_error_code trace_kernel_create_event( |
323 | struct lttng_event *ev, char *filter_expression, | |
324 | struct lttng_filter_bytecode *filter, | |
325 | struct ltt_kernel_event **kernel_event) | |
54012638 | 326 | { |
71a3bb01 | 327 | enum lttng_error_code ret; |
54012638 | 328 | struct lttng_kernel_event *attr; |
71a3bb01 | 329 | struct ltt_kernel_event *local_kernel_event; |
dcabc190 | 330 | struct lttng_userspace_probe_location *userspace_probe_location = NULL; |
54012638 | 331 | |
0525e9ae DG |
332 | assert(ev); |
333 | ||
71a3bb01 | 334 | local_kernel_event = zmalloc(sizeof(struct ltt_kernel_event)); |
ba7f0ae5 | 335 | attr = zmalloc(sizeof(struct lttng_kernel_event)); |
71a3bb01 | 336 | if (local_kernel_event == NULL || attr == NULL) { |
df0f840b | 337 | PERROR("kernel event zmalloc"); |
71a3bb01 | 338 | ret = LTTNG_ERR_NOMEM; |
54012638 DG |
339 | goto error; |
340 | } | |
341 | ||
f3ed775e | 342 | switch (ev->type) { |
7d29a247 | 343 | case LTTNG_EVENT_PROBE: |
e6ddca71 | 344 | attr->instrumentation = LTTNG_KERNEL_KPROBE; |
7d29a247 DG |
345 | attr->u.kprobe.addr = ev->attr.probe.addr; |
346 | attr->u.kprobe.offset = ev->attr.probe.offset; | |
f3ed775e | 347 | strncpy(attr->u.kprobe.symbol_name, |
dbbb3ec5 DG |
348 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
349 | attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e | 350 | break; |
dcabc190 FD |
351 | case LTTNG_EVENT_USERSPACE_PROBE: |
352 | { | |
87597c2c JG |
353 | const struct lttng_userspace_probe_location* location = NULL; |
354 | const struct lttng_userspace_probe_location_lookup_method *lookup = NULL; | |
dcabc190 FD |
355 | |
356 | location = lttng_event_get_userspace_probe_location(ev); | |
357 | if (!location) { | |
358 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
359 | goto error; | |
360 | } | |
361 | ||
362 | /* | |
363 | * From this point on, the specific term 'uprobe' is used | |
364 | * instead of the generic 'userspace probe' because it's the | |
365 | * technology used at the moment for this instrumentation. | |
366 | * LTTng currently implements userspace probes using uprobes. | |
367 | * In the interactions with the kernel tracer, we use the | |
368 | * uprobe term. | |
369 | */ | |
370 | attr->instrumentation = LTTNG_KERNEL_UPROBE; | |
371 | ||
372 | /* | |
373 | * Only the elf lookup method is supported at the moment. | |
374 | */ | |
375 | lookup = lttng_userspace_probe_location_get_lookup_method( | |
376 | location); | |
377 | if (!lookup) { | |
378 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
379 | goto error; | |
380 | } | |
381 | ||
382 | /* | |
383 | * From the kernel tracer's perspective, all userspace probe | |
384 | * event types are all the same: a file and an offset. | |
385 | */ | |
386 | switch (lttng_userspace_probe_location_lookup_method_get_type(lookup)) { | |
387 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF: | |
388 | /* Get the file descriptor on the target binary. */ | |
389 | attr->u.uprobe.fd = | |
390 | lttng_userspace_probe_location_function_get_binary_fd(location); | |
391 | ||
392 | /* | |
393 | * Save a reference to the probe location used during | |
394 | * the listing of events. Close its FD since it won't | |
395 | * be needed for listing. | |
396 | */ | |
397 | userspace_probe_location = | |
398 | lttng_userspace_probe_location_copy(location); | |
399 | ret = lttng_userspace_probe_location_function_set_binary_fd( | |
400 | userspace_probe_location, -1); | |
401 | if (ret) { | |
402 | goto error; | |
403 | } | |
404 | break; | |
405 | case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT: | |
406 | /* Get the file descriptor on the target binary. */ | |
407 | attr->u.uprobe.fd = | |
408 | lttng_userspace_probe_location_tracepoint_get_binary_fd(location); | |
409 | ||
410 | /* | |
411 | * Save a reference to the probe location used during the listing of | |
412 | * events. Close its FD since it won't be needed for listing. | |
413 | */ | |
414 | userspace_probe_location = | |
415 | lttng_userspace_probe_location_copy(location); | |
416 | ret = lttng_userspace_probe_location_tracepoint_set_binary_fd( | |
417 | userspace_probe_location, -1); | |
418 | if (ret) { | |
419 | goto error; | |
420 | } | |
421 | break; | |
422 | default: | |
423 | DBG("Unsupported lookup method type"); | |
424 | ret = LTTNG_ERR_PROBE_LOCATION_INVAL; | |
425 | goto error; | |
426 | } | |
427 | break; | |
428 | } | |
f3ed775e | 429 | case LTTNG_EVENT_FUNCTION: |
8f0d098b MD |
430 | attr->instrumentation = LTTNG_KERNEL_KRETPROBE; |
431 | attr->u.kretprobe.addr = ev->attr.probe.addr; | |
432 | attr->u.kretprobe.offset = ev->attr.probe.offset; | |
8f0d098b | 433 | strncpy(attr->u.kretprobe.symbol_name, |
dbbb3ec5 DG |
434 | ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
435 | attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
8f0d098b MD |
436 | break; |
437 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
f3ed775e DG |
438 | attr->instrumentation = LTTNG_KERNEL_FUNCTION; |
439 | strncpy(attr->u.ftrace.symbol_name, | |
dbbb3ec5 DG |
440 | ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN); |
441 | attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e | 442 | break; |
e6ddca71 DG |
443 | case LTTNG_EVENT_TRACEPOINT: |
444 | attr->instrumentation = LTTNG_KERNEL_TRACEPOINT; | |
f3ed775e | 445 | break; |
a54bd42d MD |
446 | case LTTNG_EVENT_SYSCALL: |
447 | attr->instrumentation = LTTNG_KERNEL_SYSCALL; | |
0133c199 | 448 | break; |
7a3d1328 MD |
449 | case LTTNG_EVENT_ALL: |
450 | attr->instrumentation = LTTNG_KERNEL_ALL; | |
451 | break; | |
f3ed775e DG |
452 | default: |
453 | ERR("Unknown kernel instrumentation type (%d)", ev->type); | |
71a3bb01 | 454 | ret = LTTNG_ERR_INVALID; |
f3ed775e DG |
455 | goto error; |
456 | } | |
457 | ||
458 | /* Copy event name */ | |
dbbb3ec5 DG |
459 | strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN); |
460 | attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0'; | |
f3ed775e | 461 | |
54012638 | 462 | /* Setting up a kernel event */ |
71a3bb01 FD |
463 | local_kernel_event->fd = -1; |
464 | local_kernel_event->event = attr; | |
465 | local_kernel_event->enabled = 1; | |
466 | local_kernel_event->filter_expression = filter_expression; | |
467 | local_kernel_event->filter = filter; | |
dcabc190 | 468 | local_kernel_event->userspace_probe_location = userspace_probe_location; |
54012638 | 469 | |
71a3bb01 FD |
470 | *kernel_event = local_kernel_event; |
471 | ||
472 | return LTTNG_OK; | |
54012638 DG |
473 | |
474 | error: | |
a969e101 MD |
475 | free(filter_expression); |
476 | free(filter); | |
71a3bb01 | 477 | free(local_kernel_event); |
a2c0da86 | 478 | free(attr); |
71a3bb01 | 479 | return ret; |
54012638 DG |
480 | } |
481 | ||
482 | /* | |
050349bb | 483 | * Allocate and initialize a kernel metadata. |
54012638 | 484 | * |
050349bb | 485 | * Return pointer to structure or NULL. |
54012638 | 486 | */ |
a4b92340 | 487 | struct ltt_kernel_metadata *trace_kernel_create_metadata(void) |
54012638 | 488 | { |
70513913 | 489 | int ret; |
54012638 | 490 | struct ltt_kernel_metadata *lkm; |
f3ed775e | 491 | struct lttng_channel *chan; |
54012638 | 492 | |
ba7f0ae5 DG |
493 | lkm = zmalloc(sizeof(struct ltt_kernel_metadata)); |
494 | chan = zmalloc(sizeof(struct lttng_channel)); | |
f3ed775e | 495 | if (lkm == NULL || chan == NULL) { |
df0f840b | 496 | PERROR("kernel metadata zmalloc"); |
54012638 DG |
497 | goto error; |
498 | } | |
499 | ||
70513913 JG |
500 | ret = lttng_strncpy( |
501 | chan->name, DEFAULT_METADATA_NAME, sizeof(chan->name)); | |
502 | if (ret) { | |
503 | ERR("Failed to initialize metadata channel name to `%s`", | |
504 | DEFAULT_METADATA_NAME); | |
505 | goto error; | |
506 | } | |
507 | ||
54012638 | 508 | /* Set default attributes */ |
70513913 | 509 | chan->attr.overwrite = DEFAULT_METADATA_OVERWRITE; |
3e230f92 | 510 | chan->attr.subbuf_size = default_get_metadata_subbuf_size(); |
b389abbe | 511 | chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; |
70513913 JG |
512 | chan->attr.switch_timer_interval = DEFAULT_METADATA_SWITCH_TIMER; |
513 | chan->attr.read_timer_interval = DEFAULT_METADATA_READ_TIMER;; | |
514 | ||
515 | ||
516 | /* | |
517 | * The metadata channel of kernel sessions must use the "mmap" | |
518 | * back-end since the consumer daemon accumulates complete | |
519 | * metadata units before sending them to the relay daemon in | |
520 | * live mode. The consumer daemon also needs to extract the contents | |
521 | * of the metadata cache when computing a rotation position. | |
522 | * | |
523 | * In both cases, it is not possible to rely on the splice | |
524 | * back-end as the consumer daemon may need to accumulate more | |
525 | * content than can be backed by the ring buffer's underlying | |
526 | * pages. | |
527 | */ | |
528 | chan->attr.output = LTTNG_EVENT_MMAP; | |
529 | chan->attr.tracefile_size = 0; | |
530 | chan->attr.tracefile_count = 0; | |
531 | chan->attr.live_timer_interval = 0; | |
54012638 DG |
532 | |
533 | /* Init metadata */ | |
03550b58 | 534 | lkm->fd = -1; |
f3ed775e | 535 | lkm->conf = chan; |
54012638 DG |
536 | |
537 | return lkm; | |
538 | ||
539 | error: | |
a2c0da86 MD |
540 | free(lkm); |
541 | free(chan); | |
54012638 DG |
542 | return NULL; |
543 | } | |
544 | ||
545 | /* | |
050349bb DG |
546 | * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by |
547 | * default. | |
54012638 | 548 | * |
050349bb | 549 | * Return pointer to structure or NULL. |
54012638 | 550 | */ |
00e2e675 DG |
551 | struct ltt_kernel_stream *trace_kernel_create_stream(const char *name, |
552 | unsigned int count) | |
54012638 | 553 | { |
00e2e675 | 554 | int ret; |
54012638 DG |
555 | struct ltt_kernel_stream *lks; |
556 | ||
0525e9ae DG |
557 | assert(name); |
558 | ||
ba7f0ae5 | 559 | lks = zmalloc(sizeof(struct ltt_kernel_stream)); |
54012638 | 560 | if (lks == NULL) { |
df0f840b | 561 | PERROR("kernel stream zmalloc"); |
54012638 DG |
562 | goto error; |
563 | } | |
564 | ||
00e2e675 | 565 | /* Set name */ |
535b8ff4 | 566 | ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count); |
00e2e675 DG |
567 | if (ret < 0) { |
568 | PERROR("snprintf stream name"); | |
569 | goto error; | |
570 | } | |
571 | lks->name[sizeof(lks->name) - 1] = '\0'; | |
572 | ||
54012638 | 573 | /* Init stream */ |
03550b58 | 574 | lks->fd = -1; |
54012638 | 575 | lks->state = 0; |
ffe60014 | 576 | lks->cpu = count; |
54012638 DG |
577 | |
578 | return lks; | |
579 | ||
580 | error: | |
581 | return NULL; | |
582 | } | |
c363b55d | 583 | |
050349bb DG |
584 | /* |
585 | * Cleanup kernel stream structure. | |
586 | */ | |
62499ad6 | 587 | void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream) |
c363b55d | 588 | { |
0525e9ae DG |
589 | assert(stream); |
590 | ||
33a2b854 | 591 | DBG("[trace] Closing stream fd %d", stream->fd); |
c363b55d | 592 | /* Close kernel fd */ |
03550b58 | 593 | if (stream->fd >= 0) { |
c617c0c6 MD |
594 | int ret; |
595 | ||
03550b58 MD |
596 | ret = close(stream->fd); |
597 | if (ret) { | |
598 | PERROR("close"); | |
599 | } | |
799e2c4f | 600 | } |
c363b55d DG |
601 | /* Remove from stream list */ |
602 | cds_list_del(&stream->list); | |
f9815039 | 603 | |
c363b55d DG |
604 | free(stream); |
605 | } | |
606 | ||
050349bb DG |
607 | /* |
608 | * Cleanup kernel event structure. | |
609 | */ | |
62499ad6 | 610 | void trace_kernel_destroy_event(struct ltt_kernel_event *event) |
c363b55d | 611 | { |
0525e9ae DG |
612 | assert(event); |
613 | ||
87eb4ab8 | 614 | if (event->fd >= 0) { |
c617c0c6 MD |
615 | int ret; |
616 | ||
87eb4ab8 MD |
617 | DBG("[trace] Closing event fd %d", event->fd); |
618 | /* Close kernel fd */ | |
799e2c4f MD |
619 | ret = close(event->fd); |
620 | if (ret) { | |
621 | PERROR("close"); | |
622 | } | |
87eb4ab8 MD |
623 | } else { |
624 | DBG("[trace] Tearing down event (no associated fd)"); | |
625 | } | |
c363b55d DG |
626 | |
627 | /* Remove from event list */ | |
628 | cds_list_del(&event->list); | |
f9815039 | 629 | |
00a62084 MD |
630 | free(event->filter_expression); |
631 | free(event->filter); | |
632 | ||
f9815039 | 633 | free(event->event); |
c363b55d DG |
634 | free(event); |
635 | } | |
636 | ||
645328ae DG |
637 | /* |
638 | * Cleanup kernel context structure. | |
639 | */ | |
640 | void trace_kernel_destroy_context(struct ltt_kernel_context *ctx) | |
641 | { | |
642 | assert(ctx); | |
643 | ||
ba985c3a JG |
644 | if (ctx->in_list) { |
645 | cds_list_del(&ctx->list); | |
646 | } | |
645328ae DG |
647 | free(ctx); |
648 | } | |
649 | ||
050349bb DG |
650 | /* |
651 | * Cleanup kernel channel structure. | |
652 | */ | |
62499ad6 | 653 | void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel) |
c363b55d | 654 | { |
af9737e9 DG |
655 | struct ltt_kernel_stream *stream, *stmp; |
656 | struct ltt_kernel_event *event, *etmp; | |
645328ae | 657 | struct ltt_kernel_context *ctx, *ctmp; |
799e2c4f | 658 | int ret; |
e9404c27 | 659 | enum lttng_error_code status; |
c363b55d | 660 | |
0525e9ae DG |
661 | assert(channel); |
662 | ||
33a2b854 | 663 | DBG("[trace] Closing channel fd %d", channel->fd); |
c363b55d | 664 | /* Close kernel fd */ |
03550b58 MD |
665 | if (channel->fd >= 0) { |
666 | ret = close(channel->fd); | |
667 | if (ret) { | |
668 | PERROR("close"); | |
669 | } | |
799e2c4f | 670 | } |
c363b55d DG |
671 | |
672 | /* For each stream in the channel list */ | |
af9737e9 | 673 | cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) { |
62499ad6 | 674 | trace_kernel_destroy_stream(stream); |
c363b55d DG |
675 | } |
676 | ||
677 | /* For each event in the channel list */ | |
af9737e9 | 678 | cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) { |
62499ad6 | 679 | trace_kernel_destroy_event(event); |
c363b55d DG |
680 | } |
681 | ||
645328ae DG |
682 | /* For each context in the channel list */ |
683 | cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) { | |
684 | trace_kernel_destroy_context(ctx); | |
685 | } | |
686 | ||
c363b55d DG |
687 | /* Remove from channel list */ |
688 | cds_list_del(&channel->list); | |
f9815039 | 689 | |
753873bf JR |
690 | if (notification_thread_handle |
691 | && channel->published_to_notification_thread) { | |
63aaa3dc JG |
692 | status = notification_thread_command_remove_channel( |
693 | notification_thread_handle, | |
e1f3997a | 694 | channel->key, LTTNG_DOMAIN_KERNEL); |
63aaa3dc JG |
695 | assert(status == LTTNG_OK); |
696 | } | |
e9404c27 | 697 | free(channel->channel->attr.extended.ptr); |
f9815039 | 698 | free(channel->channel); |
c363b55d DG |
699 | free(channel); |
700 | } | |
701 | ||
050349bb DG |
702 | /* |
703 | * Cleanup kernel metadata structure. | |
704 | */ | |
62499ad6 | 705 | void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata) |
c363b55d | 706 | { |
0525e9ae DG |
707 | assert(metadata); |
708 | ||
33a2b854 | 709 | DBG("[trace] Closing metadata fd %d", metadata->fd); |
c363b55d | 710 | /* Close kernel fd */ |
03550b58 | 711 | if (metadata->fd >= 0) { |
c617c0c6 MD |
712 | int ret; |
713 | ||
03550b58 MD |
714 | ret = close(metadata->fd); |
715 | if (ret) { | |
716 | PERROR("close"); | |
717 | } | |
799e2c4f | 718 | } |
c363b55d | 719 | |
f9815039 | 720 | free(metadata->conf); |
c363b55d DG |
721 | free(metadata); |
722 | } | |
723 | ||
050349bb | 724 | /* |
62499ad6 | 725 | * Cleanup kernel session structure |
36b588ed MD |
726 | * |
727 | * Should *NOT* be called with RCU read-side lock held. | |
050349bb | 728 | */ |
62499ad6 | 729 | void trace_kernel_destroy_session(struct ltt_kernel_session *session) |
c363b55d | 730 | { |
af9737e9 | 731 | struct ltt_kernel_channel *channel, *ctmp; |
799e2c4f | 732 | int ret; |
c363b55d | 733 | |
0525e9ae DG |
734 | assert(session); |
735 | ||
33a2b854 | 736 | DBG("[trace] Closing session fd %d", session->fd); |
c363b55d | 737 | /* Close kernel fds */ |
03550b58 MD |
738 | if (session->fd >= 0) { |
739 | ret = close(session->fd); | |
740 | if (ret) { | |
741 | PERROR("close"); | |
742 | } | |
799e2c4f | 743 | } |
f9815039 | 744 | |
03550b58 | 745 | if (session->metadata_stream_fd >= 0) { |
70dc1c34 | 746 | DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd); |
799e2c4f MD |
747 | ret = close(session->metadata_stream_fd); |
748 | if (ret) { | |
749 | PERROR("close"); | |
750 | } | |
70dc1c34 | 751 | } |
c363b55d | 752 | |
d36b8583 | 753 | if (session->metadata != NULL) { |
62499ad6 | 754 | trace_kernel_destroy_metadata(session->metadata); |
d36b8583 | 755 | } |
c363b55d | 756 | |
af9737e9 | 757 | cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) { |
62499ad6 | 758 | trace_kernel_destroy_channel(channel); |
c363b55d | 759 | } |
d070c424 | 760 | } |
c363b55d | 761 | |
d070c424 MD |
762 | /* Free elements needed by destroy notifiers. */ |
763 | void trace_kernel_free_session(struct ltt_kernel_session *session) | |
764 | { | |
00e2e675 | 765 | /* Wipe consumer output object */ |
6addfa37 | 766 | consumer_output_put(session->consumer); |
00e2e675 | 767 | |
f953b297 JG |
768 | process_attr_tracker_destroy(session->tracker_pid); |
769 | process_attr_tracker_destroy(session->tracker_vpid); | |
770 | process_attr_tracker_destroy(session->tracker_uid); | |
771 | process_attr_tracker_destroy(session->tracker_vuid); | |
772 | process_attr_tracker_destroy(session->tracker_gid); | |
773 | process_attr_tracker_destroy(session->tracker_vgid); | |
55c9e7ca | 774 | |
c363b55d DG |
775 | free(session); |
776 | } |