Fix: Send remove channel to notification thread only when necessary
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
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 *
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.
16 */
17
18 #define _LGPL_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <common/common.h>
25 #include <common/defaults.h>
26
27 #include "consumer.h"
28 #include "trace-kernel.h"
29 #include "lttng-sessiond.h"
30 #include "notification-thread-commands.h"
31
32 /*
33 * Find the channel name for the given kernel session.
34 */
35 struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
36 char *name, struct ltt_kernel_session *session)
37 {
38 struct ltt_kernel_channel *chan;
39
40 assert(session);
41 assert(name);
42
43 /*
44 * If we receive an empty string for channel name, it means the
45 * default channel name is requested.
46 */
47 if (name[0] == '\0')
48 name = DEFAULT_CHANNEL_NAME;
49
50 DBG("Trying to find channel %s", name);
51
52 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
53 if (strcmp(name, chan->channel->name) == 0) {
54 DBG("Found channel by name %s", name);
55 return chan;
56 }
57 }
58
59 return NULL;
60 }
61
62 /*
63 * Find the event for the given channel.
64 */
65 struct ltt_kernel_event *trace_kernel_find_event(
66 char *name, struct ltt_kernel_channel *channel,
67 enum lttng_event_type type,
68 struct lttng_filter_bytecode *filter)
69 {
70 struct ltt_kernel_event *ev;
71 int found = 0;
72
73 assert(name);
74 assert(channel);
75
76 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
77 if (type != LTTNG_EVENT_ALL && ev->type != type) {
78 continue;
79 }
80 if (strcmp(name, ev->event->name)) {
81 continue;
82 }
83 if ((ev->filter && !filter) || (!ev->filter && filter)) {
84 continue;
85 }
86 if (ev->filter && filter) {
87 if (ev->filter->len != filter->len ||
88 memcmp(ev->filter->data, filter->data,
89 filter->len) != 0) {
90 continue;
91 }
92 }
93 found = 1;
94 break;
95 }
96 if (found) {
97 DBG("Found event %s for channel %s", name,
98 channel->channel->name);
99 return ev;
100 } else {
101 return NULL;
102 }
103 }
104
105 /*
106 * Find the event name for the given channel.
107 */
108 struct ltt_kernel_event *trace_kernel_get_event_by_name(
109 char *name, struct ltt_kernel_channel *channel,
110 enum lttng_event_type type)
111 {
112 struct ltt_kernel_event *ev;
113 int found = 0;
114
115 assert(name);
116 assert(channel);
117
118 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
119 if (type != LTTNG_EVENT_ALL && ev->type != type) {
120 continue;
121 }
122 if (strcmp(name, ev->event->name)) {
123 continue;
124 }
125 found = 1;
126 break;
127 }
128 if (found) {
129 DBG("Found event %s for channel %s", name,
130 channel->channel->name);
131 return ev;
132 } else {
133 return NULL;
134 }
135 }
136
137 /*
138 * Allocate and initialize a kernel session data structure.
139 *
140 * Return pointer to structure or NULL.
141 */
142 struct ltt_kernel_session *trace_kernel_create_session(void)
143 {
144 struct ltt_kernel_session *lks = NULL;
145
146 /* Allocate a new ltt kernel session */
147 lks = zmalloc(sizeof(struct ltt_kernel_session));
148 if (lks == NULL) {
149 PERROR("create kernel session zmalloc");
150 goto alloc_error;
151 }
152
153 /* Init data structure */
154 lks->fd = -1;
155 lks->metadata_stream_fd = -1;
156 lks->channel_count = 0;
157 lks->stream_count_global = 0;
158 lks->metadata = NULL;
159 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
160
161 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
162 if (lks->consumer == NULL) {
163 goto error;
164 }
165
166 return lks;
167
168 error:
169 free(lks);
170
171 alloc_error:
172 return NULL;
173 }
174
175 /*
176 * Allocate and initialize a kernel channel data structure.
177 *
178 * Return pointer to structure or NULL.
179 */
180 struct ltt_kernel_channel *trace_kernel_create_channel(
181 struct lttng_channel *chan)
182 {
183 struct ltt_kernel_channel *lkc;
184 struct lttng_channel_extended *extended = NULL;
185
186 assert(chan);
187
188 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
189 if (lkc == NULL) {
190 PERROR("ltt_kernel_channel zmalloc");
191 goto error;
192 }
193
194 lkc->channel = zmalloc(sizeof(struct lttng_channel));
195 if (lkc->channel == NULL) {
196 PERROR("lttng_channel zmalloc");
197 goto error;
198 }
199
200 extended = zmalloc(sizeof(struct lttng_channel_extended));
201 if (!extended) {
202 PERROR("lttng_channel_channel zmalloc");
203 goto error;
204 }
205 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
206 memcpy(extended, chan->attr.extended.ptr, sizeof(struct lttng_channel_extended));
207 lkc->channel->attr.extended.ptr = extended;
208 extended = NULL;
209
210 /*
211 * If we receive an empty string for channel name, it means the
212 * default channel name is requested.
213 */
214 if (chan->name[0] == '\0') {
215 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
216 sizeof(lkc->channel->name));
217 }
218 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
219
220 lkc->fd = -1;
221 lkc->stream_count = 0;
222 lkc->event_count = 0;
223 lkc->enabled = 1;
224 lkc->published_to_notification_thread = false;
225 /* Init linked list */
226 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
227 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
228 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
229
230 return lkc;
231
232 error:
233 if (lkc) {
234 free(lkc->channel);
235 }
236 free(extended);
237 free(lkc);
238 return NULL;
239 }
240
241 /*
242 * Allocate and init a kernel context object.
243 *
244 * Return the allocated object or NULL on error.
245 */
246 struct ltt_kernel_context *trace_kernel_create_context(
247 struct lttng_kernel_context *ctx)
248 {
249 struct ltt_kernel_context *kctx;
250
251 kctx = zmalloc(sizeof(*kctx));
252 if (!kctx) {
253 PERROR("zmalloc kernel context");
254 goto error;
255 }
256
257 if (ctx) {
258 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
259 }
260
261 CDS_INIT_LIST_HEAD(&kctx->list);
262
263 error:
264 return kctx;
265 }
266
267 /*
268 * Allocate and initialize a kernel event. Set name and event type.
269 * We own filter_expression, and filter.
270 *
271 * Return pointer to structure or NULL.
272 */
273 struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev,
274 char *filter_expression, struct lttng_filter_bytecode *filter)
275 {
276 struct ltt_kernel_event *lke;
277 struct lttng_kernel_event *attr;
278
279 assert(ev);
280
281 lke = zmalloc(sizeof(struct ltt_kernel_event));
282 attr = zmalloc(sizeof(struct lttng_kernel_event));
283 if (lke == NULL || attr == NULL) {
284 PERROR("kernel event zmalloc");
285 goto error;
286 }
287
288 switch (ev->type) {
289 case LTTNG_EVENT_PROBE:
290 attr->instrumentation = LTTNG_KERNEL_KPROBE;
291 attr->u.kprobe.addr = ev->attr.probe.addr;
292 attr->u.kprobe.offset = ev->attr.probe.offset;
293 strncpy(attr->u.kprobe.symbol_name,
294 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
295 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
296 break;
297 case LTTNG_EVENT_FUNCTION:
298 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
299 attr->u.kretprobe.addr = ev->attr.probe.addr;
300 attr->u.kretprobe.offset = ev->attr.probe.offset;
301 strncpy(attr->u.kretprobe.symbol_name,
302 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
303 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
304 break;
305 case LTTNG_EVENT_FUNCTION_ENTRY:
306 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
307 strncpy(attr->u.ftrace.symbol_name,
308 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
309 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
310 break;
311 case LTTNG_EVENT_TRACEPOINT:
312 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
313 break;
314 case LTTNG_EVENT_SYSCALL:
315 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
316 break;
317 case LTTNG_EVENT_ALL:
318 attr->instrumentation = LTTNG_KERNEL_ALL;
319 break;
320 default:
321 ERR("Unknown kernel instrumentation type (%d)", ev->type);
322 goto error;
323 }
324
325 /* Copy event name */
326 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
327 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
328
329 /* Setting up a kernel event */
330 lke->fd = -1;
331 lke->event = attr;
332 lke->enabled = 1;
333 lke->filter_expression = filter_expression;
334 lke->filter = filter;
335
336 return lke;
337
338 error:
339 free(filter_expression);
340 free(filter);
341 free(lke);
342 free(attr);
343 return NULL;
344 }
345
346 /*
347 * Allocate and initialize a kernel metadata.
348 *
349 * Return pointer to structure or NULL.
350 */
351 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
352 {
353 struct ltt_kernel_metadata *lkm;
354 struct lttng_channel *chan;
355
356 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
357 chan = zmalloc(sizeof(struct lttng_channel));
358 if (lkm == NULL || chan == NULL) {
359 PERROR("kernel metadata zmalloc");
360 goto error;
361 }
362
363 /* Set default attributes */
364 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
365 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
366 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
367 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
368 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
369 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
370
371 /* Init metadata */
372 lkm->fd = -1;
373 lkm->conf = chan;
374
375 return lkm;
376
377 error:
378 free(lkm);
379 free(chan);
380 return NULL;
381 }
382
383 /*
384 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
385 * default.
386 *
387 * Return pointer to structure or NULL.
388 */
389 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
390 unsigned int count)
391 {
392 int ret;
393 struct ltt_kernel_stream *lks;
394
395 assert(name);
396
397 lks = zmalloc(sizeof(struct ltt_kernel_stream));
398 if (lks == NULL) {
399 PERROR("kernel stream zmalloc");
400 goto error;
401 }
402
403 /* Set name */
404 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
405 if (ret < 0) {
406 PERROR("snprintf stream name");
407 goto error;
408 }
409 lks->name[sizeof(lks->name) - 1] = '\0';
410
411 /* Init stream */
412 lks->fd = -1;
413 lks->state = 0;
414 lks->cpu = count;
415
416 return lks;
417
418 error:
419 return NULL;
420 }
421
422 /*
423 * Cleanup kernel stream structure.
424 */
425 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
426 {
427 assert(stream);
428
429 DBG("[trace] Closing stream fd %d", stream->fd);
430 /* Close kernel fd */
431 if (stream->fd >= 0) {
432 int ret;
433
434 ret = close(stream->fd);
435 if (ret) {
436 PERROR("close");
437 }
438 }
439 /* Remove from stream list */
440 cds_list_del(&stream->list);
441
442 free(stream);
443 }
444
445 /*
446 * Cleanup kernel event structure.
447 */
448 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
449 {
450 assert(event);
451
452 if (event->fd >= 0) {
453 int ret;
454
455 DBG("[trace] Closing event fd %d", event->fd);
456 /* Close kernel fd */
457 ret = close(event->fd);
458 if (ret) {
459 PERROR("close");
460 }
461 } else {
462 DBG("[trace] Tearing down event (no associated fd)");
463 }
464
465 /* Remove from event list */
466 cds_list_del(&event->list);
467
468 free(event->filter_expression);
469 free(event->filter);
470
471 free(event->event);
472 free(event);
473 }
474
475 /*
476 * Cleanup kernel context structure.
477 */
478 void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
479 {
480 assert(ctx);
481
482 cds_list_del(&ctx->list);
483 free(ctx);
484 }
485
486 /*
487 * Cleanup kernel channel structure.
488 */
489 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
490 {
491 struct ltt_kernel_stream *stream, *stmp;
492 struct ltt_kernel_event *event, *etmp;
493 struct ltt_kernel_context *ctx, *ctmp;
494 int ret;
495 enum lttng_error_code status;
496
497 assert(channel);
498
499 DBG("[trace] Closing channel fd %d", channel->fd);
500 /* Close kernel fd */
501 if (channel->fd >= 0) {
502 ret = close(channel->fd);
503 if (ret) {
504 PERROR("close");
505 }
506 }
507
508 /* For each stream in the channel list */
509 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
510 trace_kernel_destroy_stream(stream);
511 }
512
513 /* For each event in the channel list */
514 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
515 trace_kernel_destroy_event(event);
516 }
517
518 /* For each context in the channel list */
519 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
520 trace_kernel_destroy_context(ctx);
521 }
522
523 /* Remove from channel list */
524 cds_list_del(&channel->list);
525
526 if (notification_thread_handle
527 && channel->published_to_notification_thread) {
528 status = notification_thread_command_remove_channel(
529 notification_thread_handle,
530 channel->fd, LTTNG_DOMAIN_KERNEL);
531 assert(status == LTTNG_OK);
532 }
533 free(channel->channel->attr.extended.ptr);
534 free(channel->channel);
535 free(channel);
536 }
537
538 /*
539 * Cleanup kernel metadata structure.
540 */
541 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
542 {
543 assert(metadata);
544
545 DBG("[trace] Closing metadata fd %d", metadata->fd);
546 /* Close kernel fd */
547 if (metadata->fd >= 0) {
548 int ret;
549
550 ret = close(metadata->fd);
551 if (ret) {
552 PERROR("close");
553 }
554 }
555
556 free(metadata->conf);
557 free(metadata);
558 }
559
560 /*
561 * Cleanup kernel session structure
562 *
563 * Should *NOT* be called with RCU read-side lock held.
564 */
565 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
566 {
567 struct ltt_kernel_channel *channel, *ctmp;
568 int ret;
569
570 assert(session);
571
572 DBG("[trace] Closing session fd %d", session->fd);
573 /* Close kernel fds */
574 if (session->fd >= 0) {
575 ret = close(session->fd);
576 if (ret) {
577 PERROR("close");
578 }
579 }
580
581 if (session->metadata_stream_fd >= 0) {
582 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
583 ret = close(session->metadata_stream_fd);
584 if (ret) {
585 PERROR("close");
586 }
587 }
588
589 if (session->metadata != NULL) {
590 trace_kernel_destroy_metadata(session->metadata);
591 }
592
593 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
594 trace_kernel_destroy_channel(channel);
595 }
596
597 /* Wipe consumer output object */
598 consumer_output_put(session->consumer);
599
600 free(session);
601 }
This page took 0.041759 seconds and 4 git commands to generate.