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