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