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"
29 #include "lttng-sessiond.h"
30 #include "notification-thread-commands.h"
33 * Find the channel name for the given kernel session.
35 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
36 char *name
, struct ltt_kernel_session
*session
)
38 struct ltt_kernel_channel
*chan
;
44 * If we receive an empty string for channel name, it means the
45 * default channel name is requested.
48 name
= DEFAULT_CHANNEL_NAME
;
50 DBG("Trying to find channel %s", name
);
52 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
53 if (strcmp(name
, chan
->channel
->name
) == 0) {
54 DBG("Found channel by name %s", name
);
63 * Find the event for the given channel.
65 struct ltt_kernel_event
*trace_kernel_find_event(
66 char *name
, struct ltt_kernel_channel
*channel
,
67 enum lttng_event_type type
,
68 struct lttng_filter_bytecode
*filter
)
70 struct ltt_kernel_event
*ev
;
76 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
77 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
80 if (strcmp(name
, ev
->event
->name
)) {
83 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
86 if (ev
->filter
&& filter
) {
87 if (ev
->filter
->len
!= filter
->len
||
88 memcmp(ev
->filter
->data
, filter
->data
,
97 DBG("Found event %s for channel %s", name
,
98 channel
->channel
->name
);
106 * Find the event name for the given channel.
108 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
109 char *name
, struct ltt_kernel_channel
*channel
,
110 enum lttng_event_type type
)
112 struct ltt_kernel_event
*ev
;
118 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
119 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
122 if (strcmp(name
, ev
->event
->name
)) {
129 DBG("Found event %s for channel %s", name
,
130 channel
->channel
->name
);
138 * Allocate and initialize a kernel session data structure.
140 * Return pointer to structure or NULL.
142 struct ltt_kernel_session
*trace_kernel_create_session(void)
144 struct ltt_kernel_session
*lks
= NULL
;
146 /* Allocate a new ltt kernel session */
147 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
149 PERROR("create kernel session zmalloc");
153 /* Init data structure */
155 lks
->metadata_stream_fd
= -1;
156 lks
->channel_count
= 0;
157 lks
->stream_count_global
= 0;
158 lks
->metadata
= NULL
;
159 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
161 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
162 if (lks
->consumer
== NULL
) {
176 * Allocate and initialize a kernel channel data structure.
178 * Return pointer to structure or NULL.
180 struct ltt_kernel_channel
*trace_kernel_create_channel(
181 struct lttng_channel
*chan
)
183 struct ltt_kernel_channel
*lkc
;
184 struct lttng_channel_extended
*extended
= NULL
;
188 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
190 PERROR("ltt_kernel_channel zmalloc");
194 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
195 if (lkc
->channel
== NULL
) {
196 PERROR("lttng_channel zmalloc");
200 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
202 PERROR("lttng_channel_channel zmalloc");
205 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
206 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
207 lkc
->channel
->attr
.extended
.ptr
= extended
;
211 * If we receive an empty string for channel name, it means the
212 * default channel name is requested.
214 if (chan
->name
[0] == '\0') {
215 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
216 sizeof(lkc
->channel
->name
));
218 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
221 lkc
->stream_count
= 0;
222 lkc
->event_count
= 0;
224 lkc
->published_to_notification_thread
= false;
225 /* Init linked list */
226 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
227 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
228 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
242 * Allocate and init a kernel context object.
244 * Return the allocated object or NULL on error.
246 struct ltt_kernel_context
*trace_kernel_create_context(
247 struct lttng_kernel_context
*ctx
)
249 struct ltt_kernel_context
*kctx
;
251 kctx
= zmalloc(sizeof(*kctx
));
253 PERROR("zmalloc kernel context");
258 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
265 * Allocate and init a kernel context object from an existing kernel context
268 * Return the allocated object or NULL on error.
270 struct ltt_kernel_context
*trace_kernel_copy_context(
271 struct ltt_kernel_context
*kctx
)
273 struct ltt_kernel_context
*kctx_copy
;
276 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
278 PERROR("zmalloc ltt_kernel_context");
282 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
283 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
290 * Allocate and initialize a kernel event. Set name and event type.
291 * We own filter_expression, and filter.
293 * Return pointer to structure or NULL.
295 enum lttng_error_code
trace_kernel_create_event(
296 struct lttng_event
*ev
, char *filter_expression
,
297 struct lttng_filter_bytecode
*filter
,
298 struct ltt_kernel_event
**kernel_event
)
300 enum lttng_error_code ret
;
301 struct lttng_kernel_event
*attr
;
302 struct ltt_kernel_event
*local_kernel_event
;
306 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
307 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
308 if (local_kernel_event
== NULL
|| attr
== NULL
) {
309 PERROR("kernel event zmalloc");
310 ret
= LTTNG_ERR_NOMEM
;
315 case LTTNG_EVENT_PROBE
:
316 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
317 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
318 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
319 strncpy(attr
->u
.kprobe
.symbol_name
,
320 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
321 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
323 case LTTNG_EVENT_FUNCTION
:
324 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
325 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
326 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
327 strncpy(attr
->u
.kretprobe
.symbol_name
,
328 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
329 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
331 case LTTNG_EVENT_FUNCTION_ENTRY
:
332 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
333 strncpy(attr
->u
.ftrace
.symbol_name
,
334 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
335 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
337 case LTTNG_EVENT_TRACEPOINT
:
338 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
340 case LTTNG_EVENT_SYSCALL
:
341 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
343 case LTTNG_EVENT_ALL
:
344 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
347 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
348 ret
= LTTNG_ERR_INVALID
;
352 /* Copy event name */
353 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
354 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
356 /* Setting up a kernel event */
357 local_kernel_event
->fd
= -1;
358 local_kernel_event
->event
= attr
;
359 local_kernel_event
->enabled
= 1;
360 local_kernel_event
->filter_expression
= filter_expression
;
361 local_kernel_event
->filter
= filter
;
363 *kernel_event
= local_kernel_event
;
368 free(filter_expression
);
370 free(local_kernel_event
);
376 * Allocate and initialize a kernel metadata.
378 * Return pointer to structure or NULL.
380 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
382 struct ltt_kernel_metadata
*lkm
;
383 struct lttng_channel
*chan
;
385 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
386 chan
= zmalloc(sizeof(struct lttng_channel
));
387 if (lkm
== NULL
|| chan
== NULL
) {
388 PERROR("kernel metadata zmalloc");
392 /* Set default attributes */
393 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
394 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
395 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
396 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
397 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
398 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
413 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
416 * Return pointer to structure or NULL.
418 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
422 struct ltt_kernel_stream
*lks
;
426 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
428 PERROR("kernel stream zmalloc");
433 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
435 PERROR("snprintf stream name");
438 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
452 * Cleanup kernel stream structure.
454 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
458 DBG("[trace] Closing stream fd %d", stream
->fd
);
459 /* Close kernel fd */
460 if (stream
->fd
>= 0) {
463 ret
= close(stream
->fd
);
468 /* Remove from stream list */
469 cds_list_del(&stream
->list
);
475 * Cleanup kernel event structure.
477 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
481 if (event
->fd
>= 0) {
484 DBG("[trace] Closing event fd %d", event
->fd
);
485 /* Close kernel fd */
486 ret
= close(event
->fd
);
491 DBG("[trace] Tearing down event (no associated fd)");
494 /* Remove from event list */
495 cds_list_del(&event
->list
);
497 free(event
->filter_expression
);
505 * Cleanup kernel context structure.
507 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
512 cds_list_del(&ctx
->list
);
518 * Cleanup kernel channel structure.
520 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
522 struct ltt_kernel_stream
*stream
, *stmp
;
523 struct ltt_kernel_event
*event
, *etmp
;
524 struct ltt_kernel_context
*ctx
, *ctmp
;
526 enum lttng_error_code status
;
530 DBG("[trace] Closing channel fd %d", channel
->fd
);
531 /* Close kernel fd */
532 if (channel
->fd
>= 0) {
533 ret
= close(channel
->fd
);
539 /* For each stream in the channel list */
540 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
541 trace_kernel_destroy_stream(stream
);
544 /* For each event in the channel list */
545 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
546 trace_kernel_destroy_event(event
);
549 /* For each context in the channel list */
550 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
551 trace_kernel_destroy_context(ctx
);
554 /* Remove from channel list */
555 cds_list_del(&channel
->list
);
557 if (notification_thread_handle
558 && channel
->published_to_notification_thread
) {
559 status
= notification_thread_command_remove_channel(
560 notification_thread_handle
,
561 channel
->key
, LTTNG_DOMAIN_KERNEL
);
562 assert(status
== LTTNG_OK
);
564 free(channel
->channel
->attr
.extended
.ptr
);
565 free(channel
->channel
);
570 * Cleanup kernel metadata structure.
572 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
576 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
577 /* Close kernel fd */
578 if (metadata
->fd
>= 0) {
581 ret
= close(metadata
->fd
);
587 free(metadata
->conf
);
592 * Cleanup kernel session structure
594 * Should *NOT* be called with RCU read-side lock held.
596 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
598 struct ltt_kernel_channel
*channel
, *ctmp
;
603 DBG("[trace] Closing session fd %d", session
->fd
);
604 /* Close kernel fds */
605 if (session
->fd
>= 0) {
606 ret
= close(session
->fd
);
612 if (session
->metadata_stream_fd
>= 0) {
613 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
614 ret
= close(session
->metadata_stream_fd
);
620 if (session
->metadata
!= NULL
) {
621 trace_kernel_destroy_metadata(session
->metadata
);
624 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
625 trace_kernel_destroy_channel(channel
);
628 /* Wipe consumer output object */
629 consumer_output_put(session
->consumer
);