2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <common/common.h>
26 #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
)
89 struct ltt_kernel_session
*lks
;
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
= 0;
101 lks
->channel_count
= 0;
102 lks
->stream_count_global
= 0;
103 lks
->metadata
= NULL
;
104 lks
->consumer_fd
= 0;
105 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
107 /* Set session path */
108 ret
= asprintf(&lks
->trace_path
, "%s/kernel", path
);
110 perror("asprintf kernel traces path");
121 * Allocate and initialize a kernel channel data structure.
123 * Return pointer to structure or NULL.
125 struct ltt_kernel_channel
*trace_kernel_create_channel(struct lttng_channel
*chan
, char *path
)
128 struct ltt_kernel_channel
*lkc
;
130 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
132 perror("ltt_kernel_channel zmalloc");
136 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
137 if (lkc
->channel
== NULL
) {
138 perror("lttng_channel zmalloc");
141 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
144 lkc
->stream_count
= 0;
145 lkc
->event_count
= 0;
148 /* Init linked list */
149 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
150 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
151 /* Set default trace output path */
152 ret
= asprintf(&lkc
->pathname
, "%s", path
);
154 perror("asprintf kernel create channel");
165 * Allocate and initialize a kernel event. Set name and event type.
167 * Return pointer to structure or NULL.
169 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
171 struct ltt_kernel_event
*lke
;
172 struct lttng_kernel_event
*attr
;
174 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
175 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
176 if (lke
== NULL
|| attr
== NULL
) {
177 perror("kernel event zmalloc");
182 case LTTNG_EVENT_PROBE
:
183 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
184 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
185 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
186 strncpy(attr
->u
.kprobe
.symbol_name
,
187 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
188 attr
->u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
190 case LTTNG_EVENT_FUNCTION
:
191 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
192 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
193 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
194 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
195 strncpy(attr
->u
.kretprobe
.symbol_name
,
196 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
197 attr
->u
.kretprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
199 case LTTNG_EVENT_FUNCTION_ENTRY
:
200 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
201 strncpy(attr
->u
.ftrace
.symbol_name
,
202 ev
->attr
.ftrace
.symbol_name
, LTTNG_SYM_NAME_LEN
);
203 attr
->u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
205 case LTTNG_EVENT_TRACEPOINT
:
206 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
208 case LTTNG_EVENT_SYSCALL
:
209 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
211 case LTTNG_EVENT_ALL
:
212 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
215 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
219 /* Copy event name */
220 strncpy(attr
->name
, ev
->name
, LTTNG_SYM_NAME_LEN
);
221 attr
->name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
223 /* Setting up a kernel event */
236 * Allocate and initialize a kernel metadata.
238 * Return pointer to structure or NULL.
240 struct ltt_kernel_metadata
*trace_kernel_create_metadata(char *path
)
243 struct ltt_kernel_metadata
*lkm
;
244 struct lttng_channel
*chan
;
246 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
247 chan
= zmalloc(sizeof(struct lttng_channel
));
248 if (lkm
== NULL
|| chan
== NULL
) {
249 perror("kernel metadata zmalloc");
253 /* Set default attributes */
254 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
255 chan
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
256 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
257 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
258 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
259 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
264 /* Set default metadata path */
265 ret
= asprintf(&lkm
->pathname
, "%s/metadata", path
);
267 perror("asprintf kernel metadata");
278 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
281 * Return pointer to structure or NULL.
283 struct ltt_kernel_stream
*trace_kernel_create_stream(void)
285 struct ltt_kernel_stream
*lks
;
287 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
289 perror("kernel stream zmalloc");
295 lks
->pathname
= NULL
;
305 * Cleanup kernel stream structure.
307 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
309 DBG("[trace] Closing stream fd %d", stream
->fd
);
310 /* Close kernel fd */
312 /* Remove from stream list */
313 cds_list_del(&stream
->list
);
315 free(stream
->pathname
);
320 * Cleanup kernel event structure.
322 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
324 DBG("[trace] Closing event fd %d", event
->fd
);
325 /* Close kernel fd */
328 /* Remove from event list */
329 cds_list_del(&event
->list
);
337 * Cleanup kernel channel structure.
339 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
341 struct ltt_kernel_stream
*stream
, *stmp
;
342 struct ltt_kernel_event
*event
, *etmp
;
344 DBG("[trace] Closing channel fd %d", channel
->fd
);
345 /* Close kernel fd */
348 /* For each stream in the channel list */
349 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
350 trace_kernel_destroy_stream(stream
);
353 /* For each event in the channel list */
354 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
355 trace_kernel_destroy_event(event
);
358 /* Remove from channel list */
359 cds_list_del(&channel
->list
);
361 free(channel
->pathname
);
362 free(channel
->channel
);
368 * Cleanup kernel metadata structure.
370 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
372 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
373 /* Close kernel fd */
376 free(metadata
->conf
);
377 free(metadata
->pathname
);
382 * Cleanup kernel session structure
384 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
386 struct ltt_kernel_channel
*channel
, *ctmp
;
388 DBG("[trace] Closing session fd %d", session
->fd
);
389 /* Close kernel fds */
392 if (session
->metadata_stream_fd
!= 0) {
393 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
394 close(session
->metadata_stream_fd
);
397 if (session
->metadata
!= NULL
) {
398 trace_kernel_destroy_metadata(session
->metadata
);
401 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
402 trace_kernel_destroy_channel(channel
);
405 free(session
->trace_path
);