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>
26 #include <lttng-ust.h>
28 #include "trace-ust.h"
31 * Using a ust session list, it will return the session corresponding to the
32 * pid. Must be a session of domain LTTNG_DOMAIN_UST_PID.
34 struct ltt_ust_session
*trace_ust_get_session_by_pid(
35 struct ltt_ust_session_list
*session_list
, pid_t pid
)
37 struct ltt_ust_session
*sess
;
39 if (session_list
== NULL
) {
40 ERR("Session list is NULL");
44 cds_list_for_each_entry(sess
, &session_list
->head
, list
) {
45 if (sess
->domain
.type
== LTTNG_DOMAIN_UST_PID
&&
46 sess
->domain
.attr
.pid
== pid
) {
47 DBG2("Trace UST session found by pid %d", pid
);
57 * Find the channel name for the given ust session.
59 struct ltt_ust_channel
*trace_ust_get_channel_by_name(
60 char *name
, struct ltt_ust_session
*session
)
62 struct ltt_ust_channel
*chan
;
64 if (session
== NULL
) {
65 ERR("Undefine session");
69 cds_list_for_each_entry(chan
, &session
->channels
.head
, list
) {
70 if (strcmp(name
, chan
->name
) == 0) {
71 DBG2("Found UST channel by name %s", name
);
81 * Find the event name for the given channel.
83 struct ltt_ust_event
*trace_ust_get_event_by_name(
84 char *name
, struct ltt_ust_channel
*channel
)
86 struct ltt_ust_event
*ev
;
88 if (channel
== NULL
) {
89 ERR("Undefine channel");
93 cds_list_for_each_entry(ev
, &channel
->events
.head
, list
) {
94 if (strcmp(name
, ev
->attr
.name
) == 0) {
95 DBG("Found UST event by name %s for channel %s", name
,
106 * Allocate and initialize a ust session data structure.
108 * Return pointer to structure or NULL.
110 struct ltt_ust_session
*trace_ust_create_session(char *path
, pid_t pid
,
111 struct lttng_domain
*domain
)
114 struct ltt_ust_session
*lus
;
116 /* Allocate a new ltt ust session */
117 lus
= malloc(sizeof(struct ltt_ust_session
));
119 perror("create ust session malloc");
123 /* Init data structure */
126 lus
->uconsumer_fds_sent
= 0;
127 lus
->metadata
= NULL
;
128 lus
->channels
.count
= 0;
129 CDS_INIT_LIST_HEAD(&lus
->channels
.head
);
131 /* Copy lttng_domain */
132 memcpy(&lus
->domain
, domain
, sizeof(struct lttng_domain
));
134 /* Set session path */
135 ret
= snprintf(lus
->path
, PATH_MAX
, "%s/ust_%d", path
, pid
);
137 PERROR("snprintf kernel traces path");
141 DBG2("UST trace session create successful");
150 * Allocate and initialize a ust channel data structure.
152 * Return pointer to structure or NULL.
154 struct ltt_ust_channel
*trace_ust_create_channel(struct lttng_channel
*chan
,
158 struct ltt_ust_channel
*luc
;
160 luc
= malloc(sizeof(struct ltt_ust_channel
));
162 perror("ltt_ust_channel malloc");
166 /* Copy UST channel attributes */
167 memcpy(&luc
->attr
, &chan
->attr
, sizeof(struct lttng_ust_channel
));
169 /* Translate to UST output enum */
170 switch (luc
->attr
.output
) {
172 luc
->attr
.output
= LTTNG_UST_MMAP
;
178 luc
->events
.count
= 0;
179 CDS_INIT_LIST_HEAD(&luc
->events
.head
);
181 memset(&luc
->ctx
, 0, sizeof(struct lttng_ust_context
));
183 /* Copy channel name */
184 strncpy(luc
->name
, chan
->name
, sizeof(&luc
->name
));
185 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
187 /* Set trace output path */
188 ret
= snprintf(luc
->trace_path
, PATH_MAX
, "%s", path
);
190 perror("asprintf ust create channel");
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
->trace_path
, "%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
->trace_path
);
331 * Cleanup ust session structure
333 void trace_ust_destroy_session(struct ltt_ust_session
*session
)
335 struct ltt_ust_channel
*channel
, *ctmp
;
337 DBG("[trace] Destroy ust session %d", session
->handle
);
340 if (session
== NULL
) {
344 if (session
->metadata
!= NULL
) {
345 trace_ust_destroy_metadata(session
->metadata
);
348 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channels
.head
, list
) {
349 trace_ust_destroy_channel(channel
);