Fix: HT must not be destroyed with a rcu_read_lock held
[lttng-tools.git] / src / bin / lttng-sessiond / trace-kernel.c
CommitLineData
54012638
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
54012638
DG
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 *
d14d33bf
AM
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.
54012638
DG
16 */
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
c363b55d 22#include <unistd.h>
54012638 23
990570ed
DG
24#include <common/common.h>
25#include <common/defaults.h>
1e307fab 26
00e2e675 27#include "consumer.h"
62499ad6 28#include "trace-kernel.h"
54012638 29
19e70852 30/*
050349bb 31 * Find the channel name for the given kernel session.
19e70852 32 */
62499ad6 33struct ltt_kernel_channel *trace_kernel_get_channel_by_name(
19e70852
DG
34 char *name, struct ltt_kernel_session *session)
35{
36 struct ltt_kernel_channel *chan;
37
0525e9ae
DG
38 assert(session);
39 assert(name);
19e70852 40
85076754
MD
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
54d01ffb
DG
48 DBG("Trying to find channel %s", name);
49
19e70852
DG
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
19e70852
DG
57 return NULL;
58}
59
60/*
050349bb 61 * Find the event name for the given channel.
19e70852 62 */
62499ad6 63struct ltt_kernel_event *trace_kernel_get_event_by_name(
19e70852
DG
64 char *name, struct ltt_kernel_channel *channel)
65{
66 struct ltt_kernel_event *ev;
67
0525e9ae
DG
68 assert(name);
69 assert(channel);
19e70852
DG
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
19e70852
DG
79 return NULL;
80}
81
54012638 82/*
050349bb 83 * Allocate and initialize a kernel session data structure.
54012638 84 *
050349bb 85 * Return pointer to structure or NULL.
54012638 86 */
dec56f6c 87struct ltt_kernel_session *trace_kernel_create_session(void)
54012638 88{
a4b92340 89 struct ltt_kernel_session *lks = NULL;
54012638
DG
90
91 /* Allocate a new ltt kernel session */
ba7f0ae5 92 lks = zmalloc(sizeof(struct ltt_kernel_session));
54012638 93 if (lks == NULL) {
df0f840b 94 PERROR("create kernel session zmalloc");
a4b92340 95 goto alloc_error;
54012638
DG
96 }
97
98 /* Init data structure */
03550b58
MD
99 lks->fd = -1;
100 lks->metadata_stream_fd = -1;
54012638
DG
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
00e2e675
DG
106 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
107 if (lks->consumer == NULL) {
108 goto error;
109 }
110
111 /*
112 * The tmp_consumer stays NULL until a set_consumer_uri command is
113 * executed. At this point, the consumer should be nullify until an
114 * enable_consumer command. This assignment is symbolic since we've zmalloc
115 * the struct.
116 */
117 lks->tmp_consumer = NULL;
118
54012638
DG
119 return lks;
120
121error:
a4b92340
DG
122 free(lks);
123
124alloc_error:
54012638
DG
125 return NULL;
126}
127
128/*
050349bb 129 * Allocate and initialize a kernel channel data structure.
54012638 130 *
050349bb 131 * Return pointer to structure or NULL.
54012638 132 */
00e2e675 133struct ltt_kernel_channel *trace_kernel_create_channel(
fdd9eb17 134 struct lttng_channel *chan)
54012638 135{
54012638 136 struct ltt_kernel_channel *lkc;
54012638 137
0525e9ae
DG
138 assert(chan);
139
ba7f0ae5 140 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
f3ed775e 141 if (lkc == NULL) {
df0f840b 142 PERROR("ltt_kernel_channel zmalloc");
54012638
DG
143 goto error;
144 }
145
ba7f0ae5 146 lkc->channel = zmalloc(sizeof(struct lttng_channel));
f3ed775e 147 if (lkc->channel == NULL) {
df0f840b 148 PERROR("lttng_channel zmalloc");
ed594980 149 free(lkc);
f3ed775e
DG
150 goto error;
151 }
152 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
54012638 153
85076754
MD
154 /*
155 * If we receive an empty string for channel name, it means the
156 * default channel name is requested.
157 */
158 if (chan->name[0] == '\0') {
159 strncpy(lkc->channel->name, DEFAULT_CHANNEL_NAME,
160 sizeof(lkc->channel->name));
161 }
bd722d76 162 lkc->channel->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
85076754 163
03550b58 164 lkc->fd = -1;
54012638 165 lkc->stream_count = 0;
cbbbb275 166 lkc->event_count = 0;
d36b8583 167 lkc->enabled = 1;
54012638
DG
168 /* Init linked list */
169 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
170 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
645328ae 171 CDS_INIT_LIST_HEAD(&lkc->ctx_list);
54012638
DG
172
173 return lkc;
174
175error:
176 return NULL;
177}
178
645328ae
DG
179/*
180 * Allocate and init a kernel context object.
181 *
182 * Return the allocated object or NULL on error.
183 */
184struct ltt_kernel_context *trace_kernel_create_context(
185 struct lttng_kernel_context *ctx)
186{
187 struct ltt_kernel_context *kctx;
188
189 kctx = zmalloc(sizeof(*kctx));
190 if (!kctx) {
191 PERROR("zmalloc kernel context");
192 goto error;
193 }
194
195 if (ctx) {
196 memcpy(&kctx->ctx, ctx, sizeof(kctx->ctx));
197 }
198
7b9445b3
DG
199 CDS_INIT_LIST_HEAD(&kctx->list);
200
645328ae
DG
201error:
202 return kctx;
203}
204
54012638 205/*
050349bb 206 * Allocate and initialize a kernel event. Set name and event type.
54012638 207 *
050349bb 208 * Return pointer to structure or NULL.
54012638 209 */
62499ad6 210struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
54012638
DG
211{
212 struct ltt_kernel_event *lke;
213 struct lttng_kernel_event *attr;
214
0525e9ae
DG
215 assert(ev);
216
ba7f0ae5
DG
217 lke = zmalloc(sizeof(struct ltt_kernel_event));
218 attr = zmalloc(sizeof(struct lttng_kernel_event));
54012638 219 if (lke == NULL || attr == NULL) {
df0f840b 220 PERROR("kernel event zmalloc");
54012638
DG
221 goto error;
222 }
223
f3ed775e 224 switch (ev->type) {
7d29a247 225 case LTTNG_EVENT_PROBE:
e6ddca71 226 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
227 attr->u.kprobe.addr = ev->attr.probe.addr;
228 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 229 strncpy(attr->u.kprobe.symbol_name,
dbbb3ec5
DG
230 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
231 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e
DG
232 break;
233 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
234 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
235 attr->u.kretprobe.addr = ev->attr.probe.addr;
236 attr->u.kretprobe.offset = ev->attr.probe.offset;
8f0d098b 237 strncpy(attr->u.kretprobe.symbol_name,
dbbb3ec5
DG
238 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
239 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
240 break;
241 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
242 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
243 strncpy(attr->u.ftrace.symbol_name,
dbbb3ec5
DG
244 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
245 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 246 break;
e6ddca71
DG
247 case LTTNG_EVENT_TRACEPOINT:
248 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e 249 break;
a54bd42d
MD
250 case LTTNG_EVENT_SYSCALL:
251 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
0133c199 252 break;
7a3d1328
MD
253 case LTTNG_EVENT_ALL:
254 attr->instrumentation = LTTNG_KERNEL_ALL;
255 break;
f3ed775e
DG
256 default:
257 ERR("Unknown kernel instrumentation type (%d)", ev->type);
258 goto error;
259 }
260
261 /* Copy event name */
dbbb3ec5
DG
262 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
263 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 264
54012638 265 /* Setting up a kernel event */
03550b58 266 lke->fd = -1;
54012638 267 lke->event = attr;
d36b8583 268 lke->enabled = 1;
54012638
DG
269
270 return lke;
271
272error:
a2c0da86
MD
273 free(lke);
274 free(attr);
54012638
DG
275 return NULL;
276}
277
278/*
050349bb 279 * Allocate and initialize a kernel metadata.
54012638 280 *
050349bb 281 * Return pointer to structure or NULL.
54012638 282 */
a4b92340 283struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
54012638 284{
54012638 285 struct ltt_kernel_metadata *lkm;
f3ed775e 286 struct lttng_channel *chan;
54012638 287
ba7f0ae5
DG
288 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
289 chan = zmalloc(sizeof(struct lttng_channel));
f3ed775e 290 if (lkm == NULL || chan == NULL) {
df0f840b 291 PERROR("kernel metadata zmalloc");
54012638
DG
292 goto error;
293 }
294
295 /* Set default attributes */
f3ed775e 296 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
3e230f92 297 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
b389abbe 298 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
6bb9e85f
MD
299 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
300 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
7d452e12 301 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
302
303 /* Init metadata */
03550b58 304 lkm->fd = -1;
f3ed775e 305 lkm->conf = chan;
54012638
DG
306
307 return lkm;
308
309error:
a2c0da86
MD
310 free(lkm);
311 free(chan);
54012638
DG
312 return NULL;
313}
314
315/*
050349bb
DG
316 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
317 * default.
54012638 318 *
050349bb 319 * Return pointer to structure or NULL.
54012638 320 */
00e2e675
DG
321struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
322 unsigned int count)
54012638 323{
00e2e675 324 int ret;
54012638
DG
325 struct ltt_kernel_stream *lks;
326
0525e9ae
DG
327 assert(name);
328
ba7f0ae5 329 lks = zmalloc(sizeof(struct ltt_kernel_stream));
54012638 330 if (lks == NULL) {
df0f840b 331 PERROR("kernel stream zmalloc");
54012638
DG
332 goto error;
333 }
334
00e2e675 335 /* Set name */
535b8ff4 336 ret = snprintf(lks->name, sizeof(lks->name), "%s_%u", name, count);
00e2e675
DG
337 if (ret < 0) {
338 PERROR("snprintf stream name");
339 goto error;
340 }
341 lks->name[sizeof(lks->name) - 1] = '\0';
342
54012638 343 /* Init stream */
03550b58 344 lks->fd = -1;
54012638 345 lks->state = 0;
ffe60014 346 lks->cpu = count;
54012638
DG
347
348 return lks;
349
350error:
351 return NULL;
352}
c363b55d 353
050349bb
DG
354/*
355 * Cleanup kernel stream structure.
356 */
62499ad6 357void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 358{
0525e9ae
DG
359 assert(stream);
360
33a2b854 361 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d 362 /* Close kernel fd */
03550b58 363 if (stream->fd >= 0) {
c617c0c6
MD
364 int ret;
365
03550b58
MD
366 ret = close(stream->fd);
367 if (ret) {
368 PERROR("close");
369 }
799e2c4f 370 }
c363b55d
DG
371 /* Remove from stream list */
372 cds_list_del(&stream->list);
f9815039 373
c363b55d
DG
374 free(stream);
375}
376
050349bb
DG
377/*
378 * Cleanup kernel event structure.
379 */
62499ad6 380void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 381{
0525e9ae
DG
382 assert(event);
383
87eb4ab8 384 if (event->fd >= 0) {
c617c0c6
MD
385 int ret;
386
87eb4ab8
MD
387 DBG("[trace] Closing event fd %d", event->fd);
388 /* Close kernel fd */
799e2c4f
MD
389 ret = close(event->fd);
390 if (ret) {
391 PERROR("close");
392 }
87eb4ab8
MD
393 } else {
394 DBG("[trace] Tearing down event (no associated fd)");
395 }
c363b55d
DG
396
397 /* Remove from event list */
398 cds_list_del(&event->list);
f9815039
DG
399
400 free(event->event);
c363b55d
DG
401 free(event);
402}
403
645328ae
DG
404/*
405 * Cleanup kernel context structure.
406 */
407void trace_kernel_destroy_context(struct ltt_kernel_context *ctx)
408{
409 assert(ctx);
410
411 cds_list_del(&ctx->list);
412 free(ctx);
413}
414
050349bb
DG
415/*
416 * Cleanup kernel channel structure.
417 */
62499ad6 418void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 419{
af9737e9
DG
420 struct ltt_kernel_stream *stream, *stmp;
421 struct ltt_kernel_event *event, *etmp;
645328ae 422 struct ltt_kernel_context *ctx, *ctmp;
799e2c4f 423 int ret;
c363b55d 424
0525e9ae
DG
425 assert(channel);
426
33a2b854 427 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d 428 /* Close kernel fd */
03550b58
MD
429 if (channel->fd >= 0) {
430 ret = close(channel->fd);
431 if (ret) {
432 PERROR("close");
433 }
799e2c4f 434 }
c363b55d
DG
435
436 /* For each stream in the channel list */
af9737e9 437 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 438 trace_kernel_destroy_stream(stream);
c363b55d
DG
439 }
440
441 /* For each event in the channel list */
af9737e9 442 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 443 trace_kernel_destroy_event(event);
c363b55d
DG
444 }
445
645328ae
DG
446 /* For each context in the channel list */
447 cds_list_for_each_entry_safe(ctx, ctmp, &channel->ctx_list, list) {
448 trace_kernel_destroy_context(ctx);
449 }
450
c363b55d
DG
451 /* Remove from channel list */
452 cds_list_del(&channel->list);
f9815039 453
f9815039 454 free(channel->channel);
c363b55d
DG
455 free(channel);
456}
457
050349bb
DG
458/*
459 * Cleanup kernel metadata structure.
460 */
62499ad6 461void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 462{
0525e9ae
DG
463 assert(metadata);
464
33a2b854 465 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d 466 /* Close kernel fd */
03550b58 467 if (metadata->fd >= 0) {
c617c0c6
MD
468 int ret;
469
03550b58
MD
470 ret = close(metadata->fd);
471 if (ret) {
472 PERROR("close");
473 }
799e2c4f 474 }
c363b55d 475
f9815039 476 free(metadata->conf);
c363b55d
DG
477 free(metadata);
478}
479
050349bb 480/*
62499ad6 481 * Cleanup kernel session structure
36b588ed
MD
482 *
483 * Should *NOT* be called with RCU read-side lock held.
050349bb 484 */
62499ad6 485void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 486{
af9737e9 487 struct ltt_kernel_channel *channel, *ctmp;
799e2c4f 488 int ret;
c363b55d 489
0525e9ae
DG
490 assert(session);
491
33a2b854 492 DBG("[trace] Closing session fd %d", session->fd);
c363b55d 493 /* Close kernel fds */
03550b58
MD
494 if (session->fd >= 0) {
495 ret = close(session->fd);
496 if (ret) {
497 PERROR("close");
498 }
799e2c4f 499 }
f9815039 500
03550b58 501 if (session->metadata_stream_fd >= 0) {
70dc1c34 502 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
799e2c4f
MD
503 ret = close(session->metadata_stream_fd);
504 if (ret) {
505 PERROR("close");
506 }
70dc1c34 507 }
c363b55d 508
d36b8583 509 if (session->metadata != NULL) {
62499ad6 510 trace_kernel_destroy_metadata(session->metadata);
d36b8583 511 }
c363b55d 512
af9737e9 513 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 514 trace_kernel_destroy_channel(channel);
c363b55d
DG
515 }
516
00e2e675
DG
517 /* Wipe consumer output object */
518 consumer_destroy_output(session->consumer);
519 consumer_destroy_output(session->tmp_consumer);
520
c363b55d
DG
521 free(session);
522}
This page took 0.065314 seconds and 4 git commands to generate.