2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <urcu/list.h>
26 #include <common/error.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
32 #include "trace-ust.h"
36 * Add kernel context to all channel.
38 * Assumes the ownership of kctx.
40 static int add_kctx_all_channels(struct ltt_kernel_session
*ksession
,
41 struct ltt_kernel_context
*kctx
)
44 struct ltt_kernel_channel
*kchan
;
49 DBG("Adding kernel context to all channels");
51 /* Go over all channels */
52 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
53 struct ltt_kernel_context
*kctx_copy
;
55 kctx_copy
= trace_kernel_copy_context(kctx
);
57 PERROR("zmalloc ltt_kernel_context");
58 ret
= -LTTNG_ERR_NOMEM
;
62 /* Ownership of kctx_copy is transferred to the callee. */
63 ret
= kernel_add_channel_context(kchan
, kctx_copy
);
73 trace_kernel_destroy_context(kctx
);
78 * Add kernel context to a specific channel.
80 * Assumes the ownership of kctx.
82 static int add_kctx_to_channel(struct ltt_kernel_context
*kctx
,
83 struct ltt_kernel_channel
*kchan
)
90 DBG("Add kernel context to channel '%s'", kchan
->channel
->name
);
92 /* Ownership of kctx is transferred to the callee. */
93 ret
= kernel_add_channel_context(kchan
, kctx
);
106 * Add UST context to channel.
108 static int add_uctx_to_channel(struct ltt_ust_session
*usess
,
109 enum lttng_domain_type domain
,
110 struct ltt_ust_channel
*uchan
, struct lttng_event_context
*ctx
)
113 struct ltt_ust_context
*uctx
= NULL
;
119 /* Check if context is duplicate */
120 cds_list_for_each_entry(uctx
, &uchan
->ctx_list
, list
) {
121 if (trace_ust_match_context(uctx
, ctx
)) {
122 ret
= LTTNG_ERR_UST_CONTEXT_EXIST
;
129 case LTTNG_DOMAIN_JUL
:
130 case LTTNG_DOMAIN_LOG4J
:
134 if (ctx
->ctx
!= LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
135 /* Other contexts are not needed by the agent. */
138 agt
= trace_ust_find_agent(usess
, domain
);
141 agt
= agent_create(domain
);
143 ret
= -LTTNG_ERR_NOMEM
;
146 agent_add(agt
, usess
->agents
);
148 ret
= agent_add_context(ctx
, agt
);
149 if (ret
!= LTTNG_OK
) {
153 ret
= agent_enable_context(ctx
, domain
);
154 if (ret
!= LTTNG_OK
) {
159 case LTTNG_DOMAIN_UST
:
165 /* Create ltt UST context */
166 uctx
= trace_ust_create_context(ctx
);
168 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
172 ret
= ust_app_add_ctx_channel_glb(usess
, uchan
, uctx
);
179 /* Add ltt UST context node to ltt UST channel */
180 lttng_ht_add_ulong(uchan
->ctx
, &uctx
->node
);
182 cds_list_add_tail(&uctx
->list
, &uchan
->ctx_list
);
184 DBG("Context UST %d added to channel %s", uctx
->ctx
.ctx
, uchan
->name
);
195 * Add kernel context to tracer.
197 int context_kernel_add(struct ltt_kernel_session
*ksession
,
198 struct lttng_event_context
*ctx
, char *channel_name
)
201 struct ltt_kernel_channel
*kchan
;
202 struct ltt_kernel_context
*kctx
;
206 assert(channel_name
);
208 kctx
= trace_kernel_create_context(NULL
);
210 ret
= -LTTNG_ERR_NOMEM
;
214 /* Setup kernel context structure */
216 case LTTNG_EVENT_CONTEXT_PID
:
217 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PID
;
219 case LTTNG_EVENT_CONTEXT_PROCNAME
:
220 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PROCNAME
;
222 case LTTNG_EVENT_CONTEXT_PRIO
:
223 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PRIO
;
225 case LTTNG_EVENT_CONTEXT_NICE
:
226 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_NICE
;
228 case LTTNG_EVENT_CONTEXT_VPID
:
229 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPID
;
231 case LTTNG_EVENT_CONTEXT_TID
:
232 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_TID
;
234 case LTTNG_EVENT_CONTEXT_VTID
:
235 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VTID
;
237 case LTTNG_EVENT_CONTEXT_PPID
:
238 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PPID
;
240 case LTTNG_EVENT_CONTEXT_VPPID
:
241 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_VPPID
;
243 case LTTNG_EVENT_CONTEXT_HOSTNAME
:
244 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_HOSTNAME
;
246 case LTTNG_EVENT_CONTEXT_PERF_CPU_COUNTER
:
247 case LTTNG_EVENT_CONTEXT_PERF_COUNTER
:
248 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PERF_CPU_COUNTER
;
250 case LTTNG_EVENT_CONTEXT_INTERRUPTIBLE
:
251 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_INTERRUPTIBLE
;
253 case LTTNG_EVENT_CONTEXT_PREEMPTIBLE
:
254 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_PREEMPTIBLE
;
256 case LTTNG_EVENT_CONTEXT_NEED_RESCHEDULE
:
257 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_NEED_RESCHEDULE
;
259 case LTTNG_EVENT_CONTEXT_MIGRATABLE
:
260 kctx
->ctx
.ctx
= LTTNG_KERNEL_CONTEXT_MIGRATABLE
;
263 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
267 kctx
->ctx
.u
.perf_counter
.type
= ctx
->u
.perf_counter
.type
;
268 kctx
->ctx
.u
.perf_counter
.config
= ctx
->u
.perf_counter
.config
;
269 strncpy(kctx
->ctx
.u
.perf_counter
.name
, ctx
->u
.perf_counter
.name
,
270 LTTNG_SYMBOL_NAME_LEN
);
271 kctx
->ctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
273 if (*channel_name
== '\0') {
274 ret
= add_kctx_all_channels(ksession
, kctx
);
275 /* Ownership of kctx is transferred to the callee. */
277 if (ret
!= LTTNG_OK
) {
281 /* Get kernel channel */
282 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksession
);
284 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
288 ret
= add_kctx_to_channel(kctx
, kchan
);
289 /* Ownership of kctx is transferred to the callee. */
291 if (ret
!= LTTNG_OK
) {
300 trace_kernel_destroy_context(kctx
);
306 * Add UST context to tracer.
308 int context_ust_add(struct ltt_ust_session
*usess
,
309 enum lttng_domain_type domain
, struct lttng_event_context
*ctx
,
313 struct lttng_ht_iter iter
;
314 struct lttng_ht
*chan_ht
;
315 struct ltt_ust_channel
*uchan
= NULL
;
319 assert(channel_name
);
323 chan_ht
= usess
->domain_global
.channels
;
325 /* Get UST channel if defined */
326 if (channel_name
[0] != '\0') {
327 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
329 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
335 /* Add ctx to channel */
336 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
339 /* Add ctx all events, all channels */
340 cds_lfht_for_each_entry(chan_ht
->ht
, &iter
.iter
, uchan
, node
.node
) {
341 ret
= add_uctx_to_channel(usess
, domain
, uchan
, ctx
);
343 ERR("Failed to add context to channel %s",
352 case LTTNG_ERR_UST_CONTEXT_EXIST
:
355 case -LTTNG_ERR_NOMEM
:
356 ret
= LTTNG_ERR_FATAL
;
359 ret
= LTTNG_ERR_UST_CONTEXT_INVAL
;
362 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
365 if (ret
!= 0 && ret
!= LTTNG_OK
) {
366 ret
= ret
> 0 ? ret
: LTTNG_ERR_UNK
;