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 | } | |
192 | ||
193 | return luc; | |
194 | ||
195 | error: | |
196 | return NULL; | |
197 | } | |
198 | ||
199 | /* | |
200 | * Allocate and initialize a ust event. Set name and event type. | |
201 | * | |
202 | * Return pointer to structure or NULL. | |
203 | */ | |
204 | struct ltt_ust_event *trace_ust_create_event(struct lttng_event *ev) | |
205 | { | |
206 | struct ltt_ust_event *lue; | |
97ee3a89 DG |
207 | |
208 | lue = malloc(sizeof(struct ltt_ust_event)); | |
44d3bd01 DG |
209 | if (lue == NULL) { |
210 | PERROR("ust event malloc"); | |
97ee3a89 DG |
211 | goto error; |
212 | } | |
213 | ||
214 | switch (ev->type) { | |
215 | case LTTNG_EVENT_PROBE: | |
44d3bd01 | 216 | lue->attr.instrumentation = LTTNG_UST_PROBE; |
97ee3a89 DG |
217 | break; |
218 | case LTTNG_EVENT_FUNCTION: | |
44d3bd01 | 219 | lue->attr.instrumentation = LTTNG_UST_FUNCTION; |
97ee3a89 DG |
220 | break; |
221 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
44d3bd01 | 222 | lue->attr.instrumentation = LTTNG_UST_FUNCTION; |
97ee3a89 DG |
223 | break; |
224 | case LTTNG_EVENT_TRACEPOINT: | |
44d3bd01 | 225 | lue->attr.instrumentation = LTTNG_UST_TRACEPOINT; |
97ee3a89 DG |
226 | break; |
227 | default: | |
228 | ERR("Unknown ust instrumentation type (%d)", ev->type); | |
229 | goto error; | |
230 | } | |
231 | ||
232 | /* Copy event name */ | |
44d3bd01 DG |
233 | strncpy(lue->attr.name, ev->name, LTTNG_UST_SYM_NAME_LEN); |
234 | lue->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0'; | |
97ee3a89 DG |
235 | |
236 | /* Setting up a ust event */ | |
237 | lue->handle = -1; | |
97ee3a89 | 238 | lue->enabled = 1; |
44d3bd01 | 239 | memset(&lue->ctx, 0, sizeof(struct lttng_ust_context)); |
97ee3a89 DG |
240 | |
241 | return lue; | |
242 | ||
243 | error: | |
244 | return NULL; | |
245 | } | |
246 | ||
247 | /* | |
248 | * Allocate and initialize a ust metadata. | |
249 | * | |
250 | * Return pointer to structure or NULL. | |
251 | */ | |
252 | struct ltt_ust_metadata *trace_ust_create_metadata(char *path) | |
253 | { | |
254 | int ret; | |
255 | struct ltt_ust_metadata *lum; | |
97ee3a89 DG |
256 | |
257 | lum = malloc(sizeof(struct ltt_ust_metadata)); | |
44d3bd01 | 258 | if (lum == NULL) { |
97ee3a89 DG |
259 | perror("ust metadata malloc"); |
260 | goto error; | |
261 | } | |
262 | ||
263 | /* Set default attributes */ | |
44d3bd01 DG |
264 | lum->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE; |
265 | lum->attr.subbuf_size = DEFAULT_METADATA_SUBBUF_SIZE; | |
266 | lum->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM; | |
267 | lum->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER; | |
268 | lum->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER; | |
269 | lum->attr.output = DEFAULT_UST_CHANNEL_OUTPUT; | |
270 | ||
97ee3a89 DG |
271 | lum->handle = -1; |
272 | /* Set metadata trace path */ | |
273 | ret = asprintf(&lum->trace_path, "%s/metadata", path); | |
274 | if (ret < 0) { | |
275 | perror("asprintf ust metadata"); | |
276 | goto error; | |
277 | } | |
278 | ||
279 | return lum; | |
280 | ||
281 | error: | |
282 | return NULL; | |
283 | } | |
284 | ||
285 | /* | |
286 | * Cleanup ust event structure. | |
287 | */ | |
288 | void trace_ust_destroy_event(struct ltt_ust_event *event) | |
289 | { | |
44d3bd01 | 290 | DBG("[trace] Destroy ust event %s", event->attr.name); |
97ee3a89 DG |
291 | |
292 | /* Remove from event list */ | |
293 | cds_list_del(&event->list); | |
294 | free(event); | |
295 | } | |
296 | ||
297 | /* | |
298 | * Cleanup ust channel structure. | |
299 | */ | |
300 | void trace_ust_destroy_channel(struct ltt_ust_channel *channel) | |
301 | { | |
302 | struct ltt_ust_event *event, *etmp; | |
303 | ||
304 | DBG("[trace] Destroy ust channel %d", channel->handle); | |
305 | ||
97ee3a89 DG |
306 | /* For each event in the channel list */ |
307 | cds_list_for_each_entry_safe(event, etmp, &channel->events.head, list) { | |
308 | trace_ust_destroy_event(event); | |
309 | } | |
310 | ||
311 | /* Remove from channel list */ | |
312 | cds_list_del(&channel->list); | |
313 | free(channel); | |
314 | } | |
315 | ||
316 | /* | |
317 | * Cleanup ust metadata structure. | |
318 | */ | |
319 | void trace_ust_destroy_metadata(struct ltt_ust_metadata *metadata) | |
320 | { | |
321 | DBG("[trace] Destroy ust metadata %d", metadata->handle); | |
322 | ||
323 | /* Free attributes */ | |
97ee3a89 DG |
324 | free(metadata->trace_path); |
325 | ||
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 | } |