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 name for the given channel.
63 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
64 char *name
, struct ltt_kernel_channel
*channel
)
66 struct ltt_kernel_event
*ev
;
71 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
72 if (strcmp(name
, ev
->event
->name
) == 0) {
73 DBG("Found event by name %s for channel %s", name
,
74 channel
->channel
->name
);
83 * Allocate and initialize a kernel session data structure.
85 * Return pointer to structure or NULL.
87 struct ltt_kernel_session
*trace_kernel_create_session(void)
89 struct ltt_kernel_session
*lks
= NULL
;
91 /* Allocate a new ltt kernel session */
92 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
94 PERROR("create kernel session zmalloc");
98 /* Init data structure */
100 lks
->metadata_stream_fd
= -1;
101 lks
->channel_count
= 0;
102 lks
->stream_count_global
= 0;
103 lks
->metadata
= NULL
;
104 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
106 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
107 if (lks
->consumer
== NULL
) {
112 * The tmp_consumer stays NULL until a set_consumer_uri command is
113 * executed. At this point, the consumer should be nullify until an
114 * enable_consumer command. This assignment is symbolic since we've zmalloc
117 lks
->tmp_consumer
= NULL
;
129 * Allocate and initialize a kernel channel data structure.
131 * Return pointer to structure or NULL.
133 struct ltt_kernel_channel
*trace_kernel_create_channel(
134 struct lttng_channel
*chan
)
136 struct ltt_kernel_channel
*lkc
;
140 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
142 PERROR("ltt_kernel_channel zmalloc");
146 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
147 if (lkc
->channel
== NULL
) {
148 PERROR("lttng_channel zmalloc");
152 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
155 * If we receive an empty string for channel name, it means the
156 * default channel name is requested.
158 if (chan
->name
[0] == '\0') {
159 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
160 sizeof(lkc
->channel
->name
));
162 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
165 lkc
->stream_count
= 0;
166 lkc
->event_count
= 0;
168 /* Init linked list */
169 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
170 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
171 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
180 * Allocate and init a kernel context object.
182 * Return the allocated object or NULL on error.
184 struct ltt_kernel_context
*trace_kernel_create_context(
185 struct lttng_kernel_context
*ctx
)
187 struct ltt_kernel_context
*kctx
;
189 kctx
= zmalloc(sizeof(*kctx
));
191 PERROR("zmalloc kernel context");
196 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
199 CDS_INIT_LIST_HEAD(&kctx
->list
);
206 * Allocate and initialize a kernel event. Set name and event type.
208 * Return pointer to structure or NULL.
210 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
212 struct ltt_kernel_event
*lke
;
213 struct lttng_kernel_event
*attr
;
217 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
218 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
219 if (lke
== NULL
|| attr
== NULL
) {
220 PERROR("kernel event zmalloc");
225 case LTTNG_EVENT_PROBE
:
226 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
227 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
228 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
229 strncpy(attr
->u
.kprobe
.symbol_name
,
230 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
231 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
233 case LTTNG_EVENT_FUNCTION
:
234 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
235 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
236 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
237 strncpy(attr
->u
.kretprobe
.symbol_name
,
238 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
239 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
241 case LTTNG_EVENT_FUNCTION_ENTRY
:
242 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
243 strncpy(attr
->u
.ftrace
.symbol_name
,
244 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
245 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
247 case LTTNG_EVENT_TRACEPOINT
:
248 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
250 case LTTNG_EVENT_SYSCALL
:
251 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
253 case LTTNG_EVENT_ALL
:
254 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
257 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
261 /* Copy event name */
262 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
263 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
265 /* Setting up a kernel event */
279 * Allocate and initialize a kernel metadata.
281 * Return pointer to structure or NULL.
283 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
285 struct ltt_kernel_metadata
*lkm
;
286 struct lttng_channel
*chan
;
288 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
289 chan
= zmalloc(sizeof(struct lttng_channel
));
290 if (lkm
== NULL
|| chan
== NULL
) {
291 PERROR("kernel metadata zmalloc");
295 /* Set default attributes */
296 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
297 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
298 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
299 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
300 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
301 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
316 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
319 * Return pointer to structure or NULL.
321 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
325 struct ltt_kernel_stream
*lks
;
329 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
331 PERROR("kernel stream zmalloc");
336 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
338 PERROR("snprintf stream name");
341 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
355 * Cleanup kernel stream structure.
357 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
361 DBG("[trace] Closing stream fd %d", stream
->fd
);
362 /* Close kernel fd */
363 if (stream
->fd
>= 0) {
366 ret
= close(stream
->fd
);
371 /* Remove from stream list */
372 cds_list_del(&stream
->list
);
378 * Cleanup kernel event structure.
380 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
384 if (event
->fd
>= 0) {
387 DBG("[trace] Closing event fd %d", event
->fd
);
388 /* Close kernel fd */
389 ret
= close(event
->fd
);
394 DBG("[trace] Tearing down event (no associated fd)");
397 /* Remove from event list */
398 cds_list_del(&event
->list
);
405 * Cleanup kernel context structure.
407 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
411 cds_list_del(&ctx
->list
);
416 * Cleanup kernel channel structure.
418 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
420 struct ltt_kernel_stream
*stream
, *stmp
;
421 struct ltt_kernel_event
*event
, *etmp
;
422 struct ltt_kernel_context
*ctx
, *ctmp
;
427 DBG("[trace] Closing channel fd %d", channel
->fd
);
428 /* Close kernel fd */
429 if (channel
->fd
>= 0) {
430 ret
= close(channel
->fd
);
436 /* For each stream in the channel list */
437 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
438 trace_kernel_destroy_stream(stream
);
441 /* For each event in the channel list */
442 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
443 trace_kernel_destroy_event(event
);
446 /* For each context in the channel list */
447 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
448 trace_kernel_destroy_context(ctx
);
451 /* Remove from channel list */
452 cds_list_del(&channel
->list
);
454 free(channel
->channel
);
459 * Cleanup kernel metadata structure.
461 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
465 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
466 /* Close kernel fd */
467 if (metadata
->fd
>= 0) {
470 ret
= close(metadata
->fd
);
476 free(metadata
->conf
);
481 * Cleanup kernel session structure
483 * Should *NOT* be called with RCU read-side lock held.
485 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
487 struct ltt_kernel_channel
*channel
, *ctmp
;
492 DBG("[trace] Closing session fd %d", session
->fd
);
493 /* Close kernel fds */
494 if (session
->fd
>= 0) {
495 ret
= close(session
->fd
);
501 if (session
->metadata_stream_fd
>= 0) {
502 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
503 ret
= close(session
->metadata_stream_fd
);
509 if (session
->metadata
!= NULL
) {
510 trace_kernel_destroy_metadata(session
->metadata
);
513 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
514 trace_kernel_destroy_channel(channel
);
517 /* Wipe consumer output object */
518 consumer_destroy_output(session
->consumer
);
519 consumer_destroy_output(session
->tmp_consumer
);