Fix RCU-related hangs: incorrect lttng_ht_destroy use
[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
54d01ffb
DG
41 DBG("Trying to find channel %s", name);
42
19e70852
DG
43 cds_list_for_each_entry(chan, &session->channel_list.head, list) {
44 if (strcmp(name, chan->channel->name) == 0) {
45 DBG("Found channel by name %s", name);
46 return chan;
47 }
48 }
49
19e70852
DG
50 return NULL;
51}
52
53/*
050349bb 54 * Find the event name for the given channel.
19e70852 55 */
62499ad6 56struct ltt_kernel_event *trace_kernel_get_event_by_name(
19e70852
DG
57 char *name, struct ltt_kernel_channel *channel)
58{
59 struct ltt_kernel_event *ev;
60
0525e9ae
DG
61 assert(name);
62 assert(channel);
19e70852
DG
63
64 cds_list_for_each_entry(ev, &channel->events_list.head, list) {
65 if (strcmp(name, ev->event->name) == 0) {
66 DBG("Found event by name %s for channel %s", name,
67 channel->channel->name);
68 return ev;
69 }
70 }
71
19e70852
DG
72 return NULL;
73}
74
54012638 75/*
050349bb 76 * Allocate and initialize a kernel session data structure.
54012638 77 *
050349bb 78 * Return pointer to structure or NULL.
54012638 79 */
dec56f6c 80struct ltt_kernel_session *trace_kernel_create_session(void)
54012638 81{
a4b92340 82 struct ltt_kernel_session *lks = NULL;
54012638
DG
83
84 /* Allocate a new ltt kernel session */
ba7f0ae5 85 lks = zmalloc(sizeof(struct ltt_kernel_session));
54012638 86 if (lks == NULL) {
df0f840b 87 PERROR("create kernel session zmalloc");
a4b92340 88 goto alloc_error;
54012638
DG
89 }
90
91 /* Init data structure */
03550b58
MD
92 lks->fd = -1;
93 lks->metadata_stream_fd = -1;
54012638
DG
94 lks->channel_count = 0;
95 lks->stream_count_global = 0;
96 lks->metadata = NULL;
97 CDS_INIT_LIST_HEAD(&lks->channel_list.head);
98
00e2e675
DG
99 lks->consumer = consumer_create_output(CONSUMER_DST_LOCAL);
100 if (lks->consumer == NULL) {
101 goto error;
102 }
103
104 /*
105 * The tmp_consumer stays NULL until a set_consumer_uri command is
106 * executed. At this point, the consumer should be nullify until an
107 * enable_consumer command. This assignment is symbolic since we've zmalloc
108 * the struct.
109 */
110 lks->tmp_consumer = NULL;
111
54012638
DG
112 return lks;
113
114error:
a4b92340
DG
115 free(lks);
116
117alloc_error:
54012638
DG
118 return NULL;
119}
120
121/*
050349bb 122 * Allocate and initialize a kernel channel data structure.
54012638 123 *
050349bb 124 * Return pointer to structure or NULL.
54012638 125 */
00e2e675 126struct ltt_kernel_channel *trace_kernel_create_channel(
fdd9eb17 127 struct lttng_channel *chan)
54012638 128{
54012638 129 struct ltt_kernel_channel *lkc;
54012638 130
0525e9ae
DG
131 assert(chan);
132
ba7f0ae5 133 lkc = zmalloc(sizeof(struct ltt_kernel_channel));
f3ed775e 134 if (lkc == NULL) {
df0f840b 135 PERROR("ltt_kernel_channel zmalloc");
54012638
DG
136 goto error;
137 }
138
ba7f0ae5 139 lkc->channel = zmalloc(sizeof(struct lttng_channel));
f3ed775e 140 if (lkc->channel == NULL) {
df0f840b 141 PERROR("lttng_channel zmalloc");
ed594980 142 free(lkc);
f3ed775e
DG
143 goto error;
144 }
145 memcpy(lkc->channel, chan, sizeof(struct lttng_channel));
54012638 146
03550b58 147 lkc->fd = -1;
54012638 148 lkc->stream_count = 0;
cbbbb275 149 lkc->event_count = 0;
d36b8583 150 lkc->enabled = 1;
e75cda3a 151 lkc->ctx = NULL;
54012638
DG
152 /* Init linked list */
153 CDS_INIT_LIST_HEAD(&lkc->events_list.head);
154 CDS_INIT_LIST_HEAD(&lkc->stream_list.head);
54012638
DG
155
156 return lkc;
157
158error:
159 return NULL;
160}
161
162/*
050349bb 163 * Allocate and initialize a kernel event. Set name and event type.
54012638 164 *
050349bb 165 * Return pointer to structure or NULL.
54012638 166 */
62499ad6 167struct ltt_kernel_event *trace_kernel_create_event(struct lttng_event *ev)
54012638
DG
168{
169 struct ltt_kernel_event *lke;
170 struct lttng_kernel_event *attr;
171
0525e9ae
DG
172 assert(ev);
173
ba7f0ae5
DG
174 lke = zmalloc(sizeof(struct ltt_kernel_event));
175 attr = zmalloc(sizeof(struct lttng_kernel_event));
54012638 176 if (lke == NULL || attr == NULL) {
df0f840b 177 PERROR("kernel event zmalloc");
54012638
DG
178 goto error;
179 }
180
f3ed775e 181 switch (ev->type) {
7d29a247 182 case LTTNG_EVENT_PROBE:
e6ddca71 183 attr->instrumentation = LTTNG_KERNEL_KPROBE;
7d29a247
DG
184 attr->u.kprobe.addr = ev->attr.probe.addr;
185 attr->u.kprobe.offset = ev->attr.probe.offset;
f3ed775e 186 strncpy(attr->u.kprobe.symbol_name,
dbbb3ec5
DG
187 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
188 attr->u.kprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e
DG
189 break;
190 case LTTNG_EVENT_FUNCTION:
8f0d098b
MD
191 attr->instrumentation = LTTNG_KERNEL_KRETPROBE;
192 attr->u.kretprobe.addr = ev->attr.probe.addr;
193 attr->u.kretprobe.offset = ev->attr.probe.offset;
194 attr->u.kretprobe.offset = ev->attr.probe.offset;
195 strncpy(attr->u.kretprobe.symbol_name,
dbbb3ec5
DG
196 ev->attr.probe.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
197 attr->u.kretprobe.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
8f0d098b
MD
198 break;
199 case LTTNG_EVENT_FUNCTION_ENTRY:
f3ed775e
DG
200 attr->instrumentation = LTTNG_KERNEL_FUNCTION;
201 strncpy(attr->u.ftrace.symbol_name,
dbbb3ec5
DG
202 ev->attr.ftrace.symbol_name, LTTNG_KERNEL_SYM_NAME_LEN);
203 attr->u.ftrace.symbol_name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 204 break;
e6ddca71
DG
205 case LTTNG_EVENT_TRACEPOINT:
206 attr->instrumentation = LTTNG_KERNEL_TRACEPOINT;
f3ed775e 207 break;
a54bd42d
MD
208 case LTTNG_EVENT_SYSCALL:
209 attr->instrumentation = LTTNG_KERNEL_SYSCALL;
0133c199 210 break;
7a3d1328
MD
211 case LTTNG_EVENT_ALL:
212 attr->instrumentation = LTTNG_KERNEL_ALL;
213 break;
f3ed775e
DG
214 default:
215 ERR("Unknown kernel instrumentation type (%d)", ev->type);
216 goto error;
217 }
218
219 /* Copy event name */
dbbb3ec5
DG
220 strncpy(attr->name, ev->name, LTTNG_KERNEL_SYM_NAME_LEN);
221 attr->name[LTTNG_KERNEL_SYM_NAME_LEN - 1] = '\0';
f3ed775e 222
54012638 223 /* Setting up a kernel event */
03550b58 224 lke->fd = -1;
54012638 225 lke->event = attr;
d36b8583 226 lke->enabled = 1;
54012638
DG
227
228 return lke;
229
230error:
a2c0da86
MD
231 free(lke);
232 free(attr);
54012638
DG
233 return NULL;
234}
235
236/*
050349bb 237 * Allocate and initialize a kernel metadata.
54012638 238 *
050349bb 239 * Return pointer to structure or NULL.
54012638 240 */
a4b92340 241struct ltt_kernel_metadata *trace_kernel_create_metadata(void)
54012638 242{
54012638 243 struct ltt_kernel_metadata *lkm;
f3ed775e 244 struct lttng_channel *chan;
54012638 245
ba7f0ae5
DG
246 lkm = zmalloc(sizeof(struct ltt_kernel_metadata));
247 chan = zmalloc(sizeof(struct lttng_channel));
f3ed775e 248 if (lkm == NULL || chan == NULL) {
df0f840b 249 PERROR("kernel metadata zmalloc");
54012638
DG
250 goto error;
251 }
252
253 /* Set default attributes */
f3ed775e 254 chan->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
3e230f92 255 chan->attr.subbuf_size = default_get_metadata_subbuf_size();
b389abbe 256 chan->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
6bb9e85f
MD
257 chan->attr.switch_timer_interval = DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER;
258 chan->attr.read_timer_interval = DEFAULT_KERNEL_CHANNEL_READ_TIMER;
7d452e12 259 chan->attr.output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
54012638
DG
260
261 /* Init metadata */
03550b58 262 lkm->fd = -1;
f3ed775e 263 lkm->conf = chan;
54012638
DG
264
265 return lkm;
266
267error:
a2c0da86
MD
268 free(lkm);
269 free(chan);
54012638
DG
270 return NULL;
271}
272
273/*
050349bb
DG
274 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
275 * default.
54012638 276 *
050349bb 277 * Return pointer to structure or NULL.
54012638 278 */
00e2e675
DG
279struct ltt_kernel_stream *trace_kernel_create_stream(const char *name,
280 unsigned int count)
54012638 281{
00e2e675 282 int ret;
54012638
DG
283 struct ltt_kernel_stream *lks;
284
0525e9ae
DG
285 assert(name);
286
ba7f0ae5 287 lks = zmalloc(sizeof(struct ltt_kernel_stream));
54012638 288 if (lks == NULL) {
df0f840b 289 PERROR("kernel stream zmalloc");
54012638
DG
290 goto error;
291 }
292
00e2e675
DG
293 /* Set name */
294 ret = snprintf(lks->name, sizeof(lks->name), "%s_%d", name, count);
295 if (ret < 0) {
296 PERROR("snprintf stream name");
297 goto error;
298 }
299 lks->name[sizeof(lks->name) - 1] = '\0';
300
54012638 301 /* Init stream */
03550b58 302 lks->fd = -1;
54012638 303 lks->state = 0;
ffe60014 304 lks->cpu = count;
54012638
DG
305
306 return lks;
307
308error:
309 return NULL;
310}
c363b55d 311
050349bb
DG
312/*
313 * Cleanup kernel stream structure.
314 */
62499ad6 315void trace_kernel_destroy_stream(struct ltt_kernel_stream *stream)
c363b55d 316{
0525e9ae
DG
317 assert(stream);
318
33a2b854 319 DBG("[trace] Closing stream fd %d", stream->fd);
c363b55d 320 /* Close kernel fd */
03550b58 321 if (stream->fd >= 0) {
c617c0c6
MD
322 int ret;
323
03550b58
MD
324 ret = close(stream->fd);
325 if (ret) {
326 PERROR("close");
327 }
799e2c4f 328 }
c363b55d
DG
329 /* Remove from stream list */
330 cds_list_del(&stream->list);
f9815039 331
c363b55d
DG
332 free(stream);
333}
334
050349bb
DG
335/*
336 * Cleanup kernel event structure.
337 */
62499ad6 338void trace_kernel_destroy_event(struct ltt_kernel_event *event)
c363b55d 339{
0525e9ae
DG
340 assert(event);
341
87eb4ab8 342 if (event->fd >= 0) {
c617c0c6
MD
343 int ret;
344
87eb4ab8
MD
345 DBG("[trace] Closing event fd %d", event->fd);
346 /* Close kernel fd */
799e2c4f
MD
347 ret = close(event->fd);
348 if (ret) {
349 PERROR("close");
350 }
87eb4ab8
MD
351 } else {
352 DBG("[trace] Tearing down event (no associated fd)");
353 }
c363b55d
DG
354
355 /* Remove from event list */
356 cds_list_del(&event->list);
f9815039
DG
357
358 free(event->event);
c363b55d
DG
359 free(event);
360}
361
050349bb
DG
362/*
363 * Cleanup kernel channel structure.
364 */
62499ad6 365void trace_kernel_destroy_channel(struct ltt_kernel_channel *channel)
c363b55d 366{
af9737e9
DG
367 struct ltt_kernel_stream *stream, *stmp;
368 struct ltt_kernel_event *event, *etmp;
799e2c4f 369 int ret;
c363b55d 370
0525e9ae
DG
371 assert(channel);
372
33a2b854 373 DBG("[trace] Closing channel fd %d", channel->fd);
c363b55d 374 /* Close kernel fd */
03550b58
MD
375 if (channel->fd >= 0) {
376 ret = close(channel->fd);
377 if (ret) {
378 PERROR("close");
379 }
799e2c4f 380 }
c363b55d
DG
381
382 /* For each stream in the channel list */
af9737e9 383 cds_list_for_each_entry_safe(stream, stmp, &channel->stream_list.head, list) {
62499ad6 384 trace_kernel_destroy_stream(stream);
c363b55d
DG
385 }
386
387 /* For each event in the channel list */
af9737e9 388 cds_list_for_each_entry_safe(event, etmp, &channel->events_list.head, list) {
62499ad6 389 trace_kernel_destroy_event(event);
c363b55d
DG
390 }
391
392 /* Remove from channel list */
393 cds_list_del(&channel->list);
f9815039 394
f9815039
DG
395 free(channel->channel);
396 free(channel->ctx);
c363b55d
DG
397 free(channel);
398}
399
050349bb
DG
400/*
401 * Cleanup kernel metadata structure.
402 */
62499ad6 403void trace_kernel_destroy_metadata(struct ltt_kernel_metadata *metadata)
c363b55d 404{
0525e9ae
DG
405 assert(metadata);
406
33a2b854 407 DBG("[trace] Closing metadata fd %d", metadata->fd);
c363b55d 408 /* Close kernel fd */
03550b58 409 if (metadata->fd >= 0) {
c617c0c6
MD
410 int ret;
411
03550b58
MD
412 ret = close(metadata->fd);
413 if (ret) {
414 PERROR("close");
415 }
799e2c4f 416 }
c363b55d 417
f9815039 418 free(metadata->conf);
c363b55d
DG
419 free(metadata);
420}
421
050349bb 422/*
62499ad6 423 * Cleanup kernel session structure
36b588ed
MD
424 *
425 * Should *NOT* be called with RCU read-side lock held.
050349bb 426 */
62499ad6 427void trace_kernel_destroy_session(struct ltt_kernel_session *session)
c363b55d 428{
af9737e9 429 struct ltt_kernel_channel *channel, *ctmp;
799e2c4f 430 int ret;
c363b55d 431
0525e9ae
DG
432 assert(session);
433
33a2b854 434 DBG("[trace] Closing session fd %d", session->fd);
c363b55d 435 /* Close kernel fds */
03550b58
MD
436 if (session->fd >= 0) {
437 ret = close(session->fd);
438 if (ret) {
439 PERROR("close");
440 }
799e2c4f 441 }
f9815039 442
03550b58 443 if (session->metadata_stream_fd >= 0) {
70dc1c34 444 DBG("[trace] Closing metadata stream fd %d", session->metadata_stream_fd);
799e2c4f
MD
445 ret = close(session->metadata_stream_fd);
446 if (ret) {
447 PERROR("close");
448 }
70dc1c34 449 }
c363b55d 450
d36b8583 451 if (session->metadata != NULL) {
62499ad6 452 trace_kernel_destroy_metadata(session->metadata);
d36b8583 453 }
c363b55d 454
af9737e9 455 cds_list_for_each_entry_safe(channel, ctmp, &session->channel_list.head, list) {
62499ad6 456 trace_kernel_destroy_channel(channel);
c363b55d
DG
457 }
458
00e2e675
DG
459 /* Wipe consumer output object */
460 consumer_destroy_output(session->consumer);
461 consumer_destroy_output(session->tmp_consumer);
462
c363b55d
DG
463 free(session);
464}
This page took 0.094156 seconds and 4 git commands to generate.