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; either version 2
7 * of the License, or (at your option) any later version.
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;
142 /* Init linked list */
143 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
144 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
145 /* Set default trace output path */
146 ret
= asprintf(&lkc
->pathname
, "%s", path
);
148 perror("asprintf kernel create channel");
159 * trace_create_kernel_event
161 * Allocate and initialize a kernel event. Set name and event type.
163 * Return pointer to structure or NULL.
165 struct ltt_kernel_event
*trace_create_kernel_event(struct lttng_event
*ev
)
167 struct ltt_kernel_event
*lke
;
168 struct lttng_kernel_event
*attr
;
170 lke
= malloc(sizeof(struct ltt_kernel_event
));
171 attr
= malloc(sizeof(struct lttng_kernel_event
));
172 if (lke
== NULL
|| attr
== NULL
) {
173 perror("kernel event malloc");
178 case LTTNG_EVENT_KPROBES
:
179 attr
->instrumentation
= LTTNG_KERNEL_KPROBES
;
180 attr
->u
.kprobe
.addr
= ev
->attr
.kprobe
.addr
;
181 attr
->u
.kprobe
.offset
= ev
->attr
.kprobe
.offset
;
182 strncpy(attr
->u
.kprobe
.symbol_name
,
183 ev
->attr
.kprobe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
185 case LTTNG_EVENT_FUNCTION
:
186 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
187 strncpy(attr
->u
.ftrace
.symbol_name
,
188 ev
->attr
.ftrace
.symbol_name
, LTTNG_SYM_NAME_LEN
);
190 case LTTNG_EVENT_TRACEPOINTS
:
191 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINTS
;
194 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
198 /* Copy event name */
199 strncpy(attr
->name
, ev
->name
, LTTNG_SYM_NAME_LEN
);
201 /* Setting up a kernel event */
213 * trace_create_kernel_metadata
215 * Allocate and initialize a kernel metadata.
217 * Return pointer to structure or NULL.
219 struct ltt_kernel_metadata
*trace_create_kernel_metadata(char *path
)
222 struct ltt_kernel_metadata
*lkm
;
223 struct lttng_channel
*chan
;
225 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
226 chan
= malloc(sizeof(struct lttng_channel
));
227 if (lkm
== NULL
|| chan
== NULL
) {
228 perror("kernel metadata malloc");
232 /* Set default attributes */
233 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
234 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
235 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
236 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
237 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
238 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
243 /* Set default metadata path */
244 ret
= asprintf(&lkm
->pathname
, "%s/metadata", path
);
246 perror("asprintf kernel metadata");
257 * trace_create_kernel_stream
259 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
262 * Return pointer to structure or NULL.
264 struct ltt_kernel_stream
*trace_create_kernel_stream(void)
266 struct ltt_kernel_stream
*lks
;
268 lks
= malloc(sizeof(struct ltt_kernel_stream
));
270 perror("kernel stream malloc");
276 lks
->pathname
= NULL
;
285 void trace_destroy_kernel_stream(struct ltt_kernel_stream
*stream
)
287 DBG("[trace] Closing stream fd %d", stream
->fd
);
288 /* Close kernel fd */
290 free(stream
->pathname
);
292 /* Remove from stream list */
293 cds_list_del(&stream
->list
);
297 void trace_destroy_kernel_event(struct ltt_kernel_event
*event
)
299 DBG("[trace] Closing event fd %d", event
->fd
);
300 /* Close kernel fd */
302 /* Free attributes */
305 /* Remove from event list */
306 cds_list_del(&event
->list
);
310 void trace_destroy_kernel_channel(struct ltt_kernel_channel
*channel
)
312 struct ltt_kernel_stream
*stream
;
313 struct ltt_kernel_event
*event
;
315 DBG("[trace] Closing channel fd %d", channel
->fd
);
316 /* Close kernel fd */
318 free(channel
->pathname
);
319 /* Free attributes structure */
320 free(channel
->channel
);
322 /* For each stream in the channel list */
323 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
324 trace_destroy_kernel_stream(stream
);
327 /* For each event in the channel list */
328 cds_list_for_each_entry(event
, &channel
->events_list
.head
, list
) {
329 trace_destroy_kernel_event(event
);
332 /* Remove from channel list */
333 cds_list_del(&channel
->list
);
337 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata
*metadata
)
339 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
340 /* Close kernel fd */
342 /* Free attributes */
343 free(metadata
->conf
);
348 void trace_destroy_kernel_session(struct ltt_kernel_session
*session
)
350 struct ltt_kernel_channel
*channel
;
352 DBG("[trace] Closing session fd %d", session
->fd
);
353 /* Close kernel fds */
355 if (session
->metadata_stream_fd
!= 0) {
356 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
357 close(session
->metadata_stream_fd
);
360 if (session
->metadata
!= NULL
) {
361 trace_destroy_kernel_metadata(session
->metadata
);
364 cds_list_for_each_entry(channel
, &session
->channel_list
.head
, list
) {
365 trace_destroy_kernel_channel(channel
);