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