2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <lttng-share.h>
27 #include "trace-ust.h"
30 * Using a ust session list, it will return the session corresponding to the
31 * pid. Must be a session of domain LTTNG_DOMAIN_UST_PID.
33 struct ltt_ust_session
*trace_ust_get_session_by_pid(
34 struct ltt_ust_session_list
*session_list
, pid_t pid
)
36 struct ltt_ust_session
*sess
;
38 if (session_list
== NULL
) {
39 ERR("Session list is NULL");
43 cds_list_for_each_entry(sess
, &session_list
->head
, list
) {
44 if (sess
->domain
.type
== LTTNG_DOMAIN_UST_PID
&&
45 sess
->domain
.attr
.pid
== pid
) {
46 DBG2("Trace UST session found by pid %d", pid
);
56 * Find the channel name for the given ust session.
58 struct ltt_ust_channel
*trace_ust_get_channel_by_name(
59 char *name
, struct ltt_ust_session
*session
)
61 struct ltt_ust_channel
*chan
;
63 if (session
== NULL
) {
64 ERR("Undefine session");
68 cds_list_for_each_entry(chan
, &session
->channels
.head
, list
) {
69 if (strcmp(name
, chan
->name
) == 0) {
70 DBG2("Found UST channel by name %s", name
);
80 * Find the event name for the given channel.
82 struct ltt_ust_event
*trace_ust_get_event_by_name(
83 char *name
, struct ltt_ust_channel
*channel
)
85 struct ltt_ust_event
*ev
;
87 if (channel
== NULL
) {
88 ERR("Undefine channel");
92 cds_list_for_each_entry(ev
, &channel
->events
.head
, list
) {
93 if (strcmp(name
, ev
->attr
.name
) == 0) {
94 DBG("Found UST event by name %s for channel %s", name
,
105 * Allocate and initialize a ust session data structure.
107 * Return pointer to structure or NULL.
109 struct ltt_ust_session
*trace_ust_create_session(char *path
, pid_t pid
,
110 struct lttng_domain
*domain
)
113 struct ltt_ust_session
*lus
;
115 /* Allocate a new ltt ust session */
116 lus
= malloc(sizeof(struct ltt_ust_session
));
118 perror("create ust session malloc");
122 /* Init data structure */
125 lus
->consumer_fds_sent
= 0;
126 lus
->metadata
= NULL
;
127 lus
->channels
.count
= 0;
128 CDS_INIT_LIST_HEAD(&lus
->channels
.head
);
130 /* Copy lttng_domain */
131 memcpy(&lus
->domain
, domain
, sizeof(struct lttng_domain
));
133 /* Set session path */
134 ret
= snprintf(lus
->path
, PATH_MAX
, "%s/ust_%d", path
, pid
);
136 PERROR("snprintf kernel traces path");
140 DBG2("UST trace session create successful");
149 * Allocate and initialize a ust channel data structure.
151 * Return pointer to structure or NULL.
153 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
157 struct ltt_ust_channel
*luc
;
159 luc
= malloc(sizeof(struct ltt_ust_channel
));
161 perror("ltt_ust_channel malloc");
165 /* Copy UST channel attributes */
166 memcpy(&luc
->attr
, &chan
->attr
, sizeof(struct lttng_ust_channel
));
168 /* Translate to UST output enum */
169 switch (luc
->attr
.output
) {
171 luc
->attr
.output
= LTTNG_UST_MMAP
;
177 luc
->events
.count
= 0;
178 CDS_INIT_LIST_HEAD(&luc
->events
.head
);
180 memset(&luc
->ctx
, 0, sizeof(struct lttng_ust_context
));
182 /* Copy channel name */
183 strncpy(luc
->name
, chan
->name
, sizeof(&luc
->name
));
184 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
186 /* Set trace output path */
187 ret
= snprintf(luc
->trace_path
, PATH_MAX
, "%s", path
);
189 perror("asprintf ust create channel");
200 * Allocate and initialize a ust event. Set name and event type.
202 * Return pointer to structure or NULL.
204 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
206 struct ltt_ust_event
*lue
;
208 lue
= malloc(sizeof(struct ltt_ust_event
));
210 PERROR("ust event malloc");
215 case LTTNG_EVENT_PROBE
:
216 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
218 case LTTNG_EVENT_FUNCTION
:
219 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
221 case LTTNG_EVENT_FUNCTION_ENTRY
:
222 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
224 case LTTNG_EVENT_TRACEPOINT
:
225 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
228 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
232 /* Copy event name */
233 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
234 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
236 /* Setting up a ust event */
239 memset(&lue
->ctx
, 0, sizeof(struct lttng_ust_context
));
248 * Allocate and initialize a ust metadata.
250 * Return pointer to structure or NULL.
252 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
255 struct ltt_ust_metadata
*lum
;
257 lum
= malloc(sizeof(struct ltt_ust_metadata
));
259 perror("ust metadata malloc");
263 /* Set default attributes */
264 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
265 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
266 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
267 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
268 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
269 lum
->attr
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
272 /* Set metadata trace path */
273 ret
= asprintf(&lum
->trace_path
, "%s/metadata", path
);
275 perror("asprintf ust metadata");
286 * Cleanup ust event structure.
288 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
290 DBG("[trace] Destroy ust event %s", event
->attr
.name
);
292 /* Remove from event list */
293 cds_list_del(&event
->list
);
298 * Cleanup ust channel structure.
300 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
302 struct ltt_ust_event
*event
, *etmp
;
304 DBG("[trace] Destroy ust channel %d", channel
->handle
);
306 /* For each event in the channel list */
307 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events
.head
, list
) {
308 trace_ust_destroy_event(event
);
311 /* Remove from channel list */
312 cds_list_del(&channel
->list
);
317 * Cleanup ust metadata structure.
319 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
321 DBG("[trace] Destroy ust metadata %d", metadata
->handle
);
323 /* Free attributes */
324 free(metadata
->trace_path
);
330 * Cleanup ust session structure
332 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
334 struct ltt_ust_channel
*channel
, *ctmp
;
336 DBG("[trace] Destroy ust session %d", session
->handle
);
339 if (session
== NULL
) {
343 if (session
->metadata
!= NULL
) {
344 trace_ust_destroy_metadata(session
->metadata
);
347 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channels
.head
, list
) {
348 trace_ust_destroy_channel(channel
);