Test: kernel testing for notification
[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 error:
261 return kctx;
262 }
263
264 /*
265 * Allocate and init a kernel context object from an existing kernel context
266 * object.
267 *
268 * Return the allocated object or NULL on error.
269 */
270 struct ltt_kernel_context *trace_kernel_copy_context(
271 struct ltt_kernel_context *kctx)
272 {
273 struct ltt_kernel_context *kctx_copy;
274
275 assert(kctx);
276 kctx_copy = zmalloc(sizeof(*kctx_copy));
277 if (!kctx_copy) {
278 PERROR("zmalloc ltt_kernel_context");
279 goto error;
280 }
281
282 memcpy(kctx_copy, kctx, sizeof(*kctx_copy));
283 memset(&kctx_copy->list, 0, sizeof(kctx_copy->list));
284
285 error:
286 return kctx_copy;
287 }
288
289 /*
290 * Allocate and initialize a kernel event. Set name and event type.
291 * We own filter_expression, and filter.
292 *
293 * Return pointer to structure or NULL.
294 */
295 struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev,
296 char *filter_expression, struct lttng_filter_bytecode *filter)
297 {
298 struct ltt_kernel_event *lke;
299 struct lttng_kernel_event *attr;
300
301 assert(ev);
302
303 lke = zmalloc(sizeof(struct ltt_kernel_event));
304 attr = zmalloc(sizeof(struct lttng_kernel_event));
305 if (lke == NULL || attr == NULL) {
306 PERROR("kernel event zmalloc");
307 goto error;
308 }
309
310 switch (ev->type) {
311 case LTTNG_EVENT_PROBE:
312 attr->instrumentation = LTTNG_KERNEL_KPROBE;
313 attr->u.kprobe.addr = ev->attr.probe.addr;
314 attr->u.kprobe.offset = ev->attr.probe.offset;
315 strncpy(attr->u.kprobe.symbol_name,
316 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
317 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
318 break;
319 case LTTNG_EVENT_FUNCTION:
320 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
321 attr->u.kretprobe.addr = ev->attr.probe.addr;
322 attr->u.kretprobe.offset = ev->attr.probe.offset;
323 strncpy(attr->u.kretprobe.symbol_name,
324 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
325 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
326 break;
327 case LTTNG_EVENT_FUNCTION_ENTRY:
328 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
329 strncpy(attr->u.ftrace.symbol_name,
330 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
331 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
332 break;
333 case LTTNG_EVENT_TRACEPOINT:
334 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
335 break;
336 case LTTNG_EVENT_SYSCALL:
337 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
338 break;
339 case LTTNG_EVENT_ALL:
340 attr->instrumentation = LTTNG_KERNEL_ALL;
341 break;
342 default:
343 ERR("Unknown kernel instrumentation type (%d)", ev->type);
344 goto error;
345 }
346
347 /* Copy event name */
348 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
349 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
350
351 /* Setting up a kernel event */
352 lke->fd = -1;
353 lke->event = attr;
354 lke->enabled = 1;
355 lke->filter_expression = filter_expression;
356 lke->filter = filter;
357
358 return lke;
359
360 error:
361 free(filter_expression);
362 free(filter);
363 free(lke);
364 free(attr);
365 return NULL;
366 }
367
368 /*
369 * Allocate and initialize a kernel metadata.
370 *
371 * Return pointer to structure or NULL.
372 */
373 struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
374 {
375 struct ltt_kernel_metadata *lkm;
376 struct lttng_channel *chan;
377
378 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
379 chan = zmalloc(sizeof(struct lttng_channel));
380 if (lkm == NULL || chan == NULL) {
381 PERROR("kernel metadata zmalloc");
382 goto error;
383 }
384
385 /* Set default attributes */
386 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
387 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
388 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
389 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
390 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
391 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
392
393 /* Init metadata */
394 lkm->fd = -1;
395 lkm->conf = chan;
396
397 return lkm;
398
399 error:
400 free(lkm);
401 free(chan);
402 return NULL;
403 }
404
405 /*
406 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
407 * default.
408 *
409 * Return pointer to structure or NULL.
410 */
411 struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
412 unsigned int count)
413 {
414 int ret;
415 struct ltt_kernel_stream *lks;
416
417 assert(name);
418
419 lks = zmalloc(sizeof(struct ltt_kernel_stream));
420 if (lks == NULL) {
421 PERROR("kernel stream zmalloc");
422 goto error;
423 }
424
425 /* Set name */
426 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
427 if (ret < 0) {
428 PERROR("snprintf stream name");
429 goto error;
430 }
431 lks->name[sizeof(lks->name) - 1] = '\0';
432
433 /* Init stream */
434 lks->fd = -1;
435 lks->state = 0;
436 lks->cpu = count;
437
438 return lks;
439
440 error:
441 return NULL;
442 }
443
444 /*
445 * Cleanup kernel stream structure.
446 */
447 void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
448 {
449 assert(stream);
450
451 DBG("[trace] Closing stream fd %d", stream->fd);
452 /* Close kernel fd */
453 if (stream->fd >= 0) {
454 int ret;
455
456 ret = close(stream->fd);
457 if (ret) {
458 PERROR("close");
459 }
460 }
461 /* Remove from stream list */
462 cds_list_del(&stream->list);
463
464 free(stream);
465 }
466
467 /*
468 * Cleanup kernel event structure.
469 */
470 void trace_kernel_destroy_event(struct ltt_kernel_event *event)
471 {
472 assert(event);
473
474 if (event->fd >= 0) {
475 int ret;
476
477 DBG("[trace] Closing event fd %d", event->fd);
478 /* Close kernel fd */
479 ret = close(event->fd);
480 if (ret) {
481 PERROR("close");
482 }
483 } else {
484 DBG("[trace] Tearing down event (no associated fd)");
485 }
486
487 /* Remove from event list */
488 cds_list_del(&event->list);
489
490 free(event->filter_expression);
491 free(event->filter);
492
493 free(event->event);
494 free(event);
495 }
496
497 /*
498 * Cleanup kernel context structure.
499 */
500 void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
501 {
502 assert(ctx);
503
504 if (ctx->in_list) {
505 cds_list_del(&ctx->list);
506 }
507 free(ctx);
508 }
509
510 /*
511 * Cleanup kernel channel structure.
512 */
513 void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
514 {
515 struct ltt_kernel_stream *stream, *stmp;
516 struct ltt_kernel_event *event, *etmp;
517 struct ltt_kernel_context *ctx, *ctmp;
518 int ret;
519 enum lttng_error_code status;
520
521 assert(channel);
522
523 DBG("[trace] Closing channel fd %d", channel->fd);
524 /* Close kernel fd */
525 if (channel->fd >= 0) {
526 ret = close(channel->fd);
527 if (ret) {
528 PERROR("close");
529 }
530 }
531
532 /* For each stream in the channel list */
533 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
534 trace_kernel_destroy_stream(stream);
535 }
536
537 /* For each event in the channel list */
538 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
539 trace_kernel_destroy_event(event);
540 }
541
542 /* For each context in the channel list */
543 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
544 trace_kernel_destroy_context(ctx);
545 }
546
547 /* Remove from channel list */
548 cds_list_del(&channel->list);
549
550 if (notification_thread_handle
551 && channel->published_to_notification_thread) {
552 status = notification_thread_command_remove_channel(
553 notification_thread_handle,
554 channel->fd, LTTNG_DOMAIN_KERNEL);
555 assert(status == LTTNG_OK);
556 }
557 free(channel->channel->attr.extended.ptr);
558 free(channel->channel);
559 free(channel);
560 }
561
562 /*
563 * Cleanup kernel metadata structure.
564 */
565 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
566 {
567 assert(metadata);
568
569 DBG("[trace] Closing metadata fd %d", metadata->fd);
570 /* Close kernel fd */
571 if (metadata->fd >= 0) {
572 int ret;
573
574 ret = close(metadata->fd);
575 if (ret) {
576 PERROR("close");
577 }
578 }
579
580 free(metadata->conf);
581 free(metadata);
582 }
583
584 /*
585 * Cleanup kernel session structure
586 *
587 * Should *NOT* be called with RCU read-side lock held.
588 */
589 void trace_kernel_destroy_session(struct ltt_kernel_session *session)
590 {
591 struct ltt_kernel_channel *channel, *ctmp;
592 int ret;
593
594 assert(session);
595
596 DBG("[trace] Closing session fd %d", session->fd);
597 /* Close kernel fds */
598 if (session->fd >= 0) {
599 ret = close(session->fd);
600 if (ret) {
601 PERROR("close");
602 }
603 }
604
605 if (session->metadata_stream_fd >= 0) {
606 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
607 ret = close(session->metadata_stream_fd);
608 if (ret) {
609 PERROR("close");
610 }
611 }
612
613 if (session->metadata != NULL) {
614 trace_kernel_destroy_metadata(session->metadata);
615 }
616
617 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
618 trace_kernel_destroy_channel(channel);
619 }
620
621 /* Wipe consumer output object */
622 consumer_output_put(session->consumer);
623
624 free(session);
625 }
This page took 0.048427 seconds and 5 git commands to generate.