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