Commit | Line | Data |
---|---|---|
b579acd9 | 1 | /* |
bdf64013 JG |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
3 | * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
b579acd9 | 4 | * |
d14d33bf AM |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License, version 2 only, | |
7 | * as published by the Free Software Foundation. | |
b579acd9 | 8 | * |
d14d33bf AM |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
b579acd9 | 13 | * |
d14d33bf AM |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
b579acd9 DG |
17 | */ |
18 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
b579acd9 DG |
20 | #include <stdio.h> |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
54d01ffb | 24 | #include <urcu/list.h> |
1e307fab | 25 | |
db758600 | 26 | #include <common/error.h> |
10a8a223 | 27 | #include <common/sessiond-comm/sessiond-comm.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" |
bdf64013 | 33 | #include "agent.h" |
b579acd9 | 34 | |
b579acd9 DG |
35 | /* |
36 | * Add kernel context to all channel. | |
df3c77c8 JG |
37 | * |
38 | * Assumes the ownership of kctx. | |
b579acd9 DG |
39 | */ |
40 | static int add_kctx_all_channels(struct ltt_kernel_session *ksession, | |
645328ae | 41 | struct ltt_kernel_context *kctx) |
b579acd9 | 42 | { |
601d5acf | 43 | int ret; |
b579acd9 DG |
44 | struct ltt_kernel_channel *kchan; |
45 | ||
0525e9ae DG |
46 | assert(ksession); |
47 | assert(kctx); | |
48 | ||
601d5acf | 49 | DBG("Adding kernel context to all channels"); |
b579acd9 DG |
50 | |
51 | /* Go over all channels */ | |
52 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { | |
df3c77c8 JG |
53 | struct ltt_kernel_context *kctx_copy; |
54 | ||
55 | kctx_copy = trace_kernel_copy_context(kctx); | |
56 | if (!kctx_copy) { | |
57 | PERROR("zmalloc ltt_kernel_context"); | |
58 | goto error; | |
59 | } | |
60 | ||
61 | /* Ownership of kctx_copy is transferred to the callee. */ | |
62 | ret = kernel_add_channel_context(kchan, kctx_copy); | |
63 | kctx_copy = NULL; | |
1ae5e83e | 64 | if (ret != 0) { |
601d5acf | 65 | goto error; |
b579acd9 DG |
66 | } |
67 | } | |
68 | ||
f73fabfd | 69 | ret = LTTNG_OK; |
b579acd9 DG |
70 | |
71 | error: | |
df3c77c8 | 72 | trace_kernel_destroy_context(kctx); |
b579acd9 DG |
73 | return ret; |
74 | } | |
75 | ||
76 | /* | |
77 | * Add kernel context to a specific channel. | |
df3c77c8 JG |
78 | * |
79 | * Assumes the ownership of kctx. | |
b579acd9 | 80 | */ |
645328ae | 81 | static int add_kctx_to_channel(struct ltt_kernel_context *kctx, |
601d5acf | 82 | struct ltt_kernel_channel *kchan) |
b579acd9 | 83 | { |
601d5acf | 84 | int ret; |
b579acd9 | 85 | |
0525e9ae DG |
86 | assert(kchan); |
87 | assert(kctx); | |
88 | ||
601d5acf | 89 | DBG("Add kernel context to channel '%s'", kchan->channel->name); |
b579acd9 | 90 | |
df3c77c8 | 91 | /* Ownership of kctx is transferred to the callee. */ |
601d5acf | 92 | ret = kernel_add_channel_context(kchan, kctx); |
df3c77c8 | 93 | kctx = NULL; |
1ae5e83e | 94 | if (ret != 0) { |
b579acd9 DG |
95 | goto error; |
96 | } | |
97 | ||
f73fabfd | 98 | ret = LTTNG_OK; |
b579acd9 DG |
99 | |
100 | error: | |
101 | return ret; | |
102 | } | |
103 | ||
6b79ed20 DG |
104 | /* |
105 | * Add UST context to channel. | |
106 | */ | |
4ce7709b JG |
107 | static int add_uctx_to_channel(struct ltt_ust_session *usess, |
108 | enum lttng_domain_type domain, | |
6b79ed20 DG |
109 | struct ltt_ust_channel *uchan, struct lttng_event_context *ctx) |
110 | { | |
111 | int ret; | |
bdf64013 | 112 | struct ltt_ust_context *uctx = NULL; |
6b79ed20 | 113 | |
0525e9ae DG |
114 | assert(usess); |
115 | assert(uchan); | |
116 | assert(ctx); | |
117 | ||
aa3514e9 MD |
118 | /* Check if context is duplicate */ |
119 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { | |
120 | if (trace_ust_match_context(uctx, ctx)) { | |
121 | ret = -EEXIST; | |
122 | goto duplicate; | |
123 | } | |
124 | } | |
bdf64013 JG |
125 | uctx = NULL; |
126 | ||
127 | switch (domain) { | |
128 | case LTTNG_DOMAIN_JUL: | |
129 | case LTTNG_DOMAIN_LOG4J: | |
130 | { | |
442f8c07 JG |
131 | struct agent *agt; |
132 | ||
133 | if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) { | |
134 | /* Other contexts are not needed by the agent. */ | |
135 | break; | |
136 | } | |
137 | agt = trace_ust_find_agent(usess, domain); | |
bdf64013 JG |
138 | |
139 | if (!agt) { | |
140 | agt = agent_create(domain); | |
141 | if (!agt) { | |
142 | ret = LTTNG_ERR_NOMEM; | |
143 | goto error; | |
144 | } | |
145 | agent_add(agt, usess->agents); | |
146 | } | |
147 | ret = agent_add_context(ctx, agt); | |
148 | if (ret != LTTNG_OK) { | |
149 | goto error; | |
150 | } | |
151 | ||
152 | ret = agent_enable_context(ctx, domain); | |
153 | if (ret != LTTNG_OK) { | |
154 | goto error; | |
155 | } | |
156 | break; | |
157 | } | |
158 | case LTTNG_DOMAIN_UST: | |
159 | break; | |
160 | default: | |
161 | assert(0); | |
162 | } | |
aa3514e9 | 163 | |
6b79ed20 DG |
164 | /* Create ltt UST context */ |
165 | uctx = trace_ust_create_context(ctx); | |
166 | if (uctx == NULL) { | |
727d5404 | 167 | ret = -EINVAL; |
6b79ed20 DG |
168 | goto error; |
169 | } | |
170 | ||
c0c66aec JG |
171 | ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx); |
172 | if (ret < 0) { | |
727d5404 DG |
173 | goto error; |
174 | } | |
175 | ||
e7fe706f DG |
176 | rcu_read_lock(); |
177 | ||
6b79ed20 | 178 | /* Add ltt UST context node to ltt UST channel */ |
aa3514e9 | 179 | lttng_ht_add_ulong(uchan->ctx, &uctx->node); |
e7fe706f | 180 | rcu_read_unlock(); |
31746f93 | 181 | cds_list_add_tail(&uctx->list, &uchan->ctx_list); |
6b79ed20 | 182 | |
727d5404 DG |
183 | DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name); |
184 | ||
185 | return 0; | |
6b79ed20 DG |
186 | |
187 | error: | |
188 | free(uctx); | |
aa3514e9 | 189 | duplicate: |
6b79ed20 DG |
190 | return ret; |
191 | } | |
192 | ||
b579acd9 DG |
193 | /* |
194 | * Add kernel context to tracer. | |
195 | */ | |
54d01ffb | 196 | int context_kernel_add(struct ltt_kernel_session *ksession, |
601d5acf | 197 | struct lttng_event_context *ctx, char *channel_name) |
b579acd9 DG |
198 | { |
199 | int ret; | |
200 | struct ltt_kernel_channel *kchan; | |
645328ae | 201 | struct ltt_kernel_context *kctx; |
54d01ffb | 202 | |
0525e9ae DG |
203 | assert(ksession); |
204 | assert(ctx); | |
205 | assert(channel_name); | |
206 | ||
645328ae DG |
207 | kctx = trace_kernel_create_context(NULL); |
208 | if (!kctx) { | |
209 | ret = LTTNG_ERR_NOMEM; | |
210 | goto error; | |
211 | } | |
212 | ||
54d01ffb | 213 | /* Setup kernel context structure */ |
9197c5c4 MD |
214 | switch (ctx->ctx) { |
215 | case LTTNG_EVENT_CONTEXT_PID: | |
645328ae | 216 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID; |
9197c5c4 | 217 | break; |
9197c5c4 | 218 | case LTTNG_EVENT_CONTEXT_PROCNAME: |
645328ae | 219 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME; |
9197c5c4 MD |
220 | break; |
221 | case LTTNG_EVENT_CONTEXT_PRIO: | |
645328ae | 222 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO; |
9197c5c4 MD |
223 | break; |
224 | case LTTNG_EVENT_CONTEXT_NICE: | |
645328ae | 225 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NICE; |
9197c5c4 MD |
226 | break; |
227 | case LTTNG_EVENT_CONTEXT_VPID: | |
645328ae | 228 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPID; |
9197c5c4 MD |
229 | break; |
230 | case LTTNG_EVENT_CONTEXT_TID: | |
645328ae | 231 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_TID; |
9197c5c4 MD |
232 | break; |
233 | case LTTNG_EVENT_CONTEXT_VTID: | |
645328ae | 234 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VTID; |
9197c5c4 MD |
235 | break; |
236 | case LTTNG_EVENT_CONTEXT_PPID: | |
645328ae | 237 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PPID; |
9197c5c4 MD |
238 | break; |
239 | case LTTNG_EVENT_CONTEXT_VPPID: | |
645328ae | 240 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID; |
9197c5c4 | 241 | break; |
54773d68 | 242 | case LTTNG_EVENT_CONTEXT_HOSTNAME: |
645328ae | 243 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME; |
54773d68 | 244 | break; |
aa3514e9 MD |
245 | case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER: |
246 | case LTTNG_EVENT_CONTEXT_PERF_COUNTER: | |
645328ae | 247 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER; |
aa3514e9 | 248 | break; |
1ae5e83e JD |
249 | case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE: |
250 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE; | |
251 | break; | |
252 | case LTTNG_EVENT_CONTEXT_PREEMPTIBLE: | |
253 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PREEMPTIBLE; | |
254 | break; | |
255 | case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE: | |
256 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE; | |
257 | break; | |
258 | case LTTNG_EVENT_CONTEXT_MIGRATABLE: | |
259 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_MIGRATABLE; | |
260 | break; | |
9197c5c4 | 261 | default: |
fc7324e8 DG |
262 | ret = LTTNG_ERR_KERN_CONTEXT_FAIL; |
263 | goto error; | |
9197c5c4 MD |
264 | } |
265 | ||
645328ae DG |
266 | kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type; |
267 | kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config; | |
268 | strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name, | |
54d01ffb | 269 | LTTNG_SYMBOL_NAME_LEN); |
645328ae | 270 | kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
b579acd9 | 271 | |
9d035200 | 272 | if (*channel_name == '\0') { |
645328ae | 273 | ret = add_kctx_all_channels(ksession, kctx); |
df3c77c8 JG |
274 | /* Ownership of kctx is transferred to the callee. */ |
275 | kctx = NULL; | |
f73fabfd | 276 | if (ret != LTTNG_OK) { |
b579acd9 DG |
277 | goto error; |
278 | } | |
279 | } else { | |
280 | /* Get kernel channel */ | |
62499ad6 | 281 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
b579acd9 | 282 | if (kchan == NULL) { |
f73fabfd | 283 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
b579acd9 DG |
284 | goto error; |
285 | } | |
286 | ||
645328ae | 287 | ret = add_kctx_to_channel(kctx, kchan); |
df3c77c8 JG |
288 | /* Ownership of kctx is transferred to the callee. */ |
289 | kctx = NULL; | |
f73fabfd | 290 | if (ret != LTTNG_OK) { |
b579acd9 DG |
291 | goto error; |
292 | } | |
293 | } | |
294 | ||
df3c77c8 | 295 | ret = LTTNG_OK; |
b579acd9 DG |
296 | |
297 | error: | |
fc7324e8 DG |
298 | if (kctx) { |
299 | trace_kernel_destroy_context(kctx); | |
300 | } | |
b579acd9 DG |
301 | return ret; |
302 | } | |
2bdd86d4 MD |
303 | |
304 | /* | |
55cc08a6 | 305 | * Add UST context to tracer. |
2bdd86d4 | 306 | */ |
4ce7709b JG |
307 | int context_ust_add(struct ltt_ust_session *usess, |
308 | enum lttng_domain_type domain, struct lttng_event_context *ctx, | |
309 | char *channel_name) | |
2bdd86d4 | 310 | { |
601d5acf | 311 | int ret = LTTNG_OK; |
442359c0 | 312 | struct lttng_ht_iter iter; |
bec39940 | 313 | struct lttng_ht *chan_ht; |
55cc08a6 | 314 | struct ltt_ust_channel *uchan = NULL; |
2bdd86d4 | 315 | |
0525e9ae DG |
316 | assert(usess); |
317 | assert(ctx); | |
318 | assert(channel_name); | |
319 | ||
2223c96f DG |
320 | rcu_read_lock(); |
321 | ||
f5501dad | 322 | chan_ht = usess->domain_global.channels; |
2bdd86d4 | 323 | |
55cc08a6 | 324 | /* Get UST channel if defined */ |
9f9ee9c9 | 325 | if (channel_name[0] != '\0') { |
55cc08a6 DG |
326 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); |
327 | if (uchan == NULL) { | |
f73fabfd | 328 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2bdd86d4 MD |
329 | goto error; |
330 | } | |
55cc08a6 DG |
331 | } |
332 | ||
601d5acf DG |
333 | if (uchan) { |
334 | /* Add ctx to channel */ | |
6b79ed20 | 335 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
601d5acf | 336 | } else { |
e7fe706f | 337 | rcu_read_lock(); |
601d5acf | 338 | /* Add ctx all events, all channels */ |
bec39940 | 339 | cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) { |
6b79ed20 | 340 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
55cc08a6 | 341 | if (ret < 0) { |
9589be74 JG |
342 | ERR("Failed to add context to channel %s", |
343 | uchan->name); | |
55cc08a6 DG |
344 | continue; |
345 | } | |
2bdd86d4 | 346 | } |
e7fe706f | 347 | rcu_read_unlock(); |
55cc08a6 | 348 | } |
2bdd86d4 | 349 | |
55cc08a6 DG |
350 | switch (ret) { |
351 | case -EEXIST: | |
f73fabfd | 352 | ret = LTTNG_ERR_UST_CONTEXT_EXIST; |
727d5404 | 353 | break; |
55cc08a6 | 354 | case -ENOMEM: |
f73fabfd | 355 | ret = LTTNG_ERR_FATAL; |
727d5404 DG |
356 | break; |
357 | case -EINVAL: | |
f73fabfd | 358 | ret = LTTNG_ERR_UST_CONTEXT_INVAL; |
727d5404 DG |
359 | break; |
360 | case -ENOSYS: | |
f73fabfd | 361 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
727d5404 DG |
362 | break; |
363 | default: | |
f73fabfd | 364 | ret = LTTNG_OK; |
727d5404 | 365 | break; |
2bdd86d4 MD |
366 | } |
367 | ||
2bdd86d4 | 368 | error: |
2223c96f | 369 | rcu_read_unlock(); |
2bdd86d4 MD |
370 | return ret; |
371 | } |