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 * Return an UST session by traceable app PID.
32 struct ltt_ust_session
*trace_ust_get_session_by_pid(pid_t pid
,
33 struct ltt_ust_session_list
*session_list
)
35 struct ltt_ust_session
*lus
;
37 cds_list_for_each_entry(lus
, &session_list
->head
, list
) {
38 if (lus
->app
->pid
== pid
) {
39 DBG("Found UST session by pid %d", pid
);
48 * Find the channel name for the given ust session.
50 struct ltt_ust_channel
*trace_ust_get_channel_by_name(
51 char *name
, struct ltt_ust_session
*session
)
53 struct ltt_ust_channel
*chan
;
55 if (session
== NULL
) {
56 ERR("Undefine session");
60 cds_list_for_each_entry(chan
, &session
->channels
.head
, list
) {
61 if (strcmp(name
, chan
->name
) == 0) {
62 DBG("Found UST channel by name %s", name
);
72 * Find the event name for the given channel.
74 struct ltt_ust_event
*trace_ust_get_event_by_name(
75 char *name
, struct ltt_ust_channel
*channel
)
77 struct ltt_ust_event
*ev
;
79 if (channel
== NULL
) {
80 ERR("Undefine channel");
84 cds_list_for_each_entry(ev
, &channel
->events
.head
, list
) {
85 if (strcmp(name
, ev
->event
->name
) == 0) {
86 DBG("Found UST event by name %s for channel %s", name
,
97 * Allocate and initialize a ust session data structure.
99 * Return pointer to structure or NULL.
101 struct ltt_ust_session
*trace_ust_create_session(char *path
, pid_t pid
)
104 struct ltt_ust_session
*lus
;
106 /* Allocate a new ltt ust session */
107 lus
= malloc(sizeof(struct ltt_ust_session
));
109 perror("create ust session malloc");
113 /* Init data structure */
116 lus
->uconsumer_fds_sent
= 0;
118 lus
->metadata
= NULL
;
119 lus
->app
= NULL
; /* TODO: Search app by PID */
120 lus
->channels
.count
= 0;
121 CDS_INIT_LIST_HEAD(&lus
->channels
.head
);
123 /* Set session path */
124 ret
= asprintf(&lus
->path
, "%s/ust_%d", path
, pid
);
126 perror("asprintf kernel traces path");
137 * Allocate and initialize a ust channel data structure.
139 * Return pointer to structure or NULL.
141 struct ltt_ust_channel
*trace_ust_create_channel(char *name
, char *path
,
142 struct lttng_ust_channel
*chan
)
145 struct ltt_ust_channel
*luc
;
147 luc
= malloc(sizeof(struct ltt_ust_channel
));
149 perror("ltt_ust_channel malloc");
153 luc
->attr
= malloc(sizeof(struct lttng_ust_channel
));
154 if (luc
->attr
== NULL
) {
155 perror("lttng_ust_channel malloc");
158 memcpy(luc
->attr
, chan
, sizeof(struct lttng_ust_channel
));
163 luc
->events
.count
= 0;
164 CDS_INIT_LIST_HEAD(&luc
->events
.head
);
166 /* Copy channel name */
167 strncpy(luc
->name
, name
, LTTNG_UST_SYM_NAME_LEN
);
168 luc
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
170 /* Set trace output path */
171 ret
= asprintf(&luc
->trace_path
, "%s", path
);
173 perror("asprintf ust create channel");
184 * Allocate and initialize a ust event. Set name and event type.
186 * Return pointer to structure or NULL.
188 struct ltt_ust_event
*trace_ust_create_event(struct lttng_event
*ev
)
190 struct ltt_ust_event
*lue
;
191 struct lttng_ust_event
*event
;
193 lue
= malloc(sizeof(struct ltt_ust_event
));
194 event
= malloc(sizeof(struct lttng_ust_event
));
195 if (lue
== NULL
|| event
== NULL
) {
196 perror("ust event malloc");
201 case LTTNG_EVENT_PROBE
:
202 event
->instrumentation
= LTTNG_UST_PROBE
;
204 case LTTNG_EVENT_FUNCTION
:
205 event
->instrumentation
= LTTNG_UST_FUNCTION
;
207 case LTTNG_EVENT_FUNCTION_ENTRY
:
208 event
->instrumentation
= LTTNG_UST_FUNCTION
;
210 case LTTNG_EVENT_TRACEPOINT
:
211 event
->instrumentation
= LTTNG_UST_TRACEPOINT
;
214 ERR("Unknown ust instrumentation type (%d)", ev
->type
);
218 /* Copy event name */
219 strncpy(event
->name
, ev
->name
, LTTNG_UST_SYM_NAME_LEN
);
220 event
->name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
222 /* Setting up a ust event */
235 * Allocate and initialize a ust metadata.
237 * Return pointer to structure or NULL.
239 struct ltt_ust_metadata
*trace_ust_create_metadata(char *path
)
242 struct ltt_ust_metadata
*lum
;
243 struct lttng_ust_channel
*attr
;
245 lum
= malloc(sizeof(struct ltt_ust_metadata
));
246 attr
= malloc(sizeof(struct lttng_ust_channel
));
247 if (lum
== NULL
|| attr
== NULL
) {
248 perror("ust metadata malloc");
252 /* Set default attributes */
253 attr
->overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
254 attr
->subbuf_size
= DEFAULT_METADATA_SUBBUF_SIZE
;
255 attr
->num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
256 attr
->switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
257 attr
->read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
258 attr
->output
= DEFAULT_UST_CHANNEL_OUTPUT
;
262 /* Set metadata trace path */
263 ret
= asprintf(&lum
->trace_path
, "%s/metadata", path
);
265 perror("asprintf ust metadata");
276 * Cleanup ust event structure.
278 void trace_ust_destroy_event(struct ltt_ust_event
*event
)
280 DBG("[trace] Destroy ust event %s", event
->event
->name
);
282 /* Free attributes */
286 /* Remove from event list */
287 cds_list_del(&event
->list
);
292 * Cleanup ust channel structure.
294 void trace_ust_destroy_channel(struct ltt_ust_channel
*channel
)
296 struct ltt_ust_event
*event
, *etmp
;
298 DBG("[trace] Destroy ust channel %d", channel
->handle
);
300 free(channel
->trace_path
);
301 /* Free attributes structure */
305 /* For each event in the channel list */
306 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events
.head
, list
) {
307 trace_ust_destroy_event(event
);
310 /* Remove from channel list */
311 cds_list_del(&channel
->list
);
316 * Cleanup ust metadata structure.
318 void trace_ust_destroy_metadata(struct ltt_ust_metadata
*metadata
)
320 DBG("[trace] Destroy ust metadata %d", metadata
->handle
);
322 /* Free attributes */
323 free(metadata
->attr
);
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
);