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 * trace_create_kernel_session
33 * Allocate and initialize a kernel session data structure.
35 * Return pointer to structure or NULL.
37 struct ltt_kernel_session
*trace_create_kernel_session(void)
39 struct ltt_kernel_session
*lks
;
41 /* Allocate a new ltt kernel session */
42 lks
= malloc(sizeof(struct ltt_kernel_session
));
44 perror("create kernel session malloc");
48 /* Init data structure */
50 lks
->metadata_stream_fd
= 0;
51 lks
->channel_count
= 0;
52 lks
->stream_count_global
= 0;
54 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
63 * trace_create_kernel_channel
65 * Allocate and initialize a kernel channel data structure.
67 * Return pointer to structure or NULL.
69 struct ltt_kernel_channel
*trace_create_kernel_channel(void)
72 struct ltt_kernel_channel
*lkc
;
73 struct lttng_kernel_channel
*chan
;
75 lkc
= malloc(sizeof(struct ltt_kernel_channel
));
76 chan
= malloc(sizeof(struct lttng_kernel_channel
));
77 if (lkc
== NULL
|| chan
== NULL
) {
78 perror("kernel channel malloc");
82 /* Default value to channel */
83 chan
->overwrite
= DEFAULT_KERNEL_OVERWRITE
;
84 chan
->subbuf_size
= DEFAULT_KERNEL_SUBBUF_SIZE
;
85 chan
->num_subbuf
= DEFAULT_KERNEL_SUBBUF_NUM
;
86 chan
->switch_timer_interval
= DEFAULT_KERNEL_SWITCH_TIMER
;
87 chan
->read_timer_interval
= DEFAULT_KERNEL_READ_TIMER
;
90 lkc
->stream_count
= 0;
92 /* Init linked list */
93 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
94 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
95 /* Set default trace output path */
96 ret
= asprintf(&lkc
->pathname
, "%s", DEFAULT_TRACE_OUTPUT
);
98 perror("asprintf kernel create channel");
109 * trace_create_kernel_event
111 * Allocate and initialize a kernel event. Set name and event type.
113 * Return pointer to structure or NULL.
115 struct ltt_kernel_event
*trace_create_kernel_event(char *name
,
116 enum lttng_kernel_instrumentation type
)
118 struct ltt_kernel_event
*lke
;
119 struct lttng_kernel_event
*attr
;
121 lke
= malloc(sizeof(struct ltt_kernel_event
));
122 attr
= malloc(sizeof(struct lttng_kernel_event
));
123 if (lke
== NULL
|| attr
== NULL
) {
124 perror("kernel event malloc");
128 /* Init event attribute */
129 attr
->instrumentation
= type
;
130 strncpy(attr
->name
, name
, LTTNG_SYM_NAME_LEN
);
131 /* Setting up a kernel event */
142 * trace_create_kernel_metadata
144 * Allocate and initialize a kernel metadata.
146 * Return pointer to structure or NULL.
148 struct ltt_kernel_metadata
*trace_create_kernel_metadata(void)
151 struct ltt_kernel_metadata
*lkm
;
152 struct lttng_kernel_channel
*attr
;
154 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
155 attr
= malloc(sizeof(struct lttng_kernel_channel
));
156 if (lkm
== NULL
|| attr
== NULL
) {
157 perror("kernel metadata malloc");
161 /* Set default attributes */
162 attr
->overwrite
= DEFAULT_KERNEL_OVERWRITE
;
163 attr
->subbuf_size
= DEFAULT_KERNEL_SUBBUF_SIZE
;
164 attr
->num_subbuf
= DEFAULT_KERNEL_SUBBUF_NUM
;
165 attr
->switch_timer_interval
= DEFAULT_KERNEL_SWITCH_TIMER
;
166 attr
->read_timer_interval
= DEFAULT_KERNEL_READ_TIMER
;
171 /* Set default metadata path */
172 ret
= asprintf(&lkm
->pathname
, "%s/metadata", DEFAULT_TRACE_OUTPUT
);
174 perror("asprintf kernel metadata");
185 * trace_create_kernel_stream
187 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
190 * Return pointer to structure or NULL.
192 struct ltt_kernel_stream
*trace_create_kernel_stream(void)
194 struct ltt_kernel_stream
*lks
;
196 lks
= malloc(sizeof(struct ltt_kernel_stream
));
198 perror("kernel stream malloc");
204 lks
->pathname
= NULL
;
213 void trace_destroy_kernel_stream(struct ltt_kernel_stream
*stream
)
215 DBG("[trace] Closing stream fd %d", stream
->fd
);
216 /* Close kernel fd */
218 free(stream
->pathname
);
220 /* Remove from stream list */
221 cds_list_del(&stream
->list
);
225 void trace_destroy_kernel_event(struct ltt_kernel_event
*event
)
227 DBG("[trace] Closing event fd %d", event
->fd
);
228 /* Close kernel fd */
230 /* Free attributes */
233 /* Remove from event list */
234 cds_list_del(&event
->list
);
238 void trace_destroy_kernel_channel(struct ltt_kernel_channel
*channel
)
240 struct ltt_kernel_stream
*stream
;
241 struct ltt_kernel_event
*event
;
243 DBG("[trace] Closing channel fd %d", channel
->fd
);
244 /* Close kernel fd */
246 free(channel
->pathname
);
247 /* Free attributes structure */
248 free(channel
->channel
);
250 /* For each stream in the channel list */
251 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
252 trace_destroy_kernel_stream(stream
);
255 /* For each event in the channel list */
256 cds_list_for_each_entry(event
, &channel
->events_list
.head
, list
) {
257 trace_destroy_kernel_event(event
);
260 /* Remove from channel list */
261 cds_list_del(&channel
->list
);
265 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata
*metadata
)
267 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
268 /* Close kernel fd */
270 /* Free attributes */
271 free(metadata
->conf
);
276 void trace_destroy_kernel_session(struct ltt_kernel_session
*session
)
278 struct ltt_kernel_channel
*channel
;
280 DBG("[trace] Closing session fd %d", session
->fd
);
281 /* Close kernel fds */
283 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
284 close(session
->metadata_stream_fd
);
286 trace_destroy_kernel_metadata(session
->metadata
);
288 cds_list_for_each_entry(channel
, &session
->channel_list
.head
, list
) {
289 trace_destroy_kernel_channel(channel
);