Fix: memory leak in add relayd socket error path
[lttng-tools.git] / src / bin / lttng-sessiond / context.c
CommitLineData
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
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
54d01ffb 23#include <urcu/list.h>
1e307fab 24
db758600 25#include <common/error.h>
10a8a223 26#include <common/sessiond-comm/sessiond-comm.h>
b579acd9 27
b579acd9 28#include "context.h"
4771f025 29#include "kernel.h"
55cc08a6 30#include "ust-app.h"
34378f76 31#include "trace-ust.h"
b579acd9 32
b579acd9
DG
33/*
34 * Add kernel context to all channel.
b579acd9
DG
35 */
36static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
601d5acf 37 struct lttng_kernel_context *kctx)
b579acd9 38{
601d5acf 39 int ret;
b579acd9
DG
40 struct ltt_kernel_channel *kchan;
41
601d5acf 42 DBG("Adding kernel context to all channels");
b579acd9
DG
43
44 /* Go over all channels */
45 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
601d5acf
DG
46 ret = kernel_add_channel_context(kchan, kctx);
47 if (ret < 0) {
48 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
49 goto error;
b579acd9
DG
50 }
51 }
52
f73fabfd 53 ret = LTTNG_OK;
b579acd9
DG
54
55error:
56 return ret;
57}
58
59/*
60 * Add kernel context to a specific channel.
b579acd9
DG
61 */
62static int add_kctx_to_channel(struct lttng_kernel_context *kctx,
601d5acf 63 struct ltt_kernel_channel *kchan)
b579acd9 64{
601d5acf 65 int ret;
b579acd9 66
601d5acf 67 DBG("Add kernel context to channel '%s'", kchan->channel->name);
b579acd9 68
601d5acf
DG
69 ret = kernel_add_channel_context(kchan, kctx);
70 if (ret < 0) {
71 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
b579acd9
DG
72 goto error;
73 }
74
f73fabfd 75 ret = LTTNG_OK;
b579acd9
DG
76
77error:
78 return ret;
79}
80
6b79ed20
DG
81/*
82 * Add UST context to channel.
83 */
84static 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;
727d5404
DG
89 struct lttng_ht_iter iter;
90 struct lttng_ht_node_ulong *uctx_node;
6b79ed20
DG
91
92 /* Create ltt UST context */
93 uctx = trace_ust_create_context(ctx);
94 if (uctx == NULL) {
727d5404 95 ret = -EINVAL;
6b79ed20
DG
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:
727d5404
DG
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;
6b79ed20
DG
116 goto error;
117 }
118
119 /* Add ltt UST context node to ltt UST channel */
bec39940 120 lttng_ht_add_unique_ulong(uchan->ctx, &uctx->node);
6b79ed20 121
727d5404
DG
122 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
123
124 return 0;
6b79ed20
DG
125
126error:
127 free(uctx);
128 return ret;
129}
130
b579acd9
DG
131/*
132 * Add kernel context to tracer.
133 */
54d01ffb 134int context_kernel_add(struct ltt_kernel_session *ksession,
601d5acf 135 struct lttng_event_context *ctx, char *channel_name)
b579acd9
DG
136{
137 int ret;
138 struct ltt_kernel_channel *kchan;
54d01ffb
DG
139 struct lttng_kernel_context kctx;
140
141 /* Setup kernel context structure */
9197c5c4
MD
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;
54773d68
JD
173 case LTTNG_EVENT_CONTEXT_HOSTNAME:
174 kctx.ctx = LTTNG_KERNEL_CONTEXT_HOSTNAME;
175 break;
9197c5c4 176 default:
f73fabfd 177 return LTTNG_ERR_KERN_CONTEXT_FAIL;
9197c5c4
MD
178 }
179
54d01ffb
DG
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';
b579acd9
DG
185
186 if (strlen(channel_name) == 0) {
601d5acf 187 ret = add_kctx_all_channels(ksession, &kctx);
f73fabfd 188 if (ret != LTTNG_OK) {
b579acd9
DG
189 goto error;
190 }
191 } else {
192 /* Get kernel channel */
62499ad6 193 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
b579acd9 194 if (kchan == NULL) {
f73fabfd 195 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
b579acd9
DG
196 goto error;
197 }
198
601d5acf 199 ret = add_kctx_to_channel(&kctx, kchan);
f73fabfd 200 if (ret != LTTNG_OK) {
b579acd9
DG
201 goto error;
202 }
203 }
204
f73fabfd 205 ret = LTTNG_OK;
b579acd9
DG
206
207error:
208 return ret;
209}
2bdd86d4
MD
210
211/*
55cc08a6 212 * Add UST context to tracer.
2bdd86d4 213 */
55cc08a6 214int context_ust_add(struct ltt_ust_session *usess, int domain,
601d5acf 215 struct lttng_event_context *ctx, char *channel_name)
2bdd86d4 216{
601d5acf 217 int ret = LTTNG_OK;
442359c0 218 struct lttng_ht_iter iter;
bec39940 219 struct lttng_ht *chan_ht;
55cc08a6 220 struct ltt_ust_channel *uchan = NULL;
2bdd86d4 221
6b79ed20
DG
222 /*
223 * Define which channel's hashtable to use from the domain or quit if
224 * unknown domain.
225 */
55cc08a6
DG
226 switch (domain) {
227 case LTTNG_DOMAIN_UST:
228 chan_ht = usess->domain_global.channels;
229 break;
d78d6610 230#if 0
55cc08a6
DG
231 case LTTNG_DOMAIN_UST_EXEC_NAME:
232 case LTTNG_DOMAIN_UST_PID:
233 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
d78d6610 234#endif
55cc08a6 235 default:
f73fabfd 236 ret = LTTNG_ERR_UND;
2bdd86d4
MD
237 goto error;
238 }
2bdd86d4 239
55cc08a6
DG
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) {
f73fabfd 244 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2bdd86d4
MD
245 goto error;
246 }
55cc08a6
DG
247 }
248
601d5acf
DG
249 if (uchan) {
250 /* Add ctx to channel */
6b79ed20 251 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
601d5acf
DG
252 } else {
253 /* Add ctx all events, all channels */
bec39940 254 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
6b79ed20 255 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
55cc08a6 256 if (ret < 0) {
ae856491 257 ERR("Context failed for channel %s", uchan->name);
55cc08a6
DG
258 continue;
259 }
2bdd86d4 260 }
55cc08a6 261 }
2bdd86d4 262
55cc08a6
DG
263 switch (ret) {
264 case -EEXIST:
f73fabfd 265 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
727d5404 266 break;
55cc08a6 267 case -ENOMEM:
f73fabfd 268 ret = LTTNG_ERR_FATAL;
727d5404
DG
269 break;
270 case -EINVAL:
f73fabfd 271 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
727d5404
DG
272 break;
273 case -ENOSYS:
f73fabfd 274 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
727d5404
DG
275 break;
276 default:
f73fabfd 277 ret = LTTNG_OK;
727d5404 278 break;
2bdd86d4
MD
279 }
280
2bdd86d4
MD
281error:
282 return ret;
283}
This page took 0.043164 seconds and 4 git commands to generate.