Commit | Line | Data |
---|---|---|
b579acd9 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
b579acd9 | 7 | * |
d14d33bf AM |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
b579acd9 | 12 | * |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
b579acd9 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
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" |
b579acd9 | 33 | |
b579acd9 DG |
34 | /* |
35 | * Add kernel context to all channel. | |
b579acd9 DG |
36 | */ |
37 | static int add_kctx_all_channels(struct ltt_kernel_session *ksession, | |
645328ae | 38 | struct ltt_kernel_context *kctx) |
b579acd9 | 39 | { |
601d5acf | 40 | int ret; |
b579acd9 DG |
41 | struct ltt_kernel_channel *kchan; |
42 | ||
0525e9ae DG |
43 | assert(ksession); |
44 | assert(kctx); | |
45 | ||
601d5acf | 46 | DBG("Adding kernel context to all channels"); |
b579acd9 DG |
47 | |
48 | /* Go over all channels */ | |
49 | cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) { | |
601d5acf DG |
50 | ret = kernel_add_channel_context(kchan, kctx); |
51 | if (ret < 0) { | |
52 | ret = LTTNG_ERR_KERN_CONTEXT_FAIL; | |
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 DG |
76 | ret = kernel_add_channel_context(kchan, kctx); |
77 | if (ret < 0) { | |
78 | ret = LTTNG_ERR_KERN_CONTEXT_FAIL; | |
b579acd9 DG |
79 | goto error; |
80 | } | |
81 | ||
f73fabfd | 82 | ret = LTTNG_OK; |
b579acd9 DG |
83 | |
84 | error: | |
85 | return ret; | |
86 | } | |
87 | ||
6b79ed20 DG |
88 | /* |
89 | * Add UST context to channel. | |
90 | */ | |
91 | static int add_uctx_to_channel(struct ltt_ust_session *usess, int domain, | |
92 | struct ltt_ust_channel *uchan, struct lttng_event_context *ctx) | |
93 | { | |
94 | int ret; | |
95 | struct ltt_ust_context *uctx; | |
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 | } | |
108 | ||
6b79ed20 DG |
109 | /* Create ltt UST context */ |
110 | uctx = trace_ust_create_context(ctx); | |
111 | if (uctx == NULL) { | |
727d5404 | 112 | ret = -EINVAL; |
6b79ed20 DG |
113 | goto error; |
114 | } | |
115 | ||
116 | switch (domain) { | |
117 | case LTTNG_DOMAIN_UST: | |
118 | ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx); | |
119 | if (ret < 0) { | |
120 | goto error; | |
121 | } | |
122 | break; | |
123 | default: | |
727d5404 DG |
124 | ret = -ENOSYS; |
125 | goto error; | |
126 | } | |
127 | ||
e7fe706f DG |
128 | rcu_read_lock(); |
129 | ||
6b79ed20 | 130 | /* Add ltt UST context node to ltt UST channel */ |
aa3514e9 | 131 | lttng_ht_add_ulong(uchan->ctx, &uctx->node); |
e7fe706f | 132 | rcu_read_unlock(); |
31746f93 | 133 | cds_list_add_tail(&uctx->list, &uchan->ctx_list); |
6b79ed20 | 134 | |
727d5404 DG |
135 | DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name); |
136 | ||
137 | return 0; | |
6b79ed20 DG |
138 | |
139 | error: | |
140 | free(uctx); | |
aa3514e9 | 141 | duplicate: |
6b79ed20 DG |
142 | return ret; |
143 | } | |
144 | ||
b579acd9 DG |
145 | /* |
146 | * Add kernel context to tracer. | |
147 | */ | |
54d01ffb | 148 | int context_kernel_add(struct ltt_kernel_session *ksession, |
601d5acf | 149 | struct lttng_event_context *ctx, char *channel_name) |
b579acd9 DG |
150 | { |
151 | int ret; | |
152 | struct ltt_kernel_channel *kchan; | |
645328ae | 153 | struct ltt_kernel_context *kctx; |
54d01ffb | 154 | |
0525e9ae DG |
155 | assert(ksession); |
156 | assert(ctx); | |
157 | assert(channel_name); | |
158 | ||
645328ae DG |
159 | kctx = trace_kernel_create_context(NULL); |
160 | if (!kctx) { | |
161 | ret = LTTNG_ERR_NOMEM; | |
162 | goto error; | |
163 | } | |
164 | ||
54d01ffb | 165 | /* Setup kernel context structure */ |
9197c5c4 MD |
166 | switch (ctx->ctx) { |
167 | case LTTNG_EVENT_CONTEXT_PID: | |
645328ae | 168 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID; |
9197c5c4 | 169 | break; |
9197c5c4 | 170 | case LTTNG_EVENT_CONTEXT_PROCNAME: |
645328ae | 171 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME; |
9197c5c4 MD |
172 | break; |
173 | case LTTNG_EVENT_CONTEXT_PRIO: | |
645328ae | 174 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO; |
9197c5c4 MD |
175 | break; |
176 | case LTTNG_EVENT_CONTEXT_NICE: | |
645328ae | 177 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NICE; |
9197c5c4 MD |
178 | break; |
179 | case LTTNG_EVENT_CONTEXT_VPID: | |
645328ae | 180 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPID; |
9197c5c4 MD |
181 | break; |
182 | case LTTNG_EVENT_CONTEXT_TID: | |
645328ae | 183 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_TID; |
9197c5c4 MD |
184 | break; |
185 | case LTTNG_EVENT_CONTEXT_VTID: | |
645328ae | 186 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VTID; |
9197c5c4 MD |
187 | break; |
188 | case LTTNG_EVENT_CONTEXT_PPID: | |
645328ae | 189 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PPID; |
9197c5c4 MD |
190 | break; |
191 | case LTTNG_EVENT_CONTEXT_VPPID: | |
645328ae | 192 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID; |
9197c5c4 | 193 | break; |
54773d68 | 194 | case LTTNG_EVENT_CONTEXT_HOSTNAME: |
645328ae | 195 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME; |
54773d68 | 196 | break; |
aa3514e9 MD |
197 | case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER: |
198 | case LTTNG_EVENT_CONTEXT_PERF_COUNTER: | |
645328ae | 199 | kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER; |
aa3514e9 | 200 | break; |
9197c5c4 | 201 | default: |
fc7324e8 DG |
202 | ret = LTTNG_ERR_KERN_CONTEXT_FAIL; |
203 | goto error; | |
9197c5c4 MD |
204 | } |
205 | ||
645328ae DG |
206 | kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type; |
207 | kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config; | |
208 | strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name, | |
54d01ffb | 209 | LTTNG_SYMBOL_NAME_LEN); |
645328ae | 210 | kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; |
b579acd9 | 211 | |
9d035200 | 212 | if (*channel_name == '\0') { |
645328ae | 213 | ret = add_kctx_all_channels(ksession, kctx); |
f73fabfd | 214 | if (ret != LTTNG_OK) { |
b579acd9 DG |
215 | goto error; |
216 | } | |
217 | } else { | |
218 | /* Get kernel channel */ | |
62499ad6 | 219 | kchan = trace_kernel_get_channel_by_name(channel_name, ksession); |
b579acd9 | 220 | if (kchan == NULL) { |
f73fabfd | 221 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
b579acd9 DG |
222 | goto error; |
223 | } | |
224 | ||
645328ae | 225 | ret = add_kctx_to_channel(kctx, kchan); |
f73fabfd | 226 | if (ret != LTTNG_OK) { |
b579acd9 DG |
227 | goto error; |
228 | } | |
229 | } | |
230 | ||
fc7324e8 | 231 | return LTTNG_OK; |
b579acd9 DG |
232 | |
233 | error: | |
fc7324e8 DG |
234 | if (kctx) { |
235 | trace_kernel_destroy_context(kctx); | |
236 | } | |
b579acd9 DG |
237 | return ret; |
238 | } | |
2bdd86d4 MD |
239 | |
240 | /* | |
55cc08a6 | 241 | * Add UST context to tracer. |
2bdd86d4 | 242 | */ |
55cc08a6 | 243 | int context_ust_add(struct ltt_ust_session *usess, int domain, |
601d5acf | 244 | struct lttng_event_context *ctx, char *channel_name) |
2bdd86d4 | 245 | { |
601d5acf | 246 | int ret = LTTNG_OK; |
442359c0 | 247 | struct lttng_ht_iter iter; |
bec39940 | 248 | struct lttng_ht *chan_ht; |
55cc08a6 | 249 | struct ltt_ust_channel *uchan = NULL; |
2bdd86d4 | 250 | |
0525e9ae DG |
251 | assert(usess); |
252 | assert(ctx); | |
253 | assert(channel_name); | |
254 | ||
2223c96f DG |
255 | rcu_read_lock(); |
256 | ||
6b79ed20 DG |
257 | /* |
258 | * Define which channel's hashtable to use from the domain or quit if | |
259 | * unknown domain. | |
260 | */ | |
55cc08a6 DG |
261 | switch (domain) { |
262 | case LTTNG_DOMAIN_UST: | |
263 | chan_ht = usess->domain_global.channels; | |
264 | break; | |
55cc08a6 | 265 | default: |
f73fabfd | 266 | ret = LTTNG_ERR_UND; |
2bdd86d4 MD |
267 | goto error; |
268 | } | |
2bdd86d4 | 269 | |
55cc08a6 | 270 | /* Get UST channel if defined */ |
9f9ee9c9 | 271 | if (channel_name[0] != '\0') { |
55cc08a6 DG |
272 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); |
273 | if (uchan == NULL) { | |
f73fabfd | 274 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2bdd86d4 MD |
275 | goto error; |
276 | } | |
55cc08a6 DG |
277 | } |
278 | ||
601d5acf DG |
279 | if (uchan) { |
280 | /* Add ctx to channel */ | |
6b79ed20 | 281 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
601d5acf | 282 | } else { |
e7fe706f | 283 | rcu_read_lock(); |
601d5acf | 284 | /* Add ctx all events, all channels */ |
bec39940 | 285 | cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) { |
6b79ed20 | 286 | ret = add_uctx_to_channel(usess, domain, uchan, ctx); |
55cc08a6 | 287 | if (ret < 0) { |
ae856491 | 288 | ERR("Context failed for channel %s", uchan->name); |
55cc08a6 DG |
289 | continue; |
290 | } | |
2bdd86d4 | 291 | } |
e7fe706f | 292 | rcu_read_unlock(); |
55cc08a6 | 293 | } |
2bdd86d4 | 294 | |
55cc08a6 DG |
295 | switch (ret) { |
296 | case -EEXIST: | |
f73fabfd | 297 | ret = LTTNG_ERR_UST_CONTEXT_EXIST; |
727d5404 | 298 | break; |
55cc08a6 | 299 | case -ENOMEM: |
f73fabfd | 300 | ret = LTTNG_ERR_FATAL; |
727d5404 DG |
301 | break; |
302 | case -EINVAL: | |
f73fabfd | 303 | ret = LTTNG_ERR_UST_CONTEXT_INVAL; |
727d5404 DG |
304 | break; |
305 | case -ENOSYS: | |
f73fabfd | 306 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
727d5404 DG |
307 | break; |
308 | default: | |
f73fabfd | 309 | ret = LTTNG_OK; |
727d5404 | 310 | break; |
2bdd86d4 MD |
311 | } |
312 | ||
2bdd86d4 | 313 | error: |
2223c96f | 314 | rcu_read_unlock(); |
2bdd86d4 MD |
315 | return ret; |
316 | } |