2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <common/common.h>
25 #include <common/defaults.h>
28 #include "trace-kernel.h"
31 * Find the channel name for the given kernel session.
33 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
34 char *name
, struct ltt_kernel_session
*session
)
36 struct ltt_kernel_channel
*chan
;
41 DBG("Trying to find channel %s", name
);
43 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
44 if (strcmp(name
, chan
->channel
->name
) == 0) {
45 DBG("Found channel by name %s", name
);
54 * Find the event name for the given channel.
56 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
57 char *name
, struct ltt_kernel_channel
*channel
)
59 struct ltt_kernel_event
*ev
;
64 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
65 if (strcmp(name
, ev
->event
->name
) == 0) {
66 DBG("Found event by name %s for channel %s", name
,
67 channel
->channel
->name
);
76 * Allocate and initialize a kernel session data structure.
78 * Return pointer to structure or NULL.
80 struct ltt_kernel_session
*trace_kernel_create_session(char *path
)
82 struct ltt_kernel_session
*lks
= NULL
;
84 /* Allocate a new ltt kernel session */
85 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
87 PERROR("create kernel session zmalloc");
91 /* Init data structure */
93 lks
->metadata_stream_fd
= -1;
94 lks
->channel_count
= 0;
95 lks
->stream_count_global
= 0;
97 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
99 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
100 if (lks
->consumer
== NULL
) {
105 * The tmp_consumer stays NULL until a set_consumer_uri command is
106 * executed. At this point, the consumer should be nullify until an
107 * enable_consumer command. This assignment is symbolic since we've zmalloc
110 lks
->tmp_consumer
= NULL
;
115 /* Use the default consumer output which is the tracing session path. */
116 ret
= snprintf(lks
->consumer
->dst
.trace_path
, PATH_MAX
,
117 "%s" DEFAULT_KERNEL_TRACE_DIR
, path
);
119 PERROR("snprintf consumer trace path");
123 /* Set session path */
124 ret
= asprintf(&lks
->trace_path
, "%s" DEFAULT_KERNEL_TRACE_DIR
, path
);
126 PERROR("asprintf kernel traces path");
141 * Allocate and initialize a kernel channel data structure.
143 * Return pointer to structure or NULL.
145 struct ltt_kernel_channel
*trace_kernel_create_channel(
146 struct lttng_channel
*chan
)
148 struct ltt_kernel_channel
*lkc
;
152 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
154 PERROR("ltt_kernel_channel zmalloc");
158 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
159 if (lkc
->channel
== NULL
) {
160 PERROR("lttng_channel zmalloc");
163 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
166 lkc
->stream_count
= 0;
167 lkc
->event_count
= 0;
170 /* Init linked list */
171 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
172 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
181 * Allocate and initialize a kernel event. Set name and event type.
183 * Return pointer to structure or NULL.
185 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
)
187 struct ltt_kernel_event
*lke
;
188 struct lttng_kernel_event
*attr
;
192 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
193 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
194 if (lke
== NULL
|| attr
== NULL
) {
195 PERROR("kernel event zmalloc");
200 case LTTNG_EVENT_PROBE
:
201 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
202 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
203 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
204 strncpy(attr
->u
.kprobe
.symbol_name
,
205 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
206 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
208 case LTTNG_EVENT_FUNCTION
:
209 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
210 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
211 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
212 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
213 strncpy(attr
->u
.kretprobe
.symbol_name
,
214 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
215 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
217 case LTTNG_EVENT_FUNCTION_ENTRY
:
218 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
219 strncpy(attr
->u
.ftrace
.symbol_name
,
220 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
221 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
223 case LTTNG_EVENT_TRACEPOINT
:
224 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
226 case LTTNG_EVENT_SYSCALL
:
227 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
229 case LTTNG_EVENT_ALL
:
230 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
233 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
237 /* Copy event name */
238 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
239 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
241 /* Setting up a kernel event */
255 * Allocate and initialize a kernel metadata.
257 * Return pointer to structure or NULL.
259 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
261 struct ltt_kernel_metadata
*lkm
;
262 struct lttng_channel
*chan
;
264 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
265 chan
= zmalloc(sizeof(struct lttng_channel
));
266 if (lkm
== NULL
|| chan
== NULL
) {
267 PERROR("kernel metadata zmalloc");
271 /* Set default attributes */
272 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
273 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
274 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
275 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
276 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
277 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
292 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
295 * Return pointer to structure or NULL.
297 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
301 struct ltt_kernel_stream
*lks
;
305 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
307 PERROR("kernel stream zmalloc");
312 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%d", name
, count
);
314 PERROR("snprintf stream name");
317 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
331 * Cleanup kernel stream structure.
333 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
337 DBG("[trace] Closing stream fd %d", stream
->fd
);
338 /* Close kernel fd */
339 if (stream
->fd
>= 0) {
342 ret
= close(stream
->fd
);
347 /* Remove from stream list */
348 cds_list_del(&stream
->list
);
354 * Cleanup kernel event structure.
356 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
360 if (event
->fd
>= 0) {
363 DBG("[trace] Closing event fd %d", event
->fd
);
364 /* Close kernel fd */
365 ret
= close(event
->fd
);
370 DBG("[trace] Tearing down event (no associated fd)");
373 /* Remove from event list */
374 cds_list_del(&event
->list
);
381 * Cleanup kernel channel structure.
383 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
385 struct ltt_kernel_stream
*stream
, *stmp
;
386 struct ltt_kernel_event
*event
, *etmp
;
391 DBG("[trace] Closing channel fd %d", channel
->fd
);
392 /* Close kernel fd */
393 if (channel
->fd
>= 0) {
394 ret
= close(channel
->fd
);
400 /* For each stream in the channel list */
401 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
402 trace_kernel_destroy_stream(stream
);
405 /* For each event in the channel list */
406 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
407 trace_kernel_destroy_event(event
);
410 /* Remove from channel list */
411 cds_list_del(&channel
->list
);
413 free(channel
->channel
);
419 * Cleanup kernel metadata structure.
421 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
425 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
426 /* Close kernel fd */
427 if (metadata
->fd
>= 0) {
430 ret
= close(metadata
->fd
);
436 free(metadata
->conf
);
441 * Cleanup kernel session structure
443 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
445 struct ltt_kernel_channel
*channel
, *ctmp
;
450 DBG("[trace] Closing session fd %d", session
->fd
);
451 /* Close kernel fds */
452 if (session
->fd
>= 0) {
453 ret
= close(session
->fd
);
459 if (session
->metadata_stream_fd
>= 0) {
460 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
461 ret
= close(session
->metadata_stream_fd
);
467 if (session
->metadata
!= NULL
) {
468 trace_kernel_destroy_metadata(session
->metadata
);
471 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
472 trace_kernel_destroy_channel(channel
);
475 /* Wipe consumer output object */
476 consumer_destroy_output(session
->consumer
);
477 consumer_destroy_output(session
->tmp_consumer
);
479 free(session
->trace_path
);