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
,
66 enum lttng_event_type type
)
68 struct ltt_kernel_event
*ev
;
73 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
74 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
)
76 if (strcmp(name
, ev
->event
->name
) == 0) {
77 DBG("Found event by name %s for channel %s", name
,
78 channel
->channel
->name
);
87 * Allocate and initialize a kernel session data structure.
89 * Return pointer to structure or NULL.
91 struct ltt_kernel_session
*trace_kernel_create_session(void)
93 struct ltt_kernel_session
*lks
= NULL
;
95 /* Allocate a new ltt kernel session */
96 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
98 PERROR("create kernel session zmalloc");
102 /* Init data structure */
104 lks
->metadata_stream_fd
= -1;
105 lks
->channel_count
= 0;
106 lks
->stream_count_global
= 0;
107 lks
->metadata
= NULL
;
108 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
110 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
111 if (lks
->consumer
== NULL
) {
116 * The tmp_consumer stays NULL until a set_consumer_uri command is
117 * executed. At this point, the consumer should be nullify until an
118 * enable_consumer command. This assignment is symbolic since we've zmalloc
121 lks
->tmp_consumer
= NULL
;
133 * Allocate and initialize a kernel channel data structure.
135 * Return pointer to structure or NULL.
137 struct ltt_kernel_channel
*trace_kernel_create_channel(
138 struct lttng_channel
*chan
)
140 struct ltt_kernel_channel
*lkc
;
144 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
146 PERROR("ltt_kernel_channel zmalloc");
150 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
151 if (lkc
->channel
== NULL
) {
152 PERROR("lttng_channel zmalloc");
156 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
159 * If we receive an empty string for channel name, it means the
160 * default channel name is requested.
162 if (chan
->name
[0] == '\0') {
163 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
164 sizeof(lkc
->channel
->name
));
166 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
169 lkc
->stream_count
= 0;
170 lkc
->event_count
= 0;
172 /* Init linked list */
173 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
174 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
175 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
184 * Allocate and init a kernel context object.
186 * Return the allocated object or NULL on error.
188 struct ltt_kernel_context
*trace_kernel_create_context(
189 struct lttng_kernel_context
*ctx
)
191 struct ltt_kernel_context
*kctx
;
193 kctx
= zmalloc(sizeof(*kctx
));
195 PERROR("zmalloc kernel context");
200 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
203 CDS_INIT_LIST_HEAD(&kctx
->list
);
210 * Allocate and initialize a kernel event. Set name and event type.
212 * Return pointer to structure or NULL.
214 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
216 struct ltt_kernel_event
*lke
;
217 struct lttng_kernel_event
*attr
;
221 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
222 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
223 if (lke
== NULL
|| attr
== NULL
) {
224 PERROR("kernel event zmalloc");
229 case LTTNG_EVENT_PROBE
:
230 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
231 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
232 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
233 strncpy(attr
->u
.kprobe
.symbol_name
,
234 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
235 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
237 case LTTNG_EVENT_FUNCTION
:
238 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
239 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
240 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
241 strncpy(attr
->u
.kretprobe
.symbol_name
,
242 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
243 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
245 case LTTNG_EVENT_FUNCTION_ENTRY
:
246 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
247 strncpy(attr
->u
.ftrace
.symbol_name
,
248 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
249 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
251 case LTTNG_EVENT_TRACEPOINT
:
252 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
254 case LTTNG_EVENT_SYSCALL
:
255 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
257 case LTTNG_EVENT_ALL
:
258 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
261 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
265 /* Copy event name */
266 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
267 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
269 /* Setting up a kernel event */
283 * Allocate and initialize a kernel metadata.
285 * Return pointer to structure or NULL.
287 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
289 struct ltt_kernel_metadata
*lkm
;
290 struct lttng_channel
*chan
;
292 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
293 chan
= zmalloc(sizeof(struct lttng_channel
));
294 if (lkm
== NULL
|| chan
== NULL
) {
295 PERROR("kernel metadata zmalloc");
299 /* Set default attributes */
300 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
301 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
302 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
303 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
304 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
305 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
320 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
323 * Return pointer to structure or NULL.
325 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
329 struct ltt_kernel_stream
*lks
;
333 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
335 PERROR("kernel stream zmalloc");
340 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
342 PERROR("snprintf stream name");
345 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
359 * Cleanup kernel stream structure.
361 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
365 DBG("[trace] Closing stream fd %d", stream
->fd
);
366 /* Close kernel fd */
367 if (stream
->fd
>= 0) {
370 ret
= close(stream
->fd
);
375 /* Remove from stream list */
376 cds_list_del(&stream
->list
);
382 * Cleanup kernel event structure.
384 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
388 if (event
->fd
>= 0) {
391 DBG("[trace] Closing event fd %d", event
->fd
);
392 /* Close kernel fd */
393 ret
= close(event
->fd
);
398 DBG("[trace] Tearing down event (no associated fd)");
401 /* Remove from event list */
402 cds_list_del(&event
->list
);
409 * Cleanup kernel context structure.
411 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
415 cds_list_del(&ctx
->list
);
420 * Cleanup kernel channel structure.
422 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
424 struct ltt_kernel_stream
*stream
, *stmp
;
425 struct ltt_kernel_event
*event
, *etmp
;
426 struct ltt_kernel_context
*ctx
, *ctmp
;
431 DBG("[trace] Closing channel fd %d", channel
->fd
);
432 /* Close kernel fd */
433 if (channel
->fd
>= 0) {
434 ret
= close(channel
->fd
);
440 /* For each stream in the channel list */
441 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
442 trace_kernel_destroy_stream(stream
);
445 /* For each event in the channel list */
446 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
447 trace_kernel_destroy_event(event
);
450 /* For each context in the channel list */
451 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
452 trace_kernel_destroy_context(ctx
);
455 /* Remove from channel list */
456 cds_list_del(&channel
->list
);
458 free(channel
->channel
);
463 * Cleanup kernel metadata structure.
465 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
469 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
470 /* Close kernel fd */
471 if (metadata
->fd
>= 0) {
474 ret
= close(metadata
->fd
);
480 free(metadata
->conf
);
485 * Cleanup kernel session structure
487 * Should *NOT* be called with RCU read-side lock held.
489 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
491 struct ltt_kernel_channel
*channel
, *ctmp
;
496 DBG("[trace] Closing session fd %d", session
->fd
);
497 /* Close kernel fds */
498 if (session
->fd
>= 0) {
499 ret
= close(session
->fd
);
505 if (session
->metadata_stream_fd
>= 0) {
506 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
507 ret
= close(session
->metadata_stream_fd
);
513 if (session
->metadata
!= NULL
) {
514 trace_kernel_destroy_metadata(session
->metadata
);
517 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
518 trace_kernel_destroy_channel(channel
);
521 /* Wipe consumer output object */
522 consumer_destroy_output(session
->consumer
);
523 consumer_destroy_output(session
->tmp_consumer
);