2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
14 #include <urcu/list.h>
16 #include <common/error.h>
17 #include <common/sessiond-comm/sessiond-comm.h>
22 #include "trace-ust.h"
26 * Add kernel context to all channel.
28 * Assumes the ownership of kctx.
30 static int add_kctx_all_channels(struct ltt_kernel_session
*ksession
,
31 struct ltt_kernel_context
*kctx
)
34 struct ltt_kernel_channel
*kchan
;
39 DBG("Adding kernel context to all channels");
41 /* Go over all channels */
42 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
43 struct ltt_kernel_context
*kctx_copy
;
45 kctx_copy
= trace_kernel_copy_context(kctx
);
47 PERROR("zmalloc ltt_kernel_context");
48 ret
= -LTTNG_ERR_NOMEM
;
52 /* Ownership of kctx_copy is transferred to the callee. */
53 ret
= kernel_add_channel_context(kchan
, kctx_copy
);
63 trace_kernel_destroy_context(kctx
);
68 * Add kernel context to a specific channel.
70 * Assumes the ownership of kctx.
72 static int add_kctx_to_channel(struct ltt_kernel_context
*kctx
,
73 struct ltt_kernel_channel
*kchan
)
80 DBG("Add kernel context to channel '%s'", kchan
->channel
->name
);
82 /* Ownership of kctx is transferred to the callee. */
83 ret
= kernel_add_channel_context(kchan
, kctx
);
96 * Add UST context to channel.
98 static int add_uctx_to_channel(struct ltt_ust_session
*usess
,
99 enum lttng_domain_type domain
,
100 struct ltt_ust_channel
*uchan
,
101 const struct lttng_event_context
*ctx
)
104 struct ltt_ust_context
*uctx
= NULL
;
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
)) {
113 ret
= LTTNG_ERR_UST_CONTEXT_EXIST
;
120 case LTTNG_DOMAIN_JUL
:
121 case LTTNG_DOMAIN_LOG4J
:
125 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
126 /* Other contexts are not needed by the agent. */
129 agt
= trace_ust_find_agent(usess
, domain
);
132 agt
= agent_create(domain
);
134 ret
= -LTTNG_ERR_NOMEM
;
137 agent_add(agt
, usess
->agents
);
139 ret
= agent_add_context(ctx
, agt
);
140 if (ret
!= LTTNG_OK
) {
144 ret
= agent_enable_context(ctx
, domain
);
145 if (ret
!= LTTNG_OK
) {
150 case LTTNG_DOMAIN_UST
:
156 /* Create ltt UST context */
157 uctx
= trace_ust_create_context(ctx
);
159 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
163 /* Add ltt UST context node to ltt UST channel */
164 lttng_ht_add_ulong(uchan
->ctx
, &uctx
->node
);
165 cds_list_add_tail(&uctx
->list
, &uchan
->ctx_list
);
167 if (!usess
->active
) {
171 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
176 DBG("Context UST %d added to channel %s", uctx
->ctx
.ctx
, uchan
->name
);
187 * Add kernel context to tracer.
189 int context_kernel_add(struct ltt_kernel_session
*ksession
,
190 const struct lttng_event_context
*ctx
, char *channel_name
)
193 struct ltt_kernel_channel
*kchan
;
194 struct ltt_kernel_context
*kctx
;
198 assert(channel_name
);
200 kctx
= trace_kernel_create_context(NULL
);
202 ret
= -LTTNG_ERR_NOMEM
;
206 /* Setup kernel context structure */
208 case LTTNG_EVENT_CONTEXT_PID
:
209 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PID
;
211 case LTTNG_EVENT_CONTEXT_PROCNAME
:
212 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PROCNAME
;
214 case LTTNG_EVENT_CONTEXT_PRIO
:
215 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PRIO
;
217 case LTTNG_EVENT_CONTEXT_NICE
:
218 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_NICE
;
220 case LTTNG_EVENT_CONTEXT_VPID
:
221 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VPID
;
223 case LTTNG_EVENT_CONTEXT_TID
:
224 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_TID
;
226 case LTTNG_EVENT_CONTEXT_VTID
:
227 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VTID
;
229 case LTTNG_EVENT_CONTEXT_PPID
:
230 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PPID
;
232 case LTTNG_EVENT_CONTEXT_VPPID
:
233 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VPPID
;
235 case LTTNG_EVENT_CONTEXT_HOSTNAME
:
236 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_HOSTNAME
;
238 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
239 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
240 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PERF_CPU_COUNTER
;
242 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
:
243 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_INTERRUPTIBLE
;
245 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE
:
246 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PREEMPTIBLE
;
248 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
:
249 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_NEED_RESCHEDULE
;
251 case LTTNG_EVENT_CONTEXT_MIGRATABLE
:
252 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_MIGRATABLE
;
254 case LTTNG_EVENT_CONTEXT_CALLSTACK_KERNEL
:
255 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_KERNEL
;
257 case LTTNG_EVENT_CONTEXT_CALLSTACK_USER
:
258 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_CALLSTACK_USER
;
260 case LTTNG_EVENT_CONTEXT_CGROUP_NS
:
261 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_CGROUP_NS
;
263 case LTTNG_EVENT_CONTEXT_IPC_NS
:
264 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_IPC_NS
;
266 case LTTNG_EVENT_CONTEXT_MNT_NS
:
267 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_MNT_NS
;
269 case LTTNG_EVENT_CONTEXT_NET_NS
:
270 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_NET_NS
;
272 case LTTNG_EVENT_CONTEXT_PID_NS
:
273 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_PID_NS
;
275 case LTTNG_EVENT_CONTEXT_TIME_NS
:
276 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_TIME_NS
;
278 case LTTNG_EVENT_CONTEXT_USER_NS
:
279 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_USER_NS
;
281 case LTTNG_EVENT_CONTEXT_UTS_NS
:
282 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_UTS_NS
;
284 case LTTNG_EVENT_CONTEXT_UID
:
285 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_UID
;
287 case LTTNG_EVENT_CONTEXT_EUID
:
288 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_EUID
;
290 case LTTNG_EVENT_CONTEXT_SUID
:
291 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_SUID
;
293 case LTTNG_EVENT_CONTEXT_GID
:
294 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_GID
;
296 case LTTNG_EVENT_CONTEXT_EGID
:
297 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_EGID
;
299 case LTTNG_EVENT_CONTEXT_SGID
:
300 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_SGID
;
302 case LTTNG_EVENT_CONTEXT_VUID
:
303 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VUID
;
305 case LTTNG_EVENT_CONTEXT_VEUID
:
306 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VEUID
;
308 case LTTNG_EVENT_CONTEXT_VSUID
:
309 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VSUID
;
311 case LTTNG_EVENT_CONTEXT_VGID
:
312 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VGID
;
314 case LTTNG_EVENT_CONTEXT_VEGID
:
315 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VEGID
;
317 case LTTNG_EVENT_CONTEXT_VSGID
:
318 kctx
->ctx
.ctx
= LTTNG_KERNEL_ABI_CONTEXT_VSGID
;
321 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
325 kctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
326 kctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
327 strncpy(kctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
328 LTTNG_SYMBOL_NAME_LEN
);
329 kctx
->ctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
331 if (*channel_name
== '\0') {
332 ret
= add_kctx_all_channels(ksession
, kctx
);
333 /* Ownership of kctx is transferred to the callee. */
335 if (ret
!= LTTNG_OK
) {
339 /* Get kernel channel */
340 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
342 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
346 ret
= add_kctx_to_channel(kctx
, kchan
);
347 /* Ownership of kctx is transferred to the callee. */
349 if (ret
!= LTTNG_OK
) {
358 trace_kernel_destroy_context(kctx
);
364 * Add UST context to tracer.
366 int context_ust_add(struct ltt_ust_session
*usess
,
367 enum lttng_domain_type domain
,
368 const struct lttng_event_context
*ctx
,
372 struct lttng_ht_iter iter
;
373 struct lttng_ht
*chan_ht
;
374 struct ltt_ust_channel
*uchan
= NULL
;
378 assert(channel_name
);
382 chan_ht
= usess
->domain_global
.channels
;
384 /* Get UST channel if defined */
385 if (channel_name
[0] != '\0') {
386 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
388 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
394 /* Add ctx to channel */
395 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
398 /* Add ctx all events, all channels */
399 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
400 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
402 ERR("Failed to add context to channel %s",
411 case LTTNG_ERR_UST_CONTEXT_EXIST
:
414 case -LTTNG_ERR_NOMEM
:
415 ret
= LTTNG_ERR_FATAL
;
418 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
421 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
424 if (ret
!= 0 && ret
!= LTTNG_OK
) {
425 ret
= ret
> 0 ? ret
: LTTNG_ERR_UNK
;