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
;
38 if (session
== NULL
) {
39 ERR("Undefine session");
43 DBG("Trying to find channel %s", name
);
45 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
46 if (strcmp(name
, chan
->channel
->name
) == 0) {
47 DBG("Found channel by name %s", name
);
57 * Find the event name for the given channel.
59 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
60 char *name
, struct ltt_kernel_channel
*channel
)
62 struct ltt_kernel_event
*ev
;
64 if (channel
== NULL
) {
65 ERR("Undefine channel");
69 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
70 if (strcmp(name
, ev
->event
->name
) == 0) {
71 DBG("Found event by name %s for channel %s", name
,
72 channel
->channel
->name
);
82 * Allocate and initialize a kernel session data structure.
84 * Return pointer to structure or NULL.
86 struct ltt_kernel_session
*trace_kernel_create_session(char *path
)
88 struct ltt_kernel_session
*lks
= NULL
;
90 /* Allocate a new ltt kernel session */
91 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
93 PERROR("create kernel session zmalloc");
97 /* Init data structure */
99 lks
->metadata_stream_fd
= -1;
100 lks
->channel_count
= 0;
101 lks
->stream_count_global
= 0;
102 lks
->metadata
= NULL
;
103 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
105 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
106 if (lks
->consumer
== NULL
) {
111 * The tmp_consumer stays NULL until a set_consumer_uri command is
112 * executed. At this point, the consumer should be nullify until an
113 * enable_consumer command. This assignment is symbolic since we've zmalloc
116 lks
->tmp_consumer
= NULL
;
118 if (path
&& strlen(path
) > 0) {
121 /* Use the default consumer output which is the tracing session path. */
122 ret
= snprintf(lks
->consumer
->dst
.trace_path
, PATH_MAX
,
123 "%s" DEFAULT_KERNEL_TRACE_DIR
, path
);
125 PERROR("snprintf consumer trace path");
129 /* Set session path */
130 ret
= asprintf(&lks
->trace_path
, "%s" DEFAULT_KERNEL_TRACE_DIR
, path
);
132 PERROR("asprintf kernel traces path");
147 * Allocate and initialize a kernel channel data structure.
149 * Return pointer to structure or NULL.
151 struct ltt_kernel_channel
*trace_kernel_create_channel(
152 struct lttng_channel
*chan
, char *path
)
154 struct ltt_kernel_channel
*lkc
;
156 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
158 PERROR("ltt_kernel_channel zmalloc");
162 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
163 if (lkc
->channel
== NULL
) {
164 PERROR("lttng_channel zmalloc");
167 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
170 lkc
->stream_count
= 0;
171 lkc
->event_count
= 0;
174 /* Init linked list */
175 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
176 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
185 * Allocate and initialize a kernel event. Set name and event type.
187 * Return pointer to structure or NULL.
189 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
191 struct ltt_kernel_event
*lke
;
192 struct lttng_kernel_event
*attr
;
194 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
195 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
196 if (lke
== NULL
|| attr
== NULL
) {
197 PERROR("kernel event zmalloc");
202 case LTTNG_EVENT_PROBE
:
203 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
204 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
205 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
206 strncpy(attr
->u
.kprobe
.symbol_name
,
207 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
208 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
210 case LTTNG_EVENT_FUNCTION
:
211 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
212 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
213 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
214 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
215 strncpy(attr
->u
.kretprobe
.symbol_name
,
216 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
217 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
219 case LTTNG_EVENT_FUNCTION_ENTRY
:
220 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
221 strncpy(attr
->u
.ftrace
.symbol_name
,
222 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
223 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
225 case LTTNG_EVENT_TRACEPOINT
:
226 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
228 case LTTNG_EVENT_SYSCALL
:
229 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
231 case LTTNG_EVENT_ALL
:
232 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
235 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
239 /* Copy event name */
240 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
241 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
243 /* Setting up a kernel event */
257 * Allocate and initialize a kernel metadata.
259 * Return pointer to structure or NULL.
261 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
263 struct ltt_kernel_metadata
*lkm
;
264 struct lttng_channel
*chan
;
266 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
267 chan
= zmalloc(sizeof(struct lttng_channel
));
268 if (lkm
== NULL
|| chan
== NULL
) {
269 PERROR("kernel metadata zmalloc");
273 /* Set default attributes */
274 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
275 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
276 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
277 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
278 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
279 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
294 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
297 * Return pointer to structure or NULL.
299 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
303 struct ltt_kernel_stream
*lks
;
305 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
307 PERROR("kernel stream zmalloc");
312 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%d", name
, count
);
314 PERROR("snprintf stream name");
317 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
330 * Cleanup kernel stream structure.
332 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
334 DBG("[trace] Closing stream fd %d", stream
->fd
);
335 /* Close kernel fd */
336 if (stream
->fd
>= 0) {
339 ret
= close(stream
->fd
);
344 /* Remove from stream list */
345 cds_list_del(&stream
->list
);
351 * Cleanup kernel event structure.
353 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
355 if (event
->fd
>= 0) {
358 DBG("[trace] Closing event fd %d", event
->fd
);
359 /* Close kernel fd */
360 ret
= close(event
->fd
);
365 DBG("[trace] Tearing down event (no associated fd)");
368 /* Remove from event list */
369 cds_list_del(&event
->list
);
376 * Cleanup kernel channel structure.
378 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
380 struct ltt_kernel_stream
*stream
, *stmp
;
381 struct ltt_kernel_event
*event
, *etmp
;
384 DBG("[trace] Closing channel fd %d", channel
->fd
);
385 /* Close kernel fd */
386 if (channel
->fd
>= 0) {
387 ret
= close(channel
->fd
);
393 /* For each stream in the channel list */
394 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
395 trace_kernel_destroy_stream(stream
);
398 /* For each event in the channel list */
399 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
400 trace_kernel_destroy_event(event
);
403 /* Remove from channel list */
404 cds_list_del(&channel
->list
);
406 free(channel
->channel
);
412 * Cleanup kernel metadata structure.
414 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
416 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
417 /* Close kernel fd */
418 if (metadata
->fd
>= 0) {
421 ret
= close(metadata
->fd
);
427 free(metadata
->conf
);
432 * Cleanup kernel session structure
434 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
436 struct ltt_kernel_channel
*channel
, *ctmp
;
439 DBG("[trace] Closing session fd %d", session
->fd
);
440 /* Close kernel fds */
441 if (session
->fd
>= 0) {
442 ret
= close(session
->fd
);
448 if (session
->metadata_stream_fd
>= 0) {
449 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
450 ret
= close(session
->metadata_stream_fd
);
456 if (session
->metadata
!= NULL
) {
457 trace_kernel_destroy_metadata(session
->metadata
);
460 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
461 trace_kernel_destroy_channel(channel
);
464 /* Wipe consumer output object */
465 consumer_destroy_output(session
->consumer
);
466 consumer_destroy_output(session
->tmp_consumer
);
468 free(session
->trace_path
);