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 name for the given channel.
64 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
65 char *name
, struct ltt_kernel_channel
*channel
)
67 struct ltt_kernel_event
*ev
;
72 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
73 if (strcmp(name
, ev
->event
->name
) == 0) {
74 DBG("Found event by name %s for channel %s", name
,
75 channel
->channel
->name
);
84 * Allocate and initialize a kernel session data structure.
86 * Return pointer to structure or NULL.
88 struct ltt_kernel_session
*trace_kernel_create_session(void)
90 struct ltt_kernel_session
*lks
= NULL
;
92 /* Allocate a new ltt kernel session */
93 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
95 PERROR("create kernel session zmalloc");
99 /* Init data structure */
101 lks
->metadata_stream_fd
= -1;
102 lks
->channel_count
= 0;
103 lks
->stream_count_global
= 0;
104 lks
->metadata
= NULL
;
105 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
107 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
108 if (lks
->consumer
== NULL
) {
113 * The tmp_consumer stays NULL until a set_consumer_uri command is
114 * executed. At this point, the consumer should be nullify until an
115 * enable_consumer command. This assignment is symbolic since we've zmalloc
118 lks
->tmp_consumer
= NULL
;
130 * Allocate and initialize a kernel channel data structure.
132 * Return pointer to structure or NULL.
134 struct ltt_kernel_channel
*trace_kernel_create_channel(
135 struct lttng_channel
*chan
)
137 struct ltt_kernel_channel
*lkc
;
141 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
143 PERROR("ltt_kernel_channel zmalloc");
147 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
148 if (lkc
->channel
== NULL
) {
149 PERROR("lttng_channel zmalloc");
153 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
156 * If we receive an empty string for channel name, it means the
157 * default channel name is requested.
159 if (chan
->name
[0] == '\0') {
160 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
161 sizeof(lkc
->channel
->name
));
163 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
166 lkc
->stream_count
= 0;
167 lkc
->event_count
= 0;
169 /* Init linked list */
170 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
171 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
172 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
181 * Allocate and init a kernel context object.
183 * Return the allocated object or NULL on error.
185 struct ltt_kernel_context
*trace_kernel_create_context(
186 struct lttng_kernel_context
*ctx
)
188 struct ltt_kernel_context
*kctx
;
190 kctx
= zmalloc(sizeof(*kctx
));
192 PERROR("zmalloc kernel context");
197 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
200 CDS_INIT_LIST_HEAD(&kctx
->list
);
207 * Allocate and initialize a kernel event. Set name and event type.
209 * Return pointer to structure or NULL.
211 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
213 struct ltt_kernel_event
*lke
;
214 struct lttng_kernel_event
*attr
;
218 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
219 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
220 if (lke
== NULL
|| attr
== NULL
) {
221 PERROR("kernel event zmalloc");
226 case LTTNG_EVENT_PROBE
:
227 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
228 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
229 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
230 strncpy(attr
->u
.kprobe
.symbol_name
,
231 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
232 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
234 case LTTNG_EVENT_FUNCTION
:
235 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
236 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
237 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
238 strncpy(attr
->u
.kretprobe
.symbol_name
,
239 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
240 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
242 case LTTNG_EVENT_FUNCTION_ENTRY
:
243 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
244 strncpy(attr
->u
.ftrace
.symbol_name
,
245 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
246 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
248 case LTTNG_EVENT_TRACEPOINT
:
249 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
251 case LTTNG_EVENT_SYSCALL
:
252 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
254 case LTTNG_EVENT_ALL
:
255 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
258 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
262 /* Copy event name */
263 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
264 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
266 /* Setting up a kernel event */
280 * Allocate and initialize a kernel metadata.
282 * Return pointer to structure or NULL.
284 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
286 struct ltt_kernel_metadata
*lkm
;
287 struct lttng_channel
*chan
;
289 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
290 chan
= zmalloc(sizeof(struct lttng_channel
));
291 if (lkm
== NULL
|| chan
== NULL
) {
292 PERROR("kernel metadata zmalloc");
296 /* Set default attributes */
297 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
298 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
299 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
300 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
301 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
302 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
317 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
320 * Return pointer to structure or NULL.
322 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
326 struct ltt_kernel_stream
*lks
;
330 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
332 PERROR("kernel stream zmalloc");
337 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
339 PERROR("snprintf stream name");
342 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
356 * Cleanup kernel stream structure.
358 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
362 DBG("[trace] Closing stream fd %d", stream
->fd
);
363 /* Close kernel fd */
364 if (stream
->fd
>= 0) {
367 ret
= close(stream
->fd
);
372 /* Remove from stream list */
373 cds_list_del(&stream
->list
);
379 * Cleanup kernel event structure.
381 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
385 if (event
->fd
>= 0) {
388 DBG("[trace] Closing event fd %d", event
->fd
);
389 /* Close kernel fd */
390 ret
= close(event
->fd
);
395 DBG("[trace] Tearing down event (no associated fd)");
398 /* Remove from event list */
399 cds_list_del(&event
->list
);
406 * Cleanup kernel context structure.
408 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
412 cds_list_del(&ctx
->list
);
417 * Cleanup kernel channel structure.
419 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
421 struct ltt_kernel_stream
*stream
, *stmp
;
422 struct ltt_kernel_event
*event
, *etmp
;
423 struct ltt_kernel_context
*ctx
, *ctmp
;
428 DBG("[trace] Closing channel fd %d", channel
->fd
);
429 /* Close kernel fd */
430 if (channel
->fd
>= 0) {
431 ret
= close(channel
->fd
);
437 /* For each stream in the channel list */
438 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
439 trace_kernel_destroy_stream(stream
);
442 /* For each event in the channel list */
443 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
444 trace_kernel_destroy_event(event
);
447 /* For each context in the channel list */
448 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
449 trace_kernel_destroy_context(ctx
);
452 /* Remove from channel list */
453 cds_list_del(&channel
->list
);
455 free(channel
->channel
);
460 * Cleanup kernel metadata structure.
462 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
466 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
467 /* Close kernel fd */
468 if (metadata
->fd
>= 0) {
471 ret
= close(metadata
->fd
);
477 free(metadata
->conf
);
482 * Cleanup kernel session structure
484 * Should *NOT* be called with RCU read-side lock held.
486 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
488 struct ltt_kernel_channel
*channel
, *ctmp
;
493 DBG("[trace] Closing session fd %d", session
->fd
);
494 /* Close kernel fds */
495 if (session
->fd
>= 0) {
496 ret
= close(session
->fd
);
502 if (session
->metadata_stream_fd
>= 0) {
503 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
504 ret
= close(session
->metadata_stream_fd
);
510 if (session
->metadata
!= NULL
) {
511 trace_kernel_destroy_metadata(session
->metadata
);
514 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
515 trace_kernel_destroy_channel(channel
);
518 /* Wipe consumer output object */
519 consumer_destroy_output(session
->consumer
);
520 consumer_destroy_output(session
->tmp_consumer
);