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.
25 #include <common/common.h>
26 #include <common/defaults.h>
29 #include "trace-kernel.h"
32 * Find the channel name for the given kernel session.
34 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
35 char *name
, struct ltt_kernel_session
*session
)
37 struct ltt_kernel_channel
*chan
;
43 * If we receive an empty string for channel name, it means the
44 * default channel name is requested.
47 name
= DEFAULT_CHANNEL_NAME
;
49 DBG("Trying to find channel %s", name
);
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
);
62 * Find the event for the given channel.
64 struct ltt_kernel_event
*trace_kernel_find_event(
65 char *name
, struct ltt_kernel_channel
*channel
,
66 enum lttng_event_type type
,
67 struct lttng_filter_bytecode
*filter
)
69 struct ltt_kernel_event
*ev
;
75 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
76 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
79 if (strcmp(name
, ev
->event
->name
)) {
82 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
85 if (ev
->filter
&& filter
) {
86 if (ev
->filter
->len
!= filter
->len
||
87 memcmp(ev
->filter
->data
, filter
->data
,
96 DBG("Found event %s for channel %s", name
,
97 channel
->channel
->name
);
105 * Find the event name for the given channel.
107 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
108 char *name
, struct ltt_kernel_channel
*channel
,
109 enum lttng_event_type type
)
111 struct ltt_kernel_event
*ev
;
117 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
118 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
121 if (strcmp(name
, ev
->event
->name
)) {
128 DBG("Found event %s for channel %s", name
,
129 channel
->channel
->name
);
137 * Allocate and initialize a kernel session data structure.
139 * Return pointer to structure or NULL.
141 struct ltt_kernel_session
*trace_kernel_create_session(void)
143 struct ltt_kernel_session
*lks
= NULL
;
145 /* Allocate a new ltt kernel session */
146 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
148 PERROR("create kernel session zmalloc");
152 /* Init data structure */
154 lks
->metadata_stream_fd
= -1;
155 lks
->channel_count
= 0;
156 lks
->stream_count_global
= 0;
157 lks
->metadata
= NULL
;
158 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
160 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
161 if (lks
->consumer
== NULL
) {
166 * The tmp_consumer stays NULL until a set_consumer_uri command is
167 * executed. At this point, the consumer should be nullify until an
168 * enable_consumer command. This assignment is symbolic since we've zmalloc
171 lks
->tmp_consumer
= NULL
;
183 * Allocate and initialize a kernel channel data structure.
185 * Return pointer to structure or NULL.
187 struct ltt_kernel_channel
*trace_kernel_create_channel(
188 struct lttng_channel
*chan
)
190 struct ltt_kernel_channel
*lkc
;
194 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
196 PERROR("ltt_kernel_channel zmalloc");
200 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
201 if (lkc
->channel
== NULL
) {
202 PERROR("lttng_channel zmalloc");
206 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
209 * If we receive an empty string for channel name, it means the
210 * default channel name is requested.
212 if (chan
->name
[0] == '\0') {
213 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
214 sizeof(lkc
->channel
->name
));
216 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
219 lkc
->stream_count
= 0;
220 lkc
->event_count
= 0;
222 /* Init linked list */
223 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
224 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
225 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
234 * Allocate and init a kernel context object.
236 * Return the allocated object or NULL on error.
238 struct ltt_kernel_context
*trace_kernel_create_context(
239 struct lttng_kernel_context
*ctx
)
241 struct ltt_kernel_context
*kctx
;
243 kctx
= zmalloc(sizeof(*kctx
));
245 PERROR("zmalloc kernel context");
250 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
253 CDS_INIT_LIST_HEAD(&kctx
->list
);
260 * Allocate and initialize a kernel event. Set name and event type.
262 * Return pointer to structure or NULL.
264 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
,
265 char *filter_expression
, struct lttng_filter_bytecode
*filter
)
267 struct ltt_kernel_event
*lke
;
268 struct lttng_kernel_event
*attr
;
272 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
273 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
274 if (lke
== NULL
|| attr
== NULL
) {
275 PERROR("kernel event zmalloc");
280 case LTTNG_EVENT_PROBE
:
281 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
282 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
283 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
284 strncpy(attr
->u
.kprobe
.symbol_name
,
285 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
286 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
288 case LTTNG_EVENT_FUNCTION
:
289 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
290 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
291 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
292 strncpy(attr
->u
.kretprobe
.symbol_name
,
293 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
294 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
296 case LTTNG_EVENT_FUNCTION_ENTRY
:
297 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
298 strncpy(attr
->u
.ftrace
.symbol_name
,
299 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
300 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
302 case LTTNG_EVENT_TRACEPOINT
:
303 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
305 case LTTNG_EVENT_SYSCALL
:
306 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
308 case LTTNG_EVENT_ALL
:
309 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
312 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
316 /* Copy event name */
317 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
318 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
320 /* Setting up a kernel event */
324 lke
->filter_expression
= filter_expression
;
325 lke
->filter
= filter
;
336 * Allocate and initialize a kernel metadata.
338 * Return pointer to structure or NULL.
340 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
342 struct ltt_kernel_metadata
*lkm
;
343 struct lttng_channel
*chan
;
345 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
346 chan
= zmalloc(sizeof(struct lttng_channel
));
347 if (lkm
== NULL
|| chan
== NULL
) {
348 PERROR("kernel metadata zmalloc");
352 /* Set default attributes */
353 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
354 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
355 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
356 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
357 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
358 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
373 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
376 * Return pointer to structure or NULL.
378 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
382 struct ltt_kernel_stream
*lks
;
386 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
388 PERROR("kernel stream zmalloc");
393 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
395 PERROR("snprintf stream name");
398 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
412 * Cleanup kernel stream structure.
414 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
418 DBG("[trace] Closing stream fd %d", stream
->fd
);
419 /* Close kernel fd */
420 if (stream
->fd
>= 0) {
423 ret
= close(stream
->fd
);
428 /* Remove from stream list */
429 cds_list_del(&stream
->list
);
435 * Cleanup kernel event structure.
437 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
441 if (event
->fd
>= 0) {
444 DBG("[trace] Closing event fd %d", event
->fd
);
445 /* Close kernel fd */
446 ret
= close(event
->fd
);
451 DBG("[trace] Tearing down event (no associated fd)");
454 /* Remove from event list */
455 cds_list_del(&event
->list
);
457 free(event
->filter_expression
);
465 * Cleanup kernel context structure.
467 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
471 cds_list_del(&ctx
->list
);
476 * Cleanup kernel channel structure.
478 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
480 struct ltt_kernel_stream
*stream
, *stmp
;
481 struct ltt_kernel_event
*event
, *etmp
;
482 struct ltt_kernel_context
*ctx
, *ctmp
;
487 DBG("[trace] Closing channel fd %d", channel
->fd
);
488 /* Close kernel fd */
489 if (channel
->fd
>= 0) {
490 ret
= close(channel
->fd
);
496 /* For each stream in the channel list */
497 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
498 trace_kernel_destroy_stream(stream
);
501 /* For each event in the channel list */
502 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
503 trace_kernel_destroy_event(event
);
506 /* For each context in the channel list */
507 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
508 trace_kernel_destroy_context(ctx
);
511 /* Remove from channel list */
512 cds_list_del(&channel
->list
);
514 free(channel
->channel
);
519 * Cleanup kernel metadata structure.
521 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
525 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
526 /* Close kernel fd */
527 if (metadata
->fd
>= 0) {
530 ret
= close(metadata
->fd
);
536 free(metadata
->conf
);
541 * Cleanup kernel session structure
543 * Should *NOT* be called with RCU read-side lock held.
545 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
547 struct ltt_kernel_channel
*channel
, *ctmp
;
552 DBG("[trace] Closing session fd %d", session
->fd
);
553 /* Close kernel fds */
554 if (session
->fd
>= 0) {
555 ret
= close(session
->fd
);
561 if (session
->metadata_stream_fd
>= 0) {
562 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
563 ret
= close(session
->metadata_stream_fd
);
569 if (session
->metadata
!= NULL
) {
570 trace_kernel_destroy_metadata(session
->metadata
);
573 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
574 trace_kernel_destroy_channel(channel
);
577 /* Wipe consumer output object */
578 consumer_destroy_output(session
->consumer
);
579 consumer_destroy_output(session
->tmp_consumer
);