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