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"); | |
7682f304 | 58 | ret = -LTTNG_ERR_NOMEM; |
df3c77c8 JG |
59 | goto error; |
60 | } | |
61 | ||
62 | /* Ownership of kctx_copy is transferred to the callee. */ | |
63 | ret = kernel_add_channel_context(kchan, kctx_copy); | |
64 | kctx_copy = NULL; | |
1ae5e83e | 65 | if (ret != 0) { |
601d5acf | 66 | goto error; |
b579acd9 DG |
67 | } |
68 | } | |
69 | ||
f73fabfd | 70 | ret = LTTNG_OK; |
b579acd9 DG |
71 | |
72 | error: | |
df3c77c8 | 73 | trace_kernel_destroy_context(kctx); |
b579acd9 DG |
74 | return ret; |
75 | } | |
76 | ||
77 | /* | |
78 | * Add kernel context to a specific channel. | |
df3c77c8 JG |
79 | * |
80 | * Assumes the ownership of kctx. | |
b579acd9 | 81 | */ |
645328ae | 82 | static int add_kctx_to_channel(struct ltt_kernel_context *kctx, |
601d5acf | 83 | struct ltt_kernel_channel *kchan) |
b579acd9 | 84 | { |
601d5acf | 85 | int ret; |
b579acd9 | 86 | |
0525e9ae DG |
87 | assert(kchan); |
88 | assert(kctx); | |
89 | ||
601d5acf | 90 | DBG("Add kernel context to channel '%s'", kchan->channel->name); |
b579acd9 | 91 | |
df3c77c8 | 92 | /* Ownership of kctx is transferred to the callee. */ |
601d5acf | 93 | ret = kernel_add_channel_context(kchan, kctx); |
df3c77c8 | 94 | kctx = NULL; |
1ae5e83e | 95 | if (ret != 0) { |
b579acd9 DG |
96 | goto error; |
97 | } | |
98 | ||
f73fabfd | 99 | ret = LTTNG_OK; |
b579acd9 DG |
100 | |
101 | error: | |
102 | return ret; | |
103 | } | |
104 | ||
6b79ed20 DG |
105 | /* |
106 | * Add UST context to channel. | |
107 | */ | |
4ce7709b JG |
108 | static int add_uctx_to_channel(struct ltt_ust_session *usess, |
109 | enum lttng_domain_type domain, | |
df4f5a87 JG |
110 | struct ltt_ust_channel *uchan, |
111 | const struct lttng_event_context *ctx) | |
6b79ed20 DG |
112 | { |
113 | int ret; | |
bdf64013 | 114 | struct ltt_ust_context *uctx = NULL; |
6b79ed20 | 115 | |
0525e9ae DG |
116 | assert(usess); |
117 | assert(uchan); | |
118 | assert(ctx); | |
119 | ||
aa3514e9 MD |
120 | /* Check if context is duplicate */ |
121 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { | |
122 | if (trace_ust_match_context(uctx, ctx)) { | |
7682f304 | 123 | ret = LTTNG_ERR_UST_CONTEXT_EXIST; |
aa3514e9 MD |
124 | goto duplicate; |
125 | } | |
126 | } | |
bdf64013 JG |
127 | uctx = NULL; |
128 | ||
129 | switch (domain) { | |
130 | case LTTNG_DOMAIN_JUL: | |
131 | case LTTNG_DOMAIN_LOG4J: | |
132 | { | |
442f8c07 JG |
133 | struct agent *agt; |
134 | ||
135 | if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) { | |
136 | /* Other contexts are not needed by the agent. */ | |
137 | break; | |
138 | } | |
139 | agt = trace_ust_find_agent(usess, domain); | |
bdf64013 JG |
140 | |
141 | if (!agt) { | |
142 | agt = agent_create(domain); | |
143 | if (!agt) { | |
7682f304 | 144 | ret = -LTTNG_ERR_NOMEM; |
bdf64013 JG |
145 | goto error; |
146 | } | |
147 | agent_add(agt, usess->agents); | |
148 | } | |
149 | ret = agent_add_context(ctx, agt); | |
150 | if (ret != LTTNG_OK) { | |
151 | goto error; | |
152 | } | |
153 | ||
154 | ret = agent_enable_context(ctx, domain); | |
155 | if (ret != LTTNG_OK) { | |
156 | goto error; | |
157 | } | |
158 | break; | |
159 | } | |
160 | case LTTNG_DOMAIN_UST: | |
161 | break; | |
162 | default: | |
163 | assert(0); | |
164 | } | |
aa3514e9 | 165 | |
6b79ed20 DG |
166 | /* Create ltt UST context */ |
167 | uctx = trace_ust_create_context(ctx); | |
168 | if (uctx == NULL) { | |
7682f304 | 169 | ret = LTTNG_ERR_UST_CONTEXT_INVAL; |
6b79ed20 DG |
170 | goto error; |
171 | } | |
172 | ||
6b79ed20 | 173 | /* Add ltt UST context node to ltt UST channel */ |
aa3514e9 | 174 | lttng_ht_add_ulong(uchan->ctx, &uctx->node); |
31746f93 | 175 | cds_list_add_tail(&uctx->list, &uchan->ctx_list); |
6b79ed20 | 176 | |
88e3c2f5 JG |
177 | if (!usess->active) { |
178 | goto end; | |
179 | } | |
180 | ||
181 | ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx); | |
182 | if (ret < 0) { | |
183 | goto error; | |
184 | } | |
185 | end: | |
727d5404 DG |
186 | DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name); |
187 | ||
188 | return 0; | |
6b79ed20 DG |
189 | |
190 | error: | |
191 | free(uctx); | |
aa3514e9 | 192 | duplicate: |
6b79ed20 DG |
193 | return ret; |
194 | } | |
195 | ||
b579acd9 DG |
196 | /* |
197 | * Add kernel context to tracer. | |
198 | */ | |
54d01ffb | 199 | int context_kernel_add(struct ltt_kernel_session *ksession, |
df4f5a87 | 200 | const struct lttng_event_context *ctx, char *channel_name) |
b579acd9 DG |
201 | { |
202 | int ret; | |
203 | struct ltt_kernel_channel *kchan; | |
645328ae | 204 | struct ltt_kernel_context *kctx; |
54d01ffb | 205 | |
0525e9ae DG |
206 | assert(ksession); |
207 | assert(ctx); | |
208 | assert(channel_name); | |
209 | ||
645328ae DG |
210 | kctx = trace_kernel_create_context(NULL); |
211 | if (!kctx) { | |
7682f304 | 212 | ret = -LTTNG_ERR_NOMEM; |
645328ae DG |
213 | goto error; |
214 | } | |
215 | ||
54d01ffb | 216 | /* Setup kernel context structure */ |
9197c5c4 MD |
217 | switch (ctx->ctx) { |
218 | case LTTNG_EVENT_CONTEXT_PID: | |
645328ae | 219 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID; |
9197c5c4 | 220 | break; |
9197c5c4 | 221 | case LTTNG_EVENT_CONTEXT_PROCNAME: |
645328ae | 222 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME; |
9197c5c4 MD |
223 | break; |
224 | case LTTNG_EVENT_CONTEXT_PRIO: | |
645328ae | 225 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO; |
9197c5c4 MD |
226 | break; |
227 | case LTTNG_EVENT_CONTEXT_NICE: | |
645328ae | 228 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NICE; |
9197c5c4 MD |
229 | break; |
230 | case LTTNG_EVENT_CONTEXT_VPID: | |
645328ae | 231 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPID; |
9197c5c4 MD |
232 | break; |
233 | case LTTNG_EVENT_CONTEXT_TID: | |
645328ae | 234 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_TID; |
9197c5c4 MD |
235 | break; |
236 | case LTTNG_EVENT_CONTEXT_VTID: | |
645328ae | 237 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VTID; |
9197c5c4 MD |
238 | break; |
239 | case LTTNG_EVENT_CONTEXT_PPID: | |
645328ae | 240 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PPID; |
9197c5c4 MD |
241 | break; |
242 | case LTTNG_EVENT_CONTEXT_VPPID: | |
645328ae | 243 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID; |
9197c5c4 | 244 | break; |
54773d68 | 245 | case LTTNG_EVENT_CONTEXT_HOSTNAME: |
645328ae | 246 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME; |
54773d68 | 247 | break; |
aa3514e9 MD |
248 | case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER: |
249 | case LTTNG_EVENT_CONTEXT_PERF_COUNTER: | |
645328ae | 250 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER; |
aa3514e9 | 251 | break; |
1ae5e83e JD |
252 | case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE: |
253 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE; | |
254 | break; | |
255 | case LTTNG_EVENT_CONTEXT_PREEMPTIBLE: | |
256 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PREEMPTIBLE; | |
257 | break; | |
258 | case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE: | |
259 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE; | |
260 | break; | |
261 | case LTTNG_EVENT_CONTEXT_MIGRATABLE: | |
262 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_MIGRATABLE; | |
263 | break; | |
373148e9 FG |
264 | case LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL: |
265 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_CALLSTACK_KERNEL; | |
266 | break; | |
267 | case LTTNG_EVENT_CONTEXT_CALLSTACK_USER: | |
268 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_CALLSTACK_USER; | |
269 | break; | |
9197c5c4 | 270 | default: |
fc7324e8 DG |
271 | ret = LTTNG_ERR_KERN_CONTEXT_FAIL; |
272 | goto error; | |
9197c5c4 MD |
273 | } |
274 | ||
645328ae DG |
275 | kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type; |
276 | kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config; | |
277 | strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name, | |
54d01ffb | 278 | LTTNG_SYMBOL_NAME_LEN); |
645328ae | 279 | kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
b579acd9 | 280 | |
9d035200 | 281 | if (*channel_name == '\0') { |
645328ae | 282 | ret = add_kctx_all_channels(ksession, kctx); |
df3c77c8 JG |
283 | /* Ownership of kctx is transferred to the callee. */ |
284 | kctx = NULL; | |
f73fabfd | 285 | if (ret != LTTNG_OK) { |
b579acd9 DG |
286 | goto error; |
287 | } | |
288 | } else { | |
289 | /* Get kernel channel */ | |
62499ad6 | 290 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
b579acd9 | 291 | if (kchan == NULL) { |
f73fabfd | 292 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
b579acd9 DG |
293 | goto error; |
294 | } | |
295 | ||
645328ae | 296 | ret = add_kctx_to_channel(kctx, kchan); |
df3c77c8 JG |
297 | /* Ownership of kctx is transferred to the callee. */ |
298 | kctx = NULL; | |
f73fabfd | 299 | if (ret != LTTNG_OK) { |
b579acd9 DG |
300 | goto error; |
301 | } | |
302 | } | |
303 | ||
df3c77c8 | 304 | ret = LTTNG_OK; |
b579acd9 DG |
305 | |
306 | error: | |
fc7324e8 DG |
307 | if (kctx) { |
308 | trace_kernel_destroy_context(kctx); | |
309 | } | |
b579acd9 DG |
310 | return ret; |
311 | } | |
2bdd86d4 MD |
312 | |
313 | /* | |
55cc08a6 | 314 | * Add UST context to tracer. |
2bdd86d4 | 315 | */ |
4ce7709b | 316 | int context_ust_add(struct ltt_ust_session *usess, |
df4f5a87 JG |
317 | enum lttng_domain_type domain, |
318 | const struct lttng_event_context *ctx, | |
4ce7709b | 319 | char *channel_name) |
2bdd86d4 | 320 | { |
601d5acf | 321 | int ret = LTTNG_OK; |
442359c0 | 322 | struct lttng_ht_iter iter; |
bec39940 | 323 | struct lttng_ht *chan_ht; |
55cc08a6 | 324 | struct ltt_ust_channel *uchan = NULL; |
2bdd86d4 | 325 | |
0525e9ae DG |
326 | assert(usess); |
327 | assert(ctx); | |
328 | assert(channel_name); | |
329 | ||
2223c96f DG |
330 | rcu_read_lock(); |
331 | ||
f5501dad | 332 | chan_ht = usess->domain_global.channels; |
2bdd86d4 | 333 | |
55cc08a6 | 334 | /* Get UST channel if defined */ |
9f9ee9c9 | 335 | if (channel_name[0] != '\0') { |
55cc08a6 DG |
336 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); |
337 | if (uchan == NULL) { | |
f73fabfd | 338 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2bdd86d4 MD |
339 | goto error; |
340 | } | |
55cc08a6 DG |
341 | } |
342 | ||
601d5acf DG |
343 | if (uchan) { |
344 | /* Add ctx to channel */ | |
6b79ed20 | 345 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
601d5acf | 346 | } else { |
e7fe706f | 347 | rcu_read_lock(); |
601d5acf | 348 | /* Add ctx all events, all channels */ |
bec39940 | 349 | cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) { |
6b79ed20 | 350 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
7682f304 | 351 | if (ret) { |
9589be74 JG |
352 | ERR("Failed to add context to channel %s", |
353 | uchan->name); | |
55cc08a6 DG |
354 | continue; |
355 | } | |
2bdd86d4 | 356 | } |
e7fe706f | 357 | rcu_read_unlock(); |
55cc08a6 | 358 | } |
2bdd86d4 | 359 | |
55cc08a6 | 360 | switch (ret) { |
7682f304 | 361 | case LTTNG_ERR_UST_CONTEXT_EXIST: |
727d5404 | 362 | break; |
55cc08a6 | 363 | case -ENOMEM: |
7682f304 | 364 | case -LTTNG_ERR_NOMEM: |
f73fabfd | 365 | ret = LTTNG_ERR_FATAL; |
727d5404 DG |
366 | break; |
367 | case -EINVAL: | |
f73fabfd | 368 | ret = LTTNG_ERR_UST_CONTEXT_INVAL; |
727d5404 DG |
369 | break; |
370 | case -ENOSYS: | |
f73fabfd | 371 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
727d5404 DG |
372 | break; |
373 | default: | |
7682f304 JG |
374 | if (ret != 0 && ret != LTTNG_OK) { |
375 | ret = ret > 0 ? ret : LTTNG_ERR_UNK; | |
376 | } else { | |
377 | ret = LTTNG_OK; | |
378 | } | |
727d5404 | 379 | break; |
2bdd86d4 MD |
380 | } |
381 | ||
2bdd86d4 | 382 | error: |
2223c96f | 383 | rcu_read_unlock(); |
2bdd86d4 MD |
384 | return ret; |
385 | } |