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>
27 #include "ltt-sessiond.h"
31 * get_kernel_channel_by_name
33 * Find the channel name for the given kernel session.
35 struct ltt_kernel_channel
*get_kernel_channel_by_name(
36 char *name
, struct ltt_kernel_session
*session
)
38 struct ltt_kernel_channel
*chan
;
40 if (session
== NULL
) {
41 ERR("Undefine session");
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 * get_kernel_event_by_name
59 * Find the event name for the given channel.
61 struct ltt_kernel_event
*get_kernel_event_by_name(
62 char *name
, struct ltt_kernel_channel
*channel
)
64 struct ltt_kernel_event
*ev
;
66 if (channel
== NULL
) {
67 ERR("Undefine channel");
71 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
72 if (strcmp(name
, ev
->event
->name
) == 0) {
73 DBG("Found event by name %s for channel %s", name
,
74 channel
->channel
->name
);
84 * trace_create_kernel_session
86 * Allocate and initialize a kernel session data structure.
88 * Return pointer to structure or NULL.
90 struct ltt_kernel_session
*trace_create_kernel_session(void)
92 struct ltt_kernel_session
*lks
;
94 /* Allocate a new ltt kernel session */
95 lks
= malloc(sizeof(struct ltt_kernel_session
));
97 perror("create kernel session malloc");
101 /* Init data structure */
103 lks
->metadata_stream_fd
= 0;
104 lks
->channel_count
= 0;
105 lks
->stream_count_global
= 0;
106 lks
->metadata
= NULL
;
107 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
116 * trace_create_kernel_channel
118 * Allocate and initialize a kernel channel data structure.
120 * Return pointer to structure or NULL.
122 struct ltt_kernel_channel
*trace_create_kernel_channel(struct lttng_channel
*chan
, char *path
)
125 struct ltt_kernel_channel
*lkc
;
127 lkc
= malloc(sizeof(struct ltt_kernel_channel
));
129 perror("ltt_kernel_channel malloc");
133 lkc
->channel
= malloc(sizeof(struct lttng_channel
));
134 if (lkc
->channel
== NULL
) {
135 perror("lttng_channel malloc");
138 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
141 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_KPROBES
:
180 attr
->instrumentation
= LTTNG_KERNEL_KPROBES
;
181 attr
->u
.kprobe
.addr
= ev
->attr
.kprobe
.addr
;
182 attr
->u
.kprobe
.offset
= ev
->attr
.kprobe
.offset
;
183 strncpy(attr
->u
.kprobe
.symbol_name
,
184 ev
->attr
.kprobe
.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_TRACEPOINTS
:
192 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINTS
;
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 */
214 * trace_create_kernel_metadata
216 * Allocate and initialize a kernel metadata.
218 * Return pointer to structure or NULL.
220 struct ltt_kernel_metadata
*trace_create_kernel_metadata(void)
223 struct ltt_kernel_metadata
*lkm
;
224 struct lttng_channel
*chan
;
226 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
227 chan
= malloc(sizeof(struct lttng_channel
));
228 if (lkm
== NULL
|| chan
== NULL
) {
229 perror("kernel metadata malloc");
233 /* Set default attributes */
234 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
235 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
236 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
237 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
238 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
239 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
244 /* Set default metadata path */
245 ret
= asprintf(&lkm
->pathname
, "%s/metadata", DEFAULT_TRACE_OUTPUT
);
247 perror("asprintf kernel metadata");
258 * trace_create_kernel_stream
260 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
263 * Return pointer to structure or NULL.
265 struct ltt_kernel_stream
*trace_create_kernel_stream(void)
267 struct ltt_kernel_stream
*lks
;
269 lks
= malloc(sizeof(struct ltt_kernel_stream
));
271 perror("kernel stream malloc");
277 lks
->pathname
= NULL
;
286 void trace_destroy_kernel_stream(struct ltt_kernel_stream
*stream
)
288 DBG("[trace] Closing stream fd %d", stream
->fd
);
289 /* Close kernel fd */
291 free(stream
->pathname
);
293 /* Remove from stream list */
294 cds_list_del(&stream
->list
);
298 void trace_destroy_kernel_event(struct ltt_kernel_event
*event
)
300 DBG("[trace] Closing event fd %d", event
->fd
);
301 /* Close kernel fd */
303 /* Free attributes */
306 /* Remove from event list */
307 cds_list_del(&event
->list
);
311 void trace_destroy_kernel_channel(struct ltt_kernel_channel
*channel
)
313 struct ltt_kernel_stream
*stream
;
314 struct ltt_kernel_event
*event
;
316 DBG("[trace] Closing channel fd %d", channel
->fd
);
317 /* Close kernel fd */
319 free(channel
->pathname
);
320 /* Free attributes structure */
321 free(channel
->channel
);
323 /* For each stream in the channel list */
324 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
325 trace_destroy_kernel_stream(stream
);
328 /* For each event in the channel list */
329 cds_list_for_each_entry(event
, &channel
->events_list
.head
, list
) {
330 trace_destroy_kernel_event(event
);
333 /* Remove from channel list */
334 cds_list_del(&channel
->list
);
338 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata
*metadata
)
340 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
341 /* Close kernel fd */
343 /* Free attributes */
344 free(metadata
->conf
);
349 void trace_destroy_kernel_session(struct ltt_kernel_session
*session
)
351 struct ltt_kernel_channel
*channel
;
353 DBG("[trace] Closing session fd %d", session
->fd
);
354 /* Close kernel fds */
356 if (session
->metadata_stream_fd
!= 0) {
357 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
358 close(session
->metadata_stream_fd
);
361 if (session
->metadata
!= NULL
) {
362 trace_destroy_kernel_metadata(session
->metadata
);
365 cds_list_for_each_entry(channel
, &session
->channel_list
.head
, list
) {
366 trace_destroy_kernel_channel(channel
);