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.
30 * Find the channel name for the given kernel session.
32 struct ltt_kernel_channel
*get_kernel_channel_by_name(
33 char *name
, struct ltt_kernel_session
*session
)
35 struct ltt_kernel_channel
*chan
;
37 if (session
== NULL
) {
38 ERR("Undefine session");
42 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
43 if (strcmp(name
, chan
->channel
->name
) == 0) {
44 DBG("Found channel by name %s", name
);
54 * Find the event name for the given channel.
56 struct ltt_kernel_event
*get_kernel_event_by_name(
57 char *name
, struct ltt_kernel_channel
*channel
)
59 struct ltt_kernel_event
*ev
;
61 if (channel
== NULL
) {
62 ERR("Undefine channel");
66 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
67 if (strcmp(name
, ev
->event
->name
) == 0) {
68 DBG("Found event by name %s for channel %s", name
,
69 channel
->channel
->name
);
79 * Allocate and initialize a kernel session data structure.
81 * Return pointer to structure or NULL.
83 struct ltt_kernel_session
*trace_create_kernel_session(void)
85 struct ltt_kernel_session
*lks
;
87 /* Allocate a new ltt kernel session */
88 lks
= malloc(sizeof(struct ltt_kernel_session
));
90 perror("create kernel session malloc");
94 /* Init data structure */
96 lks
->metadata_stream_fd
= 0;
97 lks
->channel_count
= 0;
98 lks
->stream_count_global
= 0;
100 lks
->consumer_fd
= 0;
101 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
110 * Allocate and initialize a kernel channel data structure.
112 * Return pointer to structure or NULL.
114 struct ltt_kernel_channel
*trace_create_kernel_channel(struct lttng_channel
*chan
, char *path
)
117 struct ltt_kernel_channel
*lkc
;
119 lkc
= malloc(sizeof(struct ltt_kernel_channel
));
121 perror("ltt_kernel_channel malloc");
125 lkc
->channel
= malloc(sizeof(struct lttng_channel
));
126 if (lkc
->channel
== NULL
) {
127 perror("lttng_channel malloc");
130 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
133 lkc
->stream_count
= 0;
134 lkc
->event_count
= 0;
137 /* Init linked list */
138 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
139 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
140 /* Set default trace output path */
141 ret
= asprintf(&lkc
->pathname
, "%s", path
);
143 perror("asprintf kernel create channel");
154 * Allocate and initialize a kernel event. Set name and event type.
156 * Return pointer to structure or NULL.
158 struct ltt_kernel_event
*trace_create_kernel_event(struct lttng_event
*ev
)
160 struct ltt_kernel_event
*lke
;
161 struct lttng_kernel_event
*attr
;
163 lke
= malloc(sizeof(struct ltt_kernel_event
));
164 attr
= malloc(sizeof(struct lttng_kernel_event
));
165 if (lke
== NULL
|| attr
== NULL
) {
166 perror("kernel event malloc");
171 case LTTNG_EVENT_PROBE
:
172 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
173 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
174 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
175 strncpy(attr
->u
.kprobe
.symbol_name
,
176 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
177 attr
->u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
179 case LTTNG_EVENT_FUNCTION
:
180 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
181 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
182 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
183 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
184 strncpy(attr
->u
.kretprobe
.symbol_name
,
185 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
186 attr
->u
.kretprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
188 case LTTNG_EVENT_FUNCTION_ENTRY
:
189 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
190 strncpy(attr
->u
.ftrace
.symbol_name
,
191 ev
->attr
.ftrace
.symbol_name
, LTTNG_SYM_NAME_LEN
);
192 attr
->u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
194 case LTTNG_EVENT_TRACEPOINT
:
195 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
198 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
202 /* Copy event name */
203 strncpy(attr
->name
, ev
->name
, LTTNG_SYM_NAME_LEN
);
204 attr
->name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
206 /* Setting up a kernel event */
219 * Allocate and initialize a kernel metadata.
221 * Return pointer to structure or NULL.
223 struct ltt_kernel_metadata
*trace_create_kernel_metadata(char *path
)
226 struct ltt_kernel_metadata
*lkm
;
227 struct lttng_channel
*chan
;
229 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
230 chan
= malloc(sizeof(struct lttng_channel
));
231 if (lkm
== NULL
|| chan
== NULL
) {
232 perror("kernel metadata malloc");
236 /* Set default attributes */
237 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
238 chan
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
239 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
240 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
241 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
242 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
247 /* Set default metadata path */
248 ret
= asprintf(&lkm
->pathname
, "%s/metadata", path
);
250 perror("asprintf kernel metadata");
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
;
288 * Cleanup kernel stream structure.
290 void trace_destroy_kernel_stream(struct ltt_kernel_stream
*stream
)
292 DBG("[trace] Closing stream fd %d", stream
->fd
);
293 /* Close kernel fd */
295 free(stream
->pathname
);
297 /* Remove from stream list */
298 cds_list_del(&stream
->list
);
303 * Cleanup kernel event structure.
305 void trace_destroy_kernel_event(struct ltt_kernel_event
*event
)
307 DBG("[trace] Closing event fd %d", event
->fd
);
308 /* Close kernel fd */
310 /* Free attributes */
313 /* Remove from event list */
314 cds_list_del(&event
->list
);
319 * Cleanup kernel channel structure.
321 void trace_destroy_kernel_channel(struct ltt_kernel_channel
*channel
)
323 struct ltt_kernel_stream
*stream
, *stmp
;
324 struct ltt_kernel_event
*event
, *etmp
;
326 DBG("[trace] Closing channel fd %d", channel
->fd
);
327 /* Close kernel fd */
329 free(channel
->pathname
);
330 /* Free attributes structure */
331 free(channel
->channel
);
333 /* For each stream in the channel list */
334 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
335 trace_destroy_kernel_stream(stream
);
338 /* For each event in the channel list */
339 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
340 trace_destroy_kernel_event(event
);
343 /* Remove from channel list */
344 cds_list_del(&channel
->list
);
349 * Cleanup kernel metadata structure.
351 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata
*metadata
)
353 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
354 /* Close kernel fd */
356 /* Free attributes */
357 free(metadata
->conf
);
363 * Cleanup kernel session structure
365 void trace_destroy_kernel_session(struct ltt_kernel_session
*session
)
367 struct ltt_kernel_channel
*channel
, *ctmp
;
369 DBG("[trace] Closing session fd %d", session
->fd
);
370 /* Close kernel fds */
372 if (session
->metadata_stream_fd
!= 0) {
373 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
374 close(session
->metadata_stream_fd
);
377 if (session
->metadata
!= NULL
) {
378 trace_destroy_kernel_metadata(session
->metadata
);
381 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
382 trace_destroy_kernel_channel(channel
);