Commit | Line | Data |
---|---|---|
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> | |
26 | ||
27 | #include "trace-ust.h" | |
28 | ||
0177d773 | 29 | /* |
44d3bd01 DG |
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. | |
0177d773 | 32 | */ |
44d3bd01 DG |
33 | struct ltt_ust_session *trace_ust_get_session_by_pid( |
34 | struct ltt_ust_session_list *session_list, pid_t pid) | |
0177d773 | 35 | { |
44d3bd01 | 36 | struct ltt_ust_session *sess; |
0177d773 | 37 | |
44d3bd01 DG |
38 | if (session_list == NULL) { |
39 | ERR("Session list is NULL"); | |
40 | goto error; | |
41 | } | |
42 | ||
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); | |
47 | return sess; | |
0177d773 DG |
48 | } |
49 | } | |
50 | ||
44d3bd01 | 51 | error: |
0177d773 DG |
52 | return NULL; |
53 | } | |
54 | ||
97ee3a89 DG |
55 | /* |
56 | * Find the channel name for the given ust session. | |
57 | */ | |
58 | struct ltt_ust_channel *trace_ust_get_channel_by_name( | |
59 | char *name, struct ltt_ust_session *session) | |
60 | { | |
61 | struct ltt_ust_channel *chan; | |
62 | ||
63 | if (session == NULL) { | |
64 | ERR("Undefine session"); | |
65 | goto error; | |
66 | } | |
67 | ||
68 | cds_list_for_each_entry(chan, &session->channels.head, list) { | |
69 | if (strcmp(name, chan->name) == 0) { | |
44d3bd01 | 70 | DBG2("Found UST channel by name %s", name); |
97ee3a89 DG |
71 | return chan; |
72 | } | |
73 | } | |
74 | ||
75 | error: | |
76 | return NULL; | |
77 | } | |
78 | ||
79 | /* | |
80 | * Find the event name for the given channel. | |
81 | */ | |
82 | struct ltt_ust_event *trace_ust_get_event_by_name( | |
83 | char *name, struct ltt_ust_channel *channel) | |
84 | { | |
85 | struct ltt_ust_event *ev; | |
86 | ||
87 | if (channel == NULL) { | |
88 | ERR("Undefine channel"); | |
89 | goto error; | |
90 | } | |
91 | ||
92 | cds_list_for_each_entry(ev, &channel->events.head, list) { | |
44d3bd01 | 93 | if (strcmp(name, ev->attr.name) == 0) { |
97ee3a89 DG |
94 | DBG("Found UST event by name %s for channel %s", name, |
95 | channel->name); | |
96 | return ev; | |
97 | } | |
98 | } | |
99 | ||
100 | error: | |
101 | return NULL; | |
102 | } | |
103 | ||
104 | /* | |
105 | * Allocate and initialize a ust session data structure. | |
106 | * | |
107 | * Return pointer to structure or NULL. | |
108 | */ | |
44d3bd01 DG |
109 | struct ltt_ust_session *trace_ust_create_session(char *path, pid_t pid, |
110 | struct lttng_domain *domain) | |
97ee3a89 | 111 | { |
0177d773 | 112 | int ret; |
97ee3a89 DG |
113 | struct ltt_ust_session *lus; |
114 | ||
115 | /* Allocate a new ltt ust session */ | |
116 | lus = malloc(sizeof(struct ltt_ust_session)); | |
117 | if (lus == NULL) { | |
118 | perror("create ust session malloc"); | |
119 | goto error; | |
120 | } | |
121 | ||
122 | /* Init data structure */ | |
123 | lus->handle = -1; | |
124 | lus->enabled = 1; | |
3bd1e081 | 125 | lus->consumer_fds_sent = 0; |
97ee3a89 DG |
126 | lus->metadata = NULL; |
127 | lus->channels.count = 0; | |
128 | CDS_INIT_LIST_HEAD(&lus->channels.head); | |
129 | ||
44d3bd01 DG |
130 | /* Copy lttng_domain */ |
131 | memcpy(&lus->domain, domain, sizeof(struct lttng_domain)); | |
132 | ||
0177d773 | 133 | /* Set session path */ |
44d3bd01 | 134 | ret = snprintf(lus->path, PATH_MAX, "%s/ust_%d", path, pid); |
0177d773 | 135 | if (ret < 0) { |
44d3bd01 | 136 | PERROR("snprintf kernel traces path"); |
0177d773 DG |
137 | goto error; |
138 | } | |
139 | ||
44d3bd01 DG |
140 | DBG2("UST trace session create successful"); |
141 | ||
97ee3a89 DG |
142 | return lus; |
143 | ||
144 | error: | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | /* | |
149 | * Allocate and initialize a ust channel data structure. | |
150 | * | |
151 | * Return pointer to structure or NULL. | |
152 | */ | |
44d3bd01 DG |
153 | struct ltt_ust_channel *trace_ust_create_channel(struct lttng_channel *chan, |
154 | char *path) | |
97ee3a89 DG |
155 | { |
156 | int ret; | |
157 | struct ltt_ust_channel *luc; | |
158 | ||
159 | luc = malloc(sizeof(struct ltt_ust_channel)); | |
160 | if (luc == NULL) { | |
161 | perror("ltt_ust_channel malloc"); | |
162 | goto error; | |
163 | } | |
164 | ||
44d3bd01 DG |
165 | /* Copy UST channel attributes */ |
166 | memcpy(&luc->attr, &chan->attr, sizeof(struct lttng_ust_channel)); | |
167 | ||
168 | /* Translate to UST output enum */ | |
169 | switch (luc->attr.output) { | |
170 | default: | |
171 | luc->attr.output = LTTNG_UST_MMAP; | |
172 | break; | |
97ee3a89 | 173 | } |
97ee3a89 DG |
174 | |
175 | luc->handle = -1; | |
176 | luc->enabled = 1; | |
97ee3a89 DG |
177 | luc->events.count = 0; |
178 | CDS_INIT_LIST_HEAD(&luc->events.head); | |
179 | ||
44d3bd01 DG |
180 | memset(&luc->ctx, 0, sizeof(struct lttng_ust_context)); |
181 | ||
97ee3a89 | 182 | /* Copy channel name */ |
44d3bd01 | 183 | strncpy(luc->name, chan->name, sizeof(&luc->name)); |
97ee3a89 DG |
184 | luc->name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; |
185 | ||
186 | /* Set trace output path */ | |
44d3bd01 | 187 | ret = snprintf(luc->trace_path, PATH_MAX, "%s", path); |
97ee3a89 DG |
188 | if (ret < 0) { |
189 | perror("asprintf ust create channel"); | |
190 | goto error; | |
191 | } | |
2bdd86d4 | 192 | CDS_INIT_LIST_HEAD(&luc->stream_list.head); |
97ee3a89 DG |
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; | |
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 | ||
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; | |
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 */ | |
2bdd86d4 | 274 | ret = asprintf(&lum->pathname, "%s/metadata", path); |
97ee3a89 DG |
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 | { | |
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 | */ | |
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 | ||
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 | */ | |
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 */ | |
2bdd86d4 | 325 | free(metadata->pathname); |
97ee3a89 DG |
326 | free(metadata); |
327 | } | |
328 | ||
329 | /* | |
330 | * Cleanup ust session structure | |
331 | */ | |
332 | void trace_ust_destroy_session(struct ltt_ust_session *session) | |
333 | { | |
334 | struct ltt_ust_channel *channel, *ctmp; | |
335 | ||
336 | DBG("[trace] Destroy ust session %d", session->handle); | |
337 | ||
338 | /* Extra safety */ | |
339 | if (session == NULL) { | |
340 | return; | |
341 | } | |
342 | ||
343 | if (session->metadata != NULL) { | |
344 | trace_ust_destroy_metadata(session->metadata); | |
345 | } | |
346 | ||
347 | cds_list_for_each_entry_safe(channel, ctmp, &session->channels.head, list) { | |
348 | trace_ust_destroy_channel(channel); | |
349 | } | |
350 | ||
44d3bd01 DG |
351 | if (session->path) { |
352 | free(session->path); | |
353 | } | |
354 | ||
97ee3a89 DG |
355 | free(session); |
356 | } |