Fix: Clamp verbosity value read from config file
[lttng-tools.git] / src / bin / lttng-sessiond / context.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <urcu/list.h>
24
25 #include <common/error.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27
28 #include "context.h"
29 #include "kernel.h"
30 #include "ust-app.h"
31 #include "trace-ust.h"
32
33 /*
34 * Add kernel context to all channel.
35 */
36 static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
37 struct ltt_kernel_context *kctx)
38 {
39 int ret;
40 struct ltt_kernel_channel *kchan;
41
42 assert(ksession);
43 assert(kctx);
44
45 DBG("Adding kernel context to all channels");
46
47 /* Go over all channels */
48 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
49 ret = kernel_add_channel_context(kchan, kctx);
50 if (ret < 0) {
51 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
52 goto error;
53 }
54 }
55
56 ret = LTTNG_OK;
57
58 error:
59 return ret;
60 }
61
62 /*
63 * Add kernel context to a specific channel.
64 */
65 static int add_kctx_to_channel(struct ltt_kernel_context *kctx,
66 struct ltt_kernel_channel *kchan)
67 {
68 int ret;
69
70 assert(kchan);
71 assert(kctx);
72
73 DBG("Add kernel context to channel '%s'", kchan->channel->name);
74
75 ret = kernel_add_channel_context(kchan, kctx);
76 if (ret < 0) {
77 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
78 goto error;
79 }
80
81 ret = LTTNG_OK;
82
83 error:
84 return ret;
85 }
86
87 /*
88 * Add UST context to channel.
89 */
90 static int add_uctx_to_channel(struct ltt_ust_session *usess, int domain,
91 struct ltt_ust_channel *uchan, struct lttng_event_context *ctx)
92 {
93 int ret;
94 struct ltt_ust_context *uctx;
95
96 assert(usess);
97 assert(uchan);
98 assert(ctx);
99
100 /* Check if context is duplicate */
101 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
102 if (trace_ust_match_context(uctx, ctx)) {
103 ret = -EEXIST;
104 goto duplicate;
105 }
106 }
107
108 /* Create ltt UST context */
109 uctx = trace_ust_create_context(ctx);
110 if (uctx == NULL) {
111 ret = -EINVAL;
112 goto error;
113 }
114
115 switch (domain) {
116 case LTTNG_DOMAIN_UST:
117 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
118 if (ret < 0) {
119 goto error;
120 }
121 break;
122 default:
123 ret = -ENOSYS;
124 goto error;
125 }
126
127 rcu_read_lock();
128
129 /* Add ltt UST context node to ltt UST channel */
130 lttng_ht_add_ulong(uchan->ctx, &uctx->node);
131 rcu_read_unlock();
132 cds_list_add_tail(&uctx->list, &uchan->ctx_list);
133
134 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
135
136 return 0;
137
138 error:
139 free(uctx);
140 duplicate:
141 return ret;
142 }
143
144 /*
145 * Add kernel context to tracer.
146 */
147 int context_kernel_add(struct ltt_kernel_session *ksession,
148 struct lttng_event_context *ctx, char *channel_name)
149 {
150 int ret;
151 struct ltt_kernel_channel *kchan;
152 struct ltt_kernel_context *kctx;
153
154 assert(ksession);
155 assert(ctx);
156 assert(channel_name);
157
158 kctx = trace_kernel_create_context(NULL);
159 if (!kctx) {
160 ret = LTTNG_ERR_NOMEM;
161 goto error;
162 }
163
164 /* Setup kernel context structure */
165 switch (ctx->ctx) {
166 case LTTNG_EVENT_CONTEXT_PID:
167 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
168 break;
169 case LTTNG_EVENT_CONTEXT_PROCNAME:
170 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
171 break;
172 case LTTNG_EVENT_CONTEXT_PRIO:
173 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
174 break;
175 case LTTNG_EVENT_CONTEXT_NICE:
176 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
177 break;
178 case LTTNG_EVENT_CONTEXT_VPID:
179 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
180 break;
181 case LTTNG_EVENT_CONTEXT_TID:
182 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
183 break;
184 case LTTNG_EVENT_CONTEXT_VTID:
185 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
186 break;
187 case LTTNG_EVENT_CONTEXT_PPID:
188 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
189 break;
190 case LTTNG_EVENT_CONTEXT_VPPID:
191 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
192 break;
193 case LTTNG_EVENT_CONTEXT_HOSTNAME:
194 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
195 break;
196 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
197 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
198 kctx->ctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER;
199 break;
200 default:
201 return LTTNG_ERR_KERN_CONTEXT_FAIL;
202 }
203
204 kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
205 kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
206 strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
207 LTTNG_SYMBOL_NAME_LEN);
208 kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
209
210 if (*channel_name == '\0') {
211 ret = add_kctx_all_channels(ksession, kctx);
212 if (ret != LTTNG_OK) {
213 goto error;
214 }
215 } else {
216 /* Get kernel channel */
217 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
218 if (kchan == NULL) {
219 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
220 goto error;
221 }
222
223 ret = add_kctx_to_channel(kctx, kchan);
224 if (ret != LTTNG_OK) {
225 goto error;
226 }
227 }
228
229 ret = LTTNG_OK;
230
231 error:
232 return ret;
233 }
234
235 /*
236 * Add UST context to tracer.
237 */
238 int context_ust_add(struct ltt_ust_session *usess, int domain,
239 struct lttng_event_context *ctx, char *channel_name)
240 {
241 int ret = LTTNG_OK;
242 struct lttng_ht_iter iter;
243 struct lttng_ht *chan_ht;
244 struct ltt_ust_channel *uchan = NULL;
245
246 assert(usess);
247 assert(ctx);
248 assert(channel_name);
249
250 rcu_read_lock();
251
252 /*
253 * Define which channel's hashtable to use from the domain or quit if
254 * unknown domain.
255 */
256 switch (domain) {
257 case LTTNG_DOMAIN_UST:
258 chan_ht = usess->domain_global.channels;
259 break;
260 #if 0
261 case LTTNG_DOMAIN_UST_EXEC_NAME:
262 case LTTNG_DOMAIN_UST_PID:
263 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
264 #endif
265 default:
266 ret = LTTNG_ERR_UND;
267 goto error;
268 }
269
270 /* Get UST channel if defined */
271 if (channel_name[0] != '\0') {
272 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
273 if (uchan == NULL) {
274 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
275 goto error;
276 }
277 }
278
279 if (uchan) {
280 /* Add ctx to channel */
281 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
282 } else {
283 rcu_read_lock();
284 /* Add ctx all events, all channels */
285 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
286 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
287 if (ret < 0) {
288 ERR("Context failed for channel %s", uchan->name);
289 continue;
290 }
291 }
292 rcu_read_unlock();
293 }
294
295 switch (ret) {
296 case -EEXIST:
297 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
298 break;
299 case -ENOMEM:
300 ret = LTTNG_ERR_FATAL;
301 break;
302 case -EINVAL:
303 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
304 break;
305 case -ENOSYS:
306 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
307 break;
308 default:
309 ret = LTTNG_OK;
310 break;
311 }
312
313 error:
314 rcu_read_unlock();
315 return ret;
316 }
This page took 0.038728 seconds and 4 git commands to generate.