fe007141e90c0c4d09bd9e2f481e1066c63bef50
[lttng-tools.git] / ltt-sessiond / trace-ust.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <lttngerr.h>
25 #include <lttng-share.h>
26 #include <lttng-ust.h>
27
28 #include "trace-ust.h"
29
30 /*
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.
33 */
34 struct ltt_ust_session *trace_ust_get_session_by_pid(
35 struct ltt_ust_session_list *session_list, pid_t pid)
36 {
37 struct ltt_ust_session *sess;
38
39 if (session_list == NULL) {
40 ERR("Session list is NULL");
41 goto error;
42 }
43
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);
48 return sess;
49 }
50 }
51
52 error:
53 return NULL;
54 }
55
56 /*
57 * Find the channel name for the given ust session.
58 */
59 struct ltt_ust_channel *trace_ust_get_channel_by_name(
60 char *name, struct ltt_ust_session *session)
61 {
62 struct ltt_ust_channel *chan;
63
64 if (session == NULL) {
65 ERR("Undefine session");
66 goto error;
67 }
68
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);
72 return chan;
73 }
74 }
75
76 error:
77 return NULL;
78 }
79
80 /*
81 * Find the event name for the given channel.
82 */
83 struct ltt_ust_event *trace_ust_get_event_by_name(
84 char *name, struct ltt_ust_channel *channel)
85 {
86 struct ltt_ust_event *ev;
87
88 if (channel == NULL) {
89 ERR("Undefine channel");
90 goto error;
91 }
92
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,
96 channel->name);
97 return ev;
98 }
99 }
100
101 error:
102 return NULL;
103 }
104
105 /*
106 * Allocate and initialize a ust session data structure.
107 *
108 * Return pointer to structure or NULL.
109 */
110 struct ltt_ust_session *trace_ust_create_session(char *path, pid_t pid,
111 struct lttng_domain *domain)
112 {
113 int ret;
114 struct ltt_ust_session *lus;
115
116 /* Allocate a new ltt ust session */
117 lus = malloc(sizeof(struct ltt_ust_session));
118 if (lus == NULL) {
119 perror("create ust session malloc");
120 goto error;
121 }
122
123 /* Init data structure */
124 lus->handle = -1;
125 lus->enabled = 1;
126 lus->uconsumer_fds_sent = 0;
127 lus->metadata = NULL;
128 lus->channels.count = 0;
129 CDS_INIT_LIST_HEAD(&lus->channels.head);
130
131 /* Copy lttng_domain */
132 memcpy(&lus->domain, domain, sizeof(struct lttng_domain));
133
134 /* Set session path */
135 ret = snprintf(lus->path, PATH_MAX, "%s/ust_%d", path, pid);
136 if (ret < 0) {
137 PERROR("snprintf kernel traces path");
138 goto error;
139 }
140
141 DBG2("UST trace session create successful");
142
143 return lus;
144
145 error:
146 return NULL;
147 }
148
149 /*
150 * Allocate and initialize a ust channel data structure.
151 *
152 * Return pointer to structure or NULL.
153 */
154 struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
155 char *path)
156 {
157 int ret;
158 struct ltt_ust_channel *luc;
159
160 luc = malloc(sizeof(struct ltt_ust_channel));
161 if (luc == NULL) {
162 perror("ltt_ust_channel malloc");
163 goto error;
164 }
165
166 /* Copy UST channel attributes */
167 memcpy(&luc->attr, &chan->attr, sizeof(struct lttng_ust_channel));
168
169 /* Translate to UST output enum */
170 switch (luc->attr.output) {
171 default:
172 luc->attr.output = LTTNG_UST_MMAP;
173 break;
174 }
175
176 luc->handle = -1;
177 luc->enabled = 1;
178 luc->events.count = 0;
179 CDS_INIT_LIST_HEAD(&luc->events.head);
180
181 memset(&luc->ctx, 0, sizeof(struct lttng_ust_context));
182
183 /* Copy channel name */
184 strncpy(luc->name, chan->name, sizeof(&luc->name));
185 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
186
187 /* Set trace output path */
188 ret = snprintf(luc->trace_path, PATH_MAX, "%s", path);
189 if (ret < 0) {
190 perror("asprintf ust create channel");
191 goto error;
192 }
193
194 return luc;
195
196 error:
197 return NULL;
198 }
199
200 /*
201 * Allocate and initialize a ust event. Set name and event type.
202 *
203 * Return pointer to structure or NULL.
204 */
205 struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
206 {
207 struct ltt_ust_event *lue;
208
209 lue = malloc(sizeof(struct ltt_ust_event));
210 if (lue == NULL) {
211 PERROR("ust event malloc");
212 goto error;
213 }
214
215 switch (ev->type) {
216 case LTTNG_EVENT_PROBE:
217 lue->attr.instrumentation = LTTNG_UST_PROBE;
218 break;
219 case LTTNG_EVENT_FUNCTION:
220 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
221 break;
222 case LTTNG_EVENT_FUNCTION_ENTRY:
223 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
224 break;
225 case LTTNG_EVENT_TRACEPOINT:
226 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
227 break;
228 default:
229 ERR("Unknown ust instrumentation type (%d)", ev->type);
230 goto error;
231 }
232
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';
236
237 /* Setting up a ust event */
238 lue->handle = -1;
239 lue->enabled = 1;
240 memset(&lue->ctx, 0, sizeof(struct lttng_ust_context));
241
242 return lue;
243
244 error:
245 return NULL;
246 }
247
248 /*
249 * Allocate and initialize a ust metadata.
250 *
251 * Return pointer to structure or NULL.
252 */
253 struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
254 {
255 int ret;
256 struct ltt_ust_metadata *lum;
257
258 lum = malloc(sizeof(struct ltt_ust_metadata));
259 if (lum == NULL) {
260 perror("ust metadata malloc");
261 goto error;
262 }
263
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;
271
272 lum->handle = -1;
273 /* Set metadata trace path */
274 ret = asprintf(&lum->trace_path, "%s/metadata", path);
275 if (ret < 0) {
276 perror("asprintf ust metadata");
277 goto error;
278 }
279
280 return lum;
281
282 error:
283 return NULL;
284 }
285
286 /*
287 * Cleanup ust event structure.
288 */
289 void trace_ust_destroy_event(struct ltt_ust_event *event)
290 {
291 DBG("[trace] Destroy ust event %s", event->attr.name);
292
293 /* Remove from event list */
294 cds_list_del(&event->list);
295 free(event);
296 }
297
298 /*
299 * Cleanup ust channel structure.
300 */
301 void trace_ust_destroy_channel(struct ltt_ust_channel *channel)
302 {
303 struct ltt_ust_event *event, *etmp;
304
305 DBG("[trace] Destroy ust channel %d", channel->handle);
306
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);
310 }
311
312 /* Remove from channel list */
313 cds_list_del(&channel->list);
314 free(channel);
315 }
316
317 /*
318 * Cleanup ust metadata structure.
319 */
320 void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
321 {
322 DBG("[trace] Destroy ust metadata %d", metadata->handle);
323
324 /* Free attributes */
325 free(metadata->trace_path);
326
327 free(metadata);
328 }
329
330 /*
331 * Cleanup ust session structure
332 */
333 void trace_ust_destroy_session(struct ltt_ust_session *session)
334 {
335 struct ltt_ust_channel *channel, *ctmp;
336
337 DBG("[trace] Destroy ust session %d", session->handle);
338
339 /* Extra safety */
340 if (session == NULL) {
341 return;
342 }
343
344 if (session->metadata != NULL) {
345 trace_ust_destroy_metadata(session->metadata);
346 }
347
348 cds_list_for_each_entry_safe(channel, ctmp, &session->channels.head, list) {
349 trace_ust_destroy_channel(channel);
350 }
351
352 if (session->path) {
353 free(session->path);
354 }
355
356 free(session);
357 }
This page took 0.03618 seconds and 4 git commands to generate.