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