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