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 lks
->consumer_fd
= 0;
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;
142 lkc
->event_count
= 0;
145 /* Init linked list */
146 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
147 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
148 /* Set default trace output path */
149 ret
= asprintf(&lkc
->pathname
, "%s", path
);
151 perror("asprintf kernel create channel");
162 * trace_create_kernel_event
164 * Allocate and initialize a kernel event. Set name and event type.
166 * Return pointer to structure or NULL.
168 struct ltt_kernel_event
*trace_create_kernel_event(struct lttng_event
*ev
)
170 struct ltt_kernel_event
*lke
;
171 struct lttng_kernel_event
*attr
;
173 lke
= malloc(sizeof(struct ltt_kernel_event
));
174 attr
= malloc(sizeof(struct lttng_kernel_event
));
175 if (lke
== NULL
|| attr
== NULL
) {
176 perror("kernel event malloc");
181 case LTTNG_EVENT_PROBE
:
182 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
183 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
184 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
185 strncpy(attr
->u
.kprobe
.symbol_name
,
186 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
187 attr
->u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
189 case LTTNG_EVENT_FUNCTION
:
190 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
191 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
192 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
193 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
194 strncpy(attr
->u
.kretprobe
.symbol_name
,
195 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
196 attr
->u
.kretprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
198 case LTTNG_EVENT_FUNCTION_ENTRY
:
199 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
200 strncpy(attr
->u
.ftrace
.symbol_name
,
201 ev
->attr
.ftrace
.symbol_name
, LTTNG_SYM_NAME_LEN
);
202 attr
->u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
204 case LTTNG_EVENT_TRACEPOINT
:
205 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
208 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
212 /* Copy event name */
213 strncpy(attr
->name
, ev
->name
, LTTNG_SYM_NAME_LEN
);
214 attr
->name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
216 /* Setting up a kernel event */
229 * trace_create_kernel_metadata
231 * Allocate and initialize a kernel metadata.
233 * Return pointer to structure or NULL.
235 struct ltt_kernel_metadata
*trace_create_kernel_metadata(char *path
)
238 struct ltt_kernel_metadata
*lkm
;
239 struct lttng_channel
*chan
;
241 lkm
= malloc(sizeof(struct ltt_kernel_metadata
));
242 chan
= malloc(sizeof(struct lttng_channel
));
243 if (lkm
== NULL
|| chan
== NULL
) {
244 perror("kernel metadata malloc");
248 /* Set default attributes */
249 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
250 chan
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
251 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
252 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
253 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
254 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
259 /* Set default metadata path */
260 ret
= asprintf(&lkm
->pathname
, "%s/metadata", path
);
262 perror("asprintf kernel metadata");
273 * trace_create_kernel_stream
275 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
278 * Return pointer to structure or NULL.
280 struct ltt_kernel_stream
*trace_create_kernel_stream(void)
282 struct ltt_kernel_stream
*lks
;
284 lks
= malloc(sizeof(struct ltt_kernel_stream
));
286 perror("kernel stream malloc");
292 lks
->pathname
= NULL
;
301 void trace_destroy_kernel_stream(struct ltt_kernel_stream
*stream
)
303 DBG("[trace] Closing stream fd %d", stream
->fd
);
304 /* Close kernel fd */
306 free(stream
->pathname
);
308 /* Remove from stream list */
309 cds_list_del(&stream
->list
);
313 void trace_destroy_kernel_event(struct ltt_kernel_event
*event
)
315 DBG("[trace] Closing event fd %d", event
->fd
);
316 /* Close kernel fd */
318 /* Free attributes */
321 /* Remove from event list */
322 cds_list_del(&event
->list
);
326 void trace_destroy_kernel_channel(struct ltt_kernel_channel
*channel
)
328 struct ltt_kernel_stream
*stream
, *stmp
;
329 struct ltt_kernel_event
*event
, *etmp
;
331 DBG("[trace] Closing channel fd %d", channel
->fd
);
332 /* Close kernel fd */
334 free(channel
->pathname
);
335 /* Free attributes structure */
336 free(channel
->channel
);
338 /* For each stream in the channel list */
339 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
340 trace_destroy_kernel_stream(stream
);
343 /* For each event in the channel list */
344 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
345 trace_destroy_kernel_event(event
);
348 /* Remove from channel list */
349 cds_list_del(&channel
->list
);
353 void trace_destroy_kernel_metadata(struct ltt_kernel_metadata
*metadata
)
355 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
356 /* Close kernel fd */
358 /* Free attributes */
359 free(metadata
->conf
);
364 void trace_destroy_kernel_session(struct ltt_kernel_session
*session
)
366 struct ltt_kernel_channel
*channel
, *ctmp
;
368 DBG("[trace] Closing session fd %d", session
->fd
);
369 /* Close kernel fds */
371 if (session
->metadata_stream_fd
!= 0) {
372 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
373 close(session
->metadata_stream_fd
);
376 if (session
->metadata
!= NULL
) {
377 trace_destroy_kernel_metadata(session
->metadata
);
380 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
381 trace_destroy_kernel_channel(channel
);