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