sessiond: Replace uses of session_trylock_list by a dedicated assert macro
[lttng-tools.git] / src / bin / lttng-sessiond / context.cpp
CommitLineData
b579acd9 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
ab5be9fa 3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
b579acd9 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
b579acd9 6 *
b579acd9
DG
7 */
8
6c1c0768 9#define _LGPL_SOURCE
b579acd9
DG
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
54d01ffb 14#include <urcu/list.h>
1e307fab 15
c9e313bc
SM
16#include <common/error.hpp>
17#include <common/sessiond-comm/sessiond-comm.hpp>
b579acd9 18
c9e313bc
SM
19#include "context.hpp"
20#include "kernel.hpp"
21#include "ust-app.hpp"
22#include "trace-ust.hpp"
23#include "agent.hpp"
b579acd9 24
b579acd9
DG
25/*
26 * Add kernel context to all channel.
df3c77c8
JG
27 *
28 * Assumes the ownership of kctx.
b579acd9
DG
29 */
30static int add_kctx_all_channels(struct ltt_kernel_session *ksession,
645328ae 31 struct ltt_kernel_context *kctx)
b579acd9 32{
601d5acf 33 int ret;
b579acd9
DG
34 struct ltt_kernel_channel *kchan;
35
a0377dfe
FD
36 LTTNG_ASSERT(ksession);
37 LTTNG_ASSERT(kctx);
0525e9ae 38
601d5acf 39 DBG("Adding kernel context to all channels");
b579acd9
DG
40
41 /* Go over all channels */
42 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
df3c77c8
JG
43 struct ltt_kernel_context *kctx_copy;
44
45 kctx_copy = trace_kernel_copy_context(kctx);
46 if (!kctx_copy) {
47 PERROR("zmalloc ltt_kernel_context");
7682f304 48 ret = -LTTNG_ERR_NOMEM;
df3c77c8
JG
49 goto error;
50 }
51
52 /* Ownership of kctx_copy is transferred to the callee. */
53 ret = kernel_add_channel_context(kchan, kctx_copy);
54 kctx_copy = NULL;
1ae5e83e 55 if (ret != 0) {
601d5acf 56 goto error;
b579acd9
DG
57 }
58 }
59
f73fabfd 60 ret = LTTNG_OK;
b579acd9
DG
61
62error:
df3c77c8 63 trace_kernel_destroy_context(kctx);
b579acd9
DG
64 return ret;
65}
66
67/*
68 * Add kernel context to a specific channel.
df3c77c8
JG
69 *
70 * Assumes the ownership of kctx.
b579acd9 71 */
645328ae 72static int add_kctx_to_channel(struct ltt_kernel_context *kctx,
601d5acf 73 struct ltt_kernel_channel *kchan)
b579acd9 74{
601d5acf 75 int ret;
b579acd9 76
a0377dfe
FD
77 LTTNG_ASSERT(kchan);
78 LTTNG_ASSERT(kctx);
0525e9ae 79
601d5acf 80 DBG("Add kernel context to channel '%s'", kchan->channel->name);
b579acd9 81
df3c77c8 82 /* Ownership of kctx is transferred to the callee. */
601d5acf 83 ret = kernel_add_channel_context(kchan, kctx);
df3c77c8 84 kctx = NULL;
1ae5e83e 85 if (ret != 0) {
b579acd9
DG
86 goto error;
87 }
88
f73fabfd 89 ret = LTTNG_OK;
b579acd9
DG
90
91error:
92 return ret;
93}
94
6b79ed20
DG
95/*
96 * Add UST context to channel.
97 */
4ce7709b
JG
98static int add_uctx_to_channel(struct ltt_ust_session *usess,
99 enum lttng_domain_type domain,
df4f5a87
JG
100 struct ltt_ust_channel *uchan,
101 const struct lttng_event_context *ctx)
6b79ed20
DG
102{
103 int ret;
bdf64013 104 struct ltt_ust_context *uctx = NULL;
6b79ed20 105
a0377dfe
FD
106 LTTNG_ASSERT(usess);
107 LTTNG_ASSERT(uchan);
108 LTTNG_ASSERT(ctx);
0525e9ae 109
aa3514e9
MD
110 /* Check if context is duplicate */
111 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
112 if (trace_ust_match_context(uctx, ctx)) {
7682f304 113 ret = LTTNG_ERR_UST_CONTEXT_EXIST;
aa3514e9
MD
114 goto duplicate;
115 }
116 }
bdf64013
JG
117 uctx = NULL;
118
119 switch (domain) {
120 case LTTNG_DOMAIN_JUL:
121 case LTTNG_DOMAIN_LOG4J:
122 {
442f8c07
JG
123 struct agent *agt;
124
125 if (ctx->ctx != LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
126 /* Other contexts are not needed by the agent. */
127 break;
128 }
129 agt = trace_ust_find_agent(usess, domain);
bdf64013
JG
130
131 if (!agt) {
132 agt = agent_create(domain);
133 if (!agt) {
7682f304 134 ret = -LTTNG_ERR_NOMEM;
bdf64013
JG
135 goto error;
136 }
137 agent_add(agt, usess->agents);
138 }
139 ret = agent_add_context(ctx, agt);
140 if (ret != LTTNG_OK) {
141 goto error;
142 }
143
144 ret = agent_enable_context(ctx, domain);
145 if (ret != LTTNG_OK) {
146 goto error;
147 }
148 break;
149 }
150 case LTTNG_DOMAIN_UST:
151 break;
152 default:
a0377dfe 153 abort();
bdf64013 154 }
aa3514e9 155
6b79ed20
DG
156 /* Create ltt UST context */
157 uctx = trace_ust_create_context(ctx);
158 if (uctx == NULL) {
7682f304 159 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
6b79ed20
DG
160 goto error;
161 }
162
6b79ed20 163 /* Add ltt UST context node to ltt UST channel */
aa3514e9 164 lttng_ht_add_ulong(uchan->ctx, &uctx->node);
31746f93 165 cds_list_add_tail(&uctx->list, &uchan->ctx_list);
6b79ed20 166
88e3c2f5
JG
167 if (!usess->active) {
168 goto end;
169 }
170
171 ret = ust_app_add_ctx_channel_glb(usess, uchan, uctx);
172 if (ret < 0) {
173 goto error;
174 }
175end:
727d5404
DG
176 DBG("Context UST %d added to channel %s", uctx->ctx.ctx, uchan->name);
177
178 return 0;
6b79ed20
DG
179
180error:
181 free(uctx);
aa3514e9 182duplicate:
6b79ed20
DG
183 return ret;
184}
185
b579acd9
DG
186/*
187 * Add kernel context to tracer.
188 */
54d01ffb 189int context_kernel_add(struct ltt_kernel_session *ksession,
26e1c61f
JR
190 const struct lttng_event_context *ctx,
191 const char *channel_name)
b579acd9
DG
192{
193 int ret;
194 struct ltt_kernel_channel *kchan;
645328ae 195 struct ltt_kernel_context *kctx;
54d01ffb 196
a0377dfe
FD
197 LTTNG_ASSERT(ksession);
198 LTTNG_ASSERT(ctx);
199 LTTNG_ASSERT(channel_name);
0525e9ae 200
645328ae
DG
201 kctx = trace_kernel_create_context(NULL);
202 if (!kctx) {
7682f304 203 ret = -LTTNG_ERR_NOMEM;
645328ae
DG
204 goto error;
205 }
206
54d01ffb 207 /* Setup kernel context structure */
9197c5c4
MD
208 switch (ctx->ctx) {
209 case LTTNG_EVENT_CONTEXT_PID:
b8e2fb80 210 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PID;
9197c5c4 211 break;
9197c5c4 212 case LTTNG_EVENT_CONTEXT_PROCNAME:
b8e2fb80 213 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PROCNAME;
9197c5c4
MD
214 break;
215 case LTTNG_EVENT_CONTEXT_PRIO:
b8e2fb80 216 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PRIO;
9197c5c4
MD
217 break;
218 case LTTNG_EVENT_CONTEXT_NICE:
b8e2fb80 219 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_NICE;
9197c5c4
MD
220 break;
221 case LTTNG_EVENT_CONTEXT_VPID:
b8e2fb80 222 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VPID;
9197c5c4
MD
223 break;
224 case LTTNG_EVENT_CONTEXT_TID:
b8e2fb80 225 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_TID;
9197c5c4
MD
226 break;
227 case LTTNG_EVENT_CONTEXT_VTID:
b8e2fb80 228 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VTID;
9197c5c4
MD
229 break;
230 case LTTNG_EVENT_CONTEXT_PPID:
b8e2fb80 231 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PPID;
9197c5c4
MD
232 break;
233 case LTTNG_EVENT_CONTEXT_VPPID:
b8e2fb80 234 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VPPID;
9197c5c4 235 break;
54773d68 236 case LTTNG_EVENT_CONTEXT_HOSTNAME:
b8e2fb80 237 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME;
54773d68 238 break;
aa3514e9
MD
239 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER:
240 case LTTNG_EVENT_CONTEXT_PERF_COUNTER:
b8e2fb80 241 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PERF_CPU_COUNTER;
aa3514e9 242 break;
1ae5e83e 243 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE:
b8e2fb80 244 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE;
1ae5e83e
JD
245 break;
246 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE:
b8e2fb80 247 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE;
1ae5e83e
JD
248 break;
249 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE:
b8e2fb80 250 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE;
1ae5e83e
JD
251 break;
252 case LTTNG_EVENT_CONTEXT_MIGRATABLE:
b8e2fb80 253 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE;
1ae5e83e 254 break;
373148e9 255 case LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL:
b8e2fb80 256 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL;
373148e9
FG
257 break;
258 case LTTNG_EVENT_CONTEXT_CALLSTACK_USER:
b8e2fb80 259 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER;
373148e9 260 break;
40e14884 261 case LTTNG_EVENT_CONTEXT_CGROUP_NS:
b8e2fb80 262 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS;
40e14884
MJ
263 break;
264 case LTTNG_EVENT_CONTEXT_IPC_NS:
b8e2fb80 265 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_IPC_NS;
40e14884
MJ
266 break;
267 case LTTNG_EVENT_CONTEXT_MNT_NS:
b8e2fb80 268 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_MNT_NS;
40e14884
MJ
269 break;
270 case LTTNG_EVENT_CONTEXT_NET_NS:
b8e2fb80 271 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_NET_NS;
40e14884
MJ
272 break;
273 case LTTNG_EVENT_CONTEXT_PID_NS:
b8e2fb80 274 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_PID_NS;
40e14884 275 break;
d37ac3cd 276 case LTTNG_EVENT_CONTEXT_TIME_NS:
b8e2fb80 277 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_TIME_NS;
d37ac3cd 278 break;
40e14884 279 case LTTNG_EVENT_CONTEXT_USER_NS:
b8e2fb80 280 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_USER_NS;
40e14884
MJ
281 break;
282 case LTTNG_EVENT_CONTEXT_UTS_NS:
b8e2fb80 283 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_UTS_NS;
40e14884 284 break;
499cbfa1 285 case LTTNG_EVENT_CONTEXT_UID:
b8e2fb80 286 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_UID;
499cbfa1
MJ
287 break;
288 case LTTNG_EVENT_CONTEXT_EUID:
b8e2fb80 289 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_EUID;
499cbfa1
MJ
290 break;
291 case LTTNG_EVENT_CONTEXT_SUID:
b8e2fb80 292 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_SUID;
499cbfa1
MJ
293 break;
294 case LTTNG_EVENT_CONTEXT_GID:
b8e2fb80 295 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_GID;
499cbfa1
MJ
296 break;
297 case LTTNG_EVENT_CONTEXT_EGID:
b8e2fb80 298 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_EGID;
499cbfa1
MJ
299 break;
300 case LTTNG_EVENT_CONTEXT_SGID:
b8e2fb80 301 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_SGID;
499cbfa1
MJ
302 break;
303 case LTTNG_EVENT_CONTEXT_VUID:
b8e2fb80 304 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VUID;
499cbfa1
MJ
305 break;
306 case LTTNG_EVENT_CONTEXT_VEUID:
b8e2fb80 307 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VEUID;
499cbfa1
MJ
308 break;
309 case LTTNG_EVENT_CONTEXT_VSUID:
b8e2fb80 310 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VSUID;
499cbfa1
MJ
311 break;
312 case LTTNG_EVENT_CONTEXT_VGID:
b8e2fb80 313 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VGID;
499cbfa1
MJ
314 break;
315 case LTTNG_EVENT_CONTEXT_VEGID:
b8e2fb80 316 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VEGID;
499cbfa1
MJ
317 break;
318 case LTTNG_EVENT_CONTEXT_VSGID:
b8e2fb80 319 kctx->ctx.ctx = LTTNG_KERNEL_ABI_CONTEXT_VSGID;
499cbfa1 320 break;
9197c5c4 321 default:
fc7324e8
DG
322 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
323 goto error;
9197c5c4
MD
324 }
325
645328ae
DG
326 kctx->ctx.u.perf_counter.type = ctx->u.perf_counter.type;
327 kctx->ctx.u.perf_counter.config = ctx->u.perf_counter.config;
328 strncpy(kctx->ctx.u.perf_counter.name, ctx->u.perf_counter.name,
54d01ffb 329 LTTNG_SYMBOL_NAME_LEN);
645328ae 330 kctx->ctx.u.perf_counter.name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
b579acd9 331
9d035200 332 if (*channel_name == '\0') {
645328ae 333 ret = add_kctx_all_channels(ksession, kctx);
df3c77c8
JG
334 /* Ownership of kctx is transferred to the callee. */
335 kctx = NULL;
f73fabfd 336 if (ret != LTTNG_OK) {
b579acd9
DG
337 goto error;
338 }
339 } else {
340 /* Get kernel channel */
62499ad6 341 kchan = trace_kernel_get_channel_by_name(channel_name, ksession);
b579acd9 342 if (kchan == NULL) {
f73fabfd 343 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
b579acd9
DG
344 goto error;
345 }
346
645328ae 347 ret = add_kctx_to_channel(kctx, kchan);
df3c77c8
JG
348 /* Ownership of kctx is transferred to the callee. */
349 kctx = NULL;
f73fabfd 350 if (ret != LTTNG_OK) {
b579acd9
DG
351 goto error;
352 }
353 }
354
df3c77c8 355 ret = LTTNG_OK;
b579acd9
DG
356
357error:
fc7324e8
DG
358 if (kctx) {
359 trace_kernel_destroy_context(kctx);
360 }
b579acd9
DG
361 return ret;
362}
2bdd86d4
MD
363
364/*
55cc08a6 365 * Add UST context to tracer.
2bdd86d4 366 */
4ce7709b 367int context_ust_add(struct ltt_ust_session *usess,
df4f5a87
JG
368 enum lttng_domain_type domain,
369 const struct lttng_event_context *ctx,
26e1c61f 370 const char *channel_name)
2bdd86d4 371{
601d5acf 372 int ret = LTTNG_OK;
442359c0 373 struct lttng_ht_iter iter;
bec39940 374 struct lttng_ht *chan_ht;
55cc08a6 375 struct ltt_ust_channel *uchan = NULL;
2bdd86d4 376
a0377dfe
FD
377 LTTNG_ASSERT(usess);
378 LTTNG_ASSERT(ctx);
379 LTTNG_ASSERT(channel_name);
0525e9ae 380
2223c96f
DG
381 rcu_read_lock();
382
f5501dad 383 chan_ht = usess->domain_global.channels;
2bdd86d4 384
55cc08a6 385 /* Get UST channel if defined */
9f9ee9c9 386 if (channel_name[0] != '\0') {
55cc08a6
DG
387 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
388 if (uchan == NULL) {
f73fabfd 389 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
2bdd86d4
MD
390 goto error;
391 }
55cc08a6
DG
392 }
393
601d5acf
DG
394 if (uchan) {
395 /* Add ctx to channel */
6b79ed20 396 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
601d5acf 397 } else {
e7fe706f 398 rcu_read_lock();
601d5acf 399 /* Add ctx all events, all channels */
bec39940 400 cds_lfht_for_each_entry(chan_ht->ht, &iter.iter, uchan, node.node) {
6b79ed20 401 ret = add_uctx_to_channel(usess, domain, uchan, ctx);
7682f304 402 if (ret) {
9589be74
JG
403 ERR("Failed to add context to channel %s",
404 uchan->name);
55cc08a6
DG
405 continue;
406 }
2bdd86d4 407 }
e7fe706f 408 rcu_read_unlock();
55cc08a6 409 }
2bdd86d4 410
55cc08a6 411 switch (ret) {
7682f304 412 case LTTNG_ERR_UST_CONTEXT_EXIST:
727d5404 413 break;
55cc08a6 414 case -ENOMEM:
7682f304 415 case -LTTNG_ERR_NOMEM:
f73fabfd 416 ret = LTTNG_ERR_FATAL;
727d5404
DG
417 break;
418 case -EINVAL:
f73fabfd 419 ret = LTTNG_ERR_UST_CONTEXT_INVAL;
727d5404
DG
420 break;
421 case -ENOSYS:
f73fabfd 422 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
727d5404
DG
423 break;
424 default:
7682f304
JG
425 if (ret != 0 && ret != LTTNG_OK) {
426 ret = ret > 0 ? ret : LTTNG_ERR_UNK;
427 } else {
428 ret = LTTNG_OK;
429 }
727d5404 430 break;
2bdd86d4
MD
431 }
432
2bdd86d4 433error:
2223c96f 434 rcu_read_unlock();
2bdd86d4
MD
435 return ret;
436}
This page took 0.0948 seconds and 4 git commands to generate.