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");
192 CDS_INIT_LIST_HEAD(&luc
->stream_list
.head
);
201 * Allocate and initialize a ust event. Set name and event type.
203 * Return pointer to structure or NULL.
205 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
207 struct ltt_ust_event
*lue
;
209 lue
= malloc(sizeof(struct ltt_ust_event
));
211 PERROR("ust event malloc");
216 case LTTNG_EVENT_PROBE
:
217 lue
->attr
.instrumentation
= LTTNG_UST_PROBE
;
219 case LTTNG_EVENT_FUNCTION
:
220 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
222 case LTTNG_EVENT_FUNCTION_ENTRY
:
223 lue
->attr
.instrumentation
= LTTNG_UST_FUNCTION
;
225 case LTTNG_EVENT_TRACEPOINT
:
226 lue
->attr
.instrumentation
= LTTNG_UST_TRACEPOINT
;
229 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
233 /* Copy event name */
234 strncpy(lue
->attr
.name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
235 lue
->attr
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
237 /* Setting up a ust event */
240 memset(&lue
->ctx
, 0, sizeof(struct lttng_ust_context
));
249 * Allocate and initialize a ust metadata.
251 * Return pointer to structure or NULL.
253 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
256 struct ltt_ust_metadata
*lum
;
258 lum
= malloc(sizeof(struct ltt_ust_metadata
));
260 perror("ust metadata malloc");
264 /* Set default attributes */
265 lum
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
266 lum
->attr
.subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
267 lum
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
268 lum
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
269 lum
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
270 lum
->attr
.output
= DEFAULT_UST_CHANNEL_OUTPUT
;
273 /* Set metadata trace path */
274 ret
= asprintf(&lum
->pathname
, "%s/metadata", path
);
276 perror("asprintf ust metadata");
287 * Cleanup ust event structure.
289 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
291 DBG("[trace] Destroy ust event %s", event
->attr
.name
);
293 /* Remove from event list */
294 cds_list_del(&event
->list
);
299 * Cleanup ust channel structure.
301 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
303 struct ltt_ust_event
*event
, *etmp
;
305 DBG("[trace] Destroy ust channel %d", channel
->handle
);
307 /* For each event in the channel list */
308 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events
.head
, list
) {
309 trace_ust_destroy_event(event
);
312 /* Remove from channel list */
313 cds_list_del(&channel
->list
);
318 * Cleanup ust metadata structure.
320 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
322 DBG("[trace] Destroy ust metadata %d", metadata
->handle
);
324 /* Free attributes */
325 free(metadata
->pathname
);
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
);