Commit | Line | Data |
---|---|---|
b579acd9 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
1e307fab DG |
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. | |
b579acd9 | 7 | * |
1e307fab DG |
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. | |
b579acd9 | 12 | * |
1e307fab DG |
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. | |
b579acd9 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
19 | #include <stdio.h> | |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | #include <unistd.h> | |
54d01ffb | 23 | #include <urcu/list.h> |
1e307fab | 24 | |
bec39940 | 25 | #include <lttng-ht.h> |
1e307fab | 26 | #include <lttng-sessiond-comm.h> |
54d01ffb | 27 | #include <lttngerr.h> |
b579acd9 | 28 | |
b579acd9 | 29 | #include "context.h" |
4771f025 | 30 | #include "kernel.h" |
55cc08a6 | 31 | #include "ust-app.h" |
34378f76 | 32 | #include "trace-ust.h" |
b579acd9 DG |
33 | |
34 | /* | |
35 | * Add kernel context to an event of a specific channel. | |
36 | */ | |
37 | static int add_kctx_to_event(struct lttng_kernel_context *kctx, | |
38 | struct ltt_kernel_channel *kchan, char *event_name) | |
39 | { | |
40 | int ret, found = 0; | |
41 | struct ltt_kernel_event *kevent; | |
42 | ||
43 | DBG("Add kernel context to event %s", event_name); | |
44 | ||
62499ad6 | 45 | kevent = trace_kernel_get_event_by_name(event_name, kchan); |
b579acd9 DG |
46 | if (kevent != NULL) { |
47 | ret = kernel_add_event_context(kevent, kctx); | |
48 | if (ret < 0) { | |
49 | goto error; | |
50 | } | |
51 | found = 1; | |
52 | } | |
53 | ||
54 | ret = found; | |
55 | ||
56 | error: | |
57 | return ret; | |
58 | } | |
59 | ||
60 | /* | |
61 | * Add kernel context to all channel. | |
62 | * | |
63 | * If event_name is specified, add context to event instead. | |
64 | */ | |
65 | static int add_kctx_all_channels(struct ltt_kernel_session *ksession, | |
66 | struct lttng_kernel_context *kctx, char *event_name) | |
67 | { | |
68 | int ret, no_event = 0, found = 0; | |
69 | struct ltt_kernel_channel *kchan; | |
70 | ||
71 | if (strlen(event_name) == 0) { | |
72 | no_event = 1; | |
73 | } | |
74 | ||
75 | DBG("Adding kernel context to all channels (event: %s)", event_name); | |
76 | ||
77 | /* Go over all channels */ | |
78 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { | |
79 | if (no_event) { | |
80 | ret = kernel_add_channel_context(kchan, kctx); | |
81 | if (ret < 0) { | |
82 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
83 | goto error; | |
84 | } | |
85 | } else { | |
86 | ret = add_kctx_to_event(kctx, kchan, event_name); | |
87 | if (ret < 0) { | |
88 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
89 | goto error; | |
90 | } else if (ret == 1) { | |
91 | /* Event found and context added */ | |
92 | found = 1; | |
93 | break; | |
94 | } | |
95 | } | |
96 | } | |
97 | ||
98 | if (!found && !no_event) { | |
99 | ret = LTTCOMM_NO_EVENT; | |
100 | goto error; | |
101 | } | |
102 | ||
103 | ret = LTTCOMM_OK; | |
104 | ||
105 | error: | |
106 | return ret; | |
107 | } | |
108 | ||
109 | /* | |
110 | * Add kernel context to a specific channel. | |
111 | * | |
112 | * If event_name is specified, add context to that event. | |
113 | */ | |
114 | static int add_kctx_to_channel(struct lttng_kernel_context *kctx, | |
115 | struct ltt_kernel_channel *kchan, char *event_name) | |
116 | { | |
117 | int ret, no_event = 0, found = 0; | |
118 | ||
119 | if (strlen(event_name) == 0) { | |
120 | no_event = 1; | |
121 | } | |
122 | ||
123 | DBG("Add kernel context to channel '%s', event '%s'", | |
124 | kchan->channel->name, event_name); | |
125 | ||
126 | if (no_event) { | |
127 | ret = kernel_add_channel_context(kchan, kctx); | |
128 | if (ret < 0) { | |
129 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
130 | goto error; | |
131 | } | |
132 | } else { | |
133 | ret = add_kctx_to_event(kctx, kchan, event_name); | |
134 | if (ret < 0) { | |
135 | ret = LTTCOMM_KERN_CONTEXT_FAIL; | |
136 | goto error; | |
137 | } else if (ret == 1) { | |
138 | /* Event found and context added */ | |
139 | found = 1; | |
140 | } | |
141 | } | |
142 | ||
143 | if (!found && !no_event) { | |
144 | ret = LTTCOMM_NO_EVENT; | |
145 | goto error; | |
146 | } | |
147 | ||
148 | ret = LTTCOMM_OK; | |
149 | ||
150 | error: | |
151 | return ret; | |
152 | } | |
153 | ||
6b79ed20 DG |
154 | /* |
155 | * Add UST context to channel. | |
156 | */ | |
157 | static int add_uctx_to_channel(struct ltt_ust_session *usess, int domain, | |
158 | struct ltt_ust_channel *uchan, struct lttng_event_context *ctx) | |
159 | { | |
160 | int ret; | |
161 | struct ltt_ust_context *uctx; | |
162 | ||
163 | /* Create ltt UST context */ | |
164 | uctx = trace_ust_create_context(ctx); | |
165 | if (uctx == NULL) { | |
166 | ret = LTTCOMM_FATAL; | |
167 | goto error; | |
168 | } | |
169 | ||
170 | switch (domain) { | |
171 | case LTTNG_DOMAIN_UST: | |
172 | ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx); | |
173 | if (ret < 0) { | |
174 | goto error; | |
175 | } | |
176 | break; | |
177 | default: | |
178 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
179 | goto error; | |
180 | } | |
181 | ||
182 | /* Add ltt UST context node to ltt UST channel */ | |
bec39940 | 183 | lttng_ht_add_unique_ulong(uchan->ctx, &uctx->node); |
6b79ed20 DG |
184 | |
185 | return LTTCOMM_OK; | |
186 | ||
187 | error: | |
188 | free(uctx); | |
189 | return ret; | |
190 | } | |
191 | ||
192 | /* | |
193 | * Add UST context to event. | |
194 | */ | |
195 | static int add_uctx_to_event(struct ltt_ust_session *usess, int domain, | |
196 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, | |
197 | struct lttng_event_context *ctx) | |
198 | { | |
199 | int ret; | |
200 | struct ltt_ust_context *uctx; | |
201 | ||
202 | /* Create ltt UST context */ | |
203 | uctx = trace_ust_create_context(ctx); | |
204 | if (uctx == NULL) { | |
205 | ret = LTTCOMM_FATAL; | |
206 | goto error; | |
207 | } | |
208 | ||
209 | switch (domain) { | |
210 | case LTTNG_DOMAIN_UST: | |
211 | ret = ust_app_add_ctx_event_glb(usess, uchan, uevent, uctx); | |
212 | if (ret < 0) { | |
213 | goto error; | |
214 | } | |
215 | break; | |
216 | default: | |
217 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
218 | goto error; | |
219 | } | |
220 | ||
221 | /* Add ltt UST context node to ltt UST event */ | |
bec39940 | 222 | lttng_ht_add_unique_ulong(uevent->ctx, &uctx->node); |
6b79ed20 DG |
223 | |
224 | return LTTCOMM_OK; | |
225 | ||
226 | error: | |
227 | free(uctx); | |
228 | return ret; | |
229 | } | |
230 | ||
b579acd9 DG |
231 | /* |
232 | * Add kernel context to tracer. | |
233 | */ | |
54d01ffb DG |
234 | int context_kernel_add(struct ltt_kernel_session *ksession, |
235 | struct lttng_event_context *ctx, char *event_name, | |
b579acd9 DG |
236 | char *channel_name) |
237 | { | |
238 | int ret; | |
239 | struct ltt_kernel_channel *kchan; | |
54d01ffb DG |
240 | struct lttng_kernel_context kctx; |
241 | ||
242 | /* Setup kernel context structure */ | |
243 | kctx.ctx = ctx->ctx; | |
244 | kctx.u.perf_counter.type = ctx->u.perf_counter.type; | |
245 | kctx.u.perf_counter.config = ctx->u.perf_counter.config; | |
246 | strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name, | |
247 | LTTNG_SYMBOL_NAME_LEN); | |
248 | kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
b579acd9 DG |
249 | |
250 | if (strlen(channel_name) == 0) { | |
54d01ffb | 251 | ret = add_kctx_all_channels(ksession, &kctx, event_name); |
b579acd9 DG |
252 | if (ret != LTTCOMM_OK) { |
253 | goto error; | |
254 | } | |
255 | } else { | |
256 | /* Get kernel channel */ | |
62499ad6 | 257 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
b579acd9 DG |
258 | if (kchan == NULL) { |
259 | ret = LTTCOMM_KERN_CHAN_NOT_FOUND; | |
260 | goto error; | |
261 | } | |
262 | ||
54d01ffb | 263 | ret = add_kctx_to_channel(&kctx, kchan, event_name); |
b579acd9 DG |
264 | if (ret != LTTCOMM_OK) { |
265 | goto error; | |
266 | } | |
267 | } | |
268 | ||
269 | ret = LTTCOMM_OK; | |
270 | ||
271 | error: | |
272 | return ret; | |
273 | } | |
2bdd86d4 MD |
274 | |
275 | /* | |
55cc08a6 | 276 | * Add UST context to tracer. |
2bdd86d4 | 277 | */ |
55cc08a6 DG |
278 | int context_ust_add(struct ltt_ust_session *usess, int domain, |
279 | struct lttng_event_context *ctx, char *event_name, | |
280 | char *channel_name) | |
2bdd86d4 | 281 | { |
55cc08a6 | 282 | int ret = LTTCOMM_OK, have_event = 0; |
bec39940 DG |
283 | struct lttng_ht_iter iter, uiter; |
284 | struct lttng_ht *chan_ht; | |
55cc08a6 DG |
285 | struct ltt_ust_channel *uchan = NULL; |
286 | struct ltt_ust_event *uevent = NULL; | |
2bdd86d4 | 287 | |
6b79ed20 DG |
288 | /* |
289 | * Define which channel's hashtable to use from the domain or quit if | |
290 | * unknown domain. | |
291 | */ | |
55cc08a6 DG |
292 | switch (domain) { |
293 | case LTTNG_DOMAIN_UST: | |
294 | chan_ht = usess->domain_global.channels; | |
295 | break; | |
296 | case LTTNG_DOMAIN_UST_EXEC_NAME: | |
297 | case LTTNG_DOMAIN_UST_PID: | |
298 | case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN: | |
299 | default: | |
300 | ret = LTTCOMM_NOT_IMPLEMENTED; | |
2bdd86d4 MD |
301 | goto error; |
302 | } | |
2bdd86d4 | 303 | |
6b79ed20 | 304 | /* Do we have an event name */ |
55cc08a6 DG |
305 | if (strlen(event_name) != 0) { |
306 | have_event = 1; | |
2bdd86d4 MD |
307 | } |
308 | ||
55cc08a6 DG |
309 | /* Get UST channel if defined */ |
310 | if (strlen(channel_name) != 0) { | |
311 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
312 | if (uchan == NULL) { | |
313 | ret = LTTCOMM_UST_CHAN_NOT_FOUND; | |
2bdd86d4 MD |
314 | goto error; |
315 | } | |
55cc08a6 DG |
316 | } |
317 | ||
318 | /* If UST channel specified and event name, get UST event ref */ | |
319 | if (uchan && have_event) { | |
320 | uevent = trace_ust_find_event_by_name(uchan->events, event_name); | |
321 | if (uevent == NULL) { | |
322 | ret = LTTCOMM_UST_EVENT_NOT_FOUND; | |
2bdd86d4 | 323 | goto error; |
2bdd86d4 MD |
324 | } |
325 | } | |
326 | ||
55cc08a6 DG |
327 | /* At this point, we have 4 possibilities */ |
328 | ||
6b79ed20 DG |
329 | if (uchan && uevent) { /* Add ctx to event in channel */ |
330 | ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx); | |
55cc08a6 | 331 | } else if (uchan && !have_event) { /* Add ctx to channel */ |
6b79ed20 | 332 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
55cc08a6 | 333 | } else if (!uchan && have_event) { /* Add ctx to event */ |
6b79ed20 | 334 | /* Add context to event without having the channel name */ |
bec39940 | 335 | cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) { |
55cc08a6 DG |
336 | uevent = trace_ust_find_event_by_name(uchan->events, event_name); |
337 | if (uevent != NULL) { | |
6b79ed20 | 338 | ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx); |
55cc08a6 DG |
339 | /* |
340 | * LTTng UST does not allowed the same event to be registered | |
341 | * multiple time in different or the same channel. So, if we | |
6b79ed20 | 342 | * found our event, we stop. |
55cc08a6 DG |
343 | */ |
344 | goto end; | |
345 | } | |
2bdd86d4 | 346 | } |
55cc08a6 | 347 | ret = LTTCOMM_UST_EVENT_NOT_FOUND; |
6b79ed20 DG |
348 | goto error; |
349 | } else if (!uchan && !have_event) { /* Add ctx all events, all channels */ | |
350 | /* For all channels */ | |
bec39940 | 351 | cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) { |
6b79ed20 | 352 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
55cc08a6 | 353 | if (ret < 0) { |
6b79ed20 | 354 | ERR("Context added to channel %s failed", uchan->name); |
55cc08a6 DG |
355 | continue; |
356 | } | |
6b79ed20 DG |
357 | |
358 | /* For all events in channel */ | |
bec39940 DG |
359 | cds_lfht_for_each_entry(uchan->events->ht, &uiter.iter, uevent, |
360 | node.node) { | |
6b79ed20 | 361 | ret = add_uctx_to_event(usess, domain, uchan, uevent, ctx); |
55cc08a6 | 362 | if (ret < 0) { |
6b79ed20 DG |
363 | ERR("Context add to event %s in channel %s failed", |
364 | uevent->attr.name, uchan->name); | |
55cc08a6 DG |
365 | continue; |
366 | } | |
55cc08a6 | 367 | } |
2bdd86d4 | 368 | } |
55cc08a6 | 369 | } |
2bdd86d4 | 370 | |
55cc08a6 DG |
371 | end: |
372 | switch (ret) { | |
373 | case -EEXIST: | |
374 | ret = LTTCOMM_UST_CONTEXT_EXIST; | |
6b79ed20 | 375 | goto error; |
55cc08a6 DG |
376 | case -ENOMEM: |
377 | ret = LTTCOMM_FATAL; | |
6b79ed20 | 378 | goto error; |
2bdd86d4 MD |
379 | } |
380 | ||
55cc08a6 | 381 | return LTTCOMM_OK; |
2bdd86d4 MD |
382 | |
383 | error: | |
384 | return ret; | |
385 | } |