74afd4e0cad5ff87d307de751672ad1ae86bad61
[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 lttng_kernel_context *kctx)
38 {
39 int ret;
40 struct ltt_kernel_channel *kchan;
41
42 DBG("Adding kernel context to all channels");
43
44 /* Go over all channels */
45 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
46 ret = kernel_add_channel_context(kchan, kctx);
47 if (ret < 0) {
48 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
49 goto error;
50 }
51 }
52
53 ret = LTTNG_OK;
54
55 error:
56 return ret;
57 }
58
59 /*
60 * Add kernel context to a specific channel.
61 */
62 static int add_kctx_to_channel(struct lttng_kernel_context *kctx,
63 struct ltt_kernel_channel *kchan)
64 {
65 int ret;
66
67 DBG("Add kernel context to channel '%s'", kchan->channel->name);
68
69 ret = kernel_add_channel_context(kchan, kctx);
70 if (ret < 0) {
71 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
72 goto error;
73 }
74
75 ret = LTTNG_OK;
76
77 error:
78 return ret;
79 }
80
81 /*
82 * Add UST context to channel.
83 */
84 static int add_uctx_to_channel(struct ltt_ust_session *usess, int domain,
85 struct ltt_ust_channel *uchan, struct lttng_event_context *ctx)
86 {
87 int ret;
88 struct ltt_ust_context *uctx;
89 struct lttng_ht_iter iter;
90 struct lttng_ht_node_ulong *uctx_node;
91
92 /* Create ltt UST context */
93 uctx = trace_ust_create_context(ctx);
94 if (uctx == NULL) {
95 ret = -EINVAL;
96 goto error;
97 }
98
99 switch (domain) {
100 case LTTNG_DOMAIN_UST:
101 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
102 if (ret < 0) {
103 goto error;
104 }
105 break;
106 default:
107 ret = -ENOSYS;
108 goto error;
109 }
110
111 /* Lookup context before adding it */
112 lttng_ht_lookup(uchan->ctx, (void *)((unsigned long)uctx->ctx.ctx), &iter);
113 uctx_node = lttng_ht_iter_get_node_ulong(&iter);
114 if (uctx_node != NULL) {
115 ret = -EEXIST;
116 goto error;
117 }
118
119 /* Add ltt UST context node to ltt UST channel */
120 lttng_ht_add_unique_ulong(uchan->ctx, &uctx->node);
121
122 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
123
124 return 0;
125
126 error:
127 free(uctx);
128 return ret;
129 }
130
131 /*
132 * Add kernel context to tracer.
133 */
134 int context_kernel_add(struct ltt_kernel_session *ksession,
135 struct lttng_event_context *ctx, char *channel_name)
136 {
137 int ret;
138 struct ltt_kernel_channel *kchan;
139 struct lttng_kernel_context kctx;
140
141 /* Setup kernel context structure */
142 switch (ctx->ctx) {
143 case LTTNG_EVENT_CONTEXT_PID:
144 kctx.ctx = LTTNG_KERNEL_CONTEXT_PID;
145 break;
146 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
147 kctx.ctx = LTTNG_KERNEL_CONTEXT_PERF_COUNTER;
148 break;
149 case LTTNG_EVENT_CONTEXT_PROCNAME:
150 kctx.ctx = LTTNG_KERNEL_CONTEXT_PROCNAME;
151 break;
152 case LTTNG_EVENT_CONTEXT_PRIO:
153 kctx.ctx = LTTNG_KERNEL_CONTEXT_PRIO;
154 break;
155 case LTTNG_EVENT_CONTEXT_NICE:
156 kctx.ctx = LTTNG_KERNEL_CONTEXT_NICE;
157 break;
158 case LTTNG_EVENT_CONTEXT_VPID:
159 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPID;
160 break;
161 case LTTNG_EVENT_CONTEXT_TID:
162 kctx.ctx = LTTNG_KERNEL_CONTEXT_TID;
163 break;
164 case LTTNG_EVENT_CONTEXT_VTID:
165 kctx.ctx = LTTNG_KERNEL_CONTEXT_VTID;
166 break;
167 case LTTNG_EVENT_CONTEXT_PPID:
168 kctx.ctx = LTTNG_KERNEL_CONTEXT_PPID;
169 break;
170 case LTTNG_EVENT_CONTEXT_VPPID:
171 kctx.ctx = LTTNG_KERNEL_CONTEXT_VPPID;
172 break;
173 case LTTNG_EVENT_CONTEXT_HOSTNAME:
174 kctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
175 break;
176 default:
177 return LTTNG_ERR_KERN_CONTEXT_FAIL;
178 }
179
180 kctx.u.perf_counter.type = ctx->u.perf_counter.type;
181 kctx.u.perf_counter.config = ctx->u.perf_counter.config;
182 strncpy(kctx.u.perf_counter.name, ctx->u.perf_counter.name,
183 LTTNG_SYMBOL_NAME_LEN);
184 kctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
185
186 if (strlen(channel_name) == 0) {
187 ret = add_kctx_all_channels(ksession, &kctx);
188 if (ret != LTTNG_OK) {
189 goto error;
190 }
191 } else {
192 /* Get kernel channel */
193 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
194 if (kchan == NULL) {
195 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
196 goto error;
197 }
198
199 ret = add_kctx_to_channel(&kctx, kchan);
200 if (ret != LTTNG_OK) {
201 goto error;
202 }
203 }
204
205 ret = LTTNG_OK;
206
207 error:
208 return ret;
209 }
210
211 /*
212 * Add UST context to tracer.
213 */
214 int context_ust_add(struct ltt_ust_session *usess, int domain,
215 struct lttng_event_context *ctx, char *channel_name)
216 {
217 int ret = LTTNG_OK;
218 struct lttng_ht_iter iter;
219 struct lttng_ht *chan_ht;
220 struct ltt_ust_channel *uchan = NULL;
221
222 /*
223 * Define which channel's hashtable to use from the domain or quit if
224 * unknown domain.
225 */
226 switch (domain) {
227 case LTTNG_DOMAIN_UST:
228 chan_ht = usess->domain_global.channels;
229 break;
230 #if 0
231 case LTTNG_DOMAIN_UST_EXEC_NAME:
232 case LTTNG_DOMAIN_UST_PID:
233 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
234 #endif
235 default:
236 ret = LTTNG_ERR_UND;
237 goto error;
238 }
239
240 /* Get UST channel if defined */
241 if (strlen(channel_name) != 0) {
242 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
243 if (uchan == NULL) {
244 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
245 goto error;
246 }
247 }
248
249 if (uchan) {
250 /* Add ctx to channel */
251 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
252 } else {
253 /* Add ctx all events, all channels */
254 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
255 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
256 if (ret < 0) {
257 ERR("Context failed for channel %s", uchan->name);
258 continue;
259 }
260 }
261 }
262
263 switch (ret) {
264 case -EEXIST:
265 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
266 break;
267 case -ENOMEM:
268 ret = LTTNG_ERR_FATAL;
269 break;
270 case -EINVAL:
271 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
272 break;
273 case -ENOSYS:
274 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
275 break;
276 default:
277 ret = LTTNG_OK;
278 break;
279 }
280
281 error:
282 return ret;
283 }
This page took 0.033513 seconds and 3 git commands to generate.