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