Add enable channel support for UST
[lttng-tools.git] / ltt-sessiond / trace-ust.c
CommitLineData
97ee3a89
DG
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>
44d3bd01 26#include <lttng-ust.h>
97ee3a89
DG
27
28#include "trace-ust.h"
29
0177d773 30/*
44d3bd01
DG
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.
0177d773 33 */
44d3bd01
DG
34struct ltt_ust_session *trace_ust_get_session_by_pid(
35 struct ltt_ust_session_list *session_list, pid_t pid)
0177d773 36{
44d3bd01 37 struct ltt_ust_session *sess;
0177d773 38
44d3bd01
DG
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;
0177d773
DG
49 }
50 }
51
44d3bd01 52error:
0177d773
DG
53 return NULL;
54}
55
97ee3a89
DG
56/*
57 * Find the channel name for the given ust session.
58 */
59struct 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) {
44d3bd01 71 DBG2("Found UST channel by name %s", name);
97ee3a89
DG
72 return chan;
73 }
74 }
75
76error:
77 return NULL;
78}
79
80/*
81 * Find the event name for the given channel.
82 */
83struct 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) {
44d3bd01 94 if (strcmp(name, ev->attr.name) == 0) {
97ee3a89
DG
95 DBG("Found UST event by name %s for channel %s", name,
96 channel->name);
97 return ev;
98 }
99 }
100
101error:
102 return NULL;
103}
104
105/*
106 * Allocate and initialize a ust session data structure.
107 *
108 * Return pointer to structure or NULL.
109 */
44d3bd01
DG
110struct ltt_ust_session *trace_ust_create_session(char *path, pid_t pid,
111 struct lttng_domain *domain)
97ee3a89 112{
0177d773 113 int ret;
97ee3a89
DG
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;
97ee3a89
DG
127 lus->metadata = NULL;
128 lus->channels.count = 0;
129 CDS_INIT_LIST_HEAD(&lus->channels.head);
130
44d3bd01
DG
131 /* Copy lttng_domain */
132 memcpy(&lus->domain, domain, sizeof(struct lttng_domain));
133
0177d773 134 /* Set session path */
44d3bd01 135 ret = snprintf(lus->path, PATH_MAX, "%s/ust_%d", path, pid);
0177d773 136 if (ret < 0) {
44d3bd01 137 PERROR("snprintf kernel traces path");
0177d773
DG
138 goto error;
139 }
140
44d3bd01
DG
141 DBG2("UST trace session create successful");
142
97ee3a89
DG
143 return lus;
144
145error:
146 return NULL;
147}
148
149/*
150 * Allocate and initialize a ust channel data structure.
151 *
152 * Return pointer to structure or NULL.
153 */
44d3bd01
DG
154struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan,
155 char *path)
97ee3a89
DG
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
44d3bd01
DG
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;
97ee3a89 174 }
97ee3a89
DG
175
176 luc->handle = -1;
177 luc->enabled = 1;
97ee3a89
DG
178 luc->events.count = 0;
179 CDS_INIT_LIST_HEAD(&luc->events.head);
180
44d3bd01
DG
181 memset(&luc->ctx, 0, sizeof(struct lttng_ust_context));
182
97ee3a89 183 /* Copy channel name */
44d3bd01 184 strncpy(luc->name, chan->name, sizeof(&luc->name));
97ee3a89
DG
185 luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
186
187 /* Set trace output path */
44d3bd01 188 ret = snprintf(luc->trace_path, PATH_MAX, "%s", path);
97ee3a89
DG
189 if (ret < 0) {
190 perror("asprintf ust create channel");
191 goto error;
192 }
193
194 return luc;
195
196error:
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 */
205struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev)
206{
207 struct ltt_ust_event *lue;
97ee3a89
DG
208
209 lue = malloc(sizeof(struct ltt_ust_event));
44d3bd01
DG
210 if (lue == NULL) {
211 PERROR("ust event malloc");
97ee3a89
DG
212 goto error;
213 }
214
215 switch (ev->type) {
216 case LTTNG_EVENT_PROBE:
44d3bd01 217 lue->attr.instrumentation = LTTNG_UST_PROBE;
97ee3a89
DG
218 break;
219 case LTTNG_EVENT_FUNCTION:
44d3bd01 220 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
221 break;
222 case LTTNG_EVENT_FUNCTION_ENTRY:
44d3bd01 223 lue->attr.instrumentation = LTTNG_UST_FUNCTION;
97ee3a89
DG
224 break;
225 case LTTNG_EVENT_TRACEPOINT:
44d3bd01 226 lue->attr.instrumentation = LTTNG_UST_TRACEPOINT;
97ee3a89
DG
227 break;
228 default:
229 ERR("Unknown ust instrumentation type (%d)", ev->type);
230 goto error;
231 }
232
233 /* Copy event name */
44d3bd01
DG
234 strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN);
235 lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
97ee3a89
DG
236
237 /* Setting up a ust event */
238 lue->handle = -1;
97ee3a89 239 lue->enabled = 1;
44d3bd01 240 memset(&lue->ctx, 0, sizeof(struct lttng_ust_context));
97ee3a89
DG
241
242 return lue;
243
244error:
245 return NULL;
246}
247
248/*
249 * Allocate and initialize a ust metadata.
250 *
251 * Return pointer to structure or NULL.
252 */
253struct ltt_ust_metadata *trace_ust_create_metadata(char *path)
254{
255 int ret;
256 struct ltt_ust_metadata *lum;
97ee3a89
DG
257
258 lum = malloc(sizeof(struct ltt_ust_metadata));
44d3bd01 259 if (lum == NULL) {
97ee3a89
DG
260 perror("ust metadata malloc");
261 goto error;
262 }
263
264 /* Set default attributes */
44d3bd01
DG
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
97ee3a89
DG
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
282error:
283 return NULL;
284}
285
286/*
287 * Cleanup ust event structure.
288 */
289void trace_ust_destroy_event(struct ltt_ust_event *event)
290{
44d3bd01 291 DBG("[trace] Destroy ust event %s", event->attr.name);
97ee3a89
DG
292
293 /* Remove from event list */
294 cds_list_del(&event->list);
295 free(event);
296}
297
298/*
299 * Cleanup ust channel structure.
300 */
301void 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
97ee3a89
DG
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 */
320void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata)
321{
322 DBG("[trace] Destroy ust metadata %d", metadata->handle);
323
324 /* Free attributes */
97ee3a89
DG
325 free(metadata->trace_path);
326
327 free(metadata);
328}
329
330/*
331 * Cleanup ust session structure
332 */
333void 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
44d3bd01
DG
352 if (session->path) {
353 free(session->path);
354 }
355
97ee3a89
DG
356 free(session);
357}
This page took 0.036772 seconds and 4 git commands to generate.