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.
25 #include <common/lttngerr.h>
26 #include <common/common.h>
27 #include <common/defaults.h>
29 #include "trace-kernel.h"
32 * Find the channel name for the given kernel session.
34 struct ltt_kernel_channel
*trace_kernel_get_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 DBG("Trying to find channel %s", name
);
46 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
47 if (strcmp(name
, chan
->channel
->name
) == 0) {
48 DBG("Found channel by name %s", name
);
58 * Find the event name for the given channel.
60 struct ltt_kernel_event
*trace_kernel_get_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 * Allocate and initialize a kernel session data structure.
85 * Return pointer to structure or NULL.
87 struct ltt_kernel_session
*trace_kernel_create_session(char *path
)
90 struct ltt_kernel_session
*lks
;
92 /* Allocate a new ltt kernel session */
93 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
95 perror("create kernel session zmalloc");
99 /* Init data structure */
101 lks
->metadata_stream_fd
= 0;
102 lks
->channel_count
= 0;
103 lks
->stream_count_global
= 0;
104 lks
->metadata
= NULL
;
105 lks
->consumer_fd
= 0;
106 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
108 /* Set session path */
109 ret
= asprintf(&lks
->trace_path
, "%s/kernel", path
);
111 perror("asprintf kernel traces path");
122 * Allocate and initialize a kernel channel data structure.
124 * Return pointer to structure or NULL.
126 struct ltt_kernel_channel
*trace_kernel_create_channel(struct lttng_channel
*chan
, char *path
)
129 struct ltt_kernel_channel
*lkc
;
131 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
133 perror("ltt_kernel_channel zmalloc");
137 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
138 if (lkc
->channel
== NULL
) {
139 perror("lttng_channel zmalloc");
142 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
145 lkc
->stream_count
= 0;
146 lkc
->event_count
= 0;
149 /* Init linked list */
150 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
151 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
152 /* Set default trace output path */
153 ret
= asprintf(&lkc
->pathname
, "%s", path
);
155 perror("asprintf kernel create channel");
166 * Allocate and initialize a kernel event. Set name and event type.
168 * Return pointer to structure or NULL.
170 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
172 struct ltt_kernel_event
*lke
;
173 struct lttng_kernel_event
*attr
;
175 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
176 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
177 if (lke
== NULL
|| attr
== NULL
) {
178 perror("kernel event zmalloc");
183 case LTTNG_EVENT_PROBE
:
184 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
185 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
186 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
187 strncpy(attr
->u
.kprobe
.symbol_name
,
188 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
189 attr
->u
.kprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
191 case LTTNG_EVENT_FUNCTION
:
192 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
193 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
194 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
195 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
196 strncpy(attr
->u
.kretprobe
.symbol_name
,
197 ev
->attr
.probe
.symbol_name
, LTTNG_SYM_NAME_LEN
);
198 attr
->u
.kretprobe
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
200 case LTTNG_EVENT_FUNCTION_ENTRY
:
201 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
202 strncpy(attr
->u
.ftrace
.symbol_name
,
203 ev
->attr
.ftrace
.symbol_name
, LTTNG_SYM_NAME_LEN
);
204 attr
->u
.ftrace
.symbol_name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
206 case LTTNG_EVENT_TRACEPOINT
:
207 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
209 case LTTNG_EVENT_SYSCALL
:
210 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
212 case LTTNG_EVENT_ALL
:
213 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
216 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
220 /* Copy event name */
221 strncpy(attr
->name
, ev
->name
, LTTNG_SYM_NAME_LEN
);
222 attr
->name
[LTTNG_SYM_NAME_LEN
- 1] = '\0';
224 /* Setting up a kernel event */
237 * Allocate and initialize a kernel metadata.
239 * Return pointer to structure or NULL.
241 struct ltt_kernel_metadata
*trace_kernel_create_metadata(char *path
)
244 struct ltt_kernel_metadata
*lkm
;
245 struct lttng_channel
*chan
;
247 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
248 chan
= zmalloc(sizeof(struct lttng_channel
));
249 if (lkm
== NULL
|| chan
== NULL
) {
250 perror("kernel metadata zmalloc");
254 /* Set default attributes */
255 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
256 chan
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
257 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
258 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
259 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
260 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
265 /* Set default metadata path */
266 ret
= asprintf(&lkm
->pathname
, "%s/metadata", path
);
268 perror("asprintf kernel metadata");
279 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
282 * Return pointer to structure or NULL.
284 struct ltt_kernel_stream
*trace_kernel_create_stream(void)
286 struct ltt_kernel_stream
*lks
;
288 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
290 perror("kernel stream zmalloc");
296 lks
->pathname
= NULL
;
306 * Cleanup kernel stream structure.
308 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
310 DBG("[trace] Closing stream fd %d", stream
->fd
);
311 /* Close kernel fd */
313 /* Remove from stream list */
314 cds_list_del(&stream
->list
);
316 free(stream
->pathname
);
321 * Cleanup kernel event structure.
323 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
325 DBG("[trace] Closing event fd %d", event
->fd
);
326 /* Close kernel fd */
329 /* Remove from event list */
330 cds_list_del(&event
->list
);
338 * Cleanup kernel channel structure.
340 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
342 struct ltt_kernel_stream
*stream
, *stmp
;
343 struct ltt_kernel_event
*event
, *etmp
;
345 DBG("[trace] Closing channel fd %d", channel
->fd
);
346 /* Close kernel fd */
349 /* For each stream in the channel list */
350 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
351 trace_kernel_destroy_stream(stream
);
354 /* For each event in the channel list */
355 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
356 trace_kernel_destroy_event(event
);
359 /* Remove from channel list */
360 cds_list_del(&channel
->list
);
362 free(channel
->pathname
);
363 free(channel
->channel
);
369 * Cleanup kernel metadata structure.
371 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
373 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
374 /* Close kernel fd */
377 free(metadata
->conf
);
378 free(metadata
->pathname
);
383 * Cleanup kernel session structure
385 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
387 struct ltt_kernel_channel
*channel
, *ctmp
;
389 DBG("[trace] Closing session fd %d", session
->fd
);
390 /* Close kernel fds */
393 if (session
->metadata_stream_fd
!= 0) {
394 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
395 close(session
->metadata_stream_fd
);
398 if (session
->metadata
!= NULL
) {
399 trace_kernel_destroy_metadata(session
->metadata
);
402 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
403 trace_kernel_destroy_channel(channel
);
406 free(session
->trace_path
);