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.
24 #include <urcu/list.h>
30 * get_kernel_channel_by_name
32 * Find the channel name for the given kernel session.
34 struct ltt_kernel_channel
*get_kernel_channel_by_name(
35 char *name
, struct ltt_kernel_session
*session
)
37 struct ltt_kernel_channel
*chan
;
39 if (session
== NULL
) {
40 ERR("Undefine session");
44 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
45 if (strcmp(name
, chan
->channel
->name
) == 0) {
46 DBG("Found channel by name %s", name
);
56 * get_kernel_event_by_name
58 * Find the event name for the given channel.
60 struct ltt_kernel_event
*get_kernel_event_by_name(
61 char *name
, struct ltt_kernel_channel
*channel
)
63 struct ltt_kernel_event
*ev
;
65 if (channel
== NULL
) {
66 ERR("Undefine channel");
70 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
71 if (strcmp(name
, ev
->event
->name
) == 0) {
72 DBG("Found event by name %s for channel %s", name
,
73 channel
->channel
->name
);
83 * trace_create_kernel_session
85 * Allocate and initialize a kernel session data structure.
87 * Return pointer to structure or NULL.
89 struct ltt_kernel_session
*trace_create_kernel_session(void)
91 struct ltt_kernel_session
*lks
;
93 /* Allocate a new ltt kernel session */
94 lks
= malloc(sizeof(struct ltt_kernel_session
));
96 perror("create kernel session malloc");
100 /* Init data structure */
102 lks
->metadata_stream_fd
= 0;
103 lks
->channel_count
= 0;
104 lks
->stream_count_global
= 0;
105 lks
->metadata
= NULL
;
106 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
115 * trace_create_kernel_channel
117 * Allocate and initialize a kernel channel data structure.
119 * Return pointer to structure or NULL.
121 struct ltt_kernel_channel
*trace_create_kernel_channel(struct lttng_channel
*chan
, char *path
)
124 struct ltt_kernel_channel
*lkc
;
126 lkc
= malloc(sizeof(struct ltt_kernel_channel
));
128 perror("ltt_kernel_channel malloc");
132 lkc
->channel
= malloc(sizeof(struct lttng_channel
));
133 if (lkc
->channel
== NULL
) {
134 perror("lttng_channel malloc");
137 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
140 lkc
->stream_count
= 0;
143 /* Init linked list */
144 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
145 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
146 /* Set default trace output path */
147 ret
= asprintf(&lkc
->pathname
, "%s", path
);
149 perror("asprintf kernel create channel");
160 * trace_create_kernel_event
162 * Allocate and initialize a kernel event. Set name and event type.
164 * Return pointer to structure or NULL.
166 struct ltt_kernel_event
*trace_create_kernel_event(struct lttng_event
*ev
)
168 struct ltt_kernel_event
*lke
;
169 struct lttng_kernel_event
*attr
;
171 lke
= malloc(sizeof(struct ltt_kernel_event
));
172 attr
= malloc(sizeof(struct lttng_kernel_event
));
173 if (lke
== NULL
|| attr
== NULL
) {
174 perror("kernel event malloc");
179 case LTTNG_EVENT_PROBE
:
180 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
181 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
182 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
183 strncpy(attr
->u
.kprobe
.symbol_name
,
184 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
186 case LTTNG_EVENT_FUNCTION
:
187 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
188 strncpy(attr
->u
.ftrace
.symbol_name
,
189 ev
->attr
.ftrace
.symbol_name
, LTTNG_SYM_NAME_LEN
);
191 case LTTNG_EVENT_TRACEPOINT
:
192 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
195 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
199 /* Copy event name */
200 strncpy(attr
->name
, ev
->name
, LTTNG_SYM_NAME_LEN
);
202 /* Setting up a kernel event */
215 * trace_create_kernel_metadata
217 * Allocate and initialize a kernel metadata.
219 * Return pointer to structure or NULL.
221 struct ltt_kernel_metadata
*trace_create_kernel_metadata(char *path
)
224 struct ltt_kernel_metadata
*lkm
;
225 struct lttng_channel
*chan
;
227 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
228 chan
= malloc(sizeof(struct lttng_channel
));
229 if (lkm
== NULL
|| chan
== NULL
) {
230 perror("kernel metadata malloc");
234 /* Set default attributes */
235 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
236 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
237 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
238 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
239 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
240 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
245 /* Set default metadata path */
246 ret
= asprintf(&lkm
->pathname
, "%s/metadata", path
);
248 perror("asprintf kernel metadata");
259 * trace_create_kernel_stream
261 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
264 * Return pointer to structure or NULL.
266 struct ltt_kernel_stream
*trace_create_kernel_stream(void)
268 struct ltt_kernel_stream
*lks
;
270 lks
= malloc(sizeof(struct ltt_kernel_stream
));
272 perror("kernel stream malloc");
278 lks
->pathname
= NULL
;
287 void trace_destroy_kernel_stream(struct ltt_kernel_stream
*stream
)
289 DBG("[trace] Closing stream fd %d", stream
->fd
);
290 /* Close kernel fd */
292 free(stream
->pathname
);
294 /* Remove from stream list */
295 cds_list_del(&stream
->list
);
299 void trace_destroy_kernel_event(struct ltt_kernel_event
*event
)
301 DBG("[trace] Closing event fd %d", event
->fd
);
302 /* Close kernel fd */
304 /* Free attributes */
307 /* Remove from event list */
308 cds_list_del(&event
->list
);
312 void trace_destroy_kernel_channel(struct ltt_kernel_channel
*channel
)
314 struct ltt_kernel_stream
*stream
;
315 struct ltt_kernel_event
*event
;
317 DBG("[trace] Closing channel fd %d", channel
->fd
);
318 /* Close kernel fd */
320 free(channel
->pathname
);
321 /* Free attributes structure */
322 free(channel
->channel
);
324 /* For each stream in the channel list */
325 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
326 trace_destroy_kernel_stream(stream
);
329 /* For each event in the channel list */
330 cds_list_for_each_entry(event
, &channel
->events_list
.head
, list
) {
331 trace_destroy_kernel_event(event
);
334 /* Remove from channel list */
335 cds_list_del(&channel
->list
);
339 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata
*metadata
)
341 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
342 /* Close kernel fd */
344 /* Free attributes */
345 free(metadata
->conf
);
350 void trace_destroy_kernel_session(struct ltt_kernel_session
*session
)
352 struct ltt_kernel_channel
*channel
;
354 DBG("[trace] Closing session fd %d", session
->fd
);
355 /* Close kernel fds */
357 if (session
->metadata_stream_fd
!= 0) {
358 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
359 close(session
->metadata_stream_fd
);
362 if (session
->metadata
!= NULL
) {
363 trace_destroy_kernel_metadata(session
->metadata
);
366 cds_list_for_each_entry(channel
, &session
->channel_list
.head
, list
) {
367 trace_destroy_kernel_channel(channel
);