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
));
244 CDS_INIT_LIST_HEAD(&kctx
->list
);
251 * Allocate and initialize a kernel event. Set name and event type.
252 * We own filter_expression, and filter.
254 * Return pointer to structure or NULL.
256 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
,
257 char *filter_expression
, struct lttng_filter_bytecode
*filter
)
259 struct ltt_kernel_event
*lke
;
260 struct lttng_kernel_event
*attr
;
264 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
265 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
266 if (lke
== NULL
|| attr
== NULL
) {
267 PERROR("kernel event zmalloc");
272 case LTTNG_EVENT_PROBE
:
273 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
274 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
275 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
276 strncpy(attr
->u
.kprobe
.symbol_name
,
277 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
278 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
280 case LTTNG_EVENT_FUNCTION
:
281 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
282 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
283 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
284 strncpy(attr
->u
.kretprobe
.symbol_name
,
285 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
286 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
288 case LTTNG_EVENT_FUNCTION_ENTRY
:
289 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
290 strncpy(attr
->u
.ftrace
.symbol_name
,
291 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
292 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
294 case LTTNG_EVENT_TRACEPOINT
:
295 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
297 case LTTNG_EVENT_SYSCALL
:
298 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
300 case LTTNG_EVENT_ALL
:
301 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
304 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
308 /* Copy event name */
309 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
310 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
312 /* Setting up a kernel event */
316 lke
->filter_expression
= filter_expression
;
317 lke
->filter
= filter
;
322 free(filter_expression
);
330 * Allocate and initialize a kernel metadata.
332 * Return pointer to structure or NULL.
334 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
336 struct ltt_kernel_metadata
*lkm
;
337 struct lttng_channel
*chan
;
339 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
340 chan
= zmalloc(sizeof(struct lttng_channel
));
341 if (lkm
== NULL
|| chan
== NULL
) {
342 PERROR("kernel metadata zmalloc");
346 /* Set default attributes */
347 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
348 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
349 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
350 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
351 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
352 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
367 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
370 * Return pointer to structure or NULL.
372 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
376 struct ltt_kernel_stream
*lks
;
380 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
382 PERROR("kernel stream zmalloc");
387 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
389 PERROR("snprintf stream name");
392 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
406 * Cleanup kernel stream structure.
408 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
412 DBG("[trace] Closing stream fd %d", stream
->fd
);
413 /* Close kernel fd */
414 if (stream
->fd
>= 0) {
417 ret
= close(stream
->fd
);
422 /* Remove from stream list */
423 cds_list_del(&stream
->list
);
429 * Cleanup kernel event structure.
431 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
435 if (event
->fd
>= 0) {
438 DBG("[trace] Closing event fd %d", event
->fd
);
439 /* Close kernel fd */
440 ret
= close(event
->fd
);
445 DBG("[trace] Tearing down event (no associated fd)");
448 /* Remove from event list */
449 cds_list_del(&event
->list
);
451 free(event
->filter_expression
);
459 * Cleanup kernel context structure.
461 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
465 cds_list_del(&ctx
->list
);
470 * Cleanup kernel channel structure.
472 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
474 struct ltt_kernel_stream
*stream
, *stmp
;
475 struct ltt_kernel_event
*event
, *etmp
;
476 struct ltt_kernel_context
*ctx
, *ctmp
;
481 DBG("[trace] Closing channel fd %d", channel
->fd
);
482 /* Close kernel fd */
483 if (channel
->fd
>= 0) {
484 ret
= close(channel
->fd
);
490 /* For each stream in the channel list */
491 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
492 trace_kernel_destroy_stream(stream
);
495 /* For each event in the channel list */
496 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
497 trace_kernel_destroy_event(event
);
500 /* For each context in the channel list */
501 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
502 trace_kernel_destroy_context(ctx
);
505 /* Remove from channel list */
506 cds_list_del(&channel
->list
);
508 free(channel
->channel
);
513 * Cleanup kernel metadata structure.
515 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
519 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
520 /* Close kernel fd */
521 if (metadata
->fd
>= 0) {
524 ret
= close(metadata
->fd
);
530 free(metadata
->conf
);
535 * Cleanup kernel session structure
537 * Should *NOT* be called with RCU read-side lock held.
539 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
541 struct ltt_kernel_channel
*channel
, *ctmp
;
546 DBG("[trace] Closing session fd %d", session
->fd
);
547 /* Close kernel fds */
548 if (session
->fd
>= 0) {
549 ret
= close(session
->fd
);
555 if (session
->metadata_stream_fd
>= 0) {
556 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
557 ret
= close(session
->metadata_stream_fd
);
563 if (session
->metadata
!= NULL
) {
564 trace_kernel_destroy_metadata(session
->metadata
);
567 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
568 trace_kernel_destroy_channel(channel
);
571 /* Wipe consumer output object */
572 consumer_output_put(session
->consumer
);