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