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