2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
24 #include <common/common.h>
25 #include <common/defaults.h>
28 #include "trace-kernel.h"
31 * Find the channel name for the given kernel session.
33 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
34 char *name
, struct ltt_kernel_session
*session
)
36 struct ltt_kernel_channel
*chan
;
42 * If we receive an empty string for channel name, it means the
43 * default channel name is requested.
46 name
= DEFAULT_CHANNEL_NAME
;
48 DBG("Trying to find channel %s", name
);
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
);
61 * Find the event for the given channel.
63 struct ltt_kernel_event
*trace_kernel_find_event(
64 char *name
, struct ltt_kernel_channel
*channel
,
65 enum lttng_event_type type
,
66 struct lttng_filter_bytecode
*filter
)
68 struct ltt_kernel_event
*ev
;
74 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
75 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
78 if (strcmp(name
, ev
->event
->name
)) {
81 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
84 if (ev
->filter
&& filter
) {
85 if (ev
->filter
->len
!= filter
->len
||
86 memcmp(ev
->filter
->data
, filter
->data
,
95 DBG("Found event %s for channel %s", name
,
96 channel
->channel
->name
);
104 * Find the event name for the given channel.
106 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
107 char *name
, struct ltt_kernel_channel
*channel
,
108 enum lttng_event_type type
)
110 struct ltt_kernel_event
*ev
;
116 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
117 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
120 if (strcmp(name
, ev
->event
->name
)) {
127 DBG("Found event %s for channel %s", name
,
128 channel
->channel
->name
);
136 * Allocate and initialize a kernel session data structure.
138 * Return pointer to structure or NULL.
140 struct ltt_kernel_session
*trace_kernel_create_session(void)
142 struct ltt_kernel_session
*lks
= NULL
;
144 /* Allocate a new ltt kernel session */
145 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
147 PERROR("create kernel session zmalloc");
151 /* Init data structure */
153 lks
->metadata_stream_fd
= -1;
154 lks
->channel_count
= 0;
155 lks
->stream_count_global
= 0;
156 lks
->metadata
= NULL
;
157 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
159 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
160 if (lks
->consumer
== NULL
) {
174 * Allocate and initialize a kernel channel data structure.
176 * Return pointer to structure or NULL.
178 struct ltt_kernel_channel
*trace_kernel_create_channel(
179 struct lttng_channel
*chan
)
181 struct ltt_kernel_channel
*lkc
;
185 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
187 PERROR("ltt_kernel_channel zmalloc");
191 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
192 if (lkc
->channel
== NULL
) {
193 PERROR("lttng_channel zmalloc");
197 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
200 * If we receive an empty string for channel name, it means the
201 * default channel name is requested.
203 if (chan
->name
[0] == '\0') {
204 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
205 sizeof(lkc
->channel
->name
));
207 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
210 lkc
->stream_count
= 0;
211 lkc
->event_count
= 0;
213 /* Init linked list */
214 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
215 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
216 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
225 * Allocate and init a kernel context object.
227 * Return the allocated object or NULL on error.
229 struct ltt_kernel_context
*trace_kernel_create_context(
230 struct lttng_kernel_context
*ctx
)
232 struct ltt_kernel_context
*kctx
;
234 kctx
= zmalloc(sizeof(*kctx
));
236 PERROR("zmalloc kernel context");
241 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
248 * Allocate and init a kernel context object from an existing kernel context
251 * Return the allocated object or NULL on error.
253 struct ltt_kernel_context
*trace_kernel_copy_context(
254 struct ltt_kernel_context
*kctx
)
256 struct ltt_kernel_context
*kctx_copy
;
259 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
261 PERROR("zmalloc ltt_kernel_context");
265 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
266 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
273 * Allocate and initialize a kernel event. Set name and event type.
274 * We own filter_expression, and filter.
276 * Return pointer to structure or NULL.
278 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
,
279 char *filter_expression
, struct lttng_filter_bytecode
*filter
)
281 struct ltt_kernel_event
*lke
;
282 struct lttng_kernel_event
*attr
;
286 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
287 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
288 if (lke
== NULL
|| attr
== NULL
) {
289 PERROR("kernel event zmalloc");
294 case LTTNG_EVENT_PROBE
:
295 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
296 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
297 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
298 strncpy(attr
->u
.kprobe
.symbol_name
,
299 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
300 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
302 case LTTNG_EVENT_FUNCTION
:
303 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
304 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
305 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
306 strncpy(attr
->u
.kretprobe
.symbol_name
,
307 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
308 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
310 case LTTNG_EVENT_FUNCTION_ENTRY
:
311 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
312 strncpy(attr
->u
.ftrace
.symbol_name
,
313 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
314 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
316 case LTTNG_EVENT_TRACEPOINT
:
317 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
319 case LTTNG_EVENT_SYSCALL
:
320 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
322 case LTTNG_EVENT_ALL
:
323 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
326 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
330 /* Copy event name */
331 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
332 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
334 /* Setting up a kernel event */
338 lke
->filter_expression
= filter_expression
;
339 lke
->filter
= filter
;
344 free(filter_expression
);
352 * Allocate and initialize a kernel metadata.
354 * Return pointer to structure or NULL.
356 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
358 struct ltt_kernel_metadata
*lkm
;
359 struct lttng_channel
*chan
;
361 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
362 chan
= zmalloc(sizeof(struct lttng_channel
));
363 if (lkm
== NULL
|| chan
== NULL
) {
364 PERROR("kernel metadata zmalloc");
368 /* Set default attributes */
369 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
370 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
371 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
372 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
373 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
374 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
389 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
392 * Return pointer to structure or NULL.
394 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
398 struct ltt_kernel_stream
*lks
;
402 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
404 PERROR("kernel stream zmalloc");
409 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
411 PERROR("snprintf stream name");
414 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
428 * Cleanup kernel stream structure.
430 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
434 DBG("[trace] Closing stream fd %d", stream
->fd
);
435 /* Close kernel fd */
436 if (stream
->fd
>= 0) {
439 ret
= close(stream
->fd
);
444 /* Remove from stream list */
445 cds_list_del(&stream
->list
);
451 * Cleanup kernel event structure.
453 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
457 if (event
->fd
>= 0) {
460 DBG("[trace] Closing event fd %d", event
->fd
);
461 /* Close kernel fd */
462 ret
= close(event
->fd
);
467 DBG("[trace] Tearing down event (no associated fd)");
470 /* Remove from event list */
471 cds_list_del(&event
->list
);
473 free(event
->filter_expression
);
481 * Cleanup kernel context structure.
483 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
488 cds_list_del(&ctx
->list
);
494 * Cleanup kernel channel structure.
496 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
498 struct ltt_kernel_stream
*stream
, *stmp
;
499 struct ltt_kernel_event
*event
, *etmp
;
500 struct ltt_kernel_context
*ctx
, *ctmp
;
505 DBG("[trace] Closing channel fd %d", channel
->fd
);
506 /* Close kernel fd */
507 if (channel
->fd
>= 0) {
508 ret
= close(channel
->fd
);
514 /* For each stream in the channel list */
515 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
516 trace_kernel_destroy_stream(stream
);
519 /* For each event in the channel list */
520 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
521 trace_kernel_destroy_event(event
);
524 /* For each context in the channel list */
525 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
526 trace_kernel_destroy_context(ctx
);
529 /* Remove from channel list */
530 cds_list_del(&channel
->list
);
532 free(channel
->channel
);
537 * Cleanup kernel metadata structure.
539 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
543 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
544 /* Close kernel fd */
545 if (metadata
->fd
>= 0) {
548 ret
= close(metadata
->fd
);
554 free(metadata
->conf
);
559 * Cleanup kernel session structure
561 * Should *NOT* be called with RCU read-side lock held.
563 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
565 struct ltt_kernel_channel
*channel
, *ctmp
;
570 DBG("[trace] Closing session fd %d", session
->fd
);
571 /* Close kernel fds */
572 if (session
->fd
>= 0) {
573 ret
= close(session
->fd
);
579 if (session
->metadata_stream_fd
>= 0) {
580 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
581 ret
= close(session
->metadata_stream_fd
);
587 if (session
->metadata
!= NULL
) {
588 trace_kernel_destroy_metadata(session
->metadata
);
591 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
592 trace_kernel_destroy_channel(channel
);
595 /* Wipe consumer output object */
596 consumer_output_put(session
->consumer
);